Esempio n. 1
0
        public SymbolRef FindSymbolByName(string name)
        {
            if (m_ExecutionStack.Count > 0)
            {
                CallStackItem stackframe = GetTopNonClrFunction();

                if (stackframe != null)
                {
                    if (stackframe.Debug_Symbols != null)
                    {
                        for (int i = stackframe.Debug_Symbols.Length - 1; i >= 0; i--)
                        {
                            var l = stackframe.Debug_Symbols[i];

                            if (l.i_Name == name && stackframe.LocalScope[i] != null)
                            {
                                return(l);
                            }
                        }
                    }


                    var closure = stackframe.ClosureScope;

                    if (closure != null)
                    {
                        for (int i = 0; i < closure.Symbols.Length; i++)
                        {
                            if (closure.Symbols[i] == name)
                            {
                                return(SymbolRef.Upvalue(name, i));
                            }
                        }
                    }
                }
            }

            if (name != WellKnownSymbols.ENV)
            {
                SymbolRef env = FindSymbolByName(WellKnownSymbols.ENV);
                return(SymbolRef.Global(name, env));
            }
            else
            {
                return(SymbolRef.DefaultEnv);
            }
        }
Esempio n. 2
0
        public override void Compile(Execution.VM.ByteCode bc)
        {
            Instruction meta   = bc.Emit_Meta("<chunk-root>", OpCodeMetadataType.ChunkEntrypoint);
            int         metaip = bc.GetJumpPointForLastInstruction();

            bc.Emit_BeginFn(m_StackFrame);
            bc.Emit_Args(m_VarArgs);

            bc.Emit_Load(SymbolRef.Upvalue(WellKnownSymbols.ENV, 0));
            bc.Emit_Store(m_Env, 0, 0);
            bc.Emit_Pop();

            m_Block.Compile(bc);
            bc.Emit_Ret(0);

            meta.NumVal = bc.GetJumpPointForLastInstruction() - metaip;
        }
        public int CompileBody(ByteCode bc, string friendlyName)
        {
            string funcName = friendlyName ?? $"<{_begin.FormatLocation(bc.Script)}>";

            bc.PushSourceRef(_begin);

            var I = bc.Emit_Jump(OpCode.Jump, -1);

            var meta   = bc.Emit_Meta(funcName, OpCodeMetadataType.FunctionEntrypoint);
            int metaip = bc.GetJumpPointForLastInstruction();

            bc.Emit_BeginFn(_stackFrame);

            bc.LoopTracker.Loops.Push(new LoopBoundary());

            int entryPoint = bc.GetJumpPointForLastInstruction();

            if (_usesGlobalEnv)
            {
                bc.Emit_Load(SymbolRef.Upvalue(WellKnownSymbols.ENV, 0));
                bc.Emit_Store(_env, 0, 0);
                bc.Emit_Pop();
            }

            if (_paramNames.Length > 0)
            {
                bc.Emit_Args(_paramNames);
            }

            _statement.Compile(bc);

            bc.PopSourceRef();
            bc.PushSourceRef(_end);

            bc.Emit_Ret(0);

            bc.LoopTracker.Loops.Pop();

            I.NumVal    = bc.GetJumpPointForNextInstruction();
            meta.NumVal = bc.GetJumpPointForLastInstruction() - metaip;

            bc.PopSourceRef();

            return(entryPoint);
        }
        public int CompileBody(ByteCode bc, string friendlyName)
        {
            string funcName = friendlyName ?? ("<" + this.m_Begin.FormatLocation(bc.Script, true) + ">");

            bc.PushSourceRef(m_Begin);

            int I = bc.Emit_Jump(OpCode.Jump, -1);

            int meta = bc.Emit_Meta(funcName, OpCodeMetadataType.FunctionEntrypoint);

            bc.Emit_BeginFn(m_StackFrame);

            bc.LoopTracker.Loops.Push(new LoopBoundary());

            int entryPoint = bc.GetJumpPointForLastInstruction();

            if (m_UsesGlobalEnv)
            {
                bc.Emit_Load(SymbolRef.Upvalue(WellKnownSymbols.ENV, 0));
                bc.Emit_Store(m_Env, 0, 0);
                bc.Emit_Pop();
            }

            if (m_ParamNames.Length > 0)
            {
                bc.Emit_Args(m_ParamNames);
            }

            m_Statement.Compile(bc);

            bc.PopSourceRef();
            bc.PushSourceRef(m_End);

            bc.Emit_Ret(0);

            bc.LoopTracker.Loops.Pop();

            bc.SetNumVal(I, bc.GetJumpPointForNextInstruction());
            bc.SetNumVal(meta, bc.GetJumpPointForLastInstruction() - meta);

            bc.PopSourceRef();

            return(entryPoint);
        }
        public SymbolRef CreateUpvalue(BuildTimeScope scope, SymbolRef symbol)
        {
            for (int i = 0; i < _closure.Count; i++)
            {
                if (_closure[i]._name == symbol._name)
                {
                    return(SymbolRef.Upvalue(symbol._name, i));
                }
            }

            _closure.Add(symbol);

            if (_closureInstruction != null)
            {
                _closureInstruction.SymbolList = _closure.ToArray();
            }

            return(SymbolRef.Upvalue(symbol._name, _closure.Count - 1));
        }
        public SymbolRef CreateUpvalue(BuildTimeScope scope, SymbolRef symbol)
        {
            for (int i = 0; i < m_Closure.Count; i++)
            {
                if (m_Closure[i].i_Name == symbol.i_Name)
                {
                    return(SymbolRef.Upvalue(symbol.i_Name, i));
                }
            }

            m_Closure.Add(symbol);

            if (m_ClosureInstruction != -1)
            {
                var i = m_bc.Code[m_ClosureInstruction];
                i.SymbolList = m_Closure.ToArray();
                m_bc.Code[m_ClosureInstruction] = i;
            }

            return(SymbolRef.Upvalue(symbol.i_Name, m_Closure.Count - 1));
        }