lib_package.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*
  2. ** Package library.
  3. ** Copyright (C) 2005-2014 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)
  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. #undef setprogdir
  63. static void setprogdir(lua_State *L)
  64. {
  65. char buff[MAX_PATH + 1];
  66. char *lb;
  67. DWORD nsize = sizeof(buff);
  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. {
  79. DWORD error = GetLastError();
  80. char buffer[128];
  81. if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
  82. NULL, error, 0, buffer, sizeof(buffer), NULL))
  83. lua_pushstring(L, buffer);
  84. else
  85. lua_pushfstring(L, "system error %d\n", error);
  86. }
  87. static void ll_unloadlib(void *lib)
  88. {
  89. FreeLibrary((HINSTANCE)lib);
  90. }
  91. static void *ll_load(lua_State *L, const char *path, int gl)
  92. {
  93. HINSTANCE lib = LoadLibraryA(path);
  94. if (lib == NULL) pusherror(L);
  95. UNUSED(gl);
  96. return lib;
  97. }
  98. static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym)
  99. {
  100. lua_CFunction f = (lua_CFunction)GetProcAddress((HINSTANCE)lib, sym);
  101. if (f == NULL) pusherror(L);
  102. return f;
  103. }
  104. static const char *ll_bcsym(void *lib, const char *sym)
  105. {
  106. if (lib) {
  107. return (const char *)GetProcAddress((HINSTANCE)lib, sym);
  108. } else {
  109. HINSTANCE h = GetModuleHandleA(NULL);
  110. const char *p = (const char *)GetProcAddress(h, sym);
  111. if (p == NULL && GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
  112. (const char *)ll_bcsym, &h))
  113. p = (const char *)GetProcAddress(h, sym);
  114. return p;
  115. }
  116. }
  117. #else
  118. #undef PACKAGE_LIB_FAIL
  119. #define PACKAGE_LIB_FAIL "absent"
  120. #define DLMSG "dynamic libraries not enabled; no support for target OS"
  121. static void ll_unloadlib(void *lib)
  122. {
  123. UNUSED(lib);
  124. }
  125. static void *ll_load(lua_State *L, const char *path, int gl)
  126. {
  127. UNUSED(path); UNUSED(gl);
  128. lua_pushliteral(L, DLMSG);
  129. return NULL;
  130. }
  131. static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym)
  132. {
  133. UNUSED(lib); UNUSED(sym);
  134. lua_pushliteral(L, DLMSG);
  135. return NULL;
  136. }
  137. static const char *ll_bcsym(void *lib, const char *sym)
  138. {
  139. UNUSED(lib); UNUSED(sym);
  140. return NULL;
  141. }
  142. #endif
  143. /* ------------------------------------------------------------------------ */
  144. static void **ll_register(lua_State *L, const char *path)
  145. {
  146. void **plib;
  147. lua_pushfstring(L, "LOADLIB: %s", path);
  148. lua_gettable(L, LUA_REGISTRYINDEX); /* check library in registry? */
  149. if (!lua_isnil(L, -1)) { /* is there an entry? */
  150. plib = (void **)lua_touserdata(L, -1);
  151. } else { /* no entry yet; create one */
  152. lua_pop(L, 1);
  153. plib = (void **)lua_newuserdata(L, sizeof(void *));
  154. *plib = NULL;
  155. luaL_getmetatable(L, "_LOADLIB");
  156. lua_setmetatable(L, -2);
  157. lua_pushfstring(L, "LOADLIB: %s", path);
  158. lua_pushvalue(L, -2);
  159. lua_settable(L, LUA_REGISTRYINDEX);
  160. }
  161. return plib;
  162. }
  163. static const char *mksymname(lua_State *L, const char *modname,
  164. const char *prefix)
  165. {
  166. const char *funcname;
  167. const char *mark = strchr(modname, *LUA_IGMARK);
  168. if (mark) modname = mark + 1;
  169. funcname = luaL_gsub(L, modname, ".", "_");
  170. funcname = lua_pushfstring(L, prefix, funcname);
  171. lua_remove(L, -2); /* remove 'gsub' result */
  172. return funcname;
  173. }
  174. static int ll_loadfunc(lua_State *L, const char *path, const char *name, int r)
  175. {
  176. void **reg = ll_register(L, path);
  177. if (*reg == NULL) *reg = ll_load(L, path, (*name == '*'));
  178. if (*reg == NULL) {
  179. return PACKAGE_ERR_LIB; /* Unable to load library. */
  180. } else if (*name == '*') { /* Only load library into global namespace. */
  181. lua_pushboolean(L, 1);
  182. return 0;
  183. } else {
  184. const char *sym = r ? name : mksymname(L, name, SYMPREFIX_CF);
  185. lua_CFunction f = ll_sym(L, *reg, sym);
  186. if (f) {
  187. lua_pushcfunction(L, f);
  188. return 0;
  189. }
  190. if (!r) {
  191. const char *bcdata = ll_bcsym(*reg, mksymname(L, name, SYMPREFIX_BC));
  192. lua_pop(L, 1);
  193. if (bcdata) {
  194. if (luaL_loadbuffer(L, bcdata, ~(size_t)0, name) != 0)
  195. return PACKAGE_ERR_LOAD;
  196. return 0;
  197. }
  198. }
  199. return PACKAGE_ERR_FUNC; /* Unable to find function. */
  200. }
  201. }
  202. static int lj_cf_package_loadlib(lua_State *L)
  203. {
  204. const char *path = luaL_checkstring(L, 1);
  205. const char *init = luaL_checkstring(L, 2);
  206. int st = ll_loadfunc(L, path, init, 1);
  207. if (st == 0) { /* no errors? */
  208. return 1; /* return the loaded function */
  209. } else { /* error; error message is on stack top */
  210. lua_pushnil(L);
  211. lua_insert(L, -2);
  212. lua_pushstring(L, (st == PACKAGE_ERR_LIB) ? PACKAGE_LIB_FAIL : "init");
  213. return 3; /* return nil, error message, and where */
  214. }
  215. }
  216. static int lj_cf_package_unloadlib(lua_State *L)
  217. {
  218. void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB");
  219. if (*lib) ll_unloadlib(*lib);
  220. *lib = NULL; /* mark library as closed */
  221. return 0;
  222. }
  223. /* ------------------------------------------------------------------------ */
  224. static int readable(const char *filename)
  225. {
  226. FILE *f = fopen(filename, "r"); /* try to open file */
  227. if (f == NULL) return 0; /* open failed */
  228. fclose(f);
  229. return 1;
  230. }
  231. static const char *pushnexttemplate(lua_State *L, const char *path)
  232. {
  233. const char *l;
  234. while (*path == *LUA_PATHSEP) path++; /* skip separators */
  235. if (*path == '\0') return NULL; /* no more templates */
  236. l = strchr(path, *LUA_PATHSEP); /* find next separator */
  237. if (l == NULL) l = path + strlen(path);
  238. lua_pushlstring(L, path, (size_t)(l - path)); /* template */
  239. return l;
  240. }
  241. static const char *searchpath (lua_State *L, const char *name,
  242. const char *path, const char *sep,
  243. const char *dirsep)
  244. {
  245. luaL_Buffer msg; /* to build error message */
  246. luaL_buffinit(L, &msg);
  247. if (*sep != '\0') /* non-empty separator? */
  248. name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */
  249. while ((path = pushnexttemplate(L, path)) != NULL) {
  250. const char *filename = luaL_gsub(L, lua_tostring(L, -1),
  251. LUA_PATH_MARK, name);
  252. lua_remove(L, -2); /* remove path template */
  253. if (readable(filename)) /* does file exist and is readable? */
  254. return filename; /* return that file name */
  255. lua_pushfstring(L, "\n\tno file " LUA_QS, filename);
  256. lua_remove(L, -2); /* remove file name */
  257. luaL_addvalue(&msg); /* concatenate error msg. entry */
  258. }
  259. luaL_pushresult(&msg); /* create error message */
  260. return NULL; /* not found */
  261. }
  262. static int lj_cf_package_searchpath(lua_State *L)
  263. {
  264. const char *f = searchpath(L, luaL_checkstring(L, 1),
  265. luaL_checkstring(L, 2),
  266. luaL_optstring(L, 3, "."),
  267. luaL_optstring(L, 4, LUA_DIRSEP));
  268. if (f != NULL) {
  269. return 1;
  270. } else { /* error message is on top of the stack */
  271. lua_pushnil(L);
  272. lua_insert(L, -2);
  273. return 2; /* return nil + error message */
  274. }
  275. }
  276. static const char *findfile(lua_State *L, const char *name,
  277. const char *pname)
  278. {
  279. const char *path;
  280. lua_getfield(L, LUA_ENVIRONINDEX, pname);
  281. path = lua_tostring(L, -1);
  282. if (path == NULL)
  283. luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
  284. return searchpath(L, name, path, ".", LUA_DIRSEP);
  285. }
  286. static void loaderror(lua_State *L, const char *filename)
  287. {
  288. luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s",
  289. lua_tostring(L, 1), filename, lua_tostring(L, -1));
  290. }
  291. static int lj_cf_package_loader_lua(lua_State *L)
  292. {
  293. const char *filename;
  294. const char *name = luaL_checkstring(L, 1);
  295. filename = findfile(L, name, "path");
  296. if (filename == NULL) return 1; /* library not found in this path */
  297. if (luaL_loadfile(L, filename) != 0)
  298. loaderror(L, filename);
  299. return 1; /* library loaded successfully */
  300. }
  301. static int lj_cf_package_loader_c(lua_State *L)
  302. {
  303. const char *name = luaL_checkstring(L, 1);
  304. const char *filename = findfile(L, name, "cpath");
  305. if (filename == NULL) return 1; /* library not found in this path */
  306. if (ll_loadfunc(L, filename, name, 0) != 0)
  307. loaderror(L, filename);
  308. return 1; /* library loaded successfully */
  309. }
  310. static int lj_cf_package_loader_croot(lua_State *L)
  311. {
  312. const char *filename;
  313. const char *name = luaL_checkstring(L, 1);
  314. const char *p = strchr(name, '.');
  315. int st;
  316. if (p == NULL) return 0; /* is root */
  317. lua_pushlstring(L, name, (size_t)(p - name));
  318. filename = findfile(L, lua_tostring(L, -1), "cpath");
  319. if (filename == NULL) return 1; /* root not found */
  320. if ((st = ll_loadfunc(L, filename, name, 0)) != 0) {
  321. if (st != PACKAGE_ERR_FUNC) loaderror(L, filename); /* real error */
  322. lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS,
  323. name, filename);
  324. return 1; /* function not found */
  325. }
  326. return 1;
  327. }
  328. static int lj_cf_package_loader_preload(lua_State *L)
  329. {
  330. const char *name = luaL_checkstring(L, 1);
  331. lua_getfield(L, LUA_ENVIRONINDEX, "preload");
  332. if (!lua_istable(L, -1))
  333. luaL_error(L, LUA_QL("package.preload") " must be a table");
  334. lua_getfield(L, -1, name);
  335. if (lua_isnil(L, -1)) { /* Not found? */
  336. const char *bcname = mksymname(L, name, SYMPREFIX_BC);
  337. const char *bcdata = ll_bcsym(NULL, bcname);
  338. if (bcdata == NULL || luaL_loadbuffer(L, bcdata, ~(size_t)0, name) != 0)
  339. lua_pushfstring(L, "\n\tno field package.preload['%s']", name);
  340. }
  341. return 1;
  342. }
  343. /* ------------------------------------------------------------------------ */
  344. static const int sentinel_ = 0;
  345. #define sentinel ((void *)&sentinel_)
  346. static int lj_cf_package_require(lua_State *L)
  347. {
  348. const char *name = luaL_checkstring(L, 1);
  349. int i;
  350. lua_settop(L, 1); /* _LOADED table will be at index 2 */
  351. lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
  352. lua_getfield(L, 2, name);
  353. if (lua_toboolean(L, -1)) { /* is it there? */
  354. if (lua_touserdata(L, -1) == sentinel) /* check loops */
  355. luaL_error(L, "loop or previous error loading module " LUA_QS, name);
  356. return 1; /* package is already loaded */
  357. }
  358. /* else must load it; iterate over available loaders */
  359. lua_getfield(L, LUA_ENVIRONINDEX, "loaders");
  360. if (!lua_istable(L, -1))
  361. luaL_error(L, LUA_QL("package.loaders") " must be a table");
  362. lua_pushliteral(L, ""); /* error message accumulator */
  363. for (i = 1; ; i++) {
  364. lua_rawgeti(L, -2, i); /* get a loader */
  365. if (lua_isnil(L, -1))
  366. luaL_error(L, "module " LUA_QS " not found:%s",
  367. name, lua_tostring(L, -2));
  368. lua_pushstring(L, name);
  369. lua_call(L, 1, 1); /* call it */
  370. if (lua_isfunction(L, -1)) /* did it find module? */
  371. break; /* module loaded successfully */
  372. else if (lua_isstring(L, -1)) /* loader returned error message? */
  373. lua_concat(L, 2); /* accumulate it */
  374. else
  375. lua_pop(L, 1);
  376. }
  377. lua_pushlightuserdata(L, sentinel);
  378. lua_setfield(L, 2, name); /* _LOADED[name] = sentinel */
  379. lua_pushstring(L, name); /* pass name as argument to module */
  380. lua_call(L, 1, 1); /* run loaded module */
  381. if (!lua_isnil(L, -1)) /* non-nil return? */
  382. lua_setfield(L, 2, name); /* _LOADED[name] = returned value */
  383. lua_getfield(L, 2, name);
  384. if (lua_touserdata(L, -1) == sentinel) { /* module did not set a value? */
  385. lua_pushboolean(L, 1); /* use true as result */
  386. lua_pushvalue(L, -1); /* extra copy to be returned */
  387. lua_setfield(L, 2, name); /* _LOADED[name] = true */
  388. }
  389. lj_lib_checkfpu(L);
  390. return 1;
  391. }
  392. /* ------------------------------------------------------------------------ */
  393. static void setfenv(lua_State *L)
  394. {
  395. lua_Debug ar;
  396. if (lua_getstack(L, 1, &ar) == 0 ||
  397. lua_getinfo(L, "f", &ar) == 0 || /* get calling function */
  398. lua_iscfunction(L, -1))
  399. luaL_error(L, LUA_QL("module") " not called from a Lua function");
  400. lua_pushvalue(L, -2);
  401. lua_setfenv(L, -2);
  402. lua_pop(L, 1);
  403. }
  404. static void dooptions(lua_State *L, int n)
  405. {
  406. int i;
  407. for (i = 2; i <= n; i++) {
  408. lua_pushvalue(L, i); /* get option (a function) */
  409. lua_pushvalue(L, -2); /* module */
  410. lua_call(L, 1, 0);
  411. }
  412. }
  413. static void modinit(lua_State *L, const char *modname)
  414. {
  415. const char *dot;
  416. lua_pushvalue(L, -1);
  417. lua_setfield(L, -2, "_M"); /* module._M = module */
  418. lua_pushstring(L, modname);
  419. lua_setfield(L, -2, "_NAME");
  420. dot = strrchr(modname, '.'); /* look for last dot in module name */
  421. if (dot == NULL) dot = modname; else dot++;
  422. /* set _PACKAGE as package name (full module name minus last part) */
  423. lua_pushlstring(L, modname, (size_t)(dot - modname));
  424. lua_setfield(L, -2, "_PACKAGE");
  425. }
  426. static int lj_cf_package_module(lua_State *L)
  427. {
  428. const char *modname = luaL_checkstring(L, 1);
  429. int loaded = lua_gettop(L) + 1; /* index of _LOADED table */
  430. lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
  431. lua_getfield(L, loaded, modname); /* get _LOADED[modname] */
  432. if (!lua_istable(L, -1)) { /* not found? */
  433. lua_pop(L, 1); /* remove previous result */
  434. /* try global variable (and create one if it does not exist) */
  435. if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, 1) != NULL)
  436. lj_err_callerv(L, LJ_ERR_BADMODN, modname);
  437. lua_pushvalue(L, -1);
  438. lua_setfield(L, loaded, modname); /* _LOADED[modname] = new table */
  439. }
  440. /* check whether table already has a _NAME field */
  441. lua_getfield(L, -1, "_NAME");
  442. if (!lua_isnil(L, -1)) { /* is table an initialized module? */
  443. lua_pop(L, 1);
  444. } else { /* no; initialize it */
  445. lua_pop(L, 1);
  446. modinit(L, modname);
  447. }
  448. lua_pushvalue(L, -1);
  449. setfenv(L);
  450. dooptions(L, loaded - 1);
  451. return 0;
  452. }
  453. static int lj_cf_package_seeall(lua_State *L)
  454. {
  455. luaL_checktype(L, 1, LUA_TTABLE);
  456. if (!lua_getmetatable(L, 1)) {
  457. lua_createtable(L, 0, 1); /* create new metatable */
  458. lua_pushvalue(L, -1);
  459. lua_setmetatable(L, 1);
  460. }
  461. lua_pushvalue(L, LUA_GLOBALSINDEX);
  462. lua_setfield(L, -2, "__index"); /* mt.__index = _G */
  463. return 0;
  464. }
  465. /* ------------------------------------------------------------------------ */
  466. #define AUXMARK "\1"
  467. static void setpath(lua_State *L, const char *fieldname, const char *envname,
  468. const char *def, int noenv)
  469. {
  470. #if LJ_TARGET_CONSOLE
  471. const char *path = NULL;
  472. UNUSED(envname);
  473. #else
  474. const char *path = getenv(envname);
  475. #endif
  476. if (path == NULL || noenv) {
  477. lua_pushstring(L, def);
  478. } else {
  479. path = luaL_gsub(L, path, LUA_PATHSEP LUA_PATHSEP,
  480. LUA_PATHSEP AUXMARK LUA_PATHSEP);
  481. luaL_gsub(L, path, AUXMARK, def);
  482. lua_remove(L, -2);
  483. }
  484. setprogdir(L);
  485. lua_setfield(L, -2, fieldname);
  486. }
  487. static const luaL_Reg package_lib[] = {
  488. { "loadlib", lj_cf_package_loadlib },
  489. { "searchpath", lj_cf_package_searchpath },
  490. { "seeall", lj_cf_package_seeall },
  491. { NULL, NULL }
  492. };
  493. static const luaL_Reg package_global[] = {
  494. { "module", lj_cf_package_module },
  495. { "require", lj_cf_package_require },
  496. { NULL, NULL }
  497. };
  498. static const lua_CFunction package_loaders[] =
  499. {
  500. lj_cf_package_loader_preload,
  501. lj_cf_package_loader_lua,
  502. lj_cf_package_loader_c,
  503. lj_cf_package_loader_croot,
  504. NULL
  505. };
  506. LUALIB_API int luaopen_package(lua_State *L)
  507. {
  508. int i;
  509. int noenv;
  510. luaL_newmetatable(L, "_LOADLIB");
  511. lj_lib_pushcf(L, lj_cf_package_unloadlib, 1);
  512. lua_setfield(L, -2, "__gc");
  513. luaL_register(L, LUA_LOADLIBNAME, package_lib);
  514. lua_pushvalue(L, -1);
  515. lua_replace(L, LUA_ENVIRONINDEX);
  516. lua_createtable(L, sizeof(package_loaders)/sizeof(package_loaders[0])-1, 0);
  517. for (i = 0; package_loaders[i] != NULL; i++) {
  518. lj_lib_pushcf(L, package_loaders[i], 1);
  519. lua_rawseti(L, -2, i+1);
  520. }
  521. lua_setfield(L, -2, "loaders");
  522. lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
  523. noenv = lua_toboolean(L, -1);
  524. lua_pop(L, 1);
  525. setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT, noenv);
  526. setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT, noenv);
  527. lua_pushliteral(L, LUA_PATH_CONFIG);
  528. lua_setfield(L, -2, "config");
  529. luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16);
  530. lua_setfield(L, -2, "loaded");
  531. luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD", 4);
  532. lua_setfield(L, -2, "preload");
  533. lua_pushvalue(L, LUA_GLOBALSINDEX);
  534. luaL_register(L, NULL, package_global);
  535. lua_pop(L, 1);
  536. return 1;
  537. }