loadlib.c 18 KB

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