source: trunk/src/lua/loadlib.c @ 2542

Last change on this file since 2542 was 2542, checked in by sam, 10 years ago

lua: compilation fixes for non-quite-POSIX platforms.

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