2 ** $Id: loadlib.c,v 1.130 2017/01/12 17:14:26 roberto Exp $
3 ** Dynamic library loader for Lua
4 ** See Copyright Notice in lua.h
6 ** This module contains an implementation of loadlib for Unix systems
7 ** that have dlfcn, an implementation for Windows, and a stub for other
28 ** LUA_IGMARK is a mark to ignore all before it when building the
29 ** luaopen_ function name.
31 #if !defined (LUA_IGMARK)
32 #define LUA_IGMARK "-"
37 ** LUA_CSUBSEP is the character that replaces dots in submodule names
38 ** when searching for a C loader.
39 ** LUA_LSUBSEP is the character that replaces dots in submodule names
40 ** when searching for a Lua loader.
42 #if !defined(LUA_CSUBSEP)
43 #define LUA_CSUBSEP LUA_DIRSEP
46 #if !defined(LUA_LSUBSEP)
47 #define LUA_LSUBSEP LUA_DIRSEP
51 /* prefix for open functions in C libraries */
52 #define LUA_POF "luaopen_"
54 /* separator for open functions in C libraries */
59 ** unique key for table in the registry that keeps handles
60 ** for all loaded C libraries
62 static const int CLIBS
= 0;
64 #define LIB_FAIL "open"
67 #define setprogdir(L) ((void)0)
71 ** system-dependent functions
75 ** unload library 'lib'
77 static void lsys_unloadlib (void *lib
);
80 ** load C library in file 'path'. If 'seeglb', load with all names in
81 ** the library global.
82 ** Returns the library; in case of error, returns NULL plus an
83 ** error string in the stack.
85 static void *lsys_load (lua_State
*L
, const char *path
, int seeglb
);
88 ** Try to find a function named 'sym' in library 'lib'.
89 ** Returns the function; in case of error, returns NULL plus an
90 ** error string in the stack.
92 static lua_CFunction
lsys_sym (lua_State
*L
, void *lib
, const char *sym
);
97 #if defined(LUA_USE_DLOPEN) /* { */
99 ** {========================================================================
100 ** This is an implementation of loadlib based on the dlfcn interface.
101 ** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,
102 ** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least
103 ** as an emulation layer on top of native functions.
104 ** =========================================================================
110 ** Macro to convert pointer-to-void* to pointer-to-function. This cast
111 ** is undefined according to ISO C, but POSIX assumes that it works.
112 ** (The '__extension__' in gnu compilers is only to avoid warnings.)
114 #if defined(__GNUC__)
115 #define cast_func(p) (__extension__ (lua_CFunction)(p))
117 #define cast_func(p) ((lua_CFunction)(p))
121 static void lsys_unloadlib (void *lib
) {
126 static void *lsys_load (lua_State
*L
, const char *path
, int seeglb
) {
127 void *lib
= dlopen(path
, RTLD_NOW
| (seeglb
? RTLD_GLOBAL
: RTLD_LOCAL
));
128 if (lib
== NULL
) lua_pushstring(L
, dlerror());
133 static lua_CFunction
lsys_sym (lua_State
*L
, void *lib
, const char *sym
) {
134 lua_CFunction f
= cast_func(dlsym(lib
, sym
));
135 if (f
== NULL
) lua_pushstring(L
, dlerror());
139 /* }====================================================== */
143 #elif defined(LUA_DL_DLL) /* }{ */
145 ** {======================================================================
146 ** This is an implementation of loadlib for Windows using native functions.
147 ** =======================================================================
154 ** optional flags for LoadLibraryEx
156 #if !defined(LUA_LLE_FLAGS)
157 #define LUA_LLE_FLAGS 0
165 ** Replace in the path (on the top of the stack) any occurrence
166 ** of LUA_EXEC_DIR with the executable's path.
168 static void setprogdir (lua_State
*L
) {
169 char buff
[MAX_PATH
+ 1];
171 DWORD nsize
= sizeof(buff
)/sizeof(char);
172 DWORD n
= GetModuleFileNameA(NULL
, buff
, nsize
); /* get exec. name */
173 if (n
== 0 || n
== nsize
|| (lb
= strrchr(buff
, '\\')) == NULL
)
174 luaL_error(L
, "unable to get ModuleFileName");
176 *lb
= '\0'; /* cut name on the last '\\' to get the path */
177 luaL_gsub(L
, lua_tostring(L
, -1), LUA_EXEC_DIR
, buff
);
178 lua_remove(L
, -2); /* remove original string */
185 static void pusherror (lua_State
*L
) {
186 int error
= GetLastError();
188 if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS
| FORMAT_MESSAGE_FROM_SYSTEM
,
189 NULL
, error
, 0, buffer
, sizeof(buffer
)/sizeof(char), NULL
))
190 lua_pushstring(L
, buffer
);
192 lua_pushfstring(L
, "system error %d\n", error
);
195 static void lsys_unloadlib (void *lib
) {
196 FreeLibrary((HMODULE
)lib
);
200 static void *lsys_load (lua_State
*L
, const char *path
, int seeglb
) {
201 HMODULE lib
= LoadLibraryExA(path
, NULL
, LUA_LLE_FLAGS
);
202 (void)(seeglb
); /* not used: symbols are 'global' by default */
203 if (lib
== NULL
) pusherror(L
);
208 static lua_CFunction
lsys_sym (lua_State
*L
, void *lib
, const char *sym
) {
209 lua_CFunction f
= (lua_CFunction
)GetProcAddress((HMODULE
)lib
, sym
);
210 if (f
== NULL
) pusherror(L
);
214 /* }====================================================== */
219 ** {======================================================
220 ** Fallback for other systems
221 ** =======================================================
225 #define LIB_FAIL "absent"
228 #define DLMSG "dynamic libraries not enabled; check your Lua installation"
231 static void lsys_unloadlib (void *lib
) {
232 (void)(lib
); /* not used */
236 static void *lsys_load (lua_State
*L
, const char *path
, int seeglb
) {
237 (void)(path
); (void)(seeglb
); /* not used */
238 lua_pushliteral(L
, DLMSG
);
243 static lua_CFunction
lsys_sym (lua_State
*L
, void *lib
, const char *sym
) {
244 (void)(lib
); (void)(sym
); /* not used */
245 lua_pushliteral(L
, DLMSG
);
249 /* }====================================================== */
254 ** {==================================================================
256 ** ===================================================================
260 ** LUA_PATH_VAR and LUA_CPATH_VAR are the names of the environment
261 ** variables that Lua check to set its paths.
263 #if !defined(LUA_PATH_VAR)
264 #define LUA_PATH_VAR "LUA_PATH"
267 #if !defined(LUA_CPATH_VAR)
268 #define LUA_CPATH_VAR "LUA_CPATH"
272 #define AUXMARK "\1" /* auxiliary mark */
276 ** return registry.LUA_NOENV as a boolean
278 static int noenv (lua_State
*L
) {
280 lua_getfield(L
, LUA_REGISTRYINDEX
, "LUA_NOENV");
281 b
= lua_toboolean(L
, -1);
282 lua_pop(L
, 1); /* remove value */
290 static void setpath (lua_State
*L
, const char *fieldname
,
293 const char *nver
= lua_pushfstring(L
, "%s%s", envname
, LUA_VERSUFFIX
);
294 const char *path
= getenv(nver
); /* use versioned name */
295 if (path
== NULL
) /* no environment variable? */
296 path
= getenv(envname
); /* try unversioned name */
297 if (path
== NULL
|| noenv(L
)) /* no environment variable? */
298 lua_pushstring(L
, dft
); /* use default */
300 /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */
301 path
= luaL_gsub(L
, path
, LUA_PATH_SEP LUA_PATH_SEP
,
302 LUA_PATH_SEP AUXMARK LUA_PATH_SEP
);
303 luaL_gsub(L
, path
, AUXMARK
, dft
);
304 lua_remove(L
, -2); /* remove result from 1st 'gsub' */
307 lua_setfield(L
, -3, fieldname
); /* package[fieldname] = path value */
308 lua_pop(L
, 1); /* pop versioned variable name */
311 /* }================================================================== */
315 ** return registry.CLIBS[path]
317 static void *checkclib (lua_State
*L
, const char *path
) {
319 lua_rawgetp(L
, LUA_REGISTRYINDEX
, &CLIBS
);
320 lua_getfield(L
, -1, path
);
321 plib
= lua_touserdata(L
, -1); /* plib = CLIBS[path] */
322 lua_pop(L
, 2); /* pop CLIBS table and 'plib' */
328 ** registry.CLIBS[path] = plib -- for queries
329 ** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries
331 static void addtoclib (lua_State
*L
, const char *path
, void *plib
) {
332 lua_rawgetp(L
, LUA_REGISTRYINDEX
, &CLIBS
);
333 lua_pushlightuserdata(L
, plib
);
334 lua_pushvalue(L
, -1);
335 lua_setfield(L
, -3, path
); /* CLIBS[path] = plib */
336 lua_rawseti(L
, -2, luaL_len(L
, -2) + 1); /* CLIBS[#CLIBS + 1] = plib */
337 lua_pop(L
, 1); /* pop CLIBS table */
342 ** __gc tag method for CLIBS table: calls 'lsys_unloadlib' for all lib
343 ** handles in list CLIBS
345 static int gctm (lua_State
*L
) {
346 lua_Integer n
= luaL_len(L
, 1);
347 for (; n
>= 1; n
--) { /* for each handle, in reverse order */
348 lua_rawgeti(L
, 1, n
); /* get handle CLIBS[n] */
349 lsys_unloadlib(lua_touserdata(L
, -1));
350 lua_pop(L
, 1); /* pop handle */
357 /* error codes for 'lookforfunc' */
362 ** Look for a C function named 'sym' in a dynamically loaded library
364 ** First, check whether the library is already loaded; if not, try
366 ** Then, if 'sym' is '*', return true (as library has been loaded).
367 ** Otherwise, look for symbol 'sym' in the library and push a
368 ** C function with that symbol.
369 ** Return 0 and 'true' or a function in the stack; in case of
370 ** errors, return an error code and an error message in the stack.
372 static int lookforfunc (lua_State
*L
, const char *path
, const char *sym
) {
373 void *reg
= checkclib(L
, path
); /* check loaded C libraries */
374 if (reg
== NULL
) { /* must load library? */
375 reg
= lsys_load(L
, path
, *sym
== '*'); /* global symbols if 'sym'=='*' */
376 if (reg
== NULL
) return ERRLIB
; /* unable to load library */
377 addtoclib(L
, path
, reg
);
379 if (*sym
== '*') { /* loading only library (no function)? */
380 lua_pushboolean(L
, 1); /* return 'true' */
381 return 0; /* no errors */
384 lua_CFunction f
= lsys_sym(L
, reg
, sym
);
386 return ERRFUNC
; /* unable to find function */
387 lua_pushcfunction(L
, f
); /* else create new function */
388 return 0; /* no errors */
393 static int ll_loadlib (lua_State
*L
) {
394 const char *path
= luaL_checkstring(L
, 1);
395 const char *init
= luaL_checkstring(L
, 2);
396 int stat
= lookforfunc(L
, path
, init
);
397 if (stat
== 0) /* no errors? */
398 return 1; /* return the loaded function */
399 else { /* error; error message is on stack top */
402 lua_pushstring(L
, (stat
== ERRLIB
) ? LIB_FAIL
: "init");
403 return 3; /* return nil, error message, and where */
410 ** {======================================================
411 ** 'require' function
412 ** =======================================================
416 static int readable (const char *filename
) {
417 FILE *f
= fopen(filename
, "r"); /* try to open file */
418 if (f
== NULL
) return 0; /* open failed */
424 static const char *pushnexttemplate (lua_State
*L
, const char *path
) {
426 while (*path
== *LUA_PATH_SEP
) path
++; /* skip separators */
427 if (*path
== '\0') return NULL
; /* no more templates */
428 l
= strchr(path
, *LUA_PATH_SEP
); /* find next separator */
429 if (l
== NULL
) l
= path
+ strlen(path
);
430 lua_pushlstring(L
, path
, l
- path
); /* template */
435 static const char *searchpath (lua_State
*L
, const char *name
,
438 const char *dirsep
) {
439 luaL_Buffer msg
; /* to build error message */
440 luaL_buffinit(L
, &msg
);
441 if (*sep
!= '\0') /* non-empty separator? */
442 name
= luaL_gsub(L
, name
, sep
, dirsep
); /* replace it by 'dirsep' */
443 while ((path
= pushnexttemplate(L
, path
)) != NULL
) {
444 const char *filename
= luaL_gsub(L
, lua_tostring(L
, -1),
445 LUA_PATH_MARK
, name
);
446 lua_remove(L
, -2); /* remove path template */
447 if (readable(filename
)) /* does file exist and is readable? */
448 return filename
; /* return that file name */
449 lua_pushfstring(L
, "\n\tno file '%s'", filename
);
450 lua_remove(L
, -2); /* remove file name */
451 luaL_addvalue(&msg
); /* concatenate error msg. entry */
453 luaL_pushresult(&msg
); /* create error message */
454 return NULL
; /* not found */
458 static int ll_searchpath (lua_State
*L
) {
459 const char *f
= searchpath(L
, luaL_checkstring(L
, 1),
460 luaL_checkstring(L
, 2),
461 luaL_optstring(L
, 3, "."),
462 luaL_optstring(L
, 4, LUA_DIRSEP
));
463 if (f
!= NULL
) return 1;
464 else { /* error message is on top of the stack */
467 return 2; /* return nil + error message */
472 static const char *findfile (lua_State
*L
, const char *name
,
474 const char *dirsep
) {
476 lua_getfield(L
, lua_upvalueindex(1), pname
);
477 path
= lua_tostring(L
, -1);
479 luaL_error(L
, "'package.%s' must be a string", pname
);
480 return searchpath(L
, name
, path
, ".", dirsep
);
484 static int checkload (lua_State
*L
, int stat
, const char *filename
) {
485 if (stat
) { /* module loaded successfully? */
486 lua_pushstring(L
, filename
); /* will be 2nd argument to module */
487 return 2; /* return open function and file name */
490 return luaL_error(L
, "error loading module '%s' from file '%s':\n\t%s",
491 lua_tostring(L
, 1), filename
, lua_tostring(L
, -1));
495 static int searcher_Lua (lua_State
*L
) {
496 const char *filename
;
497 const char *name
= luaL_checkstring(L
, 1);
498 filename
= findfile(L
, name
, "path", LUA_LSUBSEP
);
499 if (filename
== NULL
) return 1; /* module not found in this path */
500 return checkload(L
, (luaL_loadfile(L
, filename
) == LUA_OK
), filename
);
505 ** Try to find a load function for module 'modname' at file 'filename'.
506 ** First, change '.' to '_' in 'modname'; then, if 'modname' has
507 ** the form X-Y (that is, it has an "ignore mark"), build a function
508 ** name "luaopen_X" and look for it. (For compatibility, if that
509 ** fails, it also tries "luaopen_Y".) If there is no ignore mark,
510 ** look for a function named "luaopen_modname".
512 static int loadfunc (lua_State
*L
, const char *filename
, const char *modname
) {
513 const char *openfunc
;
515 modname
= luaL_gsub(L
, modname
, ".", LUA_OFSEP
);
516 mark
= strchr(modname
, *LUA_IGMARK
);
519 openfunc
= lua_pushlstring(L
, modname
, mark
- modname
);
520 openfunc
= lua_pushfstring(L
, LUA_POF
"%s", openfunc
);
521 stat
= lookforfunc(L
, filename
, openfunc
);
522 if (stat
!= ERRFUNC
) return stat
;
523 modname
= mark
+ 1; /* else go ahead and try old-style name */
525 openfunc
= lua_pushfstring(L
, LUA_POF
"%s", modname
);
526 return lookforfunc(L
, filename
, openfunc
);
530 static int searcher_C (lua_State
*L
) {
531 const char *name
= luaL_checkstring(L
, 1);
532 const char *filename
= findfile(L
, name
, "cpath", LUA_CSUBSEP
);
533 if (filename
== NULL
) return 1; /* module not found in this path */
534 return checkload(L
, (loadfunc(L
, filename
, name
) == 0), filename
);
538 static int searcher_Croot (lua_State
*L
) {
539 const char *filename
;
540 const char *name
= luaL_checkstring(L
, 1);
541 const char *p
= strchr(name
, '.');
543 if (p
== NULL
) return 0; /* is root */
544 lua_pushlstring(L
, name
, p
- name
);
545 filename
= findfile(L
, lua_tostring(L
, -1), "cpath", LUA_CSUBSEP
);
546 if (filename
== NULL
) return 1; /* root not found */
547 if ((stat
= loadfunc(L
, filename
, name
)) != 0) {
549 return checkload(L
, 0, filename
); /* real error */
550 else { /* open function not found */
551 lua_pushfstring(L
, "\n\tno module '%s' in file '%s'", name
, filename
);
555 lua_pushstring(L
, filename
); /* will be 2nd argument to module */
560 static int searcher_preload (lua_State
*L
) {
561 const char *name
= luaL_checkstring(L
, 1);
562 lua_getfield(L
, LUA_REGISTRYINDEX
, LUA_PRELOAD_TABLE
);
563 if (lua_getfield(L
, -1, name
) == LUA_TNIL
) /* not found? */
564 lua_pushfstring(L
, "\n\tno field package.preload['%s']", name
);
569 static void findloader (lua_State
*L
, const char *name
) {
571 luaL_Buffer msg
; /* to build error message */
572 luaL_buffinit(L
, &msg
);
573 /* push 'package.searchers' to index 3 in the stack */
574 if (lua_getfield(L
, lua_upvalueindex(1), "searchers") != LUA_TTABLE
)
575 luaL_error(L
, "'package.searchers' must be a table");
576 /* iterate over available searchers to find a loader */
578 if (lua_rawgeti(L
, 3, i
) == LUA_TNIL
) { /* no more searchers? */
579 lua_pop(L
, 1); /* remove nil */
580 luaL_pushresult(&msg
); /* create error message */
581 luaL_error(L
, "module '%s' not found:%s", name
, lua_tostring(L
, -1));
583 lua_pushstring(L
, name
);
584 lua_call(L
, 1, 2); /* call it */
585 if (lua_isfunction(L
, -2)) /* did it find a loader? */
586 return; /* module loader found */
587 else if (lua_isstring(L
, -2)) { /* searcher returned error message? */
588 lua_pop(L
, 1); /* remove extra return */
589 luaL_addvalue(&msg
); /* concatenate error message */
592 lua_pop(L
, 2); /* remove both returns */
597 static int ll_require (lua_State
*L
) {
598 const char *name
= luaL_checkstring(L
, 1);
599 lua_settop(L
, 1); /* LOADED table will be at index 2 */
600 lua_getfield(L
, LUA_REGISTRYINDEX
, LUA_LOADED_TABLE
);
601 lua_getfield(L
, 2, name
); /* LOADED[name] */
602 if (lua_toboolean(L
, -1)) /* is it there? */
603 return 1; /* package is already loaded */
604 /* else must load package */
605 lua_pop(L
, 1); /* remove 'getfield' result */
607 lua_pushstring(L
, name
); /* pass name as argument to module loader */
608 lua_insert(L
, -2); /* name is 1st argument (before search data) */
609 lua_call(L
, 2, 1); /* run loader to load module */
610 if (!lua_isnil(L
, -1)) /* non-nil return? */
611 lua_setfield(L
, 2, name
); /* LOADED[name] = returned value */
612 if (lua_getfield(L
, 2, name
) == LUA_TNIL
) { /* module set no value? */
613 lua_pushboolean(L
, 1); /* use true as result */
614 lua_pushvalue(L
, -1); /* extra copy to be returned */
615 lua_setfield(L
, 2, name
); /* LOADED[name] = true */
620 /* }====================================================== */
625 ** {======================================================
627 ** =======================================================
629 #if defined(LUA_COMPAT_MODULE)
632 ** changes the environment variable of calling function
634 static void set_env (lua_State
*L
) {
636 if (lua_getstack(L
, 1, &ar
) == 0 ||
637 lua_getinfo(L
, "f", &ar
) == 0 || /* get calling function */
638 lua_iscfunction(L
, -1))
639 luaL_error(L
, "'module' not called from a Lua function");
640 lua_pushvalue(L
, -2); /* copy new environment table to top */
641 lua_setupvalue(L
, -2, 1);
642 lua_pop(L
, 1); /* remove function */
646 static void dooptions (lua_State
*L
, int n
) {
648 for (i
= 2; i
<= n
; i
++) {
649 if (lua_isfunction(L
, i
)) { /* avoid 'calling' extra info. */
650 lua_pushvalue(L
, i
); /* get option (a function) */
651 lua_pushvalue(L
, -2); /* module */
658 static void modinit (lua_State
*L
, const char *modname
) {
660 lua_pushvalue(L
, -1);
661 lua_setfield(L
, -2, "_M"); /* module._M = module */
662 lua_pushstring(L
, modname
);
663 lua_setfield(L
, -2, "_NAME");
664 dot
= strrchr(modname
, '.'); /* look for last dot in module name */
665 if (dot
== NULL
) dot
= modname
;
667 /* set _PACKAGE as package name (full module name minus last part) */
668 lua_pushlstring(L
, modname
, dot
- modname
);
669 lua_setfield(L
, -2, "_PACKAGE");
673 static int ll_module (lua_State
*L
) {
674 const char *modname
= luaL_checkstring(L
, 1);
675 int lastarg
= lua_gettop(L
); /* last parameter */
676 luaL_pushmodule(L
, modname
, 1); /* get/create module table */
677 /* check whether table already has a _NAME field */
678 if (lua_getfield(L
, -1, "_NAME") != LUA_TNIL
)
679 lua_pop(L
, 1); /* table is an initialized module */
680 else { /* no; initialize it */
684 lua_pushvalue(L
, -1);
686 dooptions(L
, lastarg
);
691 static int ll_seeall (lua_State
*L
) {
692 luaL_checktype(L
, 1, LUA_TTABLE
);
693 if (!lua_getmetatable(L
, 1)) {
694 lua_createtable(L
, 0, 1); /* create new metatable */
695 lua_pushvalue(L
, -1);
696 lua_setmetatable(L
, 1);
698 lua_pushglobaltable(L
);
699 lua_setfield(L
, -2, "__index"); /* mt.__index = _G */
704 /* }====================================================== */
708 static const luaL_Reg pk_funcs
[] = {
709 {"loadlib", ll_loadlib
},
710 {"searchpath", ll_searchpath
},
711 #if defined(LUA_COMPAT_MODULE)
712 {"seeall", ll_seeall
},
724 static const luaL_Reg ll_funcs
[] = {
725 #if defined(LUA_COMPAT_MODULE)
726 {"module", ll_module
},
728 {"require", ll_require
},
733 static void createsearcherstable (lua_State
*L
) {
734 static const lua_CFunction searchers
[] =
735 {searcher_preload
, searcher_Lua
, searcher_C
, searcher_Croot
, NULL
};
737 /* create 'searchers' table */
738 lua_createtable(L
, sizeof(searchers
)/sizeof(searchers
[0]) - 1, 0);
739 /* fill it with predefined searchers */
740 for (i
=0; searchers
[i
] != NULL
; i
++) {
741 lua_pushvalue(L
, -2); /* set 'package' as upvalue for all searchers */
742 lua_pushcclosure(L
, searchers
[i
], 1);
743 lua_rawseti(L
, -2, i
+1);
745 #if defined(LUA_COMPAT_LOADERS)
746 lua_pushvalue(L
, -1); /* make a copy of 'searchers' table */
747 lua_setfield(L
, -3, "loaders"); /* put it in field 'loaders' */
749 lua_setfield(L
, -2, "searchers"); /* put it in field 'searchers' */
754 ** create table CLIBS to keep track of loaded C libraries,
755 ** setting a finalizer to close all libraries when closing state.
757 static void createclibstable (lua_State
*L
) {
758 lua_newtable(L
); /* create CLIBS table */
759 lua_createtable(L
, 0, 1); /* create metatable for CLIBS */
760 lua_pushcfunction(L
, gctm
);
761 lua_setfield(L
, -2, "__gc"); /* set finalizer for CLIBS table */
762 lua_setmetatable(L
, -2);
763 lua_rawsetp(L
, LUA_REGISTRYINDEX
, &CLIBS
); /* set CLIBS table in registry */
767 LUAMOD_API
int luaopen_package (lua_State
*L
) {
769 luaL_newlib(L
, pk_funcs
); /* create 'package' table */
770 createsearcherstable(L
);
772 setpath(L
, "path", LUA_PATH_VAR
, LUA_PATH_DEFAULT
);
773 setpath(L
, "cpath", LUA_CPATH_VAR
, LUA_CPATH_DEFAULT
);
774 /* store config information */
775 lua_pushliteral(L
, LUA_DIRSEP
"\n" LUA_PATH_SEP
"\n" LUA_PATH_MARK
"\n"
776 LUA_EXEC_DIR
"\n" LUA_IGMARK
"\n");
777 lua_setfield(L
, -2, "config");
778 /* set field 'loaded' */
779 luaL_getsubtable(L
, LUA_REGISTRYINDEX
, LUA_LOADED_TABLE
);
780 lua_setfield(L
, -2, "loaded");
781 /* set field 'preload' */
782 luaL_getsubtable(L
, LUA_REGISTRYINDEX
, LUA_PRELOAD_TABLE
);
783 lua_setfield(L
, -2, "preload");
784 lua_pushglobaltable(L
);
785 lua_pushvalue(L
, -2); /* set 'package' as upvalue for next lib */
786 luaL_setfuncs(L
, ll_funcs
, 1); /* open lib into global table */
787 lua_pop(L
, 1); /* pop global table */
788 return 1; /* return 'package' table */