Exemplo n.º 1
0
        internal PythonFunction(CodeContext/*!*/ context, FunctionCode funcInfo, object modName, object[] defaults, MutableTuple closure) {
            Assert.NotNull(context, funcInfo);

            _context = context;
            _defaults = defaults ?? ArrayUtils.EmptyObjects;
            _code = funcInfo;
            _doc = funcInfo._initialDoc;
            _name = funcInfo.co_name;

            Debug.Assert(_defaults.Length <= _code.co_argcount);
            if (modName != Uninitialized.Instance) {
                _module = modName;
            }

            Closure = closure;
            _compat = CalculatedCachedCompat();
        }
Exemplo n.º 2
0
        private static int _CurrentId = 1;  // The current ID for functions which are called in complex ways.

        public TotemFunction(CodeContext context, FunctionCode funcInfo, object moduleName, object[] defaults, MutableTuple closure)
        {
            Assert.NotNull(context, funcInfo);

            _context = context;
            _defaults = defaults ?? ArrayUtils.EmptyObjects;
            _code = funcInfo;
            _name = funcInfo.Name;

            Debug.Assert(_defaults.Length <= _code.ArgCount);
            if (moduleName != Uninitialized.Instance)
            {
                _module = moduleName;
            }

            Closure = closure;
            _compat = CalculatedCachedCompat();
        }
Exemplo n.º 3
0
        internal RubyBlockScope(MutableTuple locals, SymbolId[]/*!*/ variableNames,
            BlockParam/*!*/ blockFlowControl, object selfObject, InterpretedFrame interpretedFrame) {
            var parent = blockFlowControl.Proc.LocalScope;

            // RuntimeFlowControl:
            _activeFlowControlScope = parent.FlowControlScope;

            // RubyScope:
            _parent = parent;
            _top = parent.Top;
            _selfObject = selfObject;
            _methodAttributes = RubyMethodAttributes.PublicInstance;
            _locals = locals;
            _variableNames = variableNames;
            InterpretedFrame = interpretedFrame;
            
            // RubyBlockScope:
            _blockFlowControl = blockFlowControl;
        }
Exemplo n.º 4
0
        internal RubyMethodScope(MutableTuple locals, SymbolId[]/*!*/ variableNames, 
            RubyScope/*!*/ parent, RubyModule/*!*/ declaringModule, string/*!*/ definitionName,
            object selfObject, Proc blockParameter, InterpretedFrame interpretedFrame) {
            Assert.NotNull(parent, declaringModule, definitionName);

            // RuntimeFlowControl:
            _activeFlowControlScope = this;
            
            // RubyScope:
            _parent = parent;
            _top = parent.Top;
            _selfObject = selfObject;
            _methodAttributes = RubyMethodAttributes.PublicInstance;
            _locals = locals;
            _variableNames = variableNames;
            InterpretedFrame = interpretedFrame;
            
            // RubyMethodScope:
            _declaringModule = declaringModule;
            _definitionName = definitionName;
            _blockParameter = blockParameter;

            InitializeRfc(blockParameter);
            SetDebugName("method " + definitionName + ((blockParameter != null) ? "&" : null));
        }
Exemplo n.º 5
0
 internal void SetEmptyLocals() {
     Debug.Assert(_variableNames == null);
     _variableNames = SymbolId.EmptySymbols;
     _locals = null;
 }
Exemplo n.º 6
0
 internal void SetLocals(MutableTuple locals, SymbolId[]/*!*/ variableNames) {
     Debug.Assert(_variableNames == null);
     _locals = locals;
     _variableNames = variableNames;
 }
Exemplo n.º 7
0
        internal RubyFileInitializerScope(MutableTuple locals, string[]/*!*/ variableNames, RubyScope/*!*/ parent) {
            // RuntimeFlowControl:
            _activeFlowControlScope = parent.FlowControlScope;

            // RubyScope:
            _parent = parent;
            _top = parent.Top;
            _selfObject = parent.SelfObject;
            _methodAttributes = RubyMethodAttributes.PublicInstance;
            _locals = locals;
            _variableNames = variableNames;
            InLoop = parent.InLoop;
            InRescue = parent.InRescue;
        }
