public override T BindDelegate <T>(CallSite <T> site, object[] args)
        {
            IFastSettable fastSet = args[0] as IFastSettable;

            if (fastSet != null)
            {
                T res = fastSet.MakeSetBinding <T>(site, this);
                if (res != null)
                {
                    return(res);
                }
            }

            IPythonObject ipo = args[0] as IPythonObject;

            if (ipo != null && !(ipo is IProxyObject))
            {
                FastBindResult <T> res = UserTypeOps.MakeSetBinding <T>(Context.SharedContext, site, ipo, args[1], this);

                if (res.Target != null)
                {
                    PerfTrack.NoteEvent(PerfTrack.Categories.BindingFast, "IPythonObject");

                    if (res.ShouldCache)
                    {
                        CacheTarget(res.Target);
                    }
                    return(res.Target);
                }

                PerfTrack.NoteEvent(PerfTrack.Categories.BindingSlow, "IPythonObject Set");
            }

            return(base.BindDelegate(site, args));
        }
        public override DynamicMetaObject /*!*/ BindDeleteMember(DeleteMemberBinder /*!*/ member)
        {
            PerfTrack.NoteEvent(PerfTrack.Categories.Binding, "Type DeleteMember " + Value.UnderlyingSystemType.FullName);
            PerfTrack.NoteEvent(PerfTrack.Categories.BindingTarget, "Type DeleteMember");
            if (Value.IsSystemType)
            {
                PythonContext state = PythonContext.GetPythonContext(member);

                MemberTracker tt = MemberTracker.FromMemberInfo(Value.UnderlyingSystemType);

                // have the default binder perform it's operation against a TypeTracker and then
                // replace the test w/ our own.
                return(new DynamicMetaObject(
                           state.Binder.DeleteMember(
                               member.Name,
                               new DynamicMetaObject(
                                   AstUtils.Constant(tt),
                                   BindingRestrictions.Empty,
                                   tt
                                   ),
                               state.SharedOverloadResolverFactory
                               ).Expression,
                           BindingRestrictions.GetInstanceRestriction(Expression, Value).Merge(Restrictions)
                           ));
            }

            return(MakeDeleteMember(member));
        }
Exemplo n.º 3
0
        private DynamicMetaObject /*!*/ InvokeWorker(DynamicMetaObjectBinder /*!*/ call, DynamicMetaObject /*!*/[] /*!*/ args, Expression /*!*/ codeContext)
        {
            PerfTrack.NoteEvent(PerfTrack.Categories.Binding, "Type Invoke " + Value.UnderlyingSystemType.FullName + args.Length);
            PerfTrack.NoteEvent(PerfTrack.Categories.BindingTarget, "Type Invoke");
            if (this.NeedsDeferral())
            {
                return(call.Defer(ArrayUtils.Insert(this, args)));
            }

            for (int i = 0; i < args.Length; i++)
            {
                if (args[i].NeedsDeferral())
                {
                    return(call.Defer(ArrayUtils.Insert(this, args)));
                }
            }

            DynamicMetaObject res;

            if (IsStandardDotNetType(call))
            {
                res = MakeStandardDotNetTypeCall(call, codeContext, args);
            }
            else
            {
                res = MakePythonTypeCall(call, codeContext, args);
            }

            return(BindingHelpers.AddPythonBoxing(res));
        }
Exemplo n.º 4
0
        public DelegateInfo(LanguageContext context, Type returnType, Type[] parameters)
        {
            Assert.NotNull(returnType);
            Assert.NotNullItems(parameters);

            _returnType     = returnType;
            _parameterTypes = parameters;

            PerfTrack.NoteEvent(PerfTrack.Categories.DelegateCreate, ToString());

            if (_returnType != typeof(void))
            {
                _convertBinder = context.CreateConvertBinder(_returnType, true);
            }

            _invokeBinder = context.CreateInvokeBinder(new CallInfo(_parameterTypes.Length));

            Type[] delegateParams = new Type[1 + _parameterTypes.Length];
            delegateParams[0] = typeof(object[]);
            for (int i = 0; i < _parameterTypes.Length; i++)
            {
                delegateParams[1 + i] = _parameterTypes[i];
            }

            EmitClrCallStub(returnType, delegateParams, out _method);
        }
