Example #1
0
        public static object CallWithCurrentContinuation(object fc1)
        {
            Callable     fc  = RequiresNotNull <Callable>(fc1);
            Continuation ccc = new Continuation {
                Stack = new StackTrace(), Thread = Thread.CurrentThread
            };

            try
            {
                CallTargetN exitproc = MakeContinuation(ccc);
                Callable    fce      = Closure.Create(exitproc) as Callable;
                object      res      = fc.Call(fce);
                return(res);
            }
            catch (Continuation c)
            {
                if (ccc == c)
                {
                    return(c.Value);
                }
                else
                {
                    throw;
                }
            }
        }
Example #2
0
        public OptimizedFunctionAny(string name, MethodInfo[] infos, MethodBase[] targets, FunctionType functionType) : base(name, targets, functionType)
        {
            for (int i = 0; i < infos.Length; i++)
            {
                Debug.Assert(infos[i].IsStatic);

                switch (infos[i].GetParameters().Length)
                {
                case 0: target0 = IronPython.Compiler.CodeGen.CreateDelegate(infos[i], typeof(CallTarget0)) as CallTarget0; break;

                case 1:
                    if (!infos[i].GetParameters()[0].ParameterType.HasElementType)
                    {
                        target1 = IronPython.Compiler.CodeGen.CreateDelegate(infos[i], typeof(CallTarget1)) as CallTarget1;
                    }
                    else
                    {
                        targetN = IronPython.Compiler.CodeGen.CreateDelegate(infos[i], typeof(CallTargetN)) as CallTargetN;
                    }
                    break;

                case 2: target2 = IronPython.Compiler.CodeGen.CreateDelegate(infos[i], typeof(CallTarget2)) as CallTarget2; break;

                case 3: target3 = IronPython.Compiler.CodeGen.CreateDelegate(infos[i], typeof(CallTarget3)) as CallTarget3; break;

                case 4: target4 = IronPython.Compiler.CodeGen.CreateDelegate(infos[i], typeof(CallTarget4)) as CallTarget4; break;

                case 5: target5 = IronPython.Compiler.CodeGen.CreateDelegate(infos[i], typeof(CallTarget5)) as CallTarget5; break;
                }
            }
        }
Example #3
0
        static CallTargetN MakeContinuation(Continuation cc)
        {
            CallTargetN ct = delegate(object[] value)
            {
                if (!CheckStack(cc))
                {
                    return(AssertionViolation("call/cc", "not supported, continuation called outside dynamic extent"));
                }
                if (value.Length == 0)
                {
                    cc.Value = Unspecified;
                    throw cc;
                }
                if (value.Length == 1)
                {
                    cc.Value = value[0];
                    if (cc.Value is MultipleValues)
                    {
                        return(AssertionViolation("call/cc", "cannot return multiple values"));
                    }
                    throw cc;
                }
                else
                {
                    cc.Value = Values(value);
                    throw cc;
                }
            };

            return(ct);
        }
Example #4
0
 public OptimizedFunctionAny(OptimizedFunctionAny from) : base(from)
 {
     target0 = from.target0;
     target1 = from.target1;
     target2 = from.target2;
     target3 = from.target3;
     target4 = from.target4;
     target5 = from.target5;
     targetN = from.targetN;
 }
Example #5
0
        public static Callable RecordConstructor(object cd)
        {
            RecordConstructorDescriptor ci = RequiresNotNull <RecordConstructorDescriptor>(cd);
            Type tt = ci.type.Finish();

            // this is foo.make(params object[] args) calls constructor(s).
            Callable pp = ci.type.Constructor as Callable;

            RecordConstructorDescriptor rcd = ci;

            List <Callable> init = new List <Callable>();

            while (rcd != null)
            {
                if (rcd.protocol != null)
                {
                    init.Add(rcd.protocol);
                }

                rcd = rcd.parent;
            }

            if (init.Count == 0)
            {
                CallTargetN np = delegate(object[] args)
                {
                    if (ci.type.TotalFieldCount != args.Length)
                    {
                        return(AssertionViolation(ci.type.Name,
                                                  string.Format("Incorrect number of arguments, expected {0} got {1}", ci.type.TotalFieldCount, args.Length), args));
                    }
                    return(pp.Call(args));
                };
                return(Closure.Create(np));
            }
            else
            {
                CallTargetN rc = delegate(object[] args)
                {
                    var instance   = ci.type.DefaultConstructor.Call();
                    var protochain = MakeProtocolCallChain(ci, instance);

                    protochain.Call(args);

                    return(instance);
                };

                return(Closure.Create(rc));
            }
        }
Example #6
0
 public FunctionObject(CodeContext context, string name, CallTargetN callTarget,
                       string [] argNames, bool isStandardConstructor)
     : base(GetPrototype())
 {        //ECMA 13.2
     this.context    = context;
     this.name       = name;
     this.argNames   = argNames;
     this.callTarget = callTarget;
     SetItem(SymbolTable.StringToId("length"), new AttributedProperty(argNames.Length, PropertyAttributes.DontDelete | PropertyAttributes.DontEnum | PropertyAttributes.ReadOnly));
     if (isStandardConstructor)
     {
         constructTarget = Delegate.CreateDelegate(typeof(ConstructTargetN), context, this.GetType().GetMethod("construct"), true) as ConstructTargetN;
         SetItem(SymbolTable.StringToId("constructor"), new AttributedProperty(constructTarget, PropertyAttributes.DontEnum));
     }
     SetItem(SymbolTable.StringToId("prototype"), new AttributedProperty(prototype, PropertyAttributes.DontDelete));
 }
