aboutsummaryrefslogtreecommitdiff
path: root/lbaselib.c
diff options
context:
space:
mode:
Diffstat (limited to 'lbaselib.c')
-rw-r--r--lbaselib.c72
1 files changed, 64 insertions, 8 deletions
diff --git a/lbaselib.c b/lbaselib.c
index 4c54a649..771d8525 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbaselib.c,v 1.157 2004/09/03 13:16:48 roberto Exp roberto $ 2** $Id: lbaselib.c,v 1.158 2004/09/15 20:39:42 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*/
@@ -463,24 +463,79 @@ static const char *getpath (lua_State *L) {
463static int luaB_require (lua_State *L) { 463static int luaB_require (lua_State *L) {
464 const char *name = luaL_checkstring(L, 1); 464 const char *name = luaL_checkstring(L, 1);
465 const char *fname; 465 const char *fname;
466 lua_getfield(L, lua_upvalueindex(1), name); 466 lua_settop(L, 1);
467 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
468 lua_getfield(L, 2, name);
467 if (lua_toboolean(L, -1)) /* is it there? */ 469 if (lua_toboolean(L, -1)) /* is it there? */
468 return 1; /* package is already loaded; return its result */ 470 return 1; /* package is already loaded; return its result */
469 /* else must load it; first mark it as loaded */ 471 /* else must load it; first mark it as loaded */
470 lua_pushboolean(L, 1); 472 lua_pushboolean(L, 1);
471 lua_setfield(L, lua_upvalueindex(1), name); /* _LOADED[name] = true */ 473 lua_setfield(L, 2, name); /* _LOADED[name] = true */
472 fname = luaL_searchpath(L, name, getpath(L)); 474 fname = luaL_gsub(L, name, ".", LUA_DIRSEP);
475 fname = luaL_searchpath(L, fname, getpath(L));
473 if (fname == NULL || luaL_loadfile(L, fname) != 0) 476 if (fname == NULL || luaL_loadfile(L, fname) != 0)
474 return luaL_error(L, "error loading package `%s' (%s)", name, 477 return luaL_error(L, "error loading package `%s' (%s)", name,
475 lua_tostring(L, -1)); 478 lua_tostring(L, -1));
476 lua_pushvalue(L, 1); /* pass name as argument to module */ 479 lua_pushvalue(L, 1); /* pass name as argument to module */
477 lua_call(L, 1, 1); /* run loaded module */ 480 lua_call(L, 1, 1); /* run loaded module */
478 if (!lua_isnil(L, -1)) /* nil return? */ 481 if (!lua_isnil(L, -1)) /* nil return? */
479 lua_setfield(L, lua_upvalueindex(1), name); 482 lua_setfield(L, 2, name);
480 lua_getfield(L, lua_upvalueindex(1), name); /* return _LOADED[name] */ 483 lua_getfield(L, 2, name); /* return _LOADED[name] */
481 return 1; 484 return 1;
482} 485}
483 486
487
488static void setfenv (lua_State *L) {
489 lua_Debug ar;
490 lua_getstack(L, 1, &ar);
491 lua_getinfo(L, "f", &ar);
492 lua_pushvalue(L, -2);
493 lua_setfenv(L, -2);
494}
495
496
497static int luaB_module (lua_State *L) {
498 const char *modname = luaL_checkstring(L, 1);
499 const char *dot;
500 lua_settop(L, 1);
501 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
502 /* try to find given table */
503 luaL_getfield(L, LUA_GLOBALSINDEX, modname);
504 if (lua_isnil(L, -1)) { /* not found? */
505 lua_pop(L, 1); /* remove previous result */
506 lua_newtable(L); /* create it */
507 /* register it with given name */
508 lua_pushvalue(L, -1);
509 luaL_setfield(L, LUA_GLOBALSINDEX, modname);
510 }
511 else if (!lua_istable(L, -1))
512 return luaL_error(L, "name conflict for module `%s'", modname);
513 /* check whether table already has a _NAME field */
514 lua_getfield(L, -1, "_NAME");
515 if (!lua_isnil(L, -1)) /* is table an initialized module? */
516 lua_pop(L, 1);
517 else { /* no; initialize it */
518 lua_pop(L, 1);
519 lua_newtable(L); /* create new metatable */
520 lua_pushvalue(L, LUA_GLOBALSINDEX);
521 lua_setfield(L, -2, "__index"); /* mt.__index = _G */
522 lua_setmetatable(L, -2);
523 lua_pushstring(L, modname);
524 lua_setfield(L, -2, "_NAME");
525 dot = strrchr(modname, '.'); /* look for last dot in module name */
526 if (dot == NULL) dot = modname;
527 else dot++;
528 /* set _PACK as package name (full module name minus last part) */
529 lua_pushlstring(L, modname, dot - modname);
530 lua_setfield(L, -2, "_PACK");
531 }
532 lua_pushvalue(L, -1);
533 lua_setfield(L, 2, modname); /* _LOADED[modname] = new table */
534 setfenv(L);
535 return 0;
536}
537
538
484/* }====================================================== */ 539/* }====================================================== */
485 540
486 541
@@ -509,6 +564,8 @@ static const luaL_reg base_funcs[] = {
509 {"dofile", luaB_dofile}, 564 {"dofile", luaB_dofile},
510 {"loadstring", luaB_loadstring}, 565 {"loadstring", luaB_loadstring},
511 {"load", luaB_load}, 566 {"load", luaB_load},
567 {"require", luaB_require},
568 {"module", luaB_module},
512 {NULL, NULL} 569 {NULL, NULL}
513}; 570};
514 571
@@ -676,8 +733,7 @@ static void base_open (lua_State *L) {
676 lua_newtable(L); 733 lua_newtable(L);
677 lua_pushvalue(L, -1); 734 lua_pushvalue(L, -1);
678 lua_setglobal(L, "_LOADED"); 735 lua_setglobal(L, "_LOADED");
679 lua_pushcclosure(L, luaB_require, 1); 736 lua_setfield(L, LUA_REGISTRYINDEX, "_LOADED");
680 lua_setfield(L, LUA_GLOBALSINDEX, "require");
681 /* set global _G */ 737 /* set global _G */
682 lua_pushvalue(L, LUA_GLOBALSINDEX); 738 lua_pushvalue(L, LUA_GLOBALSINDEX);
683 lua_setfield(L, LUA_GLOBALSINDEX, "_G"); 739 lua_setfield(L, LUA_GLOBALSINDEX, "_G");