loadlib.c 18 KB

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