diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-07-05 16:29:03 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-07-05 16:29:03 -0300 |
commit | 7f97ec286e5ab29db06ed43aab01cc5cb673bb64 (patch) | |
tree | 6ae9ee4b7ba356041abb90b5bde3e6bc774f9497 /loadlib.c | |
parent | e82b41aac2c81b701a2a0aa33bb2ef4d5c12f0dc (diff) | |
download | lua-7f97ec286e5ab29db06ed43aab01cc5cb673bb64.tar.gz lua-7f97ec286e5ab29db06ed43aab01cc5cb673bb64.tar.bz2 lua-7f97ec286e5ab29db06ed43aab01cc5cb673bb64.zip |
parent modules are also imported toghether with the module itself
Diffstat (limited to 'loadlib.c')
-rw-r--r-- | loadlib.c | 39 |
1 files changed, 26 insertions, 13 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: loadlib.c,v 1.29 2005/05/20 19:09:05 roberto Exp roberto $ | 2 | ** $Id: loadlib.c,v 1.30 2005/06/27 17:24:40 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 | ** |
@@ -366,22 +366,21 @@ static int loader_preload (lua_State *L) { | |||
366 | } | 366 | } |
367 | 367 | ||
368 | 368 | ||
369 | static int ll_require (lua_State *L) { | 369 | static void require_aux (lua_State *L, const char *name) { |
370 | const char *name = luaL_checkstring(L, 1); | ||
371 | int i; | 370 | int i; |
372 | lua_settop(L, 1); | 371 | int loadedtable = lua_gettop(L) + 1; |
373 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); | 372 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); |
374 | lua_getfield(L, 2, name); | 373 | lua_getfield(L, loadedtable, name); |
375 | if (lua_toboolean(L, -1)) /* is it there? */ | 374 | if (lua_toboolean(L, -1)) /* is it there? */ |
376 | return 1; /* package is already loaded; return its result */ | 375 | return; /* package is already loaded; return its result */ |
377 | /* else must load it; iterate over available loaders */ | 376 | /* else must load it; iterate over available loaders */ |
378 | lua_getfield(L, LUA_ENVIRONINDEX, "loaders"); | 377 | lua_getfield(L, LUA_ENVIRONINDEX, "loaders"); |
379 | if (!lua_istable(L, -1)) | 378 | if (!lua_istable(L, -1)) |
380 | luaL_error(L, LUA_QL("package.loaders") " must be a table"); | 379 | luaL_error(L, LUA_QL("package.loaders") " must be a table"); |
381 | for (i=1;; i++) { | 380 | for (i=1; ; i++) { |
382 | lua_rawgeti(L, -1, i); /* get a loader */ | 381 | lua_rawgeti(L, -1, i); /* get a loader */ |
383 | if (lua_isnil(L, -1)) | 382 | if (lua_isnil(L, -1)) |
384 | return luaL_error(L, "package " LUA_QS " not found", name); | 383 | luaL_error(L, "package " LUA_QS " not found", name); |
385 | lua_pushstring(L, name); | 384 | lua_pushstring(L, name); |
386 | lua_call(L, 1, 1); /* call it */ | 385 | lua_call(L, 1, 1); /* call it */ |
387 | if (lua_isnil(L, -1)) lua_pop(L, 1); | 386 | if (lua_isnil(L, -1)) lua_pop(L, 1); |
@@ -389,16 +388,30 @@ static int ll_require (lua_State *L) { | |||
389 | } | 388 | } |
390 | /* mark module as loaded */ | 389 | /* mark module as loaded */ |
391 | lua_pushboolean(L, 1); | 390 | lua_pushboolean(L, 1); |
392 | lua_setfield(L, 2, name); /* _LOADED[name] = true */ | 391 | lua_setfield(L, loadedtable, name); /* _LOADED[name] = true */ |
393 | lua_pushvalue(L, 1); /* pass name as argument to module */ | 392 | lua_pushstring(L, name); /* pass name as argument to module */ |
394 | lua_call(L, 1, 1); /* run loaded module */ | 393 | lua_call(L, 1, 1); /* run loaded module */ |
395 | if (!lua_isnil(L, -1)) /* non-nil return? */ | 394 | if (!lua_isnil(L, -1)) /* non-nil return? */ |
396 | lua_setfield(L, 2, name); /* update _LOADED[name] with returned value */ | 395 | lua_setfield(L, loadedtable, name); /* _LOADED[name] = returned value */ |
397 | lua_getfield(L, 2, name); /* return _LOADED[name] */ | 396 | lua_getfield(L, loadedtable, name); /* return _LOADED[name] */ |
398 | return 1; | 397 | return; |
399 | } | 398 | } |
400 | 399 | ||
401 | 400 | ||
401 | static int ll_require (lua_State *L) { | ||
402 | const char *name = luaL_checkstring(L, 1); | ||
403 | const char *pt; | ||
404 | /* load all parent modules */ | ||
405 | for (pt = name; (pt = strchr(pt, '.')) != NULL; pt++) { | ||
406 | lua_settop(L, 1); | ||
407 | lua_pushlstring(L, name, pt - name); | ||
408 | require_aux(L, lua_tostring(L, -1)); | ||
409 | } | ||
410 | require_aux(L, name); /* load module itself */ | ||
411 | return 1; | ||
412 | } | ||
413 | |||
414 | |||
402 | static void setfenv (lua_State *L) { | 415 | static void setfenv (lua_State *L) { |
403 | lua_Debug ar; | 416 | lua_Debug ar; |
404 | lua_getstack(L, 1, &ar); | 417 | lua_getstack(L, 1, &ar); |