Exemplo n.º 5
0
        public override T BindDelegate <T>(CallSite <T> site, object[] args)
        {
            IFastInvokable ifi = args[1] as IFastInvokable;

            if (ifi != null)
            {
                FastBindResult <T> res = ifi.MakeInvokeBinding(site, this, (CodeContext)args[0], ArrayUtils.ShiftLeft(args, 2));
                if (res.Target != null)
                {
                    if (res.ShouldCache)
                    {
                        base.CacheTarget(res.Target);
                    }

                    return(res.Target);
                }
            }

            if (args[1] is Types.PythonType)
            {
                PerfTrack.NoteEvent(PerfTrack.Categories.BindingSlow, "InvokeNoFast " + ((Types.PythonType)args[1]).Name);
            }
            else
            {
                PerfTrack.NoteEvent(PerfTrack.Categories.BindingSlow, "InvokeNoFast " + CompilerHelpers.GetType(args[1]));
            }

            var target = this.LightBind <T>(args, Context.Options.CompilationThreshold);

            base.CacheTarget(target);
            return(target);
        }
        private DynamicMetaObject /*!*/ GetMemberWorker(DynamicMetaObjectBinder /*!*/ member, Expression codeContext)
        {
            PerfTrack.NoteEvent(PerfTrack.Categories.Binding, "Type GetMember " + Value.UnderlyingSystemType.FullName);
            PerfTrack.NoteEvent(PerfTrack.Categories.BindingTarget, "Type GetMember");

            return(new MetaGetBinderHelper(this, member, codeContext, GetTypeTest(), MakeMetaTypeTest(Restrict(this.GetRuntimeType()).Expression)).MakeTypeGetMember());
        }
Exemplo n.º 7
0
        private DynamicMetaObject /*!*/ InvokeWorker(DynamicMetaObjectBinder /*!*/ call, Expression /*!*/ codeContext, DynamicMetaObject /*!*/[] /*!*/ args)
        {
            PerfTrack.NoteEvent(PerfTrack.Categories.Binding, "BuiltinFunc Invoke " + Value.DeclaringType.FullName + "." + Value.Name + " with " + args.Length + " args " + Value.IsUnbound);
            PerfTrack.NoteEvent(PerfTrack.Categories.BindingTarget, "BuiltinFunction " + Value.Targets.Count + ", " + Value.Targets[0].GetParameters().Length);
            PerfTrack.NoteEvent(PerfTrack.Categories.BindingSlow, "BuiltinFunction " + BindingHelpers.GetCallSignature(call));

            if (this.NeedsDeferral())
            {
                return(call.Defer(ArrayUtils.Insert(this, args)));
            }

            for (int i = 0; i < args.Length; i++)
            {
                if (args[i].NeedsDeferral())
                {
                    return(call.Defer(ArrayUtils.Insert(this, args)));
                }
            }

            if (Value.IsUnbound)
            {
                return(MakeSelflessCall(call, codeContext, args));
            }
            else
            {
                return(MakeSelfCall(call, codeContext, args));
            }
        }
Exemplo n.º 8
0
        internal override bool TryGetValue(CodeContext context, object instance, PythonType owner, out object value)
        {
            PerfTrack.NoteEvent(PerfTrack.Categories.Properties, this);

            value = CallGetter(context, owner, PythonContext.GetContext(context).GetGenericCallSiteStorage0(), instance);
            return(true);
        }
