lbaselib.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /*
  2. ** $Id: lbaselib.c,v 1.149 2004/06/21 20:05:29 roberto Exp roberto $
  3. ** Basic library
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #define lbaselib_c
  11. #define LUA_LIB
  12. #include "lua.h"
  13. #include "lauxlib.h"
  14. #include "lualib.h"
  15. /*
  16. ** If your system does not support `stdout', you can just remove this function.
  17. ** If you need, you can define your own `print' function, following this
  18. ** model but changing `fputs' to put the strings at a proper place
  19. ** (a console window or a log file, for instance).
  20. */
  21. static int luaB_print (lua_State *L) {
  22. int n = lua_gettop(L); /* number of arguments */
  23. int i;
  24. lua_getglobal(L, "tostring");
  25. for (i=1; i<=n; i++) {
  26. const char *s;
  27. lua_pushvalue(L, -1); /* function to be called */
  28. lua_pushvalue(L, i); /* value to print */
  29. lua_call(L, 1, 1);
  30. s = lua_tostring(L, -1); /* get result */
  31. if (s == NULL)
  32. return luaL_error(L, "`tostring' must return a string to `print'");
  33. if (i>1) fputs("\t", stdout);
  34. fputs(s, stdout);
  35. lua_pop(L, 1); /* pop result */
  36. }
  37. fputs("\n", stdout);
  38. return 0;
  39. }
  40. static int luaB_tonumber (lua_State *L) {
  41. int base = luaL_optint(L, 2, 10);
  42. if (base == 10) { /* standard conversion */
  43. luaL_checkany(L, 1);
  44. if (lua_isnumber(L, 1)) {
  45. lua_pushnumber(L, lua_tonumber(L, 1));
  46. return 1;
  47. }
  48. }
  49. else {
  50. const char *s1 = luaL_checkstring(L, 1);
  51. char *s2;
  52. unsigned long n;
  53. luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
  54. n = strtoul(s1, &s2, base);
  55. if (s1 != s2) { /* at least one valid digit? */
  56. while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
  57. if (*s2 == '\0') { /* no invalid trailing characters? */
  58. lua_pushnumber(L, (lua_Number)n);
  59. return 1;
  60. }
  61. }
  62. }
  63. lua_pushnil(L); /* else not a number */
  64. return 1;
  65. }
  66. static int luaB_error (lua_State *L) {
  67. int level = luaL_optint(L, 2, 1);
  68. lua_settop(L, 1);
  69. if (lua_isstring(L, 1) && level > 0) { /* add extra information? */
  70. luaL_where(L, level);
  71. lua_pushvalue(L, 1);
  72. lua_concat(L, 2);
  73. }
  74. return lua_error(L);
  75. }
  76. static int luaB_getmetatable (lua_State *L) {
  77. luaL_checkany(L, 1);
  78. if (!lua_getmetatable(L, 1)) {
  79. lua_pushnil(L);
  80. return 1; /* no metatable */
  81. }
  82. luaL_getmetafield(L, 1, "__metatable");
  83. return 1; /* returns either __metatable field (if present) or metatable */
  84. }
  85. static int luaB_setmetatable (lua_State *L) {
  86. int t = lua_type(L, 2);
  87. luaL_checktype(L, 1, LUA_TTABLE);
  88. luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
  89. "nil or table expected");
  90. if (luaL_getmetafield(L, 1, "__metatable"))
  91. luaL_error(L, "cannot change a protected metatable");
  92. lua_settop(L, 2);
  93. lua_setmetatable(L, 1);
  94. return 1;
  95. }
  96. static void getfunc (lua_State *L) {
  97. if (lua_isfunction(L, 1)) lua_pushvalue(L, 1);
  98. else {
  99. lua_Debug ar;
  100. int level = luaL_optint(L, 1, 1);
  101. luaL_argcheck(L, level >= 0, 1, "level must be non-negative");
  102. if (lua_getstack(L, level, &ar) == 0)
  103. luaL_argerror(L, 1, "invalid level");
  104. lua_getinfo(L, "f", &ar);
  105. if (lua_isnil(L, -1))
  106. luaL_error(L, "no function environment for tail call at level %d",
  107. level);
  108. }
  109. }
  110. static int luaB_getfenv (lua_State *L) {
  111. getfunc(L);
  112. lua_getfenv(L, -1);
  113. return 1;
  114. }
  115. static int luaB_setfenv (lua_State *L) {
  116. luaL_checktype(L, 2, LUA_TTABLE);
  117. getfunc(L);
  118. lua_pushvalue(L, 2);
  119. if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) {
  120. lua_replace(L, LUA_GLOBALSINDEX);
  121. return 0;
  122. }
  123. else if (lua_setfenv(L, -2) == 0)
  124. luaL_error(L, "`setfenv' cannot change environment of given function");
  125. return 1;
  126. }
  127. static int luaB_rawequal (lua_State *L) {
  128. luaL_checkany(L, 1);
  129. luaL_checkany(L, 2);
  130. lua_pushboolean(L, lua_rawequal(L, 1, 2));
  131. return 1;
  132. }
  133. static int luaB_rawget (lua_State *L) {
  134. luaL_checktype(L, 1, LUA_TTABLE);
  135. luaL_checkany(L, 2);
  136. lua_rawget(L, 1);
  137. return 1;
  138. }
  139. static int luaB_rawset (lua_State *L) {
  140. luaL_checktype(L, 1, LUA_TTABLE);
  141. luaL_checkany(L, 2);
  142. luaL_checkany(L, 3);
  143. lua_rawset(L, 1);
  144. return 1;
  145. }
  146. static int luaB_gcinfo (lua_State *L) {
  147. lua_pushinteger(L, lua_getgccount(L));
  148. return 1;
  149. }
  150. static int luaB_collectgarbage (lua_State *L) {
  151. static const char *const opts[] = {"stop", "restart", "collect", "count",
  152. "step", NULL};
  153. static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART,
  154. LUA_GCCOLLECT, LUA_GCCOUNT, LUA_GCSTEP};
  155. int o = luaL_findstring(luaL_optstring(L, 1, "collect"), opts);
  156. int ex = luaL_optint(L, 2, 0);
  157. luaL_argcheck(L, o >= 0, 1, "invalid option");
  158. lua_pushinteger(L, lua_gc(L, optsnum[o], ex));
  159. return 1;
  160. }
  161. static int luaB_type (lua_State *L) {
  162. luaL_checkany(L, 1);
  163. lua_pushstring(L, lua_typename(L, lua_type(L, 1)));
  164. return 1;
  165. }
  166. static int luaB_next (lua_State *L) {
  167. luaL_checktype(L, 1, LUA_TTABLE);
  168. lua_settop(L, 2); /* create a 2nd argument if there isn't one */
  169. if (lua_next(L, 1))
  170. return 2;
  171. else {
  172. lua_pushnil(L);
  173. return 1;
  174. }
  175. }
  176. static int luaB_pairs (lua_State *L) {
  177. luaL_checktype(L, 1, LUA_TTABLE);
  178. lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
  179. lua_pushvalue(L, 1); /* state, */
  180. lua_pushnil(L); /* and initial value */
  181. return 3;
  182. }
  183. static int ipairsaux (lua_State *L) {
  184. int i = luaL_checkint(L, 2);
  185. luaL_checktype(L, 1, LUA_TTABLE);
  186. i++; /* next value */
  187. lua_pushinteger(L, i);
  188. lua_rawgeti(L, 1, i);
  189. return (lua_isnil(L, -1)) ? 0 : 2;
  190. }
  191. static int luaB_ipairs (lua_State *L) {
  192. luaL_checktype(L, 1, LUA_TTABLE);
  193. lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
  194. lua_pushvalue(L, 1); /* state, */
  195. lua_pushinteger(L, LUA_FIRSTINDEX - 1); /* and initial value */
  196. return 3;
  197. }
  198. static int load_aux (lua_State *L, int status) {
  199. if (status == 0) /* OK? */
  200. return 1;
  201. else {
  202. lua_pushnil(L);
  203. lua_insert(L, -2); /* put before error message */
  204. return 2; /* return nil plus error message */
  205. }
  206. }
  207. static int luaB_loadstring (lua_State *L) {
  208. size_t l;
  209. const char *s = luaL_checklstring(L, 1, &l);
  210. const char *chunkname = luaL_optstring(L, 2, s);
  211. return load_aux(L, luaL_loadbuffer(L, s, l, chunkname));
  212. }
  213. static int luaB_loadfile (lua_State *L) {
  214. const char *fname = luaL_optstring(L, 1, NULL);
  215. const char *path = luaL_optstring(L, 2, NULL);
  216. int status;
  217. if (path == NULL)
  218. status = luaL_loadfile(L, fname);
  219. else {
  220. fname = luaL_searchpath(L, fname, path);
  221. status = (fname) ? luaL_loadfile(L, fname) : 1;
  222. }
  223. return load_aux(L, status);
  224. }
  225. struct Aux_load {
  226. int func;
  227. int res;
  228. };
  229. static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
  230. struct Aux_load *al = (struct Aux_load *)ud;
  231. luaL_unref(L, al->res, LUA_REGISTRYINDEX);
  232. lua_getref(L, al->func);
  233. lua_call(L, 0, 1);
  234. if (lua_isnil(L, -1)) {
  235. *size = 0;
  236. return NULL;
  237. }
  238. else if (lua_isstring(L, -1)) {
  239. const char *res = lua_tostring(L, -1);
  240. *size = lua_strlen(L, -1);
  241. al->res = luaL_ref(L, LUA_REGISTRYINDEX);
  242. return res;
  243. }
  244. else luaL_error(L, "reader function must return a string");
  245. return NULL; /* to avoid warnings */
  246. }
  247. static int luaB_load (lua_State *L) {
  248. struct Aux_load al;
  249. int status;
  250. const char *cname = luaL_optstring(L, 2, "=(load)");
  251. luaL_checktype(L, 1, LUA_TFUNCTION);
  252. lua_settop(L, 1);
  253. al.func = luaL_ref(L, LUA_REGISTRYINDEX);
  254. al.res = LUA_REFNIL;
  255. status = lua_load(L, generic_reader, &al, cname);
  256. luaL_unref(L, al.func, LUA_REGISTRYINDEX);
  257. luaL_unref(L, al.res, LUA_REGISTRYINDEX);
  258. return load_aux(L, status);
  259. }
  260. static int luaB_dofile (lua_State *L) {
  261. const char *fname = luaL_optstring(L, 1, NULL);
  262. int n = lua_gettop(L);
  263. if (luaL_loadfile(L, fname) != 0) lua_error(L);
  264. lua_call(L, 0, LUA_MULTRET);
  265. return lua_gettop(L) - n;
  266. }
  267. static int luaB_assert (lua_State *L) {
  268. luaL_checkany(L, 1);
  269. if (!lua_toboolean(L, 1))
  270. return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!"));
  271. return lua_gettop(L);
  272. }
  273. static int luaB_unpack (lua_State *L) {
  274. int i = luaL_optint(L, 2, LUA_FIRSTINDEX);
  275. int e = luaL_optint(L, 3, -1);
  276. int n;
  277. luaL_checktype(L, 1, LUA_TTABLE);
  278. if (e == -1)
  279. e = luaL_getn(L, 1) + LUA_FIRSTINDEX - 1;
  280. n = e - i + 1; /* number of elements */
  281. if (n <= 0) return 0; /* empty range */
  282. luaL_checkstack(L, n, "table too big to unpack");
  283. for (; i<=e; i++) /* push arg[i...e] */
  284. lua_rawgeti(L, 1, i);
  285. return n;
  286. }
  287. static int luaB_select (lua_State *L) {
  288. int i = luaL_checkint(L, 1);
  289. int n = lua_gettop(L);
  290. if (i < 0 || i >= n) /* index out of range? */
  291. return 0;
  292. if (i == 0)
  293. lua_pushinteger(L, n-1);
  294. else
  295. lua_pushvalue(L, i+1);
  296. return 1;
  297. }
  298. static int luaB_pcall (lua_State *L) {
  299. int status;
  300. luaL_checkany(L, 1);
  301. status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0);
  302. lua_pushboolean(L, (status == 0));
  303. lua_insert(L, 1);
  304. return lua_gettop(L); /* return status + all results */
  305. }
  306. static int luaB_xpcall (lua_State *L) {
  307. int status;
  308. luaL_checkany(L, 2);
  309. lua_settop(L, 2);
  310. lua_insert(L, 1); /* put error function under function to be called */
  311. status = lua_pcall(L, 0, LUA_MULTRET, 1);
  312. lua_pushboolean(L, (status == 0));
  313. lua_replace(L, 1);
  314. return lua_gettop(L); /* return status + all results */
  315. }
  316. static int luaB_tostring (lua_State *L) {
  317. char buff[4*sizeof(void *) + 2]; /* enough space for a `%p' */
  318. const char *tn = "";
  319. const void *p = NULL;
  320. luaL_checkany(L, 1);
  321. if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */
  322. return 1; /* use its value */
  323. switch (lua_type(L, 1)) {
  324. case LUA_TNUMBER:
  325. lua_pushstring(L, lua_tostring(L, 1));
  326. return 1;
  327. case LUA_TSTRING:
  328. lua_pushvalue(L, 1);
  329. return 1;
  330. case LUA_TBOOLEAN:
  331. lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
  332. return 1;
  333. case LUA_TNIL:
  334. lua_pushliteral(L, "nil");
  335. return 1;
  336. case LUA_TTABLE:
  337. p = lua_topointer(L, 1);
  338. tn = "table";
  339. break;
  340. case LUA_TFUNCTION:
  341. p = lua_topointer(L, 1);
  342. tn = "function";
  343. break;
  344. case LUA_TUSERDATA:
  345. case LUA_TLIGHTUSERDATA:
  346. p = lua_touserdata(L, 1);
  347. tn = "userdata";
  348. break;
  349. case LUA_TTHREAD:
  350. p = lua_tothread(L, 1);
  351. tn = "thread";
  352. break;
  353. }
  354. sprintf(buff, "%p", p);
  355. lua_pushfstring(L, "%s: %s", tn, buff);
  356. return 1;
  357. }
  358. static int luaB_newproxy (lua_State *L) {
  359. lua_settop(L, 1);
  360. lua_newuserdata(L, 0); /* create proxy */
  361. if (lua_toboolean(L, 1) == 0)
  362. return 1; /* no metatable */
  363. else if (lua_isboolean(L, 1)) {
  364. lua_newtable(L); /* create a new metatable `m' ... */
  365. lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */
  366. lua_pushboolean(L, 1);
  367. lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */
  368. }
  369. else {
  370. int validproxy = 0; /* to check if weaktable[metatable(u)] == true */
  371. if (lua_getmetatable(L, 1)) {
  372. lua_rawget(L, lua_upvalueindex(1));
  373. validproxy = lua_toboolean(L, -1);
  374. lua_pop(L, 1); /* remove value */
  375. }
  376. luaL_argcheck(L, validproxy, 1, "boolean or proxy expected");
  377. lua_getmetatable(L, 1); /* metatable is valid; get it */
  378. }
  379. lua_setmetatable(L, 2);
  380. return 1;
  381. }
  382. /*
  383. ** {======================================================
  384. ** `require' function
  385. ** =======================================================
  386. */
  387. static const char *getpath (lua_State *L) {
  388. const char *path;
  389. lua_getglobal(L, LUA_PATH); /* try global variable */
  390. path = lua_tostring(L, -1);
  391. if (path) return path;
  392. path = getenv(LUA_PATH); /* else try environment variable */
  393. if (path) return path;
  394. return LUA_PATH_DEFAULT; /* else use default */
  395. }
  396. static int luaB_require (lua_State *L) {
  397. const char *name = luaL_checkstring(L, 1);
  398. const char *path = getpath(L);
  399. const char *fname;
  400. int loaded;
  401. lua_getglobal(L, REQTAB);
  402. loaded = lua_gettop(L);
  403. if (!lua_istable(L, loaded))
  404. return luaL_error(L, "global `" REQTAB "' is not a table");
  405. lua_getfield(L, loaded, name);
  406. if (lua_toboolean(L, -1)) /* is it there? */
  407. return 1; /* package is already loaded; return its result */
  408. /* else must load it; first mark it as loaded */
  409. lua_pushboolean(L, 1);
  410. lua_setfield(L, loaded, name); /* _LOADED[name] = true */
  411. fname = luaL_searchpath(L, name, path);
  412. if (fname == NULL || luaL_loadfile(L, fname) != 0)
  413. return luaL_error(L, "error loading package `%s' (%s)", name,
  414. lua_tostring(L, -1));
  415. lua_pushvalue(L, 1); /* pass name as argument to module */
  416. lua_call(L, 1, 1); /* run loaded module */
  417. if (!lua_isnil(L, -1)) /* nil return? */
  418. lua_setfield(L, loaded, name);
  419. lua_getfield(L, loaded, name); /* return _LOADED[name] */
  420. return 1;
  421. }
  422. /* }====================================================== */
  423. static const luaL_reg base_funcs[] = {
  424. {"error", luaB_error},
  425. {"getmetatable", luaB_getmetatable},
  426. {"setmetatable", luaB_setmetatable},
  427. {"getfenv", luaB_getfenv},
  428. {"setfenv", luaB_setfenv},
  429. {"next", luaB_next},
  430. {"print", luaB_print},
  431. {"tonumber", luaB_tonumber},
  432. {"tostring", luaB_tostring},
  433. {"type", luaB_type},
  434. {"assert", luaB_assert},
  435. {"unpack", luaB_unpack},
  436. {"select", luaB_select},
  437. {"rawequal", luaB_rawequal},
  438. {"rawget", luaB_rawget},
  439. {"rawset", luaB_rawset},
  440. {"pcall", luaB_pcall},
  441. {"xpcall", luaB_xpcall},
  442. {"collectgarbage", luaB_collectgarbage},
  443. {"gcinfo", luaB_gcinfo},
  444. {"loadfile", luaB_loadfile},
  445. {"dofile", luaB_dofile},
  446. {"loadstring", luaB_loadstring},
  447. {"load", luaB_load},
  448. {"require", luaB_require},
  449. {NULL, NULL}
  450. };
  451. /*
  452. ** {======================================================
  453. ** Coroutine library
  454. ** =======================================================
  455. */
  456. static int auxresume (lua_State *L, lua_State *co, int narg) {
  457. int status;
  458. if (!lua_checkstack(co, narg))
  459. luaL_error(L, "too many arguments to resume");
  460. lua_xmove(L, co, narg);
  461. status = lua_resume(co, narg);
  462. if (status == 0) {
  463. int nres = lua_gettop(co);
  464. if (!lua_checkstack(L, nres))
  465. luaL_error(L, "too many results to resume");
  466. lua_xmove(co, L, nres); /* move yielded values */
  467. return nres;
  468. }
  469. else {
  470. lua_xmove(co, L, 1); /* move error message */
  471. return -1; /* error flag */
  472. }
  473. }
  474. static int luaB_coresume (lua_State *L) {
  475. lua_State *co = lua_tothread(L, 1);
  476. int r;
  477. luaL_argcheck(L, co, 1, "coroutine expected");
  478. r = auxresume(L, co, lua_gettop(L) - 1);
  479. if (r < 0) {
  480. lua_pushboolean(L, 0);
  481. lua_insert(L, -2);
  482. return 2; /* return false + error message */
  483. }
  484. else {
  485. lua_pushboolean(L, 1);
  486. lua_insert(L, -(r + 1));
  487. return r + 1; /* return true + `resume' returns */
  488. }
  489. }
  490. static int luaB_auxwrap (lua_State *L) {
  491. lua_State *co = lua_tothread(L, lua_upvalueindex(1));
  492. int r = auxresume(L, co, lua_gettop(L));
  493. if (r < 0) {
  494. if (lua_isstring(L, -1)) { /* error object is a string? */
  495. luaL_where(L, 1); /* add extra info */
  496. lua_insert(L, -2);
  497. lua_concat(L, 2);
  498. }
  499. lua_error(L); /* propagate error */
  500. }
  501. return r;
  502. }
  503. static int luaB_cocreate (lua_State *L) {
  504. lua_State *NL = lua_newthread(L);
  505. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
  506. "Lua function expected");
  507. lua_pushvalue(L, 1); /* move function to top */
  508. lua_xmove(L, NL, 1); /* move function from L to NL */
  509. return 1;
  510. }
  511. static int luaB_cowrap (lua_State *L) {
  512. luaB_cocreate(L);
  513. lua_pushcclosure(L, luaB_auxwrap, 1);
  514. return 1;
  515. }
  516. static int luaB_yield (lua_State *L) {
  517. return lua_yield(L, lua_gettop(L));
  518. }
  519. static int luaB_costatus (lua_State *L) {
  520. lua_State *co = lua_tothread(L, 1);
  521. luaL_argcheck(L, co, 1, "coroutine expected");
  522. if (L == co) lua_pushliteral(L, "running");
  523. else {
  524. lua_Debug ar;
  525. if (lua_getstack(co, 0, &ar) == 0 && lua_gettop(co) == 0)
  526. lua_pushliteral(L, "dead");
  527. else
  528. lua_pushliteral(L, "suspended");
  529. }
  530. return 1;
  531. }
  532. static const luaL_reg co_funcs[] = {
  533. {"create", luaB_cocreate},
  534. {"wrap", luaB_cowrap},
  535. {"resume", luaB_coresume},
  536. {"yield", luaB_yield},
  537. {"status", luaB_costatus},
  538. {NULL, NULL}
  539. };
  540. /* }====================================================== */
  541. static void auxopen (lua_State *L, const char *name,
  542. lua_CFunction f, lua_CFunction u) {
  543. lua_pushcfunction(L, u);
  544. lua_pushcclosure(L, f, 1);
  545. lua_setfield(L, -2, name);
  546. }
  547. static void base_open (lua_State *L) {
  548. lua_pushvalue(L, LUA_GLOBALSINDEX);
  549. luaL_openlib(L, NULL, base_funcs, 0); /* open lib into global table */
  550. lua_pushliteral(L, LUA_VERSION);
  551. lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */
  552. /* `ipairs' and `pairs' need auxiliary functions as upvalues */
  553. auxopen(L, "ipairs", luaB_ipairs, ipairsaux);
  554. auxopen(L, "pairs", luaB_pairs, luaB_next);
  555. /* `newproxy' needs a weaktable as upvalue */
  556. lua_newtable(L); /* new table `w' */
  557. lua_pushvalue(L, -1); /* `w' will be its own metatable */
  558. lua_setmetatable(L, -2);
  559. lua_pushliteral(L, "kv");
  560. lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */
  561. lua_pushcclosure(L, luaB_newproxy, 1);
  562. lua_setfield(L, -2, "newproxy"); /* set global `newproxy' */
  563. lua_setfield(L, -1, "_G"); /* set global _G */
  564. }
  565. LUALIB_API int luaopen_base (lua_State *L) {
  566. base_open(L);
  567. luaL_openlib(L, LUA_COLIBNAME, co_funcs, 0);
  568. lua_newtable(L);
  569. lua_setglobal(L, REQTAB);
  570. return 2;
  571. }