public static BigDynamicSite <T0, Tret> Create(DynamicAction action)
 {
     return(new BigDynamicSite <T0, Tret>(action));
 }
Example #2
0
        public virtual Statement MakeInvalidParametersError(MethodBinder binder, DynamicAction action, CallType callType, IList <MethodBase> targets, StandardRule rule, object [] args)
        {
            int  minArgs = Int32.MaxValue;
            int  maxArgs = Int32.MinValue;
            int  maxDflt = Int32.MinValue;
            int  argsProvided = args.Length - 1; // -1 to remove the object we're calling
            bool hasArgList = false, hasNamedArgument = false;
            Dictionary <string, bool> namedArgs = new Dictionary <string, bool>();

            CallAction ca = action as CallAction;

            if (ca != null)
            {
                hasNamedArgument = ca.Signature.HasNamedArgument();
                int dictArgIndex = ca.Signature.IndexOf(ArgumentKind.Dictionary);

                if (dictArgIndex > -1)
                {
                    argsProvided--;
                    IAttributesCollection iac = args[dictArgIndex + 1] as IAttributesCollection;
                    if (iac != null)
                    {
                        foreach (KeyValuePair <object, object> kvp in iac)
                        {
                            namedArgs[(string)kvp.Key] = false;
                        }
                    }
                }

                argsProvided += GetParamsArgumentCountAdjust(ca, args);
                foreach (SymbolId si in ca.Signature.GetArgumentNames())
                {
                    namedArgs[SymbolTable.IdToString(si)] = false;
                }
            }
            else
            {
                maxArgs = minArgs = rule.Parameters.Length;
                maxDflt = 0;
            }

            foreach (MethodBase mb in targets)
            {
                if (callType == CallType.ImplicitInstance && CompilerHelpers.IsStatic(mb))
                {
                    continue;
                }

                ParameterInfo[] pis  = mb.GetParameters();
                int             cnt  = pis.Length;
                int             dflt = 0;

                if (!CompilerHelpers.IsStatic(mb) && callType == CallType.None)
                {
                    cnt++;
                }

                foreach (ParameterInfo pi in pis)
                {
                    if (pi.ParameterType == typeof(CodeContext))
                    {
                        cnt--;
                    }
                    else if (CompilerHelpers.IsParamArray(pi))
                    {
                        cnt--;
                        hasArgList = true;
                    }
                    else if (CompilerHelpers.IsParamDictionary(pi))
                    {
                        cnt--;
                    }
                    else if (!CompilerHelpers.IsMandatoryParameter(pi))
                    {
                        dflt++;
                        cnt--;
                    }

                    namedArgs[pi.Name] = true;
                }

                minArgs = System.Math.Min(cnt, minArgs);
                maxArgs = System.Math.Max(cnt, maxArgs);
                maxDflt = System.Math.Max(dflt, maxDflt);
            }

            foreach (KeyValuePair <string, bool> kvp in namedArgs)
            {
                if (kvp.Value == false)
                {
                    // unbound named argument.
                    return(rule.MakeError(
                               Ast.Ast.Call(
                                   typeof(RuntimeHelpers).GetMethod("TypeErrorForExtraKeywordArgument"),
                                   Ast.Ast.Constant(binder._name, typeof(string)),
                                   Ast.Ast.Constant(kvp.Key, typeof(string))
                                   )
                               ));
                }
            }

            return(rule.MakeError(
                       Ast.Ast.Call(
                           typeof(RuntimeHelpers).GetMethod("TypeErrorForIncorrectArgumentCount", new Type[] {
                typeof(string), typeof(int), typeof(int), typeof(int), typeof(int), typeof(bool), typeof(bool)
            }),
                           Ast.Ast.Constant(binder._name, typeof(string)), // name
                           Ast.Ast.Constant(minArgs),                      // min formal normal arg cnt
                           Ast.Ast.Constant(maxArgs),                      // max formal normal arg cnt
                           Ast.Ast.Constant(maxDflt),                      // default cnt
                           Ast.Ast.Constant(argsProvided),                 // args provided
                           Ast.Ast.Constant(hasArgList),                   // hasArgList
                           Ast.Ast.Constant(hasNamedArgument)              // kwargs provided
                           )
                       ));
        }
 internal BigDynamicSite(DynamicAction action)
     : base(action)
 {
     this._rules  = RuleSet <BigDynamicSiteTarget <T0, Tret> > .EmptyRules;
     this._target = this._rules.GetOrMakeTarget(null);
 }
Example #4
0
 public virtual AbstractValue AbstractExecute(DynamicAction action, IList <AbstractValue> args)
 {
     throw new NotImplementedException();
 }
