xref: /freebsd/contrib/lua/src/loadlib.c (revision 19261079b74319502c6ffa1249920079f0f69a72)
1 /*
2 ** $Id: loadlib.c $
3 ** Dynamic library loader for Lua
4 ** See Copyright Notice in lua.h
5 **
6 ** This module contains an implementation of loadlib for Unix systems
7 ** that have dlfcn, an implementation for Windows, and a stub for other
8 ** systems.
9 */
10 
11 #define loadlib_c
12 #define LUA_LIB
13 
14 #include "lprefix.h"
15 
16 
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 
21 #include "lua.h"
22 
23 #include "lauxlib.h"
24 #include "lualib.h"
25 
26 
27 /*
28 ** LUA_IGMARK is a mark to ignore all before it when building the
29 ** luaopen_ function name.
30 */
31 #if !defined (LUA_IGMARK)
32 #define LUA_IGMARK		"-"
33 #endif
34 
35 
36 /*
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.
41 */
42 #if !defined(LUA_CSUBSEP)
43 #define LUA_CSUBSEP		LUA_DIRSEP
44 #endif
45 
46 #if !defined(LUA_LSUBSEP)
47 #define LUA_LSUBSEP		LUA_DIRSEP
48 #endif
49 
50 
51 /* prefix for open functions in C libraries */
52 #define LUA_POF		"luaopen_"
53 
54 /* separator for open functions in C libraries */
55 #define LUA_OFSEP	"_"
56 
57 
58 /*
59 ** key for table in the registry that keeps handles
60 ** for all loaded C libraries
61 */
62 static const char *const CLIBS = "_CLIBS";
63 
64 #define LIB_FAIL	"open"
65 
66 
67 #define setprogdir(L)           ((void)0)
68 
69 
70 /*
71 ** Special type equivalent to '(void*)' for functions in gcc
72 ** (to suppress warnings when converting function pointers)
73 */
74 typedef void (*voidf)(void);
75 
76 
77 /*
78 ** system-dependent functions
79 */
80 
81 /*
82 ** unload library 'lib'
83 */
84 static void lsys_unloadlib (void *lib);
85 
86 /*
87 ** load C library in file 'path'. If 'seeglb', load with all names in
88 ** the library global.
89 ** Returns the library; in case of error, returns NULL plus an
90 ** error string in the stack.
91 */
92 static void *lsys_load (lua_State *L, const char *path, int seeglb);
93 
94 /*
95 ** Try to find a function named 'sym' in library 'lib'.
96 ** Returns the function; in case of error, returns NULL plus an
97 ** error string in the stack.
98 */
99 static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym);
100 
101 
102 
103 
104 #if defined(LUA_USE_DLOPEN)	/* { */
105 /*
106 ** {========================================================================
107 ** This is an implementation of loadlib based on the dlfcn interface.
108 ** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,
109 ** NetBSD, AIX 4.2, HPUX 11, and  probably most other Unix flavors, at least
110 ** as an emulation layer on top of native functions.
111 ** =========================================================================
112 */
113 
114 #include <dlfcn.h>
115 
116 /*
117 ** Macro to convert pointer-to-void* to pointer-to-function. This cast
118 ** is undefined according to ISO C, but POSIX assumes that it works.
119 ** (The '__extension__' in gnu compilers is only to avoid warnings.)
120 */
121 #if defined(__GNUC__)
122 #define cast_func(p) (__extension__ (lua_CFunction)(p))
123 #else
124 #define cast_func(p) ((lua_CFunction)(p))
125 #endif
126 
127 
128 static void lsys_unloadlib (void *lib) {
129   dlclose(lib);
130 }
131 
132 
133 static 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 (lib == NULL) lua_pushstring(L, dlerror());
136   return lib;
137 }
138 
139 
140 static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
141   lua_CFunction f = cast_func(dlsym(lib, sym));
142   if (f == NULL) lua_pushstring(L, dlerror());
143   return f;
144 }
145 
146 /* }====================================================== */
147 
148 
149 
150 #elif defined(LUA_DL_DLL)	/* }{ */
151 /*
152 ** {======================================================================
153 ** This is an implementation of loadlib for Windows using native functions.
154 ** =======================================================================
155 */
156 
157 #include <windows.h>
158 
159 
160 /*
161 ** optional flags for LoadLibraryEx
162 */
163 #if !defined(LUA_LLE_FLAGS)
164 #define LUA_LLE_FLAGS	0
165 #endif
166 
167 
168 #undef setprogdir
169 
170 
171 /*
172 ** Replace in the path (on the top of the stack) any occurrence
173 ** of LUA_EXEC_DIR with the executable's path.
174 */
175 static void setprogdir (lua_State *L) {
176   char buff[MAX_PATH + 1];
177   char *lb;
178   DWORD nsize = sizeof(buff)/sizeof(char);
179   DWORD n = GetModuleFileNameA(NULL, buff, nsize);  /* get exec. name */
180   if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL)
181     luaL_error(L, "unable to get ModuleFileName");
182   else {
183     *lb = '\0';  /* cut name on the last '\\' to get the path */
184     luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff);
185     lua_remove(L, -2);  /* remove original string */
186   }
187 }
188 
189 
190 
191 
192 static void pusherror (lua_State *L) {
193   int error = GetLastError();
194   char buffer[128];
195   if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
196       NULL, error, 0, buffer, sizeof(buffer)/sizeof(char), NULL))
197     lua_pushstring(L, buffer);
198   else
199     lua_pushfstring(L, "system error %d\n", error);
200 }
201 
202 static void lsys_unloadlib (void *lib) {
203   FreeLibrary((HMODULE)lib);
204 }
205 
206 
207 static void *lsys_load (lua_State *L, const char *path, int seeglb) {
208   HMODULE lib = LoadLibraryExA(path, NULL, LUA_LLE_FLAGS);
209   (void)(seeglb);  /* not used: symbols are 'global' by default */
210   if (lib == NULL) pusherror(L);
211   return lib;
212 }
213 
214 
215 static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
216   lua_CFunction f = (lua_CFunction)(voidf)GetProcAddress((HMODULE)lib, sym);
217   if (f == NULL) pusherror(L);
218   return f;
219 }
220 
221 /* }====================================================== */
222 
223 
224 #else				/* }{ */
225 /*
226 ** {======================================================
227 ** Fallback for other systems
228 ** =======================================================
229 */
230 
231 #undef LIB_FAIL
232 #define LIB_FAIL	"absent"
233 
234 
235 #define DLMSG	"dynamic libraries not enabled; check your Lua installation"
236 
237 
238 static void lsys_unloadlib (void *lib) {
239   (void)(lib);  /* not used */
240 }
241 
242 
243 static void *lsys_load (lua_State *L, const char *path, int seeglb) {
244   (void)(path); (void)(seeglb);  /* not used */
245   lua_pushliteral(L, DLMSG);
246   return NULL;
247 }
248 
249 
250 static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
251   (void)(lib); (void)(sym);  /* not used */
252   lua_pushliteral(L, DLMSG);
253   return NULL;
254 }
255 
256 /* }====================================================== */
257 #endif				/* } */
258 
259 
260 /*
261 ** {==================================================================
262 ** Set Paths
263 ** ===================================================================
264 */
265 
266 /*
267 ** LUA_PATH_VAR and LUA_CPATH_VAR are the names of the environment
268 ** variables that Lua check to set its paths.
269 */
270 #if !defined(LUA_PATH_VAR)
271 #define LUA_PATH_VAR    "LUA_PATH"
272 #endif
273 
274 #if !defined(LUA_CPATH_VAR)
275 #define LUA_CPATH_VAR   "LUA_CPATH"
276 #endif
277 
278 
279 
280 /*
281 ** return registry.LUA_NOENV as a boolean
282 */
283 static int noenv (lua_State *L) {
284   int b;
285   lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
286   b = lua_toboolean(L, -1);
287   lua_pop(L, 1);  /* remove value */
288   return b;
289 }
290 
291 
292 /*
293 ** Set a path
294 */
295 static void setpath (lua_State *L, const char *fieldname,
296                                    const char *envname,
297                                    const char *dft) {
298   const char *dftmark;
299   const char *nver = lua_pushfstring(L, "%s%s", envname, LUA_VERSUFFIX);
300   const char *path = getenv(nver);  /* try versioned name */
301   if (path == NULL)  /* no versioned environment variable? */
302     path = getenv(envname);  /* try unversioned name */
303   if (path == NULL || noenv(L))  /* no environment variable? */
304     lua_pushstring(L, dft);  /* use default */
305   else if ((dftmark = strstr(path, LUA_PATH_SEP LUA_PATH_SEP)) == NULL)
306     lua_pushstring(L, path);  /* nothing to change */
307   else {  /* path contains a ";;": insert default path in its place */
308     size_t len = strlen(path);
309     luaL_Buffer b;
310     luaL_buffinit(L, &b);
311     if (path < dftmark) {  /* is there a prefix before ';;'? */
312       luaL_addlstring(&b, path, dftmark - path);  /* add it */
313       luaL_addchar(&b, *LUA_PATH_SEP);
314     }
315     luaL_addstring(&b, dft);  /* add default */
316     if (dftmark < path + len - 2) {  /* is there a suffix after ';;'? */
317       luaL_addchar(&b, *LUA_PATH_SEP);
318       luaL_addlstring(&b, dftmark + 2, (path + len - 2) - dftmark);
319     }
320     luaL_pushresult(&b);
321   }
322   setprogdir(L);
323   lua_setfield(L, -3, fieldname);  /* package[fieldname] = path value */
324   lua_pop(L, 1);  /* pop versioned variable name ('nver') */
325 }
326 
327 /* }================================================================== */
328 
329 
330 /*
331 ** return registry.CLIBS[path]
332 */
333 static void *checkclib (lua_State *L, const char *path) {
334   void *plib;
335   lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
336   lua_getfield(L, -1, path);
337   plib = lua_touserdata(L, -1);  /* plib = CLIBS[path] */
338   lua_pop(L, 2);  /* pop CLIBS table and 'plib' */
339   return plib;
340 }
341 
342 
343 /*
344 ** registry.CLIBS[path] = plib        -- for queries
345 ** registry.CLIBS[#CLIBS + 1] = plib  -- also keep a list of all libraries
346 */
347 static void addtoclib (lua_State *L, const char *path, void *plib) {
348   lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
349   lua_pushlightuserdata(L, plib);
350   lua_pushvalue(L, -1);
351   lua_setfield(L, -3, path);  /* CLIBS[path] = plib */
352   lua_rawseti(L, -2, luaL_len(L, -2) + 1);  /* CLIBS[#CLIBS + 1] = plib */
353   lua_pop(L, 1);  /* pop CLIBS table */
354 }
355 
356 
357 /*
358 ** __gc tag method for CLIBS table: calls 'lsys_unloadlib' for all lib
359 ** handles in list CLIBS
360 */
361 static int gctm (lua_State *L) {
362   lua_Integer n = luaL_len(L, 1);
363   for (; n >= 1; n--) {  /* for each handle, in reverse order */
364     lua_rawgeti(L, 1, n);  /* get handle CLIBS[n] */
365     lsys_unloadlib(lua_touserdata(L, -1));
366     lua_pop(L, 1);  /* pop handle */
367   }
368   return 0;
369 }
370 
371 
372 
373 /* error codes for 'lookforfunc' */
374 #define ERRLIB		1
375 #define ERRFUNC		2
376 
377 /*
378 ** Look for a C function named 'sym' in a dynamically loaded library
379 ** 'path'.
380 ** First, check whether the library is already loaded; if not, try
381 ** to load it.
382 ** Then, if 'sym' is '*', return true (as library has been loaded).
383 ** Otherwise, look for symbol 'sym' in the library and push a
384 ** C function with that symbol.
385 ** Return 0 and 'true' or a function in the stack; in case of
386 ** errors, return an error code and an error message in the stack.
387 */
388 static int lookforfunc (lua_State *L, const char *path, const char *sym) {
389   void *reg = checkclib(L, path);  /* check loaded C libraries */
390   if (reg == NULL) {  /* must load library? */
391     reg = lsys_load(L, path, *sym == '*');  /* global symbols if 'sym'=='*' */
392     if (reg == NULL) return ERRLIB;  /* unable to load library */
393     addtoclib(L, path, reg);
394   }
395   if (*sym == '*') {  /* loading only library (no function)? */
396     lua_pushboolean(L, 1);  /* return 'true' */
397     return 0;  /* no errors */
398   }
399   else {
400     lua_CFunction f = lsys_sym(L, reg, sym);
401     if (f == NULL)
402       return ERRFUNC;  /* unable to find function */
403     lua_pushcfunction(L, f);  /* else create new function */
404     return 0;  /* no errors */
405   }
406 }
407 
408 
409 static int ll_loadlib (lua_State *L) {
410   const char *path = luaL_checkstring(L, 1);
411   const char *init = luaL_checkstring(L, 2);
412   int stat = lookforfunc(L, path, init);
413   if (stat == 0)  /* no errors? */
414     return 1;  /* return the loaded function */
415   else {  /* error; error message is on stack top */
416     luaL_pushfail(L);
417     lua_insert(L, -2);
418     lua_pushstring(L, (stat == ERRLIB) ?  LIB_FAIL : "init");
419     return 3;  /* return fail, error message, and where */
420   }
421 }
422 
423 
424 
425 /*
426 ** {======================================================
427 ** 'require' function
428 ** =======================================================
429 */
430 
431 
432 static int readable (const char *filename) {
433   FILE *f = fopen(filename, "r");  /* try to open file */
434   if (f == NULL) return 0;  /* open failed */
435   fclose(f);
436   return 1;
437 }
438 
439 
440 /*
441 ** Get the next name in '*path' = 'name1;name2;name3;...', changing
442 ** the ending ';' to '\0' to create a zero-terminated string. Return
443 ** NULL when list ends.
444 */
445 static const char *getnextfilename (char **path, char *end) {
446   char *sep;
447   char *name = *path;
448   if (name == end)
449     return NULL;  /* no more names */
450   else if (*name == '\0') {  /* from previous iteration? */
451     *name = *LUA_PATH_SEP;  /* restore separator */
452     name++;  /* skip it */
453   }
454   sep = strchr(name, *LUA_PATH_SEP);  /* find next separator */
455   if (sep == NULL)  /* separator not found? */
456     sep = end;  /* name goes until the end */
457   *sep = '\0';  /* finish file name */
458   *path = sep;  /* will start next search from here */
459   return name;
460 }
461 
462 
463 /*
464 ** Given a path such as ";blabla.so;blublu.so", pushes the string
465 **
466 ** no file 'blabla.so'
467 **	no file 'blublu.so'
468 */
469 static void pusherrornotfound (lua_State *L, const char *path) {
470   luaL_Buffer b;
471   luaL_buffinit(L, &b);
472   luaL_addstring(&b, "no file '");
473   luaL_addgsub(&b, path, LUA_PATH_SEP, "'\n\tno file '");
474   luaL_addstring(&b, "'");
475   luaL_pushresult(&b);
476 }
477 
478 
479 static const char *searchpath (lua_State *L, const char *name,
480                                              const char *path,
481                                              const char *sep,
482                                              const char *dirsep) {
483   luaL_Buffer buff;
484   char *pathname;  /* path with name inserted */
485   char *endpathname;  /* its end */
486   const char *filename;
487   /* separator is non-empty and appears in 'name'? */
488   if (*sep != '\0' && strchr(name, *sep) != NULL)
489     name = luaL_gsub(L, name, sep, dirsep);  /* replace it by 'dirsep' */
490   luaL_buffinit(L, &buff);
491   /* add path to the buffer, replacing marks ('?') with the file name */
492   luaL_addgsub(&buff, path, LUA_PATH_MARK, name);
493   luaL_addchar(&buff, '\0');
494   pathname = luaL_buffaddr(&buff);  /* writable list of file names */
495   endpathname = pathname + luaL_bufflen(&buff) - 1;
496   while ((filename = getnextfilename(&pathname, endpathname)) != NULL) {
497     if (readable(filename))  /* does file exist and is readable? */
498       return lua_pushstring(L, filename);  /* save and return name */
499   }
500   luaL_pushresult(&buff);  /* push path to create error message */
501   pusherrornotfound(L, lua_tostring(L, -1));  /* create error message */
502   return NULL;  /* not found */
503 }
504 
505 
506 static int ll_searchpath (lua_State *L) {
507   const char *f = searchpath(L, luaL_checkstring(L, 1),
508                                 luaL_checkstring(L, 2),
509                                 luaL_optstring(L, 3, "."),
510                                 luaL_optstring(L, 4, LUA_DIRSEP));
511   if (f != NULL) return 1;
512   else {  /* error message is on top of the stack */
513     luaL_pushfail(L);
514     lua_insert(L, -2);
515     return 2;  /* return fail + error message */
516   }
517 }
518 
519 
520 static const char *findfile (lua_State *L, const char *name,
521                                            const char *pname,
522                                            const char *dirsep) {
523   const char *path;
524   lua_getfield(L, lua_upvalueindex(1), pname);
525   path = lua_tostring(L, -1);
526   if (path == NULL)
527     luaL_error(L, "'package.%s' must be a string", pname);
528   return searchpath(L, name, path, ".", dirsep);
529 }
530 
531 
532 static int checkload (lua_State *L, int stat, const char *filename) {
533   if (stat) {  /* module loaded successfully? */
534     lua_pushstring(L, filename);  /* will be 2nd argument to module */
535     return 2;  /* return open function and file name */
536   }
537   else
538     return luaL_error(L, "error loading module '%s' from file '%s':\n\t%s",
539                           lua_tostring(L, 1), filename, lua_tostring(L, -1));
540 }
541 
542 
543 static int searcher_Lua (lua_State *L) {
544   const char *filename;
545   const char *name = luaL_checkstring(L, 1);
546   filename = findfile(L, name, "path", LUA_LSUBSEP);
547   if (filename == NULL) return 1;  /* module not found in this path */
548   return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename);
549 }
550 
551 
552 /*
553 ** Try to find a load function for module 'modname' at file 'filename'.
554 ** First, change '.' to '_' in 'modname'; then, if 'modname' has
555 ** the form X-Y (that is, it has an "ignore mark"), build a function
556 ** name "luaopen_X" and look for it. (For compatibility, if that
557 ** fails, it also tries "luaopen_Y".) If there is no ignore mark,
558 ** look for a function named "luaopen_modname".
559 */
560 static int loadfunc (lua_State *L, const char *filename, const char *modname) {
561   const char *openfunc;
562   const char *mark;
563   modname = luaL_gsub(L, modname, ".", LUA_OFSEP);
564   mark = strchr(modname, *LUA_IGMARK);
565   if (mark) {
566     int stat;
567     openfunc = lua_pushlstring(L, modname, mark - modname);
568     openfunc = lua_pushfstring(L, LUA_POF"%s", openfunc);
569     stat = lookforfunc(L, filename, openfunc);
570     if (stat != ERRFUNC) return stat;
571     modname = mark + 1;  /* else go ahead and try old-style name */
572   }
573   openfunc = lua_pushfstring(L, LUA_POF"%s", modname);
574   return lookforfunc(L, filename, openfunc);
575 }
576 
577 
578 static int searcher_C (lua_State *L) {
579   const char *name = luaL_checkstring(L, 1);
580   const char *filename = findfile(L, name, "cpath", LUA_CSUBSEP);
581   if (filename == NULL) return 1;  /* module not found in this path */
582   return checkload(L, (loadfunc(L, filename, name) == 0), filename);
583 }
584 
585 
586 static int searcher_Croot (lua_State *L) {
587   const char *filename;
588   const char *name = luaL_checkstring(L, 1);
589   const char *p = strchr(name, '.');
590   int stat;
591   if (p == NULL) return 0;  /* is root */
592   lua_pushlstring(L, name, p - name);
593   filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP);
594   if (filename == NULL) return 1;  /* root not found */
595   if ((stat = loadfunc(L, filename, name)) != 0) {
596     if (stat != ERRFUNC)
597       return checkload(L, 0, filename);  /* real error */
598     else {  /* open function not found */
599       lua_pushfstring(L, "no module '%s' in file '%s'", name, filename);
600       return 1;
601     }
602   }
603   lua_pushstring(L, filename);  /* will be 2nd argument to module */
604   return 2;
605 }
606 
607 
608 static int searcher_preload (lua_State *L) {
609   const char *name = luaL_checkstring(L, 1);
610   lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
611   if (lua_getfield(L, -1, name) == LUA_TNIL) {  /* not found? */
612     lua_pushfstring(L, "no field package.preload['%s']", name);
613     return 1;
614   }
615   else {
616     lua_pushliteral(L, ":preload:");
617     return 2;
618   }
619 }
620 
621 
622 static void findloader (lua_State *L, const char *name) {
623   int i;
624   luaL_Buffer msg;  /* to build error message */
625   /* push 'package.searchers' to index 3 in the stack */
626   if (lua_getfield(L, lua_upvalueindex(1), "searchers") != LUA_TTABLE)
627     luaL_error(L, "'package.searchers' must be a table");
628   luaL_buffinit(L, &msg);
629   /*  iterate over available searchers to find a loader */
630   for (i = 1; ; i++) {
631     luaL_addstring(&msg, "\n\t");  /* error-message prefix */
632     if (lua_rawgeti(L, 3, i) == LUA_TNIL) {  /* no more searchers? */
633       lua_pop(L, 1);  /* remove nil */
634       luaL_buffsub(&msg, 2);  /* remove prefix */
635       luaL_pushresult(&msg);  /* create error message */
636       luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1));
637     }
638     lua_pushstring(L, name);
639     lua_call(L, 1, 2);  /* call it */
640     if (lua_isfunction(L, -2))  /* did it find a loader? */
641       return;  /* module loader found */
642     else if (lua_isstring(L, -2)) {  /* searcher returned error message? */
643       lua_pop(L, 1);  /* remove extra return */
644       luaL_addvalue(&msg);  /* concatenate error message */
645     }
646     else {  /* no error message */
647       lua_pop(L, 2);  /* remove both returns */
648       luaL_buffsub(&msg, 2);  /* remove prefix */
649     }
650   }
651 }
652 
653 
654 static int ll_require (lua_State *L) {
655   const char *name = luaL_checkstring(L, 1);
656   lua_settop(L, 1);  /* LOADED table will be at index 2 */
657   lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
658   lua_getfield(L, 2, name);  /* LOADED[name] */
659   if (lua_toboolean(L, -1))  /* is it there? */
660     return 1;  /* package is already loaded */
661   /* else must load package */
662   lua_pop(L, 1);  /* remove 'getfield' result */
663   findloader(L, name);
664   lua_rotate(L, -2, 1);  /* function <-> loader data */
665   lua_pushvalue(L, 1);  /* name is 1st argument to module loader */
666   lua_pushvalue(L, -3);  /* loader data is 2nd argument */
667   /* stack: ...; loader data; loader function; mod. name; loader data */
668   lua_call(L, 2, 1);  /* run loader to load module */
669   /* stack: ...; loader data; result from loader */
670   if (!lua_isnil(L, -1))  /* non-nil return? */
671     lua_setfield(L, 2, name);  /* LOADED[name] = returned value */
672   else
673     lua_pop(L, 1);  /* pop nil */
674   if (lua_getfield(L, 2, name) == LUA_TNIL) {   /* module set no value? */
675     lua_pushboolean(L, 1);  /* use true as result */
676     lua_copy(L, -1, -2);  /* replace loader result */
677     lua_setfield(L, 2, name);  /* LOADED[name] = true */
678   }
679   lua_rotate(L, -2, 1);  /* loader data <-> module result  */
680   return 2;  /* return module result and loader data */
681 }
682 
683 /* }====================================================== */
684 
685 
686 
687 
688 static const luaL_Reg pk_funcs[] = {
689   {"loadlib", ll_loadlib},
690   {"searchpath", ll_searchpath},
691   /* placeholders */
692   {"preload", NULL},
693   {"cpath", NULL},
694   {"path", NULL},
695   {"searchers", NULL},
696   {"loaded", NULL},
697   {NULL, NULL}
698 };
699 
700 
701 static const luaL_Reg ll_funcs[] = {
702   {"require", ll_require},
703   {NULL, NULL}
704 };
705 
706 
707 static void createsearcherstable (lua_State *L) {
708   static const lua_CFunction searchers[] =
709     {searcher_preload, searcher_Lua, searcher_C, searcher_Croot, NULL};
710   int i;
711   /* create 'searchers' table */
712   lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0);
713   /* fill it with predefined searchers */
714   for (i=0; searchers[i] != NULL; i++) {
715     lua_pushvalue(L, -2);  /* set 'package' as upvalue for all searchers */
716     lua_pushcclosure(L, searchers[i], 1);
717     lua_rawseti(L, -2, i+1);
718   }
719   lua_setfield(L, -2, "searchers");  /* put it in field 'searchers' */
720 }
721 
722 
723 /*
724 ** create table CLIBS to keep track of loaded C libraries,
725 ** setting a finalizer to close all libraries when closing state.
726 */
727 static void createclibstable (lua_State *L) {
728   luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS);  /* create CLIBS table */
729   lua_createtable(L, 0, 1);  /* create metatable for CLIBS */
730   lua_pushcfunction(L, gctm);
731   lua_setfield(L, -2, "__gc");  /* set finalizer for CLIBS table */
732   lua_setmetatable(L, -2);
733 }
734 
735 
736 LUAMOD_API int luaopen_package (lua_State *L) {
737   createclibstable(L);
738   luaL_newlib(L, pk_funcs);  /* create 'package' table */
739   createsearcherstable(L);
740   /* set paths */
741   setpath(L, "path", LUA_PATH_VAR, LUA_PATH_DEFAULT);
742   setpath(L, "cpath", LUA_CPATH_VAR, LUA_CPATH_DEFAULT);
743   /* store config information */
744   lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n"
745                      LUA_EXEC_DIR "\n" LUA_IGMARK "\n");
746   lua_setfield(L, -2, "config");
747   /* set field 'loaded' */
748   luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
749   lua_setfield(L, -2, "loaded");
750   /* set field 'preload' */
751   luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
752   lua_setfield(L, -2, "preload");
753   lua_pushglobaltable(L);
754   lua_pushvalue(L, -2);  /* set 'package' as upvalue for next lib */
755   luaL_setfuncs(L, ll_funcs, 1);  /* open lib into global table */
756   lua_pop(L, 1);  /* pop global table */
757   return 1;  /* return 'package' table */
758 }
759 
760