]>
Commit | Line | Data |
---|---|---|
16a5fed6 | 1 | /*\r |
2 | ** $Id: loadlib.c,v 1.111.1.1 2013/04/12 18:48:47 roberto Exp $\r | |
3 | ** Dynamic library loader for Lua\r | |
4 | ** See Copyright Notice in lua.h\r | |
5 | **\r | |
6 | ** This module contains an implementation of loadlib for Unix systems\r | |
7 | ** that have dlfcn, an implementation for Windows, and a stub for other\r | |
8 | ** systems.\r | |
9 | */\r | |
10 | \r | |
11 | \r | |
12 | /*\r | |
13 | ** if needed, includes windows header before everything else\r | |
14 | */\r | |
15 | #if defined(_WIN32) && !defined(UEFI_C_SOURCE) && !defined(_DOS_WATCOM)\r | |
16 | #include <windows.h>\r | |
17 | #endif\r | |
18 | \r | |
19 | \r | |
20 | #include <stdlib.h>\r | |
21 | #include <string.h>\r | |
22 | \r | |
23 | \r | |
24 | #define loadlib_c\r | |
25 | #define LUA_LIB\r | |
26 | \r | |
27 | #include "lua.h"\r | |
28 | \r | |
29 | #include "lauxlib.h"\r | |
30 | #include "lualib.h"\r | |
31 | \r | |
32 | \r | |
33 | /*\r | |
34 | ** LUA_PATH and LUA_CPATH are the names of the environment\r | |
35 | ** variables that Lua check to set its paths.\r | |
36 | */\r | |
37 | #if !defined(LUA_PATH)\r | |
38 | #define LUA_PATH "LUA_PATH"\r | |
39 | #endif\r | |
40 | \r | |
41 | #if !defined(LUA_CPATH)\r | |
42 | #define LUA_CPATH "LUA_CPATH"\r | |
43 | #endif\r | |
44 | \r | |
45 | #define LUA_PATHSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR\r | |
46 | \r | |
47 | #define LUA_PATHVERSION LUA_PATH LUA_PATHSUFFIX\r | |
48 | #define LUA_CPATHVERSION LUA_CPATH LUA_PATHSUFFIX\r | |
49 | \r | |
50 | /*\r | |
51 | ** LUA_PATH_SEP is the character that separates templates in a path.\r | |
52 | ** LUA_PATH_MARK is the string that marks the substitution points in a\r | |
53 | ** template.\r | |
54 | ** LUA_EXEC_DIR in a Windows path is replaced by the executable's\r | |
55 | ** directory.\r | |
56 | ** LUA_IGMARK is a mark to ignore all before it when building the\r | |
57 | ** luaopen_ function name.\r | |
58 | */\r | |
59 | #if !defined (LUA_PATH_SEP)\r | |
60 | #define LUA_PATH_SEP ";"\r | |
61 | #endif\r | |
62 | #if !defined (LUA_PATH_MARK)\r | |
63 | #define LUA_PATH_MARK "?"\r | |
64 | #endif\r | |
65 | #if !defined (LUA_EXEC_DIR)\r | |
66 | #define LUA_EXEC_DIR "!"\r | |
67 | #endif\r | |
68 | #if !defined (LUA_IGMARK)\r | |
69 | #define LUA_IGMARK "-"\r | |
70 | #endif\r | |
71 | \r | |
72 | \r | |
73 | /*\r | |
74 | ** LUA_CSUBSEP is the character that replaces dots in submodule names\r | |
75 | ** when searching for a C loader.\r | |
76 | ** LUA_LSUBSEP is the character that replaces dots in submodule names\r | |
77 | ** when searching for a Lua loader.\r | |
78 | */\r | |
79 | #if !defined(LUA_CSUBSEP)\r | |
80 | #define LUA_CSUBSEP LUA_DIRSEP\r | |
81 | #endif\r | |
82 | \r | |
83 | #if !defined(LUA_LSUBSEP)\r | |
84 | #define LUA_LSUBSEP LUA_DIRSEP\r | |
85 | #endif\r | |
86 | \r | |
87 | \r | |
88 | /* prefix for open functions in C libraries */\r | |
89 | #define LUA_POF "luaopen_"\r | |
90 | \r | |
91 | /* separator for open functions in C libraries */\r | |
92 | #define LUA_OFSEP "_"\r | |
93 | \r | |
94 | \r | |
95 | /* table (in the registry) that keeps handles for all loaded C libraries */\r | |
96 | #define CLIBS "_CLIBS"\r | |
97 | \r | |
98 | #define LIB_FAIL "open"\r | |
99 | \r | |
100 | \r | |
101 | /* error codes for ll_loadfunc */\r | |
102 | #define ERRLIB 1\r | |
103 | #define ERRFUNC 2\r | |
104 | \r | |
105 | #define setprogdir(L) ((void)0)\r | |
106 | \r | |
107 | \r | |
108 | /*\r | |
109 | ** system-dependent functions\r | |
110 | */\r | |
111 | static void ll_unloadlib (void *lib);\r | |
112 | static void *ll_load (lua_State *L, const char *path, int seeglb);\r | |
113 | static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym);\r | |
114 | \r | |
115 | \r | |
116 | \r | |
117 | #if defined(LUA_USE_DLOPEN)\r | |
118 | /*\r | |
119 | ** {========================================================================\r | |
120 | ** This is an implementation of loadlib based on the dlfcn interface.\r | |
121 | ** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,\r | |
122 | ** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least\r | |
123 | ** as an emulation layer on top of native functions.\r | |
124 | ** =========================================================================\r | |
125 | */\r | |
126 | \r | |
127 | #include <dlfcn.h>\r | |
128 | \r | |
129 | static void ll_unloadlib (void *lib) {\r | |
130 | dlclose(lib);\r | |
131 | }\r | |
132 | \r | |
133 | \r | |
134 | static void *ll_load (lua_State *L, const char *path, int seeglb) {\r | |
135 | void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL));\r | |
136 | if (lib == NULL) lua_pushstring(L, dlerror());\r | |
137 | return lib;\r | |
138 | }\r | |
139 | \r | |
140 | \r | |
141 | static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {\r | |
142 | lua_CFunction f = (lua_CFunction)dlsym(lib, sym);\r | |
143 | if (f == NULL) lua_pushstring(L, dlerror());\r | |
144 | return f;\r | |
145 | }\r | |
146 | \r | |
147 | /* }====================================================== */\r | |
148 | \r | |
149 | \r | |
150 | \r | |
151 | #elif defined(LUA_DL_DLL)\r | |
152 | /*\r | |
153 | ** {======================================================================\r | |
154 | ** This is an implementation of loadlib for Windows using native functions.\r | |
155 | ** =======================================================================\r | |
156 | */\r | |
157 | \r | |
158 | #undef setprogdir\r | |
159 | \r | |
160 | /*\r | |
161 | ** optional flags for LoadLibraryEx\r | |
162 | */\r | |
163 | #if !defined(LUA_LLE_FLAGS)\r | |
164 | #define LUA_LLE_FLAGS 0\r | |
165 | #endif\r | |
166 | \r | |
167 | \r | |
168 | static void setprogdir (lua_State *L) {\r | |
169 | char buff[MAX_PATH + 1];\r | |
170 | char *lb;\r | |
171 | DWORD nsize = sizeof(buff)/sizeof(char);\r | |
172 | DWORD n = GetModuleFileNameA(NULL, buff, nsize);\r | |
173 | if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL)\r | |
174 | luaL_error(L, "unable to get ModuleFileName");\r | |
175 | else {\r | |
176 | *lb = '\0';\r | |
177 | luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff);\r | |
178 | lua_remove(L, -2); /* remove original string */\r | |
179 | }\r | |
180 | }\r | |
181 | \r | |
182 | \r | |
183 | static void pusherror (lua_State *L) {\r | |
184 | int error = GetLastError();\r | |
185 | char buffer[128];\r | |
186 | if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,\r | |
187 | NULL, error, 0, buffer, sizeof(buffer)/sizeof(char), NULL))\r | |
188 | lua_pushstring(L, buffer);\r | |
189 | else\r | |
190 | lua_pushfstring(L, "system error %d\n", error);\r | |
191 | }\r | |
192 | \r | |
193 | static void ll_unloadlib (void *lib) {\r | |
194 | FreeLibrary((HMODULE)lib);\r | |
195 | }\r | |
196 | \r | |
197 | \r | |
198 | static void *ll_load (lua_State *L, const char *path, int seeglb) {\r | |
199 | HMODULE lib = LoadLibraryExA(path, NULL, LUA_LLE_FLAGS);\r | |
200 | (void)(seeglb); /* not used: symbols are 'global' by default */\r | |
201 | if (lib == NULL) pusherror(L);\r | |
202 | return lib;\r | |
203 | }\r | |
204 | \r | |
205 | \r | |
206 | static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {\r | |
207 | lua_CFunction f = (lua_CFunction)GetProcAddress((HMODULE)lib, sym);\r | |
208 | if (f == NULL) pusherror(L);\r | |
209 | return f;\r | |
210 | }\r | |
211 | \r | |
212 | /* }====================================================== */\r | |
213 | \r | |
214 | \r | |
215 | #else\r | |
216 | /*\r | |
217 | ** {======================================================\r | |
218 | ** Fallback for other systems\r | |
219 | ** =======================================================\r | |
220 | */\r | |
221 | \r | |
222 | #undef LIB_FAIL\r | |
223 | #define LIB_FAIL "absent"\r | |
224 | \r | |
225 | \r | |
226 | #define DLMSG "dynamic libraries not enabled; check your Lua installation"\r | |
227 | \r | |
228 | \r | |
229 | static void ll_unloadlib (void *lib) {\r | |
230 | (void)(lib); /* not used */\r | |
231 | }\r | |
232 | \r | |
233 | \r | |
234 | static void *ll_load (lua_State *L, const char *path, int seeglb) {\r | |
235 | (void)(path); (void)(seeglb); /* not used */\r | |
236 | lua_pushliteral(L, DLMSG);\r | |
237 | return NULL;\r | |
238 | }\r | |
239 | \r | |
240 | \r | |
241 | static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {\r | |
242 | (void)(lib); (void)(sym); /* not used */\r | |
243 | lua_pushliteral(L, DLMSG);\r | |
244 | return NULL;\r | |
245 | }\r | |
246 | \r | |
247 | /* }====================================================== */\r | |
248 | #endif\r | |
249 | \r | |
250 | \r | |
251 | static void *ll_checkclib (lua_State *L, const char *path) {\r | |
252 | void *plib;\r | |
253 | lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);\r | |
254 | lua_getfield(L, -1, path);\r | |
255 | plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */\r | |
256 | lua_pop(L, 2); /* pop CLIBS table and 'plib' */\r | |
257 | return plib;\r | |
258 | }\r | |
259 | \r | |
260 | \r | |
261 | static void ll_addtoclib (lua_State *L, const char *path, void *plib) {\r | |
262 | lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);\r | |
263 | lua_pushlightuserdata(L, plib);\r | |
264 | lua_pushvalue(L, -1);\r | |
265 | lua_setfield(L, -3, path); /* CLIBS[path] = plib */\r | |
266 | lua_rawseti(L, -2, luaL_len(L, -2) + 1); /* CLIBS[#CLIBS + 1] = plib */\r | |
267 | lua_pop(L, 1); /* pop CLIBS table */\r | |
268 | }\r | |
269 | \r | |
270 | \r | |
271 | /*\r | |
272 | ** __gc tag method for CLIBS table: calls 'll_unloadlib' for all lib\r | |
273 | ** handles in list CLIBS\r | |
274 | */\r | |
275 | static int gctm (lua_State *L) {\r | |
276 | int n = luaL_len(L, 1);\r | |
277 | for (; n >= 1; n--) { /* for each handle, in reverse order */\r | |
278 | lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */\r | |
279 | ll_unloadlib(lua_touserdata(L, -1));\r | |
280 | lua_pop(L, 1); /* pop handle */\r | |
281 | }\r | |
282 | return 0;\r | |
283 | }\r | |
284 | \r | |
285 | \r | |
286 | static int ll_loadfunc (lua_State *L, const char *path, const char *sym) {\r | |
287 | void *reg = ll_checkclib(L, path); /* check loaded C libraries */\r | |
288 | if (reg == NULL) { /* must load library? */\r | |
289 | reg = ll_load(L, path, *sym == '*');\r | |
290 | if (reg == NULL) return ERRLIB; /* unable to load library */\r | |
291 | ll_addtoclib(L, path, reg);\r | |
292 | }\r | |
293 | if (*sym == '*') { /* loading only library (no function)? */\r | |
294 | lua_pushboolean(L, 1); /* return 'true' */\r | |
295 | return 0; /* no errors */\r | |
296 | }\r | |
297 | else {\r | |
298 | lua_CFunction f = ll_sym(L, reg, sym);\r | |
299 | if (f == NULL)\r | |
300 | return ERRFUNC; /* unable to find function */\r | |
301 | lua_pushcfunction(L, f); /* else create new function */\r | |
302 | return 0; /* no errors */\r | |
303 | }\r | |
304 | }\r | |
305 | \r | |
306 | \r | |
307 | static int ll_loadlib (lua_State *L) {\r | |
308 | const char *path = luaL_checkstring(L, 1);\r | |
309 | const char *init = luaL_checkstring(L, 2);\r | |
310 | int stat = ll_loadfunc(L, path, init);\r | |
311 | if (stat == 0) /* no errors? */\r | |
312 | return 1; /* return the loaded function */\r | |
313 | else { /* error; error message is on stack top */\r | |
314 | lua_pushnil(L);\r | |
315 | lua_insert(L, -2);\r | |
316 | lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init");\r | |
317 | return 3; /* return nil, error message, and where */\r | |
318 | }\r | |
319 | }\r | |
320 | \r | |
321 | \r | |
322 | \r | |
323 | /*\r | |
324 | ** {======================================================\r | |
325 | ** 'require' function\r | |
326 | ** =======================================================\r | |
327 | */\r | |
328 | \r | |
329 | \r | |
330 | static int readable (const char *filename) {\r | |
331 | FILE *f = fopen(filename, "r"); /* try to open file */\r | |
332 | if (f == NULL) return 0; /* open failed */\r | |
333 | fclose(f);\r | |
334 | return 1;\r | |
335 | }\r | |
336 | \r | |
337 | \r | |
338 | static const char *pushnexttemplate (lua_State *L, const char *path) {\r | |
339 | const char *l;\r | |
340 | while (*path == *LUA_PATH_SEP) path++; /* skip separators */\r | |
341 | if (*path == '\0') return NULL; /* no more templates */\r | |
342 | l = strchr(path, *LUA_PATH_SEP); /* find next separator */\r | |
343 | if (l == NULL) l = path + strlen(path);\r | |
344 | lua_pushlstring(L, path, l - path); /* template */\r | |
345 | return l;\r | |
346 | }\r | |
347 | \r | |
348 | \r | |
349 | static const char *searchpath (lua_State *L, const char *name,\r | |
350 | const char *path,\r | |
351 | const char *sep,\r | |
352 | const char *dirsep) {\r | |
353 | luaL_Buffer msg; /* to build error message */\r | |
354 | luaL_buffinit(L, &msg);\r | |
355 | if (*sep != '\0') /* non-empty separator? */\r | |
356 | name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */\r | |
357 | while ((path = pushnexttemplate(L, path)) != NULL) {\r | |
358 | const char *filename = luaL_gsub(L, lua_tostring(L, -1),\r | |
359 | LUA_PATH_MARK, name);\r | |
360 | lua_remove(L, -2); /* remove path template */\r | |
361 | if (readable(filename)) /* does file exist and is readable? */\r | |
362 | return filename; /* return that file name */\r | |
363 | lua_pushfstring(L, "\n\tno file " LUA_QS, filename);\r | |
364 | lua_remove(L, -2); /* remove file name */\r | |
365 | luaL_addvalue(&msg); /* concatenate error msg. entry */\r | |
366 | }\r | |
367 | luaL_pushresult(&msg); /* create error message */\r | |
368 | return NULL; /* not found */\r | |
369 | }\r | |
370 | \r | |
371 | \r | |
372 | static int ll_searchpath (lua_State *L) {\r | |
373 | const char *f = searchpath(L, luaL_checkstring(L, 1),\r | |
374 | luaL_checkstring(L, 2),\r | |
375 | luaL_optstring(L, 3, "."),\r | |
376 | luaL_optstring(L, 4, LUA_DIRSEP));\r | |
377 | if (f != NULL) return 1;\r | |
378 | else { /* error message is on top of the stack */\r | |
379 | lua_pushnil(L);\r | |
380 | lua_insert(L, -2);\r | |
381 | return 2; /* return nil + error message */\r | |
382 | }\r | |
383 | }\r | |
384 | \r | |
385 | \r | |
386 | static const char *findfile (lua_State *L, const char *name,\r | |
387 | const char *pname,\r | |
388 | const char *dirsep) {\r | |
389 | const char *path;\r | |
390 | lua_getfield(L, lua_upvalueindex(1), pname);\r | |
391 | path = lua_tostring(L, -1);\r | |
392 | if (path == NULL)\r | |
393 | luaL_error(L, LUA_QL("package.%s") " must be a string", pname);\r | |
394 | return searchpath(L, name, path, ".", dirsep);\r | |
395 | }\r | |
396 | \r | |
397 | \r | |
398 | static int checkload (lua_State *L, int stat, const char *filename) {\r | |
399 | if (stat) { /* module loaded successfully? */\r | |
400 | lua_pushstring(L, filename); /* will be 2nd argument to module */\r | |
401 | return 2; /* return open function and file name */\r | |
402 | }\r | |
403 | else\r | |
404 | return luaL_error(L, "error loading module " LUA_QS\r | |
405 | " from file " LUA_QS ":\n\t%s",\r | |
406 | lua_tostring(L, 1), filename, lua_tostring(L, -1));\r | |
407 | }\r | |
408 | \r | |
409 | \r | |
410 | static int searcher_Lua (lua_State *L) {\r | |
411 | const char *filename;\r | |
412 | const char *name = luaL_checkstring(L, 1);\r | |
413 | filename = findfile(L, name, "path", LUA_LSUBSEP);\r | |
414 | if (filename == NULL) return 1; /* module not found in this path */\r | |
415 | return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename);\r | |
416 | }\r | |
417 | \r | |
418 | \r | |
419 | static int loadfunc (lua_State *L, const char *filename, const char *modname) {\r | |
420 | const char *funcname;\r | |
421 | const char *mark;\r | |
422 | modname = luaL_gsub(L, modname, ".", LUA_OFSEP);\r | |
423 | mark = strchr(modname, *LUA_IGMARK);\r | |
424 | if (mark) {\r | |
425 | int stat;\r | |
426 | funcname = lua_pushlstring(L, modname, mark - modname);\r | |
427 | funcname = lua_pushfstring(L, LUA_POF"%s", funcname);\r | |
428 | stat = ll_loadfunc(L, filename, funcname);\r | |
429 | if (stat != ERRFUNC) return stat;\r | |
430 | modname = mark + 1; /* else go ahead and try old-style name */\r | |
431 | }\r | |
432 | funcname = lua_pushfstring(L, LUA_POF"%s", modname);\r | |
433 | return ll_loadfunc(L, filename, funcname);\r | |
434 | }\r | |
435 | \r | |
436 | \r | |
437 | static int searcher_C (lua_State *L) {\r | |
438 | const char *name = luaL_checkstring(L, 1);\r | |
439 | const char *filename = findfile(L, name, "cpath", LUA_CSUBSEP);\r | |
440 | if (filename == NULL) return 1; /* module not found in this path */\r | |
441 | return checkload(L, (loadfunc(L, filename, name) == 0), filename);\r | |
442 | }\r | |
443 | \r | |
444 | \r | |
445 | static int searcher_Croot (lua_State *L) {\r | |
446 | const char *filename;\r | |
447 | const char *name = luaL_checkstring(L, 1);\r | |
448 | const char *p = strchr(name, '.');\r | |
449 | int stat;\r | |
450 | if (p == NULL) return 0; /* is root */\r | |
451 | lua_pushlstring(L, name, p - name);\r | |
452 | filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP);\r | |
453 | if (filename == NULL) return 1; /* root not found */\r | |
454 | if ((stat = loadfunc(L, filename, name)) != 0) {\r | |
455 | if (stat != ERRFUNC)\r | |
456 | return checkload(L, 0, filename); /* real error */\r | |
457 | else { /* open function not found */\r | |
458 | lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS,\r | |
459 | name, filename);\r | |
460 | return 1;\r | |
461 | }\r | |
462 | }\r | |
463 | lua_pushstring(L, filename); /* will be 2nd argument to module */\r | |
464 | return 2;\r | |
465 | }\r | |
466 | \r | |
467 | \r | |
468 | static int searcher_preload (lua_State *L) {\r | |
469 | const char *name = luaL_checkstring(L, 1);\r | |
470 | lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD");\r | |
471 | lua_getfield(L, -1, name);\r | |
472 | if (lua_isnil(L, -1)) /* not found? */\r | |
473 | lua_pushfstring(L, "\n\tno field package.preload['%s']", name);\r | |
474 | return 1;\r | |
475 | }\r | |
476 | \r | |
477 | \r | |
478 | static void findloader (lua_State *L, const char *name) {\r | |
479 | int i;\r | |
480 | luaL_Buffer msg; /* to build error message */\r | |
481 | luaL_buffinit(L, &msg);\r | |
482 | lua_getfield(L, lua_upvalueindex(1), "searchers"); /* will be at index 3 */\r | |
483 | if (!lua_istable(L, 3))\r | |
484 | luaL_error(L, LUA_QL("package.searchers") " must be a table");\r | |
485 | /* iterate over available searchers to find a loader */\r | |
486 | for (i = 1; ; i++) {\r | |
487 | lua_rawgeti(L, 3, i); /* get a searcher */\r | |
488 | if (lua_isnil(L, -1)) { /* no more searchers? */\r | |
489 | lua_pop(L, 1); /* remove nil */\r | |
490 | luaL_pushresult(&msg); /* create error message */\r | |
491 | luaL_error(L, "module " LUA_QS " not found:%s",\r | |
492 | name, lua_tostring(L, -1));\r | |
493 | }\r | |
494 | lua_pushstring(L, name);\r | |
495 | lua_call(L, 1, 2); /* call it */\r | |
496 | if (lua_isfunction(L, -2)) /* did it find a loader? */\r | |
497 | return; /* module loader found */\r | |
498 | else if (lua_isstring(L, -2)) { /* searcher returned error message? */\r | |
499 | lua_pop(L, 1); /* remove extra return */\r | |
500 | luaL_addvalue(&msg); /* concatenate error message */\r | |
501 | }\r | |
502 | else\r | |
503 | lua_pop(L, 2); /* remove both returns */\r | |
504 | }\r | |
505 | }\r | |
506 | \r | |
507 | \r | |
508 | static int ll_require (lua_State *L) {\r | |
509 | const char *name = luaL_checkstring(L, 1);\r | |
510 | lua_settop(L, 1); /* _LOADED table will be at index 2 */\r | |
511 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");\r | |
512 | lua_getfield(L, 2, name); /* _LOADED[name] */\r | |
513 | if (lua_toboolean(L, -1)) /* is it there? */\r | |
514 | return 1; /* package is already loaded */\r | |
515 | /* else must load package */\r | |
516 | lua_pop(L, 1); /* remove 'getfield' result */\r | |
517 | findloader(L, name);\r | |
518 | lua_pushstring(L, name); /* pass name as argument to module loader */\r | |
519 | lua_insert(L, -2); /* name is 1st argument (before search data) */\r | |
520 | lua_call(L, 2, 1); /* run loader to load module */\r | |
521 | if (!lua_isnil(L, -1)) /* non-nil return? */\r | |
522 | lua_setfield(L, 2, name); /* _LOADED[name] = returned value */\r | |
523 | lua_getfield(L, 2, name);\r | |
524 | if (lua_isnil(L, -1)) { /* module did not set a value? */\r | |
525 | lua_pushboolean(L, 1); /* use true as result */\r | |
526 | lua_pushvalue(L, -1); /* extra copy to be returned */\r | |
527 | lua_setfield(L, 2, name); /* _LOADED[name] = true */\r | |
528 | }\r | |
529 | return 1;\r | |
530 | }\r | |
531 | \r | |
532 | /* }====================================================== */\r | |
533 | \r | |
534 | \r | |
535 | \r | |
536 | /*\r | |
537 | ** {======================================================\r | |
538 | ** 'module' function\r | |
539 | ** =======================================================\r | |
540 | */\r | |
541 | #if defined(LUA_COMPAT_MODULE)\r | |
542 | \r | |
543 | /*\r | |
544 | ** changes the environment variable of calling function\r | |
545 | */\r | |
546 | static void set_env (lua_State *L) {\r | |
547 | lua_Debug ar;\r | |
548 | if (lua_getstack(L, 1, &ar) == 0 ||\r | |
549 | lua_getinfo(L, "f", &ar) == 0 || /* get calling function */\r | |
550 | lua_iscfunction(L, -1))\r | |
551 | luaL_error(L, LUA_QL("module") " not called from a Lua function");\r | |
552 | lua_pushvalue(L, -2); /* copy new environment table to top */\r | |
553 | lua_setupvalue(L, -2, 1);\r | |
554 | lua_pop(L, 1); /* remove function */\r | |
555 | }\r | |
556 | \r | |
557 | \r | |
558 | static void dooptions (lua_State *L, int n) {\r | |
559 | int i;\r | |
560 | for (i = 2; i <= n; i++) {\r | |
561 | if (lua_isfunction(L, i)) { /* avoid 'calling' extra info. */\r | |
562 | lua_pushvalue(L, i); /* get option (a function) */\r | |
563 | lua_pushvalue(L, -2); /* module */\r | |
564 | lua_call(L, 1, 0);\r | |
565 | }\r | |
566 | }\r | |
567 | }\r | |
568 | \r | |
569 | \r | |
570 | static void modinit (lua_State *L, const char *modname) {\r | |
571 | const char *dot;\r | |
572 | lua_pushvalue(L, -1);\r | |
573 | lua_setfield(L, -2, "_M"); /* module._M = module */\r | |
574 | lua_pushstring(L, modname);\r | |
575 | lua_setfield(L, -2, "_NAME");\r | |
576 | dot = strrchr(modname, '.'); /* look for last dot in module name */\r | |
577 | if (dot == NULL) dot = modname;\r | |
578 | else dot++;\r | |
579 | /* set _PACKAGE as package name (full module name minus last part) */\r | |
580 | lua_pushlstring(L, modname, dot - modname);\r | |
581 | lua_setfield(L, -2, "_PACKAGE");\r | |
582 | }\r | |
583 | \r | |
584 | \r | |
585 | static int ll_module (lua_State *L) {\r | |
586 | const char *modname = luaL_checkstring(L, 1);\r | |
587 | int lastarg = lua_gettop(L); /* last parameter */\r | |
588 | luaL_pushmodule(L, modname, 1); /* get/create module table */\r | |
589 | /* check whether table already has a _NAME field */\r | |
590 | lua_getfield(L, -1, "_NAME");\r | |
591 | if (!lua_isnil(L, -1)) /* is table an initialized module? */\r | |
592 | lua_pop(L, 1);\r | |
593 | else { /* no; initialize it */\r | |
594 | lua_pop(L, 1);\r | |
595 | modinit(L, modname);\r | |
596 | }\r | |
597 | lua_pushvalue(L, -1);\r | |
598 | set_env(L);\r | |
599 | dooptions(L, lastarg);\r | |
600 | return 1;\r | |
601 | }\r | |
602 | \r | |
603 | \r | |
604 | static int ll_seeall (lua_State *L) {\r | |
605 | luaL_checktype(L, 1, LUA_TTABLE);\r | |
606 | if (!lua_getmetatable(L, 1)) {\r | |
607 | lua_createtable(L, 0, 1); /* create new metatable */\r | |
608 | lua_pushvalue(L, -1);\r | |
609 | lua_setmetatable(L, 1);\r | |
610 | }\r | |
611 | lua_pushglobaltable(L);\r | |
612 | lua_setfield(L, -2, "__index"); /* mt.__index = _G */\r | |
613 | return 0;\r | |
614 | }\r | |
615 | \r | |
616 | #endif\r | |
617 | /* }====================================================== */\r | |
618 | \r | |
619 | \r | |
620 | \r | |
621 | /* auxiliary mark (for internal use) */\r | |
622 | #define AUXMARK "\1"\r | |
623 | \r | |
624 | \r | |
625 | /*\r | |
626 | ** return registry.LUA_NOENV as a boolean\r | |
627 | */\r | |
628 | static int noenv (lua_State *L) {\r | |
629 | int b;\r | |
630 | lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");\r | |
631 | b = lua_toboolean(L, -1);\r | |
632 | lua_pop(L, 1); /* remove value */\r | |
633 | return b;\r | |
634 | }\r | |
635 | \r | |
636 | \r | |
637 | static void setpath (lua_State *L, const char *fieldname, const char *envname1,\r | |
638 | const char *envname2, const char *def) {\r | |
639 | const char *path = getenv(envname1);\r | |
640 | if (path == NULL) /* no environment variable? */\r | |
641 | path = getenv(envname2); /* try alternative name */\r | |
642 | if (path == NULL || noenv(L)) /* no environment variable? */\r | |
643 | lua_pushstring(L, def); /* use default */\r | |
644 | else {\r | |
645 | /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */\r | |
646 | path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP,\r | |
647 | LUA_PATH_SEP AUXMARK LUA_PATH_SEP);\r | |
648 | luaL_gsub(L, path, AUXMARK, def);\r | |
649 | lua_remove(L, -2);\r | |
650 | }\r | |
651 | setprogdir(L);\r | |
652 | lua_setfield(L, -2, fieldname);\r | |
653 | }\r | |
654 | \r | |
655 | \r | |
656 | static const luaL_Reg pk_funcs[] = {\r | |
657 | {"loadlib", ll_loadlib},\r | |
658 | {"searchpath", ll_searchpath},\r | |
659 | #if defined(LUA_COMPAT_MODULE)\r | |
660 | {"seeall", ll_seeall},\r | |
661 | #endif\r | |
662 | {NULL, NULL}\r | |
663 | };\r | |
664 | \r | |
665 | \r | |
666 | static const luaL_Reg ll_funcs[] = {\r | |
667 | #if defined(LUA_COMPAT_MODULE)\r | |
668 | {"module", ll_module},\r | |
669 | #endif\r | |
670 | {"require", ll_require},\r | |
671 | {NULL, NULL}\r | |
672 | };\r | |
673 | \r | |
674 | \r | |
675 | static void createsearcherstable (lua_State *L) {\r | |
676 | static const lua_CFunction searchers[] =\r | |
677 | {searcher_preload, searcher_Lua, searcher_C, searcher_Croot, NULL};\r | |
678 | int i;\r | |
679 | /* create 'searchers' table */\r | |
680 | lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0);\r | |
681 | /* fill it with pre-defined searchers */\r | |
682 | for (i=0; searchers[i] != NULL; i++) {\r | |
683 | lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */\r | |
684 | lua_pushcclosure(L, searchers[i], 1);\r | |
685 | lua_rawseti(L, -2, i+1);\r | |
686 | }\r | |
687 | }\r | |
688 | \r | |
689 | \r | |
690 | LUAMOD_API int luaopen_package (lua_State *L) {\r | |
691 | /* create table CLIBS to keep track of loaded C libraries */\r | |
692 | luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS);\r | |
693 | lua_createtable(L, 0, 1); /* metatable for CLIBS */\r | |
694 | lua_pushcfunction(L, gctm);\r | |
695 | lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */\r | |
696 | lua_setmetatable(L, -2);\r | |
697 | /* create `package' table */\r | |
698 | luaL_newlib(L, pk_funcs);\r | |
699 | createsearcherstable(L);\r | |
700 | #if defined(LUA_COMPAT_LOADERS)\r | |
701 | lua_pushvalue(L, -1); /* make a copy of 'searchers' table */\r | |
702 | lua_setfield(L, -3, "loaders"); /* put it in field `loaders' */\r | |
703 | #endif\r | |
704 | lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */\r | |
705 | /* set field 'path' */\r | |
706 | setpath(L, "path", LUA_PATHVERSION, LUA_PATH, LUA_PATH_DEFAULT);\r | |
707 | /* set field 'cpath' */\r | |
708 | setpath(L, "cpath", LUA_CPATHVERSION, LUA_CPATH, LUA_CPATH_DEFAULT);\r | |
709 | /* store config information */\r | |
710 | lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n"\r | |
711 | LUA_EXEC_DIR "\n" LUA_IGMARK "\n");\r | |
712 | lua_setfield(L, -2, "config");\r | |
713 | /* set field `loaded' */\r | |
714 | luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");\r | |
715 | lua_setfield(L, -2, "loaded");\r | |
716 | /* set field `preload' */\r | |
717 | luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");\r | |
718 | lua_setfield(L, -2, "preload");\r | |
719 | lua_pushglobaltable(L);\r | |
720 | lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */\r | |
721 | luaL_setfuncs(L, ll_funcs, 1); /* open lib into global table */\r | |
722 | lua_pop(L, 1); /* pop global table */\r | |
723 | return 1; /* return 'package' table */\r | |
724 | }\r | |
725 | \r |