Example #7
0
        static Callable MakeProtocolCallChain(RecordConstructorDescriptor rcd, object instance)
        {
            CallTargetN ipc = delegate(object[] iargs)
            {
                var nargs = new List <object>(iargs.Length + 1);
                nargs.Add(instance);
                nargs.AddRange(iargs);
                return(rcd.type.DefaultInit.Call(nargs.ToArray()));
            };

            CallTargetN ppp = ipc;

            if (rcd.parent != null)
            {
                var parent = MakeProtocolCallChain(rcd.parent, instance);

                CallTargetN rr = delegate(object[] args)
                {
                    parent.Call(args);
                    return(Closure.Create(ipc));
                };

                ppp = rr;
            }

            CallTargetN pc = delegate(object[] args)
            {
                var ppc = Closure.Create(ppp);

                if (rcd.protocol != null)
                {
                    ppc = ((Callable)rcd.protocol.Call(ppc));
                }

                ppc.Call(args);

                return(instance);
            };

            return(Closure.Create(pc));
        }
Example #8
0
        public FunctionX(PythonModule globals, string name, CallTargetN target, string[] argNames, object[] defaults, FuncDefType flags)
            :
            base(globals, name, target, argNames, defaults)
        {
            this.flags = flags;
            nparams    = argNames.Length;

            if ((flags & FuncDefType.KeywordDict) != 0)
            {
                extraArgs++;
                nparams--;
                kwDictPos = nparams;
            }

            if ((flags & FuncDefType.ArgList) != 0)
            {
                extraArgs++;
                nparams--;
                argListPos = nparams;
            }

            Debug.Assert(defaults.Length <= nparams);
        }
Example #9
0
        public static Callable RecordConstructor(object cd)
        {
            RecordConstructorDescriptor ci = RequiresNotNull <RecordConstructorDescriptor>(cd);
            Type tt = ci.type.Finish();

            // this is foo.make(params object[] args) calls constructor(s).
            Callable pp = ci.type.Constructor as Callable;

            RecordConstructorDescriptor rcd = ci;

            List <Callable> init = new List <Callable>();

            while (rcd != null)
            {
                if (rcd.protocol != null)
                {
                    init.Add(rcd.protocol);
                }

                rcd = rcd.parent;
            }

            if (init.Count == 0)
            {
                CallTargetN np = delegate(object[] args)
                {
                    if (ci.type.TotalFieldCount != args.Length)
                    {
                        return(AssertionViolation(ci.type.Name,
                                                  string.Format("Incorrect number of arguments, expected {0} got {1}", ci.type.TotalFieldCount, args.Length), args));
                    }
                    return(pp.Call(args));
                };
                return(Closure.Create(np));
            }
            else
            {
                init.Reverse();

                CallTargetN np = delegate(object[] args)
                {
                    Callable ppp = pp;

                    List <object> allargs   = new List <object>();
                    int           i         = init.Count;
                    Callable      collector = null;
                    CallTargetN   xxx       = delegate(object[] margs)
                    {
                        allargs.AddRange(margs);
                        if (i == 0)
                        {
                            if (ci.type.TotalFieldCount != allargs.Count)
                            {
                                return(AssertionViolation(ci.type.Name,
                                                          string.Format("Incorrect number of arguments, expected {0} got {1}", ci.type.TotalFieldCount, allargs.Count), allargs.ToArray()));
                            }
                            return(pp.Call(allargs.ToArray()));
                        }
                        else
                        {
                            i--;
                            return(collector);
                        }
                    };
                    ppp = collector = Closure.Create(xxx) as Callable;

                    foreach (Callable ctr in init)
                    {
                        ppp = ctr.Call(ppp) as Callable;
                    }

                    object result = ppp.Call(args);

                    if (result == collector)
                    {
                        result = collector.Call();
                    }
                    return(result);
                };

                return(Closure.Create(np));
            }
        }
Example #10
0
 public FunctionN(PythonModule globals, string name, CallTargetN target, string[] argNames, object[] defaults)
     : base(globals, name, argNames, defaults)
 {
     this.target = target;
 }
Example #11
0
 public OptimizedFunctionN(OptimizedFunctionN from) : base(from)
 {
     target = from.target;
 }
Example #12
0
 public OptimizedFunctionN(string name, MethodInfo info, MethodBase[] targets, FunctionType functionType) : base(name, targets, functionType)
 {
     target = IronPython.Compiler.CodeGen.CreateDelegate(info, typeof(CallTargetN)) as CallTargetN;
 }
Example #13
0
        public FunctionX(PythonModule globals, string name, CallTargetN target, string[] argNames, object[] defaults, FunctionAttributes flags)
            : base(globals, name, target, argNames, defaults)
        {
            this.flags = flags;
            nparams = argNames.Length;

            if ((flags & FunctionAttributes.KeywordDictionary) != 0) {
                extraArgs++;
                nparams--;
                kwDictPos = nparams;
            }

            if ((flags & FunctionAttributes.ArgumentList) != 0) {
                extraArgs++;
                nparams--;
                argListPos = nparams;
            }

            Debug.Assert(defaults.Length <= nparams);
        }
Example #14
0
 public FunctionN(PythonModule globals, string name, CallTargetN target, string[] argNames, object[] defaults)
     : base(globals, name, argNames, defaults)
 {
     this.target = target;
 }