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