diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-11-10 18:00:04 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-11-10 18:00:04 -0200 |
| commit | 0a6a6b9d9d7b4ad0d627a8841ce4409e437bf70e (patch) | |
| tree | b3df95519f235fcf53e8823936068ed3e9ef9723 | |
| parent | 1b54197491f3305be08a5a77745865f268c790b5 (diff) | |
| download | lua-0a6a6b9d9d7b4ad0d627a8841ce4409e437bf70e.tar.gz lua-0a6a6b9d9d7b4ad0d627a8841ce4409e437bf70e.tar.bz2 lua-0a6a6b9d9d7b4ad0d627a8841ce4409e437bf70e.zip | |
no more sentinel to detect loops in module dependencies;
usual message for infinite recursion is good enough.
| -rw-r--r-- | loadlib.c | 15 |
1 files changed, 3 insertions, 12 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: loadlib.c,v 1.92 2010/10/29 14:35:09 roberto Exp roberto $ | 2 | ** $Id: loadlib.c,v 1.93 2010/11/10 18:05:36 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 | ** |
| @@ -434,21 +434,14 @@ static int loader_preload (lua_State *L) { | |||
| 434 | } | 434 | } |
| 435 | 435 | ||
| 436 | 436 | ||
| 437 | static const int sentinel_ = 0; | ||
| 438 | #define sentinel ((void *)&sentinel_) | ||
| 439 | |||
| 440 | |||
| 441 | static int ll_require (lua_State *L) { | 437 | static int ll_require (lua_State *L) { |
| 442 | const char *name = luaL_checkstring(L, 1); | 438 | const char *name = luaL_checkstring(L, 1); |
| 443 | int i; | 439 | int i; |
| 444 | lua_settop(L, 1); /* _LOADED table will be at index 2 */ | 440 | lua_settop(L, 1); /* _LOADED table will be at index 2 */ |
| 445 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); | 441 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); |
| 446 | lua_getfield(L, 2, name); | 442 | lua_getfield(L, 2, name); |
| 447 | if (lua_toboolean(L, -1)) { /* is it there? */ | 443 | if (lua_toboolean(L, -1)) /* is it there? */ |
| 448 | if (lua_touserdata(L, -1) == sentinel) /* check loops */ | ||
| 449 | luaL_error(L, "loop or previous error loading module " LUA_QS, name); | ||
| 450 | return 1; /* package is already loaded */ | 444 | return 1; /* package is already loaded */ |
| 451 | } | ||
| 452 | /* else must load it; iterate over available loaders */ | 445 | /* else must load it; iterate over available loaders */ |
| 453 | lua_getfield(L, lua_upvalueindex(1), "loaders"); | 446 | lua_getfield(L, lua_upvalueindex(1), "loaders"); |
| 454 | if (!lua_istable(L, -1)) | 447 | if (!lua_istable(L, -1)) |
| @@ -468,14 +461,12 @@ static int ll_require (lua_State *L) { | |||
| 468 | else | 461 | else |
| 469 | lua_pop(L, 1); | 462 | lua_pop(L, 1); |
| 470 | } | 463 | } |
| 471 | lua_pushlightuserdata(L, sentinel); | ||
| 472 | lua_setfield(L, 2, name); /* _LOADED[name] = sentinel */ | ||
| 473 | lua_pushstring(L, name); /* pass name as argument to module */ | 464 | lua_pushstring(L, name); /* pass name as argument to module */ |
| 474 | lua_call(L, 1, 1); /* run loaded module */ | 465 | lua_call(L, 1, 1); /* run loaded module */ |
| 475 | if (!lua_isnil(L, -1)) /* non-nil return? */ | 466 | if (!lua_isnil(L, -1)) /* non-nil return? */ |
| 476 | lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ | 467 | lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ |
| 477 | lua_getfield(L, 2, name); | 468 | lua_getfield(L, 2, name); |
| 478 | if (lua_touserdata(L, -1) == sentinel) { /* module did not set a value? */ | 469 | if (lua_isnil(L, -1)) { /* module did not set a value? */ |
| 479 | lua_pushboolean(L, 1); /* use true as result */ | 470 | lua_pushboolean(L, 1); /* use true as result */ |
| 480 | lua_pushvalue(L, -1); /* extra copy to be returned */ | 471 | lua_pushvalue(L, -1); /* extra copy to be returned */ |
| 481 | lua_setfield(L, 2, name); /* _LOADED[name] = true */ | 472 | lua_setfield(L, 2, name); /* _LOADED[name] = true */ |
