2
0

loadlib.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /*
  2. ** $Id: loadlib.c,v 1.47 2005/10/06 20:46:10 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 Darwin (Mac OS X), an
  8. ** implementation for Windows, and a stub for other 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. /* environment variables that hold the search path for packages */
  18. #define LUA_PATH "LUA_PATH"
  19. #define LUA_CPATH "LUA_CPATH"
  20. /* prefix for open functions in C libraries */
  21. #define LUA_POF "luaopen_"
  22. /* separator for open functions in C libraries */
  23. #define LUA_OFSEP "_"
  24. #define LIBPREFIX "LOADLIB: "
  25. #define POF LUA_POF
  26. #define LIB_FAIL "open"
  27. /* error codes for ll_loadfunc */
  28. #define ERRLIB 1
  29. #define ERRFUNC 2
  30. #define setprogdir(L) ((void)0)
  31. static void ll_unloadlib (void *lib);
  32. static void *ll_load (lua_State *L, const char *path);
  33. static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym);
  34. #if defined(LUA_DL_DLOPEN)
  35. /*
  36. ** {========================================================================
  37. ** This is an implementation of loadlib based on the dlfcn interface.
  38. ** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,
  39. ** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least
  40. ** as an emulation layer on top of native functions.
  41. ** =========================================================================
  42. */
  43. #include <dlfcn.h>
  44. static void ll_unloadlib (void *lib) {
  45. dlclose(lib);
  46. }
  47. static void *ll_load (lua_State *L, const char *path) {
  48. void *lib = dlopen(path, RTLD_NOW);
  49. if (lib == NULL) lua_pushstring(L, dlerror());
  50. return lib;
  51. }
  52. static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
  53. lua_CFunction f = (lua_CFunction)dlsym(lib, sym);
  54. if (f == NULL) lua_pushstring(L, dlerror());
  55. return f;
  56. }
  57. /* }====================================================== */
  58. #elif defined(LUA_DL_DLL)
  59. /*
  60. ** {======================================================================
  61. ** This is an implementation of loadlib for Windows using native functions.
  62. ** =======================================================================
  63. */
  64. #include <windows.h>
  65. #undef setprogdir
  66. static void setprogdir (lua_State *L) {
  67. char buff[MAX_PATH + 1];
  68. char *lb;
  69. DWORD nsize = sizeof(buff)/sizeof(char);
  70. DWORD n = GetModuleFileName(NULL, buff, nsize);
  71. if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL)
  72. luaL_error(L, "unable to get ModuleFileName");
  73. else {
  74. *lb = '\0';
  75. luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, buff);
  76. lua_remove(L, -2); /* remove original string */
  77. }
  78. }
  79. static void pusherror (lua_State *L) {
  80. int error = GetLastError();
  81. char buffer[128];
  82. if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
  83. NULL, error, 0, buffer, sizeof(buffer), NULL))
  84. lua_pushstring(L, buffer);
  85. else
  86. lua_pushfstring(L, "system error %d\n", error);
  87. }
  88. static void ll_unloadlib (void *lib) {
  89. FreeLibrary((HINSTANCE)lib);
  90. }
  91. static void *ll_load (lua_State *L, const char *path) {
  92. HINSTANCE lib = LoadLibrary(path);
  93. if (lib == NULL) pusherror(L);
  94. return lib;
  95. }
  96. static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
  97. lua_CFunction f = (lua_CFunction)GetProcAddress((HINSTANCE)lib, sym);
  98. if (f == NULL) pusherror(L);
  99. return f;
  100. }
  101. /* }====================================================== */
  102. #elif defined(LUA_DL_DYLD)
  103. /*
  104. ** {======================================================================
  105. ** Native Mac OS X / Darwin Implementation
  106. ** =======================================================================
  107. */
  108. #include <mach-o/dyld.h>
  109. /* Mac appends a `_' before C function names */
  110. #undef POF
  111. #define POF "_" LUA_POF
  112. static void pusherror (lua_State *L) {
  113. const char *err_str;
  114. const char *err_file;
  115. NSLinkEditErrors err;
  116. int err_num;
  117. NSLinkEditError(&err, &err_num, &err_file, &err_str);
  118. lua_pushstring(L, err_str);
  119. }
  120. static const char *errorfromcode (NSObjectFileImageReturnCode ret) {
  121. switch (ret) {
  122. case NSObjectFileImageInappropriateFile:
  123. return "file is not a bundle";
  124. case NSObjectFileImageArch:
  125. return "library is for wrong CPU type";
  126. case NSObjectFileImageFormat:
  127. return "bad format";
  128. case NSObjectFileImageAccess:
  129. return "cannot access file";
  130. case NSObjectFileImageFailure:
  131. default:
  132. return "unable to load library";
  133. }
  134. }
  135. static void ll_unloadlib (void *lib) {
  136. NSUnLinkModule((NSModule)lib, NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES);
  137. }
  138. static void *ll_load (lua_State *L, const char *path) {
  139. NSObjectFileImage img;
  140. NSObjectFileImageReturnCode ret;
  141. /* this would be a rare case, but prevents crashing if it happens */
  142. if(!_dyld_present()) {
  143. lua_pushliteral(L, "dyld not present");
  144. return NULL;
  145. }
  146. ret = NSCreateObjectFileImageFromFile(path, &img);
  147. if (ret == NSObjectFileImageSuccess) {
  148. NSModule mod = NSLinkModule(img, path, NSLINKMODULE_OPTION_PRIVATE |
  149. NSLINKMODULE_OPTION_RETURN_ON_ERROR);
  150. NSDestroyObjectFileImage(img);
  151. if (mod == NULL) pusherror(L);
  152. return mod;
  153. }
  154. lua_pushstring(L, errorfromcode(ret));
  155. return NULL;
  156. }
  157. static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
  158. NSSymbol nss = NSLookupSymbolInModule((NSModule)lib, sym);
  159. if (nss == NULL) {
  160. lua_pushfstring(L, "symbol " LUA_QS " not found", sym);
  161. return NULL;
  162. }
  163. return (lua_CFunction)NSAddressOfSymbol(nss);
  164. }
  165. /* }====================================================== */
  166. #else
  167. /*
  168. ** {======================================================
  169. ** Fallback for other systems
  170. ** =======================================================
  171. */
  172. #undef LIB_FAIL
  173. #define LIB_FAIL "absent"
  174. #if defined(__ELF__) || defined(__sun) || defined(sgi) || defined(__hpux)
  175. #define DLMSG LUA_QL("loadlib") " not enabled; check your Lua installation"
  176. #else
  177. #define DLMSG LUA_QL("loadlib") " not supported"
  178. #endif
  179. static void ll_unloadlib (void *lib) {
  180. (void)lib; /* to avoid warnings */
  181. }
  182. static void *ll_load (lua_State *L, const char *path) {
  183. (void)path; /* to avoid warnings */
  184. lua_pushliteral(L, DLMSG);
  185. return NULL;
  186. }
  187. static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
  188. (void)lib; (void)sym; /* to avoid warnings */
  189. lua_pushliteral(L, DLMSG);
  190. return NULL;
  191. }
  192. /* }====================================================== */
  193. #endif
  194. static void **ll_register (lua_State *L, const char *path) {
  195. void **plib;
  196. lua_pushfstring(L, "%s%s", LIBPREFIX, path);
  197. lua_gettable(L, LUA_REGISTRYINDEX); /* check library in registry? */
  198. if (!lua_isnil(L, -1)) /* is there an entry? */
  199. plib = (void **)lua_touserdata(L, -1);
  200. else { /* no entry yet; create one */
  201. lua_pop(L, 1);
  202. plib = (void **)lua_newuserdata(L, sizeof(const void *));
  203. *plib = NULL;
  204. luaL_getmetatable(L, "_LOADLIB");
  205. lua_setmetatable(L, -2);
  206. lua_pushfstring(L, "%s%s", LIBPREFIX, path);
  207. lua_pushvalue(L, -2);
  208. lua_settable(L, LUA_REGISTRYINDEX);
  209. }
  210. return plib;
  211. }
  212. /*
  213. ** __gc tag method: calls library's `ll_unloadlib' function with the lib
  214. ** handle
  215. */
  216. static int gctm (lua_State *L) {
  217. void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB");
  218. if (*lib) ll_unloadlib(*lib);
  219. *lib = NULL; /* mark library as closed */
  220. return 0;
  221. }
  222. static int ll_loadfunc (lua_State *L, const char *path, const char *sym) {
  223. void **reg = ll_register(L, path);
  224. if (*reg == NULL) *reg = ll_load(L, path);
  225. if (*reg == NULL)
  226. return ERRLIB; /* unable to load library */
  227. else {
  228. lua_CFunction f = ll_sym(L, *reg, sym);
  229. if (f == NULL)
  230. return ERRFUNC; /* unable to find function */
  231. lua_pushcfunction(L, f);
  232. return 0; /* return function */
  233. }
  234. }
  235. static int ll_loadlib (lua_State *L) {
  236. const char *path = luaL_checkstring(L, 1);
  237. const char *init = luaL_checkstring(L, 2);
  238. int stat = ll_loadfunc(L, path, init);
  239. if (stat == 0) /* no errors? */
  240. return 1; /* return the loaded function */
  241. else { /* error; error message is on stack top */
  242. lua_pushnil(L);
  243. lua_insert(L, -2);
  244. lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init");
  245. return 3; /* return nil, error message, and where */
  246. }
  247. }
  248. /*
  249. ** {======================================================
  250. ** 'require' function
  251. ** =======================================================
  252. */
  253. static int readable (const char *filename) {
  254. FILE *f = fopen(filename, "r"); /* try to open file */
  255. if (f == NULL) return 0; /* open failed */
  256. fclose(f);
  257. return 1;
  258. }
  259. static const char *pushnexttemplate (lua_State *L, const char *path) {
  260. const char *l;
  261. while (*path == *LUA_PATHSEP) path++; /* skip separators */
  262. if (*path == '\0') return NULL; /* no more templates */
  263. l = strchr(path, *LUA_PATHSEP); /* find next separator */
  264. if (l == NULL) l = path + strlen(path);
  265. lua_pushlstring(L, path, l - path); /* template */
  266. return l;
  267. }
  268. static const char *findfile (lua_State *L, const char *name,
  269. const char *pname) {
  270. const char *path;
  271. name = luaL_gsub(L, name, ".", LUA_DIRSEP);
  272. lua_getfield(L, LUA_ENVIRONINDEX, pname);
  273. path = lua_tostring(L, -1);
  274. if (path == NULL)
  275. luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
  276. while ((path = pushnexttemplate(L, path)) != NULL) {
  277. const char *filename;
  278. filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name);
  279. if (readable(filename)) /* does file exist and is readable? */
  280. return filename; /* return that file name */
  281. lua_pop(L, 2); /* remove path template and file name */
  282. }
  283. return NULL; /* not found */
  284. }
  285. static void loaderror (lua_State *L) {
  286. luaL_error(L, "error loading module " LUA_QS " (%s)",
  287. lua_tostring(L, 1), lua_tostring(L, -1));
  288. }
  289. static int loader_Lua (lua_State *L) {
  290. const char *filename;
  291. const char *name = luaL_checkstring(L, 1);
  292. filename = findfile(L, name, "path");
  293. if (filename == NULL) return 0; /* library not found in this path */
  294. if (luaL_loadfile(L, filename) != 0)
  295. loaderror(L);
  296. return 1; /* library loaded successfully */
  297. }
  298. static const char *mkfuncname (lua_State *L, const char *modname) {
  299. const char *funcname;
  300. const char *mark = strchr(modname, *LUA_IGMARK);
  301. if (mark) modname = mark + 1;
  302. funcname = luaL_gsub(L, modname, ".", LUA_OFSEP);
  303. funcname = lua_pushfstring(L, POF"%s", funcname);
  304. lua_remove(L, -2); /* remove 'gsub' result */
  305. return funcname;
  306. }
  307. static int loader_C (lua_State *L) {
  308. const char *funcname;
  309. const char *name = luaL_checkstring(L, 1);
  310. const char *filename = findfile(L, name, "cpath");
  311. if (filename == NULL) return 0; /* library not found in this path */
  312. funcname = mkfuncname(L, name);
  313. if (ll_loadfunc(L, filename, funcname) != 0)
  314. loaderror(L);
  315. return 1; /* library loaded successfully */
  316. }
  317. static int loader_Croot (lua_State *L) {
  318. const char *funcname;
  319. const char *filename;
  320. const char *name = luaL_checkstring(L, 1);
  321. const char *p = strchr(name, '.');
  322. int stat;
  323. if (p == NULL) return 0; /* is root */
  324. lua_pushlstring(L, name, p - name);
  325. filename = findfile(L, lua_tostring(L, -1), "cpath");
  326. if (filename == NULL) return 0; /* root not found */
  327. funcname = mkfuncname(L, name);
  328. if ((stat = ll_loadfunc(L, filename, funcname)) != 0) {
  329. if (stat == ERRFUNC) return 0; /* function not found */
  330. else
  331. loaderror(L); /* real error */
  332. }
  333. return 1;
  334. }
  335. static int loader_preload (lua_State *L) {
  336. lua_getfield(L, LUA_ENVIRONINDEX, "preload");
  337. if (!lua_istable(L, -1))
  338. luaL_error(L, LUA_QL("package.preload") " must be a table");
  339. lua_getfield(L, -1, luaL_checkstring(L, 1));
  340. return 1;
  341. }
  342. static const int sentinel_ = 0;
  343. #define sentinel ((void *)&sentinel_)
  344. static int ll_require (lua_State *L) {
  345. const char *name = luaL_checkstring(L, 1);
  346. int i;
  347. lua_settop(L, 1); /* _LOADED table will be at index 2 */
  348. lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
  349. lua_getfield(L, 2, name);
  350. if (lua_toboolean(L, -1)) { /* is it there? */
  351. if (lua_touserdata(L, -1) == sentinel) /* check loops */
  352. luaL_error(L, "loop or previous error loading module " LUA_QS, name);
  353. return 1; /* package is already loaded */
  354. }
  355. /* else must load it; iterate over available loaders */
  356. lua_getfield(L, LUA_ENVIRONINDEX, "loaders");
  357. if (!lua_istable(L, -1))
  358. luaL_error(L, LUA_QL("package.loaders") " must be a table");
  359. for (i=1; ; i++) {
  360. lua_rawgeti(L, -1, i); /* get a loader */
  361. if (lua_isnil(L, -1))
  362. luaL_error(L, "module " LUA_QS " not found", name);
  363. lua_pushstring(L, name);
  364. lua_call(L, 1, 1); /* call it */
  365. if (lua_isnil(L, -1)) lua_pop(L, 1); /* did not found module */
  366. else break; /* module loaded successfully */
  367. }
  368. lua_pushlightuserdata(L, sentinel);
  369. lua_setfield(L, 2, name); /* _LOADED[name] = sentinel */
  370. lua_pushstring(L, name); /* pass name as argument to module */
  371. lua_call(L, 1, 1); /* run loaded module */
  372. if (!lua_isnil(L, -1)) /* non-nil return? */
  373. lua_setfield(L, 2, name); /* _LOADED[name] = returned value */
  374. lua_getfield(L, 2, name);
  375. if (lua_touserdata(L, -1) == sentinel) { /* module did not set a value? */
  376. lua_pushboolean(L, 1); /* use true as result */
  377. lua_pushvalue(L, -1); /* extra copy to be returned */
  378. lua_setfield(L, 2, name); /* _LOADED[name] = true */
  379. }
  380. return 1;
  381. }
  382. /* }====================================================== */
  383. /*
  384. ** {======================================================
  385. ** 'module' function
  386. ** =======================================================
  387. */
  388. static void setfenv (lua_State *L) {
  389. lua_Debug ar;
  390. lua_getstack(L, 1, &ar);
  391. lua_getinfo(L, "f", &ar);
  392. lua_pushvalue(L, -2);
  393. lua_setfenv(L, -2);
  394. lua_pop(L, 1);
  395. }
  396. static void dooptions (lua_State *L, int n) {
  397. int i;
  398. for (i = 2; i <= n; i++) {
  399. lua_pushvalue(L, i); /* get option (a function) */
  400. lua_pushvalue(L, -2); /* module */
  401. lua_call(L, 1, 0);
  402. }
  403. }
  404. static void modinit (lua_State *L, const char *modname) {
  405. const char *dot;
  406. lua_pushvalue(L, -1);
  407. lua_setfield(L, -2, "_M"); /* module._M = module */
  408. lua_pushstring(L, modname);
  409. lua_setfield(L, -2, "_NAME");
  410. dot = strrchr(modname, '.'); /* look for last dot in module name */
  411. if (dot == NULL) dot = modname;
  412. else dot++;
  413. /* set _PACKAGE as package name (full module name minus last part) */
  414. lua_pushlstring(L, modname, dot - modname);
  415. lua_setfield(L, -2, "_PACKAGE");
  416. }
  417. static int ll_module (lua_State *L) {
  418. const char *modname = luaL_checkstring(L, 1);
  419. int loaded = lua_gettop(L) + 1; /* index of _LOADED table */
  420. lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
  421. lua_getfield(L, loaded, modname); /* get _LOADED[modname] */
  422. if (!lua_istable(L, -1)) { /* not found? */
  423. lua_pop(L, 1); /* remove previous result */
  424. /* try global variable (and create one if it does not exist) */
  425. if (luaL_findtable(L, LUA_GLOBALSINDEX, modname) != NULL)
  426. return luaL_error(L, "name conflict for module " LUA_QS, modname);
  427. lua_pushvalue(L, -1);
  428. lua_setfield(L, loaded, modname); /* _LOADED[modname] = new table */
  429. }
  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. setfenv(L);
  440. dooptions(L, loaded - 1);
  441. return 0;
  442. }
  443. static int ll_seeall (lua_State *L) {
  444. luaL_checktype(L, 1, LUA_TTABLE);
  445. if (!lua_getmetatable(L, 1)) {
  446. lua_newtable(L); /* create new metatable */
  447. lua_pushvalue(L, -1);
  448. lua_setmetatable(L, 1);
  449. }
  450. lua_pushvalue(L, LUA_GLOBALSINDEX);
  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_PATHSEP LUA_PATHSEP,
  465. LUA_PATHSEP AUXMARK LUA_PATHSEP);
  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. {"seeall", ll_seeall},
  475. {NULL, NULL}
  476. };
  477. static const luaL_Reg ll_funcs[] = {
  478. {"module", ll_module},
  479. {"require", ll_require},
  480. {NULL, NULL}
  481. };
  482. static const lua_CFunction loaders[] =
  483. {loader_preload, loader_Lua, loader_C, loader_Croot, NULL};
  484. LUALIB_API int luaopen_package (lua_State *L) {
  485. int i;
  486. /* create new type _LOADLIB */
  487. luaL_newmetatable(L, "_LOADLIB");
  488. lua_pushcfunction(L, gctm);
  489. lua_setfield(L, -2, "__gc");
  490. /* create `package' table */
  491. luaL_register(L, LUA_LOADLIBNAME, pk_funcs);
  492. #if defined(LUA_COMPAT_LOADLIB)
  493. lua_getfield(L, -1, "loadlib");
  494. lua_setfield(L, LUA_GLOBALSINDEX, "loadlib");
  495. #endif
  496. lua_pushvalue(L, -1);
  497. lua_replace(L, LUA_ENVIRONINDEX);
  498. /* create `loaders' table */
  499. lua_newtable(L);
  500. /* fill it with pre-defined loaders */
  501. for (i=0; loaders[i] != NULL; i++) {
  502. lua_pushcfunction(L, loaders[i]);
  503. lua_rawseti(L, -2, i+1);
  504. }
  505. lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */
  506. setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT); /* set field `path' */
  507. setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT); /* set field `cpath' */
  508. /* store config information */
  509. lua_pushstring(L, LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n"
  510. LUA_EXECDIR "\n" LUA_IGMARK);
  511. lua_setfield(L, -2, "config");
  512. /* set field `loaded' */
  513. luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED");
  514. lua_setfield(L, -2, "loaded");
  515. /* set field `preload' */
  516. lua_newtable(L);
  517. lua_setfield(L, -2, "preload");
  518. lua_pushvalue(L, LUA_GLOBALSINDEX);
  519. luaL_register(L, NULL, ll_funcs); /* open lib into global table */
  520. lua_pop(L, 1);
  521. return 1; /* return 'package' table */
  522. }