loadlib.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. /*
  2. ** $Id: loadlib.c,v 1.104 2011/11/10 11:42:58 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 Windows, and a stub for other
  8. ** systems.
  9. */
  10. /*
  11. ** if needed, includes windows header before everything else
  12. */
  13. #if defined(_WIN32)
  14. #include <windows.h>
  15. #endif
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #define loadlib_c
  19. #define LUA_LIB
  20. #include "lua.h"
  21. #include "lauxlib.h"
  22. #include "lualib.h"
  23. /*
  24. ** LUA_PATH and LUA_CPATH are the names of the environment
  25. ** variables that Lua check to set its paths.
  26. */
  27. #if !defined(LUA_PATH)
  28. #define LUA_PATH "LUA_PATH"
  29. #endif
  30. #if !defined(LUA_CPATH)
  31. #define LUA_CPATH "LUA_CPATH"
  32. #endif
  33. #define LUA_PATHSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR
  34. #define LUA_PATHVERSION LUA_PATH LUA_PATHSUFFIX
  35. #define LUA_CPATHVERSION LUA_CPATH LUA_PATHSUFFIX
  36. /*
  37. ** LUA_PATH_SEP is the character that separates templates in a path.
  38. ** LUA_PATH_MARK is the string that marks the substitution points in a
  39. ** template.
  40. ** LUA_EXEC_DIR in a Windows path is replaced by the executable's
  41. ** directory.
  42. ** LUA_IGMARK is a mark to ignore all before it when building the
  43. ** luaopen_ function name.
  44. */
  45. #if !defined (LUA_PATH_SEP)
  46. #define LUA_PATH_SEP ";"
  47. #endif
  48. #if !defined (LUA_PATH_MARK)
  49. #define LUA_PATH_MARK "?"
  50. #endif
  51. #if !defined (LUA_EXEC_DIR)
  52. #define LUA_EXEC_DIR "!"
  53. #endif
  54. #if !defined (LUA_IGMARK)
  55. #define LUA_IGMARK "-"
  56. #endif
  57. /*
  58. ** LUA_CSUBSEP is the character that replaces dots in submodule names
  59. ** when searching for a C loader.
  60. ** LUA_LSUBSEP is the character that replaces dots in submodule names
  61. ** when searching for a Lua loader.
  62. */
  63. #if !defined(LUA_CSUBSEP)
  64. #define LUA_CSUBSEP LUA_DIRSEP
  65. #endif
  66. #if !defined(LUA_LSUBSEP)
  67. #define LUA_LSUBSEP LUA_DIRSEP
  68. #endif
  69. /* prefix for open functions in C libraries */
  70. #define LUA_POF "luaopen_"
  71. /* separator for open functions in C libraries */
  72. #define LUA_OFSEP "_"
  73. #define LIBPREFIX "LOADLIB: "
  74. #define POF LUA_POF
  75. #define LIB_FAIL "open"
  76. /* error codes for ll_loadfunc */
  77. #define ERRLIB 1
  78. #define ERRFUNC 2
  79. #define setprogdir(L) ((void)0)
  80. /*
  81. ** system-dependent functions
  82. */
  83. static void ll_unloadlib (void *lib);
  84. static void *ll_load (lua_State *L, const char *path, int seeglb);
  85. static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym);
  86. #if defined(LUA_USE_DLOPEN)
  87. /*
  88. ** {========================================================================
  89. ** This is an implementation of loadlib based on the dlfcn interface.
  90. ** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,
  91. ** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least
  92. ** as an emulation layer on top of native functions.
  93. ** =========================================================================
  94. */
  95. #include <dlfcn.h>
  96. static void ll_unloadlib (void *lib) {
  97. dlclose(lib);
  98. }
  99. static void *ll_load (lua_State *L, const char *path, int seeglb) {
  100. void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : 0));
  101. if (lib == NULL) lua_pushstring(L, dlerror());
  102. return lib;
  103. }
  104. static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
  105. lua_CFunction f = (lua_CFunction)dlsym(lib, sym);
  106. if (f == NULL) lua_pushstring(L, dlerror());
  107. return f;
  108. }
  109. /* }====================================================== */
  110. #elif defined(LUA_DL_DLL)
  111. /*
  112. ** {======================================================================
  113. ** This is an implementation of loadlib for Windows using native functions.
  114. ** =======================================================================
  115. */
  116. #undef setprogdir
  117. /*
  118. ** optional flags for LoadLibraryEx
  119. */
  120. #if !defined(LUA_LLE_FLAGS)
  121. #define LUA_LLE_FLAGS 0
  122. #endif
  123. static void setprogdir (lua_State *L) {
  124. char buff[MAX_PATH + 1];
  125. char *lb;
  126. DWORD nsize = sizeof(buff)/sizeof(char);
  127. DWORD n = GetModuleFileNameA(NULL, buff, nsize);
  128. if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL)
  129. luaL_error(L, "unable to get ModuleFileName");
  130. else {
  131. *lb = '\0';
  132. luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff);
  133. lua_remove(L, -2); /* remove original string */
  134. }
  135. }
  136. static void pusherror (lua_State *L) {
  137. int error = GetLastError();
  138. char buffer[128];
  139. if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
  140. NULL, error, 0, buffer, sizeof(buffer)/sizeof(char), NULL))
  141. lua_pushstring(L, buffer);
  142. else
  143. lua_pushfstring(L, "system error %d\n", error);
  144. }
  145. static void ll_unloadlib (void *lib) {
  146. FreeLibrary((HMODULE)lib);
  147. }
  148. static void *ll_load (lua_State *L, const char *path, int seeglb) {
  149. HMODULE lib = LoadLibraryExA(path, NULL, LUA_LLE_FLAGS);
  150. (void)(seeglb); /* symbols are 'global' by default */
  151. if (lib == NULL) pusherror(L);
  152. return lib;
  153. }
  154. static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
  155. lua_CFunction f = (lua_CFunction)GetProcAddress((HMODULE)lib, sym);
  156. if (f == NULL) pusherror(L);
  157. return f;
  158. }
  159. /* }====================================================== */
  160. #else
  161. /*
  162. ** {======================================================
  163. ** Fallback for other systems
  164. ** =======================================================
  165. */
  166. #undef LIB_FAIL
  167. #define LIB_FAIL "absent"
  168. #define DLMSG "dynamic libraries not enabled; check your Lua installation"
  169. static void ll_unloadlib (void *lib) {
  170. (void)(lib); /* to avoid warnings */
  171. }
  172. static void *ll_load (lua_State *L, const char *path, int seeglb) {
  173. (void)(path); (void)(seeglb); /* to avoid warnings */
  174. lua_pushliteral(L, DLMSG);
  175. return NULL;
  176. }
  177. static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
  178. (void)(lib); (void)(sym); /* to avoid warnings */
  179. lua_pushliteral(L, DLMSG);
  180. return NULL;
  181. }
  182. /* }====================================================== */
  183. #endif
  184. static void **ll_register (lua_State *L, const char *path) {
  185. void **plib;
  186. lua_pushfstring(L, "%s%s", LIBPREFIX, path);
  187. lua_gettable(L, LUA_REGISTRYINDEX); /* check library in registry? */
  188. if (!lua_isnil(L, -1)) /* is there an entry? */
  189. plib = (void **)lua_touserdata(L, -1);
  190. else { /* no entry yet; create one */
  191. lua_pop(L, 1); /* remove result from gettable */
  192. plib = (void **)lua_newuserdata(L, sizeof(const void *));
  193. *plib = NULL;
  194. luaL_setmetatable(L, "_LOADLIB");
  195. lua_pushfstring(L, "%s%s", LIBPREFIX, path);
  196. lua_pushvalue(L, -2);
  197. lua_settable(L, LUA_REGISTRYINDEX);
  198. }
  199. return plib;
  200. }
  201. /*
  202. ** __gc tag method: calls library's `ll_unloadlib' function with the lib
  203. ** handle
  204. */
  205. static int gctm (lua_State *L) {
  206. void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB");
  207. if (*lib) ll_unloadlib(*lib);
  208. *lib = NULL; /* mark library as closed */
  209. return 0;
  210. }
  211. static int ll_loadfunc (lua_State *L, const char *path, const char *sym) {
  212. void **reg = ll_register(L, path);
  213. if (*reg == NULL) *reg = ll_load(L, path, *sym == '*');
  214. if (*reg == NULL) return ERRLIB; /* unable to load library */
  215. if (*sym == '*') { /* loading only library (no function)? */
  216. lua_pushboolean(L, 1); /* return 'true' */
  217. return 0; /* no errors */
  218. }
  219. else {
  220. lua_CFunction f = ll_sym(L, *reg, sym);
  221. if (f == NULL)
  222. return ERRFUNC; /* unable to find function */
  223. lua_pushcfunction(L, f); /* else create new function */
  224. return 0; /* no errors */
  225. }
  226. }
  227. static int ll_loadlib (lua_State *L) {
  228. const char *path = luaL_checkstring(L, 1);
  229. const char *init = luaL_checkstring(L, 2);
  230. int stat = ll_loadfunc(L, path, init);
  231. if (stat == 0) /* no errors? */
  232. return 1; /* return the loaded function */
  233. else { /* error; error message is on stack top */
  234. lua_pushnil(L);
  235. lua_insert(L, -2);
  236. lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init");
  237. return 3; /* return nil, error message, and where */
  238. }
  239. }
  240. /*
  241. ** {======================================================
  242. ** 'require' function
  243. ** =======================================================
  244. */
  245. static int readable (const char *filename) {
  246. FILE *f = fopen(filename, "r"); /* try to open file */
  247. if (f == NULL) return 0; /* open failed */
  248. fclose(f);
  249. return 1;
  250. }
  251. static const char *pushnexttemplate (lua_State *L, const char *path) {
  252. const char *l;
  253. while (*path == *LUA_PATH_SEP) path++; /* skip separators */
  254. if (*path == '\0') return NULL; /* no more templates */
  255. l = strchr(path, *LUA_PATH_SEP); /* find next separator */
  256. if (l == NULL) l = path + strlen(path);
  257. lua_pushlstring(L, path, l - path); /* template */
  258. return l;
  259. }
  260. static const char *searchpath (lua_State *L, const char *name,
  261. const char *path,
  262. const char *sep,
  263. const char *dirsep) {
  264. luaL_Buffer msg; /* to build error message */
  265. luaL_buffinit(L, &msg);
  266. if (*sep != '\0') /* non-empty separator? */
  267. name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */
  268. while ((path = pushnexttemplate(L, path)) != NULL) {
  269. const char *filename = luaL_gsub(L, lua_tostring(L, -1),
  270. LUA_PATH_MARK, name);
  271. lua_remove(L, -2); /* remove path template */
  272. if (readable(filename)) /* does file exist and is readable? */
  273. return filename; /* return that file name */
  274. lua_pushfstring(L, "\n\tno file " LUA_QS, filename);
  275. lua_remove(L, -2); /* remove file name */
  276. luaL_addvalue(&msg); /* concatenate error msg. entry */
  277. }
  278. luaL_pushresult(&msg); /* create error message */
  279. return NULL; /* not found */
  280. }
  281. static int ll_searchpath (lua_State *L) {
  282. const char *f = searchpath(L, luaL_checkstring(L, 1),
  283. luaL_checkstring(L, 2),
  284. luaL_optstring(L, 3, "."),
  285. luaL_optstring(L, 4, LUA_DIRSEP));
  286. if (f != NULL) return 1;
  287. else { /* error message is on top of the stack */
  288. lua_pushnil(L);
  289. lua_insert(L, -2);
  290. return 2; /* return nil + error message */
  291. }
  292. }
  293. static const char *findfile (lua_State *L, const char *name,
  294. const char *pname,
  295. const char *dirsep) {
  296. const char *path;
  297. lua_getfield(L, lua_upvalueindex(1), pname);
  298. path = lua_tostring(L, -1);
  299. if (path == NULL)
  300. luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
  301. return searchpath(L, name, path, ".", dirsep);
  302. }
  303. static int checkload (lua_State *L, int stat, const char *filename) {
  304. if (stat) { /* module loaded successfully? */
  305. lua_pushstring(L, filename); /* will be 2nd argument to module */
  306. return 2; /* return open function and file name */
  307. }
  308. else
  309. return luaL_error(L, "error loading module " LUA_QS
  310. " from file " LUA_QS ":\n\t%s",
  311. lua_tostring(L, 1), filename, lua_tostring(L, -1));
  312. }
  313. static int searcher_Lua (lua_State *L) {
  314. const char *filename;
  315. const char *name = luaL_checkstring(L, 1);
  316. filename = findfile(L, name, "path", LUA_LSUBSEP);
  317. if (filename == NULL) return 1; /* module not found in this path */
  318. return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename);
  319. }
  320. static int loadfunc (lua_State *L, const char *filename, const char *modname) {
  321. const char *funcname;
  322. const char *mark;
  323. modname = luaL_gsub(L, modname, ".", LUA_OFSEP);
  324. mark = strchr(modname, *LUA_IGMARK);
  325. if (mark) {
  326. int stat;
  327. funcname = lua_pushlstring(L, modname, mark - modname);
  328. funcname = lua_pushfstring(L, POF"%s", funcname);
  329. stat = ll_loadfunc(L, filename, funcname);
  330. if (stat != ERRFUNC) return stat;
  331. modname = mark + 1; /* else go ahead and try old-style name */
  332. }
  333. funcname = lua_pushfstring(L, POF"%s", modname);
  334. return ll_loadfunc(L, filename, funcname);
  335. }
  336. static int searcher_C (lua_State *L) {
  337. const char *name = luaL_checkstring(L, 1);
  338. const char *filename = findfile(L, name, "cpath", LUA_CSUBSEP);
  339. if (filename == NULL) return 1; /* module not found in this path */
  340. return checkload(L, (loadfunc(L, filename, name) == 0), filename);
  341. }
  342. static int searcher_Croot (lua_State *L) {
  343. const char *filename;
  344. const char *name = luaL_checkstring(L, 1);
  345. const char *p = strchr(name, '.');
  346. int stat;
  347. if (p == NULL) return 0; /* is root */
  348. lua_pushlstring(L, name, p - name);
  349. filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP);
  350. if (filename == NULL) return 1; /* root not found */
  351. if ((stat = loadfunc(L, filename, name)) != 0) {
  352. if (stat != ERRFUNC)
  353. return checkload(L, 0, filename); /* real error */
  354. else { /* open function not found */
  355. lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS,
  356. name, filename);
  357. return 1;
  358. }
  359. }
  360. lua_pushstring(L, filename); /* will be 2nd argument to module */
  361. return 2;
  362. }
  363. static int searcher_preload (lua_State *L) {
  364. const char *name = luaL_checkstring(L, 1);
  365. lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD");
  366. lua_getfield(L, -1, name);
  367. if (lua_isnil(L, -1)) /* not found? */
  368. lua_pushfstring(L, "\n\tno field package.preload['%s']", name);
  369. return 1;
  370. }
  371. static void findloader (lua_State *L, const char *name) {
  372. int i;
  373. luaL_Buffer msg; /* to build error message */
  374. luaL_buffinit(L, &msg);
  375. lua_getfield(L, lua_upvalueindex(1), "searchers"); /* will be at index 3 */
  376. if (!lua_istable(L, 3))
  377. luaL_error(L, LUA_QL("package.searchers") " must be a table");
  378. /* iterate over available seachers to find a loader */
  379. for (i = 1; ; i++) {
  380. lua_rawgeti(L, 3, i); /* get a seacher */
  381. if (lua_isnil(L, -1)) { /* no more searchers? */
  382. lua_pop(L, 1); /* remove nil */
  383. luaL_pushresult(&msg); /* create error message */
  384. luaL_error(L, "module " LUA_QS " not found:%s",
  385. name, lua_tostring(L, -1));
  386. }
  387. lua_pushstring(L, name);
  388. lua_call(L, 1, 2); /* call it */
  389. if (lua_isfunction(L, -2)) /* did it find a loader? */
  390. return; /* module loader found */
  391. else if (lua_isstring(L, -2)) { /* searcher returned error message? */
  392. lua_pop(L, 1); /* remove extra return */
  393. luaL_addvalue(&msg); /* concatenate error message */
  394. }
  395. else
  396. lua_pop(L, 2); /* remove both returns */
  397. }
  398. }
  399. static int ll_require (lua_State *L) {
  400. const char *name = luaL_checkstring(L, 1);
  401. lua_settop(L, 1); /* _LOADED table will be at index 2 */
  402. lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
  403. lua_getfield(L, 2, name); /* _LOADED[name] */
  404. if (lua_toboolean(L, -1)) /* is it there? */
  405. return 1; /* package is already loaded */
  406. /* else must load package */
  407. lua_pop(L, 1); /* remove 'getfield' result */
  408. findloader(L, name);
  409. lua_pushstring(L, name); /* pass name as argument to module loader */
  410. lua_insert(L, -2); /* name is 1st argument (before search data) */
  411. lua_call(L, 2, 1); /* run loader to load 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 (lua_isnil(L, -1)) { /* 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. return 1;
  421. }
  422. /* }====================================================== */
  423. /*
  424. ** {======================================================
  425. ** 'module' function
  426. ** =======================================================
  427. */
  428. #if defined(LUA_COMPAT_MODULE)
  429. /*
  430. ** changes the environment variable of calling function
  431. */
  432. static void set_env (lua_State *L) {
  433. lua_Debug ar;
  434. if (lua_getstack(L, 1, &ar) == 0 ||
  435. lua_getinfo(L, "f", &ar) == 0 || /* get calling function */
  436. lua_iscfunction(L, -1))
  437. luaL_error(L, LUA_QL("module") " not called from a Lua function");
  438. lua_pushvalue(L, -2); /* copy new environment table to top */
  439. lua_setupvalue(L, -2, 1);
  440. lua_pop(L, 1); /* remove function */
  441. }
  442. static void dooptions (lua_State *L, int n) {
  443. int i;
  444. for (i = 2; i <= n; i++) {
  445. if (lua_isfunction(L, i)) { /* avoid 'calling' extra info. */
  446. lua_pushvalue(L, i); /* get option (a function) */
  447. lua_pushvalue(L, -2); /* module */
  448. lua_call(L, 1, 0);
  449. }
  450. }
  451. }
  452. static void modinit (lua_State *L, const char *modname) {
  453. const char *dot;
  454. lua_pushvalue(L, -1);
  455. lua_setfield(L, -2, "_M"); /* module._M = module */
  456. lua_pushstring(L, modname);
  457. lua_setfield(L, -2, "_NAME");
  458. dot = strrchr(modname, '.'); /* look for last dot in module name */
  459. if (dot == NULL) dot = modname;
  460. else dot++;
  461. /* set _PACKAGE as package name (full module name minus last part) */
  462. lua_pushlstring(L, modname, dot - modname);
  463. lua_setfield(L, -2, "_PACKAGE");
  464. }
  465. static int ll_module (lua_State *L) {
  466. const char *modname = luaL_checkstring(L, 1);
  467. int lastarg = lua_gettop(L); /* last parameter */
  468. luaL_pushmodule(L, modname, 1); /* get/create module table */
  469. /* check whether table already has a _NAME field */
  470. lua_getfield(L, -1, "_NAME");
  471. if (!lua_isnil(L, -1)) /* is table an initialized module? */
  472. lua_pop(L, 1);
  473. else { /* no; initialize it */
  474. lua_pop(L, 1);
  475. modinit(L, modname);
  476. }
  477. lua_pushvalue(L, -1);
  478. set_env(L);
  479. dooptions(L, lastarg);
  480. return 1;
  481. }
  482. static int ll_seeall (lua_State *L) {
  483. luaL_checktype(L, 1, LUA_TTABLE);
  484. if (!lua_getmetatable(L, 1)) {
  485. lua_createtable(L, 0, 1); /* create new metatable */
  486. lua_pushvalue(L, -1);
  487. lua_setmetatable(L, 1);
  488. }
  489. lua_pushglobaltable(L);
  490. lua_setfield(L, -2, "__index"); /* mt.__index = _G */
  491. return 0;
  492. }
  493. #endif
  494. /* }====================================================== */
  495. /* auxiliary mark (for internal use) */
  496. #define AUXMARK "\1"
  497. static void setpath (lua_State *L, const char *fieldname, const char *envname1,
  498. const char *envname2, const char *def) {
  499. const char *path = getenv(envname1);
  500. if (path == NULL) /* no environment variable? */
  501. path = getenv(envname2); /* try alternative name */
  502. if (path == NULL) /* no environment variable? */
  503. lua_pushstring(L, def); /* use default */
  504. else {
  505. /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */
  506. path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP,
  507. LUA_PATH_SEP AUXMARK LUA_PATH_SEP);
  508. luaL_gsub(L, path, AUXMARK, def);
  509. lua_remove(L, -2);
  510. }
  511. setprogdir(L);
  512. lua_setfield(L, -2, fieldname);
  513. }
  514. static const luaL_Reg pk_funcs[] = {
  515. {"loadlib", ll_loadlib},
  516. {"searchpath", ll_searchpath},
  517. #if defined(LUA_COMPAT_MODULE)
  518. {"seeall", ll_seeall},
  519. #endif
  520. {NULL, NULL}
  521. };
  522. static const luaL_Reg ll_funcs[] = {
  523. #if defined(LUA_COMPAT_MODULE)
  524. {"module", ll_module},
  525. #endif
  526. {"require", ll_require},
  527. {NULL, NULL}
  528. };
  529. static const lua_CFunction searchers[] =
  530. {searcher_preload, searcher_Lua, searcher_C, searcher_Croot, NULL};
  531. LUAMOD_API int luaopen_package (lua_State *L) {
  532. int i;
  533. /* create new type _LOADLIB */
  534. luaL_newmetatable(L, "_LOADLIB");
  535. lua_pushcfunction(L, gctm);
  536. lua_setfield(L, -2, "__gc");
  537. /* create `package' table */
  538. luaL_newlib(L, pk_funcs);
  539. /* create 'searchers' table */
  540. lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0);
  541. /* fill it with pre-defined searchers */
  542. for (i=0; searchers[i] != NULL; i++) {
  543. lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */
  544. lua_pushcclosure(L, searchers[i], 1);
  545. lua_rawseti(L, -2, i+1);
  546. }
  547. #if defined(LUA_COMPAT_LOADERS)
  548. lua_pushvalue(L, -1); /* make a copy of 'searchers' table */
  549. lua_setfield(L, -3, "loaders"); /* put it in field `loaders' */
  550. #endif
  551. lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */
  552. /* set field 'path' */
  553. setpath(L, "path", LUA_PATHVERSION, LUA_PATH, LUA_PATH_DEFAULT);
  554. /* set field 'cpath' */
  555. setpath(L, "cpath", LUA_CPATHVERSION, LUA_CPATH, LUA_CPATH_DEFAULT);
  556. /* store config information */
  557. lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n"
  558. LUA_EXEC_DIR "\n" LUA_IGMARK "\n");
  559. lua_setfield(L, -2, "config");
  560. /* set field `loaded' */
  561. luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
  562. lua_setfield(L, -2, "loaded");
  563. /* set field `preload' */
  564. luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
  565. lua_setfield(L, -2, "preload");
  566. lua_pushglobaltable(L);
  567. lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */
  568. luaL_setfuncs(L, ll_funcs, 1); /* open lib into global table */
  569. lua_pop(L, 1); /* pop global table */
  570. return 1; /* return 'package' table */
  571. }