diff options
Diffstat (limited to 'lauxlib.c')
-rw-r--r-- | lauxlib.c | 29 |
1 files changed, 15 insertions, 14 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.147 2005/08/18 16:04:05 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.148 2005/08/18 20:36:26 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 | */ |
@@ -231,22 +231,23 @@ LUALIB_API void luaI_openlib (lua_State *L, const char *libname, | |||
231 | const luaL_reg *l, int nup) { | 231 | const luaL_reg *l, int nup) { |
232 | if (libname) { | 232 | if (libname) { |
233 | /* check whether lib already exists */ | 233 | /* check whether lib already exists */ |
234 | luaL_getfield(L, LUA_GLOBALSINDEX, libname); | 234 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); |
235 | if (lua_isnil(L, -1)) { /* not found? */ | 235 | lua_getfield(L, -1, libname); /* get _LOADED[libname] */ |
236 | if (!lua_istable(L, -1)) { /* not found? */ | ||
236 | lua_pop(L, 1); /* remove previous result */ | 237 | lua_pop(L, 1); /* remove previous result */ |
237 | lua_newtable(L); /* create it */ | 238 | luaL_getfield(L, LUA_GLOBALSINDEX, libname); /* try global variable */ |
238 | if (lua_getmetatable(L, LUA_GLOBALSINDEX)) | 239 | if (!lua_istable(L, -1)) { |
239 | lua_setmetatable(L, -2); /* share metatable with global table */ | 240 | if (!lua_isnil(L, -1)) |
240 | /* register it with given name */ | 241 | luaL_error(L, "name conflict for module " LUA_QS, libname); |
242 | lua_pop(L, 1); | ||
243 | lua_newtable(L); /* create it */ | ||
244 | lua_pushvalue(L, -1); /* register it with given name */ | ||
245 | luaL_setfield(L, LUA_GLOBALSINDEX, libname); | ||
246 | } | ||
241 | lua_pushvalue(L, -1); | 247 | lua_pushvalue(L, -1); |
242 | luaL_setfield(L, LUA_GLOBALSINDEX, libname); | 248 | lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ |
243 | } | 249 | } |
244 | else if (!lua_istable(L, -1)) | 250 | lua_remove(L, -2); /* remove _LOADED table */ |
245 | luaL_error(L, "name conflict for library " LUA_QS, libname); | ||
246 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); | ||
247 | lua_pushvalue(L, -2); | ||
248 | lua_setfield(L, -2, libname); /* _LOADED[modname] = new table */ | ||
249 | lua_pop(L, 1); /* remove _LOADED table */ | ||
250 | lua_insert(L, -(nup+1)); /* move library table to below upvalues */ | 251 | lua_insert(L, -(nup+1)); /* move library table to below upvalues */ |
251 | } | 252 | } |
252 | for (; l->name; l++) { | 253 | for (; l->name; l++) { |