diff options
Diffstat (limited to 'src/lua51/loadlib.c')
-rw-r--r-- | src/lua51/loadlib.c | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/src/lua51/loadlib.c b/src/lua51/loadlib.c index a7c68c6..b55f9b5 100644 --- a/src/lua51/loadlib.c +++ b/src/lua51/loadlib.c @@ -526,6 +526,7 @@ static void dooptions (lua_State *L, int n) { } +//c 设置模块的特殊域 _M _NAME _PACKAGE static void modinit (lua_State *L, const char *modname) { const char *dot; lua_pushvalue(L, -1); @@ -541,6 +542,7 @@ static void modinit (lua_State *L, const char *modname) { } +//c 创建模块即module("") static int ll_module (lua_State *L) { const char *modname = luaL_checkstring(L, 1); int loaded = lua_gettop(L) + 1; /* index of _LOADED table */ @@ -563,12 +565,13 @@ static int ll_module (lua_State *L) { modinit(L, modname); } lua_pushvalue(L, -1); - setfenv(L); + setfenv(L); // 注意这里会将模块的环境设为当前模块的表,所以外面的全局变量将会在后续无法访问到,除非开启packge.seeall dooptions(L, loaded - 1); return 0; } +//c 让这个模块的代码能够访问_G表,通过给模块设置一个元表,__index指向_G static int ll_seeall (lua_State *L) { luaL_checktype(L, 1, LUA_TTABLE); if (!lua_getmetatable(L, 1)) { @@ -624,12 +627,19 @@ static const lua_CFunction loaders[] = {loader_preload, loader_Lua, loader_C, loader_Croot, NULL}; + +//c package模块 , 包含 +// loaded 表,即registry["_LOADED"] +// preload 表 +// LUALIB_API int luaopen_package (lua_State *L) { int i; + /* create new type _LOADLIB */ luaL_newmetatable(L, "_LOADLIB"); lua_pushcfunction(L, gctm); lua_setfield(L, -2, "__gc"); + /* create `package' table */ luaL_register(L, LUA_LOADLIBNAME, pk_funcs); #if defined(LUA_COMPAT_LOADLIB) @@ -638,6 +648,7 @@ LUALIB_API int luaopen_package (lua_State *L) { #endif lua_pushvalue(L, -1); lua_replace(L, LUA_ENVIRONINDEX); + /* create `loaders' table */ lua_createtable(L, sizeof(loaders)/sizeof(loaders[0]) - 1, 0); /* fill it with pre-defined loaders */ @@ -648,6 +659,7 @@ LUALIB_API int luaopen_package (lua_State *L) { lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */ setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT); /* set field `path' */ setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT); /* set field `cpath' */ + /* store config information */ lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n" LUA_EXECDIR "\n" LUA_IGMARK); |