/* * CAUTION: LuaInterface.Lua instances can't share the same lua state! */ public Lua(LuaCore.lua_State lState) { LuaLib.lua_pushstring(lState, "LUAINTERFACE LOADED"); LuaLib.lua_gettable(lState, (int)LuaIndexes.Registry); if (LuaLib.lua_toboolean(lState, -1)) { LuaLib.lua_settop(lState, -2); throw new LuaException("There is already a LuaInterface.Lua instance associated with this Lua state"); } else { LuaLib.lua_settop(lState, -2); LuaLib.lua_pushstring(lState, "LUAINTERFACE LOADED"); LuaLib.lua_pushboolean(lState, true); LuaLib.lua_settable(lState, (int)LuaIndexes.Registry); luaState = lState; LuaLib.lua_pushvalue(lState, (int)LuaIndexes.Globals); LuaLib.lua_getglobal(lState, "luanet"); LuaLib.lua_pushstring(lState, "getmetatable"); LuaLib.lua_getglobal(lState, "getmetatable"); LuaLib.lua_settable(lState, -3); LuaLib.lua_replace(lState, (int)LuaIndexes.Globals); translator = new ObjectTranslator(this, luaState); LuaLib.lua_replace(lState, (int)LuaIndexes.Globals); LuaLib.luaL_dostring(lState, Lua.init_luanet); // steffenj: lua_dostring renamed to luaL_dostring } _StatePassed = true; }
public Lua() { luaState = LuaLib.luaL_newstate(); // steffenj: Lua 5.1.1 API change (lua_open is gone) //LuaLib.luaopen_base(luaState); // steffenj: luaopen_* no longer used LuaLib.luaL_openlibs(luaState); // steffenj: Lua 5.1.1 API change (luaopen_base is gone, just open all libs right here) LuaLib.lua_pushstring(luaState, "LUAINTERFACE LOADED"); LuaLib.lua_pushboolean(luaState, true); LuaLib.lua_settable(luaState, (int)LuaIndexes.Registry); LuaLib.lua_newtable(luaState); LuaLib.lua_setglobal(luaState, "luanet"); LuaLib.lua_pushvalue(luaState, (int)LuaIndexes.Globals); LuaLib.lua_getglobal(luaState, "luanet"); LuaLib.lua_pushstring(luaState, "getmetatable"); LuaLib.lua_getglobal(luaState, "getmetatable"); LuaLib.lua_settable(luaState, -3); LuaLib.lua_replace(luaState, (int)LuaIndexes.Globals); translator = new ObjectTranslator(this, luaState); LuaLib.lua_replace(luaState, (int)LuaIndexes.Globals); LuaLib.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 LuaCore.lua_CFunction(PanicCallback); LuaLib.lua_atpanic(luaState, panicCallback); //LuaLib.lua_atlock(luaState, lockCallback = new LuaCore.lua_CFunction(LockCallback)); //LuaLib.lua_atunlock(luaState, unlockCallback = new LuaCore.lua_CFunction(UnlockCallback)); }
/* * Implementation of make_object. Registers a table (first * argument in the stack) as an object subclassing the * type passed as second argument in the stack. */ private int registerTable(LuaCore.lua_State luaState) { if (LuaLib.lua_type(luaState, 1) == LuaTypes.Table) { var luaTable = getTable(luaState, 1); string superclassName = LuaLib.lua_tostring(luaState, 2).ToString(); if (!superclassName.IsNull()) { var klass = FindType(superclassName); if (!klass.IsNull()) { // Creates and pushes the object in the stack, setting // it as the metatable of the first argument object obj = CodeGeneration.Instance.GetClassInstance(klass, luaTable); pushObject(luaState, obj, "luaNet_metatable"); LuaLib.lua_newtable(luaState); LuaLib.lua_pushstring(luaState, "__index"); LuaLib.lua_pushvalue(luaState, -3); LuaLib.lua_settable(luaState, -3); LuaLib.lua_pushstring(luaState, "__newindex"); LuaLib.lua_pushvalue(luaState, -3); LuaLib.lua_settable(luaState, -3); LuaLib.lua_setmetatable(luaState, 1); // Pushes the object again, this time as the base field // of the table and with the luaNet_searchbase metatable LuaLib.lua_pushstring(luaState, "base"); int index = addObject(obj); pushNewObject(luaState, obj, index, "luaNet_searchbase"); LuaLib.lua_rawset(luaState, 1); } else { throwError(luaState, "register_table: can not find superclass '" + superclassName + "'"); } } else { throwError(luaState, "register_table: superclass name can not be null"); } } else { throwError(luaState, "register_table: first arg is not a table"); } return(0); }
/* * Pushes a new object into the Lua stack with the provided * metatable */ private void pushNewObject(LuaCore.lua_State luaState, object o, int index, string metatable) { if (metatable == "luaNet_metatable") { // Gets or creates the metatable for the object's type LuaLib.luaL_getmetatable(luaState, o.GetType().AssemblyQualifiedName); if (LuaLib.lua_isnil(luaState, -1)) { LuaLib.lua_settop(luaState, -2); LuaLib.luaL_newmetatable(luaState, o.GetType().AssemblyQualifiedName); LuaLib.lua_pushstring(luaState, "cache"); LuaLib.lua_newtable(luaState); LuaLib.lua_rawset(luaState, -3); LuaLib.lua_pushlightuserdata(luaState, LuaLib.luanet_gettag()); LuaLib.lua_pushnumber(luaState, 1); LuaLib.lua_rawset(luaState, -3); LuaLib.lua_pushstring(luaState, "__index"); LuaLib.lua_pushstring(luaState, "luaNet_indexfunction"); LuaLib.lua_rawget(luaState, (int)LuaIndexes.Registry); LuaLib.lua_rawset(luaState, -3); LuaLib.lua_pushstring(luaState, "__gc"); LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.gcFunction); LuaLib.lua_rawset(luaState, -3); LuaLib.lua_pushstring(luaState, "__tostring"); LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.toStringFunction); LuaLib.lua_rawset(luaState, -3); LuaLib.lua_pushstring(luaState, "__newindex"); LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.newindexFunction); LuaLib.lua_rawset(luaState, -3); } } else { LuaLib.luaL_getmetatable(luaState, metatable); } // Stores the object index in the Lua list and pushes the // index into the Lua stack LuaLib.luaL_getmetatable(luaState, "luaNet_objects"); LuaLib.luanet_newudata(luaState, index); LuaLib.lua_pushvalue(luaState, -3); LuaLib.lua_remove(luaState, -4); LuaLib.lua_setmetatable(luaState, -2); LuaLib.lua_pushvalue(luaState, -1); LuaLib.lua_rawseti(luaState, -3, index); LuaLib.lua_remove(luaState, -2); }
/* * Gets the function in the index positon of the Lua stack. */ internal LuaFunction getFunction(LuaCore.lua_State luaState, int index) { LuaLib.lua_pushvalue(luaState, index); return(new LuaFunction(LuaLib.lua_ref(luaState, 1), interpreter)); }
/* * Gets the userdata in the index positon of the Lua stack. */ internal LuaUserData getUserData(LuaCore.lua_State luaState, int index) { LuaLib.lua_pushvalue(luaState, index); return(new LuaUserData(LuaLib.lua_ref(luaState, 1), interpreter)); }
/* * Gets the table in the index positon of the Lua stack. */ internal LuaTable getTable(LuaCore.lua_State luaState, int index) { LuaLib.lua_pushvalue(luaState, index); return(new LuaTable(LuaLib.lua_ref(luaState, 1), interpreter)); }