loadlib.c 19 KB

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