Exemplo n.º 9
0
        internal DelegateInfo(LanguageContext context, Type returnType, ParameterInfo[] parameters)
        {
            Assert.NotNull(returnType);
            Assert.NotNullItems(parameters);

            _returnType = returnType;
            _parameters = parameters;

            PerfTrack.NoteEvent(PerfTrack.Categories.DelegateCreate, ToString());

            if (_returnType != typeof(void))
            {
                _convertBinder = context.CreateConvertBinder(_returnType, true);
            }

            _invokeBinder = context.CreateInvokeBinder(new CallInfo(_parameters.Length));

            Type[] delegateParams = new Type[_parameters.Length];
            for (int i = 0; i < _parameters.Length; i++)
            {
                delegateParams[i] = _parameters[i].ParameterType;
            }

            // Create the method with a special name so the langauge compiler knows that method's stack frame is not visible
            DynamicILGen cg = Snippets.Shared.CreateDynamicMethod("_Scripting_", _returnType, ArrayUtils.Insert(typeof(object[]), delegateParams), false);

            // Emit the stub
            _constants = EmitClrCallStub(cg);
            _method    = cg.Finish();
        }
Exemplo n.º 10
0
        private DynamicMetaObject BindConvert(DynamicMetaObject self)
        {
            PerfTrack.NoteEvent(PerfTrack.Categories.Binding, "Convert " + Type.FullName + " " + self.LimitType);
            PerfTrack.NoteEvent(PerfTrack.Categories.BindingTarget, "Conversion");

            DynamicMetaObject res;

#if FEATURE_COM
            DynamicMetaObject comConvert;
            if (Microsoft.Scripting.ComInterop.ComBinder.TryConvert(CompatBinder, self, out comConvert))
            {
                res = comConvert;
            }
            else
#endif
            {
                res = self.BindConvert(CompatBinder);
            }

            // if we return object and the interop binder had to put on an extra conversion
            // to the strong type go ahead and remove it now.
            if (ReturnType == typeof(object) &&
                res.Expression.Type != typeof(object) &&
                res.Expression.NodeType == ExpressionType.Convert)
            {
                res = new DynamicMetaObject(
                    ((UnaryExpression)res.Expression).Operand,
                    res.Restrictions
                    );
            }

            return(res);
        }
Exemplo n.º 11
0
        private void Compile(object frameObj)
        {
            if (Compiled)
            {
                return;
            }

            lock (this) {
                if (Compiled)
                {
                    return;
                }

                PerfTrack.NoteEvent(PerfTrack.Categories.Compiler, "Interpreted loop compiled");

                InterpretedFrame frame = (InterpretedFrame)frameObj;
                var compiler           = new LoopCompiler(_loop, frame.Interpreter.LabelMapping, _variables, _closureVariables, _instructionIndex, _loopEnd);
                var instructions       = frame.Interpreter.Instructions.Instructions;

                // replace this instruction with an optimized one:
                Interlocked.Exchange(ref instructions[_instructionIndex], new CompiledLoopInstruction(compiler.CreateDelegate()));

                // invalidate this instruction, some threads may still hold on it:
                _loop             = null;
                _variables        = null;
                _closureVariables = null;
            }
        }
Exemplo n.º 12
0
        protected Node(SourceSpan location)
        {
            _location = location;

#if DEBUG
            PerfTrack.NoteEvent(PerfTrack.Categories.Count, "RubyAST: " + GetType().Name);
#endif
        }
Exemplo n.º 13
0
        public static PythonType /*!*/ GetPythonTypeFromType(Type /*!*/ type)
        {
            ContractUtils.RequiresNotNull(type, "type");

            PerfTrack.NoteEvent(PerfTrack.Categories.DictInvoke, "TypeLookup " + type.FullName);

            return(PythonType.GetPythonType(type));
        }
