From d738c8d18bcc5651109b3a46103d6aa983772e68 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 7 Dec 2022 15:12:52 -0300 Subject: New function 'luaL_openselectedlibs' Makes it easier to start Lua with only some standard libraries. --- ltests.c | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) (limited to 'ltests.c') diff --git a/ltests.c b/ltests.c index 3f67c23a..d5d2ae68 100644 --- a/ltests.c +++ b/ltests.c @@ -1178,31 +1178,15 @@ static lua_State *getstate (lua_State *L) { static int loadlib (lua_State *L) { - static const luaL_Reg libs[] = { - {LUA_GNAME, luaopen_base}, - {"coroutine", luaopen_coroutine}, - {"debug", luaopen_debug}, - {"io", luaopen_io}, - {"os", luaopen_os}, - {"math", luaopen_math}, - {"string", luaopen_string}, - {"table", luaopen_table}, - {"T", luaB_opentests}, - {NULL, NULL} - }; lua_State *L1 = getstate(L); - int i; - luaL_requiref(L1, "package", luaopen_package, 0); + int what = luaL_checkinteger(L, 2); + luaL_openselectedlibs(L1, what); + luaL_requiref(L1, "T", luaB_opentests, 0); lua_assert(lua_type(L1, -1) == LUA_TTABLE); /* 'requiref' should not reload module already loaded... */ - luaL_requiref(L1, "package", NULL, 1); /* seg. fault if it reloads */ + luaL_requiref(L1, "T", NULL, 1); /* seg. fault if it reloads */ /* ...but should return the same module */ lua_assert(lua_compare(L1, -1, -2, LUA_OPEQ)); - luaL_getsubtable(L1, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); - for (i = 0; libs[i].name; i++) { - lua_pushcfunction(L1, libs[i].func); - lua_setfield(L1, -2, libs[i].name); - } return 0; } -- cgit v1.2.3-55-g6feb