loadlib.c 19 KB

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