Exemplo n.º 14
0
        private DynamicMetaObject /*!*/ MakeCallRule(DynamicMetaObjectBinder /*!*/ call, Expression /*!*/ codeContext, DynamicMetaObject[] args)
        {
            PerfTrack.NoteEvent(PerfTrack.Categories.Binding, "OldClass Invoke w/ " + args.Length + " args");
            PerfTrack.NoteEvent(PerfTrack.Categories.BindingTarget, "OldClass Invoke");

            CallSignature signature = BindingHelpers.GetCallSignature(call);

            // TODO: If we know __init__ wasn't present we could construct the OldInstance directly.

            Expression[] exprArgs = new Expression[args.Length];
            for (int i = 0; i < args.Length; i++)
            {
                exprArgs[i] = args[i].Expression;
            }

            ParameterExpression init    = Ast.Variable(typeof(object), "init");
            ParameterExpression instTmp = Ast.Variable(typeof(object), "inst");
            DynamicMetaObject   self    = Restrict(typeof(OldClass));

            return(new DynamicMetaObject(
                       Ast.Block(
                           new ParameterExpression[] { init, instTmp },
                           Ast.Assign(
                               instTmp,
                               Ast.New(
                                   typeof(OldInstance).GetConstructor(new Type[] { typeof(CodeContext), typeof(OldClass) }),
                                   codeContext,
                                   self.Expression
                                   )
                               ),
                           Ast.Condition(
                               Expression.Not(
                                   Expression.TypeIs(
                                       Expression.Assign(
                                           init,
                                           Ast.Call(
                                               typeof(PythonOps).GetMethod("OldClassTryLookupInit"),
                                               self.Expression,
                                               instTmp
                                               )
                                           ),
                                       typeof(OperationFailed)
                                       )
                                   ),
                               Ast.Dynamic(
                                   PythonContext.GetPythonContext(call).Invoke(
                                       signature
                                       ),
                                   typeof(object),
                                   ArrayUtils.Insert <Expression>(codeContext, init, exprArgs)
                                   ),
                               NoInitCheckNoArgs(signature, self, args)
                               ),
                           instTmp
                           ),
                       self.Restrictions.Merge(BindingRestrictions.Combine(args))
                       ));
        }
Exemplo n.º 15
0
        private DynamicMetaObject InvokeWorker(DynamicMetaObjectBinder /*!*/ callAction, DynamicMetaObject /*!*/[] args)
        {
            PerfTrack.NoteEvent(PerfTrack.Categories.Binding, "Method Invoke " + args.Length);
            PerfTrack.NoteEvent(PerfTrack.Categories.BindingTarget, "Method");

            CallSignature       signature    = BindingHelpers.GetCallSignature(callAction);
            DynamicMetaObject   self         = Restrict(typeof(Method));
            BindingRestrictions restrictions = self.Restrictions;

            // restrict to non-null self (Method is immutable so this is an invariant test)
            restrictions = restrictions.Merge(
                BindingRestrictions.GetExpressionRestriction(
                    Ast.NotEqual(
                        GetSelfExpression(self),
                        AstUtils.Constant(null)
                        )
                    )
                );

            DynamicMetaObject[] newArgs = ArrayUtils.Insert(GetMetaFunction(self), GetMetaSelf(self), args);
            var newSig = new CallSignature(ArrayUtils.Insert(new Argument(ArgumentType.Simple), signature.GetArgumentInfos()));

            var call = new DynamicMetaObject(
                DynamicExpression.Dynamic(
                    PythonContext.GetPythonContext(callAction).Invoke(
                        newSig
                        ).GetLightExceptionBinder(callAction.SupportsLightThrow()),
                    typeof(object),
                    ArrayUtils.Insert(PythonContext.GetCodeContext(callAction), DynamicUtils.GetExpressions(newArgs))
                    ),
                BindingRestrictions.Empty
                );

            /*
             * call = func.Invoke(
             *  new CallBinder(
             *      PythonContext.GetBinderState(callAction),
             *      newSig
             *  ),
             *  newArgs
             * );*/

            if (call.HasValue)
            {
                return(new DynamicMetaObject(
                           call.Expression,
                           restrictions.Merge(call.Restrictions),
                           call.Value
                           ));
            }
            else
            {
                return(new DynamicMetaObject(
                           call.Expression,
                           restrictions.Merge(call.Restrictions)
                           ));
            }
        }
