loadlib.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. /*
  2. ** $Id: loadlib.c,v 1.93 2010/11/10 18:05:36 roberto Exp roberto $
  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. #include <stdlib.h>
  11. #include <string.h>
  12. #define loadlib_c
  13. #define LUA_LIB
  14. #include "lua.h"
  15. #include "lauxlib.h"
  16. #include "lualib.h"
  17. /*
  18. ** LUA_PATH and LUA_CPATH are the names of the environment
  19. ** variables that Lua check to set its paths.
  20. */
  21. #if !defined(LUA_PATH)
  22. #define LUA_PATH "LUA_PATH"
  23. #endif
  24. #if !defined(LUA_CPATH)
  25. #define LUA_CPATH "LUA_CPATH"
  26. #endif
  27. #define LUA_PATHSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR
  28. #define LUA_PATHVERSION LUA_PATH LUA_PATHSUFFIX
  29. #define LUA_CPATHVERSION LUA_CPATH LUA_PATHSUFFIX
  30. /*
  31. ** LUA_PATH_SEP is the character that separates templates in a path.
  32. ** LUA_PATH_MARK is the string that marks the substitution points in a
  33. ** template.
  34. ** LUA_EXEC_DIR in a Windows path is replaced by the executable's
  35. ** directory.
  36. ** LUA_IGMARK is a mark to ignore all before it when building the
  37. ** luaopen_ function name.
  38. */
  39. #if !defined (LUA_PATH_SEP)
  40. #define LUA_PATH_SEP ";"
  41. #endif
  42. #if !defined (LUA_PATH_MARK)
  43. #define LUA_PATH_MARK "?"
  44. #endif
  45. #if !defined (LUA_EXEC_DIR)
  46. #define LUA_EXEC_DIR "!"
  47. #endif
  48. #if !defined (LUA_IGMARK)
  49. #define LUA_IGMARK "-"
  50. #endif
  51. /* prefix for open functions in C libraries */
  52. #define LUA_POF "luaopen_"
  53. /* separator for open functions in C libraries */
  54. #define LUA_OFSEP "_"
  55. #define LIBPREFIX "LOADLIB: "
  56. #define POF LUA_POF
  57. #define LIB_FAIL "open"
  58. /* error codes for ll_loadfunc */
  59. #define ERRLIB 1
  60. #define ERRFUNC 2
  61. #define setprogdir(L) ((void)0)
  62. /*
  63. ** system-dependent functions
  64. */
  65. static void ll_unloadlib (void *lib);
  66. static void *ll_load (lua_State *L, const char *path, int seeglb);
  67. static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym);
  68. #if defined(LUA_USE_DLOPEN)
  69. /*
  70. ** {========================================================================
  71. ** This is an implementation of loadlib based on the dlfcn interface.
  72. ** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,
  73. ** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least
  74. ** as an emulation layer on top of native functions.
  75. ** =========================================================================
  76. */
  77. #include <dlfcn.h>
  78. static void ll_unloadlib (void *lib) {
  79. dlclose(lib);
  80. }
  81. static void *ll_load (lua_State *L, const char *path, int seeglb) {
  82. void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : 0));
  83. if (lib == NULL) lua_pushstring(L, dlerror());
  84. return lib;
  85. }
  86. static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
  87. lua_CFunction f = (lua_CFunction)dlsym(lib, sym);
  88. if (f == NULL) lua_pushstring(L, dlerror());
  89. return f;
  90. }
  91. /* }====================================================== */
  92. #elif defined(LUA_DL_DLL)
  93. /*
  94. ** {======================================================================
  95. ** This is an implementation of loadlib for Windows using native functions.
  96. ** =======================================================================
  97. */
  98. #include <windows.h>
  99. #undef setprogdir
  100. /*
  101. ** optional flags for LoadLibraryEx
  102. */
  103. #if !defined(LUA_LLE_FLAGS)
  104. #define LUA_LLE_FLAGS 0
  105. #endif
  106. static void setprogdir (lua_State *L) {
  107. char buff[MAX_PATH + 1];
  108. char *lb;
  109. DWORD nsize = sizeof(buff)/sizeof(char);
  110. DWORD n = GetModuleFileNameA(NULL, buff, nsize);
  111. if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL)
  112. luaL_error(L, "unable to get ModuleFileName");
  113. else {
  114. *lb = '\0';
  115. luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff);
  116. lua_remove(L, -2); /* remove original string */
  117. }
  118. }
  119. static void pusherror (lua_State *L) {
  120. int error = GetLastError();
  121. char buffer[128];
  122. if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
  123. NULL, error, 0, buffer, sizeof(buffer), NULL))
  124. lua_pushstring(L, buffer);
  125. else
  126. lua_pushfstring(L, "system error %d\n", error);
  127. }
  128. static void ll_unloadlib (void *lib) {
  129. FreeLibrary((HMODULE)lib);
  130. }
  131. static void *ll_load (lua_State *L, const char *path, int seeglb) {
  132. HMODULE lib = LoadLibraryExA(path, NULL, LUA_LLE_FLAGS);
  133. (void)(seeglb); /* symbols are 'global' by default */
  134. if (lib == NULL) pusherror(L);
  135. return lib;
  136. }
  137. static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
  138. lua_CFunction f = (lua_CFunction)GetProcAddress((HMODULE)lib, sym);
  139. if (f == NULL) pusherror(L);
  140. return f;
  141. }
  142. /* }====================================================== */
  143. #else
  144. /*
  145. ** {======================================================
  146. ** Fallback for other systems
  147. ** =======================================================
  148. */
  149. #undef LIB_FAIL
  150. #define LIB_FAIL "absent"
  151. #define DLMSG "dynamic libraries not enabled; check your Lua installation"
  152. static void ll_unloadlib (void *lib) {
  153. (void)(lib); /* to avoid warnings */
  154. }
  155. static void *ll_load (lua_State *L, const char *path, int seeglb) {
  156. (void)(path); (void)(seeglb); /* to avoid warnings */
  157. lua_pushliteral(L, DLMSG);
  158. return NULL;
  159. }
  160. static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
  161. (void)(lib); (void)(sym); /* to avoid warnings */
  162. lua_pushliteral(L, DLMSG);
  163. return NULL;
  164. }
  165. /* }====================================================== */
  166. #endif
  167. static void **ll_register (lua_State *L, const char *path) {
  168. void **plib;
  169. lua_pushfstring(L, "%s%s", LIBPREFIX, path);
  170. lua_gettable(L, LUA_REGISTRYINDEX); /* check library in registry? */
  171. if (!lua_isnil(L, -1)) /* is there an entry? */
  172. plib = (void **)lua_touserdata(L, -1);
  173. else { /* no entry yet; create one */
  174. lua_pop(L, 1); /* remove result from gettable */
  175. plib = (void **)lua_newuserdata(L, sizeof(const void *));
  176. *plib = NULL;
  177. luaL_setmetatable(L, "_LOADLIB");
  178. lua_pushfstring(L, "%s%s", LIBPREFIX, path);
  179. lua_pushvalue(L, -2);
  180. lua_settable(L, LUA_REGISTRYINDEX);
  181. }
  182. return plib;
  183. }
  184. /*
  185. ** __gc tag method: calls library's `ll_unloadlib' function with the lib
  186. ** handle
  187. */
  188. static int gctm (lua_State *L) {
  189. void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB");
  190. if (*lib) ll_unloadlib(*lib);
  191. *lib = NULL; /* mark library as closed */
  192. return 0;
  193. }
  194. static int ll_loadfunc (lua_State *L, const char *path, const char *sym) {
  195. void **reg = ll_register(L, path);
  196. if (*reg == NULL) *reg = ll_load(L, path, *sym == '*');
  197. if (*reg == NULL) return ERRLIB; /* unable to load library */
  198. if (*sym == '*') { /* loading only library (no function)? */
  199. lua_pushboolean(L, 1); /* return 'true' */
  200. return 0; /* no errors */
  201. }
  202. else {
  203. lua_CFunction f = ll_sym(L, *reg, sym);
  204. if (f == NULL)
  205. return ERRFUNC; /* unable to find function */
  206. lua_pushcfunction(L, f); /* else create new function */
  207. return 0; /* no errors */
  208. }
  209. }
  210. static int ll_loadlib (lua_State *L) {
  211. const char *path = luaL_checkstring(L, 1);
  212. const char *init = luaL_checkstring(L, 2);
  213. int stat = ll_loadfunc(L, path, init);
  214. if (stat == 0) /* no errors? */
  215. return 1; /* return the loaded function */
  216. else { /* error; error message is on stack top */
  217. lua_pushnil(L);
  218. lua_insert(L, -2);
  219. lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init");
  220. return 3; /* return nil, error message, and where */
  221. }
  222. }
  223. /*
  224. ** {======================================================
  225. ** 'require' function
  226. ** =======================================================
  227. */
  228. static int readable (const char *filename) {
  229. FILE *f = fopen(filename, "r"); /* try to open file */
  230. if (f == NULL) return 0; /* open failed */
  231. fclose(f);
  232. return 1;
  233. }
  234. static const char *pushnexttemplate (lua_State *L, const char *path) {
  235. const char *l;
  236. while (*path == *LUA_PATH_SEP) path++; /* skip separators */
  237. if (*path == '\0') return NULL; /* no more templates */
  238. l = strchr(path, *LUA_PATH_SEP); /* find next separator */
  239. if (l == NULL) l = path + strlen(path);
  240. lua_pushlstring(L, path, l - path); /* template */
  241. return l;
  242. }
  243. static const char *searchpath (lua_State *L, const char *name,
  244. const char *path) {
  245. name = luaL_gsub(L, name, ".", LUA_DIRSEP);
  246. lua_pushliteral(L, ""); /* error accumulator */
  247. while ((path = pushnexttemplate(L, path)) != NULL) {
  248. const char *filename = luaL_gsub(L, lua_tostring(L, -1),
  249. LUA_PATH_MARK, name);
  250. lua_remove(L, -2); /* remove path template */
  251. if (readable(filename)) /* does file exist and is readable? */
  252. return filename; /* return that file name */
  253. lua_pushfstring(L, "\n\tno file " LUA_QS, filename);
  254. lua_remove(L, -2); /* remove file name */
  255. lua_concat(L, 2); /* add entry to possible error message */
  256. }
  257. return NULL; /* not found */
  258. }
  259. static int ll_searchpath (lua_State *L) {
  260. const char *f = searchpath(L, luaL_checkstring(L, 1), luaL_checkstring(L, 2));
  261. if (f != NULL) return 1;
  262. else { /* error message is on top of the stack */
  263. lua_pushnil(L);
  264. lua_insert(L, -2);
  265. return 2; /* return nil + error message */
  266. }
  267. }
  268. static const char *findfile (lua_State *L, const char *name,
  269. const char *pname) {
  270. const char *path;
  271. lua_getfield(L, lua_upvalueindex(1), pname);
  272. path = lua_tostring(L, -1);
  273. if (path == NULL)
  274. luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
  275. return searchpath(L, name, path);
  276. }
  277. static void loaderror (lua_State *L, const char *filename) {
  278. luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s",
  279. lua_tostring(L, 1), filename, lua_tostring(L, -1));
  280. }
  281. static int loader_Lua (lua_State *L) {
  282. const char *filename;
  283. const char *name = luaL_checkstring(L, 1);
  284. filename = findfile(L, name, "path");
  285. if (filename == NULL) return 1; /* library not found in this path */
  286. if (luaL_loadfile(L, filename) != LUA_OK)
  287. loaderror(L, filename);
  288. return 1; /* library loaded successfully */
  289. }
  290. static int loadfunc (lua_State *L, const char *filename, const char *modname) {
  291. const char *funcname;
  292. const char *mark;
  293. modname = luaL_gsub(L, modname, ".", LUA_OFSEP);
  294. mark = strchr(modname, *LUA_IGMARK);
  295. if (mark) {
  296. int stat;
  297. funcname = lua_pushlstring(L, modname, mark - modname);
  298. funcname = lua_pushfstring(L, POF"%s", funcname);
  299. stat = ll_loadfunc(L, filename, funcname);
  300. if (stat != ERRFUNC) return stat;
  301. modname = mark + 1; /* else go ahead and try old-style name */
  302. }
  303. funcname = lua_pushfstring(L, POF"%s", modname);
  304. return ll_loadfunc(L, filename, funcname);
  305. }
  306. static int loader_C (lua_State *L) {
  307. const char *name = luaL_checkstring(L, 1);
  308. const char *filename = findfile(L, name, "cpath");
  309. if (filename == NULL) return 1; /* library not found in this path */
  310. if (loadfunc(L, filename, name) != 0)
  311. loaderror(L, filename);
  312. return 1; /* library loaded successfully */
  313. }
  314. static int loader_Croot (lua_State *L) {
  315. const char *filename;
  316. const char *name = luaL_checkstring(L, 1);
  317. const char *p = strchr(name, '.');
  318. int stat;
  319. if (p == NULL) return 0; /* is root */
  320. lua_pushlstring(L, name, p - name);
  321. filename = findfile(L, lua_tostring(L, -1), "cpath");
  322. if (filename == NULL) return 1; /* root not found */
  323. if ((stat = loadfunc(L, filename, name)) != 0) {
  324. if (stat != ERRFUNC) loaderror(L, filename); /* real error */
  325. lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS,
  326. name, filename);
  327. return 1; /* function not found */
  328. }
  329. return 1;
  330. }
  331. static int loader_preload (lua_State *L) {
  332. const char *name = luaL_checkstring(L, 1);
  333. lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD");
  334. lua_getfield(L, -1, name);
  335. if (lua_isnil(L, -1)) /* not found? */
  336. lua_pushfstring(L, "\n\tno field package.preload['%s']", name);
  337. return 1;
  338. }
  339. static int ll_require (lua_State *L) {
  340. const char *name = luaL_checkstring(L, 1);
  341. int i;
  342. lua_settop(L, 1); /* _LOADED table will be at index 2 */
  343. lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
  344. lua_getfield(L, 2, name);
  345. if (lua_toboolean(L, -1)) /* is it there? */
  346. return 1; /* package is already loaded */
  347. /* else must load it; iterate over available loaders */
  348. lua_getfield(L, lua_upvalueindex(1), "loaders");
  349. if (!lua_istable(L, -1))
  350. luaL_error(L, LUA_QL("package.loaders") " must be a table");
  351. lua_pushliteral(L, ""); /* error message accumulator */
  352. for (i=1; ; i++) {
  353. lua_rawgeti(L, -2, i); /* get a loader */
  354. if (lua_isnil(L, -1))
  355. luaL_error(L, "module " LUA_QS " not found:%s",
  356. name, lua_tostring(L, -2));
  357. lua_pushstring(L, name);
  358. lua_call(L, 1, 1); /* call it */
  359. if (lua_isfunction(L, -1)) /* did it find module? */
  360. break; /* module loaded successfully */
  361. else if (lua_isstring(L, -1)) /* loader returned error message? */
  362. lua_concat(L, 2); /* accumulate it */
  363. else
  364. lua_pop(L, 1);
  365. }
  366. lua_pushstring(L, name); /* pass name as argument to module */
  367. lua_call(L, 1, 1); /* run loaded module */
  368. if (!lua_isnil(L, -1)) /* non-nil return? */
  369. lua_setfield(L, 2, name); /* _LOADED[name] = returned value */
  370. lua_getfield(L, 2, name);
  371. if (lua_isnil(L, -1)) { /* module did not set a value? */
  372. lua_pushboolean(L, 1); /* use true as result */
  373. lua_pushvalue(L, -1); /* extra copy to be returned */
  374. lua_setfield(L, 2, name); /* _LOADED[name] = true */
  375. }
  376. return 1;
  377. }
  378. /* }====================================================== */
  379. /*
  380. ** {======================================================
  381. ** 'module' function
  382. ** =======================================================
  383. */
  384. #if defined(LUA_COMPAT_MODULE)
  385. /*
  386. ** changes the environment variable of calling function
  387. */
  388. static void set_env (lua_State *L) {
  389. lua_Debug ar;
  390. if (lua_getstack(L, 1, &ar) == 0 ||
  391. lua_getinfo(L, "f", &ar) == 0 || /* get calling function */
  392. lua_iscfunction(L, -1))
  393. luaL_error(L, LUA_QL("module") " not called from a Lua function");
  394. lua_pushvalue(L, -2); /* copy new environment table to top */
  395. lua_setupvalue(L, -2, 1);
  396. lua_pop(L, 1); /* remove function */
  397. }
  398. static void dooptions (lua_State *L, int n) {
  399. int i;
  400. for (i = 2; i <= n; i++) {
  401. lua_pushvalue(L, i); /* get option (a function) */
  402. lua_pushvalue(L, -2); /* module */
  403. lua_call(L, 1, 0);
  404. }
  405. }
  406. static void modinit (lua_State *L, const char *modname) {
  407. const char *dot;
  408. lua_pushvalue(L, -1);
  409. lua_setfield(L, -2, "_M"); /* module._M = module */
  410. lua_pushstring(L, modname);
  411. lua_setfield(L, -2, "_NAME");
  412. dot = strrchr(modname, '.'); /* look for last dot in module name */
  413. if (dot == NULL) dot = modname;
  414. else dot++;
  415. /* set _PACKAGE as package name (full module name minus last part) */
  416. lua_pushlstring(L, modname, dot - modname);
  417. lua_setfield(L, -2, "_PACKAGE");
  418. }
  419. static int ll_module (lua_State *L) {
  420. const char *modname = luaL_checkstring(L, 1);
  421. int lastarg = lua_gettop(L); /* last parameter */
  422. luaL_pushmodule(L, modname, 1); /* get/create module table */
  423. /* check whether table already has a _NAME field */
  424. lua_getfield(L, -1, "_NAME");
  425. if (!lua_isnil(L, -1)) /* is table an initialized module? */
  426. lua_pop(L, 1);
  427. else { /* no; initialize it */
  428. lua_pop(L, 1);
  429. modinit(L, modname);
  430. }
  431. lua_pushvalue(L, -1);
  432. set_env(L);
  433. dooptions(L, lastarg);
  434. return 1;
  435. }
  436. static int ll_seeall (lua_State *L) {
  437. luaL_checktype(L, 1, LUA_TTABLE);
  438. if (!lua_getmetatable(L, 1)) {
  439. lua_createtable(L, 0, 1); /* create new metatable */
  440. lua_pushvalue(L, -1);
  441. lua_setmetatable(L, 1);
  442. }
  443. lua_pushglobaltable(L);
  444. lua_setfield(L, -2, "__index"); /* mt.__index = _G */
  445. return 0;
  446. }
  447. #else
  448. static int ll_seeall (lua_State *L) {
  449. return luaL_error(L, "deprecated function");
  450. }
  451. static int ll_module (lua_State *L) {
  452. return luaL_error(L, "deprecated function");
  453. }
  454. #endif
  455. /* }====================================================== */
  456. /* auxiliary mark (for internal use) */
  457. #define AUXMARK "\1"
  458. static void setpath (lua_State *L, const char *fieldname, const char *envname1,
  459. const char *envname2, const char *def) {
  460. const char *path = getenv(envname1);
  461. if (path == NULL) /* no environment variable? */
  462. path = getenv(envname2); /* try alternative name */
  463. if (path == NULL) /* no environment variable? */
  464. lua_pushstring(L, def); /* use default */
  465. else {
  466. /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */
  467. path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP,
  468. LUA_PATH_SEP AUXMARK LUA_PATH_SEP);
  469. luaL_gsub(L, path, AUXMARK, def);
  470. lua_remove(L, -2);
  471. }
  472. setprogdir(L);
  473. lua_setfield(L, -2, fieldname);
  474. }
  475. static const luaL_Reg pk_funcs[] = {
  476. {"loadlib", ll_loadlib},
  477. {"searchpath", ll_searchpath},
  478. {"seeall", ll_seeall},
  479. {NULL, NULL}
  480. };
  481. static const luaL_Reg ll_funcs[] = {
  482. {"module", ll_module},
  483. {"require", ll_require},
  484. {NULL, NULL}
  485. };
  486. static const lua_CFunction loaders[] =
  487. {loader_preload, loader_Lua, loader_C, loader_Croot, NULL};
  488. LUAMOD_API int luaopen_package (lua_State *L) {
  489. int i;
  490. /* create new type _LOADLIB */
  491. luaL_newmetatable(L, "_LOADLIB");
  492. lua_pushcfunction(L, gctm);
  493. lua_setfield(L, -2, "__gc");
  494. /* create `package' table */
  495. luaL_newlib(L, pk_funcs);
  496. /* create `loaders' table */
  497. lua_createtable(L, sizeof(loaders)/sizeof(loaders[0]) - 1, 0);
  498. /* fill it with pre-defined loaders */
  499. for (i=0; loaders[i] != NULL; i++) {
  500. lua_pushvalue(L, -2); /* set 'package' as upvalue for all loaders */
  501. lua_pushcclosure(L, loaders[i], 1);
  502. lua_rawseti(L, -2, i+1);
  503. }
  504. lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */
  505. /* set field 'path' */
  506. setpath(L, "path", LUA_PATHVERSION, LUA_PATH, LUA_PATH_DEFAULT);
  507. /* set field 'cpath' */
  508. setpath(L, "cpath", LUA_CPATHVERSION, LUA_CPATH, LUA_CPATH_DEFAULT);
  509. /* store config information */
  510. lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n"
  511. LUA_EXEC_DIR "\n" LUA_IGMARK "\n");
  512. lua_setfield(L, -2, "config");
  513. /* set field `loaded' */
  514. luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED");
  515. lua_setfield(L, -2, "loaded");
  516. /* set field `preload' */
  517. luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
  518. lua_setfield(L, -2, "preload");
  519. lua_pushglobaltable(L);
  520. lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */
  521. luaL_setfuncs(L, ll_funcs, 1); /* open lib into global table */
  522. lua_pop(L, 1); /* pop global table */
  523. return 1; /* return 'package' table */
  524. }