loadlib.c 20 KB

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