diff options
| -rw-r--r-- | linit.c | 33 | ||||
| -rw-r--r-- | ltests.h | 5 |
2 files changed, 12 insertions, 26 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: linit.c,v 1.35 2014/11/02 19:19:04 roberto Exp roberto $ | 2 | ** $Id: linit.c,v 1.36 2014/12/06 20:42:58 roberto Exp roberto $ |
| 3 | ** Initialization of libraries for lua.c and other clients | 3 | ** Initialization of libraries for lua.c and other clients |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -16,6 +16,15 @@ | |||
| 16 | ** libraries, call luaL_openlibs in your program. If you need a | 16 | ** libraries, call luaL_openlibs in your program. If you need a |
| 17 | ** different set of libraries, copy this file to your project and edit | 17 | ** different set of libraries, copy this file to your project and edit |
| 18 | ** it to suit your needs. | 18 | ** it to suit your needs. |
| 19 | ** | ||
| 20 | ** You can also *preload* libraries, so that a later 'require' can | ||
| 21 | ** open the library, which is already linked to the application. | ||
| 22 | ** For that, do the following code: | ||
| 23 | ** | ||
| 24 | ** luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); | ||
| 25 | ** lua_pushcfunction(L, luaopen_modname); | ||
| 26 | ** lua_setfield(L, -2, modname); | ||
| 27 | ** lua_pop(L, 1); // remove _PRELOAD table | ||
| 19 | */ | 28 | */ |
| 20 | 29 | ||
| 21 | #include "lua.h" | 30 | #include "lua.h" |
| @@ -38,9 +47,7 @@ static const luaL_Reg loadedlibs[] = { | |||
| 38 | {LUA_STRLIBNAME, luaopen_string}, | 47 | {LUA_STRLIBNAME, luaopen_string}, |
| 39 | {LUA_MATHLIBNAME, luaopen_math}, | 48 | {LUA_MATHLIBNAME, luaopen_math}, |
| 40 | {LUA_UTF8LIBNAME, luaopen_utf8}, | 49 | {LUA_UTF8LIBNAME, luaopen_utf8}, |
| 41 | #if !defined(LUA_NODEBUGLIB) | ||
| 42 | {LUA_DBLIBNAME, luaopen_debug}, | 50 | {LUA_DBLIBNAME, luaopen_debug}, |
| 43 | #endif | ||
| 44 | #if defined(LUA_COMPAT_BITLIB) | 51 | #if defined(LUA_COMPAT_BITLIB) |
| 45 | {LUA_BITLIBNAME, luaopen_bit32}, | 52 | {LUA_BITLIBNAME, luaopen_bit32}, |
| 46 | #endif | 53 | #endif |
| @@ -48,30 +55,12 @@ static const luaL_Reg loadedlibs[] = { | |||
| 48 | }; | 55 | }; |
| 49 | 56 | ||
| 50 | 57 | ||
| 51 | /* | ||
| 52 | ** these libs are preloaded and must be required before used | ||
| 53 | */ | ||
| 54 | static const luaL_Reg preloadedlibs[] = { | ||
| 55 | #if defined(LUA_NODEBUGLIB) | ||
| 56 | {LUA_DBLIBNAME, luaopen_debug}, | ||
| 57 | #endif | ||
| 58 | {NULL, NULL} | ||
| 59 | }; | ||
| 60 | |||
| 61 | |||
| 62 | LUALIB_API void luaL_openlibs (lua_State *L) { | 58 | LUALIB_API void luaL_openlibs (lua_State *L) { |
| 63 | const luaL_Reg *lib; | 59 | const luaL_Reg *lib; |
| 64 | /* call open functions from 'loadedlibs' and set results to global table */ | 60 | /* "require" functions from 'loadedlibs' and set results to global table */ |
| 65 | for (lib = loadedlibs; lib->func; lib++) { | 61 | for (lib = loadedlibs; lib->func; lib++) { |
| 66 | luaL_requiref(L, lib->name, lib->func, 1); | 62 | luaL_requiref(L, lib->name, lib->func, 1); |
| 67 | lua_pop(L, 1); /* remove lib */ | 63 | lua_pop(L, 1); /* remove lib */ |
| 68 | } | 64 | } |
| 69 | /* add open functions from 'preloadedlibs' into 'package.preload' table */ | ||
| 70 | luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); | ||
| 71 | for (lib = preloadedlibs; lib->func; lib++) { | ||
| 72 | lua_pushcfunction(L, lib->func); | ||
| 73 | lua_setfield(L, -2, lib->name); | ||
| 74 | } | ||
| 75 | lua_pop(L, 1); /* remove _PRELOAD table */ | ||
| 76 | } | 65 | } |
| 77 | 66 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltests.h,v 2.42 2014/11/29 19:45:37 roberto Exp roberto $ | 2 | ** $Id: ltests.h,v 2.43 2014/12/06 20:42:58 roberto Exp roberto $ |
| 3 | ** Internal Header for Debugging of the Lua Implementation | 3 | ** Internal Header for Debugging of the Lua Implementation |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -24,9 +24,6 @@ | |||
| 24 | #undef LUA_COMPAT_MODULE | 24 | #undef LUA_COMPAT_MODULE |
| 25 | 25 | ||
| 26 | 26 | ||
| 27 | /* test without preloding debug library */ | ||
| 28 | #define LUA_NODEBUGLIB | ||
| 29 | |||
| 30 | #define LUA_DEBUG | 27 | #define LUA_DEBUG |
| 31 | 28 | ||
| 32 | 29 | ||
