new protocol to open standard libraries

This commit is contained in:
Roberto Ierusalimschy 2005-04-13 14:24:20 -03:00
parent 2873d4efff
commit 04f657c7f8
5 changed files with 15 additions and 20 deletions

17
linit.c
View File

@ -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;
}

View File

@ -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);

View File

@ -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

4
lua.c
View File

@ -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);

View File

@ -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