LUALIB_API int luaopen_package(lua_State *L) { int i; int noenv; luaL_newmetatable(L, "_LOADLIB"); lj_lib_pushcf(L, lj_cf_package_unloadlib, 1); lua_setfield(L, -2, "__gc"); luaL_register(L, LUA_LOADLIBNAME, package_lib); lua_pushvalue(L, -1); lua_replace(L, LUA_ENVIRONINDEX); lua_createtable(L, sizeof(package_loaders)/sizeof(package_loaders[0])-1, 0); for (i = 0; package_loaders[i] != NULL; i++) { lj_lib_pushcf(L, package_loaders[i], 1); lua_rawseti(L, -2, i+1); } lua_setfield(L, -2, "loaders"); lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); noenv = lua_toboolean(L, -1); lua_pop(L, 1); setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT, noenv); setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT, noenv); lua_pushliteral(L, LUA_PATH_CONFIG); lua_setfield(L, -2, "config"); luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16); lua_setfield(L, -2, "loaded"); luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD", 4); lua_setfield(L, -2, "preload"); lua_pushvalue(L, LUA_GLOBALSINDEX); luaL_register(L, NULL, package_global); lua_pop(L, 1); return 1; }
int lj_lib_postreg(lua_State *L, lua_CFunction cf, int id, const char *name) { GCfunc *fn = lj_lib_pushcf(L, cf, id); GCtab *t = tabref(curr_func(L)->c.env); /* Reference to parent table. */ setfuncV(L, lj_tab_setstr(L, t, lj_str_newz(L, name)), fn); lj_gc_anybarriert(L, t); setfuncV(L, L->top++, fn); return 1; }