Пример #1
0
        private static Table CreateModuleNamespace(Table gtable, Type t)
        {
            MoonSharpModuleAttribute attr = (MoonSharpModuleAttribute)(Framework.Do.GetCustomAttributes(t, typeof(MoonSharpModuleAttribute), false).First());

            if (string.IsNullOrEmpty(attr.Namespace))
            {
                return(gtable);
            }
            else
            {
                Table table = null;

                DynValue found = gtable.Get(attr.Namespace);

                if (found.Type == DataType.Table)
                {
                    table = found.Table;
                }
                else
                {
                    table = new Table(gtable.OwnerScript);
                    gtable.Set(attr.Namespace, DynValue.NewTable(table));
                }


                DynValue package = gtable.RawGet("package");

                if (package == null || package.Type != DataType.Table)
                {
                    gtable.Set("package", package = DynValue.NewTable(gtable.OwnerScript));
                }


                DynValue loaded = package.Table.RawGet("loaded");

                if (loaded == null || loaded.Type != DataType.Table)
                {
                    package.Table.Set("loaded", loaded = DynValue.NewTable(gtable.OwnerScript));
                }

                loaded.Table.Set(attr.Namespace, DynValue.NewTable(table));

                return(table);
            }
        }
Пример #2
0
        /// <summary>
        /// Registers the standard constants (_G, _VERSION, _MOONSHARP) to a table
        /// </summary>
        /// <param name="table">The table.</param>
        /// <returns></returns>
        public static Table RegisterConstants(this Table table)
        {
            DynValue moonsharp_table = DynValue.NewTable(table.OwnerScript);
            Table    m = moonsharp_table.Table;

            table.Set("_G", DynValue.NewTable(table));
            table.Set("_VERSION", DynValue.NewString(string.Format("MoonSharp {0}", Script.VERSION)));
            table.Set("_MOONSHARP", moonsharp_table);

            m.Set("version", DynValue.NewString(Script.VERSION));
            m.Set("luacompat", DynValue.NewString(Script.LUA_VERSION));
            m.Set("platform", DynValue.NewString(Script.GlobalOptions.Platform.GetPlatformName()));
            m.Set("is_aot", DynValue.NewBoolean(Script.GlobalOptions.Platform.IsRunningOnAOT()));
            m.Set("is_unity", DynValue.NewBoolean(PlatformAutoDetector.IsRunningOnUnity));
            m.Set("is_mono", DynValue.NewBoolean(PlatformAutoDetector.IsRunningOnMono));
            m.Set("is_clr4", DynValue.NewBoolean(PlatformAutoDetector.IsRunningOnClr4));
            m.Set("is_pcl", DynValue.NewBoolean(PlatformAutoDetector.IsPortableFramework));
            m.Set("banner", DynValue.NewString(Script.GetBanner()));

            return(table);
        }
Пример #3
0
        /// <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));
        }