Example #5
0
        /// <summary>
        /// Produces a rule for the specified Action for the given arguments.
        ///
        /// The default implementation can produce rules for standard .NET types.  Languages should
        /// override this and provide any custom behavior they need and fallback to the default
        /// implementation if no custom behavior is required.
        /// </summary>
        /// <typeparam name="T">The type of the DynamicSite the rule is being produced for.</typeparam>
        /// <param name="action">The Action that is being performed.</param>
        /// <param name="args">The arguments to the action as provided from the call site at runtime.</param>
        /// <param name="callerContext">The CodeContext that is requesting the rule and should be use</param>
        /// <returns></returns>
        protected virtual StandardRule <T> MakeRule <T>(CodeContext /*!*/ callerContext, DynamicAction /*!*/ action, object[] /*!*/ args)
        {
            Contract.RequiresNotNull(callerContext, "callerContext");
            Contract.RequiresNotNull(action, "action");
            Contract.RequiresNotNull(args, "args");

            switch (action.Kind)
            {
            case DynamicActionKind.Call:
                return(new CallBinderHelper <T, CallAction>(callerContext, (CallAction)action, args).MakeRule());

            case DynamicActionKind.GetMember:
                return(new GetMemberBinderHelper <T>(callerContext, (GetMemberAction)action, args).MakeNewRule());

            case DynamicActionKind.SetMember:
                return(new SetMemberBinderHelper <T>(callerContext, (SetMemberAction)action, args).MakeNewRule());

            case DynamicActionKind.CreateInstance:
                return(new CreateInstanceBinderHelper <T>(callerContext, (CreateInstanceAction)action, args).MakeRule());

            case DynamicActionKind.DoOperation:
                return(new DoOperationBinderHelper <T>(callerContext, (DoOperationAction)action, args).MakeRule());

            case DynamicActionKind.DeleteMember:
                return(new DeleteMemberBinderHelper <T>(callerContext, (DeleteMemberAction)action, args).MakeRule());

            case DynamicActionKind.InvokeMember:
                return(new InvokeMemberBinderHelper <T>(callerContext, (InvokeMemberAction)action, args).MakeRule());

            case DynamicActionKind.ConvertTo:
                return(new ConvertToBinderHelper <T>(callerContext, (ConvertToAction)action, args).MakeRule());

            default:
                throw new NotImplementedException(action.ToString());
            }
        }
Example #6
0
 protected FastDynamicSite(CodeContext context, DynamicAction action) {
     this._context = context;
     this._action = action;
 }
Example #7
0
 protected FastDynamicSite(CodeContext context, DynamicAction action)
 {
     this._context = context;
     this._action  = action;
 }
 private static void ValidateAction(DynamicAction action, IList<Expression> arguments) {
     switch (action.Kind) {
         case DynamicActionKind.DoOperation:
             // TODO: ValidateDoOperationAction((DoOperationAction)action, arguments, result);
             break;
         case DynamicActionKind.ConvertTo:
             Contract.Requires(arguments.Count == 1, "arguments", "One argument required for convert action");
             break;
         case DynamicActionKind.GetMember:
             Contract.Requires(arguments.Count == 1, "arguments", "One argument required for get member action");
             break;
         case DynamicActionKind.SetMember:
             Contract.Requires(arguments.Count == 2, "arguments", "Two arguments required for set member action");
             break;
         case DynamicActionKind.DeleteMember:
             Contract.Requires(arguments.Count == 1, "arguments", "One argument required for delete member action");
             break;
         case DynamicActionKind.InvokeMember:
         case DynamicActionKind.Call:
         case DynamicActionKind.CreateInstance:
             break;
         default:
             throw new ArgumentException("Invalid action kind", "action");
     }
 }
 internal ActionExpression(DynamicAction /*!*/ action, ReadOnlyCollection<Expression> /*!*/ arguments, Type /*!*/ result)
     : base(AstNodeType.ActionExpression) {
     _action = action;
     _arguments = arguments;
     _result = result;
 }
Example #10
0
        internal void AddRule <T>(DynamicAction action, object[] args, StandardRule <T> rule)
        {
            ActionRuleCache actionRuleCache = FindActionRuleCache(action);

            actionRuleCache.AddRule <T>(args, rule);
        }
            internal static ActionExpression ActionExpression(DynamicAction action, IList<Expression> arguments, Type result) {
                Contract.RequiresNotNull(action, "action");
                Contract.RequiresNotNullItems(arguments, "arguments");
                Contract.RequiresNotNull(result, "result");

                ValidateAction(action, arguments);

                return new ActionExpression(action, CollectionUtils.ToReadOnlyCollection(arguments), result);
            }
Example #12
0
        internal StandardRule <T> ExecuteRuleAndUpdateSite <T>(CodeContext callerContext, DynamicAction action, object[] args, object site, ref T target, ref RuleSet <T> rules, out object result)
        {
            ActionRuleCache actionRuleCache = FindActionRuleCache(action);

            return(actionRuleCache.ExecuteRuleAndUpdateSite <T>(callerContext, args, site, ref target, ref rules, out result));
        }