Exemplo n.º 8
0
 internal void SetEmptyLocals() {
     Debug.Assert(_variableNames == null);
     _variableNames = ArrayUtils.EmptyStrings;
     _locals = null;
 }
Exemplo n.º 9
0
 public static CodeContext CreateLocalContext(CodeContext/*!*/ outerContext, MutableTuple boxes, string[] args)
 {
     return new CodeContext(
         new TotemDictionary(
             new RuntimeVariablesDictionaryStorage(boxes, args)
         ),
         outerContext.GlobalContext,
         outerContext.LanguageContext);
 }
Exemplo n.º 10
0
        public static object MakeFunctionDebug(CodeContext context, FunctionCode funcInfo, object modName, MutableTuple closure, object[] defaults, Delegate target)
        {
            funcInfo.SetDebugTarget(context.LanguageContext, target);

            return new TotemFunction(context, funcInfo, modName, defaults, closure);
        }
Exemplo n.º 11
0
 public static object MakeFunction(CodeContext context, FunctionCode funcInfo, object modName, MutableTuple closure, object[] defaults)
 {
     return new TotemFunction(context, funcInfo, modName, defaults, closure);
 }
 public RuntimeVariablesDictionaryStorage(MutableTuple boxes, string[] args)
 {
     _boxes = boxes;
     _args = args;
 }
Exemplo n.º 13
0
        internal static TotemGenerator MakeGenerator(TotemFunction function, MutableTuple data, object generatorCode)
        {
            Func<MutableTuple, object> next = generatorCode as Func<MutableTuple, object>;
            if (next == null)
                next = ((LazyCode<Func<MutableTuple, object>>)generatorCode).EnsureDelegate();

            return new TotemGenerator(function, next, data);
        }
Exemplo n.º 14
0
        internal static Task<object> MakeAsync(TotemFunction function, MutableTuple data, object generatorCode)
        {
            Func<MutableTuple, object> next = generatorCode as Func<MutableTuple, object>;
            if (next == null)
                next = ((LazyCode<Func<MutableTuple, object>>)generatorCode).EnsureDelegate();

            var gen = new TotemAsyncStateMachine(function, next, data);
            return gen.Run();
        }
Exemplo n.º 15
0
        /// <summary>
        /// Gets the values from a tuple including unpacking nested values.
        /// </summary>
        public static object[] GetTupleValues(MutableTuple tuple) {
            ContractUtils.RequiresNotNull(tuple, "tuple");

            List<object> res = new List<object>();

            GetTupleValues(tuple, res);

            return res.ToArray();
        }
Exemplo n.º 16
0
        /// <summary>
        /// Python ctor - maps to function.__new__
        /// 
        /// y = func(x.__code__, globals(), 'foo', None, (a, ))
        /// </summary>
        public PythonFunction(CodeContext context, FunctionCode code, PythonDictionary globals, string name, PythonTuple defaults, PythonTuple closure) {
            if (closure != null && closure.__len__() != 0) {
                throw new NotImplementedException("non empty closure argument is not supported");
            }

            if (globals == context.GlobalDict) {
                _module = context.Module.GetName();
                _context = context;
            } else {
                _module = null;
                _context = new CodeContext(new PythonDictionary(), new ModuleContext(globals, DefaultContext.DefaultPythonContext));
            }

            _defaults = defaults == null ? ArrayUtils.EmptyObjects : defaults.ToArray();
            _code = code;
            _name = name;
            _doc = code._initialDoc;
            Closure = null;

            var scopeStatement = _code.PythonCode;
            if (scopeStatement.IsClosure) {
                throw new NotImplementedException("code containing closures is not supported");
            }
            scopeStatement.RewriteBody(FunctionDefinition.ArbitraryGlobalsVisitorInstance);

            _compat = CalculatedCachedCompat();
        }