Exemplo n.º 16
0
        private DynamicMetaObject /*!*/ InvokeWorker(DynamicMetaObjectBinder /*!*/ invoke, Expression /*!*/ codeContext, DynamicMetaObject /*!*/[] args)
        {
            PerfTrack.NoteEvent(PerfTrack.Categories.Binding, "OldClass Invoke");

            DynamicMetaObject self = Restrict(typeof(OldInstance));

            Expression[] exprArgs = new Expression[args.Length + 1];
            for (int i = 0; i < args.Length; i++)
            {
                exprArgs[i + 1] = args[i].Expression;
            }

            ParameterExpression tmp = Ast.Variable(typeof(object), "callFunc");

            exprArgs[0] = tmp;
            return(new DynamicMetaObject(
                       // we could get better throughput w/ a more specific rule against our current custom old class but
                       // this favors less code generation.

                       Ast.Block(
                           new ParameterExpression[] { tmp },
                           Ast.Condition(
                               Ast.Call(
                                   typeof(PythonOps).GetMethod("OldInstanceTryGetBoundCustomMember"),
                                   codeContext,
                                   self.Expression,
                                   AstUtils.Constant("__call__"),
                                   tmp
                                   ),
                               Ast.Block(
                                   Utils.Try(
                                       Ast.Call(typeof(PythonOps).GetMethod("FunctionPushFrameCodeContext"), codeContext),
                                       Ast.Assign(
                                           tmp,
                                           Ast.Dynamic(
                                               PythonContext.GetPythonContext(invoke).Invoke(
                                                   BindingHelpers.GetCallSignature(invoke)
                                                   ),
                                               typeof(object),
                                               ArrayUtils.Insert(codeContext, exprArgs)
                                               )
                                           )
                                       ).Finally(
                                       Ast.Call(typeof(PythonOps).GetMethod("FunctionPopFrame"))
                                       ),
                                   tmp
                                   ),
                               Utils.Convert(
                                   BindingHelpers.InvokeFallback(invoke, codeContext, this, args).Expression,
                                   typeof(object)
                                   )
                               )
                           ),
                       self.Restrictions.Merge(BindingRestrictions.Combine(args))
                       ));
        }
Exemplo n.º 17
0
        DynamicMetaObject IPythonOperable.BindOperation(PythonOperationBinder action, DynamicMetaObject[] args) {
            PerfTrack.NoteEvent(PerfTrack.Categories.Binding, "BuiltinFunc Operation " + action.Operation);
            PerfTrack.NoteEvent(PerfTrack.Categories.BindingTarget, "BuiltinFunc Operation");
            switch (action.Operation) {
                case PythonOperationKind.CallSignatures:
                    return PythonProtocol.MakeCallSignatureOperation(this, Value.Targets);
            }

            return null;
        }
Exemplo n.º 18
0
 public DynamicMetaObject ConvertWorker(DynamicMetaObjectBinder binder, Type toType, ConversionResultKind kind)
 {
     PerfTrack.NoteEvent(PerfTrack.Categories.Binding, "OldClass Convert");
     PerfTrack.NoteEvent(PerfTrack.Categories.BindingTarget, "OldClass Convert");
     if (toType.IsSubclassOf(typeof(Delegate)))
     {
         return(MakeDelegateTarget(binder, toType, Restrict(typeof(OldClass))));
     }
     return(FallbackConvert(binder));
 }
