void luax_register(lua_State *L, const char *name, const luaL_Reg *l) { if (name) lua_newtable(L); luax_setfuncs(L, l); if (name) { lua_pushvalue(L, -1); lua_setglobal(L, name); } }
int luax_register_module(lua_State *L, const WrappedModule &m) { m.type->init(); // Put a reference to the C++ module in Lua. luax_insistregistry(L, REGISTRY_MODULES); Proxy *p = (Proxy *)lua_newuserdata(L, sizeof(Proxy)); p->object = m.module; p->type = m.type; luaL_newmetatable(L, m.module->getName()); lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, w__gc); lua_setfield(L, -2, "__gc"); lua_setmetatable(L, -2); lua_setfield(L, -2, m.name); // _modules[name] = proxy lua_pop(L, 1); // Gets the love table. luax_insistglobal(L, "love"); // Create new table for module. lua_newtable(L); // Register all the functions. if (m.functions != nullptr) luax_setfuncs(L, m.functions); // Register types. if (m.types != nullptr) { for (const lua_CFunction *t = m.types; *t != nullptr; t++) (*t)(L); } lua_pushvalue(L, -1); lua_setfield(L, -3, m.name); // love.graphics = table lua_remove(L, -2); // love // Register module instance Module::registerInstance(m.module); return 1; }
int luax_register_type(lua_State *L, love::Type *type, ...) { type->init(); // Get the place for storing and re-using instantiated love types. luax_getregistry(L, REGISTRY_OBJECTS); // Create registry._loveobjects if it doesn't exist yet. if (!lua_istable(L, -1)) { lua_newtable(L); lua_replace(L, -2); // Create a metatable. lua_newtable(L); // metatable.__mode = "v". Weak userdata values. lua_pushliteral(L, "v"); lua_setfield(L, -2, "__mode"); // setmetatable(newtable, metatable) lua_setmetatable(L, -2); // registry._loveobjects = newtable lua_setfield(L, LUA_REGISTRYINDEX, "_loveobjects"); } else lua_pop(L, 1); luaL_newmetatable(L, type->getName()); // m.__index = m lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); // setup gc lua_pushcfunction(L, w__gc); lua_setfield(L, -2, "__gc"); // Add equality lua_pushcfunction(L, w__eq); lua_setfield(L, -2, "__eq"); // Add tostring function. lua_pushstring(L, type->getName()); lua_pushcclosure(L, w__tostring, 1); lua_setfield(L, -2, "__tostring"); // Add type lua_pushstring(L, type->getName()); lua_pushcclosure(L, w__type, 1); lua_setfield(L, -2, "type"); // Add typeOf lua_pushcfunction(L, w__typeOf); lua_setfield(L, -2, "typeOf"); // Add release lua_pushcfunction(L, w__release); lua_setfield(L, -2, "release"); va_list fs; va_start(fs, type); for (const luaL_Reg *f = va_arg(fs, const luaL_Reg *); f; f = va_arg(fs, const luaL_Reg *)) luax_setfuncs(L, f); va_end(fs); lua_pop(L, 1); // Pops metatable. return 0; }
int luax_register_type(lua_State *L, const char *tname, const luaL_Reg *f) { // Verify that this type name has a matching Type ID and type name mapping. love::Type ltype; if (!love::getType(tname, ltype)) printf("Missing type entry for type name: %s\n", tname); // Get the place for storing and re-using instantiated love types. luax_getregistry(L, REGISTRY_TYPES); // Create registry._lovetypes if it doesn't exist yet. if (!lua_istable(L, -1)) { lua_newtable(L); lua_replace(L, -2); // Create a metatable. lua_newtable(L); // metatable.__mode = "v". Weak userdata values. lua_pushliteral(L, "v"); lua_setfield(L, -2, "__mode"); // setmetatable(newtable, metatable) lua_setmetatable(L, -2); // registry._lovetypes = newtable lua_setfield(L, LUA_REGISTRYINDEX, "_lovetypes"); } else lua_pop(L, 1); luaL_newmetatable(L, tname); // m.__index = m lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); // setup gc lua_pushcfunction(L, w__gc); lua_setfield(L, -2, "__gc"); // Add equality lua_pushcfunction(L, w__eq); lua_setfield(L, -2, "__eq"); // Add tostring function. lua_pushstring(L, tname); lua_pushcclosure(L, w__tostring, 1); lua_setfield(L, -2, "__tostring"); // Add tostring to as type() as well. lua_pushstring(L, tname); lua_pushcclosure(L, w__tostring, 1); lua_setfield(L, -2, "type"); // Add typeOf lua_pushcfunction(L, w__typeOf); lua_setfield(L, -2, "typeOf"); if (f != 0) luax_setfuncs(L, f); lua_pop(L, 1); // Pops metatable. return 0; }