/// <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) { c = new Closure(this, address, new SymbolRef[0], new DynValue[0]); } else { var syms = new SymbolRef[1] { new SymbolRef() { i_Env = null, i_Index = 0, i_Name = WellKnownSymbols.ENV, i_Type = SymbolRefType.DefaultEnv }, }; var vals = new DynValue[1] { DynValue.NewTable(envTable) }; c = new Closure(this, address, syms, vals); } return(DynValue.NewClosure(c)); }
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(Platform.Current.Name)); return(table); }
/// <summary> /// Registers the standard constants (_G, _VERSION, _MOONSHARP) to a table /// </summary> /// <param name="table">The table.</param> public static Table RegisterConstants(this Table table) { var moonsharpTable = DynValue.NewTable(table.OwnerScript); var m = moonsharpTable.Table; table.Set("_G", DynValue.NewTable(table)); table.Set("_VERSION", DynValue.NewString($"MoonSharp {Script.VERSION}")); table.Set("_MOONSHARP", moonsharpTable); m.Set("version", DynValue.NewString(Script.VERSION)); m.Set("luacompat", DynValue.NewString(Script.LUA_VERSION)); m.Set("is_aot", DynValue.NewBoolean(Script.GlobalOptions.Platform.IsRunningOnAOT())); m.Set("is_mono", DynValue.NewBoolean(PlatformAutoDetector.IsRunningOnMono)); return(table); }
private static Table CreateModuleNamespace(Table gtable, Type t) { MoonSharpModuleAttribute attr = (MoonSharpModuleAttribute)t.GetCustomAttributes(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); } }
/// <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) { if (!m_isAlive) { throw new InvalidOperationException(string.Format("Attempting to make a closure on dead Script [{0}]", FriendlyName)); } 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)) { var index = HeapAllocatedDynValue.Allocate(ref meta.Value); ClosureRefValue refValue = new ClosureRefValue(SymbolRef.Upvalue(WellKnownSymbols.ENV, 0), index); c = new Closure(this, address, new ClosureRefValue[] { refValue }); HeapAllocatedDynValue.DecreaseReferenceCount(index); } else { ClosureRefValue[] refValue = new ClosureRefValue[0]; c = new Closure(this, address, refValue); } } else { var syms = new SymbolRef[] { new SymbolRef() { i_Env = null, i_Index = 0, i_Name = WellKnownSymbols.ENV, i_Type = SymbolRefType.DefaultEnv }, }; var d = DynValue.NewTable(envTable); var index = HeapAllocatedDynValue.Allocate(ref d); ClosureRefValue refValue = new ClosureRefValue(syms[0], index); c = new Closure(this, address, new ClosureRefValue[] { refValue }); HeapAllocatedDynValue.DecreaseReferenceCount(index); } return(DynValue.NewClosure(c)); }
/// <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)); return(table); }
/// <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)); }
public static DynValue ClassToLuaTable(Script script, object obj) { MoonSharp.Interpreter.Table table = new MoonSharp.Interpreter.Table(script); var properties = obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly); for (int i = 0; i < properties.Length; i++) { var property = properties[i]; var value = property.GetValue(obj, null); if (value == null) { continue; } if (value.GetType() == typeof(string)) { table.Set(property.Name, DynValue.NewString((string)value)); } else if (value.GetType() == typeof(int)) { table.Set(property.Name, DynValue.NewNumber((int)value)); } else if (value.GetType() == typeof(float)) { table.Set(property.Name, DynValue.NewNumber((float)value)); } else if (value.GetType() == typeof(bool)) { table.Set(property.Name, DynValue.NewBoolean((bool)value)); } else if (value is ProtoBuf.IExtensible) { table.Set(property.Name, ClassToLuaTable(script, value)); } else if (value is IList) { IList list = value as IList; MoonSharp.Interpreter.Table value_table = new MoonSharp.Interpreter.Table(script); for (int j = 0; j < list.Count; ++j) { DynValue item = null; if (list[j] is ProtoBuf.IExtensible) { item = ClassToLuaTable(script, list[j]); } else if (list[j] is int) { item = DynValue.NewNumber((int)list[j]); } else if (list[j] is float) { item = DynValue.NewNumber((float)list[j]); } else if (list[j] is string) { item = DynValue.NewString((string)list[j]); } else if (list[j] is bool) { item = DynValue.NewBoolean((bool)list[j]); } else { Debug.LogError("Convert List to lua table error: unsupported types " + list[j].GetType()); } value_table.Append(item); } table.Set(property.Name, DynValue.NewTable(value_table)); } else { Debug.LogError("Convert class to lua table error: unsupported types " + value); } } return(DynValue.NewTable(table)); }