Exemplo n.º 19
0
        /// <summary>
        /// Creates the LambdaExpression which is the actual function body.
        /// </summary>
        private LightLambdaExpression EnsureFunctionLambda()
        {
            if (_dlrBody == null)
            {
                PerfTrack.NoteEvent(PerfTrack.Categories.Compiler, "Creating FunctionBody");
                _dlrBody = CreateFunctionLambda();
            }

            return(_dlrBody);
        }
Exemplo n.º 20
0
        internal override LightLambdaExpression GetLambda()
        {
            if (_dlrBody == null)
            {
                PerfTrack.NoteEvent(PerfTrack.Categories.Compiler, "Creating FunctionBody");
                _dlrBody = MakeClassBody();
            }

            return(_dlrBody);
        }
Exemplo n.º 21
0
        DynamicMetaObject IPythonOperable.BindOperation(PythonOperationBinder action, DynamicMetaObject[] args)
        {
            PerfTrack.NoteEvent(PerfTrack.Categories.Binding, "OldClass PythonOperation " + action.Operation);

            if (action.Operation == PythonOperationKind.IsCallable)
            {
                return(MakeIsCallable(action));
            }

            return(null);
        }
Exemplo n.º 22
0
        internal static bool TryInvokeTernaryOperator(CodeContext context, object o, object arg1, object arg2, string name, out object value)
        {
            PerfTrack.NoteEvent(PerfTrack.Categories.Temporary, "TernaryOp " + CompilerHelpers.GetType(o).Name + " " + name);

            if (TryGetOperator(context, o, name, out object callable))
            {
                value = PythonCalls.Call(context, callable, arg1, arg2);
                return(true);
            }

            value = null;
            return(false);
        }
Exemplo n.º 23
0
        private void DoSet(CodeContext context, object instance, object val, bool suppressWarning)
        {
            PerfTrack.NoteEvent(PerfTrack.Categories.Fields, this);
            if (_info.IsInitOnly || _info.IsLiteral)
            {
                throw PythonOps.AttributeErrorForReadonlyAttribute(_info.DeclaringType.Name, _info.Name);
            }
            else if (!suppressWarning && instance != null && instance.GetType().IsValueType)
            {
                PythonOps.Warn(context, PythonExceptions.RuntimeWarning, UpdateValueTypeFieldWarning, _info.Name, _info.DeclaringType.Name);
            }

            _info.SetValue(instance, context.LanguageContext.Binder.Convert(val, _info.FieldType));
        }
        internal static DynamicMetaObject FallbackWorker(PythonContext context, DynamicMetaObject /*!*/ self, DynamicMetaObject /*!*/ codeContext, string name, GetMemberOptions options, DynamicMetaObjectBinder action, DynamicMetaObject errorSuggestion)
        {
            if (self.NeedsDeferral())
            {
                return(action.Defer(self));
            }
            PythonOverloadResolverFactory resolverFactory = new PythonOverloadResolverFactory(context.Binder, codeContext.Expression);

            PerfTrack.NoteEvent(PerfTrack.Categories.BindingTarget, "FallbackGet");

            bool isNoThrow = ((options & GetMemberOptions.IsNoThrow) != 0) ? true : false;
            Type limitType = self.GetLimitType();

            if (limitType == typeof(DynamicNull) || PythonBinder.IsPythonType(limitType))
            {
                // look up in the PythonType so that we can
                // get our custom method names (e.g. string.startswith)
                PythonType argType = DynamicHelpers.GetPythonTypeFromType(limitType);

                // if the name is defined in the CLS context but not the normal context then
                // we will hide it.
                if (argType.IsHiddenMember(name))
                {
                    DynamicMetaObject baseRes = PythonContext.GetPythonContext(action).Binder.GetMember(
                        name,
                        self,
                        resolverFactory,
                        isNoThrow,
                        errorSuggestion
                        );
                    Expression failure = GetFailureExpression(limitType, self, name, isNoThrow, action);

                    return(BindingHelpers.FilterShowCls(codeContext, action, baseRes, failure));
                }
            }

            var res = PythonContext.GetPythonContext(action).Binder.GetMember(name, self, resolverFactory, isNoThrow, errorSuggestion);

            // Default binder can return something typed to boolean or int.
            // If that happens, we need to apply Python's boxing rules.
            if (res.Expression.Type.IsValueType)
            {
                res = new DynamicMetaObject(
                    AstUtils.Convert(res.Expression, typeof(object)),
                    res.Restrictions
                    );
            }

            return(res);
        }