Exemplo n.º 17
0
 private static void GetTupleValues(MutableTuple tuple, List<object> args) {
     Type[] types = tuple.GetType().GetGenericArguments();
     for (int i = 0; i < types.Length; i++) {
         if (typeof(MutableTuple).IsAssignableFrom(types[i])) {
             GetTupleValues((MutableTuple)tuple.GetValue(i), args);
         } else if (types[i] != typeof(DynamicNull)) {
             args.Add(tuple.GetValue(i));
         }
     }
 }
Exemplo n.º 18
0
        public void VerifyTuple(int size)
        {
            //Construct a tuple of the right type
            MethodInfo mi = typeof(MutableTuple).GetMethod("MakeTupleType", BindingFlags.Public | BindingFlags.Static);

            Assert(mi != null, "Could not find Tuple.MakeTupleType");

            Type[]   args   = new Type[size];
            object[] values = new object[size];
            for (int i = 0; i < size; i++)
            {
                args[i]   = typeof(int);
                values[i] = 0;
            }

            Type         tupleType = (Type)mi.Invoke(null, new object[] { args });
            MutableTuple t         = MutableTuple.MakeTuple(tupleType, values);

            /////////////////////
            //Properties

            //Write
            for (int i = 0; i < size; i++)
            {
                object o = t;
                foreach (PropertyInfo pi in MutableTuple.GetAccessPath(tupleType, i))
                {
                    if (typeof(MutableTuple).IsAssignableFrom(pi.PropertyType))
                    {
                        o = pi.GetValue(o, null);
                    }
                    else
                    {
                        pi.SetValue(o, i * 5, null);
                    }
                }
            }

            //Read
            for (int i = 0; i < size; i++)
            {
                object o = t;
                foreach (PropertyInfo pi in MutableTuple.GetAccessPath(tupleType, i))
                {
                    o = pi.GetValue(o, null);
                }
                AreEqual(typeof(int), o.GetType());
                AreEqual((int)o, i * 5);
            }

            //Negative cases for properties
            AssertError <ArgumentException>(delegate() {
                foreach (PropertyInfo pi in MutableTuple.GetAccessPath(tupleType, -1))
                {
                    Console.WriteLine(pi.Name); //This won't run, but we need it so that this call isn't inlined
                }
            });

            /////////////////////
            //GetTupleValues
            values = MutableTuple.GetTupleValues(t);
            AreEqual(values.Length, size);
            for (int i = 0; i < size; i++)
            {
                AreEqual(typeof(int), values[i].GetType());
                AreEqual((int)(values[i]), i * 5);
            }

            /////////////////////
            //Access methods

            if (size <= MutableTuple.MaxSize)
            {
                //SetValue
                for (int i = 0; i < size; i++)
                {
                    t.SetValue(i, i * 3);
                }

                //GetValue
                for (int i = 0; i < size; i++)
                {
                    AreEqual(t.GetValue(i), i * 3);
                }

                //Ensure there are no extras
                if (tupleType.GetGenericArguments().Length <= size)
                {
                    //We're requesting an index beyond the end of this tuple.
                    AssertError <ArgumentException>(delegate() { t.SetValue(size, 3); });
                    AssertError <ArgumentException>(delegate() { t.GetValue(size); });
                }
                else
                {
                    /*We're requesting an index in the scope of this tuple but beyond the scope of our
                     * requested capacity (in which case the field's type will be Microsoft.Scripting.None
                     * and we won't be able to convert "3" to that).  Imagine asking for a tuple of 3 ints,
                     * we'd actually get a Tuple<int,int,int,Microsoft.Scripting.None> since there is no
                     * Tuple that takes only 3 generic arguments.*/
                    AssertError <InvalidCastException>(delegate() { t.SetValue(size, 3); });

                    //Verify the type of the field
                    AreEqual(typeof(Microsoft.Scripting.Runtime.DynamicNull), tupleType.GetGenericArguments()[size]);

                    //Verify the value of the field is null
                    AreEqual(null, t.GetValue(size));
                }
            }
        }