private static DynamicMetaObject MakeGetItemIterable(DynamicMetaObject metaUserObject, PythonContext state, PythonTypeSlot pts, string method) { ParameterExpression tmp = Ast.Parameter(typeof(object), "getitemVal"); return(new DynamicMetaObject( Expression.Block( new[] { tmp }, Expression.Call( typeof(PythonOps).GetMethod(method), Ast.Block( MetaPythonObject.MakeTryGetTypeMember( state, pts, tmp, metaUserObject.Expression, Ast.Call( typeof(DynamicHelpers).GetMethod("GetPythonType"), AstUtils.Convert( metaUserObject.Expression, typeof(object) ) ) ), tmp ), AstUtils.Constant( CallSite <Func <CallSite, CodeContext, object, int, object> > .Create( new PythonInvokeBinder(state, new CallSignature(1)) ) ) ) ), metaUserObject.Restrictions )); }
/// <summary> /// Determines if the type associated with the first MetaObject is a subclass of the /// type associated with the second MetaObject. /// </summary> internal static bool IsSubclassOf(DynamicMetaObject /*!*/ xType, DynamicMetaObject /*!*/ yType) { PythonType x = MetaPythonObject.GetPythonType(xType); PythonType y = MetaPythonObject.GetPythonType(yType); return(x.IsSubclassOf(y)); }
public static SlotOrFunction /*!*/ GetSlotOrFunction(PythonContext /*!*/ state, string op, params DynamicMetaObject[] types) { PythonTypeSlot slot; SlotOrFunction res; if (TryGetBinder(state, types, op, null, out res)) { if (res != SlotOrFunction.Empty) { return(res); } } else if (MetaUserObject.GetPythonType(types[0]).TryResolveSlot(state.SharedContext, op, out slot)) { ParameterExpression tmp = Ast.Variable(typeof(object), "slotVal"); Expression[] args = new Expression[types.Length - 1]; for (int i = 1; i < types.Length; i++) { args[i - 1] = types[i].Expression; } return(new SlotOrFunction( new DynamicMetaObject( Ast.Block( new ParameterExpression[] { tmp }, MetaPythonObject.MakeTryGetTypeMember( state, slot, tmp, types[0].Expression, Ast.Call( typeof(DynamicHelpers).GetMethod("GetPythonType"), types[0].Expression ) ), DynamicExpression.Dynamic( state.Invoke( new CallSignature(args.Length) ), typeof(object), ArrayUtils.Insert <Expression>( AstUtils.Constant(state.SharedContext), tmp, args ) ) ), BindingRestrictions.Combine(types).Merge(BindingRestrictionsHelpers.GetRuntimeTypeRestriction(types[0].Expression, types[0].GetLimitType())) ), slot )); } return(SlotOrFunction.Empty); }
internal static DynamicMetaObject ConvertToIEnumerator(DynamicMetaObjectBinder /*!*/ conversion, DynamicMetaObject /*!*/ metaUserObject) { PythonType pt = MetaPythonObject.GetPythonType(metaUserObject); PythonContext state = PythonContext.GetPythonContext(conversion); CodeContext context = state.SharedContext; PythonTypeSlot pts; if (pt.TryResolveSlot(context, "__iter__", out pts)) { ParameterExpression tmp = Ast.Parameter(typeof(object), "iterVal"); return(new DynamicMetaObject( Expression.Block( new[] { tmp }, Expression.Call( typeof(PythonOps).GetMethod("CreatePythonEnumerator"), Ast.Block( MetaPythonObject.MakeTryGetTypeMember( state, pts, metaUserObject.Expression, tmp ), Ast.Dynamic( new PythonInvokeBinder( state, new CallSignature(0) ), typeof(object), AstUtils.Constant(context), tmp ) ) ) ), metaUserObject.Restrictions )); } else if (pt.TryResolveSlot(context, "__getitem__", out pts)) { return(MakeGetItemIterable(metaUserObject, state, pts, "CreateItemEnumerator")); } return(null); }
internal static DynamicMetaObject ConvertToIEnumerable(DynamicMetaObjectBinder /*!*/ conversion, DynamicMetaObject /*!*/ metaUserObject) { PythonType pt = MetaPythonObject.GetPythonType(metaUserObject); PythonContext pyContext = PythonContext.GetPythonContext(conversion); CodeContext context = pyContext.SharedContext; PythonTypeSlot pts; if (pt.TryResolveSlot(context, "__iter__", out pts)) { return(MakeIterRule(metaUserObject, "CreatePythonEnumerable")); } else if (pt.TryResolveSlot(context, "__getitem__", out pts)) { return(MakeGetItemIterable(metaUserObject, pyContext, pts, "CreateItemEnumerable")); } return(null); }
/// <summary> /// Tries to get the BuiltinFunction for the given name on the type of the provided MetaObject. /// /// Succeeds if the MetaObject is a BuiltinFunction or BuiltinMethodDescriptor. /// </summary> internal static bool TryGetStaticFunction(PythonContext /*!*/ state, string op, DynamicMetaObject /*!*/ mo, out BuiltinFunction function) { PythonType type = MetaPythonObject.GetPythonType(mo); function = null; if (!String.IsNullOrEmpty(op)) { PythonTypeSlot xSlot; object val; if (type.TryResolveSlot(state.SharedContext, op, out xSlot) && xSlot.TryGetValue(state.SharedContext, null, type, out val)) { function = TryConvertToBuiltinFunction(val); if (function == null) { return(false); } } } return(true); }