private int GetBaseMethodInternal(LuaState luaState) { object obj = translator.GetRawNetObject(luaState, 1); if (obj == null) { translator.ThrowError(luaState, "trying to index an invalid object reference"); LuaLib.LuaPushNil(luaState); LuaLib.LuaPushBoolean(luaState, false); return(2); } string methodName = LuaLib.LuaToString(luaState, 2).ToString(); if (string.IsNullOrEmpty(methodName)) { LuaLib.LuaPushNil(luaState); LuaLib.LuaPushBoolean(luaState, false); return(2); } GetMember(luaState, obj.GetType(), obj, "__luaInterface_base_" + methodName, BindingFlags.Instance | BindingFlags.IgnoreCase); LuaLib.LuaSetTop(luaState, -2); if (LuaLib.LuaType(luaState, -1) == LuaTypes.Nil) { LuaLib.LuaSetTop(luaState, -2); return(GetMember(luaState, obj.GetType(), obj, methodName, BindingFlags.Instance | BindingFlags.IgnoreCase)); } LuaLib.LuaPushBoolean(luaState, false); return(2); }
internal int DoTraceback(LuaState state) { // if err is not a string, just pass it on (the stack is local to the function) // I spent a solid hour of debugging non stop only to fix the issue with this one line // (the issue was that nlua would throw exception objects and this thing would inappropriately // stringify them btw, if you're interested) if (!LuaLib.LuaIsString(state, -1)) { return(LuaLib.LuaGetTop(state)); } var error = LuaLib.LuaToString(state, -1); LuaLib.LuaPop(state, 1); LuaLib.LuaNewTable(state); // t LuaLib.LuaPushString(state, "__etgmod_error"); LuaLib.LuaPushBoolean(state, true); LuaLib.LuaSetTable(state, -3); LuaLib.LuaPushString(state, "msg"); // k LuaLib.LuaPushString(state, error); // v LuaLib.LuaSetTable(state, -3); // t[k] = v LuaLib.LuaPushString(state, "traceback"); // k LuaLib.LuaLTraceback(state, state, null, 0); // v LuaLib.LuaSetTable(state, -3); // t[k] = v return(1); // t is left }
/* * Pushes the object into the Lua stack according to its type. */ internal void Push(LuaState luaState, object o) { if (o == null) { LuaLib.LuaPushNil(luaState); } else if (o is sbyte || o is byte || o is short || o is ushort || o is int || o is uint || o is long || o is float || o is ulong || o is decimal || o is double) { double d = Convert.ToDouble(o); LuaLib.LuaPushNumber(luaState, d); } else if (o is char) { double d = (char)o; LuaLib.LuaPushNumber(luaState, d); } else if (o is string) { string str = (string)o; LuaLib.LuaPushString(luaState, str); } else if (o is bool) { bool b = (bool)o; LuaLib.LuaPushBoolean(luaState, b); } else if (IsILua(o)) { (((ILuaGeneratedType)o).LuaInterfaceGetLuaTable()).Push(luaState); } else if (o is LuaTable) { ((LuaTable)o).Push(luaState); } else if (o is Dictionary <object, object> dictionary) { PushDictionaryAsTable(luaState, dictionary); } else if (o is LuaNativeFunction) { PushFunction(luaState, (LuaNativeFunction)o); } else if (o is LuaFunction) { ((LuaFunction)o).Push(luaState); } else { PushObject(luaState, o, "luaNet_metatable"); } }
void Init() { LuaLib.LuaPushString(luaState, "LUAINTERFACE LOADED"); LuaLib.LuaPushBoolean(luaState, true); LuaLib.LuaSetTable(luaState, (int)LuaIndexes.Registry); if (_StatePassed == false) { LuaLib.LuaNewTable(luaState); LuaLib.LuaSetGlobal(luaState, "luanet"); } LuaLib.LuaNetPushGlobalTable(luaState); LuaLib.LuaGetGlobal(luaState, "luanet"); LuaLib.LuaPushString(luaState, "getmetatable"); LuaLib.LuaGetGlobal(luaState, "getmetatable"); LuaLib.LuaSetTable(luaState, -3); LuaLib.LuaNetPopGlobalTable(luaState); translator = new ObjectTranslator(this, luaState); ObjectTranslatorPool.Instance.Add(luaState, translator); LuaLib.LuaNetPopGlobalTable(luaState); }
private int GetMethodInternal(LuaState luaState) { object obj = translator.GetRawNetObject(luaState, 1); if (obj == null) { translator.ThrowError(luaState, "trying to index an invalid object reference"); LuaLib.LuaPushNil(luaState); return(1); } object index = translator.GetObject(luaState, 2); //var indexType = index.GetType(); string methodName = index as string; // will be null if not a string arg var 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 (!string.IsNullOrEmpty(methodName) && IsMemberPresent(objType, methodName)) { return(GetMember(luaState, objType, obj, methodName, BindingFlags.Instance | BindingFlags.IgnoreCase)); } } catch { } // 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); if (objType.UnderlyingSystemType == typeof(float[])) { float[] arr = ((float[])obj); translator.Push(luaState, arr [intIndex]); } else if (objType.UnderlyingSystemType == typeof(double[])) { double[] arr = ((double[])obj); translator.Push(luaState, arr [intIndex]); } else if (objType.UnderlyingSystemType == typeof(int[])) { int[] arr = ((int[])obj); translator.Push(luaState, arr [intIndex]); } else { object[] arr = (object[])obj; translator.Push(luaState, arr [intIndex]); } } else { // Try to use get_Item to index into this .net object var methods = objType.GetMethods(); foreach (var mInfo in methods) { if (mInfo.Name == "get_Item") { //check if the signature matches the input if (mInfo.GetParameters().Length == 1) { var getter = mInfo; var actualParms = (getter != null) ? getter.GetParameters() : null; if (actualParms == null || actualParms.Length != 1) { translator.ThrowError(luaState, "method not found (or no indexer): " + index); LuaLib.LuaPushNil(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); } LuaLib.LuaPushNil(luaState); } } } } } } LuaLib.LuaPushBoolean(luaState, false); return(2); }