Example #13
0
        internal StandardRule <T> FindRule <T>(CodeContext callerContext, DynamicAction action, object[] args)
        {
            ActionRuleCache actionRuleCache = FindActionRuleCache(action);

            return(actionRuleCache.FindRule <T>(callerContext, args));
        }
Example #14
0
 protected DynamicSite(DynamicAction action)
 {
     this._action = action;
 }
 internal FastDynamicSite(CodeContext context, DynamicAction action)
     : base(context, action)
 {
     this._rules  = RuleSet <FastDynamicSiteTarget <T0, Tret> > .EmptyRules;
     this._target = this._rules.GetOrMakeTarget(null);
 }
Example #16
0
 private static void NoteRuleCreation(DynamicAction action, object[] args)
 {
     PerfTrack.NoteEvent(PerfTrack.Categories.Rules, "MakeRule " + action.ToString() + " " + CompilerHelpers.GetType(args[0]).Name);
 }
 public static FastDynamicSite <T0, Tret> Create(CodeContext context, DynamicAction action)
 {
     return(new FastDynamicSite <T0, Tret>(context, action));
 }
Example #18
0
 /// <summary>
 /// Gets a rule for the provided action and arguments and executes it without compiling.
 /// </summary>
 public object Execute(CodeContext cc, DynamicAction action, object[] args)
 {
     return(DynamicSiteHelpers.Execute(cc, this, action, args));
 }
Example #19
0
 protected DynamicSite(DynamicAction action) {
     this._action = action;
 }
        public static object Execute(CodeContext context, ActionBinder binder, DynamicAction action, params object[] args)
        {
            switch (args.Length)
            {
            case 1:
                return(binder.ExecuteRule <DynamicSiteTarget <object, object> >(context, action, args));

            case 2:
                return(binder.ExecuteRule <DynamicSiteTarget <object, object, object> >(context, action, args));

            case 3:
                return(binder.ExecuteRule <DynamicSiteTarget <object, object, object, object> >(context, action, args));

            case 4:
                return(binder.ExecuteRule <DynamicSiteTarget <object, object, object, object, object> >(context, action, args));

            case 5:
                return(binder.ExecuteRule <DynamicSiteTarget <object, object, object, object, object, object> >(context, action, args));

            case 6:
                return(binder.ExecuteRule <DynamicSiteTarget <object, object, object, object, object, object, object> >(context, action, args));

            default:
                //TODO: use CompilerHelpers.GetTypes(args) instead?
                Type       tupleType  = Tuple.MakeTupleType(CompilerHelpers.MakeRepeatedArray <Type>(typeof(object), args.Length));
                Type       targetType = typeof(BigDynamicSiteTarget <,>).MakeGenericType(tupleType, typeof(object));
                Type       ruleType   = typeof(StandardRule <>).MakeGenericType(targetType);
                MethodInfo getRule    = typeof(ActionBinder).GetMethod("GetRule").MakeGenericMethod(targetType);
                while (true)
                {
                    object         ruleN     = getRule.Invoke(binder, new object[] { context, action, args });
                    Ast.Expression test      = (Ast.Expression)ruleType.GetProperty("Test").GetValue(ruleN, null);
                    Ast.Statement  target    = (Ast.Statement)ruleType.GetProperty("Target").GetValue(ruleN, null);
                    Ast.Variable[] paramVars = (Ast.Variable[])ruleType.GetProperty("ParamVariables",
                                                                                    BindingFlags.Instance | BindingFlags.NonPublic).GetValue(ruleN, null);
                    Ast.Variable[] tempVars = (Ast.Variable[])ruleType.GetProperty("TemporaryVariables",
                                                                                   BindingFlags.Instance | BindingFlags.NonPublic).GetValue(ruleN, null);


                    Tuple       t      = Tuple.MakeTuple(tupleType, args);
                    object[]    tupArg = new object[] { t };
                    CodeContext tmpCtx = context.Scope.GetTemporaryVariableContext(context, paramVars, tupArg);
                    try {
                        bool result = (bool)test.Evaluate(tmpCtx);
                        if (!result)
                        {
                            // The test may evaluate as false if:
                            // 1. The rule was generated as invalid. In this case, the language binder should be fixed to avoid
                            //    generating invalid rules.
                            // 2. The rule was invalidated in the small window between calling GetRule and Evaluate. This is a
                            //    valid scenario. In such a case, we need to call Evaluate again to ensure that all expected
                            //    side-effects are visible to Execute below.
                            // This assert is not valid in the face to #2 above. However, it is left here until all issues in
                            // the interpreter and the language binders are flushed out
                            Debug.Assert(result);
                            continue;
                        }

                        return(target.Execute(tmpCtx));
                    } finally {
                        tmpCtx.Scope.TemporaryStorage.Clear();
                    }
                }
            }
        }