Example #1
0
        public DynamicMetaObject Create(CallSignature signature, DynamicMetaObject target, DynamicMetaObject[] args, Expression contextExpression)
        {
            Type t = GetTargetType(target.Value);

            if (t != null)
            {
                if (typeof(Delegate).IsAssignableFrom(t) && args.Length == 1)
                {
                    // PythonOps.GetDelegate(CodeContext context, object callable, Type t);
                    return(new DynamicMetaObject(
                               Ast.Call(
                                   typeof(PythonOps).GetMethod("GetDelegate"),
                                   contextExpression,
                                   AstUtils.Convert(args[0].Expression, typeof(object)),
                                   Expression.Constant(t)
                                   ),
                               target.Restrictions.Merge(BindingRestrictions.GetInstanceRestriction(target.Expression, target.Value))
                               ));
                }

                return(CallMethod(
                           new PythonOverloadResolver(
                               this,
                               args,
                               signature,
                               contextExpression
                               ),
                           CompilerHelpers.GetConstructors(t, PrivateBinding),
                           target.Restrictions.Merge(BindingRestrictions.GetInstanceRestriction(target.Expression, target.Value))
                           ));
            }

            return(null);
        }
Example #2
0
        private static BuiltinFunction GetConstructor(Type t, bool privateBinding)
        {
            BuiltinFunction ctorFunc = InstanceOps.NonDefaultNewInst;

            MethodBase[] ctors = CompilerHelpers.GetConstructors(t, privateBinding, true);

            return(GetConstructor(t, ctorFunc, ctors));
        }
Example #3
0
 internal static MethodBase[] GetConstructors(Type t, bool privateBinding, bool includeProtected = false)
 {
     MethodBase[] ctors = CompilerHelpers.GetConstructors(t, privateBinding, includeProtected);
     if (t.IsEnum())
     {
         var enumCtor = typeof(PythonTypeOps).GetDeclaredMethods(nameof(CreateEnum)).Single().MakeGenericMethod(t);
         ctors = ctors.Concat(new[] { enumCtor }).ToArray();
     }
     return(ctors);
 }
Example #4
0
        protected override MethodBase[] GetTargetMethods()
        {
            object target = Arguments[0];
            Type   t      = GetTargetType(target);

            if (t != null)
            {
                Test = Ast.AndAlso(Test, Ast.Equal(Rule.Parameters[0], Ast.Constant(target)));

                return(CompilerHelpers.GetConstructors(t, Binder.PrivateBinding));
            }

            return(null);
        }
Example #5
0
        /// <summary>
        /// Creating a standard .NET type is easy - we just call it's constructor with the provided
        /// arguments.
        /// </summary>
        private DynamicMetaObject /*!*/ MakeStandardDotNetTypeCall(DynamicMetaObjectBinder /*!*/ call, Expression /*!*/ codeContext, DynamicMetaObject /*!*/[] /*!*/ args)
        {
            CallSignature signature = BindingHelpers.GetCallSignature(call);
            PythonContext state     = PythonContext.GetPythonContext(call);

            MethodBase[] ctors = CompilerHelpers.GetConstructors(Value.UnderlyingSystemType, state.Binder.PrivateBinding);

            if (ctors.Length > 0)
            {
                return(state.Binder.CallMethod(
                           new PythonOverloadResolver(
                               state.Binder,
                               args,
                               signature,
                               codeContext
                               ),
                           ctors,
                           Restrictions.Merge(BindingRestrictions.GetInstanceRestriction(Expression, Value))
                           ));
            }
            else
            {
                string msg;
                if (Value.UnderlyingSystemType.IsAbstract)
                {
                    msg = String.Format("Cannot create instances of {0} because it is abstract", Value.Name);
                }
                else
                {
                    msg = String.Format("Cannot create instances of {0} because it has no public constructors", Value.Name);
                }
                return(new DynamicMetaObject(
                           call.Throw(
                               Ast.New(
                                   typeof(TypeErrorException).GetConstructor(new Type[] { typeof(string) }),
                                   AstUtils.Constant(msg)
                                   )
                               ),
                           Restrictions.Merge(BindingRestrictions.GetInstanceRestriction(Expression, Value))
                           ));
            }
        }
Example #6
0
        public MetaObject Create(CallSignature signature, ParameterBinderWithCodeContext parameterBinder, MetaObject target, MetaObject[] args)
        {
            Type t = GetTargetType(target.Value);

            if (t != null)
            {
                if (typeof(Delegate).IsAssignableFrom(t) && args.Length == 1)
                {
                    MethodInfo dc = GetDelegateCtor(t);

                    // BinderOps.CreateDelegate<T>(CodeContext context, object callable);
                    return(new MetaObject(
                               Ast.Call(null, dc, parameterBinder.ContextExpression, args[0].Expression),
                               target.Restrictions.Merge(Restrictions.GetInstanceRestriction(target.Expression, target.Value))
                               ));
                }

                return(CallMethod(parameterBinder, CompilerHelpers.GetConstructors(t, PrivateBinding), args, signature));
            }

            return(null);
        }