public void InitPackagePath() { //把package压栈 LuaDLL.lua_getglobal(_L, "package"); //把package.path压栈 LuaDLL.lua_getfield(_L, -1, "path"); string current = LuaDLL.lua_tostring(_L, -1); string[] paths = current.Split(';'); for (int i = 0; i < paths.Length; i++) { if (!string.IsNullOrEmpty(paths[i])) { string path = paths[i].Replace('\\', '/'); LuaFileUtils.instance.AddSearchPath(path); } } //把''压栈 LuaDLL.lua_pushstring(_L, ""); //把package.path值设为'',同时把''出栈 LuaDLL.lua_setfield(_L, -3, "path"); //把package和package.path出栈 LuaDLL.lua_pop(_L, 2); }
public LuaState() { this.L = LuaDLL.luaL_newstate(); LuaDLL.luaL_openlibs(this.L); LuaDLL.lua_pushstring(this.L, "LUAINTERFACE LOADED"); LuaDLL.lua_pushboolean(this.L, true); LuaDLL.lua_settable(this.L, LuaIndexes.LUA_REGISTRYINDEX); LuaDLL.lua_newtable(this.L); LuaDLL.lua_setglobal(this.L, "luanet"); LuaDLL.lua_pushvalue(this.L, LuaIndexes.LUA_GLOBALSINDEX); LuaDLL.lua_getglobal(this.L, "luanet"); LuaDLL.lua_pushstring(this.L, "getmetatable"); LuaDLL.lua_getglobal(this.L, "getmetatable"); LuaDLL.lua_settable(this.L, -3); LuaDLL.lua_pushstring(this.L, "rawget"); LuaDLL.lua_getglobal(this.L, "rawget"); LuaDLL.lua_settable(this.L, -3); LuaDLL.lua_pushstring(this.L, "rawset"); LuaDLL.lua_getglobal(this.L, "rawset"); LuaDLL.lua_settable(this.L, -3); LuaDLL.lua_replace(this.L, LuaIndexes.LUA_GLOBALSINDEX); this.translator = new ObjectTranslator(this, this.L); LuaDLL.lua_replace(this.L, LuaIndexes.LUA_GLOBALSINDEX); this.translator.PushTranslator(this.L); this.panicCallback = new LuaCSFunction(LuaStatic.panic); LuaDLL.lua_atpanic(this.L, this.panicCallback); this.printFunction = new LuaCSFunction(LuaStatic.print); LuaDLL.lua_pushstdcallcfunction(this.L, this.printFunction, 0); LuaDLL.lua_setfield(this.L, LuaIndexes.LUA_GLOBALSINDEX, "print"); this.loadfileFunction = new LuaCSFunction(LuaStatic.loadfile); LuaDLL.lua_pushstdcallcfunction(this.L, this.loadfileFunction, 0); LuaDLL.lua_setfield(this.L, LuaIndexes.LUA_GLOBALSINDEX, "loadfile"); this.dofileFunction = new LuaCSFunction(LuaStatic.dofile); LuaDLL.lua_pushstdcallcfunction(this.L, this.dofileFunction, 0); LuaDLL.lua_setfield(this.L, LuaIndexes.LUA_GLOBALSINDEX, "dofile"); this.import_wrapFunction = new LuaCSFunction(LuaStatic.importWrap); LuaDLL.lua_pushstdcallcfunction(this.L, this.import_wrapFunction, 0); LuaDLL.lua_setfield(this.L, LuaIndexes.LUA_GLOBALSINDEX, "import"); this.loaderFunction = new LuaCSFunction(LuaStatic.loader); LuaDLL.lua_pushstdcallcfunction(this.L, this.loaderFunction, 0); int index = LuaDLL.lua_gettop(this.L); LuaDLL.lua_getfield(this.L, LuaIndexes.LUA_GLOBALSINDEX, "package"); LuaDLL.lua_getfield(this.L, -1, "loaders"); int num = LuaDLL.lua_gettop(this.L); for (int i = LuaDLL.luaL_getn(this.L, num) + 1; i > 1; i--) { LuaDLL.lua_rawgeti(this.L, num, i - 1); LuaDLL.lua_rawseti(this.L, num, i); } LuaDLL.lua_pushvalue(this.L, index); LuaDLL.lua_rawseti(this.L, num, 1); LuaDLL.lua_settop(this.L, 0); this.DoString(LuaStatic.init_luanet); this.tracebackFunction = new LuaCSFunction(LuaStatic.traceback); }
public static int traceback(IntPtr L) { LuaDLL.lua_getglobal(L, "debug"); LuaDLL.lua_getfield(L, -1, "traceback"); LuaDLL.lua_pushvalue(L, 1); LuaDLL.lua_pushnumber(L, 2); LuaDLL.lua_call(L, 2, 1); return(1); }
// 因为没有找到dup来复制顶部的element,所以只能临时创建一个table,把需要复制的element写入该table的field,然后再读两次 public void LuaDup() { LuaDLL.lua_newtable(L); LuaDLL.lua_insert(L, -2); LuaDLL.lua_setfield(L, -2, "xxx"); LuaDLL.lua_getfield(L, -1, "xxx"); LuaDLL.lua_getfield(L, -2, "xxx"); LuaDLL.lua_remove(L, -3); }
private static int GetUserDataFromCache(IntPtr L, int index) { int result = 1; LuaDLL.lua_getfield(L, LuaIndexes.LUA_REGISTRYINDEX, CACHE_CSHARP_OBJECT_TABLE); LuaDLL.lua_pushnumber(L, index); LuaDLL.lua_gettable(L, -2); return(result); }
private int traceback(IntPtr luaState) { LuaDLL.lua_getglobal(luaState, "debug"); LuaDLL.lua_getfield(luaState, -1, "traceback"); LuaDLL.lua_pushvalue(luaState, 1); LuaDLL.lua_pushnumber(luaState, 2); LuaDLL.lua_call(luaState, 2, 1); return(1); }
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); tracebackFunction = new LuaCSFunction(error_traceback); // We need to keep this in a managed reference so the delegate doesn't get garbage collected panicCallback = new LuaCSFunction(panic); LuaDLL.lua_atpanic(L, panicCallback); printFunction = new LuaCSFunction(print); LuaDLL.lua_pushstdcallcfunction(L, printFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "print"); loadfileFunction = new LuaCSFunction(loadfile); LuaDLL.lua_pushstdcallcfunction(L, loadfileFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "loadfile"); dofileFunction = new LuaCSFunction(dofile); LuaDLL.lua_pushstdcallcfunction(L, dofileFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "dofile"); LuaDLL.lua_pushstdcallcfunction(L, dofileFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "require"); LuaDLL.lua_newtable(L); LuaDLL.lua_newtable(L); LuaDLL.lua_pushstring(L, "v"); LuaDLL.lua_setfield(L, -2, "__mode"); LuaDLL.lua_setmetatable(L, -2); LuaDLL.lua_setfield(L, LuaIndexes.LUA_REGISTRYINDEX, LuaStatic.CACHE_CSHARP_OBJECT_TABLE); // Insert our loader FIRST loaderFunction = new LuaCSFunction(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); }
public static int traceback(IntPtr L, string err = "Lua traceback:") { LuaDLL.lua_getglobal(L, "debug"); LuaDLL.lua_getfield(L, -1, "traceback"); LuaDLL.lua_remove(L, -2); LuaDLL.lua_pushstring(L, err); LuaDLL.lua_pushnumber(L, 1.0); LuaDLL.lua_call(L, 2, 1); return(1); }
private static int CacheUserData(IntPtr L, int lo) { int result = 1; LuaDLL.lua_getfield(L, LuaIndexes.LUA_REGISTRYINDEX, CACHE_CSHARP_OBJECT_TABLE); LuaDLL.lua_pushnumber(L, objsRefId); LuaDLL.lua_pushvalue(L, lo); LuaDLL.lua_settable(L, -3); LuaDLL.lua_pop(L, 1); return(result); }
static void AddLuaLoader(IntPtr L) { LuaDLL.lua_getglobal(L, "package"); LuaDLL.lua_getfield(L, -1, "loaders"); int pos = LuaDLL.lua_objlen(L, -1) + 1; LuaDLL.lua_pushstdcallcfunction(L, Loader); LuaDLL.lua_rawseti(L, -2, pos); LuaDLL.lua_setfield(L, -2, "loaders"); LuaDLL.lua_pop(L, 1); }
public static Vector2 lua_tovector2(IntPtr L, int index) { LuaDLL.lua_getfield(L, index, "x"); float num = LuaDLL.lua_tofloat(L, -1); LuaDLL.lua_pop(L, 1); LuaDLL.lua_getfield(L, index, "y"); float num2 = LuaDLL.lua_tofloat(L, -1); LuaDLL.lua_pop(L, 1); return(new Vector2(num, num2)); }
private static void AddLuaLoader(IntPtr L) { LuaDLL.lua_getglobal(L, "package"); LuaDLL.lua_getfield(L, -1, "loaders"); LuaDLL.tolua_pushcfunction(L, new LuaCSFunction(LuaStatic.Loader)); for (int i = LuaDLL.lua_objlen(L, -2) + 1; i > 2; i--) { LuaDLL.lua_rawgeti(L, -2, i - 1); LuaDLL.lua_rawseti(L, -3, i); } LuaDLL.lua_rawseti(L, -2, 2); LuaDLL.lua_settop(L, 0); }
public static String GetTraceBackInfo(IntPtr L, string err = "Lua traceback:") { int oldTop = LuaDLL.lua_gettop(L); LuaDLL.lua_checkstack(L, 3); LuaDLL.lua_getglobal(L, "debug"); LuaDLL.lua_getfield(L, -1, "traceback"); LuaDLL.lua_pushstring(L, err); LuaDLL.lua_pushnumber(L, 1); LuaDLL.lua_call(L, 2, 1); string trace = LuaDLL.lua_tostring(L, -1); LuaDLL.lua_settop(L, oldTop); return(trace); }
public static IntPtr luaL_checkudata(IntPtr L, int ud, string tname) { IntPtr intPtr = LuaDLL.lua_touserdata(L, ud); if (intPtr != IntPtr.Zero && LuaDLL.lua_getmetatable(L, ud) != 0) { LuaDLL.lua_getfield(L, LuaIndexes.LUA_REGISTRYINDEX, tname); if (LuaDLL.lua_rawequal(L, -1, -2) != 0) { LuaDLL.lua_pop(L, 2); return(intPtr); } } LuaDLL.luaL_typerror(L, ud, tname, null); return(IntPtr.Zero); }
static void AddLuaLoader2(IntPtr L) { LuaDLL.lua_getglobal(L, "package"); LuaDLL.lua_getfield(L, -1, "loaders"); int loaderTable = LuaDLL.lua_gettop(L); for (int i = LuaDLL.lua_objlen(L, loaderTable) + 1; i > 1; i--) { LuaDLL.lua_rawgeti(L, loaderTable, i - 1); LuaDLL.lua_rawseti(L, loaderTable, i); } LuaDLL.lua_pushstdcallcfunction(L, Loader); LuaDLL.lua_rawseti(L, loaderTable, 1); LuaDLL.lua_settop(L, 0); }
public static int toluaL_exception(IntPtr L, Exception e, string msg = null) { msg = msg == null ? e.Message : msg; LuaException.luaStack = new LuaException(msg, e, 2); //log lua stack int top = LuaDLL.lua_gettop(L); LuaDLL.lua_getglobal(L, "debug"); LuaDLL.lua_getfield(L, -1, "traceback"); LuaDLL.lua_pcall(L, 0, 1, 0); string luastack = LuaDLL.lua_tostring(L, -1); LuaDLL.lua_settop(L, top); msg = string.Format("{0}\n{1}", msg, luastack); return(LuaDLL.tolua_error(L, msg)); }
//public static LuaTable lua_toluatable(IntPtr luaState, int index) { // LuaDLL.lua_pushvalue(luaState, index); // //return new LuaTable(LuaDLL.luaL_ref(luaState, LuaIndexes.LUA_REGISTRYINDEX), luaState); //} public static Rect lua_torect(IntPtr L, int index) { LuaDLL.lua_getfield(L, index, "x"); float num = LuaDLL.lua_tofloat(L, -1); LuaDLL.lua_pop(L, 1); LuaDLL.lua_getfield(L, index, "y"); float num2 = LuaDLL.lua_tofloat(L, -1); LuaDLL.lua_pop(L, 1); LuaDLL.lua_getfield(L, index, "width"); float num3 = LuaDLL.lua_tofloat(L, -1); LuaDLL.lua_pop(L, 1); LuaDLL.lua_getfield(L, index, "height"); float num4 = LuaDLL.lua_tofloat(L, -1); LuaDLL.lua_pop(L, 1); return(new Rect(num, num2, num3, num4)); }
public static Vector4 lua_tovector4(IntPtr L, int index) { LuaDLL.lua_getfield(L, index, "x"); float num = LuaDLL.lua_tofloat(L, -1); LuaDLL.lua_pop(L, 1); LuaDLL.lua_getfield(L, index, "y"); float num2 = LuaDLL.lua_tofloat(L, -1); LuaDLL.lua_pop(L, 1); LuaDLL.lua_getfield(L, index, "z"); float num3 = LuaDLL.lua_tofloat(L, -1); LuaDLL.lua_pop(L, 1); LuaDLL.lua_getfield(L, index, "w"); float num4 = LuaDLL.lua_tofloat(L, -1); LuaDLL.lua_pop(L, 1); return(new Vector4(num, num2, num3, num4)); }
public object[] CallTableFunction(string tableName, string functionName, object[] args, int returnNum) { if (!LuaDLL.lua_checkstack(L, args.Length + 6)) { LuaStatic.print("Lua stack overflow"); return(null); } int oldTop = LuaDLL.lua_gettop(L); LuaDLL.lua_getglobal(L, tableName); if (LuaDLL.lua_type(L, -1) != LuaTypes.LUA_TTABLE) { LuaDLL.lua_pop(L, 1); LuaStatic.print("Not a Lua table"); return(null); } LuaDLL.lua_getfield(L, -1, functionName); object[] ret = CallLuaFunction(args, returnNum, true); LuaDLL.lua_settop(L, oldTop); return(ret); }
public static int errorFunc_traceback(IntPtr L) { if (!LuaDLL.lua_isstring(L, 1)) /* 'message' not a string? */ { return(1); /* keep it intact */ } LuaDLL.lua_getfield(L, LuaIndexes.LUA_GLOBALSINDEX, "debug"); if (LuaDLL.lua_istable(L, -1)) { LuaDLL.lua_pop(L, 1); return(1); } LuaDLL.lua_getfield(L, -1, "traceback"); if (LuaDLL.lua_isfunction(L, -1)) { LuaDLL.lua_pop(L, 2); return(1); } LuaDLL.lua_pushvalue(L, 1); /* pass error message */ LuaDLL.lua_pushinteger(L, 2); /* skip this function and traceback */ LuaDLL.lua_call(L, 2, 1); /* call debug.traceback */ return(1); }
public static void luaL_getmetatable(IntPtr luaState, string meta) { LuaDLL.lua_getfield(luaState, LuaIndexes.LUA_REGISTRYINDEX, meta); }
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); //鍘嬪叆浜哶G琛? LuaDLL.lua_getglobal(L, "luanet"); LuaDLL.lua_pushstring(L, "getmetatable"); LuaDLL.lua_getglobal(L, "getmetatable"); LuaDLL.lua_settable(L, -3); LuaDLL.lua_pushstring(L, "rawget"); LuaDLL.lua_getglobal(L, "rawget"); LuaDLL.lua_settable(L, -3); LuaDLL.lua_pushstring(L, "rawset"); LuaDLL.lua_getglobal(L, "rawset"); LuaDLL.lua_settable(L, -3); // Set luanet as global for object translator LuaDLL.lua_replace(L, (int)LuaIndexes.LUA_GLOBALSINDEX); //鐢╨uanet鏇挎崲_G琛? translator = new ObjectTranslator(this, L); LuaDLL.lua_replace(L, (int)LuaIndexes.LUA_GLOBALSINDEX); //鎭㈠_G琛? translator.PushTranslator(L); // 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, "lprint"); warnFunction = new LuaCSFunction(LuaStatic.warn); LuaDLL.lua_pushstdcallcfunction(L, warnFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "lwarn"); breakFunction = new LuaCSFunction(LuaStatic.breakFunc); LuaDLL.lua_pushstdcallcfunction(L, breakFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "breakpoint"); 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"); pcallFunction = new LuaCSFunction(LuaStatic.pcall); LuaDLL.lua_pushstdcallcfunction(L, pcallFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "pcall"); LuaDLL.lua_pushstdcallcfunction(L, LuaStatic.errorFunc_traceback); errorFuncRef = LuaDLL.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); // 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); }
public void LuaGetField(int index, string key) { LuaDLL.lua_getfield(L, index, key); }
public static void lua_getglobal(IntPtr luaState, string name) { LuaDLL.lua_getfield(luaState, LuaIndexes.LUA_GLOBALSINDEX, name); }
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); 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); }