From 165389b27bc54e7c5214276db177e3ef75226f18 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 15 Feb 2024 11:17:39 -0300 Subject: New interface to function 'luaL_openselectedlibs' Instead of preloading all non-loaded libraries, there is another mask to select which libraries to preload. --- linit.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'linit.c') diff --git a/linit.c b/linit.c index 675fb65f..140f6d75 100644 --- a/linit.c +++ b/linit.c @@ -21,12 +21,12 @@ /* -** Standard Libraries +** Standard Libraries. (Must be listed in the same ORDER of their +** respective constants LUA_K.) */ static const luaL_Reg stdlibs[] = { {LUA_GNAME, luaopen_base}, {LUA_LOADLIBNAME, luaopen_package}, - {LUA_COLIBNAME, luaopen_coroutine}, {LUA_DBLIBNAME, luaopen_debug}, {LUA_IOLIBNAME, luaopen_io}, @@ -35,30 +35,28 @@ static const luaL_Reg stdlibs[] = { {LUA_STRLIBNAME, luaopen_string}, {LUA_TABLIBNAME, luaopen_table}, {LUA_UTF8LIBNAME, luaopen_utf8}, - {NULL, NULL} }; /* -** require selected standard libraries and add the others to the -** preload table. +** require and preload selected standard libraries */ -LUALIB_API void luaL_openselectedlibs (lua_State *L, int what) { - int mask = 1; +LUALIB_API void luaL_openselectedlibs (lua_State *L, int load, int preload) { + int mask; const luaL_Reg *lib; luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); - for (lib = stdlibs; lib->func; (lib++, mask <<= 1)) { - if (what & mask) { /* selected? */ + for (lib = stdlibs, mask = 1; lib->name != NULL; lib++, mask <<= 1) { + if (load & mask) { /* selected? */ luaL_requiref(L, lib->name, lib->func, 1); /* require library */ lua_pop(L, 1); /* remove result from the stack */ } - else { /* add library to PRELOAD table */ + else if (preload & mask) { /* selected? */ lua_pushcfunction(L, lib->func); - lua_setfield(L, -2, lib->name); + lua_setfield(L, -2, lib->name); /* add library to PRELOAD table */ } } lua_assert((mask >> 1) == LUA_UTF8LIBK); - lua_pop(L, 1); // remove PRELOAD table + lua_pop(L, 1); /* remove PRELOAD table */ } -- cgit v1.2.3-55-g6feb