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