loadlib.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /*
  2. ** $Id: loadlib.c,v 1.91 2010/09/07 19:21:39 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_getmetatable(L, "_LOADLIB");
  178. lua_setmetatable(L, -2);
  179. lua_pushfstring(L, "%s%s", LIBPREFIX, path);
  180. lua_pushvalue(L, -2);
  181. lua_settable(L, LUA_REGISTRYINDEX);
  182. }
  183. return plib;
  184. }
  185. /*
  186. ** __gc tag method: calls library's `ll_unloadlib' function with the lib
  187. ** handle
  188. */
  189. static int gctm (lua_State *L) {
  190. void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB");
  191. if (*lib) ll_unloadlib(*lib);
  192. *lib = NULL; /* mark library as closed */
  193. return 0;
  194. }
  195. static int ll_loadfunc (lua_State *L, const char *path, const char *sym) {
  196. void **reg = ll_register(L, path);
  197. if (*reg == NULL) *reg = ll_load(L, path, *sym == '*');
  198. if (*reg == NULL) return ERRLIB; /* unable to load library */
  199. if (*sym == '*') { /* loading only library (no function)? */
  200. lua_pushboolean(L, 1); /* return 'true' */
  201. return 0; /* no errors */
  202. }
  203. else {
  204. lua_CFunction f = ll_sym(L, *reg, sym);
  205. if (f == NULL)
  206. return ERRFUNC; /* unable to find function */
  207. lua_pushcfunction(L, f); /* else create new function */
  208. return 0; /* no errors */
  209. }
  210. }
  211. static int ll_loadlib (lua_State *L) {
  212. const char *path = luaL_checkstring(L, 1);
  213. const char *init = luaL_checkstring(L, 2);
  214. int stat = ll_loadfunc(L, path, init);
  215. if (stat == 0) /* no errors? */
  216. return 1; /* return the loaded function */
  217. else { /* error; error message is on stack top */
  218. lua_pushnil(L);
  219. lua_insert(L, -2);
  220. lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init");
  221. return 3; /* return nil, error message, and where */
  222. }
  223. }
  224. /*
  225. ** {======================================================
  226. ** 'require' function
  227. ** =======================================================
  228. */
  229. static int readable (const char *filename) {
  230. FILE *f = fopen(filename, "r"); /* try to open file */
  231. if (f == NULL) return 0; /* open failed */
  232. fclose(f);
  233. return 1;
  234. }
  235. static const char *pushnexttemplate (lua_State *L, const char *path) {
  236. const char *l;
  237. while (*path == *LUA_PATH_SEP) path++; /* skip separators */
  238. if (*path == '\0') return NULL; /* no more templates */
  239. l = strchr(path, *LUA_PATH_SEP); /* find next separator */
  240. if (l == NULL) l = path + strlen(path);
  241. lua_pushlstring(L, path, l - path); /* template */
  242. return l;
  243. }
  244. static const char *searchpath (lua_State *L, const char *name,
  245. const char *path) {
  246. name = luaL_gsub(L, name, ".", LUA_DIRSEP);
  247. lua_pushliteral(L, ""); /* error accumulator */
  248. while ((path = pushnexttemplate(L, path)) != NULL) {
  249. const char *filename = luaL_gsub(L, lua_tostring(L, -1),
  250. LUA_PATH_MARK, name);
  251. lua_remove(L, -2); /* remove path template */
  252. if (readable(filename)) /* does file exist and is readable? */
  253. return filename; /* return that file name */
  254. lua_pushfstring(L, "\n\tno file " LUA_QS, filename);
  255. lua_remove(L, -2); /* remove file name */
  256. lua_concat(L, 2); /* add entry to possible error message */
  257. }
  258. return NULL; /* not found */
  259. }
  260. static int ll_searchpath (lua_State *L) {
  261. const char *f = searchpath(L, luaL_checkstring(L, 1), luaL_checkstring(L, 2));
  262. if (f != NULL) return 1;
  263. else { /* error message is on top of the stack */
  264. lua_pushnil(L);
  265. lua_insert(L, -2);
  266. return 2; /* return nil + error message */
  267. }
  268. }
  269. static const char *findfile (lua_State *L, const char *name,
  270. const char *pname) {
  271. const char *path;
  272. lua_getfield(L, lua_upvalueindex(1), 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. return searchpath(L, name, path);
  277. }
  278. static void loaderror (lua_State *L, const char *filename) {
  279. luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s",
  280. lua_tostring(L, 1), filename, lua_tostring(L, -1));
  281. }
  282. static int loader_Lua (lua_State *L) {
  283. const char *filename;
  284. const char *name = luaL_checkstring(L, 1);
  285. filename = findfile(L, name, "path");
  286. if (filename == NULL) return 1; /* library not found in this path */
  287. if (luaL_loadfile(L, filename) != LUA_OK)
  288. loaderror(L, filename);
  289. return 1; /* library loaded successfully */
  290. }
  291. static int loadfunc (lua_State *L, const char *filename, const char *modname) {
  292. const char *funcname;
  293. const char *mark;
  294. modname = luaL_gsub(L, modname, ".", LUA_OFSEP);
  295. mark = strchr(modname, *LUA_IGMARK);
  296. if (mark) {
  297. int stat;
  298. funcname = lua_pushlstring(L, modname, mark - modname);
  299. funcname = lua_pushfstring(L, POF"%s", funcname);
  300. stat = ll_loadfunc(L, filename, funcname);
  301. if (stat != ERRFUNC) return stat;
  302. modname = mark + 1; /* else go ahead and try old-style name */
  303. }
  304. funcname = lua_pushfstring(L, POF"%s", modname);
  305. return ll_loadfunc(L, filename, funcname);
  306. }
  307. static int loader_C (lua_State *L) {
  308. const char *name = luaL_checkstring(L, 1);
  309. const char *filename = findfile(L, name, "cpath");
  310. if (filename == NULL) return 1; /* library not found in this path */
  311. if (loadfunc(L, filename, name) != 0)
  312. loaderror(L, filename);
  313. return 1; /* library loaded successfully */
  314. }
  315. static int loader_Croot (lua_State *L) {
  316. const char *filename;
  317. const char *name = luaL_checkstring(L, 1);
  318. const char *p = strchr(name, '.');
  319. int stat;
  320. if (p == NULL) return 0; /* is root */
  321. lua_pushlstring(L, name, p - name);
  322. filename = findfile(L, lua_tostring(L, -1), "cpath");
  323. if (filename == NULL) return 1; /* root not found */
  324. if ((stat = loadfunc(L, filename, name)) != 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_REGISTRYINDEX, "_PRELOAD");
  335. lua_getfield(L, -1, name);
  336. if (lua_isnil(L, -1)) /* not found? */
  337. lua_pushfstring(L, "\n\tno field package.preload['%s']", name);
  338. return 1;
  339. }
  340. static const int sentinel_ = 0;
  341. #define sentinel ((void *)&sentinel_)
  342. static int ll_require (lua_State *L) {
  343. const char *name = luaL_checkstring(L, 1);
  344. int i;
  345. lua_settop(L, 1); /* _LOADED table will be at index 2 */
  346. lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
  347. lua_getfield(L, 2, name);
  348. if (lua_toboolean(L, -1)) { /* is it there? */
  349. if (lua_touserdata(L, -1) == sentinel) /* check loops */
  350. luaL_error(L, "loop or previous error loading module " LUA_QS, name);
  351. return 1; /* package is already loaded */
  352. }
  353. /* else must load it; iterate over available loaders */
  354. lua_getfield(L, lua_upvalueindex(1), "loaders");
  355. if (!lua_istable(L, -1))
  356. luaL_error(L, LUA_QL("package.loaders") " must be a table");
  357. lua_pushliteral(L, ""); /* error message accumulator */
  358. for (i=1; ; i++) {
  359. lua_rawgeti(L, -2, i); /* get a loader */
  360. if (lua_isnil(L, -1))
  361. luaL_error(L, "module " LUA_QS " not found:%s",
  362. name, lua_tostring(L, -2));
  363. lua_pushstring(L, name);
  364. lua_call(L, 1, 1); /* call it */
  365. if (lua_isfunction(L, -1)) /* did it find module? */
  366. break; /* module loaded successfully */
  367. else if (lua_isstring(L, -1)) /* loader returned error message? */
  368. lua_concat(L, 2); /* accumulate it */
  369. else
  370. lua_pop(L, 1);
  371. }
  372. lua_pushlightuserdata(L, sentinel);
  373. lua_setfield(L, 2, name); /* _LOADED[name] = sentinel */
  374. lua_pushstring(L, name); /* pass name as argument to module */
  375. lua_call(L, 1, 1); /* run loaded module */
  376. if (!lua_isnil(L, -1)) /* non-nil return? */
  377. lua_setfield(L, 2, name); /* _LOADED[name] = returned value */
  378. lua_getfield(L, 2, name);
  379. if (lua_touserdata(L, -1) == sentinel) { /* module did not set a value? */
  380. lua_pushboolean(L, 1); /* use true as result */
  381. lua_pushvalue(L, -1); /* extra copy to be returned */
  382. lua_setfield(L, 2, name); /* _LOADED[name] = true */
  383. }
  384. return 1;
  385. }
  386. /* }====================================================== */
  387. /*
  388. ** {======================================================
  389. ** 'module' function
  390. ** =======================================================
  391. */
  392. #if defined(LUA_COMPAT_MODULE)
  393. /*
  394. ** changes the environment variable of calling function
  395. */
  396. static void set_env (lua_State *L) {
  397. lua_Debug ar;
  398. if (lua_getstack(L, 1, &ar) == 0 ||
  399. lua_getinfo(L, "f", &ar) == 0 || /* get calling function */
  400. lua_iscfunction(L, -1))
  401. luaL_error(L, LUA_QL("module") " not called from a Lua function");
  402. lua_pushvalue(L, -2); /* copy new environment table to top */
  403. lua_setupvalue(L, -2, 1);
  404. lua_pop(L, 1); /* remove function */
  405. }
  406. static void dooptions (lua_State *L, int n) {
  407. int i;
  408. for (i = 2; i <= n; i++) {
  409. lua_pushvalue(L, i); /* get option (a function) */
  410. lua_pushvalue(L, -2); /* module */
  411. lua_call(L, 1, 0);
  412. }
  413. }
  414. static void modinit (lua_State *L, const char *modname) {
  415. const char *dot;
  416. lua_pushvalue(L, -1);
  417. lua_setfield(L, -2, "_M"); /* module._M = module */
  418. lua_pushstring(L, modname);
  419. lua_setfield(L, -2, "_NAME");
  420. dot = strrchr(modname, '.'); /* look for last dot in module name */
  421. if (dot == NULL) dot = modname;
  422. else dot++;
  423. /* set _PACKAGE as package name (full module name minus last part) */
  424. lua_pushlstring(L, modname, dot - modname);
  425. lua_setfield(L, -2, "_PACKAGE");
  426. }
  427. static int ll_module (lua_State *L) {
  428. const char *modname = luaL_checkstring(L, 1);
  429. int lastarg = lua_gettop(L); /* last parameter */
  430. luaL_pushmodule(L, modname, 1); /* get/create module table */
  431. /* check whether table already has a _NAME field */
  432. lua_getfield(L, -1, "_NAME");
  433. if (!lua_isnil(L, -1)) /* is table an initialized module? */
  434. lua_pop(L, 1);
  435. else { /* no; initialize it */
  436. lua_pop(L, 1);
  437. modinit(L, modname);
  438. }
  439. lua_pushvalue(L, -1);
  440. set_env(L);
  441. dooptions(L, lastarg);
  442. return 1;
  443. }
  444. static int ll_seeall (lua_State *L) {
  445. luaL_checktype(L, 1, LUA_TTABLE);
  446. if (!lua_getmetatable(L, 1)) {
  447. lua_createtable(L, 0, 1); /* create new metatable */
  448. lua_pushvalue(L, -1);
  449. lua_setmetatable(L, 1);
  450. }
  451. lua_pushglobaltable(L);
  452. lua_setfield(L, -2, "__index"); /* mt.__index = _G */
  453. return 0;
  454. }
  455. #else
  456. static int ll_seeall (lua_State *L) {
  457. return luaL_error(L, "deprecated function");
  458. }
  459. static int ll_module (lua_State *L) {
  460. return luaL_error(L, "deprecated function");
  461. }
  462. #endif
  463. /* }====================================================== */
  464. /* auxiliary mark (for internal use) */
  465. #define AUXMARK "\1"
  466. static void setpath (lua_State *L, const char *fieldname, const char *envname1,
  467. const char *envname2, const char *def) {
  468. const char *path = getenv(envname1);
  469. if (path == NULL) /* no environment variable? */
  470. path = getenv(envname2); /* try alternative name */
  471. if (path == NULL) /* no environment variable? */
  472. lua_pushstring(L, def); /* use default */
  473. else {
  474. /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */
  475. path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP,
  476. LUA_PATH_SEP AUXMARK LUA_PATH_SEP);
  477. luaL_gsub(L, path, AUXMARK, def);
  478. lua_remove(L, -2);
  479. }
  480. setprogdir(L);
  481. lua_setfield(L, -2, fieldname);
  482. }
  483. static const luaL_Reg pk_funcs[] = {
  484. {"loadlib", ll_loadlib},
  485. {"searchpath", ll_searchpath},
  486. {"seeall", ll_seeall},
  487. {NULL, NULL}
  488. };
  489. static const luaL_Reg ll_funcs[] = {
  490. {"module", ll_module},
  491. {"require", ll_require},
  492. {NULL, NULL}
  493. };
  494. static const lua_CFunction loaders[] =
  495. {loader_preload, loader_Lua, loader_C, loader_Croot, NULL};
  496. LUAMOD_API int luaopen_package (lua_State *L) {
  497. int i;
  498. /* create new type _LOADLIB */
  499. luaL_newmetatable(L, "_LOADLIB");
  500. lua_pushcfunction(L, gctm);
  501. lua_setfield(L, -2, "__gc");
  502. /* create `package' table */
  503. luaL_newlib(L, pk_funcs);
  504. /* create `loaders' table */
  505. lua_createtable(L, sizeof(loaders)/sizeof(loaders[0]) - 1, 0);
  506. /* fill it with pre-defined loaders */
  507. for (i=0; loaders[i] != NULL; i++) {
  508. lua_pushvalue(L, -2); /* set 'package' as upvalue for all loaders */
  509. lua_pushcclosure(L, loaders[i], 1);
  510. lua_rawseti(L, -2, i+1);
  511. }
  512. lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */
  513. /* set field 'path' */
  514. setpath(L, "path", LUA_PATHVERSION, LUA_PATH, LUA_PATH_DEFAULT);
  515. /* set field 'cpath' */
  516. setpath(L, "cpath", LUA_CPATHVERSION, LUA_CPATH, LUA_CPATH_DEFAULT);
  517. /* store config information */
  518. lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n"
  519. LUA_EXEC_DIR "\n" LUA_IGMARK "\n");
  520. lua_setfield(L, -2, "config");
  521. /* set field `loaded' */
  522. luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED");
  523. lua_setfield(L, -2, "loaded");
  524. /* set field `preload' */
  525. luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
  526. lua_setfield(L, -2, "preload");
  527. lua_pushglobaltable(L);
  528. lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */
  529. luaL_setfuncs(L, ll_funcs, 1); /* open lib into global table */
  530. lua_pop(L, 1); /* pop global table */
  531. return 1; /* return 'package' table */
  532. }