public Lua(bool enableLengthWorkaround) { // NOTE: You should enable the length workaround on Mac and Linux this.enableLengthWorkaround = enableLengthWorkaround; luaState = LuaDLL.luaL_newstate(); // steffenj: Lua 5.1.1 API change (lua_open is gone) // Load libraries LuaDLL.luaL_openlibs(luaState); // Add LuaInterface marker LuaDLL.lua_pushstring(luaState, "LUAINTERFACE LOADED"); LuaDLL.lua_pushboolean(luaState, true); LuaDLL.lua_settable(luaState, (int)LuaIndexes.LUA_REGISTRYINDEX); translator = new ObjectTranslator(this, luaState); tracebackFunction = new LuaCSFunction(traceback); // We need to keep this in a managed reference so the delegate doesn't get garbage collected panicCallback = new LuaFunctionCallback(PanicCallback); LuaDLL.lua_atpanic(luaState, panicCallback); }
public Lua() { luaState = LuaDLL.luaL_newstate(); // steffenj: Lua 5.1.1 API change (lua_open is gone) //LuaDLL.luaopen_base(luaState); // steffenj: luaopen_* no longer used LuaDLL.luaL_openlibs(luaState); // steffenj: Lua 5.1.1 API change (luaopen_base is gone, just open all libs right here) LuaDLL.lua_pushstring(luaState, "LUAINTERFACE LOADED"); LuaDLL.lua_pushboolean(luaState, true); LuaDLL.lua_settable(luaState, LuaIndexes.LUA_REGISTRYINDEX); LuaDLL.lua_newtable(luaState); LuaDLL.lua_setglobal(luaState, "luanet"); LuaDLL.lua_pushvalue(luaState, LuaIndexes.LUA_GLOBALSINDEX); LuaDLL.lua_getglobal(luaState, "luanet"); LuaDLL.lua_pushstring(luaState, "getmetatable"); LuaDLL.lua_getglobal(luaState, "getmetatable"); LuaDLL.lua_settable(luaState, -3); LuaDLL.lua_replace(luaState, LuaIndexes.LUA_GLOBALSINDEX); translator = new ObjectTranslator(this, luaState); LuaDLL.lua_replace(luaState, LuaIndexes.LUA_GLOBALSINDEX); LuaDLL.luaL_dostring(luaState, Lua.init_luanet); // steffenj: lua_dostring renamed to luaL_dostring // We need to keep this in a managed reference so the delegate doesn't get garbage collected panicCallback = new LuaFunctionCallback(PanicCallback); LuaDLL.lua_atpanic(luaState, panicCallback); }
public void LuaPushBoolean(bool value) { LuaDLL.lua_pushboolean(L, value ? 1 : 0); }
public LuaState() { // Create State L = LuaDLL.luaL_newstate(); // Create LuaInterface library LuaDLL.luaL_openlibs(L); LuaDLL.lua_pushstring(L, "LUAINTERFACE LOADED"); LuaDLL.lua_pushboolean(L, true); LuaDLL.lua_settable(L, (int)LuaIndexes.LUA_REGISTRYINDEX); LuaDLL.lua_newtable(L); LuaDLL.lua_setglobal(L, "luanet"); LuaDLL.lua_pushvalue(L, (int)LuaIndexes.LUA_GLOBALSINDEX); LuaDLL.lua_getglobal(L, "luanet"); LuaDLL.lua_pushstring(L, "getmetatable"); LuaDLL.lua_getglobal(L, "getmetatable"); LuaDLL.lua_settable(L, -3); // Set luanet as global for object translator LuaDLL.lua_replace(L, (int)LuaIndexes.LUA_GLOBALSINDEX); translator = new ObjectTranslator(this, L); LuaDLL.lua_replace(L, (int)LuaIndexes.LUA_GLOBALSINDEX); translator.PushTranslator(L); //GCHandle handle = GCHandle.Alloc(translator, GCHandleType.Pinned); //IntPtr thisptr = GCHandle.ToIntPtr(handle); //LuaDLL.lua_pushlightuserdata(L, thisptr); //LuaDLL.lua_setglobal(L, "_translator"); // We need to keep this in a managed reference so the delegate doesn't get garbage collected panicCallback = new LuaCSFunction(LuaStatic.panic); LuaDLL.lua_atpanic(L, panicCallback); printFunction = new LuaCSFunction(LuaStatic.print); LuaDLL.lua_pushstdcallcfunction(L, printFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "print"); loadfileFunction = new LuaCSFunction(LuaStatic.loadfile); LuaDLL.lua_pushstdcallcfunction(L, loadfileFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "loadfile"); dofileFunction = new LuaCSFunction(LuaStatic.dofile); LuaDLL.lua_pushstdcallcfunction(L, dofileFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "dofile"); // Insert our loader FIRST loaderFunction = new LuaCSFunction(LuaStatic.loader); LuaDLL.lua_pushstdcallcfunction(L, loaderFunction); int loaderFunc = LuaDLL.lua_gettop(L); LuaDLL.lua_getfield(L, LuaIndexes.LUA_GLOBALSINDEX, "package"); LuaDLL.lua_getfield(L, -1, "loaders"); int loaderTable = LuaDLL.lua_gettop(L); // Shift table elements right for (int e = LuaDLL.luaL_getn(L, loaderTable) + 1; e > 1; e--) { LuaDLL.lua_rawgeti(L, loaderTable, e - 1); LuaDLL.lua_rawseti(L, loaderTable, e); } LuaDLL.lua_pushvalue(L, loaderFunc); LuaDLL.lua_rawseti(L, loaderTable, 1); LuaDLL.lua_settop(L, 0); DoString(LuaStatic.init_luanet); tracebackFunction = new LuaCSFunction(LuaStatic.traceback); }
/* * Pushes the object into the Lua stack according to its type. */ internal void push(IntPtr luaState, object o) { if (o == null) { LuaDLL.lua_pushnil(luaState); return; } Type t = o.GetType(); if (t == typeof(bool)) { bool b = (bool)o; LuaDLL.lua_pushboolean(luaState, b); } else if (t == typeof(UnityEngine.Object)) { UnityEngine.Object obj = (UnityEngine.Object)o; if (obj == null) { LuaDLL.lua_pushnil(luaState); return; } else { pushObject(luaState, o, "luaNet_metatable"); } } else if (t.IsEnum) { LuaScriptMgr.PushEnum(luaState, o); } else if (t.IsArray) { LuaScriptMgr.PushArray(luaState, o); } else if (t.IsPrimitive) { double d = Convert.ToDouble(o); LuaDLL.lua_pushnumber(luaState, d); } else if (t == typeof(string)) { string str = (string)o; LuaDLL.lua_pushstring(luaState, str); } else if (IsILua(o)) { #if !__NOGEN__ (((ILuaGeneratedType)o).__luaInterface_getLuaTable()).push(luaState); #endif } else if (t == typeof(LuaTable)) { ((LuaTable)o).push(luaState); } else if (t == typeof(LuaCSFunction)) { pushFunction(luaState, (LuaCSFunction)o); } else if (t == typeof(LuaFunction)) { ((LuaFunction)o).push(luaState); } else if (t.IsValueType) { int index = addObject(o); PushNewValueObject(luaState, o, index, "luaNet_metatable"); } else { pushObject(luaState, o, "luaNet_metatable"); } }
/* * Called by the __index metafunction of CLR objects in case the * method is not cached or it is a field/property/event. * Receives the object and the member name as arguments and returns * either the value of the member or a delegate to call it. * If the member does not exist returns nil. */ private int getMethod(IntPtr luaState) { object obj = translator.getRawNetObject(luaState, 1); if (obj == null) { translator.throwError(luaState, "trying to index an invalid object reference"); LuaDLL.lua_pushnil(luaState); return(1); } object index = translator.getObject(luaState, 2); Type indexType = index.GetType(); //* not used string methodName = index as string; // will be null if not a string arg Type objType = obj.GetType(); // Handle the most common case, looking up the method by name. // CP: This will fail when using indexers and attempting to get a value with the same name as a property of the object, // ie: xmlelement['item'] <- item is a property of xmlelement try { if (methodName != null && isMemberPresent(objType, methodName)) { return(getMember(luaState, objType, obj, methodName, BindingFlags.Instance | BindingFlags.IgnoreCase)); } } catch { } bool failed = true; // Try to access by array if the type is right and index is an int (lua numbers always come across as double) if (objType.IsArray && index is double) { int intIndex = (int)((double)index); Array aa = obj as Array; if (intIndex >= aa.Length) { return(translator.pushError(luaState, "array index out of bounds: " + intIndex + " " + aa.Length)); } object val = aa.GetValue(intIndex); translator.push(luaState, val); failed = false; } else { // Try to use get_Item to index into this .net object //MethodInfo getter = objType.GetMethod("get_Item"); // issue here is that there may be multiple indexers.. MethodInfo[] methods = objType.GetMethods(); foreach (MethodInfo mInfo in methods) { if (mInfo.Name == "get_Item") { //check if the signature matches the input if (mInfo.GetParameters().Length == 1) { MethodInfo getter = mInfo; ParameterInfo[] actualParms = (getter != null) ? getter.GetParameters() : null; if (actualParms == null || actualParms.Length != 1) { return(translator.pushError(luaState, "method not found (or no indexer): " + index)); } else { // Get the index in a form acceptable to the getter index = translator.getAsType(luaState, 2, actualParms[0].ParameterType); // Just call the indexer - if out of bounds an exception will happen try { object result = getter.Invoke(obj, new object[] { index }); translator.push(luaState, result); failed = false; } catch (TargetInvocationException e) { // Provide a more readable description for the common case of key not found if (e.InnerException is KeyNotFoundException) { return(translator.pushError(luaState, "key '" + index + "' not found ")); } else { return(translator.pushError(luaState, "exception indexing '" + index + "' " + e.Message)); } } } } } } } if (failed) { return(translator.pushError(luaState, "cannot find " + index)); } LuaDLL.lua_pushboolean(luaState, false); return(2); }
/*static void PushNull(IntPtr L) * { * LuaDLL.tolua_pushudata(L, 1); * }*/ //PushVarObject public static void Push(IntPtr L, object obj) { if (obj == null) { LuaDLL.lua_pushnil(L); return; } Type t = obj.GetType(); if (t.IsValueType) { if (t == typeof(bool)) { bool b = (bool)obj; LuaDLL.lua_pushboolean(L, b); } else if (t.IsEnum) { Push(L, (System.Enum)obj); } else if (t.IsPrimitive) { double d = LuaMisc.ToDouble(obj); LuaDLL.lua_pushnumber(L, d); } else if (t == typeof(Vector3)) { Push(L, (Vector3)obj); } else if (t == typeof(Quaternion)) { Push(L, (Quaternion)obj); } else if (t == typeof(Vector2)) { Push(L, (Vector2)obj); } else if (t == typeof(Vector4)) { Push(L, (Vector4)obj); } else if (t == typeof(Color)) { Push(L, (Color)obj); } else if (t == typeof(RaycastHit)) { Push(L, (RaycastHit)obj); } else if (t == typeof(Touch)) { Push(L, (Touch)obj); } else if (t == typeof(Ray)) { Push(L, (Ray)obj); } else if (t == typeof(Bounds)) { Push(L, (Bounds)obj); } else if (t == typeof(LayerMask)) { PushLayerMask(L, (LayerMask)obj); } else { PushValue(L, (ValueType)obj); } } else { if (t.IsArray) { Push(L, (Array)obj); } else if (t == typeof(string)) { LuaDLL.lua_pushstring(L, (string)obj); } else if (t.IsSubclassOf(typeof(LuaBaseRef))) { Push(L, (LuaBaseRef)obj); } else if (t.IsSubclassOf(typeof(UnityEngine.Object))) { Push(L, (UnityEngine.Object)obj); } else if (t.IsSubclassOf(typeof(UnityEngine.TrackedReference))) { Push(L, (UnityEngine.TrackedReference)obj); } else if (t == typeof(LuaByteBuffer)) { LuaByteBuffer lbb = (LuaByteBuffer)obj; LuaDLL.lua_pushlstring(L, lbb.buffer, lbb.buffer.Length); } else if (t.IsSubclassOf(typeof(Delegate))) { Push(L, (Delegate)obj); } else if (obj is System.Collections.IEnumerator) { Push(L, (IEnumerator)obj); } else if (t == typeof(EventObject)) { Push(L, (EventObject)obj); } else if (t == monoType) { Push(L, (Type)obj); } else { PushObject(L, obj); } } }
/* * Called by the __index metafunction of CLR objects in case the * method is not cached or it is a field/property/event. * Receives the object and the member name as arguments and returns * either the value of the member or a delegate to call it. * If the member does not exist returns nil. */ private int getMethod(IntPtr luaState) { object obj = translator.getRawNetObject(luaState, 1); if (obj == null) { translator.throwError(luaState, "trying to index an invalid object reference"); LuaDLL.lua_pushnil(luaState); return(1); } object index = translator.getObject(luaState, 2); Type indexType = index.GetType(); string methodName = index as string; // will be null if not a string arg Type objType = obj.GetType(); // Handle the most common case, looking up the method by name if (methodName != null && isMemberPresent(objType, methodName)) { return(getMember(luaState, objType, obj, methodName, BindingFlags.Instance)); } // Try to access by array if the type is right and index is an int (lua numbers always come across as double) if (objType.IsArray && index is double) { object[] arr = (object[])obj; translator.push(luaState, arr[(int)((double)index)]); } else { // Try to use get_Item to index into this .net object MethodInfo getter = objType.GetMethod("get_Item"); ParameterInfo[] actualParms = (getter != null) ? getter.GetParameters() : null; if (actualParms == null || actualParms.Length != 1) { translator.throwError(luaState, "method not found (or no indexer): " + index); LuaDLL.lua_pushnil(luaState); } else { // Get the index in a form acceptable to the getter index = translator.getAsType(luaState, 2, actualParms[0].ParameterType); object[] args = new object[1]; // Just call the indexer - if out of bounds an exception will happen args[0] = index; try { object result = getter.Invoke(obj, args); translator.push(luaState, result); } catch (TargetInvocationException e) { // Provide a more readable description for the common case of key not found if (e.InnerException is KeyNotFoundException) { translator.throwError(luaState, "key '" + index + "' not found "); } else { translator.throwError(luaState, "exception indexing '" + index + "' " + e.Message); } LuaDLL.lua_pushnil(luaState); } } } LuaDLL.lua_pushboolean(luaState, false); return(2); }
public static int getMethod(IntPtr luaState) { ObjectTranslator objectTranslator = ObjectTranslator.FromState(luaState); object rawNetObject = objectTranslator.getRawNetObject(luaState, 1); if (rawNetObject == null) { objectTranslator.throwError(luaState, "trying to index an invalid object reference"); LuaDLL.lua_pushnil(luaState); return(1); } object obj = objectTranslator.getObject(luaState, 2); string text = obj as string; Type type = rawNetObject.GetType(); try { if (text != null && objectTranslator.metaFunctions.isMemberPresent(type, text)) { int result = objectTranslator.metaFunctions.getMember(luaState, type, rawNetObject, text, BindingFlags.IgnoreCase | BindingFlags.Instance); return(result); } } catch { } bool flag = true; if (type.IsArray && obj is double) { int num = (int)((double)obj); Array array = rawNetObject as Array; if (num >= array.Length) { return(objectTranslator.pushError(luaState, string.Concat(new object[] { "array index out of bounds: ", num, " ", array.Length }))); } object value = array.GetValue(num); objectTranslator.push(luaState, value); flag = false; } else { MethodInfo[] methods = type.GetMethods(); MethodInfo[] array2 = methods; for (int i = 0; i < array2.Length; i++) { MethodInfo methodInfo = array2[i]; if (methodInfo.Name == "get_Item" && methodInfo.GetParameters().Length == 1) { MethodInfo methodInfo2 = methodInfo; ParameterInfo[] array3 = (methodInfo2 == null) ? null : methodInfo2.GetParameters(); if (array3 == null || array3.Length != 1) { return(objectTranslator.pushError(luaState, "method not found (or no indexer): " + obj)); } obj = objectTranslator.getAsType(luaState, 2, array3[0].ParameterType); try { object o = methodInfo2.Invoke(rawNetObject, new object[] { obj }); objectTranslator.push(luaState, o); flag = false; } catch (TargetInvocationException ex) { int result; if (ex.InnerException is KeyNotFoundException) { result = objectTranslator.pushError(luaState, "key '" + obj + "' not found "); return(result); } result = objectTranslator.pushError(luaState, string.Concat(new object[] { "exception indexing '", obj, "' ", ex.Message })); return(result); } } } } if (flag) { return(objectTranslator.pushError(luaState, "cannot find " + obj)); } LuaDLL.lua_pushboolean(luaState, false); return(2); }
public static void Push(IntPtr L, object obj) { if (obj == null) { LuaDLL.lua_pushnil(L); return; } Type type = obj.GetType(); if (type.get_IsValueType()) { if (type == typeof(bool)) { bool value = (bool)obj; LuaDLL.lua_pushboolean(L, value); } else if (type.get_IsEnum()) { ToLua.Push(L, (Enum)obj); } else if (type.get_IsPrimitive()) { double number = LuaMisc.ToDouble(obj); LuaDLL.lua_pushnumber(L, number); } else if (type == typeof(Vector3)) { ToLua.Push(L, (Vector3)obj); } else if (type == typeof(Quaternion)) { ToLua.Push(L, (Quaternion)obj); } else if (type == typeof(Vector2)) { ToLua.Push(L, (Vector2)obj); } else if (type == typeof(Vector4)) { ToLua.Push(L, (Vector4)obj); } else if (type == typeof(Color)) { ToLua.Push(L, (Color)obj); } else if (type == typeof(RaycastHit)) { ToLua.Push(L, (RaycastHit)obj); } else if (type == typeof(Touch)) { ToLua.Push(L, (Touch)obj); } else if (type == typeof(Ray)) { ToLua.Push(L, (Ray)obj); } else if (type == typeof(Bounds)) { ToLua.Push(L, (Bounds)obj); } else if (type == typeof(LayerMask)) { ToLua.PushLayerMask(L, (LayerMask)obj); } else { ToLua.PushValue(L, (ValueType)obj); } } else if (type.get_IsArray()) { ToLua.Push(L, (Array)obj); } else if (type == typeof(string)) { LuaDLL.lua_pushstring(L, (string)obj); } else if (type.IsSubclassOf(typeof(LuaBaseRef))) { ToLua.Push(L, (LuaBaseRef)obj); } else if (type.IsSubclassOf(typeof(Object))) { ToLua.Push(L, (Object)obj); } else if (type.IsSubclassOf(typeof(TrackedReference))) { ToLua.Push(L, (TrackedReference)obj); } else if (type == typeof(LuaByteBuffer)) { LuaByteBuffer luaByteBuffer = (LuaByteBuffer)obj; LuaDLL.lua_pushlstring(L, luaByteBuffer.buffer, luaByteBuffer.buffer.Length); } else if (type.IsSubclassOf(typeof(Delegate))) { ToLua.Push(L, (Delegate)obj); } else if (obj is IEnumerator) { ToLua.Push(L, (IEnumerator)obj); } else if (type == typeof(EventObject)) { ToLua.Push(L, (EventObject)obj); } else if (type == ToLua.monoType) { ToLua.Push(L, (Type)obj); } else { ToLua.PushObject(L, obj); } }