From 7a40c6ec643db4253cc3c9c1a25cb0ed98533e1d Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Fri, 25 May 2018 20:15:42 -0700 Subject: [PATCH] Share lua builtins open function across all lua-based languages. --- src/luaapi.c | 77 ++++++++++++++++------------------------------------ 1 file changed, 24 insertions(+), 53 deletions(-) diff --git a/src/luaapi.c b/src/luaapi.c index 547c858..e3fbbcb 100644 --- a/src/luaapi.c +++ b/src/luaapi.c @@ -1131,6 +1131,27 @@ static s32 lua_loadfile(lua_State *lua) return 0; } +static void lua_open_builtins(lua_State *lua) +{ + static const luaL_Reg loadedlibs[] = + { + { "_G", luaopen_base }, + { LUA_LOADLIBNAME, luaopen_package }, + { LUA_COLIBNAME, luaopen_coroutine }, + { LUA_TABLIBNAME, luaopen_table }, + { LUA_STRLIBNAME, luaopen_string }, + { LUA_MATHLIBNAME, luaopen_math }, + { LUA_DBLIBNAME, luaopen_debug }, + { NULL, NULL } + }; + + for (const luaL_Reg *lib = loadedlibs; lib->func; lib++) + { + luaL_requiref(lua, lib->name, lib->func, 1); + lua_pop(lua, 1); + } +} + static const char* const ApiKeywords[] = API_KEYWORDS; static const lua_CFunction ApiFunc[] = { @@ -1187,23 +1208,7 @@ static bool initLua(tic_mem* tic, const char* code) closeLua(tic); lua_State* lua = machine->lua = luaL_newstate(); - - static const luaL_Reg loadedlibs[] = - { - { "_G", luaopen_base }, - { LUA_COLIBNAME, luaopen_coroutine }, - { LUA_TABLIBNAME, luaopen_table }, - { LUA_STRLIBNAME, luaopen_string }, - { LUA_MATHLIBNAME, luaopen_math }, - { LUA_DBLIBNAME, luaopen_debug }, - { NULL, NULL } - }; - - for (const luaL_Reg *lib = loadedlibs; lib->func; lib++) - { - luaL_requiref(lua, lib->name, lib->func, 1); - lua_pop(lua, 1); - } + lua_open_builtins(lua); initAPI(machine); @@ -1462,24 +1467,7 @@ static bool initMoonscript(tic_mem* tic, const char* code) closeLua(tic); lua_State* lua = machine->lua = luaL_newstate(); - - static const luaL_Reg loadedlibs[] = - { - { "_G", luaopen_base }, - { LUA_LOADLIBNAME, luaopen_package }, - { LUA_COLIBNAME, luaopen_coroutine }, - { LUA_TABLIBNAME, luaopen_table }, - { LUA_STRLIBNAME, luaopen_string }, - { LUA_MATHLIBNAME, luaopen_math }, - { LUA_DBLIBNAME, luaopen_debug }, - { NULL, NULL } - }; - - for (const luaL_Reg *lib = loadedlibs; lib->func; lib++) - { - luaL_requiref(lua, lib->name, lib->func, 1); - lua_pop(lua, 1); - } + lua_open_builtins(lua); luaopen_lpeg(lua); setloaded(lua, "lpeg"); @@ -1630,24 +1618,7 @@ static bool initFennel(tic_mem* tic, const char* code) closeLua(tic); lua_State* lua = machine->lua = luaL_newstate(); - - static const luaL_Reg loadedlibs[] = - { - { "_G", luaopen_base }, - { LUA_LOADLIBNAME, luaopen_package }, - { LUA_COLIBNAME, luaopen_coroutine }, - { LUA_TABLIBNAME, luaopen_table }, - { LUA_STRLIBNAME, luaopen_string }, - { LUA_MATHLIBNAME, luaopen_math }, - { LUA_DBLIBNAME, luaopen_debug }, - { NULL, NULL } - }; - - for (const luaL_Reg *lib = loadedlibs; lib->func; lib++) - { - luaL_requiref(lua, lib->name, lib->func, 1); - lua_pop(lua, 1); - } + lua_open_builtins(lua); initAPI(machine);