loadlib.c 20 KB

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