diff options
| -rw-r--r-- | lauxlib.c | 19 | ||||
| -rw-r--r-- | lbaselib.c | 35 | ||||
| -rw-r--r-- | lua.c | 9 | ||||
| -rw-r--r-- | luaconf.h | 6 |
4 files changed, 38 insertions, 31 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lauxlib.c,v 1.121 2004/07/13 20:11:32 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.122 2004/08/13 19:52:53 roberto Exp roberto $ |
| 3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -341,17 +341,6 @@ LUALIB_API int luaL_getn (lua_State *L, int t) { | |||
| 341 | /* }====================================================== */ | 341 | /* }====================================================== */ |
| 342 | 342 | ||
| 343 | 343 | ||
| 344 | static const char *getpath (lua_State *L) { | ||
| 345 | const char *path; | ||
| 346 | lua_getglobal(L, LUA_PATH); /* try global variable */ | ||
| 347 | path = lua_tostring(L, -1); | ||
| 348 | if (path) return path; | ||
| 349 | path = getenv(LUA_PATH); /* else try environment variable */ | ||
| 350 | if (path) return path; | ||
| 351 | return LUA_PATH_DEFAULT; /* else use default */ | ||
| 352 | } | ||
| 353 | |||
| 354 | |||
| 355 | static const char *pushnexttemplate (lua_State *L, const char *path) { | 344 | static const char *pushnexttemplate (lua_State *L, const char *path) { |
| 356 | const char *l; | 345 | const char *l; |
| 357 | if (*path == '\0') return NULL; /* no more templates */ | 346 | if (*path == '\0') return NULL; /* no more templates */ |
| @@ -383,10 +372,7 @@ static const char *luaL_gsub (lua_State *L, const char *s, | |||
| 383 | LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name, | 372 | LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name, |
| 384 | const char *path) { | 373 | const char *path) { |
| 385 | FILE *f; | 374 | FILE *f; |
| 386 | const char *p; | 375 | const char *p = path; |
| 387 | if (path == NULL) path = getpath(L); | ||
| 388 | else lua_pushnil(L); /* to balance item pushed by `getpath' */ | ||
| 389 | p = path; | ||
| 390 | for (;;) { | 376 | for (;;) { |
| 391 | const char *fname; | 377 | const char *fname; |
| 392 | if ((p = pushnexttemplate(L, p)) == NULL) { | 378 | if ((p = pushnexttemplate(L, p)) == NULL) { |
| @@ -398,7 +384,6 @@ LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name, | |||
| 398 | f = fopen(fname, "r"); /* try to read it */ | 384 | f = fopen(fname, "r"); /* try to read it */ |
| 399 | if (f) { | 385 | if (f) { |
| 400 | fclose(f); | 386 | fclose(f); |
| 401 | lua_remove(L, -2); /* remove path */ | ||
| 402 | return fname; | 387 | return fname; |
| 403 | } | 388 | } |
| 404 | lua_pop(L, 1); /* remove file name */ | 389 | lua_pop(L, 1); /* remove file name */ |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lbaselib.c,v 1.154 2004/07/09 18:23:17 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.155 2004/08/30 15:28:32 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 | */ |
| @@ -452,6 +452,19 @@ static int luaB_newproxy (lua_State *L) { | |||
| 452 | */ | 452 | */ |
| 453 | 453 | ||
| 454 | 454 | ||
| 455 | static const char *getpath (lua_State *L) { | ||
| 456 | /* try first `LUA_PATH' for compatibility */ | ||
| 457 | lua_getfield(L, LUA_GLOBALSINDEX, "LUA_PATH"); | ||
| 458 | if (!lua_isstring(L, -1)) { | ||
| 459 | lua_pop(L, 1); | ||
| 460 | lua_getfield(L, LUA_GLOBALSINDEX, "_PATH"); | ||
| 461 | } | ||
| 462 | if (!lua_isstring(L, -1)) | ||
| 463 | luaL_error(L, "global _PATH must be a string"); | ||
| 464 | return lua_tostring(L, -1); | ||
| 465 | } | ||
| 466 | |||
| 467 | |||
| 455 | static int luaB_require (lua_State *L) { | 468 | static int luaB_require (lua_State *L) { |
| 456 | const char *name = luaL_checkstring(L, 1); | 469 | const char *name = luaL_checkstring(L, 1); |
| 457 | const char *fname; | 470 | const char *fname; |
| @@ -461,7 +474,7 @@ static int luaB_require (lua_State *L) { | |||
| 461 | /* else must load it; first mark it as loaded */ | 474 | /* else must load it; first mark it as loaded */ |
| 462 | lua_pushboolean(L, 1); | 475 | lua_pushboolean(L, 1); |
| 463 | lua_setfield(L, lua_upvalueindex(1), name); /* _LOADED[name] = true */ | 476 | lua_setfield(L, lua_upvalueindex(1), name); /* _LOADED[name] = true */ |
| 464 | fname = luaL_searchpath(L, name, NULL); | 477 | fname = luaL_searchpath(L, name, getpath(L)); |
| 465 | if (fname == NULL || luaL_loadfile(L, fname) != 0) | 478 | if (fname == NULL || luaL_loadfile(L, fname) != 0) |
| 466 | return luaL_error(L, "error loading package `%s' (%s)", name, | 479 | return luaL_error(L, "error loading package `%s' (%s)", name, |
| 467 | lua_tostring(L, -1)); | 480 | lua_tostring(L, -1)); |
| @@ -622,10 +635,11 @@ static void auxopen (lua_State *L, const char *name, | |||
| 622 | 635 | ||
| 623 | 636 | ||
| 624 | static void base_open (lua_State *L) { | 637 | static void base_open (lua_State *L) { |
| 638 | const char *path; | ||
| 625 | lua_pushvalue(L, LUA_GLOBALSINDEX); | 639 | lua_pushvalue(L, LUA_GLOBALSINDEX); |
| 626 | luaL_openlib(L, NULL, base_funcs, 0); /* open lib into global table */ | 640 | luaL_openlib(L, NULL, base_funcs, 0); /* open lib into global table */ |
| 627 | lua_pushliteral(L, LUA_VERSION); | 641 | lua_pushliteral(L, LUA_VERSION); |
| 628 | lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */ | 642 | lua_setfield(L, LUA_GLOBALSINDEX, "_VERSION"); /* set global _VERSION */ |
| 629 | /* `ipairs' and `pairs' need auxiliary functions as upvalues */ | 643 | /* `ipairs' and `pairs' need auxiliary functions as upvalues */ |
| 630 | auxopen(L, "ipairs", luaB_ipairs, ipairsaux); | 644 | auxopen(L, "ipairs", luaB_ipairs, ipairsaux); |
| 631 | auxopen(L, "pairs", luaB_pairs, luaB_next); | 645 | auxopen(L, "pairs", luaB_pairs, luaB_next); |
| @@ -636,16 +650,21 @@ static void base_open (lua_State *L) { | |||
| 636 | lua_pushliteral(L, "kv"); | 650 | lua_pushliteral(L, "kv"); |
| 637 | lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */ | 651 | lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */ |
| 638 | lua_pushcclosure(L, luaB_newproxy, 1); | 652 | lua_pushcclosure(L, luaB_newproxy, 1); |
| 639 | lua_setfield(L, -2, "newproxy"); /* set global `newproxy' */ | 653 | lua_setfield(L, LUA_GLOBALSINDEX, "newproxy"); /* set global `newproxy' */ |
| 640 | /* `require' needs a table to keep loaded chunks */ | 654 | /* `require' needs a table to keep loaded chunks */ |
| 641 | lua_newtable(L); | 655 | lua_newtable(L); |
| 642 | lua_pushvalue(L, -1); | 656 | lua_pushvalue(L, -1); |
| 643 | lua_setglobal(L, REQTAB); | 657 | lua_setglobal(L, "_LOADED"); |
| 644 | lua_pushcclosure(L, luaB_require, 1); | 658 | lua_pushcclosure(L, luaB_require, 1); |
| 645 | lua_setfield(L, -2, "require"); | 659 | lua_setfield(L, LUA_GLOBALSINDEX, "require"); |
| 646 | /* set global _G */ | 660 | /* set global _G */ |
| 647 | lua_pushvalue(L, -1); | 661 | lua_pushvalue(L, LUA_GLOBALSINDEX); |
| 648 | lua_setfield(L, -2, "_G"); | 662 | lua_setfield(L, LUA_GLOBALSINDEX, "_G"); |
| 663 | /* set global _PATH */ | ||
| 664 | path = getenv(LUA_PATH); | ||
| 665 | if (path == NULL) path = LUA_PATH_DEFAULT; | ||
| 666 | lua_pushstring(L, path); | ||
| 667 | lua_setfield(L, LUA_GLOBALSINDEX, "_PATH"); | ||
| 649 | } | 668 | } |
| 650 | 669 | ||
| 651 | 670 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.c,v 1.130 2004/07/13 19:56:44 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.131 2004/08/26 14:19:55 roberto Exp roberto $ |
| 3 | ** Lua stand-alone interpreter | 3 | ** Lua stand-alone interpreter |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -127,7 +127,12 @@ static int dostring (lua_State *L, const char *s, const char *name) { | |||
| 127 | 127 | ||
| 128 | 128 | ||
| 129 | static int dolibrary (lua_State *L, const char *name) { | 129 | static int dolibrary (lua_State *L, const char *name) { |
| 130 | name = luaL_searchpath(L, name, NULL); | 130 | lua_getfield(L, LUA_GLOBALSINDEX, "_PATH"); |
| 131 | if (!lua_isstring(L, -1)) { | ||
| 132 | l_message(progname, "global _PATH must be a string"); | ||
| 133 | return 1; | ||
| 134 | } | ||
| 135 | name = luaL_searchpath(L, name, lua_tostring(L, -1)); | ||
| 131 | if (name == NULL) return report(L, 1); | 136 | if (name == NULL) return report(L, 1); |
| 132 | else return dofile(L, name); | 137 | else return dofile(L, name); |
| 133 | } | 138 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: luaconf.h,v 1.9 2004/07/09 14:29:29 roberto Exp roberto $ | 2 | ** $Id: luaconf.h,v 1.10 2004/08/30 13:44:04 roberto Exp roberto $ |
| 3 | ** Configuration file for Lua | 3 | ** Configuration file for Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -271,10 +271,8 @@ | |||
| 271 | 271 | ||
| 272 | 272 | ||
| 273 | /* `assert' options */ | 273 | /* `assert' options */ |
| 274 | /* name of global that holds table with loaded packages */ | ||
| 275 | #define REQTAB "_LOADED" | ||
| 276 | 274 | ||
| 277 | /* name of global that holds the search path for packages */ | 275 | /* environment variable that holds the search path for packages */ |
| 278 | #define LUA_PATH "LUA_PATH" | 276 | #define LUA_PATH "LUA_PATH" |
| 279 | 277 | ||
| 280 | /* separator of templates in a path */ | 278 | /* separator of templates in a path */ |
