lbaselib.c 17 KB

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