aboutsummaryrefslogtreecommitdiff
path: root/linit.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-02-15 11:17:39 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-02-15 11:17:39 -0300
commit165389b27bc54e7c5214276db177e3ef75226f18 (patch)
treef6ce6e7bff04ff6cbe735ee68d64290cc04c04bf /linit.c
parentc8121ce34b39c6fd31899f4da91e26063c8af54f (diff)
downloadlua-165389b27bc54e7c5214276db177e3ef75226f18.tar.gz
lua-165389b27bc54e7c5214276db177e3ef75226f18.tar.bz2
lua-165389b27bc54e7c5214276db177e3ef75226f18.zip
New interface to function 'luaL_openselectedlibs'
Instead of preloading all non-loaded libraries, there is another mask to select which libraries to preload.
Diffstat (limited to 'linit.c')
-rw-r--r--linit.c22
1 files changed, 10 insertions, 12 deletions
diff --git a/linit.c b/linit.c
index 675fb65f..140f6d75 100644
--- a/linit.c
+++ b/linit.c
@@ -21,12 +21,12 @@
21 21
22 22
23/* 23/*
24** Standard Libraries 24** Standard Libraries. (Must be listed in the same ORDER of their
25** respective constants LUA_<libname>K.)
25*/ 26*/
26static const luaL_Reg stdlibs[] = { 27static const luaL_Reg stdlibs[] = {
27 {LUA_GNAME, luaopen_base}, 28 {LUA_GNAME, luaopen_base},
28 {LUA_LOADLIBNAME, luaopen_package}, 29 {LUA_LOADLIBNAME, luaopen_package},
29
30 {LUA_COLIBNAME, luaopen_coroutine}, 30 {LUA_COLIBNAME, luaopen_coroutine},
31 {LUA_DBLIBNAME, luaopen_debug}, 31 {LUA_DBLIBNAME, luaopen_debug},
32 {LUA_IOLIBNAME, luaopen_io}, 32 {LUA_IOLIBNAME, luaopen_io},
@@ -35,30 +35,28 @@ static const luaL_Reg stdlibs[] = {
35 {LUA_STRLIBNAME, luaopen_string}, 35 {LUA_STRLIBNAME, luaopen_string},
36 {LUA_TABLIBNAME, luaopen_table}, 36 {LUA_TABLIBNAME, luaopen_table},
37 {LUA_UTF8LIBNAME, luaopen_utf8}, 37 {LUA_UTF8LIBNAME, luaopen_utf8},
38
39 {NULL, NULL} 38 {NULL, NULL}
40}; 39};
41 40
42 41
43/* 42/*
44** require selected standard libraries and add the others to the 43** require and preload selected standard libraries
45** preload table.
46*/ 44*/
47LUALIB_API void luaL_openselectedlibs (lua_State *L, int what) { 45LUALIB_API void luaL_openselectedlibs (lua_State *L, int load, int preload) {
48 int mask = 1; 46 int mask;
49 const luaL_Reg *lib; 47 const luaL_Reg *lib;
50 luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); 48 luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
51 for (lib = stdlibs; lib->func; (lib++, mask <<= 1)) { 49 for (lib = stdlibs, mask = 1; lib->name != NULL; lib++, mask <<= 1) {
52 if (what & mask) { /* selected? */ 50 if (load & mask) { /* selected? */
53 luaL_requiref(L, lib->name, lib->func, 1); /* require library */ 51 luaL_requiref(L, lib->name, lib->func, 1); /* require library */
54 lua_pop(L, 1); /* remove result from the stack */ 52 lua_pop(L, 1); /* remove result from the stack */
55 } 53 }
56 else { /* add library to PRELOAD table */ 54 else if (preload & mask) { /* selected? */
57 lua_pushcfunction(L, lib->func); 55 lua_pushcfunction(L, lib->func);
58 lua_setfield(L, -2, lib->name); 56 lua_setfield(L, -2, lib->name); /* add library to PRELOAD table */
59 } 57 }
60 } 58 }
61 lua_assert((mask >> 1) == LUA_UTF8LIBK); 59 lua_assert((mask >> 1) == LUA_UTF8LIBK);
62 lua_pop(L, 1); // remove PRELOAD table 60 lua_pop(L, 1); /* remove PRELOAD table */
63} 61}
64 62