loadlib.c 19 KB

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