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 LuaFunction RawGetFunc(string field) { IntPtr l = this._Interpreter.L; LuaFunction result = null; int newTop = LuaDLL.lua_gettop(l); LuaDLL.lua_getref(l, this._Reference); LuaDLL.lua_pushstring(l, field); LuaDLL.lua_gettable(l, -2); LuaTypes luaTypes = LuaDLL.lua_type(l, -1); if (luaTypes == LuaTypes.LUA_TFUNCTION) { result = new LuaFunction(LuaDLL.luaL_ref(l, LuaIndexes.LUA_REGISTRYINDEX), l); } LuaDLL.lua_settop(l, newTop); return(result); }
private static void PushMetaTable(IntPtr L, Type t) { int num = -1; if (!ObjectTranslator.typeMetaMap.TryGetValue(t, out num)) { LuaDLL.luaL_getmetatable(L, t.AssemblyQualifiedName); if (!LuaDLL.lua_isnil(L, -1)) { LuaDLL.lua_pushvalue(L, -1); num = LuaDLL.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); ObjectTranslator.typeMetaMap.Add(t, num); } } else { LuaDLL.lua_getref(L, num); } }
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); } }
public LuaFunction RawGetFunc(string field) { IntPtr L = _Interpreter.L; LuaTypes type = LuaTypes.LUA_TNONE; LuaFunction func = null; int oldTop = LuaDLL.lua_gettop(L); LuaDLL.lua_getref(L, _Reference); LuaDLL.lua_pushstring(L, field); LuaDLL.lua_gettable(L, -2); type = LuaDLL.lua_type(L, -1); if (type == LuaTypes.LUA_TFUNCTION) { func = new LuaFunction(LuaDLL.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX), L); } LuaDLL.lua_settop(L, oldTop); return(func); }
public LuaThread(LuaState parentState, LuaFunction threadFunc) { // Copy from parent this.tracebackFunction = parentState.tracebackFunction; this.translator = parentState.translator; this.translator.interpreter = this; this.panicCallback = parentState.panicCallback; this.printFunction = parentState.printFunction; this.loadfileFunction = parentState.loadfileFunction; this.loaderFunction = parentState.loaderFunction; this.dofileFunction = parentState.dofileFunction; // Assign to store func = threadFunc; parent = parentState; // Create Thread L = LuaDLL.lua_newthread(parent.L); // Store thread in registry threadRef = LuaDLL.luaL_ref(parent.L, LuaIndexes.LUA_REGISTRYINDEX); }
/* * 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); }
/* * Gets the table in the index positon of the Lua stack. */ internal LuaTable getTable(IntPtr luaState, int index) { LuaDLL.lua_pushvalue(luaState, index); return(new LuaTable(LuaDLL.luaL_ref(luaState, LuaIndexes.LUA_REGISTRYINDEX), interpreter)); }
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); }
/* compatibility with ref system */ public static int lua_ref(IntPtr luaState) { return(LuaDLL.luaL_ref(luaState, LuaIndexes.LUA_REGISTRYINDEX)); }
public int LuaRef(int t) { return(LuaDLL.luaL_ref(L, t)); }