aboutsummaryrefslogtreecommitdiff
path: root/lbaselib.c
diff options
context:
space:
mode:
Diffstat (limited to 'lbaselib.c')
-rw-r--r--lbaselib.c35
1 files changed, 27 insertions, 8 deletions
diff --git a/lbaselib.c b/lbaselib.c
index 7bed0bb5..522516c0 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -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
455static 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
455static int luaB_require (lua_State *L) { 468static 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
624static void base_open (lua_State *L) { 637static 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