mirror of
https://github.com/lua/lua.git
synced 2025-02-04 06:13:04 +08:00
parent modules are also imported toghether with the module itself
This commit is contained in:
parent
e82b41aac2
commit
7f97ec286e
35
loadlib.c
35
loadlib.c
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: loadlib.c,v 1.29 2005/05/20 19:09:05 roberto Exp roberto $
|
** $Id: loadlib.c,v 1.30 2005/06/27 17:24:40 roberto Exp roberto $
|
||||||
** Dynamic library loader for Lua
|
** Dynamic library loader for Lua
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
**
|
**
|
||||||
@ -366,14 +366,13 @@ static int loader_preload (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int ll_require (lua_State *L) {
|
static void require_aux (lua_State *L, const char *name) {
|
||||||
const char *name = luaL_checkstring(L, 1);
|
|
||||||
int i;
|
int i;
|
||||||
lua_settop(L, 1);
|
int loadedtable = lua_gettop(L) + 1;
|
||||||
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
|
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
|
||||||
lua_getfield(L, 2, name);
|
lua_getfield(L, loadedtable, name);
|
||||||
if (lua_toboolean(L, -1)) /* is it there? */
|
if (lua_toboolean(L, -1)) /* is it there? */
|
||||||
return 1; /* package is already loaded; return its result */
|
return; /* package is already loaded; return its result */
|
||||||
/* else must load it; iterate over available loaders */
|
/* else must load it; iterate over available loaders */
|
||||||
lua_getfield(L, LUA_ENVIRONINDEX, "loaders");
|
lua_getfield(L, LUA_ENVIRONINDEX, "loaders");
|
||||||
if (!lua_istable(L, -1))
|
if (!lua_istable(L, -1))
|
||||||
@ -381,7 +380,7 @@ static int ll_require (lua_State *L) {
|
|||||||
for (i=1; ; i++) {
|
for (i=1; ; i++) {
|
||||||
lua_rawgeti(L, -1, i); /* get a loader */
|
lua_rawgeti(L, -1, i); /* get a loader */
|
||||||
if (lua_isnil(L, -1))
|
if (lua_isnil(L, -1))
|
||||||
return luaL_error(L, "package " LUA_QS " not found", name);
|
luaL_error(L, "package " LUA_QS " not found", name);
|
||||||
lua_pushstring(L, name);
|
lua_pushstring(L, name);
|
||||||
lua_call(L, 1, 1); /* call it */
|
lua_call(L, 1, 1); /* call it */
|
||||||
if (lua_isnil(L, -1)) lua_pop(L, 1);
|
if (lua_isnil(L, -1)) lua_pop(L, 1);
|
||||||
@ -389,12 +388,26 @@ static int ll_require (lua_State *L) {
|
|||||||
}
|
}
|
||||||
/* mark module as loaded */
|
/* mark module as loaded */
|
||||||
lua_pushboolean(L, 1);
|
lua_pushboolean(L, 1);
|
||||||
lua_setfield(L, 2, name); /* _LOADED[name] = true */
|
lua_setfield(L, loadedtable, name); /* _LOADED[name] = true */
|
||||||
lua_pushvalue(L, 1); /* pass name as argument to module */
|
lua_pushstring(L, name); /* pass name as argument to module */
|
||||||
lua_call(L, 1, 1); /* run loaded module */
|
lua_call(L, 1, 1); /* run loaded module */
|
||||||
if (!lua_isnil(L, -1)) /* non-nil return? */
|
if (!lua_isnil(L, -1)) /* non-nil return? */
|
||||||
lua_setfield(L, 2, name); /* update _LOADED[name] with returned value */
|
lua_setfield(L, loadedtable, name); /* _LOADED[name] = returned value */
|
||||||
lua_getfield(L, 2, name); /* return _LOADED[name] */
|
lua_getfield(L, loadedtable, name); /* return _LOADED[name] */
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int ll_require (lua_State *L) {
|
||||||
|
const char *name = luaL_checkstring(L, 1);
|
||||||
|
const char *pt;
|
||||||
|
/* load all parent modules */
|
||||||
|
for (pt = name; (pt = strchr(pt, '.')) != NULL; pt++) {
|
||||||
|
lua_settop(L, 1);
|
||||||
|
lua_pushlstring(L, name, pt - name);
|
||||||
|
require_aux(L, lua_tostring(L, -1));
|
||||||
|
}
|
||||||
|
require_aux(L, name); /* load module itself */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user