diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-05-31 13:34:19 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-05-31 13:34:19 -0300 |
commit | 2c1a5d678daadec5de189c58a5fef7e12d463b71 (patch) | |
tree | 00d363586a9efe67b532c19c96ffdefca31396ba /loadlib.c | |
parent | bd619b931173fc35f38dfbb07746bcdc5ef11808 (diff) | |
download | lua-2c1a5d678daadec5de189c58a5fef7e12d463b71.tar.gz lua-2c1a5d678daadec5de189c58a5fef7e12d463b71.tar.bz2 lua-2c1a5d678daadec5de189c58a5fef7e12d463b71.zip |
factoring out common code in 'module' and 'luaL_openlib'
Diffstat (limited to 'loadlib.c')
-rw-r--r-- | loadlib.c | 18 |
1 files changed, 4 insertions, 14 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: loadlib.c,v 1.81 2010/03/17 21:37:37 roberto Exp roberto $ | 2 | ** $Id: loadlib.c,v 1.82 2010/03/19 15:02:34 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 | ** |
@@ -620,18 +620,8 @@ static void modinit (lua_State *L, const char *modname) { | |||
620 | 620 | ||
621 | static int ll_module (lua_State *L) { | 621 | static int ll_module (lua_State *L) { |
622 | const char *modname = luaL_checkstring(L, 1); | 622 | const char *modname = luaL_checkstring(L, 1); |
623 | int loaded = lua_gettop(L) + 1; /* index of _LOADED table */ | 623 | int lastarg = lua_gettop(L); /* last parameter */ |
624 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); | 624 | luaL_pushmodule(L, modname, 1); /* get/create module table */ |
625 | lua_getfield(L, loaded, modname); /* get _LOADED[modname] */ | ||
626 | if (!lua_istable(L, -1)) { /* not found? */ | ||
627 | lua_pop(L, 1); /* remove previous result */ | ||
628 | /* try global variable (and create one if it does not exist) */ | ||
629 | lua_pushglobaltable(L); | ||
630 | if (luaL_findtable(L, 0, modname, 1) != NULL) | ||
631 | return luaL_error(L, "name conflict for module " LUA_QS, modname); | ||
632 | lua_pushvalue(L, -1); | ||
633 | lua_setfield(L, loaded, modname); /* _LOADED[modname] = new table */ | ||
634 | } | ||
635 | /* check whether table already has a _NAME field */ | 625 | /* check whether table already has a _NAME field */ |
636 | lua_getfield(L, -1, "_NAME"); | 626 | lua_getfield(L, -1, "_NAME"); |
637 | if (!lua_isnil(L, -1)) /* is table an initialized module? */ | 627 | if (!lua_isnil(L, -1)) /* is table an initialized module? */ |
@@ -642,7 +632,7 @@ static int ll_module (lua_State *L) { | |||
642 | } | 632 | } |
643 | lua_pushvalue(L, -1); | 633 | lua_pushvalue(L, -1); |
644 | set_env(L); | 634 | set_env(L); |
645 | dooptions(L, loaded - 1); | 635 | dooptions(L, lastarg); |
646 | return 1; | 636 | return 1; |
647 | } | 637 | } |
648 | 638 | ||