public void a(int t, string s, double x, LuaTable tbl, object o, bool b) { //Console.WriteLine(self); Console.WriteLine(t==null); Console.WriteLine(s==null); Console.WriteLine(x==null); Console.WriteLine(tbl==null); Console.WriteLine(o==null); Console.WriteLine(b==null); }
/* * Gets an instance of an implementation of the klass interface or * subclass of klass that delegates public virtual methods to the * luaTable table. * Caches the generated type. */ public object GetClassInstance(Type klass, LuaTable luaTable) { LuaClassType luaClassType; if (classCollection.ContainsKey(klass)) { luaClassType = classCollection[klass]; } else { luaClassType = new LuaClassType(); GenerateClass(klass, out luaClassType.klass, out luaClassType.returnTypes, luaTable); classCollection[klass] = luaClassType; } return Activator.CreateInstance(luaClassType.klass, new object[] { luaTable, luaClassType.returnTypes }); }
/* * Gets the function called name from the provided table, * returning null if it does not exist */ public static LuaFunction getTableFunction(LuaTable luaTable, string name) { object funcObj = luaTable.rawget(name); if (funcObj is LuaFunction) return (LuaFunction)funcObj; else return null; }
/* * Generates an implementation of klass, if it is an interface, or * a subclass of klass that delegates its virtual methods to a Lua table. */ public void GenerateClass(Type klass, out Type newType, out Type[][] returnTypes, LuaTable luaTable) { string typeName; lock (this) { typeName = "LuaGeneratedClass" + luaClassNumber; luaClassNumber++; } TypeBuilder myType; // Define a public class in the assembly, called typeName if (klass.IsInterface) myType = newModule.DefineType(typeName, TypeAttributes.Public, typeof(object), new Type[] { klass, typeof(ILuaGeneratedType) }); else myType = newModule.DefineType(typeName, TypeAttributes.Public, klass, new Type[] { typeof(ILuaGeneratedType) }); // Field that stores the Lua table FieldBuilder luaTableField = myType.DefineField("__luaInterface_luaTable", typeof(LuaTable), FieldAttributes.Public); // Field that stores the return types array FieldBuilder returnTypesField = myType.DefineField("__luaInterface_returnTypes", typeof(Type[][]), FieldAttributes.Public); // Generates the constructor for the new type, it takes a Lua table and an array // of return types and stores them in the respective fields ConstructorBuilder constructor = myType.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(LuaTable), typeof(Type[][]) }); ILGenerator generator = constructor.GetILGenerator(); generator.Emit(OpCodes.Ldarg_0); if (klass.IsInterface) generator.Emit(OpCodes.Call, typeof(object).GetConstructor(Type.EmptyTypes)); else generator.Emit(OpCodes.Call, klass.GetConstructor(Type.EmptyTypes)); generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldarg_1); generator.Emit(OpCodes.Stfld, luaTableField); generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldarg_2); generator.Emit(OpCodes.Stfld, returnTypesField); generator.Emit(OpCodes.Ret); // Generates overriden versions of the klass' public and protected virtual methods BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; MethodInfo[] classMethods = klass.GetMethods(flags); returnTypes = new Type[classMethods.Length][]; int i = 0; foreach (MethodInfo method in classMethods) { if (klass.IsInterface) { GenerateMethod(myType, method, MethodAttributes.HideBySig | MethodAttributes.Virtual | MethodAttributes.NewSlot, i, luaTableField, returnTypesField, false, out returnTypes[i]); i++; } else { if (!method.IsPrivate && !method.IsFinal && method.IsVirtual && luaTable[method.Name] != null) { GenerateMethod(myType, method, (method.Attributes | MethodAttributes.NewSlot) ^ MethodAttributes.NewSlot, i, luaTableField, returnTypesField, true, out returnTypes[i]); i++; } } } // Generates an implementation of the __luaInterface_getLuaTable method MethodBuilder returnTableMethod = myType.DefineMethod("__luaInterface_getLuaTable", MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual, typeof(LuaTable), new Type[0]); myType.DefineMethodOverride(returnTableMethod, typeof(ILuaGeneratedType).GetMethod("__luaInterface_getLuaTable")); generator = returnTableMethod.GetILGenerator(); generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldfld, luaTableField); generator.Emit(OpCodes.Ret); // Creates the type newType = myType.CreateType(); }
public ListDictionary GetTableDict(LuaTable table) { ListDictionary dict = new ListDictionary(); int oldTop = LuaDLL.lua_gettop(luaState); translator.push(luaState, table); LuaDLL.lua_pushnil(luaState); while (LuaDLL.lua_next(luaState, -2) != 0) { dict[translator.getObject(luaState, -2)] = translator.getObject(luaState, -1); LuaDLL.lua_settop(luaState, -2); } LuaDLL.lua_settop(luaState, oldTop); return dict; }