loadlib.c 18 KB

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