From 04f657c7f892072d7cdc021e9e2635acc086f898 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 13 Apr 2005 14:24:20 -0300 Subject: [PATCH] new protocol to open standard libraries --- linit.c | 17 ++++++----------- loadlib.c | 4 ++-- ltests.h | 4 ++-- lua.c | 4 ++-- lualib.h | 6 +++--- 5 files changed, 15 insertions(+), 20 deletions(-) diff --git a/linit.c b/linit.c index 5d447c6d..e5cb343a 100644 --- a/linit.c +++ b/linit.c @@ -1,5 +1,5 @@ /* -** $Id: linit.c,v 1.9 2005/02/18 12:40:02 roberto Exp roberto $ +** $Id: linit.c,v 1.10 2005/03/08 13:37:55 roberto Exp roberto $ ** Initialization of libraries for lua.c ** See Copyright Notice in lua.h */ @@ -22,22 +22,17 @@ static const luaL_reg lualibs[] = { {LUA_STRLIBNAME, luaopen_string}, {LUA_MATHLIBNAME, luaopen_math}, {LUA_DBLIBNAME, luaopen_debug}, - {"", luaopen_loadlib}, + {LUA_LOADLIBNAME, luaopen_loadlib}, {NULL, NULL} }; -LUALIB_API int luaopen_stdlibs (lua_State *L) { +LUALIB_API void luaL_openlibs (lua_State *L) { const luaL_reg *lib = lualibs; - int t = lua_gettop(L); - lua_pushvalue(L, LUA_ENVIRONINDEX); /* save original environment */ for (; lib->func; lib++) { - lib->func(L); /* open library */ - lua_settop(L, t + 1); /* discard any results */ - lua_pushvalue(L, -1); - lua_replace(L, LUA_ENVIRONINDEX); /* restore environment */ + lua_pushcfunction(L, lib->func); + lua_pushstring(L, lib->name); + lua_call(L, 1, 0); } - lua_pop(L, 1); - return 0; } diff --git a/loadlib.c b/loadlib.c index bbe7789a..d1de0e69 100644 --- a/loadlib.c +++ b/loadlib.c @@ -1,5 +1,5 @@ /* -** $Id: loadlib.c,v 1.24 2005/03/29 16:20:48 roberto Exp roberto $ +** $Id: loadlib.c,v 1.25 2005/03/30 19:50:29 roberto Exp roberto $ ** Dynamic library loader for Lua ** See Copyright Notice in lua.h ** @@ -474,7 +474,7 @@ LUALIB_API int luaopen_loadlib (lua_State *L) { /* create `package' table */ lua_newtable(L); lua_pushvalue(L, -1); - lua_setglobal(L, "package"); + lua_setglobal(L, LUA_LOADLIBNAME); lua_pushvalue(L, -1); lua_setfield(L, LUA_REGISTRYINDEX, "_PACKAGE"); lua_pushvalue(L, -1); diff --git a/ltests.h b/ltests.h index ed762e36..7c145680 100644 --- a/ltests.h +++ b/ltests.h @@ -1,5 +1,5 @@ /* -** $Id: ltests.h,v 2.11 2005/01/10 16:31:30 roberto Exp roberto $ +** $Id: ltests.h,v 2.12 2005/03/18 18:55:45 roberto Exp roberto $ ** Internal Header for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -67,7 +67,7 @@ extern int islocked; int luaB_opentests (lua_State *L); #ifdef lua_c -#define luaopen_stdlibs(L) { (luaopen_stdlibs)(L); luaB_opentests(L); } +#define luaL_openlibs(L) { (luaL_openlibs)(L); luaB_opentests(L); } #endif diff --git a/lua.c b/lua.c index 7f3a4943..9e47308b 100644 --- a/lua.c +++ b/lua.c @@ -1,5 +1,5 @@ /* -** $Id: lua.c,v 1.140 2005/03/30 19:50:29 roberto Exp roberto $ +** $Id: lua.c,v 1.141 2005/04/11 18:01:35 roberto Exp roberto $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ @@ -343,7 +343,7 @@ static int pmain (lua_State *L) { int interactive = 1; if (s->argv[0] && s->argv[0][0]) progname = s->argv[0]; globalL = L; - luaopen_stdlibs(L); /* open libraries */ + luaL_openlibs(L); /* open libraries */ status = handle_luainit(L); if (status == 0) { status = handle_argv(L, s->argc, s->argv, &interactive); diff --git a/lualib.h b/lualib.h index 5c0bf255..1d8178d1 100644 --- a/lualib.h +++ b/lualib.h @@ -1,5 +1,5 @@ /* -** $Id: lualib.h,v 1.32 2004/07/09 15:47:48 roberto Exp roberto $ +** $Id: lualib.h,v 1.33 2005/01/10 16:31:30 roberto Exp roberto $ ** Lua standard libraries ** See Copyright Notice in lua.h */ @@ -36,12 +36,12 @@ LUALIB_API int (luaopen_math) (lua_State *L); #define LUA_DBLIBNAME "debug" LUALIB_API int (luaopen_debug) (lua_State *L); - +#define LUA_LOADLIBNAME "package" LUALIB_API int (luaopen_loadlib) (lua_State *L); /* open all previous libraries */ -LUALIB_API int (luaopen_stdlibs) (lua_State *L); +LUALIB_API void (luaL_openlibs) (lua_State *L); #endif