loadlib.c 20 KB

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