mirror of
https://github.com/lua/lua.git
synced 2025-01-28 06:03:00 +08:00
parent modules are also imported toghether with the module itself
This commit is contained in:
parent
e82b41aac2
commit
7f97ec286e
39
loadlib.c
39
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
|
||||
** See Copyright Notice in lua.h
|
||||
**
|
||||
@ -366,22 +366,21 @@ static int loader_preload (lua_State *L) {
|
||||
}
|
||||
|
||||
|
||||
static int ll_require (lua_State *L) {
|
||||
const char *name = luaL_checkstring(L, 1);
|
||||
static void require_aux (lua_State *L, const char *name) {
|
||||
int i;
|
||||
lua_settop(L, 1);
|
||||
int loadedtable = lua_gettop(L) + 1;
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
|
||||
lua_getfield(L, 2, name);
|
||||
lua_getfield(L, loadedtable, name);
|
||||
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 */
|
||||
lua_getfield(L, LUA_ENVIRONINDEX, "loaders");
|
||||
if (!lua_istable(L, -1))
|
||||
luaL_error(L, LUA_QL("package.loaders") " must be a table");
|
||||
for (i=1;; i++) {
|
||||
for (i=1; ; i++) {
|
||||
lua_rawgeti(L, -1, i); /* get a loader */
|
||||
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_call(L, 1, 1); /* call it */
|
||||
if (lua_isnil(L, -1)) lua_pop(L, 1);
|
||||
@ -389,16 +388,30 @@ static int ll_require (lua_State *L) {
|
||||
}
|
||||
/* mark module as loaded */
|
||||
lua_pushboolean(L, 1);
|
||||
lua_setfield(L, 2, name); /* _LOADED[name] = true */
|
||||
lua_pushvalue(L, 1); /* pass name as argument to module */
|
||||
lua_setfield(L, loadedtable, name); /* _LOADED[name] = true */
|
||||
lua_pushstring(L, name); /* pass name as argument to module */
|
||||
lua_call(L, 1, 1); /* run loaded module */
|
||||
if (!lua_isnil(L, -1)) /* non-nil return? */
|
||||
lua_setfield(L, 2, name); /* update _LOADED[name] with returned value */
|
||||
lua_getfield(L, 2, name); /* return _LOADED[name] */
|
||||
return 1;
|
||||
lua_setfield(L, loadedtable, name); /* _LOADED[name] = returned value */
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
static void setfenv (lua_State *L) {
|
||||
lua_Debug ar;
|
||||
lua_getstack(L, 1, &ar);
|
||||
|
Loading…
x
Reference in New Issue
Block a user