lbaselib.c 17 KB

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