loadlib.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. #define loadlib_c
  11. #define LUA_LIB
  12. #include "lua.h"
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <fcntl.h>
  16. #ifndef LUA_CROSS_COMPILER
  17. #include "vfs.h"
  18. #endif
  19. #include "lauxlib.h"
  20. #include "lualib.h"
  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 = GetModuleFileNameA(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 (FormatMessageA(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 = LoadLibraryA(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. #ifdef LUA_CROSS_COMPILER
  251. static int readable (const char *filename) {
  252. FILE *f = fopen(filename, "r"); /* try to open file */
  253. if (f == NULL) return 0; /* open failed */
  254. fclose(f);
  255. return 1;
  256. }
  257. #else
  258. static int readable (const char *filename) {
  259. int f = vfs_open(filename, "r"); /* try to open file */
  260. if (!f) return 0; /* open failed */
  261. vfs_close(f);
  262. return 1;
  263. }
  264. #endif
  265. static const char * pushnexttemplate (lua_State *L, const char *path) {
  266. const char *l;
  267. while (*path == *LUA_PATHSEP) path++; /* skip separators */
  268. if (*path == '\0') return NULL; /* no more templates */
  269. l = strchr(path, *LUA_PATHSEP); /* find next separator */
  270. if (l == NULL) l = path + strlen(path);
  271. lua_pushlstring(L, path, l - path); /* template */
  272. return l;
  273. }
  274. static const char * findfile (lua_State *L, const char *name,
  275. const char *pname) {
  276. const char *path;
  277. name = luaL_gsub(L, name, ".", LUA_DIRSEP);
  278. lua_getfield(L, LUA_GLOBALSINDEX, "package");
  279. lua_getfield(L, -1, pname);
  280. lua_remove(L, -2);
  281. path = lua_tostring(L, -1);
  282. if (path == NULL)
  283. luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
  284. lua_pushliteral(L, ""); /* error accumulator */
  285. while ((path = pushnexttemplate(L, path)) != NULL) {
  286. const char *filename;
  287. filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name);
  288. lua_remove(L, -2); /* remove path template */
  289. if (readable(filename)) /* does file exist and is readable? */
  290. return filename; /* return that file name */
  291. lua_pushfstring(L, "\n\tno file " LUA_QS, filename);
  292. lua_remove(L, -2); /* remove file name */
  293. lua_concat(L, 2); /* add entry to possible error message */
  294. }
  295. return NULL; /* not found */
  296. }
  297. static void loaderror (lua_State *L, const char *filename) {
  298. luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s",
  299. lua_tostring(L, 1), filename, lua_tostring(L, -1));
  300. }
  301. static int loader_Lua (lua_State *L) {
  302. const char *filename;
  303. const char *name = luaL_checkstring(L, 1);
  304. filename = findfile(L, name, "path");
  305. if (filename == NULL) return 1; /* library not found in this path */
  306. if (luaL_loadfile(L, filename) != 0)
  307. loaderror(L, filename);
  308. return 1; /* library loaded successfully */
  309. }
  310. static const char *mkfuncname (lua_State *L, const char *modname) {
  311. const char *funcname;
  312. const char *mark = strchr(modname, *LUA_IGMARK);
  313. if (mark) modname = mark + 1;
  314. funcname = luaL_gsub(L, modname, ".", LUA_OFSEP);
  315. funcname = lua_pushfstring(L, POF"%s", funcname);
  316. lua_remove(L, -2); /* remove 'gsub' result */
  317. return funcname;
  318. }
  319. static int loader_C (lua_State *L) {
  320. const char *funcname;
  321. const char *name = luaL_checkstring(L, 1);
  322. const char *filename = findfile(L, name, "cpath");
  323. if (filename == NULL) return 1; /* library not found in this path */
  324. funcname = mkfuncname(L, name);
  325. if (ll_loadfunc(L, filename, funcname) != 0)
  326. loaderror(L, filename);
  327. return 1; /* library loaded successfully */
  328. }
  329. static int loader_Croot (lua_State *L) {
  330. const char *funcname;
  331. const char *filename;
  332. const char *name = luaL_checkstring(L, 1);
  333. const char *p = strchr(name, '.');
  334. int stat;
  335. if (p == NULL) return 0; /* is root */
  336. lua_pushlstring(L, name, p - name);
  337. filename = findfile(L, lua_tostring(L, -1), "cpath");
  338. if (filename == NULL) return 1; /* root not found */
  339. funcname = mkfuncname(L, name);
  340. if ((stat = ll_loadfunc(L, filename, funcname)) != 0) {
  341. if (stat != ERRFUNC) loaderror(L, filename); /* real error */
  342. lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS,
  343. name, filename);
  344. return 1; /* function not found */
  345. }
  346. return 1;
  347. }
  348. static int loader_preload (lua_State *L) {
  349. const char *name = luaL_checkstring(L, 1);
  350. lua_getfield(L, LUA_GLOBALSINDEX, "package");
  351. lua_getfield(L, -1, "preload");
  352. lua_remove(L, -2);
  353. if (!lua_istable(L, -1))
  354. luaL_error(L, LUA_QL("package.preload") " must be a table");
  355. lua_getfield(L, -1, name);
  356. if (lua_isnil(L, -1)) /* not found? */
  357. lua_pushfstring(L, "\n\tno field package.preload['%s']", name);
  358. return 1;
  359. }
  360. static const int sentinel_ = 0;
  361. #define sentinel ((void *)&sentinel_)
  362. static int ll_require (lua_State *L) {
  363. const char *name = luaL_checkstring(L, 1);
  364. int i;
  365. lua_settop(L, 1); /* _LOADED table will be at index 2 */
  366. lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
  367. lua_getfield(L, 2, name);
  368. if (lua_toboolean(L, -1)) { /* is it there? */
  369. if (lua_touserdata(L, -1) == sentinel) /* check loops */
  370. luaL_error(L, "loop or previous error loading module " LUA_QS, name);
  371. return 1; /* package is already loaded */
  372. }
  373. /* Is this a readonly table? */
  374. lua_getfield(L, LUA_GLOBALSINDEX, name);
  375. if(lua_istable(L,-1)) {
  376. return 1;
  377. } else {
  378. lua_pop(L, 1);
  379. }
  380. /* else must load it; iterate over available loaders */
  381. lua_getfield(L, LUA_GLOBALSINDEX, "package");
  382. lua_getfield(L, -1, "loaders");
  383. lua_remove(L, -2);
  384. if (!lua_istable(L, -1))
  385. luaL_error(L, LUA_QL("package.loaders") " must be a table");
  386. lua_pushliteral(L, ""); /* error message accumulator */
  387. for (i=1; ; i++) {
  388. lua_rawgeti(L, -2, i); /* get a loader */
  389. if (lua_isnil(L, -1))
  390. luaL_error(L, "module " LUA_QS " not found:%s",
  391. name, lua_tostring(L, -2));
  392. lua_pushstring(L, name);
  393. lua_call(L, 1, 1); /* call it */
  394. if (lua_isfunction(L, -1)) /* did it find module? */
  395. break; /* module loaded successfully */
  396. else if (lua_isstring(L, -1)) /* loader returned error message? */
  397. lua_concat(L, 2); /* accumulate it */
  398. else
  399. lua_pop(L, 1);
  400. }
  401. lua_pushlightuserdata(L, sentinel);
  402. lua_setfield(L, 2, name); /* _LOADED[name] = sentinel */
  403. lua_pushstring(L, name); /* pass name as argument to module */
  404. lua_call(L, 1, 1); /* run loaded module */
  405. if (!lua_isnil(L, -1)) /* non-nil return? */
  406. lua_setfield(L, 2, name); /* _LOADED[name] = returned value */
  407. lua_getfield(L, 2, name);
  408. if (lua_touserdata(L, -1) == sentinel) { /* module did not set a value? */
  409. lua_pushboolean(L, 1); /* use true as result */
  410. lua_pushvalue(L, -1); /* extra copy to be returned */
  411. lua_setfield(L, 2, name); /* _LOADED[name] = true */
  412. }
  413. return 1;
  414. }
  415. /* }====================================================== */
  416. /*
  417. ** {======================================================
  418. ** 'module' function
  419. ** =======================================================
  420. */
  421. static void setfenv (lua_State *L) {
  422. lua_Debug ar;
  423. if (lua_getstack(L, 1, &ar) == 0 ||
  424. lua_getinfo(L, "f", &ar) == 0 || /* get calling function */
  425. lua_iscfunction(L, -1))
  426. luaL_error(L, LUA_QL("module") " not called from a Lua function");
  427. lua_pushvalue(L, -2);
  428. lua_setfenv(L, -2);
  429. lua_pop(L, 1);
  430. }
  431. static void dooptions (lua_State *L, int n) {
  432. int i;
  433. for (i = 2; i <= n; i++) {
  434. lua_pushvalue(L, i); /* get option (a function) */
  435. lua_pushvalue(L, -2); /* module */
  436. lua_call(L, 1, 0);
  437. }
  438. }
  439. static void modinit (lua_State *L, const char *modname) {
  440. const char *dot;
  441. lua_pushvalue(L, -1);
  442. lua_setfield(L, -2, "_M"); /* module._M = module */
  443. lua_pushstring(L, modname);
  444. lua_setfield(L, -2, "_NAME");
  445. dot = strrchr(modname, '.'); /* look for last dot in module name */
  446. if (dot == NULL) dot = modname;
  447. else dot++;
  448. /* set _PACKAGE as package name (full module name minus last part) */
  449. lua_pushlstring(L, modname, dot - modname);
  450. lua_setfield(L, -2, "_PACKAGE");
  451. }
  452. static int ll_module (lua_State *L) {
  453. const char *modname = luaL_checkstring(L, 1);
  454. /* Is this a readonly table? */
  455. lua_getfield(L, LUA_GLOBALSINDEX, modname);
  456. if(lua_istable(L,-1)) {
  457. return 0;
  458. } else {
  459. lua_pop(L, 1);
  460. }
  461. int loaded = lua_gettop(L) + 1; /* index of _LOADED table */
  462. lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
  463. lua_getfield(L, loaded, modname); /* get _LOADED[modname] */
  464. if (!lua_istable(L, -1)) { /* not found? */
  465. lua_pop(L, 1); /* remove previous result */
  466. /* try global variable (and create one if it does not exist) */
  467. if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, 1) != NULL)
  468. return luaL_error(L, "name conflict for module " LUA_QS, modname);
  469. lua_pushvalue(L, -1);
  470. lua_setfield(L, loaded, modname); /* _LOADED[modname] = new table */
  471. }
  472. /* check whether table already has a _NAME field */
  473. lua_getfield(L, -1, "_NAME");
  474. if (!lua_isnil(L, -1)) /* is table an initialized module? */
  475. lua_pop(L, 1);
  476. else { /* no; initialize it */
  477. lua_pop(L, 1);
  478. modinit(L, modname);
  479. }
  480. lua_pushvalue(L, -1);
  481. setfenv(L);
  482. dooptions(L, loaded - 1);
  483. return 0;
  484. }
  485. static int ll_seeall (lua_State *L) {
  486. luaL_checktype(L, 1, LUA_TTABLE);
  487. if (!lua_getmetatable(L, 1)) {
  488. lua_createtable(L, 0, 1); /* create new metatable */
  489. lua_pushvalue(L, -1);
  490. lua_setmetatable(L, 1);
  491. }
  492. lua_pushvalue(L, LUA_GLOBALSINDEX);
  493. lua_setfield(L, -2, "__index"); /* mt.__index = _G */
  494. return 0;
  495. }
  496. /* }====================================================== */
  497. /* auxiliary mark (for internal use) */
  498. #define AUXMARK "\1"
  499. static void setpath (lua_State *L, const char *fieldname, const char *envname,
  500. const char *def) {
  501. const char *path = NULL; /* getenv(envname) not used in NodeMCU */;
  502. if (path == NULL) /* no environment variable? */
  503. lua_pushstring(L, def); /* use default */
  504. else {
  505. /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */
  506. path = luaL_gsub(L, path, LUA_PATHSEP LUA_PATHSEP,
  507. LUA_PATHSEP AUXMARK LUA_PATHSEP);
  508. luaL_gsub(L, path, AUXMARK, def);
  509. lua_remove(L, -2);
  510. }
  511. setprogdir(L);
  512. lua_setfield(L, -2, fieldname);
  513. }
  514. static const luaL_Reg pk_funcs[] = {
  515. {"loadlib", ll_loadlib},
  516. {"seeall", ll_seeall},
  517. {NULL, NULL}
  518. };
  519. static const luaL_Reg ll_funcs[] = {
  520. {"module", ll_module},
  521. {"require", ll_require},
  522. {NULL, NULL}
  523. };
  524. static const lua_CFunction loaders[] =
  525. {loader_preload, loader_Lua, loader_C, loader_Croot, NULL};
  526. LROT_BEGIN(lmt, NULL, LROT_MASK_GC)
  527. LROT_FUNCENTRY( __gc, gctm )
  528. LROT_END(lmt, NULL, LROT_MASK_GC)
  529. LUALIB_API int luaopen_package (lua_State *L) {
  530. int i;
  531. /* create new type _LOADLIB */
  532. luaL_rometatable(L, "_LOADLIB",LROT_TABLEREF(lmt));
  533. /* create `package' table */
  534. luaL_register_light(L, LUA_LOADLIBNAME, pk_funcs);
  535. #if defined(LUA_COMPAT_LOADLIB)
  536. lua_getfield(L, -1, "loadlib");
  537. lua_setfield(L, LUA_GLOBALSINDEX, "loadlib");
  538. #endif
  539. /* create `loaders' table */
  540. lua_createtable(L, sizeof(loaders)/sizeof(loaders[0]) - 1, 0);
  541. /* fill it with pre-defined loaders */
  542. for (i=0; loaders[i] != NULL; i++) {
  543. lua_pushcfunction(L, loaders[i]);
  544. lua_rawseti(L, -2, i+1);
  545. }
  546. lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */
  547. setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT); /* set field `path' */
  548. setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT); /* set field `cpath' */
  549. /* store config information */
  550. lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n"
  551. LUA_EXECDIR "\n" LUA_IGMARK);
  552. lua_setfield(L, -2, "config");
  553. /* set field `loaded' */
  554. luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 2);
  555. lua_setfield(L, -2, "loaded");
  556. /* set field `preload' */
  557. lua_newtable(L);
  558. lua_setfield(L, -2, "preload");
  559. lua_pushvalue(L, LUA_GLOBALSINDEX);
  560. luaL_register(L, NULL, ll_funcs); /* open lib into global table */
  561. lua_pop(L, 1);
  562. return 1; /* return 'package' table */
  563. }