Ejemplo n.º 1
0
        internal Expression CreateCallSite(IBinderDefinition binder, Type delegateType, Type siteType, string nameSuggestion)
        {
            //string nameSuggestion = String.Format("{0}.{1}", this.CurrentMethodName, selector);
            string name = this.Client.Compiler.NativeGenerator.AsLegalMethodName(nameSuggestion);
            int    idx  = 0;

            while (this.DefinedCallSites.Any(def => def.Name == name))
            {
                name = this.Client.Compiler.NativeGenerator.AsLegalMethodName(String.Format("{0}${1}", nameSuggestion, idx++));
            }

            FieldBuilder field = this.CallSitesType.DefineField(name, siteType, FieldAttributes.Static | FieldAttributes.InitOnly | FieldAttributes.Assembly);

            this.DefinedCallSites.Add(new CallSiteDefinition(name, delegateType, siteType, field, binder));

            return(Expression.Field(null, field));

            //MethodInfo create = TypeUtilities.Method(siteType, "Create");
            //MethodInfo getCallSite = TypeUtilities.Method(typeof(NativeDynamicCallStrategy), "GetCallSite");

            //Expression binder = Expression.Call(null, getCallSite,
            //    Expression.Constant(selector, typeof(string)),
            //    Expression.Constant(nativeName, typeof(string)),
            //    Expression.Constant(isSuperSend, typeof(bool)),
            //    Expression.Constant(isConstantReceiver, typeof(bool)),
            //    Expression.Constant(superLookupScope, typeof(string)));
            //return Expression.Call(null, create, binder);
        }
Ejemplo n.º 2
0
 public CallSiteDefinition(string name, Type delegateType, Type siteType, FieldBuilder callSiteField, IBinderDefinition binder)
 {
     this.Name          = name;
     this.DelegateType  = delegateType;
     this.SiteType      = siteType;
     this.CallSiteField = callSiteField;
     this.Binder        = binder;
 }
        private Expression GenerateLiteralCallSite(EncoderVisitor visitor, IBinderDefinition binder, string nameSuggestion)
        {
            Type       delegateType = typeof(Func <CallSite, ExecutionContext, object>);
            Type       siteType     = typeof(CallSite <>).MakeGenericType(delegateType);
            Expression callSite     = this.CallSiteGenerator.CreateCallSite(binder, delegateType, siteType, nameSuggestion);

            List <Expression> args = new List <Expression>();

            args.Add(callSite);
            args.Add(visitor.Context.ExecutionContextArgument);

            FieldInfo  target = TypeUtilities.Field(siteType, "Target", BindingFlags.Instance | BindingFlags.Public);
            MethodInfo invoke = TypeUtilities.Method(delegateType, "Invoke");

            ParameterInfo[] pis = invoke.GetParameters();

            // siteExpr.Target.Invoke(siteExpr, *args)
            return(Expression.Call(
                       Expression.Field(callSite, target),
                       invoke,
                       args));

            /* C# Style
             * ParameterExpression site = Expression.Variable(siteType, "$site");
             * List<Expression> args = new List<Expression>();
             * args.Add(site);
             * args.Add(context.ExecutionContext);
             * args.AddRange(arguments);
             *
             * FieldInfo target = TypeUtilities.Field(siteType, "Target", BindingFlags.Instance | BindingFlags.Public);
             * MethodInfo invoke = TypeUtilities.Method(delegateType, "Invoke");
             * ParameterInfo[] pis = invoke.GetParameters();
             * // ($site = siteExpr).Target.Invoke($site, *args)
             * return Expression.Block(
             *  new[] { site },
             *  Expression.Call(
             *      Expression.Field(Expression.Assign(site, callSite), target),
             *      invoke,
             *      args));
             */
        }