lib_base.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. /*
  2. ** Base and coroutine library.
  3. ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
  4. **
  5. ** Major portions taken verbatim or adapted from the Lua interpreter.
  6. ** Copyright (C) 1994-2011 Lua.org, PUC-Rio. See Copyright Notice in lua.h
  7. */
  8. #include <stdio.h>
  9. #define lib_base_c
  10. #define LUA_LIB
  11. #include "lua.h"
  12. #include "lauxlib.h"
  13. #include "lualib.h"
  14. #include "lj_obj.h"
  15. #include "lj_gc.h"
  16. #include "lj_err.h"
  17. #include "lj_debug.h"
  18. #include "lj_buf.h"
  19. #include "lj_str.h"
  20. #include "lj_tab.h"
  21. #include "lj_meta.h"
  22. #include "lj_state.h"
  23. #include "lj_frame.h"
  24. #if LJ_HASFFI
  25. #include "lj_ctype.h"
  26. #include "lj_cconv.h"
  27. #endif
  28. #include "lj_bc.h"
  29. #include "lj_ff.h"
  30. #include "lj_dispatch.h"
  31. #include "lj_char.h"
  32. #include "lj_strscan.h"
  33. #include "lj_strfmt.h"
  34. #include "lj_lib.h"
  35. /* -- Base library: checks ------------------------------------------------ */
  36. #define LJLIB_MODULE_base
  37. LJLIB_ASM(assert) LJLIB_REC(.)
  38. {
  39. lj_lib_checkany(L, 1);
  40. if (L->top == L->base+1)
  41. lj_err_caller(L, LJ_ERR_ASSERT);
  42. else if (tvisstr(L->base+1) || tvisnumber(L->base+1))
  43. lj_err_callermsg(L, strdata(lj_lib_checkstr(L, 2)));
  44. else
  45. lj_err_run(L);
  46. return FFH_UNREACHABLE;
  47. }
  48. /* ORDER LJ_T */
  49. LJLIB_PUSH("nil")
  50. LJLIB_PUSH("boolean")
  51. LJLIB_PUSH(top-1) /* boolean */
  52. LJLIB_PUSH("userdata")
  53. LJLIB_PUSH("string")
  54. LJLIB_PUSH("upval")
  55. LJLIB_PUSH("thread")
  56. LJLIB_PUSH("proto")
  57. LJLIB_PUSH("function")
  58. LJLIB_PUSH("trace")
  59. LJLIB_PUSH("cdata")
  60. LJLIB_PUSH("table")
  61. LJLIB_PUSH(top-9) /* userdata */
  62. LJLIB_PUSH("number")
  63. LJLIB_ASM_(type) LJLIB_REC(.)
  64. /* Recycle the lj_lib_checkany(L, 1) from assert. */
  65. /* -- Base library: iterators --------------------------------------------- */
  66. /* This solves a circular dependency problem -- change FF_next_N as needed. */
  67. LJ_STATIC_ASSERT((int)FF_next == FF_next_N);
  68. LJLIB_ASM(next) LJLIB_REC(.)
  69. {
  70. lj_lib_checktab(L, 1);
  71. lj_err_msg(L, LJ_ERR_NEXTIDX);
  72. return FFH_UNREACHABLE;
  73. }
  74. #if LJ_52 || LJ_HASFFI
  75. static int ffh_pairs(lua_State *L, MMS mm)
  76. {
  77. TValue *o = lj_lib_checkany(L, 1);
  78. cTValue *mo = lj_meta_lookup(L, o, mm);
  79. if ((LJ_52 || tviscdata(o)) && !tvisnil(mo)) {
  80. L->top = o+1; /* Only keep one argument. */
  81. copyTV(L, L->base-1-LJ_FR2, mo); /* Replace callable. */
  82. return FFH_TAILCALL;
  83. } else {
  84. if (!tvistab(o)) lj_err_argt(L, 1, LUA_TTABLE);
  85. if (LJ_FR2) { copyTV(L, o-1, o); o--; }
  86. setfuncV(L, o-1, funcV(lj_lib_upvalue(L, 1)));
  87. if (mm == MM_pairs) setnilV(o+1); else setintV(o+1, 0);
  88. return FFH_RES(3);
  89. }
  90. }
  91. #else
  92. #define ffh_pairs(L, mm) (lj_lib_checktab(L, 1), FFH_UNREACHABLE)
  93. #endif
  94. LJLIB_PUSH(lastcl)
  95. LJLIB_ASM(pairs) LJLIB_REC(xpairs 0)
  96. {
  97. return ffh_pairs(L, MM_pairs);
  98. }
  99. LJLIB_NOREGUV LJLIB_ASM(ipairs_aux) LJLIB_REC(.)
  100. {
  101. lj_lib_checktab(L, 1);
  102. lj_lib_checkint(L, 2);
  103. return FFH_UNREACHABLE;
  104. }
  105. LJLIB_PUSH(lastcl)
  106. LJLIB_ASM(ipairs) LJLIB_REC(xpairs 1)
  107. {
  108. return ffh_pairs(L, MM_ipairs);
  109. }
  110. /* -- Base library: getters and setters ----------------------------------- */
  111. LJLIB_ASM_(getmetatable) LJLIB_REC(.)
  112. /* Recycle the lj_lib_checkany(L, 1) from assert. */
  113. LJLIB_ASM(setmetatable) LJLIB_REC(.)
  114. {
  115. GCtab *t = lj_lib_checktab(L, 1);
  116. GCtab *mt = lj_lib_checktabornil(L, 2);
  117. if (!tvisnil(lj_meta_lookup(L, L->base, MM_metatable)))
  118. lj_err_caller(L, LJ_ERR_PROTMT);
  119. setgcref(t->metatable, obj2gco(mt));
  120. if (mt) { lj_gc_objbarriert(L, t, mt); }
  121. settabV(L, L->base-1-LJ_FR2, t);
  122. return FFH_RES(1);
  123. }
  124. LJLIB_CF(getfenv) LJLIB_REC(.)
  125. {
  126. GCfunc *fn;
  127. cTValue *o = L->base;
  128. if (!(o < L->top && tvisfunc(o))) {
  129. int level = lj_lib_optint(L, 1, 1);
  130. o = lj_debug_frame(L, level, &level);
  131. if (o == NULL)
  132. lj_err_arg(L, 1, LJ_ERR_INVLVL);
  133. if (LJ_FR2) o--;
  134. }
  135. fn = &gcval(o)->fn;
  136. settabV(L, L->top++, isluafunc(fn) ? tabref(fn->l.env) : tabref(L->env));
  137. return 1;
  138. }
  139. LJLIB_CF(setfenv)
  140. {
  141. GCfunc *fn;
  142. GCtab *t = lj_lib_checktab(L, 2);
  143. cTValue *o = L->base;
  144. if (!(o < L->top && tvisfunc(o))) {
  145. int level = lj_lib_checkint(L, 1);
  146. if (level == 0) {
  147. /* NOBARRIER: A thread (i.e. L) is never black. */
  148. setgcref(L->env, obj2gco(t));
  149. return 0;
  150. }
  151. o = lj_debug_frame(L, level, &level);
  152. if (o == NULL)
  153. lj_err_arg(L, 1, LJ_ERR_INVLVL);
  154. if (LJ_FR2) o--;
  155. }
  156. fn = &gcval(o)->fn;
  157. if (!isluafunc(fn))
  158. lj_err_caller(L, LJ_ERR_SETFENV);
  159. setgcref(fn->l.env, obj2gco(t));
  160. lj_gc_objbarrier(L, obj2gco(fn), t);
  161. setfuncV(L, L->top++, fn);
  162. return 1;
  163. }
  164. LJLIB_ASM(rawget) LJLIB_REC(.)
  165. {
  166. lj_lib_checktab(L, 1);
  167. lj_lib_checkany(L, 2);
  168. return FFH_UNREACHABLE;
  169. }
  170. LJLIB_CF(rawset) LJLIB_REC(.)
  171. {
  172. lj_lib_checktab(L, 1);
  173. lj_lib_checkany(L, 2);
  174. L->top = 1+lj_lib_checkany(L, 3);
  175. lua_rawset(L, 1);
  176. return 1;
  177. }
  178. LJLIB_CF(rawequal) LJLIB_REC(.)
  179. {
  180. cTValue *o1 = lj_lib_checkany(L, 1);
  181. cTValue *o2 = lj_lib_checkany(L, 2);
  182. setboolV(L->top-1, lj_obj_equal(o1, o2));
  183. return 1;
  184. }
  185. #if LJ_52
  186. LJLIB_CF(rawlen) LJLIB_REC(.)
  187. {
  188. cTValue *o = L->base;
  189. int32_t len;
  190. if (L->top > o && tvisstr(o))
  191. len = (int32_t)strV(o)->len;
  192. else
  193. len = (int32_t)lj_tab_len(lj_lib_checktab(L, 1));
  194. setintV(L->top-1, len);
  195. return 1;
  196. }
  197. #endif
  198. LJLIB_CF(unpack)
  199. {
  200. GCtab *t = lj_lib_checktab(L, 1);
  201. int32_t n, i = lj_lib_optint(L, 2, 1);
  202. int32_t e = (L->base+3-1 < L->top && !tvisnil(L->base+3-1)) ?
  203. lj_lib_checkint(L, 3) : (int32_t)lj_tab_len(t);
  204. uint32_t nu;
  205. if (i > e) return 0;
  206. nu = (uint32_t)e - (uint32_t)i;
  207. n = (int32_t)(nu+1);
  208. if (nu >= LUAI_MAXCSTACK || !lua_checkstack(L, n))
  209. lj_err_caller(L, LJ_ERR_UNPACK);
  210. do {
  211. cTValue *tv = lj_tab_getint(t, i);
  212. if (tv) {
  213. copyTV(L, L->top++, tv);
  214. } else {
  215. setnilV(L->top++);
  216. }
  217. } while (i++ < e);
  218. return n;
  219. }
  220. LJLIB_CF(select) LJLIB_REC(.)
  221. {
  222. int32_t n = (int32_t)(L->top - L->base);
  223. if (n >= 1 && tvisstr(L->base) && *strVdata(L->base) == '#') {
  224. setintV(L->top-1, n-1);
  225. return 1;
  226. } else {
  227. int32_t i = lj_lib_checkint(L, 1);
  228. if (i < 0) i = n + i; else if (i > n) i = n;
  229. if (i < 1)
  230. lj_err_arg(L, 1, LJ_ERR_IDXRNG);
  231. return n - i;
  232. }
  233. }
  234. /* -- Base library: conversions ------------------------------------------- */
  235. LJLIB_ASM(tonumber) LJLIB_REC(.)
  236. {
  237. int32_t base = lj_lib_optint(L, 2, 10);
  238. if (base == 10) {
  239. TValue *o = lj_lib_checkany(L, 1);
  240. if (lj_strscan_numberobj(o)) {
  241. copyTV(L, L->base-1-LJ_FR2, o);
  242. return FFH_RES(1);
  243. }
  244. #if LJ_HASFFI
  245. if (tviscdata(o)) {
  246. CTState *cts = ctype_cts(L);
  247. CType *ct = lj_ctype_rawref(cts, cdataV(o)->ctypeid);
  248. if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);
  249. if (ctype_isnum(ct->info) || ctype_iscomplex(ct->info)) {
  250. if (LJ_DUALNUM && ctype_isinteger_or_bool(ct->info) &&
  251. ct->size <= 4 && !(ct->size == 4 && (ct->info & CTF_UNSIGNED))) {
  252. int32_t i;
  253. lj_cconv_ct_tv(cts, ctype_get(cts, CTID_INT32), (uint8_t *)&i, o, 0);
  254. setintV(L->base-1-LJ_FR2, i);
  255. return FFH_RES(1);
  256. }
  257. lj_cconv_ct_tv(cts, ctype_get(cts, CTID_DOUBLE),
  258. (uint8_t *)&(L->base-1-LJ_FR2)->n, o, 0);
  259. return FFH_RES(1);
  260. }
  261. }
  262. #endif
  263. } else {
  264. const char *p = strdata(lj_lib_checkstr(L, 1));
  265. char *ep;
  266. unsigned int neg = 0;
  267. unsigned long ul;
  268. if (base < 2 || base > 36)
  269. lj_err_arg(L, 2, LJ_ERR_BASERNG);
  270. while (lj_char_isspace((unsigned char)(*p))) p++;
  271. if (*p == '-') { p++; neg = 1; } else if (*p == '+') { p++; }
  272. if (lj_char_isalnum((unsigned char)(*p))) {
  273. ul = strtoul(p, &ep, base);
  274. if (p != ep) {
  275. while (lj_char_isspace((unsigned char)(*ep))) ep++;
  276. if (*ep == '\0') {
  277. if (LJ_DUALNUM && LJ_LIKELY(ul < 0x80000000u+neg)) {
  278. if (neg) ul = ~ul+1u;
  279. setintV(L->base-1-LJ_FR2, (int32_t)ul);
  280. } else {
  281. lua_Number n = (lua_Number)ul;
  282. if (neg) n = -n;
  283. setnumV(L->base-1-LJ_FR2, n);
  284. }
  285. return FFH_RES(1);
  286. }
  287. }
  288. }
  289. }
  290. setnilV(L->base-1-LJ_FR2);
  291. return FFH_RES(1);
  292. }
  293. LJLIB_ASM(tostring) LJLIB_REC(.)
  294. {
  295. TValue *o = lj_lib_checkany(L, 1);
  296. cTValue *mo;
  297. L->top = o+1; /* Only keep one argument. */
  298. if (!tvisnil(mo = lj_meta_lookup(L, o, MM_tostring))) {
  299. copyTV(L, L->base-1-LJ_FR2, mo); /* Replace callable. */
  300. return FFH_TAILCALL;
  301. }
  302. lj_gc_check(L);
  303. setstrV(L, L->base-1-LJ_FR2, lj_strfmt_obj(L, L->base));
  304. return FFH_RES(1);
  305. }
  306. /* -- Base library: throw and catch errors -------------------------------- */
  307. LJLIB_CF(error)
  308. {
  309. int32_t level = lj_lib_optint(L, 2, 1);
  310. lua_settop(L, 1);
  311. if (lua_isstring(L, 1) && level > 0) {
  312. luaL_where(L, level);
  313. lua_pushvalue(L, 1);
  314. lua_concat(L, 2);
  315. }
  316. return lua_error(L);
  317. }
  318. LJLIB_ASM(pcall) LJLIB_REC(.)
  319. {
  320. lj_lib_checkany(L, 1);
  321. lj_lib_checkfunc(L, 2); /* For xpcall only. */
  322. return FFH_UNREACHABLE;
  323. }
  324. LJLIB_ASM_(xpcall) LJLIB_REC(.)
  325. /* -- Base library: load Lua code ----------------------------------------- */
  326. static int load_aux(lua_State *L, int status, int envarg)
  327. {
  328. if (status == LUA_OK) {
  329. /*
  330. ** Set environment table for top-level function.
  331. ** Don't do this for non-native bytecode, which returns a prototype.
  332. */
  333. if (tvistab(L->base+envarg-1) && tvisfunc(L->top-1)) {
  334. GCfunc *fn = funcV(L->top-1);
  335. GCtab *t = tabV(L->base+envarg-1);
  336. setgcref(fn->c.env, obj2gco(t));
  337. lj_gc_objbarrier(L, fn, t);
  338. }
  339. return 1;
  340. } else {
  341. setnilV(L->top-2);
  342. return 2;
  343. }
  344. }
  345. LJLIB_CF(loadfile)
  346. {
  347. GCstr *fname = lj_lib_optstr(L, 1);
  348. GCstr *mode = lj_lib_optstr(L, 2);
  349. int status;
  350. lua_settop(L, 3); /* Ensure env arg exists. */
  351. status = luaL_loadfilex(L, fname ? strdata(fname) : NULL,
  352. mode ? strdata(mode) : NULL);
  353. return load_aux(L, status, 3);
  354. }
  355. static const char *reader_func(lua_State *L, void *ud, size_t *size)
  356. {
  357. UNUSED(ud);
  358. luaL_checkstack(L, 2, "too many nested functions");
  359. copyTV(L, L->top++, L->base);
  360. lua_call(L, 0, 1); /* Call user-supplied function. */
  361. L->top--;
  362. if (tvisnil(L->top)) {
  363. *size = 0;
  364. return NULL;
  365. } else if (tvisstr(L->top) || tvisnumber(L->top)) {
  366. copyTV(L, L->base+4, L->top); /* Anchor string in reserved stack slot. */
  367. return lua_tolstring(L, 5, size);
  368. } else {
  369. lj_err_caller(L, LJ_ERR_RDRSTR);
  370. return NULL;
  371. }
  372. }
  373. LJLIB_CF(load)
  374. {
  375. GCstr *name = lj_lib_optstr(L, 2);
  376. GCstr *mode = lj_lib_optstr(L, 3);
  377. int status;
  378. if (L->base < L->top &&
  379. (tvisstr(L->base) || tvisnumber(L->base) || tvisbuf(L->base))) {
  380. const char *s;
  381. MSize len;
  382. if (tvisbuf(L->base)) {
  383. SBufExt *sbx = bufV(L->base);
  384. s = sbx->r;
  385. len = sbufxlen(sbx);
  386. if (!name) name = &G(L)->strempty; /* Buffers are not NUL-terminated. */
  387. } else {
  388. GCstr *str = lj_lib_checkstr(L, 1);
  389. s = strdata(str);
  390. len = str->len;
  391. }
  392. lua_settop(L, 4); /* Ensure env arg exists. */
  393. status = luaL_loadbufferx(L, s, len, name ? strdata(name) : s,
  394. mode ? strdata(mode) : NULL);
  395. } else {
  396. lj_lib_checkfunc(L, 1);
  397. lua_settop(L, 5); /* Reserve a slot for the string from the reader. */
  398. status = lua_loadx(L, reader_func, NULL, name ? strdata(name) : "=(load)",
  399. mode ? strdata(mode) : NULL);
  400. }
  401. return load_aux(L, status, 4);
  402. }
  403. LJLIB_CF(loadstring)
  404. {
  405. return lj_cf_load(L);
  406. }
  407. LJLIB_CF(dofile)
  408. {
  409. GCstr *fname = lj_lib_optstr(L, 1);
  410. setnilV(L->top);
  411. L->top = L->base+1;
  412. if (luaL_loadfile(L, fname ? strdata(fname) : NULL) != LUA_OK)
  413. lua_error(L);
  414. lua_call(L, 0, LUA_MULTRET);
  415. return (int)(L->top - L->base) - 1;
  416. }
  417. /* -- Base library: GC control -------------------------------------------- */
  418. LJLIB_CF(gcinfo)
  419. {
  420. setintV(L->top++, (int32_t)(G(L)->gc.total >> 10));
  421. return 1;
  422. }
  423. LJLIB_CF(collectgarbage)
  424. {
  425. int opt = lj_lib_checkopt(L, 1, LUA_GCCOLLECT, /* ORDER LUA_GC* */
  426. "\4stop\7restart\7collect\5count\1\377\4step\10setpause\12setstepmul\1\377\11isrunning");
  427. int32_t data = lj_lib_optint(L, 2, 0);
  428. if (opt == LUA_GCCOUNT) {
  429. setnumV(L->top, (lua_Number)G(L)->gc.total/1024.0);
  430. } else {
  431. int res = lua_gc(L, opt, data);
  432. if (opt == LUA_GCSTEP || opt == LUA_GCISRUNNING)
  433. setboolV(L->top, res);
  434. else
  435. setintV(L->top, res);
  436. }
  437. L->top++;
  438. return 1;
  439. }
  440. /* -- Base library: miscellaneous functions ------------------------------- */
  441. LJLIB_PUSH(top-2) /* Upvalue holds weak table. */
  442. LJLIB_CF(newproxy)
  443. {
  444. lua_settop(L, 1);
  445. lua_newuserdata(L, 0);
  446. if (lua_toboolean(L, 1) == 0) { /* newproxy(): without metatable. */
  447. return 1;
  448. } else if (lua_isboolean(L, 1)) { /* newproxy(true): with metatable. */
  449. lua_newtable(L);
  450. lua_pushvalue(L, -1);
  451. lua_pushboolean(L, 1);
  452. lua_rawset(L, lua_upvalueindex(1)); /* Remember mt in weak table. */
  453. } else { /* newproxy(proxy): inherit metatable. */
  454. int validproxy = 0;
  455. if (lua_getmetatable(L, 1)) {
  456. lua_rawget(L, lua_upvalueindex(1));
  457. validproxy = lua_toboolean(L, -1);
  458. lua_pop(L, 1);
  459. }
  460. if (!validproxy)
  461. lj_err_arg(L, 1, LJ_ERR_NOPROXY);
  462. lua_getmetatable(L, 1);
  463. }
  464. lua_setmetatable(L, 2);
  465. return 1;
  466. }
  467. LJLIB_PUSH("tostring")
  468. LJLIB_CF(print)
  469. {
  470. ptrdiff_t i, nargs = L->top - L->base;
  471. cTValue *tv = lj_tab_getstr(tabref(L->env), strV(lj_lib_upvalue(L, 1)));
  472. int shortcut;
  473. if (tv && !tvisnil(tv)) {
  474. copyTV(L, L->top++, tv);
  475. } else {
  476. setstrV(L, L->top++, strV(lj_lib_upvalue(L, 1)));
  477. lua_gettable(L, LUA_GLOBALSINDEX);
  478. tv = L->top-1;
  479. }
  480. shortcut = (tvisfunc(tv) && funcV(tv)->c.ffid == FF_tostring) &&
  481. !gcrefu(basemt_it(G(L), LJ_TNUMX));
  482. for (i = 0; i < nargs; i++) {
  483. cTValue *o = &L->base[i];
  484. const char *str;
  485. size_t size;
  486. MSize len;
  487. if (shortcut && (str = lj_strfmt_wstrnum(L, o, &len)) != NULL) {
  488. size = len;
  489. } else {
  490. copyTV(L, L->top+1, o);
  491. copyTV(L, L->top, L->top-1);
  492. L->top += 2;
  493. lua_call(L, 1, 1);
  494. str = lua_tolstring(L, -1, &size);
  495. if (!str)
  496. lj_err_caller(L, LJ_ERR_PRTOSTR);
  497. L->top--;
  498. }
  499. if (i)
  500. putchar('\t');
  501. fwrite(str, 1, size, stdout);
  502. }
  503. putchar('\n');
  504. return 0;
  505. }
  506. LJLIB_PUSH(top-3)
  507. LJLIB_SET(_VERSION)
  508. #include "lj_libdef.h"
  509. /* -- Coroutine library --------------------------------------------------- */
  510. #define LJLIB_MODULE_coroutine
  511. LJLIB_CF(coroutine_status)
  512. {
  513. const char *s;
  514. lua_State *co;
  515. if (!(L->top > L->base && tvisthread(L->base)))
  516. lj_err_arg(L, 1, LJ_ERR_NOCORO);
  517. co = threadV(L->base);
  518. if (co == L) s = "running";
  519. else if (co->status == LUA_YIELD) s = "suspended";
  520. else if (co->status != LUA_OK) s = "dead";
  521. else if (co->base > tvref(co->stack)+1+LJ_FR2) s = "normal";
  522. else if (co->top == co->base) s = "dead";
  523. else s = "suspended";
  524. lua_pushstring(L, s);
  525. return 1;
  526. }
  527. LJLIB_CF(coroutine_running)
  528. {
  529. #if LJ_52
  530. int ismain = lua_pushthread(L);
  531. setboolV(L->top++, ismain);
  532. return 2;
  533. #else
  534. if (lua_pushthread(L))
  535. setnilV(L->top++);
  536. return 1;
  537. #endif
  538. }
  539. LJLIB_CF(coroutine_isyieldable)
  540. {
  541. setboolV(L->top++, cframe_canyield(L->cframe));
  542. return 1;
  543. }
  544. LJLIB_CF(coroutine_create)
  545. {
  546. lua_State *L1;
  547. if (!(L->base < L->top && tvisfunc(L->base)))
  548. lj_err_argt(L, 1, LUA_TFUNCTION);
  549. L1 = lua_newthread(L);
  550. setfuncV(L, L1->top++, funcV(L->base));
  551. return 1;
  552. }
  553. LJLIB_ASM(coroutine_yield)
  554. {
  555. lj_err_caller(L, LJ_ERR_CYIELD);
  556. return FFH_UNREACHABLE;
  557. }
  558. static int ffh_resume(lua_State *L, lua_State *co, int wrap)
  559. {
  560. if (co->cframe != NULL || co->status > LUA_YIELD ||
  561. (co->status == LUA_OK && co->top == co->base)) {
  562. ErrMsg em = co->cframe ? LJ_ERR_CORUN : LJ_ERR_CODEAD;
  563. if (wrap) lj_err_caller(L, em);
  564. setboolV(L->base-1-LJ_FR2, 0);
  565. setstrV(L, L->base-LJ_FR2, lj_err_str(L, em));
  566. return FFH_RES(2);
  567. }
  568. if (lj_state_cpgrowstack(co, (MSize)(L->top - L->base)) != LUA_OK) {
  569. cTValue *msg = --co->top;
  570. lj_err_callermsg(L, strVdata(msg));
  571. }
  572. return FFH_RETRY;
  573. }
  574. LJLIB_ASM(coroutine_resume)
  575. {
  576. if (!(L->top > L->base && tvisthread(L->base)))
  577. lj_err_arg(L, 1, LJ_ERR_NOCORO);
  578. return ffh_resume(L, threadV(L->base), 0);
  579. }
  580. LJLIB_NOREG LJLIB_ASM(coroutine_wrap_aux)
  581. {
  582. return ffh_resume(L, threadV(lj_lib_upvalue(L, 1)), 1);
  583. }
  584. /* Inline declarations. */
  585. LJ_ASMF void lj_ff_coroutine_wrap_aux(void);
  586. #if !(LJ_TARGET_MIPS && defined(ljamalg_c))
  587. LJ_FUNCA_NORET void LJ_FASTCALL lj_ffh_coroutine_wrap_err(lua_State *L,
  588. lua_State *co);
  589. #endif
  590. /* Error handler, called from assembler VM. */
  591. void LJ_FASTCALL lj_ffh_coroutine_wrap_err(lua_State *L, lua_State *co)
  592. {
  593. co->top--; copyTV(L, L->top, co->top); L->top++;
  594. if (tvisstr(L->top-1))
  595. lj_err_callermsg(L, strVdata(L->top-1));
  596. else
  597. lj_err_run(L);
  598. }
  599. /* Forward declaration. */
  600. static void setpc_wrap_aux(lua_State *L, GCfunc *fn);
  601. LJLIB_CF(coroutine_wrap)
  602. {
  603. GCfunc *fn;
  604. lj_cf_coroutine_create(L);
  605. fn = lj_lib_pushcc(L, lj_ffh_coroutine_wrap_aux, FF_coroutine_wrap_aux, 1);
  606. setpc_wrap_aux(L, fn);
  607. return 1;
  608. }
  609. #include "lj_libdef.h"
  610. /* Fix the PC of wrap_aux. Really ugly workaround. */
  611. static void setpc_wrap_aux(lua_State *L, GCfunc *fn)
  612. {
  613. setmref(fn->c.pc, &L2GG(L)->bcff[lj_lib_init_coroutine[1]+2]);
  614. }
  615. /* ------------------------------------------------------------------------ */
  616. static void newproxy_weaktable(lua_State *L)
  617. {
  618. /* NOBARRIER: The table is new (marked white). */
  619. GCtab *t = lj_tab_new(L, 0, 1);
  620. settabV(L, L->top++, t);
  621. setgcref(t->metatable, obj2gco(t));
  622. setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "__mode")),
  623. lj_str_newlit(L, "kv"));
  624. t->nomm = (uint8_t)(~(1u<<MM_mode));
  625. }
  626. LUALIB_API int luaopen_base(lua_State *L)
  627. {
  628. /* NOBARRIER: Table and value are the same. */
  629. GCtab *env = tabref(L->env);
  630. settabV(L, lj_tab_setstr(L, env, lj_str_newlit(L, "_G")), env);
  631. lua_pushliteral(L, LUA_VERSION); /* top-3. */
  632. newproxy_weaktable(L); /* top-2. */
  633. LJ_LIB_REG(L, "_G", base);
  634. LJ_LIB_REG(L, LUA_COLIBNAME, coroutine);
  635. return 2;
  636. }