lib_package.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*
  2. ** Package library.
  3. ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
  4. **
  5. ** Major portions taken verbatim or adapted from the Lua interpreter.
  6. ** Copyright (C) 1994-2012 Lua.org, PUC-Rio. See Copyright Notice in lua.h
  7. */
  8. #define lib_package_c
  9. #define LUA_LIB
  10. #include "lua.h"
  11. #include "lauxlib.h"
  12. #include "lualib.h"
  13. #include "lj_obj.h"
  14. #include "lj_err.h"
  15. #include "lj_lib.h"
  16. /* ------------------------------------------------------------------------ */
  17. /* Error codes for ll_loadfunc. */
  18. #define PACKAGE_ERR_LIB 1
  19. #define PACKAGE_ERR_FUNC 2
  20. #define PACKAGE_ERR_LOAD 3
  21. /* Redefined in platform specific part. */
  22. #define PACKAGE_LIB_FAIL "open"
  23. #define setprogdir(L) ((void)0)
  24. /* Symbol name prefixes. */
  25. #define SYMPREFIX_CF "luaopen_%s"
  26. #define SYMPREFIX_BC "luaJIT_BC_%s"
  27. #if LJ_TARGET_DLOPEN
  28. #include <dlfcn.h>
  29. static void ll_unloadlib(void *lib)
  30. {
  31. dlclose(lib);
  32. }
  33. static void *ll_load(lua_State *L, const char *path, int gl)
  34. {
  35. void *lib = dlopen(path, RTLD_NOW | (gl ? RTLD_GLOBAL : RTLD_LOCAL));
  36. if (lib == NULL) lua_pushstring(L, dlerror());
  37. return lib;
  38. }
  39. static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym)
  40. {
  41. lua_CFunction f = (lua_CFunction)dlsym(lib, sym);
  42. if (f == NULL) lua_pushstring(L, dlerror());
  43. return f;
  44. }
  45. static const char *ll_bcsym(void *lib, const char *sym)
  46. {
  47. #if defined(RTLD_DEFAULT) && !defined(NO_RTLD_DEFAULT)
  48. if (lib == NULL) lib = RTLD_DEFAULT;
  49. #elif LJ_TARGET_OSX || LJ_TARGET_BSD
  50. if (lib == NULL) lib = (void *)(intptr_t)-2;
  51. #endif
  52. return (const char *)dlsym(lib, sym);
  53. }
  54. #elif LJ_TARGET_WINDOWS
  55. #define WIN32_LEAN_AND_MEAN
  56. #include <windows.h>
  57. #ifndef GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
  58. #define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS 4
  59. #define GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT 2
  60. BOOL WINAPI GetModuleHandleExA(DWORD, LPCSTR, HMODULE*);
  61. #endif
  62. #if LJ_TARGET_UWP
  63. void *LJ_WIN_LOADLIBA(const char *path)
  64. {
  65. DWORD err = GetLastError();
  66. wchar_t wpath[256];
  67. HANDLE lib = NULL;
  68. if (MultiByteToWideChar(CP_ACP, 0, path, -1, wpath, 256) > 0) {
  69. lib = LoadPackagedLibrary(wpath, 0);
  70. }
  71. SetLastError(err);
  72. return lib;
  73. }
  74. #endif
  75. #undef setprogdir
  76. static void setprogdir(lua_State *L)
  77. {
  78. char buff[MAX_PATH + 1];
  79. char *lb;
  80. DWORD nsize = sizeof(buff);
  81. DWORD n = GetModuleFileNameA(NULL, buff, nsize);
  82. if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) {
  83. luaL_error(L, "unable to get ModuleFileName");
  84. } else {
  85. *lb = '\0';
  86. luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, buff);
  87. lua_remove(L, -2); /* remove original string */
  88. }
  89. }
  90. static void pusherror(lua_State *L)
  91. {
  92. DWORD error = GetLastError();
  93. #if LJ_TARGET_XBOXONE
  94. wchar_t wbuffer[128];
  95. char buffer[128*2];
  96. if (FormatMessageW(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
  97. NULL, error, 0, wbuffer, sizeof(wbuffer)/sizeof(wchar_t), NULL) &&
  98. WideCharToMultiByte(CP_ACP, 0, wbuffer, 128, buffer, 128*2, NULL, NULL))
  99. #else
  100. char buffer[128];
  101. if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
  102. NULL, error, 0, buffer, sizeof(buffer), NULL))
  103. #endif
  104. lua_pushstring(L, buffer);
  105. else
  106. lua_pushfstring(L, "system error %d\n", error);
  107. }
  108. static void ll_unloadlib(void *lib)
  109. {
  110. FreeLibrary((HINSTANCE)lib);
  111. }
  112. static void *ll_load(lua_State *L, const char *path, int gl)
  113. {
  114. HINSTANCE lib = LJ_WIN_LOADLIBA(path);
  115. if (lib == NULL) pusherror(L);
  116. UNUSED(gl);
  117. return lib;
  118. }
  119. static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym)
  120. {
  121. lua_CFunction f = (lua_CFunction)GetProcAddress((HINSTANCE)lib, sym);
  122. if (f == NULL) pusherror(L);
  123. return f;
  124. }
  125. #if LJ_TARGET_UWP
  126. EXTERN_C IMAGE_DOS_HEADER __ImageBase;
  127. #endif
  128. static const char *ll_bcsym(void *lib, const char *sym)
  129. {
  130. if (lib) {
  131. return (const char *)GetProcAddress((HINSTANCE)lib, sym);
  132. } else {
  133. #if LJ_TARGET_UWP
  134. return (const char *)GetProcAddress((HINSTANCE)&__ImageBase, sym);
  135. #else
  136. HINSTANCE h = GetModuleHandleA(NULL);
  137. const char *p = (const char *)GetProcAddress(h, sym);
  138. if (p == NULL && GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
  139. (const char *)ll_bcsym, &h))
  140. p = (const char *)GetProcAddress(h, sym);
  141. return p;
  142. #endif
  143. }
  144. }
  145. #else
  146. #undef PACKAGE_LIB_FAIL
  147. #define PACKAGE_LIB_FAIL "absent"
  148. #define DLMSG "dynamic libraries not enabled; no support for target OS"
  149. static void ll_unloadlib(void *lib)
  150. {
  151. UNUSED(lib);
  152. }
  153. static void *ll_load(lua_State *L, const char *path, int gl)
  154. {
  155. UNUSED(path); UNUSED(gl);
  156. lua_pushliteral(L, DLMSG);
  157. return NULL;
  158. }
  159. static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym)
  160. {
  161. UNUSED(lib); UNUSED(sym);
  162. lua_pushliteral(L, DLMSG);
  163. return NULL;
  164. }
  165. static const char *ll_bcsym(void *lib, const char *sym)
  166. {
  167. UNUSED(lib); UNUSED(sym);
  168. return NULL;
  169. }
  170. #endif
  171. /* ------------------------------------------------------------------------ */
  172. static void **ll_register(lua_State *L, const char *path)
  173. {
  174. void **plib;
  175. lua_pushfstring(L, "LOADLIB: %s", path);
  176. lua_gettable(L, LUA_REGISTRYINDEX); /* check library in registry? */
  177. if (!lua_isnil(L, -1)) { /* is there an entry? */
  178. plib = (void **)lua_touserdata(L, -1);
  179. } else { /* no entry yet; create one */
  180. lua_pop(L, 1);
  181. plib = (void **)lua_newuserdata(L, sizeof(void *));
  182. *plib = NULL;
  183. luaL_setmetatable(L, "_LOADLIB");
  184. lua_pushfstring(L, "LOADLIB: %s", path);
  185. lua_pushvalue(L, -2);
  186. lua_settable(L, LUA_REGISTRYINDEX);
  187. }
  188. return plib;
  189. }
  190. static const char *mksymname(lua_State *L, const char *modname,
  191. const char *prefix)
  192. {
  193. const char *funcname;
  194. const char *mark = strchr(modname, *LUA_IGMARK);
  195. if (mark) modname = mark + 1;
  196. funcname = luaL_gsub(L, modname, ".", "_");
  197. funcname = lua_pushfstring(L, prefix, funcname);
  198. lua_remove(L, -2); /* remove 'gsub' result */
  199. return funcname;
  200. }
  201. static int ll_loadfunc(lua_State *L, const char *path, const char *name, int r)
  202. {
  203. void **reg;
  204. if (strlen(path) >= 4096) {
  205. lua_pushliteral(L, "path too long");
  206. return PACKAGE_ERR_LIB;
  207. }
  208. reg = ll_register(L, path);
  209. if (*reg == NULL) *reg = ll_load(L, path, (*name == '*'));
  210. if (*reg == NULL) {
  211. return PACKAGE_ERR_LIB; /* Unable to load library. */
  212. } else if (*name == '*') { /* Only load library into global namespace. */
  213. lua_pushboolean(L, 1);
  214. return 0;
  215. } else {
  216. const char *sym = r ? name : mksymname(L, name, SYMPREFIX_CF);
  217. lua_CFunction f = ll_sym(L, *reg, sym);
  218. if (f) {
  219. lua_pushcfunction(L, f);
  220. return 0;
  221. }
  222. if (!r) {
  223. const char *bcdata = ll_bcsym(*reg, mksymname(L, name, SYMPREFIX_BC));
  224. lua_pop(L, 1);
  225. if (bcdata) {
  226. if (luaL_loadbuffer(L, bcdata, ~(size_t)0, name) != 0)
  227. return PACKAGE_ERR_LOAD;
  228. return 0;
  229. }
  230. }
  231. return PACKAGE_ERR_FUNC; /* Unable to find function. */
  232. }
  233. }
  234. static int lj_cf_package_loadlib(lua_State *L)
  235. {
  236. const char *path = luaL_checkstring(L, 1);
  237. const char *init = luaL_checkstring(L, 2);
  238. int st = ll_loadfunc(L, path, init, 1);
  239. if (st == 0) { /* no errors? */
  240. return 1; /* return the loaded function */
  241. } else { /* error; error message is on stack top */
  242. lua_pushnil(L);
  243. lua_insert(L, -2);
  244. lua_pushstring(L, (st == PACKAGE_ERR_LIB) ? PACKAGE_LIB_FAIL : "init");
  245. return 3; /* return nil, error message, and where */
  246. }
  247. }
  248. static int lj_cf_package_unloadlib(lua_State *L)
  249. {
  250. void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB");
  251. if (*lib) ll_unloadlib(*lib);
  252. *lib = NULL; /* mark library as closed */
  253. return 0;
  254. }
  255. /* ------------------------------------------------------------------------ */
  256. static int readable(const char *filename)
  257. {
  258. FILE *f = fopen(filename, "r"); /* try to open file */
  259. if (f == NULL) return 0; /* open failed */
  260. fclose(f);
  261. return 1;
  262. }
  263. static const char *pushnexttemplate(lua_State *L, const char *path)
  264. {
  265. const char *l;
  266. while (*path == *LUA_PATHSEP) path++; /* skip separators */
  267. if (*path == '\0') return NULL; /* no more templates */
  268. l = strchr(path, *LUA_PATHSEP); /* find next separator */
  269. if (l == NULL) l = path + strlen(path);
  270. lua_pushlstring(L, path, (size_t)(l - path)); /* template */
  271. return l;
  272. }
  273. static const char *searchpath (lua_State *L, const char *name,
  274. const char *path, const char *sep,
  275. const char *dirsep)
  276. {
  277. luaL_Buffer msg; /* to build error message */
  278. luaL_buffinit(L, &msg);
  279. if (*sep != '\0') /* non-empty separator? */
  280. name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */
  281. while ((path = pushnexttemplate(L, path)) != NULL) {
  282. const char *filename = luaL_gsub(L, lua_tostring(L, -1),
  283. LUA_PATH_MARK, name);
  284. lua_remove(L, -2); /* remove path template */
  285. if (readable(filename)) /* does file exist and is readable? */
  286. return filename; /* return that file name */
  287. lua_pushfstring(L, "\n\tno file " LUA_QS, filename);
  288. lua_remove(L, -2); /* remove file name */
  289. luaL_addvalue(&msg); /* concatenate error msg. entry */
  290. }
  291. luaL_pushresult(&msg); /* create error message */
  292. return NULL; /* not found */
  293. }
  294. static int lj_cf_package_searchpath(lua_State *L)
  295. {
  296. const char *f = searchpath(L, luaL_checkstring(L, 1),
  297. luaL_checkstring(L, 2),
  298. luaL_optstring(L, 3, "."),
  299. luaL_optstring(L, 4, LUA_DIRSEP));
  300. if (f != NULL) {
  301. return 1;
  302. } else { /* error message is on top of the stack */
  303. lua_pushnil(L);
  304. lua_insert(L, -2);
  305. return 2; /* return nil + error message */
  306. }
  307. }
  308. static const char *findfile(lua_State *L, const char *name,
  309. const char *pname)
  310. {
  311. const char *path;
  312. lua_getfield(L, LUA_ENVIRONINDEX, pname);
  313. path = lua_tostring(L, -1);
  314. if (path == NULL)
  315. luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
  316. return searchpath(L, name, path, ".", LUA_DIRSEP);
  317. }
  318. static void loaderror(lua_State *L, const char *filename)
  319. {
  320. luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s",
  321. lua_tostring(L, 1), filename, lua_tostring(L, -1));
  322. }
  323. static int lj_cf_package_loader_lua(lua_State *L)
  324. {
  325. const char *filename;
  326. const char *name = luaL_checkstring(L, 1);
  327. filename = findfile(L, name, "path");
  328. if (filename == NULL) return 1; /* library not found in this path */
  329. if (luaL_loadfile(L, filename) != 0)
  330. loaderror(L, filename);
  331. return 1; /* library loaded successfully */
  332. }
  333. static int lj_cf_package_loader_c(lua_State *L)
  334. {
  335. const char *name = luaL_checkstring(L, 1);
  336. const char *filename = findfile(L, name, "cpath");
  337. if (filename == NULL) return 1; /* library not found in this path */
  338. if (ll_loadfunc(L, filename, name, 0) != 0)
  339. loaderror(L, filename);
  340. return 1; /* library loaded successfully */
  341. }
  342. static int lj_cf_package_loader_croot(lua_State *L)
  343. {
  344. const char *filename;
  345. const char *name = luaL_checkstring(L, 1);
  346. const char *p = strchr(name, '.');
  347. int st;
  348. if (p == NULL) return 0; /* is root */
  349. lua_pushlstring(L, name, (size_t)(p - name));
  350. filename = findfile(L, lua_tostring(L, -1), "cpath");
  351. if (filename == NULL) return 1; /* root not found */
  352. if ((st = ll_loadfunc(L, filename, name, 0)) != 0) {
  353. if (st != PACKAGE_ERR_FUNC) loaderror(L, filename); /* real error */
  354. lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS,
  355. name, filename);
  356. return 1; /* function not found */
  357. }
  358. return 1;
  359. }
  360. static int lj_cf_package_loader_preload(lua_State *L)
  361. {
  362. const char *name = luaL_checkstring(L, 1);
  363. lua_getfield(L, LUA_ENVIRONINDEX, "preload");
  364. if (!lua_istable(L, -1))
  365. luaL_error(L, LUA_QL("package.preload") " must be a table");
  366. lua_getfield(L, -1, name);
  367. if (lua_isnil(L, -1)) { /* Not found? */
  368. const char *bcname = mksymname(L, name, SYMPREFIX_BC);
  369. const char *bcdata = ll_bcsym(NULL, bcname);
  370. if (bcdata == NULL || luaL_loadbuffer(L, bcdata, ~(size_t)0, name) != 0)
  371. lua_pushfstring(L, "\n\tno field package.preload['%s']", name);
  372. }
  373. return 1;
  374. }
  375. /* ------------------------------------------------------------------------ */
  376. #define KEY_SENTINEL (U64x(80000000,00000000)|'s')
  377. static int lj_cf_package_require(lua_State *L)
  378. {
  379. const char *name = luaL_checkstring(L, 1);
  380. int i;
  381. lua_settop(L, 1); /* _LOADED table will be at index 2 */
  382. lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
  383. lua_getfield(L, 2, name);
  384. if (lua_toboolean(L, -1)) { /* is it there? */
  385. if ((L->top-1)->u64 == KEY_SENTINEL) /* check loops */
  386. luaL_error(L, "loop or previous error loading module " LUA_QS, name);
  387. return 1; /* package is already loaded */
  388. }
  389. /* else must load it; iterate over available loaders */
  390. lua_getfield(L, LUA_ENVIRONINDEX, "loaders");
  391. if (!lua_istable(L, -1))
  392. luaL_error(L, LUA_QL("package.loaders") " must be a table");
  393. lua_pushliteral(L, ""); /* error message accumulator */
  394. for (i = 1; ; i++) {
  395. lua_rawgeti(L, -2, i); /* get a loader */
  396. if (lua_isnil(L, -1))
  397. luaL_error(L, "module " LUA_QS " not found:%s",
  398. name, lua_tostring(L, -2));
  399. lua_pushstring(L, name);
  400. lua_call(L, 1, 1); /* call it */
  401. if (lua_isfunction(L, -1)) /* did it find module? */
  402. break; /* module loaded successfully */
  403. else if (lua_isstring(L, -1)) /* loader returned error message? */
  404. lua_concat(L, 2); /* accumulate it */
  405. else
  406. lua_pop(L, 1);
  407. }
  408. (L->top++)->u64 = KEY_SENTINEL;
  409. lua_setfield(L, 2, name); /* _LOADED[name] = sentinel */
  410. lua_pushstring(L, name); /* pass name as argument to module */
  411. lua_call(L, 1, 1); /* run loaded module */
  412. if (!lua_isnil(L, -1)) /* non-nil return? */
  413. lua_setfield(L, 2, name); /* _LOADED[name] = returned value */
  414. lua_getfield(L, 2, name);
  415. if ((L->top-1)->u64 == KEY_SENTINEL) { /* module did not set a value? */
  416. lua_pushboolean(L, 1); /* use true as result */
  417. lua_pushvalue(L, -1); /* extra copy to be returned */
  418. lua_setfield(L, 2, name); /* _LOADED[name] = true */
  419. }
  420. lj_lib_checkfpu(L);
  421. return 1;
  422. }
  423. /* ------------------------------------------------------------------------ */
  424. static void setfenv(lua_State *L)
  425. {
  426. lua_Debug ar;
  427. if (lua_getstack(L, 1, &ar) == 0 ||
  428. lua_getinfo(L, "f", &ar) == 0 || /* get calling function */
  429. lua_iscfunction(L, -1))
  430. luaL_error(L, LUA_QL("module") " not called from a Lua function");
  431. lua_pushvalue(L, -2);
  432. lua_setfenv(L, -2);
  433. lua_pop(L, 1);
  434. }
  435. static void dooptions(lua_State *L, int n)
  436. {
  437. int i;
  438. for (i = 2; i <= n; i++) {
  439. lua_pushvalue(L, i); /* get option (a function) */
  440. lua_pushvalue(L, -2); /* module */
  441. lua_call(L, 1, 0);
  442. }
  443. }
  444. static void modinit(lua_State *L, const char *modname)
  445. {
  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; else dot++;
  453. /* set _PACKAGE as package name (full module name minus last part) */
  454. lua_pushlstring(L, modname, (size_t)(dot - modname));
  455. lua_setfield(L, -2, "_PACKAGE");
  456. }
  457. static int lj_cf_package_module(lua_State *L)
  458. {
  459. const char *modname = luaL_checkstring(L, 1);
  460. int lastarg = (int)(L->top - L->base);
  461. luaL_pushmodule(L, modname, 1);
  462. lua_getfield(L, -1, "_NAME");
  463. if (!lua_isnil(L, -1)) { /* Module already initialized? */
  464. lua_pop(L, 1);
  465. } else {
  466. lua_pop(L, 1);
  467. modinit(L, modname);
  468. }
  469. lua_pushvalue(L, -1);
  470. setfenv(L);
  471. dooptions(L, lastarg);
  472. return LJ_52;
  473. }
  474. static int lj_cf_package_seeall(lua_State *L)
  475. {
  476. luaL_checktype(L, 1, LUA_TTABLE);
  477. if (!lua_getmetatable(L, 1)) {
  478. lua_createtable(L, 0, 1); /* create new metatable */
  479. lua_pushvalue(L, -1);
  480. lua_setmetatable(L, 1);
  481. }
  482. lua_pushvalue(L, LUA_GLOBALSINDEX);
  483. lua_setfield(L, -2, "__index"); /* mt.__index = _G */
  484. return 0;
  485. }
  486. /* ------------------------------------------------------------------------ */
  487. #define AUXMARK "\1"
  488. static void setpath(lua_State *L, const char *fieldname, const char *envname,
  489. const char *def, int noenv)
  490. {
  491. #if LJ_TARGET_CONSOLE
  492. const char *path = NULL;
  493. UNUSED(envname);
  494. #else
  495. const char *path = getenv(envname);
  496. #endif
  497. if (path == NULL || noenv) {
  498. lua_pushstring(L, def);
  499. } else {
  500. path = luaL_gsub(L, path, LUA_PATHSEP LUA_PATHSEP,
  501. LUA_PATHSEP AUXMARK LUA_PATHSEP);
  502. luaL_gsub(L, path, AUXMARK, def);
  503. lua_remove(L, -2);
  504. }
  505. setprogdir(L);
  506. lua_setfield(L, -2, fieldname);
  507. }
  508. static const luaL_Reg package_lib[] = {
  509. { "loadlib", lj_cf_package_loadlib },
  510. { "searchpath", lj_cf_package_searchpath },
  511. { "seeall", lj_cf_package_seeall },
  512. { NULL, NULL }
  513. };
  514. static const luaL_Reg package_global[] = {
  515. { "module", lj_cf_package_module },
  516. { "require", lj_cf_package_require },
  517. { NULL, NULL }
  518. };
  519. static const lua_CFunction package_loaders[] =
  520. {
  521. lj_cf_package_loader_preload,
  522. lj_cf_package_loader_lua,
  523. lj_cf_package_loader_c,
  524. lj_cf_package_loader_croot,
  525. NULL
  526. };
  527. LUALIB_API int luaopen_package(lua_State *L)
  528. {
  529. int i;
  530. int noenv;
  531. luaL_newmetatable(L, "_LOADLIB");
  532. lj_lib_pushcf(L, lj_cf_package_unloadlib, 1);
  533. lua_setfield(L, -2, "__gc");
  534. luaL_register(L, LUA_LOADLIBNAME, package_lib);
  535. lua_copy(L, -1, LUA_ENVIRONINDEX);
  536. lua_createtable(L, sizeof(package_loaders)/sizeof(package_loaders[0])-1, 0);
  537. for (i = 0; package_loaders[i] != NULL; i++) {
  538. lj_lib_pushcf(L, package_loaders[i], 1);
  539. lua_rawseti(L, -2, i+1);
  540. }
  541. #if LJ_52
  542. lua_pushvalue(L, -1);
  543. lua_setfield(L, -3, "searchers");
  544. #endif
  545. lua_setfield(L, -2, "loaders");
  546. lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
  547. noenv = lua_toboolean(L, -1);
  548. lua_pop(L, 1);
  549. setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT, noenv);
  550. setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT, noenv);
  551. lua_pushliteral(L, LUA_PATH_CONFIG);
  552. lua_setfield(L, -2, "config");
  553. luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16);
  554. lua_setfield(L, -2, "loaded");
  555. luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD", 4);
  556. lua_setfield(L, -2, "preload");
  557. lua_pushvalue(L, LUA_GLOBALSINDEX);
  558. luaL_register(L, NULL, package_global);
  559. lua_pop(L, 1);
  560. return 1;
  561. }