Exemplo n.º 25
0
        public DynamicMetaObject ConvertWorker(DynamicMetaObjectBinder binder, Type type, Type retType, ConversionResultKind kind)
        {
            PerfTrack.NoteEvent(PerfTrack.Categories.Binding, "Conversion " + type.FullName);
            PerfTrack.NoteEvent(PerfTrack.Categories.BindingTarget, "Conversion");
            ValidationInfo typeTest = BindingHelpers.GetValidationInfo(this, Value.PythonType);

            return(BindingHelpers.AddDynamicTestAndDefer(
                       binder,
                       TryPythonConversion(binder, type) ?? FallbackConvert(binder),
                       new DynamicMetaObject[] { this },
                       typeTest,
                       retType
                       ));
        }
Exemplo n.º 26
0
        internal override bool TryGetValue(CodeContext context, object instance, PythonType owner, out object value)
        {
            PerfTrack.NoteEvent(PerfTrack.Categories.Fields, this);
            if (instance == null)
            {
                value = _info.IsStatic ? _info.GetValue(null) : this;
            }
            else
            {
                value = _info.GetValue(context.LanguageContext.Binder.Convert(instance, _info.DeclaringType));
            }

            return(true);
        }
Exemplo n.º 27
0
        private DynamicMetaObject /*!*/ MakeSetMember(string /*!*/ name, DynamicMetaObject /*!*/ value)
        {
            PerfTrack.NoteEvent(PerfTrack.Categories.Binding, "OldClass SetMember");
            PerfTrack.NoteEvent(PerfTrack.Categories.BindingTarget, "OldClass SetMember");
            DynamicMetaObject self = Restrict(typeof(OldClass));

            Expression call, valueExpr = AstUtils.Convert(value.Expression, typeof(object));

            switch (name)
            {
            case "__bases__":
                call = Ast.Call(
                    typeof(PythonOps).GetMethod("OldClassSetBases"),
                    self.Expression,
                    valueExpr
                    );
                break;

            case "__name__":
                call = Ast.Call(
                    typeof(PythonOps).GetMethod("OldClassSetName"),
                    self.Expression,
                    valueExpr
                    );
                break;

            case "__dict__":
                call = Ast.Call(
                    typeof(PythonOps).GetMethod("OldClassSetDictionary"),
                    self.Expression,
                    valueExpr
                    );
                break;

            default:
                call = Ast.Call(
                    typeof(PythonOps).GetMethod("OldClassSetNameHelper"),
                    self.Expression,
                    AstUtils.Constant(name),
                    valueExpr
                    );
                break;
            }

            return(new DynamicMetaObject(
                       call,
                       self.Restrictions.Merge(value.Restrictions)
                       ));
        }
