Exemplo n.º 1
0
        ///// <summary>
        ///// Loads and executes a stream containing a Lua/MoonSharp script.
        ///// </summary>
        ///// <param name="stream">The stream.</param>
        ///// <param name="globalContext">The global context.</param>
        ///// <param name="codeFriendlyName">Name of the code - used to report errors, etc. Also used by debuggers to locate the original source file.</param>
        ///// <returns>
        ///// A DynValue containing the result of the processing of the loaded chunk.
        ///// </returns>
        //public DynValue DoStream(Stream stream, Table globalContext = null, string codeFriendlyName = null)
        //{
        //	DynValue func = LoadStream(stream, globalContext, codeFriendlyName);
        //	return Call(func);
        //}


        ///// <summary>
        ///// Loads and executes a file containing a Lua/MoonSharp script.
        ///// </summary>
        ///// <param name="filename">The filename.</param>
        ///// <param name="globalContext">The global context.</param>
        ///// <param name="codeFriendlyName">Name of the code - used to report errors, etc. Also used by debuggers to locate the original source file.</param>
        ///// <returns>
        ///// A DynValue containing the result of the processing of the loaded chunk.
        ///// </returns>
        //public DynValue DoFile(string filename, Table globalContext = null, string codeFriendlyName = null)
        //{
        //	DynValue func = LoadFile(filename, globalContext, codeFriendlyName);
        //	return Call(func);
        //}


        ///// <summary>
        ///// Runs the specified file with all possible defaults for quick experimenting.
        ///// </summary>
        ///// <param name="filename">The filename.</param>
        ///// A DynValue containing the result of the processing of the executed script.
        //public static DynValue RunFile(string filename)
        //{
        //	Script S = new Script();
        //	return S.DoFile(filename);
        //}

        ///// <summary>
        ///// Runs the specified code with all possible defaults for quick experimenting.
        ///// </summary>
        ///// <param name="code">The Lua/MoonSharp code.</param>
        ///// A DynValue containing the result of the processing of the executed script.
        //public static DynValue RunString(string code)
        //{
        //	Script S = new Script();
        //	return S.DoString(code);
        //}

        /// <summary>
        /// Creates a closure from a bytecode address.
        /// </summary>
        /// <param name="address">The address.</param>
        /// <param name="envTable">The env table to create a 0-upvalue</param>
        /// <returns></returns>
        private DynValue MakeClosure(int address, Table envTable = null)
        {
            this.CheckScriptOwnership(envTable);
            Closure c;

            if (envTable == null)
            {
                Instruction meta = m_MainProcessor.FindMeta(ref address);

                // if we find the meta for a new chunk, we use the value in the meta for the _ENV upvalue
                if ((meta != null) && (meta.NumVal2 == (int)OpCodeMetadataType.ChunkEntrypoint))
                {
                    c = new Closure(this, address,
                                    new SymbolRef[] { SymbolRef.Upvalue(WellKnownSymbols.ENV, 0) },
                                    new DynValue[] { meta.Value });
                }
                else
                {
                    c = new Closure(this, address, new SymbolRef[0], new DynValue[0]);
                }
            }
            else
            {
                var syms = new SymbolRef[] {
                    new SymbolRef()
                    {
                        i_Env = null, i_Index = 0, i_Name = WellKnownSymbols.ENV, i_Type = SymbolRefType.DefaultEnv
                    },
                };

                var vals = new DynValue[] {
                    DynValue.NewTable(envTable)
                };

                c = new Closure(this, address, syms, vals);
            }

            return(DynValue.NewClosure(c));
        }