lbaselib.c 20 KB

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