aboutsummaryrefslogtreecommitdiff
path: root/linit.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2010-07-02 08:38:13 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2010-07-02 08:38:13 -0300
commit7192afafeeb1a96b3de60af90a72cd8762b09d94 (patch)
treed1e7e061822f755c33cc497d98ea451c7e7e32e8 /linit.c
parenta139e2e003e0b62b7d34eeda20dd2354e74885f9 (diff)
downloadlua-7192afafeeb1a96b3de60af90a72cd8762b09d94.tar.gz
lua-7192afafeeb1a96b3de60af90a72cd8762b09d94.tar.bz2
lua-7192afafeeb1a96b3de60af90a72cd8762b09d94.zip
new module policy: C modules do not create globals and do not register
themselves with 'require' (let 'require' do its work); new auxiliary functions luaL_newlib/luaL_newlibtable/luaL_setfuncs/luaL_requiref. Old luaL_register will be deprecated.
Diffstat (limited to 'linit.c')
-rw-r--r--linit.c21
1 files changed, 8 insertions, 13 deletions
diff --git a/linit.c b/linit.c
index 6c803270..2779e9b1 100644
--- a/linit.c
+++ b/linit.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: linit.c,v 1.26 2010/06/10 21:29:47 roberto Exp roberto $ 2** $Id: linit.c,v 1.27 2010/06/30 17:40:27 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*/
@@ -36,6 +36,9 @@ static const luaL_Reg loadedlibs[] = {
36 {LUA_STRLIBNAME, luaopen_string}, 36 {LUA_STRLIBNAME, luaopen_string},
37 {LUA_BITLIBNAME, luaopen_bit}, 37 {LUA_BITLIBNAME, luaopen_bit},
38 {LUA_MATHLIBNAME, luaopen_math}, 38 {LUA_MATHLIBNAME, luaopen_math},
39#if defined(LUA_COMPAT_DEBUGLIB)
40 {LUA_DBLIBNAME, luaopen_debug},
41#endif
39 {NULL, NULL} 42 {NULL, NULL}
40}; 43};
41 44
@@ -51,25 +54,17 @@ static const luaL_Reg preloadedlibs[] = {
51 54
52LUALIB_API void luaL_openlibs (lua_State *L) { 55LUALIB_API void luaL_openlibs (lua_State *L) {
53 const luaL_Reg *lib; 56 const luaL_Reg *lib;
54 /* call open functions from 'loadedlibs' */ 57 /* call open functions from 'loadedlibs' and set results to global table */
55 for (lib = loadedlibs; lib->func; lib++) { 58 for (lib = loadedlibs; lib->func; lib++) {
56 lua_pushcfunction(L, lib->func); 59 luaL_requiref(L, lib->name, lib->func, 1);
57 lua_pushstring(L, lib->name); 60 lua_pop(L, 1); /* remove lib */
58 lua_call(L, 1, 0);
59 } 61 }
60 /* add open functions from 'preloadedlibs' into 'package.preload' table */ 62 /* add open functions from 'preloadedlibs' into 'package.preload' table */
61 lua_pushglobaltable(L);
62 luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); 63 luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
63 for (lib = preloadedlibs; lib->func; lib++) { 64 for (lib = preloadedlibs; lib->func; lib++) {
64 lua_pushcfunction(L, lib->func); 65 lua_pushcfunction(L, lib->func);
65 lua_setfield(L, -2, lib->name); 66 lua_setfield(L, -2, lib->name);
66 } 67 }
67 lua_pop(L, 1); /* remove package.preload table */ 68 lua_pop(L, 1); /* remove _PRELOAD table */
68#if defined(LUA_COMPAT_DEBUGLIB)
69 lua_getglobal(L, "require");
70 lua_pushliteral(L, LUA_DBLIBNAME);
71 lua_call(L, 1, 0); /* call 'require"debug"' */
72 lua_pop(L, 1); /* remove global table */
73#endif
74} 69}
75 70