Exemplo n.º 28
0
        protected override T MakeTarget(CodeContext context)
        {
            if (_rules.Count == 1 && this != _rules[0].MonomorphicRuleSet)
            {
                // use the monomorphic rule if we only have 1 rule.
                return(_rules[0].MonomorphicRuleSet.GetOrMakeTarget(context));
            }

            PerfTrack.NoteEvent(PerfTrack.Categories.Rules, "GenerateRule");

            MethodInfo mi = typeof(T).GetMethod("Invoke");
            CodeGen    cg = ScriptDomainManager.CurrentManager.Snippets.Assembly.DefineMethod(
                StubName,
                mi.ReturnType,
                ReflectionUtils.GetParameterTypes(mi.GetParameters()),
                new ConstantPool()
                );

            cg.EmitLineInfo = false;
            cg.Binder       = context.LanguageContext.Binder;

            if (DynamicSiteHelpers.IsFastTarget(typeof(T)))
            {
                cg.ContextSlot = new PropertySlot(cg.ArgumentSlots[0], typeof(FastDynamicSite).GetProperty("Context"));
            }
            else
            {
                cg.ContextSlot = cg.ArgumentSlots[1];
            }

            foreach (StandardRule <T> rule in _rules)
            {
                Label nextTest = cg.DefineLabel();
                rule.Emit(cg, nextTest);
                cg.MarkLabel(nextTest);
            }
            EmitNoMatch(cg);

            if (_rules.Count == 1 &&
                this == _rules[0].MonomorphicRuleSet &&
                _rules[0].TemplateParameterCount > 0 &&
                cg.IsDynamicMethod)
            {
                _monomorphicTemplate = (DynamicMethod)cg.MethodInfo;
            }

            return((T)(object)cg.CreateDelegate(typeof(T)));
        }
Exemplo n.º 29
0
        private DynamicMetaObject /*!*/ InvokeWorker(DynamicMetaObjectBinder /*!*/ call, Expression /*!*/ codeContext, DynamicMetaObject /*!*/[] args)
        {
            PerfTrack.NoteEvent(PerfTrack.Categories.Binding, "BuiltinMethodDesc Invoke " + Value.DeclaringType + "." + Value.__name__ + " w/ " + args.Length + " args");
            PerfTrack.NoteEvent(PerfTrack.Categories.BindingTarget, "BuiltinMethodDesc Invoke");

            CallSignature       signature    = BindingHelpers.GetCallSignature(call);
            BindingRestrictions selfRestrict = BindingRestrictions.GetInstanceRestriction(Expression, Value).Merge(Restrictions);

            selfRestrict = selfRestrict.Merge(
                BindingRestrictions.GetExpressionRestriction(
                    MakeFunctionTest(
                        Ast.Call(
                            typeof(PythonOps).GetMethod("GetBuiltinMethodDescriptorTemplate"),
                            Ast.Convert(Expression, typeof(BuiltinMethodDescriptor))
                            )
                        )
                    )
                );

            return(Value.Template.MakeBuiltinFunctionCall(
                       call,
                       codeContext,
                       this,
                       args,
                       false, // no self
                       selfRestrict,
                       (newArgs) => {
                BindingTarget target;
                PythonContext state = PythonContext.GetPythonContext(call);

                DynamicMetaObject res = state.Binder.CallMethod(
                    new PythonOverloadResolver(
                        state.Binder,
                        newArgs,
                        signature,
                        codeContext
                        ),
                    Value.Template.Targets,
                    selfRestrict,
                    Value.Template.Name,
                    NarrowingLevel.None,
                    Value.Template.IsBinaryOperator ? PythonNarrowing.BinaryOperator : NarrowingLevel.All,
                    out target
                    );

                return BindingHelpers.CheckLightThrow(call, res, target);
            }));
        }
Exemplo n.º 30
0
        private DynamicMetaObject /*!*/ MakeDeleteMember(DeleteMemberBinder /*!*/ member)
        {
            PerfTrack.NoteEvent(PerfTrack.Categories.Binding, "OldClass DeleteMember");
            PerfTrack.NoteEvent(PerfTrack.Categories.BindingTarget, "OldClass DeleteMember");
            DynamicMetaObject self = Restrict(typeof(OldClass));

            return(new DynamicMetaObject(
                       Ast.Call(
                           typeof(PythonOps).GetMethod("OldClassDeleteMember"),
                           AstUtils.Constant(PythonContext.GetPythonContext(member).SharedContext),
                           self.Expression,
                           AstUtils.Constant(member.Name)
                           ),
                       self.Restrictions
                       ));
        }