diff options
Diffstat (limited to 'loadlib.c')
| -rw-r--r-- | loadlib.c | 27 |
1 files changed, 15 insertions, 12 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: loadlib.c,v 1.38 2005/08/15 14:12:32 roberto Exp roberto $ | 2 | ** $Id: loadlib.c,v 1.39 2005/08/17 19:05:04 roberto Exp roberto $ |
| 3 | ** Dynamic library loader for Lua | 3 | ** Dynamic library loader for Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | ** | 5 | ** |
| @@ -508,24 +508,29 @@ static int ll_module (lua_State *L) { | |||
| 508 | const char *dot; | 508 | const char *dot; |
| 509 | lua_settop(L, 1); | 509 | lua_settop(L, 1); |
| 510 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); | 510 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); |
| 511 | /* try to find given table */ | 511 | lua_getfield(L, 2, modname); /* get _LOADED[modname] */ |
| 512 | luaL_getfield(L, LUA_GLOBALSINDEX, modname); | 512 | if (!lua_istable(L, -1)) { /* not found? */ |
| 513 | if (lua_isnil(L, -1)) { /* not found? */ | ||
| 514 | lua_pop(L, 1); /* remove previous result */ | 513 | lua_pop(L, 1); /* remove previous result */ |
| 515 | lua_newtable(L); /* create it */ | 514 | luaL_getfield(L, LUA_GLOBALSINDEX, modname); /* try global variable */ |
| 516 | /* register it with given name */ | 515 | if (!lua_istable(L, -1)) { |
| 516 | if (!lua_isnil(L, -1)) | ||
| 517 | return luaL_error(L, "name conflict for module " LUA_QS, modname); | ||
| 518 | lua_pop(L, 1); | ||
| 519 | lua_newtable(L); /* create it */ | ||
| 520 | lua_pushvalue(L, -1); /* register it with given name */ | ||
| 521 | luaL_setfield(L, LUA_GLOBALSINDEX, modname); | ||
| 522 | } | ||
| 517 | lua_pushvalue(L, -1); | 523 | lua_pushvalue(L, -1); |
| 518 | luaL_setfield(L, LUA_GLOBALSINDEX, modname); | 524 | lua_setfield(L, 2, modname); /* _LOADED[modname] = new table */ |
| 519 | } | 525 | } |
| 520 | else if (!lua_istable(L, -1)) | ||
| 521 | return luaL_error(L, "name conflict for module " LUA_QS, modname); | ||
| 522 | /* check whether table already has a _NAME field */ | 526 | /* check whether table already has a _NAME field */ |
| 523 | lua_getfield(L, -1, "_NAME"); | 527 | lua_getfield(L, -1, "_NAME"); |
| 524 | if (!lua_isnil(L, -1)) /* is table an initialized module? */ | 528 | if (!lua_isnil(L, -1)) /* is table an initialized module? */ |
| 525 | lua_pop(L, 1); | 529 | lua_pop(L, 1); |
| 526 | else { /* no; initialize it */ | 530 | else { /* no; initialize it */ |
| 527 | lua_pop(L, 1); | 531 | lua_pop(L, 1); |
| 528 | lua_newtable(L); /* create new metatable */ | 532 | /* create new metatable */ |
| 533 | lua_newtable(L); | ||
| 529 | lua_pushvalue(L, LUA_GLOBALSINDEX); | 534 | lua_pushvalue(L, LUA_GLOBALSINDEX); |
| 530 | lua_setfield(L, -2, "__index"); /* mt.__index = _G */ | 535 | lua_setfield(L, -2, "__index"); /* mt.__index = _G */ |
| 531 | lua_setmetatable(L, -2); | 536 | lua_setmetatable(L, -2); |
| @@ -540,8 +545,6 @@ static int ll_module (lua_State *L) { | |||
| 540 | lua_pushlstring(L, modname, dot - modname); | 545 | lua_pushlstring(L, modname, dot - modname); |
| 541 | lua_setfield(L, -2, "_PACKAGE"); | 546 | lua_setfield(L, -2, "_PACKAGE"); |
| 542 | } | 547 | } |
| 543 | lua_pushvalue(L, -1); | ||
| 544 | lua_setfield(L, 2, modname); /* _LOADED[modname] = new table */ | ||
| 545 | setfenv(L); | 548 | setfenv(L); |
| 546 | return 0; | 549 | return 0; |
| 547 | } | 550 | } |
