From 7d309480dd783112aad8c4761921c4d45ec70327 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 30 Aug 2004 15:35:14 -0300 Subject: new way to handle _PATH (old LUA_PATH) --- lauxlib.c | 19 ++----------------- lbaselib.c | 35 +++++++++++++++++++++++++++-------- lua.c | 9 +++++++-- luaconf.h | 6 ++---- 4 files changed, 38 insertions(+), 31 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 83bae2e4..8cfe594b 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.121 2004/07/13 20:11:32 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.122 2004/08/13 19:52:53 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -341,17 +341,6 @@ LUALIB_API int luaL_getn (lua_State *L, int t) { /* }====================================================== */ -static const char *getpath (lua_State *L) { - const char *path; - lua_getglobal(L, LUA_PATH); /* try global variable */ - path = lua_tostring(L, -1); - if (path) return path; - path = getenv(LUA_PATH); /* else try environment variable */ - if (path) return path; - return LUA_PATH_DEFAULT; /* else use default */ -} - - static const char *pushnexttemplate (lua_State *L, const char *path) { const char *l; if (*path == '\0') return NULL; /* no more templates */ @@ -383,10 +372,7 @@ static const char *luaL_gsub (lua_State *L, const char *s, LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name, const char *path) { FILE *f; - const char *p; - if (path == NULL) path = getpath(L); - else lua_pushnil(L); /* to balance item pushed by `getpath' */ - p = path; + const char *p = path; for (;;) { const char *fname; if ((p = pushnexttemplate(L, p)) == NULL) { @@ -398,7 +384,6 @@ LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name, f = fopen(fname, "r"); /* try to read it */ if (f) { fclose(f); - lua_remove(L, -2); /* remove path */ return fname; } lua_pop(L, 1); /* remove file name */ diff --git a/lbaselib.c b/lbaselib.c index 7bed0bb5..522516c0 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.154 2004/07/09 18:23:17 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.155 2004/08/30 15:28:32 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -452,6 +452,19 @@ static int luaB_newproxy (lua_State *L) { */ +static const char *getpath (lua_State *L) { + /* try first `LUA_PATH' for compatibility */ + lua_getfield(L, LUA_GLOBALSINDEX, "LUA_PATH"); + if (!lua_isstring(L, -1)) { + lua_pop(L, 1); + lua_getfield(L, LUA_GLOBALSINDEX, "_PATH"); + } + if (!lua_isstring(L, -1)) + luaL_error(L, "global _PATH must be a string"); + return lua_tostring(L, -1); +} + + static int luaB_require (lua_State *L) { const char *name = luaL_checkstring(L, 1); const char *fname; @@ -461,7 +474,7 @@ static int luaB_require (lua_State *L) { /* else must load it; first mark it as loaded */ lua_pushboolean(L, 1); lua_setfield(L, lua_upvalueindex(1), name); /* _LOADED[name] = true */ - fname = luaL_searchpath(L, name, NULL); + fname = luaL_searchpath(L, name, getpath(L)); if (fname == NULL || luaL_loadfile(L, fname) != 0) return luaL_error(L, "error loading package `%s' (%s)", name, lua_tostring(L, -1)); @@ -622,10 +635,11 @@ static void auxopen (lua_State *L, const char *name, static void base_open (lua_State *L) { + const char *path; lua_pushvalue(L, LUA_GLOBALSINDEX); luaL_openlib(L, NULL, base_funcs, 0); /* open lib into global table */ lua_pushliteral(L, LUA_VERSION); - lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */ + lua_setfield(L, LUA_GLOBALSINDEX, "_VERSION"); /* set global _VERSION */ /* `ipairs' and `pairs' need auxiliary functions as upvalues */ auxopen(L, "ipairs", luaB_ipairs, ipairsaux); auxopen(L, "pairs", luaB_pairs, luaB_next); @@ -636,16 +650,21 @@ static void base_open (lua_State *L) { lua_pushliteral(L, "kv"); lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */ lua_pushcclosure(L, luaB_newproxy, 1); - lua_setfield(L, -2, "newproxy"); /* set global `newproxy' */ + lua_setfield(L, LUA_GLOBALSINDEX, "newproxy"); /* set global `newproxy' */ /* `require' needs a table to keep loaded chunks */ lua_newtable(L); lua_pushvalue(L, -1); - lua_setglobal(L, REQTAB); + lua_setglobal(L, "_LOADED"); lua_pushcclosure(L, luaB_require, 1); - lua_setfield(L, -2, "require"); + lua_setfield(L, LUA_GLOBALSINDEX, "require"); /* set global _G */ - lua_pushvalue(L, -1); - lua_setfield(L, -2, "_G"); + lua_pushvalue(L, LUA_GLOBALSINDEX); + lua_setfield(L, LUA_GLOBALSINDEX, "_G"); + /* set global _PATH */ + path = getenv(LUA_PATH); + if (path == NULL) path = LUA_PATH_DEFAULT; + lua_pushstring(L, path); + lua_setfield(L, LUA_GLOBALSINDEX, "_PATH"); } diff --git a/lua.c b/lua.c index a8c289e5..88ad392e 100644 --- a/lua.c +++ b/lua.c @@ -1,5 +1,5 @@ /* -** $Id: lua.c,v 1.130 2004/07/13 19:56:44 roberto Exp roberto $ +** $Id: lua.c,v 1.131 2004/08/26 14:19:55 roberto Exp roberto $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ @@ -127,7 +127,12 @@ static int dostring (lua_State *L, const char *s, const char *name) { static int dolibrary (lua_State *L, const char *name) { - name = luaL_searchpath(L, name, NULL); + lua_getfield(L, LUA_GLOBALSINDEX, "_PATH"); + if (!lua_isstring(L, -1)) { + l_message(progname, "global _PATH must be a string"); + return 1; + } + name = luaL_searchpath(L, name, lua_tostring(L, -1)); if (name == NULL) return report(L, 1); else return dofile(L, name); } diff --git a/luaconf.h b/luaconf.h index 5636beaf..a9f0cff7 100644 --- a/luaconf.h +++ b/luaconf.h @@ -1,5 +1,5 @@ /* -** $Id: luaconf.h,v 1.9 2004/07/09 14:29:29 roberto Exp roberto $ +** $Id: luaconf.h,v 1.10 2004/08/30 13:44:04 roberto Exp roberto $ ** Configuration file for Lua ** See Copyright Notice in lua.h */ @@ -271,10 +271,8 @@ /* `assert' options */ -/* name of global that holds table with loaded packages */ -#define REQTAB "_LOADED" -/* name of global that holds the search path for packages */ +/* environment variable that holds the search path for packages */ #define LUA_PATH "LUA_PATH" /* separator of templates in a path */ -- cgit v1.2.3-55-g6feb