Line data Source code
1 : /*
2 : ** $Id: loadlib.c,v 1.130.1.1 2017/04/19 17:20:42 roberto Exp $
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 : ** unique key for table in the registry that keeps handles
60 : ** for all loaded C libraries
61 : */
62 : static const int CLIBS = 0;
63 :
64 : #define LIB_FAIL "open"
65 :
66 :
67 : #define setprogdir(L) ((void)0)
68 :
69 :
70 : /*
71 : ** system-dependent functions
72 : */
73 :
74 : /*
75 : ** unload library 'lib'
76 : */
77 : static void lsys_unloadlib (void *lib);
78 :
79 : /*
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.
84 : */
85 : static void *lsys_load (lua_State *L, const char *path, int seeglb);
86 :
87 : /*
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.
91 : */
92 : static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym);
93 :
94 :
95 :
96 :
97 : #if defined(LUA_USE_DLOPEN) /* { */
98 : /*
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 : ** =========================================================================
105 : */
106 :
107 : #include <dlfcn.h>
108 :
109 : /*
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.)
113 : */
114 : #if defined(__GNUC__)
115 : #define cast_func(p) (__extension__ (lua_CFunction)(p))
116 : #else
117 : #define cast_func(p) ((lua_CFunction)(p))
118 : #endif
119 :
120 :
121 0 : static void lsys_unloadlib (void *lib) {
122 0 : dlclose(lib);
123 0 : }
124 :
125 :
126 2 : static void *lsys_load (lua_State *L, const char *path, int seeglb) {
127 2 : void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL));
128 2 : if (lib == NULL) lua_pushstring(L, dlerror());
129 2 : return lib;
130 : }
131 :
132 :
133 0 : static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
134 0 : lua_CFunction f = cast_func(dlsym(lib, sym));
135 0 : if (f == NULL) lua_pushstring(L, dlerror());
136 0 : return f;
137 : }
138 :
139 : /* }====================================================== */
140 :
141 :
142 :
143 : #elif defined(LUA_DL_DLL) /* }{ */
144 : /*
145 : ** {======================================================================
146 : ** This is an implementation of loadlib for Windows using native functions.
147 : ** =======================================================================
148 : */
149 :
150 : #include <windows.h>
151 :
152 :
153 : /*
154 : ** optional flags for LoadLibraryEx
155 : */
156 : #if !defined(LUA_LLE_FLAGS)
157 : #define LUA_LLE_FLAGS 0
158 : #endif
159 :
160 :
161 : #undef setprogdir
162 :
163 :
164 : /*
165 : ** Replace in the path (on the top of the stack) any occurrence
166 : ** of LUA_EXEC_DIR with the executable's path.
167 : */
168 : static void setprogdir (lua_State *L) {
169 : char buff[MAX_PATH + 1];
170 : char *lb;
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");
175 : else {
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 */
179 : }
180 : }
181 :
182 :
183 :
184 :
185 : static void pusherror (lua_State *L) {
186 : int error = GetLastError();
187 : char buffer[128];
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);
191 : else
192 : lua_pushfstring(L, "system error %d\n", error);
193 : }
194 :
195 : static void lsys_unloadlib (void *lib) {
196 : FreeLibrary((HMODULE)lib);
197 : }
198 :
199 :
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);
204 : return lib;
205 : }
206 :
207 :
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);
211 : return f;
212 : }
213 :
214 : /* }====================================================== */
215 :
216 :
217 : #else /* }{ */
218 : /*
219 : ** {======================================================
220 : ** Fallback for other systems
221 : ** =======================================================
222 : */
223 :
224 : #undef LIB_FAIL
225 : #define LIB_FAIL "absent"
226 :
227 :
228 : #define DLMSG "dynamic libraries not enabled; check your Lua installation"
229 :
230 :
231 : static void lsys_unloadlib (void *lib) {
232 : (void)(lib); /* not used */
233 : }
234 :
235 :
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);
239 : return NULL;
240 : }
241 :
242 :
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);
246 : return NULL;
247 : }
248 :
249 : /* }====================================================== */
250 : #endif /* } */
251 :
252 :
253 : /*
254 : ** {==================================================================
255 : ** Set Paths
256 : ** ===================================================================
257 : */
258 :
259 : /*
260 : ** LUA_PATH_VAR and LUA_CPATH_VAR are the names of the environment
261 : ** variables that Lua check to set its paths.
262 : */
263 : #if !defined(LUA_PATH_VAR)
264 : #define LUA_PATH_VAR "LUA_PATH"
265 : #endif
266 :
267 : #if !defined(LUA_CPATH_VAR)
268 : #define LUA_CPATH_VAR "LUA_CPATH"
269 : #endif
270 :
271 :
272 : #define AUXMARK "\1" /* auxiliary mark */
273 :
274 :
275 : /*
276 : ** return registry.LUA_NOENV as a boolean
277 : */
278 172 : static int noenv (lua_State *L) {
279 : int b;
280 172 : lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
281 172 : b = lua_toboolean(L, -1);
282 172 : lua_pop(L, 1); /* remove value */
283 172 : return b;
284 : }
285 :
286 :
287 : /*
288 : ** Set a path
289 : */
290 172 : static void setpath (lua_State *L, const char *fieldname,
291 : const char *envname,
292 : const char *dft) {
293 172 : const char *nver = lua_pushfstring(L, "%s%s", envname, LUA_VERSUFFIX);
294 172 : const char *path = getenv(nver); /* use versioned name */
295 172 : if (path == NULL) /* no environment variable? */
296 172 : path = getenv(envname); /* try unversioned name */
297 172 : if (path == NULL || noenv(L)) /* no environment variable? */
298 2 : lua_pushstring(L, dft); /* use default */
299 : else {
300 : /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */
301 170 : path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP,
302 : LUA_PATH_SEP AUXMARK LUA_PATH_SEP);
303 170 : luaL_gsub(L, path, AUXMARK, dft);
304 170 : lua_remove(L, -2); /* remove result from 1st 'gsub' */
305 : }
306 : setprogdir(L);
307 172 : lua_setfield(L, -3, fieldname); /* package[fieldname] = path value */
308 172 : lua_pop(L, 1); /* pop versioned variable name */
309 172 : }
310 :
311 : /* }================================================================== */
312 :
313 :
314 : /*
315 : ** return registry.CLIBS[path]
316 : */
317 2 : static void *checkclib (lua_State *L, const char *path) {
318 : void *plib;
319 2 : lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS);
320 2 : lua_getfield(L, -1, path);
321 2 : plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */
322 2 : lua_pop(L, 2); /* pop CLIBS table and 'plib' */
323 2 : return plib;
324 : }
325 :
326 :
327 : /*
328 : ** registry.CLIBS[path] = plib -- for queries
329 : ** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries
330 : */
331 0 : static void addtoclib (lua_State *L, const char *path, void *plib) {
332 0 : lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS);
333 0 : lua_pushlightuserdata(L, plib);
334 0 : lua_pushvalue(L, -1);
335 0 : lua_setfield(L, -3, path); /* CLIBS[path] = plib */
336 0 : lua_rawseti(L, -2, luaL_len(L, -2) + 1); /* CLIBS[#CLIBS + 1] = plib */
337 0 : lua_pop(L, 1); /* pop CLIBS table */
338 0 : }
339 :
340 :
341 : /*
342 : ** __gc tag method for CLIBS table: calls 'lsys_unloadlib' for all lib
343 : ** handles in list CLIBS
344 : */
345 77 : static int gctm (lua_State *L) {
346 77 : lua_Integer n = luaL_len(L, 1);
347 77 : for (; n >= 1; n--) { /* for each handle, in reverse order */
348 0 : lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */
349 0 : lsys_unloadlib(lua_touserdata(L, -1));
350 0 : lua_pop(L, 1); /* pop handle */
351 : }
352 77 : return 0;
353 : }
354 :
355 :
356 :
357 : /* error codes for 'lookforfunc' */
358 : #define ERRLIB 1
359 : #define ERRFUNC 2
360 :
361 : /*
362 : ** Look for a C function named 'sym' in a dynamically loaded library
363 : ** 'path'.
364 : ** First, check whether the library is already loaded; if not, try
365 : ** to load it.
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.
371 : */
372 2 : static int lookforfunc (lua_State *L, const char *path, const char *sym) {
373 2 : void *reg = checkclib(L, path); /* check loaded C libraries */
374 2 : if (reg == NULL) { /* must load library? */
375 2 : reg = lsys_load(L, path, *sym == '*'); /* global symbols if 'sym'=='*' */
376 2 : if (reg == NULL) return ERRLIB; /* unable to load library */
377 0 : addtoclib(L, path, reg);
378 : }
379 0 : if (*sym == '*') { /* loading only library (no function)? */
380 0 : lua_pushboolean(L, 1); /* return 'true' */
381 0 : return 0; /* no errors */
382 : }
383 : else {
384 0 : lua_CFunction f = lsys_sym(L, reg, sym);
385 0 : if (f == NULL)
386 0 : return ERRFUNC; /* unable to find function */
387 0 : lua_pushcfunction(L, f); /* else create new function */
388 0 : return 0; /* no errors */
389 : }
390 : }
391 :
392 :
393 2 : static int ll_loadlib (lua_State *L) {
394 2 : const char *path = luaL_checkstring(L, 1);
395 2 : const char *init = luaL_checkstring(L, 2);
396 2 : int stat = lookforfunc(L, path, init);
397 2 : if (stat == 0) /* no errors? */
398 0 : return 1; /* return the loaded function */
399 : else { /* error; error message is on stack top */
400 2 : lua_pushnil(L);
401 2 : lua_insert(L, -2);
402 2 : lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init");
403 2 : return 3; /* return nil, error message, and where */
404 : }
405 : }
406 :
407 :
408 :
409 : /*
410 : ** {======================================================
411 : ** 'require' function
412 : ** =======================================================
413 : */
414 :
415 :
416 1276 : static int readable (const char *filename) {
417 1276 : FILE *f = fopen(filename, "r"); /* try to open file */
418 1276 : if (f == NULL) return 0; /* open failed */
419 100 : fclose(f);
420 100 : return 1;
421 : }
422 :
423 :
424 1412 : static const char *pushnexttemplate (lua_State *L, const char *path) {
425 : const char *l;
426 2587 : while (*path == *LUA_PATH_SEP) path++; /* skip separators */
427 1412 : if (*path == '\0') return NULL; /* no more templates */
428 1276 : l = strchr(path, *LUA_PATH_SEP); /* find next separator */
429 1276 : if (l == NULL) l = path + strlen(path);
430 1276 : lua_pushlstring(L, path, l - path); /* template */
431 1276 : return l;
432 : }
433 :
434 :
435 236 : static const char *searchpath (lua_State *L, const char *name,
436 : const char *path,
437 : const char *sep,
438 : const char *dirsep) {
439 : luaL_Buffer msg; /* to build error message */
440 236 : luaL_buffinit(L, &msg);
441 236 : if (*sep != '\0') /* non-empty separator? */
442 236 : name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */
443 1412 : while ((path = pushnexttemplate(L, path)) != NULL) {
444 1276 : const char *filename = luaL_gsub(L, lua_tostring(L, -1),
445 : LUA_PATH_MARK, name);
446 1276 : lua_remove(L, -2); /* remove path template */
447 1276 : if (readable(filename)) /* does file exist and is readable? */
448 100 : return filename; /* return that file name */
449 1176 : lua_pushfstring(L, "\n\tno file '%s'", filename);
450 1176 : lua_remove(L, -2); /* remove file name */
451 1176 : luaL_addvalue(&msg); /* concatenate error msg. entry */
452 : }
453 136 : luaL_pushresult(&msg); /* create error message */
454 136 : return NULL; /* not found */
455 : }
456 :
457 :
458 3 : static int ll_searchpath (lua_State *L) {
459 3 : 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 3 : if (f != NULL) return 1;
464 : else { /* error message is on top of the stack */
465 2 : lua_pushnil(L);
466 2 : lua_insert(L, -2);
467 2 : return 2; /* return nil + error message */
468 : }
469 : }
470 :
471 :
472 233 : static const char *findfile (lua_State *L, const char *name,
473 : const char *pname,
474 : const char *dirsep) {
475 : const char *path;
476 233 : lua_getfield(L, lua_upvalueindex(1), pname);
477 233 : path = lua_tostring(L, -1);
478 233 : if (path == NULL)
479 0 : luaL_error(L, "'package.%s' must be a string", pname);
480 233 : return searchpath(L, name, path, ".", dirsep);
481 : }
482 :
483 :
484 99 : static int checkload (lua_State *L, int stat, const char *filename) {
485 99 : if (stat) { /* module loaded successfully? */
486 98 : lua_pushstring(L, filename); /* will be 2nd argument to module */
487 98 : return 2; /* return open function and file name */
488 : }
489 : else
490 1 : return luaL_error(L, "error loading module '%s' from file '%s':\n\t%s",
491 : lua_tostring(L, 1), filename, lua_tostring(L, -1));
492 : }
493 :
494 :
495 144 : static int searcher_Lua (lua_State *L) {
496 : const char *filename;
497 144 : const char *name = luaL_checkstring(L, 1);
498 144 : filename = findfile(L, name, "path", LUA_LSUBSEP);
499 144 : if (filename == NULL) return 1; /* module not found in this path */
500 99 : return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename);
501 : }
502 :
503 :
504 : /*
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".
511 : */
512 0 : static int loadfunc (lua_State *L, const char *filename, const char *modname) {
513 : const char *openfunc;
514 : const char *mark;
515 0 : modname = luaL_gsub(L, modname, ".", LUA_OFSEP);
516 0 : mark = strchr(modname, *LUA_IGMARK);
517 0 : if (mark) {
518 : int stat;
519 0 : openfunc = lua_pushlstring(L, modname, mark - modname);
520 0 : openfunc = lua_pushfstring(L, LUA_POF"%s", openfunc);
521 0 : stat = lookforfunc(L, filename, openfunc);
522 0 : if (stat != ERRFUNC) return stat;
523 0 : modname = mark + 1; /* else go ahead and try old-style name */
524 : }
525 0 : openfunc = lua_pushfstring(L, LUA_POF"%s", modname);
526 0 : return lookforfunc(L, filename, openfunc);
527 : }
528 :
529 :
530 45 : static int searcher_C (lua_State *L) {
531 45 : const char *name = luaL_checkstring(L, 1);
532 45 : const char *filename = findfile(L, name, "cpath", LUA_CSUBSEP);
533 45 : if (filename == NULL) return 1; /* module not found in this path */
534 0 : return checkload(L, (loadfunc(L, filename, name) == 0), filename);
535 : }
536 :
537 :
538 45 : static int searcher_Croot (lua_State *L) {
539 : const char *filename;
540 45 : const char *name = luaL_checkstring(L, 1);
541 45 : const char *p = strchr(name, '.');
542 : int stat;
543 45 : if (p == NULL) return 0; /* is root */
544 44 : lua_pushlstring(L, name, p - name);
545 44 : filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP);
546 44 : if (filename == NULL) return 1; /* root not found */
547 0 : if ((stat = loadfunc(L, filename, name)) != 0) {
548 0 : if (stat != ERRFUNC)
549 0 : return checkload(L, 0, filename); /* real error */
550 : else { /* open function not found */
551 0 : lua_pushfstring(L, "\n\tno module '%s' in file '%s'", name, filename);
552 0 : return 1;
553 : }
554 : }
555 0 : lua_pushstring(L, filename); /* will be 2nd argument to module */
556 0 : return 2;
557 : }
558 :
559 :
560 145 : static int searcher_preload (lua_State *L) {
561 145 : const char *name = luaL_checkstring(L, 1);
562 145 : lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
563 145 : if (lua_getfield(L, -1, name) == LUA_TNIL) /* not found? */
564 144 : lua_pushfstring(L, "\n\tno field package.preload['%s']", name);
565 145 : return 1;
566 : }
567 :
568 :
569 145 : static void findloader (lua_State *L, const char *name) {
570 : int i;
571 : luaL_Buffer msg; /* to build error message */
572 145 : luaL_buffinit(L, &msg);
573 : /* push 'package.searchers' to index 3 in the stack */
574 145 : if (lua_getfield(L, lua_upvalueindex(1), "searchers") != LUA_TTABLE)
575 0 : luaL_error(L, "'package.searchers' must be a table");
576 : /* iterate over available searchers to find a loader */
577 145 : for (i = 1; ; i++) {
578 424 : if (lua_rawgeti(L, 3, i) == LUA_TNIL) { /* no more searchers? */
579 45 : lua_pop(L, 1); /* remove nil */
580 45 : luaL_pushresult(&msg); /* create error message */
581 45 : luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1));
582 : }
583 379 : lua_pushstring(L, name);
584 379 : lua_call(L, 1, 2); /* call it */
585 378 : if (lua_isfunction(L, -2)) /* did it find a loader? */
586 99 : return; /* module loader found */
587 279 : else if (lua_isstring(L, -2)) { /* searcher returned error message? */
588 278 : lua_pop(L, 1); /* remove extra return */
589 278 : luaL_addvalue(&msg); /* concatenate error message */
590 : }
591 : else
592 1 : lua_pop(L, 2); /* remove both returns */
593 : }
594 : }
595 :
596 :
597 169 : static int ll_require (lua_State *L) {
598 169 : const char *name = luaL_checkstring(L, 1);
599 169 : lua_settop(L, 1); /* LOADED table will be at index 2 */
600 169 : lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
601 169 : lua_getfield(L, 2, name); /* LOADED[name] */
602 169 : if (lua_toboolean(L, -1)) /* is it there? */
603 24 : return 1; /* package is already loaded */
604 : /* else must load package */
605 145 : lua_pop(L, 1); /* remove 'getfield' result */
606 145 : findloader(L, name);
607 99 : lua_pushstring(L, name); /* pass name as argument to module loader */
608 99 : lua_insert(L, -2); /* name is 1st argument (before search data) */
609 99 : lua_call(L, 2, 1); /* run loader to load module */
610 99 : if (!lua_isnil(L, -1)) /* non-nil return? */
611 53 : lua_setfield(L, 2, name); /* LOADED[name] = returned value */
612 99 : if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */
613 46 : lua_pushboolean(L, 1); /* use true as result */
614 46 : lua_pushvalue(L, -1); /* extra copy to be returned */
615 46 : lua_setfield(L, 2, name); /* LOADED[name] = true */
616 : }
617 99 : return 1;
618 : }
619 :
620 : /* }====================================================== */
621 :
622 :
623 :
624 : /*
625 : ** {======================================================
626 : ** 'module' function
627 : ** =======================================================
628 : */
629 : #if defined(LUA_COMPAT_MODULE)
630 :
631 : /*
632 : ** changes the environment variable of calling function
633 : */
634 : static void set_env (lua_State *L) {
635 : lua_Debug ar;
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 */
643 : }
644 :
645 :
646 : static void dooptions (lua_State *L, int n) {
647 : int i;
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 */
652 : lua_call(L, 1, 0);
653 : }
654 : }
655 : }
656 :
657 :
658 : static void modinit (lua_State *L, const char *modname) {
659 : const char *dot;
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;
666 : else dot++;
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");
670 : }
671 :
672 :
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 */
681 : lua_pop(L, 1);
682 : modinit(L, modname);
683 : }
684 : lua_pushvalue(L, -1);
685 : set_env(L);
686 : dooptions(L, lastarg);
687 : return 1;
688 : }
689 :
690 :
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);
697 : }
698 : lua_pushglobaltable(L);
699 : lua_setfield(L, -2, "__index"); /* mt.__index = _G */
700 : return 0;
701 : }
702 :
703 : #endif
704 : /* }====================================================== */
705 :
706 :
707 :
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},
713 : #endif
714 : /* placeholders */
715 : {"preload", NULL},
716 : {"cpath", NULL},
717 : {"path", NULL},
718 : {"searchers", NULL},
719 : {"loaded", NULL},
720 : {NULL, NULL}
721 : };
722 :
723 :
724 : static const luaL_Reg ll_funcs[] = {
725 : #if defined(LUA_COMPAT_MODULE)
726 : {"module", ll_module},
727 : #endif
728 : {"require", ll_require},
729 : {NULL, NULL}
730 : };
731 :
732 :
733 86 : static void createsearcherstable (lua_State *L) {
734 : static const lua_CFunction searchers[] =
735 : {searcher_preload, searcher_Lua, searcher_C, searcher_Croot, NULL};
736 : int i;
737 : /* create 'searchers' table */
738 86 : lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0);
739 : /* fill it with predefined searchers */
740 430 : for (i=0; searchers[i] != NULL; i++) {
741 344 : lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */
742 344 : lua_pushcclosure(L, searchers[i], 1);
743 344 : lua_rawseti(L, -2, i+1);
744 : }
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' */
748 : #endif
749 86 : lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */
750 86 : }
751 :
752 :
753 : /*
754 : ** create table CLIBS to keep track of loaded C libraries,
755 : ** setting a finalizer to close all libraries when closing state.
756 : */
757 86 : static void createclibstable (lua_State *L) {
758 86 : lua_newtable(L); /* create CLIBS table */
759 86 : lua_createtable(L, 0, 1); /* create metatable for CLIBS */
760 86 : lua_pushcfunction(L, gctm);
761 86 : lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */
762 86 : lua_setmetatable(L, -2);
763 86 : lua_rawsetp(L, LUA_REGISTRYINDEX, &CLIBS); /* set CLIBS table in registry */
764 86 : }
765 :
766 :
767 86 : LUAMOD_API int luaopen_package (lua_State *L) {
768 86 : createclibstable(L);
769 86 : luaL_newlib(L, pk_funcs); /* create 'package' table */
770 86 : createsearcherstable(L);
771 : /* set paths */
772 86 : setpath(L, "path", LUA_PATH_VAR, LUA_PATH_DEFAULT);
773 86 : setpath(L, "cpath", LUA_CPATH_VAR, LUA_CPATH_DEFAULT);
774 : /* store config information */
775 86 : lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n"
776 : LUA_EXEC_DIR "\n" LUA_IGMARK "\n");
777 86 : lua_setfield(L, -2, "config");
778 : /* set field 'loaded' */
779 86 : luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
780 86 : lua_setfield(L, -2, "loaded");
781 : /* set field 'preload' */
782 86 : luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
783 86 : lua_setfield(L, -2, "preload");
784 86 : lua_pushglobaltable(L);
785 86 : lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */
786 86 : luaL_setfuncs(L, ll_funcs, 1); /* open lib into global table */
787 86 : lua_pop(L, 1); /* pop global table */
788 86 : return 1; /* return 'package' table */
789 : }
790 :
|