Beispiel #1
0
 public FuncDef(AsyncFn func, int minArity, int maxArity, string name)
 {
     this.func         = func;
     kind              = FuncKind.AsyncFn;
     this.minArity     = minArity;
     this.maxArity     = maxArity;
     this.flags        = FuncFlags.Defaults;
     flagsArgCanBeLazy = flagsArgCanBeVector = 0xFFFFFFFF;
     this.name         = name;
 }
Beispiel #2
0
 internal RuntimeActionFunc(ActionContext context, DefineFunction2Action info)
 {
     _flags = info.Flags;
     _registerCount = info.NumRegisters;
     _parameters = info.Params;
     _code = info.Actions;
     _context = new ActionContext
     {
         Constants = context.Constants,
         DefaultTarget = context.DefaultTarget,
         RootClip = context.RootClip,
         Scope = new LinkedList<ActionObject>(context.Scope)
     };
 }
Beispiel #3
0
 public FuncDef(Delegate func, string name,
                int minArity, int maxArity, ValueInfo[] argsInfo, ValueInfo[] resultsInfo,
                FuncFlags flags                        = FuncFlags.Defaults,
                uint flagsArgCanBeLazy                 = 0, uint flagsArgCanBeVector = 0,
                TimeSpan cachingExpiration             = default(TimeSpan), string cacheSubdomain = null,
                IDictionary <string, object> xtraAttrs = null)
 {
     kind = KindOf(func);
     System.Diagnostics.Trace.Assert(kind != FuncKind.Other);
     this.func                = func;
     this.name                = name ?? func.Method.Name;
     this.minArity            = minArity;
     this.maxArity            = maxArity;
     this.argsInfo            = argsInfo;
     this.resultsInfo         = resultsInfo;
     this.flags               = flags;
     this.flagsArgCanBeLazy   = flagsArgCanBeLazy;
     this.flagsArgCanBeVector = flagsArgCanBeVector;
     this.cachingExpiration   = cachingExpiration;
     this.cacheSubdomain      = cacheSubdomain;
     this.xtraAttrs           = xtraAttrs;
 }
Beispiel #4
0
        internal FuncDef(Delegate def, string name)
        {
            this.name = name;
            kind      = KindOf(def);
            System.Diagnostics.Trace.Assert(kind != FuncKind.Other);
            func = def;
            MethodInfo method = def.Method;

            if (method.GetCustomAttributes(typeof(IsNotPureAttribute), true).Length > 0)
            {
                flags |= FuncFlags.isNotPure;
            }
            if (method.GetCustomAttributes(typeof(CanBeLazyAttribute), false).Length > 0)
            {
                flags |= FuncFlags.resultCanBeLazy;
            }
            if (method.GetCustomAttributes(typeof(CanBeVectorAttribute), false).Length > 0)
            {
                flags |= FuncFlags.resultCanBeVector;
            }
            {
                var attrs = method.GetCustomAttributes(typeof(CacheableAttribute), false);
                if (attrs.Length > 0)
                {
                    var ca = (CacheableAttribute)attrs[0];
                    cachingExpiration = ca.expiration;
                    cacheSubdomain    = ca.subdomain;
                }
            }
            ParameterInfo[] prms = method.GetParameters();
            int             n    = prms.Length;

            System.Diagnostics.Trace.Assert(n < 32, "Functions with more than 32 arguments is not supported");
            flagsArgCanBeLazy   = 0;
            flagsArgCanBeVector = 0;
            bool aritySpecified;

            if (kind == FuncKind.AsyncFn || kind == FuncKind.Macro || kind == FuncKind.Fn)
            {
                object[] tmp = method.GetCustomAttributes(typeof(ArityAttribute), false);
                if (tmp.Length > 0)
                {
                    ArityAttribute arity = (ArityAttribute)tmp[0];
                    minArity       = arity.minArity;
                    maxArity       = arity.maxArity;
                    aritySpecified = true;
                }
                else
                {
                    minArity = 0; maxArity = int.MaxValue; aritySpecified = false;
                }
            }
            else
            {
                minArity = maxArity = n; aritySpecified = true;
            }

            #region Determine attributes of parameters

            var valueInfos   = new Dictionary <int, ValueInfo>(n);
            int maxInfoIndex = -1;
            foreach (ArgumentInfoAttribute a in method.GetCustomAttributes(typeof(ArgumentInfoAttribute), false))
            {
                int i;
                if (a.index >= 0)
                {
                    i = a.index;
                    if (a.index > maxInfoIndex)
                    {
                        maxInfoIndex = a.index;
                    }
                }
                else
                {
                    maxInfoIndex++;
                    i = maxInfoIndex;
                }
                System.Diagnostics.Trace.Assert(i < maxArity || kind == FuncKind.Fx, "Number of ArgumentInfoAttribute is greater than number of parameters for '" + method.Name + "'");
                valueInfos.Add(i, a.info);
            }
            for (int i = 0; i < n; i++)
            {
                ParameterInfo pi = prms[i];
                if (pi.GetCustomAttributes(typeof(CanBeLazyAttribute), false).Length > 0)
                {
                    flagsArgCanBeLazy |= (uint)(1 << i);
                }
                if (pi.GetCustomAttributes(typeof(CanBeVectorAttribute), false).Length > 0)
                {
                    flagsArgCanBeVector |= (uint)(1 << i);
                }
                var infos = pi.GetCustomAttributes(typeof(ParameterInfoAttribute), false);
                if (infos.Length > 0)
                {
                    System.Diagnostics.Trace.Assert(infos.Length == 1, "More than one ParameterInfoAttribute specified for parameter '" + pi.Name + "'");
                    if (maxInfoIndex < i)
                    {
                        maxInfoIndex = i;
                    }
                    valueInfos.Add(i, ((ArgumentInfoAttribute)infos[0]).info);
                }
            }
            if (maxInfoIndex >= 0)
            {
                argsInfo = new ValueInfo[maxInfoIndex + 1];
                foreach (var vi in valueInfos)
                {
                    argsInfo[vi.Key] = vi.Value;
                }
                if (!aritySpecified)
                {
                    minArity = maxArity = maxInfoIndex + 1;
                }
            }
            valueInfos.Clear();
            #endregion

            #region Determine attributes of result

            maxInfoIndex = -1;
            foreach (ResultInfoAttribute a in method.ReturnTypeCustomAttributes.GetCustomAttributes(typeof(ResultInfoAttribute), false))
            {
                int i = a.index;
                if (i > maxInfoIndex)
                {
                    maxInfoIndex = i;
                }
                valueInfos.Add(i, a.info);
            }
            if (maxInfoIndex >= 0)
            {
                resultsInfo = new ValueInfo[maxInfoIndex + 1];
                foreach (var vi in valueInfos)
                {
                    resultsInfo[vi.Key] = vi.Value;
                }
            }
            valueInfos.Clear();
            #endregion
        }