rlm@1: /* rlm@1: ** $Id: loadlib.c,v 1.52.1.3 2008/08/06 13:29:28 roberto Exp $ rlm@1: ** Dynamic library loader for Lua rlm@1: ** See Copyright Notice in lua.h rlm@1: ** rlm@1: ** This module contains an implementation of loadlib for Unix systems rlm@1: ** that have dlfcn, an implementation for Darwin (Mac OS X), an rlm@1: ** implementation for Windows, and a stub for other systems. rlm@1: */ rlm@1: rlm@1: rlm@1: #include rlm@1: #include rlm@1: rlm@1: rlm@1: #define loadlib_c rlm@1: #define LUA_LIB rlm@1: rlm@1: #include "lua.h" rlm@1: rlm@1: #include "lauxlib.h" rlm@1: #include "lualib.h" rlm@1: rlm@1: rlm@1: /* prefix for open functions in C libraries */ rlm@1: #define LUA_POF "luaopen_" rlm@1: rlm@1: /* separator for open functions in C libraries */ rlm@1: #define LUA_OFSEP "_" rlm@1: rlm@1: rlm@1: #define LIBPREFIX "LOADLIB: " rlm@1: rlm@1: #define POF LUA_POF rlm@1: #define LIB_FAIL "open" rlm@1: rlm@1: rlm@1: /* error codes for ll_loadfunc */ rlm@1: #define ERRLIB 1 rlm@1: #define ERRFUNC 2 rlm@1: rlm@1: #define setprogdir(L) ((void)0) rlm@1: rlm@1: rlm@1: static void ll_unloadlib (void *lib); rlm@1: static void *ll_load (lua_State *L, const char *path); rlm@1: static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym); rlm@1: rlm@1: rlm@1: rlm@1: #if defined(LUA_DL_DLOPEN) rlm@1: /* rlm@1: ** {======================================================================== rlm@1: ** This is an implementation of loadlib based on the dlfcn interface. rlm@1: ** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD, rlm@1: ** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least rlm@1: ** as an emulation layer on top of native functions. rlm@1: ** ========================================================================= rlm@1: */ rlm@1: rlm@1: #include rlm@1: rlm@1: static void ll_unloadlib (void *lib) { rlm@1: dlclose(lib); rlm@1: } rlm@1: rlm@1: rlm@1: static void *ll_load (lua_State *L, const char *path) { rlm@1: void *lib = dlopen(path, RTLD_NOW); rlm@1: if (lib == NULL) lua_pushstring(L, dlerror()); rlm@1: return lib; rlm@1: } rlm@1: rlm@1: rlm@1: static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { rlm@1: lua_CFunction f = (lua_CFunction)dlsym(lib, sym); rlm@1: if (f == NULL) lua_pushstring(L, dlerror()); rlm@1: return f; rlm@1: } rlm@1: rlm@1: /* }====================================================== */ rlm@1: rlm@1: rlm@1: rlm@1: #elif defined(LUA_DL_DLL) rlm@1: /* rlm@1: ** {====================================================================== rlm@1: ** This is an implementation of loadlib for Windows using native functions. rlm@1: ** ======================================================================= rlm@1: */ rlm@1: rlm@1: #include rlm@1: rlm@1: rlm@1: #undef setprogdir rlm@1: rlm@1: static void setprogdir (lua_State *L) { rlm@1: char buff[MAX_PATH + 1]; rlm@1: char *lb; rlm@1: DWORD nsize = sizeof(buff)/sizeof(char); rlm@1: DWORD n = GetModuleFileNameA(NULL, buff, nsize); rlm@1: if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) rlm@1: luaL_error(L, "unable to get ModuleFileName"); rlm@1: else { rlm@1: *lb = '\0'; rlm@1: luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, buff); rlm@1: lua_remove(L, -2); /* remove original string */ rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: static void pusherror (lua_State *L) { rlm@1: int error = GetLastError(); rlm@1: char buffer[128]; rlm@1: if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, rlm@1: NULL, error, 0, buffer, sizeof(buffer), NULL)) rlm@1: lua_pushstring(L, buffer); rlm@1: else rlm@1: lua_pushfstring(L, "system error %d\n", error); rlm@1: } rlm@1: rlm@1: static void ll_unloadlib (void *lib) { rlm@1: FreeLibrary((HINSTANCE)lib); rlm@1: } rlm@1: rlm@1: rlm@1: static void *ll_load (lua_State *L, const char *path) { rlm@1: HINSTANCE lib = LoadLibraryA(path); rlm@1: if (lib == NULL) pusherror(L); rlm@1: return lib; rlm@1: } rlm@1: rlm@1: rlm@1: static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { rlm@1: lua_CFunction f = (lua_CFunction)GetProcAddress((HINSTANCE)lib, sym); rlm@1: if (f == NULL) pusherror(L); rlm@1: return f; rlm@1: } rlm@1: rlm@1: /* }====================================================== */ rlm@1: rlm@1: rlm@1: rlm@1: #elif defined(LUA_DL_DYLD) rlm@1: /* rlm@1: ** {====================================================================== rlm@1: ** Native Mac OS X / Darwin Implementation rlm@1: ** ======================================================================= rlm@1: */ rlm@1: rlm@1: #include rlm@1: rlm@1: rlm@1: /* Mac appends a `_' before C function names */ rlm@1: #undef POF rlm@1: #define POF "_" LUA_POF rlm@1: rlm@1: rlm@1: static void pusherror (lua_State *L) { rlm@1: const char *err_str; rlm@1: const char *err_file; rlm@1: NSLinkEditErrors err; rlm@1: int err_num; rlm@1: NSLinkEditError(&err, &err_num, &err_file, &err_str); rlm@1: lua_pushstring(L, err_str); rlm@1: } rlm@1: rlm@1: rlm@1: static const char *errorfromcode (NSObjectFileImageReturnCode ret) { rlm@1: switch (ret) { rlm@1: case NSObjectFileImageInappropriateFile: rlm@1: return "file is not a bundle"; rlm@1: case NSObjectFileImageArch: rlm@1: return "library is for wrong CPU type"; rlm@1: case NSObjectFileImageFormat: rlm@1: return "bad format"; rlm@1: case NSObjectFileImageAccess: rlm@1: return "cannot access file"; rlm@1: case NSObjectFileImageFailure: rlm@1: default: rlm@1: return "unable to load library"; rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: static void ll_unloadlib (void *lib) { rlm@1: NSUnLinkModule((NSModule)lib, NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES); rlm@1: } rlm@1: rlm@1: rlm@1: static void *ll_load (lua_State *L, const char *path) { rlm@1: NSObjectFileImage img; rlm@1: NSObjectFileImageReturnCode ret; rlm@1: /* this would be a rare case, but prevents crashing if it happens */ rlm@1: if(!_dyld_present()) { rlm@1: lua_pushliteral(L, "dyld not present"); rlm@1: return NULL; rlm@1: } rlm@1: ret = NSCreateObjectFileImageFromFile(path, &img); rlm@1: if (ret == NSObjectFileImageSuccess) { rlm@1: NSModule mod = NSLinkModule(img, path, NSLINKMODULE_OPTION_PRIVATE | rlm@1: NSLINKMODULE_OPTION_RETURN_ON_ERROR); rlm@1: NSDestroyObjectFileImage(img); rlm@1: if (mod == NULL) pusherror(L); rlm@1: return mod; rlm@1: } rlm@1: lua_pushstring(L, errorfromcode(ret)); rlm@1: return NULL; rlm@1: } rlm@1: rlm@1: rlm@1: static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { rlm@1: NSSymbol nss = NSLookupSymbolInModule((NSModule)lib, sym); rlm@1: if (nss == NULL) { rlm@1: lua_pushfstring(L, "symbol " LUA_QS " not found", sym); rlm@1: return NULL; rlm@1: } rlm@1: return (lua_CFunction)NSAddressOfSymbol(nss); rlm@1: } rlm@1: rlm@1: /* }====================================================== */ rlm@1: rlm@1: rlm@1: rlm@1: #else rlm@1: /* rlm@1: ** {====================================================== rlm@1: ** Fallback for other systems rlm@1: ** ======================================================= rlm@1: */ rlm@1: rlm@1: #undef LIB_FAIL rlm@1: #define LIB_FAIL "absent" rlm@1: rlm@1: rlm@1: #define DLMSG "dynamic libraries not enabled; check your Lua installation" rlm@1: rlm@1: rlm@1: static void ll_unloadlib (void *lib) { rlm@1: (void)lib; /* to avoid warnings */ rlm@1: } rlm@1: rlm@1: rlm@1: static void *ll_load (lua_State *L, const char *path) { rlm@1: (void)path; /* to avoid warnings */ rlm@1: lua_pushliteral(L, DLMSG); rlm@1: return NULL; rlm@1: } rlm@1: rlm@1: rlm@1: static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { rlm@1: (void)lib; (void)sym; /* to avoid warnings */ rlm@1: lua_pushliteral(L, DLMSG); rlm@1: return NULL; rlm@1: } rlm@1: rlm@1: /* }====================================================== */ rlm@1: #endif rlm@1: rlm@1: rlm@1: rlm@1: static void **ll_register (lua_State *L, const char *path) { rlm@1: void **plib; rlm@1: lua_pushfstring(L, "%s%s", LIBPREFIX, path); rlm@1: lua_gettable(L, LUA_REGISTRYINDEX); /* check library in registry? */ rlm@1: if (!lua_isnil(L, -1)) /* is there an entry? */ rlm@1: plib = (void **)lua_touserdata(L, -1); rlm@1: else { /* no entry yet; create one */ rlm@1: lua_pop(L, 1); rlm@1: plib = (void **)lua_newuserdata(L, sizeof(const void *)); rlm@1: *plib = NULL; rlm@1: luaL_getmetatable(L, "_LOADLIB"); rlm@1: lua_setmetatable(L, -2); rlm@1: lua_pushfstring(L, "%s%s", LIBPREFIX, path); rlm@1: lua_pushvalue(L, -2); rlm@1: lua_settable(L, LUA_REGISTRYINDEX); rlm@1: } rlm@1: return plib; rlm@1: } rlm@1: rlm@1: rlm@1: /* rlm@1: ** __gc tag method: calls library's `ll_unloadlib' function with the lib rlm@1: ** handle rlm@1: */ rlm@1: static int gctm (lua_State *L) { rlm@1: void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB"); rlm@1: if (*lib) ll_unloadlib(*lib); rlm@1: *lib = NULL; /* mark library as closed */ rlm@1: return 0; rlm@1: } rlm@1: rlm@1: rlm@1: static int ll_loadfunc (lua_State *L, const char *path, const char *sym) { rlm@1: void **reg = ll_register(L, path); rlm@1: if (*reg == NULL) *reg = ll_load(L, path); rlm@1: if (*reg == NULL) rlm@1: return ERRLIB; /* unable to load library */ rlm@1: else { rlm@1: lua_CFunction f = ll_sym(L, *reg, sym); rlm@1: if (f == NULL) rlm@1: return ERRFUNC; /* unable to find function */ rlm@1: lua_pushcfunction(L, f); rlm@1: return 0; /* return function */ rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: static int ll_loadlib (lua_State *L) { rlm@1: const char *path = luaL_checkstring(L, 1); rlm@1: const char *init = luaL_checkstring(L, 2); rlm@1: int stat = ll_loadfunc(L, path, init); rlm@1: if (stat == 0) /* no errors? */ rlm@1: return 1; /* return the loaded function */ rlm@1: else { /* error; error message is on stack top */ rlm@1: lua_pushnil(L); rlm@1: lua_insert(L, -2); rlm@1: lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init"); rlm@1: return 3; /* return nil, error message, and where */ rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: rlm@1: /* rlm@1: ** {====================================================== rlm@1: ** 'require' function rlm@1: ** ======================================================= rlm@1: */ rlm@1: rlm@1: rlm@1: static int readable (const char *filename) { rlm@1: FILE *f = fopen(filename, "r"); /* try to open file */ rlm@1: if (f == NULL) return 0; /* open failed */ rlm@1: fclose(f); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: static const char *pushnexttemplate (lua_State *L, const char *path) { rlm@1: const char *l; rlm@1: while (*path == *LUA_PATHSEP) path++; /* skip separators */ rlm@1: if (*path == '\0') return NULL; /* no more templates */ rlm@1: l = strchr(path, *LUA_PATHSEP); /* find next separator */ rlm@1: if (l == NULL) l = path + strlen(path); rlm@1: lua_pushlstring(L, path, l - path); /* template */ rlm@1: return l; rlm@1: } rlm@1: rlm@1: rlm@1: static const char *findfile (lua_State *L, const char *name, rlm@1: const char *pname) { rlm@1: const char *path; rlm@1: name = luaL_gsub(L, name, ".", LUA_DIRSEP); rlm@1: lua_getfield(L, LUA_ENVIRONINDEX, pname); rlm@1: path = lua_tostring(L, -1); rlm@1: if (path == NULL) rlm@1: luaL_error(L, LUA_QL("package.%s") " must be a string", pname); rlm@1: lua_pushliteral(L, ""); /* error accumulator */ rlm@1: while ((path = pushnexttemplate(L, path)) != NULL) { rlm@1: const char *filename; rlm@1: filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); rlm@1: lua_remove(L, -2); /* remove path template */ rlm@1: if (readable(filename)) /* does file exist and is readable? */ rlm@1: return filename; /* return that file name */ rlm@1: lua_pushfstring(L, "\n\tno file " LUA_QS, filename); rlm@1: lua_remove(L, -2); /* remove file name */ rlm@1: lua_concat(L, 2); /* add entry to possible error message */ rlm@1: } rlm@1: return NULL; /* not found */ rlm@1: } rlm@1: rlm@1: rlm@1: static void loaderror (lua_State *L, const char *filename) { rlm@1: luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s", rlm@1: lua_tostring(L, 1), filename, lua_tostring(L, -1)); rlm@1: } rlm@1: rlm@1: rlm@1: static int loader_Lua (lua_State *L) { rlm@1: const char *filename; rlm@1: const char *name = luaL_checkstring(L, 1); rlm@1: filename = findfile(L, name, "path"); rlm@1: if (filename == NULL) return 1; /* library not found in this path */ rlm@1: if (luaL_loadfile(L, filename) != 0) rlm@1: loaderror(L, filename); rlm@1: return 1; /* library loaded successfully */ rlm@1: } rlm@1: rlm@1: rlm@1: static const char *mkfuncname (lua_State *L, const char *modname) { rlm@1: const char *funcname; rlm@1: const char *mark = strchr(modname, *LUA_IGMARK); rlm@1: if (mark) modname = mark + 1; rlm@1: funcname = luaL_gsub(L, modname, ".", LUA_OFSEP); rlm@1: funcname = lua_pushfstring(L, POF"%s", funcname); rlm@1: lua_remove(L, -2); /* remove 'gsub' result */ rlm@1: return funcname; rlm@1: } rlm@1: rlm@1: rlm@1: static int loader_C (lua_State *L) { rlm@1: const char *funcname; rlm@1: const char *name = luaL_checkstring(L, 1); rlm@1: const char *filename = findfile(L, name, "cpath"); rlm@1: if (filename == NULL) return 1; /* library not found in this path */ rlm@1: funcname = mkfuncname(L, name); rlm@1: if (ll_loadfunc(L, filename, funcname) != 0) rlm@1: loaderror(L, filename); rlm@1: return 1; /* library loaded successfully */ rlm@1: } rlm@1: rlm@1: rlm@1: static int loader_Croot (lua_State *L) { rlm@1: const char *funcname; rlm@1: const char *filename; rlm@1: const char *name = luaL_checkstring(L, 1); rlm@1: const char *p = strchr(name, '.'); rlm@1: int stat; rlm@1: if (p == NULL) return 0; /* is root */ rlm@1: lua_pushlstring(L, name, p - name); rlm@1: filename = findfile(L, lua_tostring(L, -1), "cpath"); rlm@1: if (filename == NULL) return 1; /* root not found */ rlm@1: funcname = mkfuncname(L, name); rlm@1: if ((stat = ll_loadfunc(L, filename, funcname)) != 0) { rlm@1: if (stat != ERRFUNC) loaderror(L, filename); /* real error */ rlm@33: lua_pushfstring(L, "\n\tno module " LUA_QS " in derp " LUA_QS, rlm@1: name, filename); rlm@1: return 1; /* function not found */ rlm@1: } rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: static int loader_preload (lua_State *L) { rlm@1: const char *name = luaL_checkstring(L, 1); rlm@1: lua_getfield(L, LUA_ENVIRONINDEX, "preload"); rlm@1: if (!lua_istable(L, -1)) rlm@1: luaL_error(L, LUA_QL("package.preload") " must be a table"); rlm@1: lua_getfield(L, -1, name); rlm@1: if (lua_isnil(L, -1)) /* not found? */ rlm@1: lua_pushfstring(L, "\n\tno field package.preload['%s']", name); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: static const int sentinel_ = 0; rlm@1: #define sentinel ((void *)&sentinel_) rlm@1: rlm@1: rlm@1: static int ll_require (lua_State *L) { rlm@1: const char *name = luaL_checkstring(L, 1); rlm@1: int i; rlm@1: lua_settop(L, 1); /* _LOADED table will be at index 2 */ rlm@1: lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); rlm@1: lua_getfield(L, 2, name); rlm@1: if (lua_toboolean(L, -1)) { /* is it there? */ rlm@1: if (lua_touserdata(L, -1) == sentinel) /* check loops */ rlm@1: luaL_error(L, "loop or previous error loading module " LUA_QS, name); rlm@1: return 1; /* package is already loaded */ rlm@1: } rlm@1: /* else must load it; iterate over available loaders */ rlm@1: lua_getfield(L, LUA_ENVIRONINDEX, "loaders"); rlm@1: if (!lua_istable(L, -1)) rlm@1: luaL_error(L, LUA_QL("package.loaders") " must be a table"); rlm@1: lua_pushliteral(L, ""); /* error message accumulator */ rlm@1: for (i=1; ; i++) { rlm@1: lua_rawgeti(L, -2, i); /* get a loader */ rlm@1: if (lua_isnil(L, -1)) rlm@1: luaL_error(L, "module " LUA_QS " not found:%s", rlm@1: name, lua_tostring(L, -2)); rlm@1: lua_pushstring(L, name); rlm@1: lua_call(L, 1, 1); /* call it */ rlm@1: if (lua_isfunction(L, -1)) /* did it find module? */ rlm@1: break; /* module loaded successfully */ rlm@1: else if (lua_isstring(L, -1)) /* loader returned error message? */ rlm@1: lua_concat(L, 2); /* accumulate it */ rlm@1: else rlm@1: lua_pop(L, 1); rlm@1: } rlm@1: lua_pushlightuserdata(L, sentinel); rlm@1: lua_setfield(L, 2, name); /* _LOADED[name] = sentinel */ rlm@1: lua_pushstring(L, name); /* pass name as argument to module */ rlm@1: lua_call(L, 1, 1); /* run loaded module */ rlm@1: if (!lua_isnil(L, -1)) /* non-nil return? */ rlm@1: lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ rlm@1: lua_getfield(L, 2, name); rlm@1: if (lua_touserdata(L, -1) == sentinel) { /* module did not set a value? */ rlm@1: lua_pushboolean(L, 1); /* use true as result */ rlm@1: lua_pushvalue(L, -1); /* extra copy to be returned */ rlm@1: lua_setfield(L, 2, name); /* _LOADED[name] = true */ rlm@1: } rlm@1: return 1; rlm@1: } rlm@1: rlm@1: /* }====================================================== */ rlm@1: rlm@1: rlm@1: rlm@1: /* rlm@1: ** {====================================================== rlm@1: ** 'module' function rlm@1: ** ======================================================= rlm@1: */ rlm@1: rlm@1: rlm@1: static void setfenv (lua_State *L) { rlm@1: lua_Debug ar; rlm@1: if (lua_getstack(L, 1, &ar) == 0 || rlm@1: lua_getinfo(L, "f", &ar) == 0 || /* get calling function */ rlm@1: lua_iscfunction(L, -1)) rlm@1: luaL_error(L, LUA_QL("module") " not called from a Lua function"); rlm@1: lua_pushvalue(L, -2); rlm@1: lua_setfenv(L, -2); rlm@1: lua_pop(L, 1); rlm@1: } rlm@1: rlm@1: rlm@1: static void dooptions (lua_State *L, int n) { rlm@1: int i; rlm@1: for (i = 2; i <= n; i++) { rlm@1: lua_pushvalue(L, i); /* get option (a function) */ rlm@1: lua_pushvalue(L, -2); /* module */ rlm@1: lua_call(L, 1, 0); rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: static void modinit (lua_State *L, const char *modname) { rlm@1: const char *dot; rlm@1: lua_pushvalue(L, -1); rlm@1: lua_setfield(L, -2, "_M"); /* module._M = module */ rlm@1: lua_pushstring(L, modname); rlm@1: lua_setfield(L, -2, "_NAME"); rlm@1: dot = strrchr(modname, '.'); /* look for last dot in module name */ rlm@1: if (dot == NULL) dot = modname; rlm@1: else dot++; rlm@1: /* set _PACKAGE as package name (full module name minus last part) */ rlm@1: lua_pushlstring(L, modname, dot - modname); rlm@1: lua_setfield(L, -2, "_PACKAGE"); rlm@1: } rlm@1: rlm@1: rlm@1: static int ll_module (lua_State *L) { rlm@1: const char *modname = luaL_checkstring(L, 1); rlm@1: int loaded = lua_gettop(L) + 1; /* index of _LOADED table */ rlm@1: lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); rlm@1: lua_getfield(L, loaded, modname); /* get _LOADED[modname] */ rlm@1: if (!lua_istable(L, -1)) { /* not found? */ rlm@1: lua_pop(L, 1); /* remove previous result */ rlm@1: /* try global variable (and create one if it does not exist) */ rlm@1: if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, 1) != NULL) rlm@1: return luaL_error(L, "name conflict for module " LUA_QS, modname); rlm@1: lua_pushvalue(L, -1); rlm@1: lua_setfield(L, loaded, modname); /* _LOADED[modname] = new table */ rlm@1: } rlm@1: /* check whether table already has a _NAME field */ rlm@1: lua_getfield(L, -1, "_NAME"); rlm@1: if (!lua_isnil(L, -1)) /* is table an initialized module? */ rlm@1: lua_pop(L, 1); rlm@1: else { /* no; initialize it */ rlm@1: lua_pop(L, 1); rlm@1: modinit(L, modname); rlm@1: } rlm@1: lua_pushvalue(L, -1); rlm@1: setfenv(L); rlm@1: dooptions(L, loaded - 1); rlm@1: return 0; rlm@1: } rlm@1: rlm@1: rlm@1: static int ll_seeall (lua_State *L) { rlm@1: luaL_checktype(L, 1, LUA_TTABLE); rlm@1: if (!lua_getmetatable(L, 1)) { rlm@1: lua_createtable(L, 0, 1); /* create new metatable */ rlm@1: lua_pushvalue(L, -1); rlm@1: lua_setmetatable(L, 1); rlm@1: } rlm@1: lua_pushvalue(L, LUA_GLOBALSINDEX); rlm@1: lua_setfield(L, -2, "__index"); /* mt.__index = _G */ rlm@1: return 0; rlm@1: } rlm@1: rlm@1: rlm@1: /* }====================================================== */ rlm@1: rlm@1: rlm@1: rlm@1: /* auxiliary mark (for internal use) */ rlm@1: #define AUXMARK "\1" rlm@1: rlm@1: static void setpath (lua_State *L, const char *fieldname, const char *envname, rlm@1: const char *def) { rlm@1: const char *path = getenv(envname); rlm@1: if (path == NULL) /* no environment variable? */ rlm@1: lua_pushstring(L, def); /* use default */ rlm@1: else { rlm@1: /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */ rlm@1: path = luaL_gsub(L, path, LUA_PATHSEP LUA_PATHSEP, rlm@1: LUA_PATHSEP AUXMARK LUA_PATHSEP); rlm@1: luaL_gsub(L, path, AUXMARK, def); rlm@1: lua_remove(L, -2); rlm@1: } rlm@1: setprogdir(L); rlm@1: lua_setfield(L, -2, fieldname); rlm@1: } rlm@1: rlm@1: rlm@1: static const luaL_Reg pk_funcs[] = { rlm@1: {"loadlib", ll_loadlib}, rlm@1: {"seeall", ll_seeall}, rlm@1: {NULL, NULL} rlm@1: }; rlm@1: rlm@1: rlm@1: static const luaL_Reg ll_funcs[] = { rlm@1: {"module", ll_module}, rlm@1: {"require", ll_require}, rlm@1: {NULL, NULL} rlm@1: }; rlm@1: rlm@1: rlm@1: static const lua_CFunction loaders[] = rlm@1: {loader_preload, loader_Lua, loader_C, loader_Croot, NULL}; rlm@1: rlm@1: rlm@1: LUALIB_API int luaopen_package (lua_State *L) { rlm@1: int i; rlm@1: /* create new type _LOADLIB */ rlm@1: luaL_newmetatable(L, "_LOADLIB"); rlm@1: lua_pushcfunction(L, gctm); rlm@1: lua_setfield(L, -2, "__gc"); rlm@1: /* create `package' table */ rlm@1: luaL_register(L, LUA_LOADLIBNAME, pk_funcs); rlm@1: #if defined(LUA_COMPAT_LOADLIB) rlm@1: lua_getfield(L, -1, "loadlib"); rlm@1: lua_setfield(L, LUA_GLOBALSINDEX, "loadlib"); rlm@1: #endif rlm@1: lua_pushvalue(L, -1); rlm@1: lua_replace(L, LUA_ENVIRONINDEX); rlm@1: /* create `loaders' table */ rlm@1: lua_createtable(L, 0, sizeof(loaders)/sizeof(loaders[0]) - 1); rlm@1: /* fill it with pre-defined loaders */ rlm@1: for (i=0; loaders[i] != NULL; i++) { rlm@1: lua_pushcfunction(L, loaders[i]); rlm@1: lua_rawseti(L, -2, i+1); rlm@1: } rlm@1: lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */ rlm@1: setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT); /* set field `path' */ rlm@1: setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT); /* set field `cpath' */ rlm@1: /* store config information */ rlm@1: lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n" rlm@1: LUA_EXECDIR "\n" LUA_IGMARK); rlm@1: lua_setfield(L, -2, "config"); rlm@1: /* set field `loaded' */ rlm@1: luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 2); rlm@1: lua_setfield(L, -2, "loaded"); rlm@1: /* set field `preload' */ rlm@1: lua_newtable(L); rlm@1: lua_setfield(L, -2, "preload"); rlm@1: lua_pushvalue(L, LUA_GLOBALSINDEX); rlm@1: luaL_register(L, NULL, ll_funcs); /* open lib into global table */ rlm@1: lua_pop(L, 1); rlm@1: return 1; /* return 'package' table */ rlm@1: } rlm@1: