int luaopen_sdl(lua_State *L) { fps_ctrl* ctrl = (fps_ctrl*)lua_newuserdata(L, sizeof(fps_ctrl)); ctrl->init(); luaT_register(L, "sdl", sdllib); const luaL_Reg *pUpvaluedFunctions = sdllib_with_upvalue; for(; pUpvaluedFunctions->name; ++pUpvaluedFunctions) { lua_pushvalue(L, -2); luaT_pushcclosure(L, pUpvaluedFunctions->func, 1); lua_setfield(L, -2, pUpvaluedFunctions->name); } #define LOAD_EXTRA(name, fn) \ luaT_pushcfunction(L, fn); \ lua_call(L, 0, 1); \ lua_setfield(L, -2, name) LOAD_EXTRA("audio", luaopen_sdl_audio); LOAD_EXTRA("wm", luaopen_sdl_wm); #undef LOAD_EXTRA return 1; }
int luaopen_iso_fs(lua_State *L) { lua_settop(L, 1); if(!lua_tostring(L, 1)) { lua_pushliteral(L, "ISO_FS"); lua_replace(L, 1); } // Metatable lua_createtable(L, 0, 2); lua_pushvalue(L, -1); lua_replace(L, luaT_environindex); luaT_pushcclosure(L, luaT_stdgc<IsoFilesystem, luaT_environindex>, 0); lua_setfield(L, -2, "__gc"); // Methods table luaT_pushcclosuretable(L, l_isofs_new, 0); lua_pushvalue(L, -1); lua_setfield(L, -3, "__index"); lua_pushcfunction(L, l_isofs_set_path_separator); lua_setfield(L, -2, "setPathSeparator"); lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE); luaT_pushcclosure(L, l_isofs_set_root, 1); lua_setfield(L, -2, "setRoot"); lua_pushcfunction(L, l_isofs_read_contents); lua_setfield(L, -2, "readContents"); lua_pushcfunction(L, l_isofs_list_files); lua_setfield(L, -2, "listFiles"); lua_pushvalue(L, 1); lua_pushvalue(L, 2); #ifndef LUA_GLOBALSINDEX lua_pushglobaltable(L); lua_insert(L, -3); lua_settable(L, -3); lua_pop(L, 1); #else lua_settable(L, LUA_GLOBALSINDEX); #endif return 1; }
void THLuaRegisterStrings(const THLuaRegisterState_t *pState) { lua_State *L = pState->L; // Create Value, and Cache weak tables for inside-out objects. for(int i = 0; i <= 1; ++i) { lua_pushlightuserdata(L, &g_aStringDummyGlobals[i]); lua_newtable(L); lua_createtable(L, 0, 1); lua_pushliteral(L, "__mode"); lua_pushliteral(L, "k"); lua_rawset(L, -3); if(i == 1) { // Have the cache weak table automatically create caches on demand lua_pushliteral(L, "__index"); lua_createtable(L, 0, 1); lua_pushliteral(L, "__mode"); lua_pushliteral(L, "kv"); lua_rawset(L, -3); luaT_pushcclosure(L, l_mk_cache, 1); lua_rawset(L, -3); } lua_setmetatable(L, -2); lua_rawset(L, LUA_REGISTRYINDEX); } // Give the Value weak table a friendly name for Lua code to use lua_pushliteral(L, "StringProxyValues"); lua_pushlightuserdata(L, &g_aStringDummyGlobals[0]); lua_rawget(L, LUA_REGISTRYINDEX); lua_rawset(L, LUA_REGISTRYINDEX); luaT_class(THStringProxy_t, l_str_new, "stringProxy", MT_StringProxy); // As we overwrite __index, move methods to MT_StringProxy[4] lua_getfield(L, pState->aiMetatables[MT_StringProxy], "__index"); lua_rawseti(L, pState->aiMetatables[MT_StringProxy], 4); luaT_setmetamethod(l_str_index, "index"); luaT_setmetamethod(l_str_newindex, "newindex"); luaT_setmetamethod(l_str_concat, "concat"); luaT_setmetamethod(l_str_len, "len"); luaT_setmetamethod(l_str_tostring, "tostring"); luaT_setmetamethod(l_str_persist, "persist"); luaT_setmetamethod(l_str_depersist, "depersist"); luaT_setmetamethod(l_str_call, "call"); luaT_setmetamethod(l_str_lt, "lt"); luaT_setmetamethod(l_str_pairs, "pairs"); luaT_setmetamethod(l_str_ipairs, "ipairs"); luaT_setmetamethod(l_str_next, "next"); luaT_setmetamethod(l_str_inext, "inext"); luaT_setfunction(l_str_func, "format" , MT_DummyString, "format"); luaT_setfunction(l_str_func, "lower" , MT_DummyString, "lower"); luaT_setfunction(l_str_func, "rep" , MT_DummyString, "rep"); luaT_setfunction(l_str_func, "reverse", MT_DummyString, "reverse"); luaT_setfunction(l_str_func, "upper" , MT_DummyString, "upper"); luaT_setfunction(l_str_unwrap, "_unwrap"); luaT_setfunction(l_str_reload, "reload"); luaT_endclass(); }
//! Push a C closure as a callable table void luaT_pushcclosuretable(lua_State *L, lua_CFunction fn, int n) { luaT_pushcclosure(L, fn, n); // .. fn <top lua_createtable(L, 0, 1); // .. fn mt <top lua_pushliteral(L, "__call"); // .. fn mt __call <top lua_pushvalue(L, -3); // .. fn mt __call fn <top lua_settable(L, -3); // .. fn mt <top lua_newtable(L); // .. fn mt t <top lua_replace(L, -3); // .. t mt <top lua_setmetatable(L, -2); // .. t <top }
int luaopen_sdl(lua_State *L) { fps_ctrl* ctrl = (fps_ctrl*)lua_newuserdata(L, sizeof(fps_ctrl)); ctrl->init(); luaT_register(L, "sdl", sdllib); for (auto reg = sdllib_with_upvalue.begin(); reg->name; ++reg) { lua_pushvalue(L, -2); luaT_pushcclosure(L, reg->func, 1); lua_setfield(L, -2, reg->name); } load_extra(L, "audio", luaopen_sdl_audio); load_extra(L, "wm", luaopen_sdl_wm); return 1; }
int luaopen_sdl(lua_State *L) { fps_ctrl* ctrl = (fps_ctrl*)lua_newuserdata(L, sizeof(fps_ctrl)); ctrl->init(); luaT_register(L, "sdl", sdllib); const luaL_Reg *pUpvaluedFunctions = sdllib_with_upvalue; for(; pUpvaluedFunctions->name; ++pUpvaluedFunctions) { lua_pushvalue(L, -2); luaT_pushcclosure(L, pUpvaluedFunctions->func, 1); lua_setfield(L, -2, pUpvaluedFunctions->name); } load_extra(L, "audio", luaopen_sdl_audio); load_extra(L, "wm", luaopen_sdl_wm); return 1; }
int luaopen_sdl_audio(lua_State *L) { lua_newtable(L); luaT_setfuncs(L, sdl_audiolib); lua_pushboolean(L, 1); lua_setfield(L, -2, "loaded"); lua_createtable(L, 0, 2); lua_pushvalue(L, -1); lua_replace(L, luaT_environindex); lua_pushvalue(L, luaT_environindex); luaT_pushcclosure(L, luaT_stdgc<music_t, luaT_environindex>, 1); lua_setfield(L, -2, "__gc"); lua_pushvalue(L, 1); lua_setfield(L, -2, "__index"); lua_pop(L, 1); luaT_setfuncs(L, sdl_musiclib); return 1; }
void luaT_setclosure(const lua_register_state *pState, lua_CFunction fn, size_t iUps) { luaT_pushcclosure(pState->L, fn, iUps); }