31#if !defined (LUA_IGMARK)
42#if !defined(LUA_CSUBSEP)
43#define LUA_CSUBSEP LUA_DIRSEP
46#if !defined(LUA_LSUBSEP)
47#define LUA_LSUBSEP LUA_DIRSEP
52#define LUA_POF "luaopen_"
62static const char *
const CLIBS =
"_CLIBS";
64#define LIB_FAIL "open"
67#define setprogdir(L) ((void)0)
74typedef void (*voidf)(void);
84static void lsys_unloadlib (
void *lib);
92static void *lsys_load (
lua_State *L,
const char *path,
int seeglb);
99static lua_CFunction lsys_sym (
lua_State *L,
void *lib,
const char *sym);
104#if defined(LUA_USE_DLOPEN)
122#define cast_func(p) (__extension__ (lua_CFunction)(p))
124#define cast_func(p) ((lua_CFunction)(p))
128static void lsys_unloadlib (
void *lib) {
133static void *lsys_load (
lua_State *L,
const char *path,
int seeglb) {
134 void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL));
135 if (l_unlikely(lib == NULL))
136 lua_pushstring(L, dlerror());
141static lua_CFunction lsys_sym (
lua_State *L,
void *lib,
const char *sym) {
142 lua_CFunction f = cast_func(dlsym(lib, sym));
143 if (l_unlikely(f == NULL))
144 lua_pushstring(L, dlerror());
152#elif defined(LUA_DL_DLL)
165#if !defined(LUA_LLE_FLAGS)
166#define LUA_LLE_FLAGS 0
178 char buff[MAX_PATH + 1];
180 DWORD nsize =
sizeof(buff)/
sizeof(
char);
181 DWORD n = GetModuleFileNameA(NULL, buff, nsize);
182 if (n == 0 || n == nsize || (lb = strrchr(buff,
'\\')) == NULL)
183 luaL_error(L,
"unable to get ModuleFileName");
186 luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff);
195 int error = GetLastError();
197 if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
198 NULL, error, 0, buffer,
sizeof(buffer)/
sizeof(
char), NULL))
199 lua_pushstring(L, buffer);
201 lua_pushfstring(L,
"system error %d\n", error);
204static void lsys_unloadlib (
void *lib) {
205 FreeLibrary((HMODULE)lib);
209static void *lsys_load (
lua_State *L,
const char *path,
int seeglb) {
210 HMODULE lib = LoadLibraryExA(path, NULL, LUA_LLE_FLAGS);
212 if (lib == NULL) pusherror(L);
217static lua_CFunction lsys_sym (
lua_State *L,
void *lib,
const char *sym) {
218 lua_CFunction f = (lua_CFunction)(voidf)GetProcAddress((HMODULE)lib, sym);
219 if (f == NULL) pusherror(L);
234#define LIB_FAIL "absent"
237#define DLMSG "dynamic libraries not enabled; check your Lua installation"
240static void lsys_unloadlib (
void *lib) {
245static void *lsys_load (
lua_State *L,
const char *path,
int seeglb) {
246 (void)(path); (void)(seeglb);
247 lua_pushliteral(L, DLMSG);
252static lua_CFunction lsys_sym (
lua_State *L,
void *lib,
const char *sym) {
253 (void)(lib); (void)(sym);
254 lua_pushliteral(L, DLMSG);
272#if !defined(LUA_PATH_VAR)
273#define LUA_PATH_VAR "LUA_PATH"
276#if !defined(LUA_CPATH_VAR)
277#define LUA_CPATH_VAR "LUA_CPATH"
287 lua_getfield(L, LUA_REGISTRYINDEX,
"LUA_NOENV");
288 b = lua_toboolean(L, -1);
297static void setpath (
lua_State *L,
const char *fieldname,
301 const char *nver = lua_pushfstring(L,
"%s%s", envname, LUA_VERSUFFIX);
302 const char *path = getenv(nver);
304 path = getenv(envname);
305 if (path == NULL || noenv(L))
306 lua_pushstring(L, dft);
307 else if ((dftmark = strstr(path, LUA_PATH_SEP LUA_PATH_SEP)) == NULL)
308 lua_pushstring(L, path);
310 size_t len = strlen(path);
312 luaL_buffinit(L, &b);
313 if (path < dftmark) {
314 luaL_addlstring(&b, path, dftmark - path);
315 luaL_addchar(&b, *LUA_PATH_SEP);
317 luaL_addstring(&b, dft);
318 if (dftmark < path + len - 2) {
319 luaL_addchar(&b, *LUA_PATH_SEP);
320 luaL_addlstring(&b, dftmark + 2, (path + len - 2) - dftmark);
325 lua_setfield(L, -3, fieldname);
335static void *checkclib (
lua_State *L,
const char *path) {
337 lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
338 lua_getfield(L, -1, path);
339 plib = lua_touserdata(L, -1);
349static void addtoclib (
lua_State *L,
const char *path,
void *plib) {
350 lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
351 lua_pushlightuserdata(L, plib);
352 lua_pushvalue(L, -1);
353 lua_setfield(L, -3, path);
354 lua_rawseti(L, -2, luaL_len(L, -2) + 1);
364 lua_Integer n = luaL_len(L, 1);
365 for (; n >= 1; n--) {
366 lua_rawgeti(L, 1, n);
367 lsys_unloadlib(lua_touserdata(L, -1));
390static int lookforfunc (
lua_State *L,
const char *path,
const char *sym) {
391 void *reg = checkclib(L, path);
393 reg = lsys_load(L, path, *sym ==
'*');
394 if (reg == NULL)
return ERRLIB;
395 addtoclib(L, path, reg);
398 lua_pushboolean(L, 1);
402 lua_CFunction f = lsys_sym(L, reg, sym);
405 lua_pushcfunction(L, f);
412 const char *path = luaL_checkstring(L, 1);
413 const char *init = luaL_checkstring(L, 2);
414 int stat = lookforfunc(L, path, init);
415 if (l_likely(stat == 0))
420 lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL :
"init");
434static int readable (
const char *filename) {
435 FILE *f = fopen(filename,
"r");
436 if (f == NULL)
return 0;
447static const char *getnextfilename (
char **path,
char *end) {
452 else if (*name ==
'\0') {
453 *name = *LUA_PATH_SEP;
456 sep = strchr(name, *LUA_PATH_SEP);
471static void pusherrornotfound (
lua_State *L,
const char *path) {
473 luaL_buffinit(L, &b);
474 luaL_addstring(&b,
"no file '");
475 luaL_addgsub(&b, path, LUA_PATH_SEP,
"'\n\tno file '");
476 luaL_addstring(&b,
"'");
481static const char *searchpath (
lua_State *L,
const char *name,
484 const char *dirsep) {
488 const char *filename;
490 if (*sep !=
'\0' && strchr(name, *sep) != NULL)
491 name = luaL_gsub(L, name, sep, dirsep);
492 luaL_buffinit(L, &buff);
494 luaL_addgsub(&buff, path, LUA_PATH_MARK, name);
495 luaL_addchar(&buff,
'\0');
496 pathname = luaL_buffaddr(&buff);
497 endpathname = pathname + luaL_bufflen(&buff) - 1;
498 while ((filename = getnextfilename(&pathname, endpathname)) != NULL) {
499 if (readable(filename))
500 return lua_pushstring(L, filename);
502 luaL_pushresult(&buff);
503 pusherrornotfound(L, lua_tostring(L, -1));
509 const char *f = searchpath(L, luaL_checkstring(L, 1),
510 luaL_checkstring(L, 2),
511 luaL_optstring(L, 3,
"."),
512 luaL_optstring(L, 4, LUA_DIRSEP));
513 if (f != NULL)
return 1;
522static const char *findfile (
lua_State *L,
const char *name,
524 const char *dirsep) {
526 lua_getfield(L, lua_upvalueindex(1), pname);
527 path = lua_tostring(L, -1);
528 if (l_unlikely(path == NULL))
529 luaL_error(L,
"'package.%s' must be a string", pname);
530 return searchpath(L, name, path,
".", dirsep);
534static int checkload (
lua_State *L,
int stat,
const char *filename) {
535 if (l_likely(stat)) {
536 lua_pushstring(L, filename);
540 return luaL_error(L,
"error loading module '%s' from file '%s':\n\t%s",
541 lua_tostring(L, 1), filename, lua_tostring(L, -1));
546 const char *filename;
547 const char *name = luaL_checkstring(L, 1);
548 filename = findfile(L, name,
"path", LUA_LSUBSEP);
549 if (filename == NULL)
return 1;
550 return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename);
562static int loadfunc (
lua_State *L,
const char *filename,
const char *modname) {
563 const char *openfunc;
565 modname = luaL_gsub(L, modname,
".", LUA_OFSEP);
566 mark = strchr(modname, *LUA_IGMARK);
569 openfunc = lua_pushlstring(L, modname, mark - modname);
570 openfunc = lua_pushfstring(L, LUA_POF
"%s", openfunc);
571 stat = lookforfunc(L, filename, openfunc);
572 if (stat != ERRFUNC)
return stat;
575 openfunc = lua_pushfstring(L, LUA_POF
"%s", modname);
576 return lookforfunc(L, filename, openfunc);
581 const char *name = luaL_checkstring(L, 1);
582 const char *filename = findfile(L, name,
"cpath", LUA_CSUBSEP);
583 if (filename == NULL)
return 1;
584 return checkload(L, (loadfunc(L, filename, name) == 0), filename);
588static int searcher_Croot (
lua_State *L) {
589 const char *filename;
590 const char *name = luaL_checkstring(L, 1);
591 const char *p = strchr(name,
'.');
593 if (p == NULL)
return 0;
594 lua_pushlstring(L, name, p - name);
595 filename = findfile(L, lua_tostring(L, -1),
"cpath", LUA_CSUBSEP);
596 if (filename == NULL)
return 1;
597 if ((stat = loadfunc(L, filename, name)) != 0) {
599 return checkload(L, 0, filename);
601 lua_pushfstring(L,
"no module '%s' in file '%s'", name, filename);
605 lua_pushstring(L, filename);
610static int searcher_preload (
lua_State *L) {
611 const char *name = luaL_checkstring(L, 1);
612 lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
613 if (lua_getfield(L, -1, name) == LUA_TNIL) {
614 lua_pushfstring(L,
"no field package.preload['%s']", name);
618 lua_pushliteral(L,
":preload:");
624static void findloader (
lua_State *L,
const char *name) {
628 if (l_unlikely(lua_getfield(L, lua_upvalueindex(1),
"searchers")
630 luaL_error(L,
"'package.searchers' must be a table");
631 luaL_buffinit(L, &msg);
634 luaL_addstring(&msg,
"\n\t");
635 if (l_unlikely(lua_rawgeti(L, 3, i) == LUA_TNIL)) {
637 luaL_buffsub(&msg, 2);
638 luaL_pushresult(&msg);
639 luaL_error(L,
"module '%s' not found:%s", name, lua_tostring(L, -1));
641 lua_pushstring(L, name);
643 if (lua_isfunction(L, -2))
645 else if (lua_isstring(L, -2)) {
651 luaL_buffsub(&msg, 2);
658 const char *name = luaL_checkstring(L, 1);
660 lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
661 lua_getfield(L, 2, name);
662 if (lua_toboolean(L, -1))
667 lua_rotate(L, -2, 1);
669 lua_pushvalue(L, -3);
673 if (!lua_isnil(L, -1))
674 lua_setfield(L, 2, name);
677 if (lua_getfield(L, 2, name) == LUA_TNIL) {
678 lua_pushboolean(L, 1);
680 lua_setfield(L, 2, name);
682 lua_rotate(L, -2, 1);
692 {
"loadlib", ll_loadlib},
693 {
"searchpath", ll_searchpath},
705 {
"require", ll_require},
710static void createsearcherstable (
lua_State *L) {
711 static const lua_CFunction searchers[] = {
720 lua_createtable(L,
sizeof(searchers)/
sizeof(searchers[0]) - 1, 0);
722 for (i=0; searchers[i] != NULL; i++) {
723 lua_pushvalue(L, -2);
724 lua_pushcclosure(L, searchers[i], 1);
725 lua_rawseti(L, -2, i+1);
727 lua_setfield(L, -2,
"searchers");
735static void createclibstable (
lua_State *L) {
736 luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS);
737 lua_createtable(L, 0, 1);
738 lua_pushcfunction(L, gctm);
739 lua_setfield(L, -2,
"__gc");
740 lua_setmetatable(L, -2);
744LUAMOD_API
int luaopen_package (
lua_State *L) {
746 luaL_newlib(L, pk_funcs);
747 createsearcherstable(L);
749 setpath(L,
"path", LUA_PATH_VAR, LUA_PATH_DEFAULT);
750 setpath(L,
"cpath", LUA_CPATH_VAR, LUA_CPATH_DEFAULT);
752 lua_pushliteral(L, LUA_DIRSEP
"\n" LUA_PATH_SEP
"\n" LUA_PATH_MARK
"\n"
753 LUA_EXEC_DIR
"\n" LUA_IGMARK
"\n");
754 lua_setfield(L, -2,
"config");
756 luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
757 lua_setfield(L, -2,
"loaded");
759 luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
760 lua_setfield(L, -2,
"preload");
761 lua_pushglobaltable(L);
762 lua_pushvalue(L, -2);
763 luaL_setfuncs(L, ll_funcs, 1);