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