lbaselib.c 19 KB

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