diff options
| author | Mike Pall <mike> | 2011-02-20 20:48:13 +0100 |
|---|---|---|
| committer | Mike Pall <mike> | 2011-02-20 20:48:13 +0100 |
| commit | baef199ece6b77a291043be21f88d175add61ecd (patch) | |
| tree | d72d33c1ea21a4e47ba2c9061d22c6de09563056 /src | |
| parent | f83d58d6fbb1b1d28414ae2ea97807a0f46b4637 (diff) | |
| download | luajit-baef199ece6b77a291043be21f88d175add61ecd.tar.gz luajit-baef199ece6b77a291043be21f88d175add61ecd.tar.bz2 luajit-baef199ece6b77a291043be21f88d175add61ecd.zip | |
From Lua 5.2: Add package.searchpath().
Thanks to F. Perrad.
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib_package.c | 39 |
1 files changed, 29 insertions, 10 deletions
diff --git a/src/lib_package.c b/src/lib_package.c index 45a80618..48d4b140 100644 --- a/src/lib_package.c +++ b/src/lib_package.c | |||
| @@ -3,7 +3,7 @@ | |||
| 3 | ** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h | 3 | ** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h |
| 4 | ** | 4 | ** |
| 5 | ** Major portions taken verbatim or adapted from the Lua interpreter. | 5 | ** Major portions taken verbatim or adapted from the Lua interpreter. |
| 6 | ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h | 6 | ** Copyright (C) 1994-2011 Lua.org, PUC-Rio. See Copyright Notice in lua.h |
| 7 | */ | 7 | */ |
| 8 | 8 | ||
| 9 | #define lib_package_c | 9 | #define lib_package_c |
| @@ -210,19 +210,14 @@ static const char *pushnexttemplate(lua_State *L, const char *path) | |||
| 210 | return l; | 210 | return l; |
| 211 | } | 211 | } |
| 212 | 212 | ||
| 213 | static const char *findfile(lua_State *L, const char *name, | 213 | static const char *searchpath (lua_State *L, const char *name, |
| 214 | const char *pname) | 214 | const char *path) |
| 215 | { | 215 | { |
| 216 | const char *path; | ||
| 217 | name = luaL_gsub(L, name, ".", LUA_DIRSEP); | 216 | name = luaL_gsub(L, name, ".", LUA_DIRSEP); |
| 218 | lua_getfield(L, LUA_ENVIRONINDEX, pname); | ||
| 219 | path = lua_tostring(L, -1); | ||
| 220 | if (path == NULL) | ||
| 221 | luaL_error(L, LUA_QL("package.%s") " must be a string", pname); | ||
| 222 | lua_pushliteral(L, ""); /* error accumulator */ | 217 | lua_pushliteral(L, ""); /* error accumulator */ |
| 223 | while ((path = pushnexttemplate(L, path)) != NULL) { | 218 | while ((path = pushnexttemplate(L, path)) != NULL) { |
| 224 | const char *filename; | 219 | const char *filename = luaL_gsub(L, lua_tostring(L, -1), |
| 225 | filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); | 220 | LUA_PATH_MARK, name); |
| 226 | lua_remove(L, -2); /* remove path template */ | 221 | lua_remove(L, -2); /* remove path template */ |
| 227 | if (readable(filename)) /* does file exist and is readable? */ | 222 | if (readable(filename)) /* does file exist and is readable? */ |
| 228 | return filename; /* return that file name */ | 223 | return filename; /* return that file name */ |
| @@ -233,6 +228,29 @@ static const char *findfile(lua_State *L, const char *name, | |||
| 233 | return NULL; /* not found */ | 228 | return NULL; /* not found */ |
| 234 | } | 229 | } |
| 235 | 230 | ||
| 231 | static int lj_cf_package_searchpath(lua_State *L) | ||
| 232 | { | ||
| 233 | const char *f = searchpath(L, luaL_checkstring(L, 1), luaL_checkstring(L, 2)); | ||
| 234 | if (f != NULL) { | ||
| 235 | return 1; | ||
| 236 | } else { /* error message is on top of the stack */ | ||
| 237 | lua_pushnil(L); | ||
| 238 | lua_insert(L, -2); | ||
| 239 | return 2; /* return nil + error message */ | ||
| 240 | } | ||
| 241 | } | ||
| 242 | |||
| 243 | static const char *findfile(lua_State *L, const char *name, | ||
| 244 | const char *pname) | ||
| 245 | { | ||
| 246 | const char *path; | ||
| 247 | lua_getfield(L, LUA_ENVIRONINDEX, pname); | ||
| 248 | path = lua_tostring(L, -1); | ||
| 249 | if (path == NULL) | ||
| 250 | luaL_error(L, LUA_QL("package.%s") " must be a string", pname); | ||
| 251 | return searchpath(L, name, path); | ||
| 252 | } | ||
| 253 | |||
| 236 | static void loaderror(lua_State *L, const char *filename) | 254 | static void loaderror(lua_State *L, const char *filename) |
| 237 | { | 255 | { |
| 238 | luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s", | 256 | luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s", |
| @@ -459,6 +477,7 @@ static void setpath(lua_State *L, const char *fieldname, const char *envname, | |||
| 459 | 477 | ||
| 460 | static const luaL_Reg package_lib[] = { | 478 | static const luaL_Reg package_lib[] = { |
| 461 | { "loadlib", lj_cf_package_loadlib }, | 479 | { "loadlib", lj_cf_package_loadlib }, |
| 480 | { "searchpath", lj_cf_package_searchpath }, | ||
| 462 | { "seeall", lj_cf_package_seeall }, | 481 | { "seeall", lj_cf_package_seeall }, |
| 463 | { NULL, NULL } | 482 | { NULL, NULL } |
| 464 | }; | 483 | }; |
