static int BeginSample(IntPtr L) { try { int count = LuaDLL.lua_gettop(L); if (count == 1 && TypeChecker.CheckTypes <System.IntPtr>(L, 1)) { System.IntPtr arg0 = ToLua.CheckIntPtr(L, 1); MikuLuaProfiler.LuaProfiler.BeginSample(arg0); return(0); } else if (count == 1 && TypeChecker.CheckTypes <string>(L, 1)) { string arg0 = ToLua.ToString(L, 1); MikuLuaProfiler.LuaProfiler.BeginSample(arg0); return(0); } else if (count == 2) { System.IntPtr arg0 = ToLua.CheckIntPtr(L, 1); string arg1 = ToLua.CheckString(L, 2); MikuLuaProfiler.LuaProfiler.BeginSample(arg0, arg1); return(0); } else { return(LuaDLL.luaL_throw(L, "invalid arguments to method: MikuLuaProfiler.LuaProfiler.BeginSample")); } } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
static int EndSample(IntPtr L) { try { int count = LuaDLL.lua_gettop(L); if (count == 0) { MikuLuaProfiler.LuaProfiler.EndSample(); return(0); } else if (count == 1) { System.IntPtr arg0 = ToLua.CheckIntPtr(L, 1); MikuLuaProfiler.LuaProfiler.EndSample(arg0); return(0); } else { return(LuaDLL.luaL_throw(L, "invalid arguments to method: MikuLuaProfiler.LuaProfiler.EndSample")); } } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
public static void RunGC() { var env = LuaProfiler.mainL; if (env != IntPtr.Zero) { LuaDLL.lua_gc(env, LuaGCOptions.LUA_GCCOLLECT, 0); } }
public static void StopGC() { var env = LuaProfiler.mainL; if (env != IntPtr.Zero) { LuaDLL.lua_gc(env, LuaGCOptions.LUA_GCSTOP, 0); } }
public static void ResumeGC() { var env = LuaProfiler.mainL; if (env != IntPtr.Zero) { LuaDLL.lua_gc(env, LuaGCOptions.LUA_GCRESTART, 0); } }
public static long GetLuaMemory(IntPtr luaState) { long result = 0; result = LuaDLL.lua_gc(luaState, LuaGCOptions.LUA_GCCOUNT, 0); result = result * 1024 + LuaDLL.lua_gc(luaState, LuaGCOptions.LUA_GCCOUNTB, 0); return(result); }
public static void RefString(IntPtr strPoint, int index, string s, IntPtr L) { int oldTop = LuaDLL.lua_gettop(L); LuaDLL.lua_pushvalue(L, index); //把字符串ref了之后就不GC了 LuaDLL.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); LuaDLL.lua_settop(L, oldTop); stringDict[(long)strPoint] = s; }
public static string lua_tostring(IntPtr L, int index) { int len = 0; IntPtr str = LuaDLL.tolua_tolstring(L, index, out len); if (str != IntPtr.Zero) { string s; if (!TryGetLuaString(str, out s)) { s = LuaDLL.lua_ptrtostring(str, len); } return(s); } return(null); }
public static int registerTable(IntPtr luaState) { #if __NOGEN__ throwError(luaState,"Tables as Objects not implemnented"); #else ObjectTranslator translator = ObjectTranslator.FromState(luaState); if (LuaDLL.lua_type(luaState, 1) == LuaTypes.LUA_TTABLE) { LuaTable luaTable = translator.getTable(luaState, 1); string superclassName = LuaDLL.lua_tostring(luaState, 2); if (superclassName != null) { Type klass = translator.FindType(superclassName); if (klass != null) { // Creates and pushes the object in the stack, setting // it as the metatable of the first argument object obj = CodeGeneration.Instance.GetClassInstance(klass, luaTable); translator.pushObject(luaState, obj, "luaNet_metatable"); LuaDLL.lua_newtable(luaState); LuaDLL.lua_pushstring(luaState, "__index"); LuaDLL.lua_pushvalue(luaState, -3); LuaDLL.lua_settable(luaState, -3); LuaDLL.lua_pushstring(luaState, "__newindex"); LuaDLL.lua_pushvalue(luaState, -3); LuaDLL.lua_settable(luaState, -3); LuaDLL.lua_setmetatable(luaState, 1); // Pushes the object again, this time as the base field // of the table and with the luaNet_searchbase metatable LuaDLL.lua_pushstring(luaState, "base"); int index = translator.addObject(obj); translator.pushNewObject(luaState, obj, index, "luaNet_searchbase"); LuaDLL.lua_rawset(luaState, 1); } else translator.throwError(luaState, "register_table: can not find superclass '" + superclassName + "'"); } else translator.throwError(luaState, "register_table: superclass name can not be null"); } else translator.throwError(luaState, "register_table: first arg is not a table"); #endif return 0; }
public void Call <T>(string name, T arg1, bool beLogMiss) { int top = LuaDLL.lua_gettop(L); try { if (BeginCall(name, top, beLogMiss)) { PushGeneric(arg1); Call(1, top + 1, top); LuaDLL.lua_settop(L, top); } } catch (Exception e) { LuaDLL.lua_settop(L, top); throw e; } }
public bool BeginCall(string name, int top, bool beLogMiss) { LuaDLL.tolua_pushtraceback(L); if (PushLuaFunction(name, false)) { return(true); } else { LuaDLL.lua_settop(L, top); if (beLogMiss) { Debugger.Log("Lua function {0} not exists", name); } return(false); } }
/* * Creates the metatable for type references */ private void createClassMetatable(IntPtr luaState) { LuaDLL.luaL_newmetatable(luaState, "luaNet_class"); LuaDLL.lua_pushstring(luaState, "__gc"); LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.gcFunction); LuaDLL.lua_settable(luaState, -3); LuaDLL.lua_pushstring(luaState, "__tostring"); LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.toStringFunction); LuaDLL.lua_settable(luaState, -3); LuaDLL.lua_pushstring(luaState, "__index"); LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.classIndexFunction); LuaDLL.lua_settable(luaState, -3); LuaDLL.lua_pushstring(luaState, "__newindex"); LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.classNewindexFunction); LuaDLL.lua_settable(luaState, -3); LuaDLL.lua_pushstring(luaState, "__call"); LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.callConstructorFunction); LuaDLL.lua_settable(luaState, -3); LuaDLL.lua_settop(luaState, -2); }
static void PushMetaTable(IntPtr L, Type t) { int reference = -1; if (!typeMetaMap.TryGetValue(t, out reference)) { LuaDLL.luaL_getmetatable(L, t.AssemblyQualifiedName); if (!LuaDLL.lua_isnil(L, -1)) { LuaDLL.lua_pushvalue(L, -1); reference = LuaDLL.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); typeMetaMap.Add(t, reference); } } else { LuaDLL.lua_getref(L, reference); } }
/* * Gets the values from the provided index to * the top of the stack and returns them in an array. */ internal object[] popValues(IntPtr luaState, int oldTop) { int newTop = LuaDLL.lua_gettop(luaState); if (oldTop == newTop) { return null; } else { List<object> returnValues = new List<object>(); for (int i = oldTop + 1; i <= newTop; i++) { returnValues.Add(getObject(luaState, i)); } LuaDLL.lua_settop(luaState, oldTop); return returnValues.ToArray(); } }
public static int enumFromInt(IntPtr luaState) { ObjectTranslator translator = ObjectTranslator.FromState(luaState); Type t = translator.typeOf(luaState, 1); if (t == null || !t.IsEnum) { return translator.pushError(luaState, "not an enum"); } object res = null; LuaTypes lt = LuaDLL.lua_type(luaState, 2); if (lt == LuaTypes.LUA_TNUMBER) { int ival = (int)LuaDLL.lua_tonumber(luaState, 2); res = Enum.ToObject(t, ival); } else if (lt == LuaTypes.LUA_TSTRING) { string sflags = LuaDLL.lua_tostring(luaState, 2); string err = null; try { res = Enum.Parse(t, sflags); } catch (ArgumentException e) { err = e.Message; } if (err != null) { return translator.pushError(luaState, err); } } else { return translator.pushError(luaState, "second argument must be a integer or a string"); } translator.pushObject(luaState, res, "luaNet_metatable"); return 1; }
/* * Registers the global functions used by LuaInterface */ private void setGlobalFunctions(IntPtr luaState) { LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.indexFunction); LuaDLL.lua_setglobal(luaState, "get_object_member"); LuaDLL.lua_pushstdcallcfunction(luaState, importTypeFunction); LuaDLL.lua_setglobal(luaState, "import_type"); LuaDLL.lua_pushstdcallcfunction(luaState, loadAssemblyFunction); LuaDLL.lua_setglobal(luaState, "load_assembly"); LuaDLL.lua_pushstdcallcfunction(luaState, registerTableFunction); LuaDLL.lua_setglobal(luaState, "make_object"); LuaDLL.lua_pushstdcallcfunction(luaState, unregisterTableFunction); LuaDLL.lua_setglobal(luaState, "free_object"); LuaDLL.lua_pushstdcallcfunction(luaState, getMethodSigFunction); LuaDLL.lua_setglobal(luaState, "get_method_bysig"); LuaDLL.lua_pushstdcallcfunction(luaState, getConstructorSigFunction); LuaDLL.lua_setglobal(luaState, "get_constructor_bysig"); LuaDLL.lua_pushstdcallcfunction(luaState, ctypeFunction); LuaDLL.lua_setglobal(luaState, "ctype"); LuaDLL.lua_pushstdcallcfunction(luaState, enumFromIntFunction); LuaDLL.lua_setglobal(luaState, "enum"); }
public static int getMethodSignature(IntPtr luaState) { ObjectTranslator translator = ObjectTranslator.FromState(luaState); IReflect klass; object target; int udata = LuaDLL.luanet_checkudata(luaState, 1, "luaNet_class"); if (udata != -1) { klass = (IReflect)translator.objects[udata]; target = null; } else { target = translator.getRawNetObject(luaState, 1); if (target == null) { translator.throwError(luaState, "get_method_bysig: first arg is not type or object reference"); LuaDLL.lua_pushnil(luaState); return 1; } klass = target.GetType(); } string methodName = LuaDLL.lua_tostring(luaState, 2); Type[] signature = new Type[LuaDLL.lua_gettop(luaState) - 2]; for (int i = 0; i < signature.Length; i++) signature[i] = translator.FindType(LuaDLL.lua_tostring(luaState, i + 3)); try { //CP: Added ignore case MethodInfo method = klass.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.IgnoreCase, null, signature, null); translator.pushFunction(luaState, new LuaCSFunction((new LuaMethodWrapper(translator, target, klass, method)).call)); } catch (Exception e) { translator.throwError(luaState, e.Message); LuaDLL.lua_pushnil(luaState); } return 1; }
public void PushNewValueObject(IntPtr luaState, object o, int index) { LuaDLL.luanet_newudata(luaState, index); //string meta = GetAQName(o.GetType()); //LuaDLL.luaL_getmetatable(luaState, meta); Type t = o.GetType(); PushMetaTable(luaState, o.GetType()); if (LuaDLL.lua_isnil(luaState, -1)) { string meta = t.AssemblyQualifiedName; Debugger.LogError("Create not wrap ulua type:" + meta); LuaDLL.lua_settop(luaState, -2); LuaDLL.luaL_newmetatable(luaState, meta); LuaDLL.lua_pushstring(luaState, "cache"); LuaDLL.lua_newtable(luaState); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushlightuserdata(luaState, LuaDLL.luanet_gettag()); LuaDLL.lua_pushnumber(luaState, 1); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__index"); LuaDLL.lua_pushstring(luaState, "luaNet_indexfunction"); LuaDLL.lua_rawget(luaState, (int)LuaIndexes.LUA_REGISTRYINDEX); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__gc"); LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.gcFunction); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__tostring"); LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.toStringFunction); LuaDLL.lua_rawset(luaState, -3); LuaDLL.lua_pushstring(luaState, "__newindex"); LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.newindexFunction); LuaDLL.lua_rawset(luaState, -3); } LuaDLL.lua_setmetatable(luaState, -2); }
public static int loadAssembly(IntPtr luaState) { ObjectTranslator translator = ObjectTranslator.FromState(luaState); try { string assemblyName = LuaDLL.lua_tostring(luaState, 1); Assembly assembly = null; //assembly = Assembly.GetExecutingAssembly(); try { assembly = Assembly.Load(assemblyName); } catch (BadImageFormatException) { // The assemblyName was invalid. It is most likely a path. } if (assembly == null) { assembly = Assembly.Load(AssemblyName.GetAssemblyName(assemblyName)); } if (assembly != null && !translator.assemblies.Contains(assembly)) { translator.assemblies.Add(assembly); } } catch (Exception e) { translator.throwError(luaState, e.Message); } return 0; }
/* * Passes errors (argument e) to the Lua interpreter */ internal void throwError(IntPtr luaState, string message) { // We use this to remove anything pushed by luaL_where /*int oldTop = LuaDLL.lua_gettop(luaState); // Stack frame #1 is our C# wrapper, so not very interesting to the user // Stack frame #2 must be the lua code that called us, so that's what we want to use LuaDLL.luaL_where(luaState, 1); object[] curlev = popValues(luaState, oldTop); // Determine the position in the script where the exception was triggered string errLocation = ""; if (curlev.Length > 0) errLocation = curlev[0].ToString(); string message = e as string; if (message != null) { // Wrap Lua error (just a string) and store the error location e = new LuaScriptException(message, errLocation); } else { Exception ex = e as Exception; if (ex != null) { // Wrap generic .NET exception as an InnerException and store the error location e = new LuaScriptException(ex, errLocation); } } push(luaState, e); LuaDLL.lua_error(luaState);*/ LuaDLL.luaL_error(luaState, message); }
/* * Gets the values from the provided index to * the top of the stack and returns them in an array, casting * them to the provided types. */ internal object[] popValues(IntPtr luaState, int oldTop, Type[] popTypes) { int newTop = LuaDLL.lua_gettop(luaState); if (oldTop == newTop) { return null; } else { int iTypes; List<object> returnValues = new List<object>(); if (popTypes[0] == typeof(void)) iTypes = 1; else iTypes = 0; for (int i = oldTop + 1; i <= newTop; i++) { returnValues.Add(getAsType(luaState, i, popTypes[iTypes])); iTypes++; } LuaDLL.lua_settop(luaState, oldTop); return returnValues.ToArray(); } }
public static bool lua_isboolean(IntPtr luaState, int index) { return LuaDLL.lua_type(luaState,index)==LuaTypes.LUA_TBOOLEAN; }
public static void luaL_getmetatable(IntPtr luaState, string meta) { LuaDLL.lua_getfield(luaState, LuaIndexes.LUA_REGISTRYINDEX, meta); }
public static bool lua_isnil(IntPtr luaState, int index) { return (LuaDLL.lua_type(luaState,index)==LuaTypes.LUA_TNIL); }
/* * Gets the function in the index positon of the Lua stack. */ internal LuaFunction getFunction(IntPtr luaState, int index) { LuaDLL.lua_pushvalue(luaState, index); return new LuaFunction(LuaDLL.luaL_ref(luaState, LuaIndexes.LUA_REGISTRYINDEX), interpreter); }
// steffenj: BEGIN added lua_pop "macro" public static void lua_pop(IntPtr luaState, int amount) { LuaDLL.lua_settop(luaState, -(amount) - 1); }
public static void lua_setglobal(IntPtr luaState, string name) { LuaDLL.lua_pushstring(luaState,name); LuaDLL.lua_insert(luaState,-2); LuaDLL.lua_settable(luaState,LuaIndexes.LUA_GLOBALSINDEX); }
public int pushError(IntPtr luaState, string msg) { LuaDLL.lua_pushnil(luaState); LuaDLL.lua_pushstring(luaState, msg); return 2; }
public static void lua_unref(IntPtr luaState, int reference) { LuaDLL.luaL_unref(luaState,LuaIndexes.LUA_REGISTRYINDEX,reference); }
public static void lua_newtable(IntPtr luaState) { LuaDLL.lua_createtable(luaState, 0, 0); }