diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-06-29 13:58:17 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-06-29 13:58:17 -0300 |
| commit | 0f5a497ed903fea2065f76d2cc2512ebeacef1d2 (patch) | |
| tree | 98fe6afdba6f197cbfdbc0ab093581e9799a6f16 | |
| parent | 753625c3f377b013d00a8bd20f4492338e2d87b8 (diff) | |
| download | lua-0f5a497ed903fea2065f76d2cc2512ebeacef1d2.tar.gz lua-0f5a497ed903fea2065f76d2cc2512ebeacef1d2.tar.bz2 lua-0f5a497ed903fea2065f76d2cc2512ebeacef1d2.zip | |
new interface for search-path function + small changes in require
| -rw-r--r-- | lbaselib.c | 37 |
1 files changed, 22 insertions, 15 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lbaselib.c,v 1.148 2004/06/21 16:45:09 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.149 2004/06/21 20:05:29 roberto Exp roberto $ |
| 3 | ** Basic library | 3 | ** Basic library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -262,9 +262,13 @@ static int luaB_loadstring (lua_State *L) { | |||
| 262 | static int luaB_loadfile (lua_State *L) { | 262 | static int luaB_loadfile (lua_State *L) { |
| 263 | const char *fname = luaL_optstring(L, 1, NULL); | 263 | const char *fname = luaL_optstring(L, 1, NULL); |
| 264 | const char *path = luaL_optstring(L, 2, NULL); | 264 | const char *path = luaL_optstring(L, 2, NULL); |
| 265 | int status = (path == NULL) | 265 | int status; |
| 266 | ? luaL_loadfile(L, fname) | 266 | if (path == NULL) |
| 267 | : luaL_searchpath(L, fname, path, (luaL_Loader)luaL_loadfile, L); | 267 | status = luaL_loadfile(L, fname); |
| 268 | else { | ||
| 269 | fname = luaL_searchpath(L, fname, path); | ||
| 270 | status = (fname) ? luaL_loadfile(L, fname) : 1; | ||
| 271 | } | ||
| 268 | return load_aux(L, status); | 272 | return load_aux(L, status); |
| 269 | } | 273 | } |
| 270 | 274 | ||
| @@ -469,25 +473,28 @@ static const char *getpath (lua_State *L) { | |||
| 469 | static int luaB_require (lua_State *L) { | 473 | static int luaB_require (lua_State *L) { |
| 470 | const char *name = luaL_checkstring(L, 1); | 474 | const char *name = luaL_checkstring(L, 1); |
| 471 | const char *path = getpath(L); | 475 | const char *path = getpath(L); |
| 476 | const char *fname; | ||
| 477 | int loaded; | ||
| 472 | lua_getglobal(L, REQTAB); | 478 | lua_getglobal(L, REQTAB); |
| 473 | if (!lua_istable(L, -1)) | 479 | loaded = lua_gettop(L); |
| 480 | if (!lua_istable(L, loaded)) | ||
| 474 | return luaL_error(L, "global `" REQTAB "' is not a table"); | 481 | return luaL_error(L, "global `" REQTAB "' is not a table"); |
| 475 | lua_getfield(L, -1, name); | 482 | lua_getfield(L, loaded, name); |
| 476 | if (lua_toboolean(L, -1)) /* is it there? */ | 483 | if (lua_toboolean(L, -1)) /* is it there? */ |
| 477 | return 1; /* package is already loaded; return its result */ | 484 | return 1; /* package is already loaded; return its result */ |
| 478 | /* else must load it */ | 485 | /* else must load it; first mark it as loaded */ |
| 479 | if (luaL_searchpath(L, name, path, (luaL_Loader)luaL_loadfile, L) != 0) | 486 | lua_pushboolean(L, 1); |
| 487 | lua_setfield(L, loaded, name); /* _LOADED[name] = true */ | ||
| 488 | fname = luaL_searchpath(L, name, path); | ||
| 489 | if (fname == NULL || luaL_loadfile(L, fname) != 0) | ||
| 480 | return luaL_error(L, "error loading package `%s' (%s)", name, | 490 | return luaL_error(L, "error loading package `%s' (%s)", name, |
| 481 | lua_tostring(L, -1)); | 491 | lua_tostring(L, -1)); |
| 482 | lua_pushvalue(L, 1); /* pass name as argument to module */ | 492 | lua_pushvalue(L, 1); /* pass name as argument to module */ |
| 483 | lua_call(L, 1, 1); /* run loaded module */ | 493 | lua_call(L, 1, 1); /* run loaded module */ |
| 484 | if (lua_isnil(L, -1)) { /* nil return? */ | 494 | if (!lua_isnil(L, -1)) /* nil return? */ |
| 485 | lua_pop(L, 1); /* remove it */ | 495 | lua_setfield(L, loaded, name); |
| 486 | lua_pushboolean(L, 1); /* replace to true */ | 496 | lua_getfield(L, loaded, name); /* return _LOADED[name] */ |
| 487 | } | 497 | return 1; |
| 488 | lua_pushvalue(L, -1); /* duplicate result (to return it) */ | ||
| 489 | lua_setfield(L, -4, name); /* mark `name' as loaded */ | ||
| 490 | return 1; /* return value */ | ||
| 491 | } | 498 | } |
| 492 | 499 | ||
| 493 | /* }====================================================== */ | 500 | /* }====================================================== */ |
