lib_base.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /*
  2. ** Base and coroutine library.
  3. ** Copyright (C) 2005-2021 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 = (unsigned long)-(long)ul;
  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. if (tvistab(L->base+envarg-1)) {
  330. GCfunc *fn = funcV(L->top-1);
  331. GCtab *t = tabV(L->base+envarg-1);
  332. setgcref(fn->c.env, obj2gco(t));
  333. lj_gc_objbarrier(L, fn, t);
  334. }
  335. return 1;
  336. } else {
  337. setnilV(L->top-2);
  338. return 2;
  339. }
  340. }
  341. LJLIB_CF(loadfile)
  342. {
  343. GCstr *fname = lj_lib_optstr(L, 1);
  344. GCstr *mode = lj_lib_optstr(L, 2);
  345. int status;
  346. lua_settop(L, 3); /* Ensure env arg exists. */
  347. status = luaL_loadfilex(L, fname ? strdata(fname) : NULL,
  348. mode ? strdata(mode) : NULL);
  349. return load_aux(L, status, 3);
  350. }
  351. static const char *reader_func(lua_State *L, void *ud, size_t *size)
  352. {
  353. UNUSED(ud);
  354. luaL_checkstack(L, 2, "too many nested functions");
  355. copyTV(L, L->top++, L->base);
  356. lua_call(L, 0, 1); /* Call user-supplied function. */
  357. L->top--;
  358. if (tvisnil(L->top)) {
  359. *size = 0;
  360. return NULL;
  361. } else if (tvisstr(L->top) || tvisnumber(L->top)) {
  362. copyTV(L, L->base+4, L->top); /* Anchor string in reserved stack slot. */
  363. return lua_tolstring(L, 5, size);
  364. } else {
  365. lj_err_caller(L, LJ_ERR_RDRSTR);
  366. return NULL;
  367. }
  368. }
  369. LJLIB_CF(load)
  370. {
  371. GCstr *name = lj_lib_optstr(L, 2);
  372. GCstr *mode = lj_lib_optstr(L, 3);
  373. int status;
  374. if (L->base < L->top &&
  375. (tvisstr(L->base) || tvisnumber(L->base) || tvisbuf(L->base))) {
  376. const char *s;
  377. MSize len;
  378. if (tvisbuf(L->base)) {
  379. SBufExt *sbx = bufV(L->base);
  380. s = sbx->r;
  381. len = sbufxlen(sbx);
  382. if (!name) name = &G(L)->strempty; /* Buffers are not NUL-terminated. */
  383. } else {
  384. GCstr *str = lj_lib_checkstr(L, 1);
  385. s = strdata(str);
  386. len = str->len;
  387. }
  388. lua_settop(L, 4); /* Ensure env arg exists. */
  389. status = luaL_loadbufferx(L, s, len, name ? strdata(name) : s,
  390. mode ? strdata(mode) : NULL);
  391. } else {
  392. lj_lib_checkfunc(L, 1);
  393. lua_settop(L, 5); /* Reserve a slot for the string from the reader. */
  394. status = lua_loadx(L, reader_func, NULL, name ? strdata(name) : "=(load)",
  395. mode ? strdata(mode) : NULL);
  396. }
  397. return load_aux(L, status, 4);
  398. }
  399. LJLIB_CF(loadstring)
  400. {
  401. return lj_cf_load(L);
  402. }
  403. LJLIB_CF(dofile)
  404. {
  405. GCstr *fname = lj_lib_optstr(L, 1);
  406. setnilV(L->top);
  407. L->top = L->base+1;
  408. if (luaL_loadfile(L, fname ? strdata(fname) : NULL) != LUA_OK)
  409. lua_error(L);
  410. lua_call(L, 0, LUA_MULTRET);
  411. return (int)(L->top - L->base) - 1;
  412. }
  413. /* -- Base library: GC control -------------------------------------------- */
  414. LJLIB_CF(gcinfo)
  415. {
  416. setintV(L->top++, (int32_t)(G(L)->gc.total >> 10));
  417. return 1;
  418. }
  419. LJLIB_CF(collectgarbage)
  420. {
  421. int opt = lj_lib_checkopt(L, 1, LUA_GCCOLLECT, /* ORDER LUA_GC* */
  422. "\4stop\7restart\7collect\5count\1\377\4step\10setpause\12setstepmul\1\377\11isrunning");
  423. int32_t data = lj_lib_optint(L, 2, 0);
  424. if (opt == LUA_GCCOUNT) {
  425. setnumV(L->top, (lua_Number)G(L)->gc.total/1024.0);
  426. } else {
  427. int res = lua_gc(L, opt, data);
  428. if (opt == LUA_GCSTEP || opt == LUA_GCISRUNNING)
  429. setboolV(L->top, res);
  430. else
  431. setintV(L->top, res);
  432. }
  433. L->top++;
  434. return 1;
  435. }
  436. /* -- Base library: miscellaneous functions ------------------------------- */
  437. LJLIB_PUSH(top-2) /* Upvalue holds weak table. */
  438. LJLIB_CF(newproxy)
  439. {
  440. lua_settop(L, 1);
  441. lua_newuserdata(L, 0);
  442. if (lua_toboolean(L, 1) == 0) { /* newproxy(): without metatable. */
  443. return 1;
  444. } else if (lua_isboolean(L, 1)) { /* newproxy(true): with metatable. */
  445. lua_newtable(L);
  446. lua_pushvalue(L, -1);
  447. lua_pushboolean(L, 1);
  448. lua_rawset(L, lua_upvalueindex(1)); /* Remember mt in weak table. */
  449. } else { /* newproxy(proxy): inherit metatable. */
  450. int validproxy = 0;
  451. if (lua_getmetatable(L, 1)) {
  452. lua_rawget(L, lua_upvalueindex(1));
  453. validproxy = lua_toboolean(L, -1);
  454. lua_pop(L, 1);
  455. }
  456. if (!validproxy)
  457. lj_err_arg(L, 1, LJ_ERR_NOPROXY);
  458. lua_getmetatable(L, 1);
  459. }
  460. lua_setmetatable(L, 2);
  461. return 1;
  462. }
  463. LJLIB_PUSH("tostring")
  464. LJLIB_CF(print)
  465. {
  466. ptrdiff_t i, nargs = L->top - L->base;
  467. cTValue *tv = lj_tab_getstr(tabref(L->env), strV(lj_lib_upvalue(L, 1)));
  468. int shortcut;
  469. if (tv && !tvisnil(tv)) {
  470. copyTV(L, L->top++, tv);
  471. } else {
  472. setstrV(L, L->top++, strV(lj_lib_upvalue(L, 1)));
  473. lua_gettable(L, LUA_GLOBALSINDEX);
  474. tv = L->top-1;
  475. }
  476. shortcut = (tvisfunc(tv) && funcV(tv)->c.ffid == FF_tostring) &&
  477. !gcrefu(basemt_it(G(L), LJ_TNUMX));
  478. for (i = 0; i < nargs; i++) {
  479. cTValue *o = &L->base[i];
  480. const char *str;
  481. size_t size;
  482. MSize len;
  483. if (shortcut && (str = lj_strfmt_wstrnum(L, o, &len)) != NULL) {
  484. size = len;
  485. } else {
  486. copyTV(L, L->top+1, o);
  487. copyTV(L, L->top, L->top-1);
  488. L->top += 2;
  489. lua_call(L, 1, 1);
  490. str = lua_tolstring(L, -1, &size);
  491. if (!str)
  492. lj_err_caller(L, LJ_ERR_PRTOSTR);
  493. L->top--;
  494. }
  495. if (i)
  496. putchar('\t');
  497. fwrite(str, 1, size, stdout);
  498. }
  499. putchar('\n');
  500. return 0;
  501. }
  502. LJLIB_PUSH(top-3)
  503. LJLIB_SET(_VERSION)
  504. #include "lj_libdef.h"
  505. /* -- Coroutine library --------------------------------------------------- */
  506. #define LJLIB_MODULE_coroutine
  507. LJLIB_CF(coroutine_status)
  508. {
  509. const char *s;
  510. lua_State *co;
  511. if (!(L->top > L->base && tvisthread(L->base)))
  512. lj_err_arg(L, 1, LJ_ERR_NOCORO);
  513. co = threadV(L->base);
  514. if (co == L) s = "running";
  515. else if (co->status == LUA_YIELD) s = "suspended";
  516. else if (co->status != LUA_OK) s = "dead";
  517. else if (co->base > tvref(co->stack)+1+LJ_FR2) s = "normal";
  518. else if (co->top == co->base) s = "dead";
  519. else s = "suspended";
  520. lua_pushstring(L, s);
  521. return 1;
  522. }
  523. LJLIB_CF(coroutine_running)
  524. {
  525. #if LJ_52
  526. int ismain = lua_pushthread(L);
  527. setboolV(L->top++, ismain);
  528. return 2;
  529. #else
  530. if (lua_pushthread(L))
  531. setnilV(L->top++);
  532. return 1;
  533. #endif
  534. }
  535. LJLIB_CF(coroutine_isyieldable)
  536. {
  537. setboolV(L->top++, cframe_canyield(L->cframe));
  538. return 1;
  539. }
  540. LJLIB_CF(coroutine_create)
  541. {
  542. lua_State *L1;
  543. if (!(L->base < L->top && tvisfunc(L->base)))
  544. lj_err_argt(L, 1, LUA_TFUNCTION);
  545. L1 = lua_newthread(L);
  546. setfuncV(L, L1->top++, funcV(L->base));
  547. return 1;
  548. }
  549. LJLIB_ASM(coroutine_yield)
  550. {
  551. lj_err_caller(L, LJ_ERR_CYIELD);
  552. return FFH_UNREACHABLE;
  553. }
  554. static int ffh_resume(lua_State *L, lua_State *co, int wrap)
  555. {
  556. if (co->cframe != NULL || co->status > LUA_YIELD ||
  557. (co->status == LUA_OK && co->top == co->base)) {
  558. ErrMsg em = co->cframe ? LJ_ERR_CORUN : LJ_ERR_CODEAD;
  559. if (wrap) lj_err_caller(L, em);
  560. setboolV(L->base-1-LJ_FR2, 0);
  561. setstrV(L, L->base-LJ_FR2, lj_err_str(L, em));
  562. return FFH_RES(2);
  563. }
  564. lj_state_growstack(co, (MSize)(L->top - L->base));
  565. return FFH_RETRY;
  566. }
  567. LJLIB_ASM(coroutine_resume)
  568. {
  569. if (!(L->top > L->base && tvisthread(L->base)))
  570. lj_err_arg(L, 1, LJ_ERR_NOCORO);
  571. return ffh_resume(L, threadV(L->base), 0);
  572. }
  573. LJLIB_NOREG LJLIB_ASM(coroutine_wrap_aux)
  574. {
  575. return ffh_resume(L, threadV(lj_lib_upvalue(L, 1)), 1);
  576. }
  577. /* Inline declarations. */
  578. LJ_ASMF void lj_ff_coroutine_wrap_aux(void);
  579. #if !(LJ_TARGET_MIPS && defined(ljamalg_c))
  580. LJ_FUNCA_NORET void LJ_FASTCALL lj_ffh_coroutine_wrap_err(lua_State *L,
  581. lua_State *co);
  582. #endif
  583. /* Error handler, called from assembler VM. */
  584. void LJ_FASTCALL lj_ffh_coroutine_wrap_err(lua_State *L, lua_State *co)
  585. {
  586. co->top--; copyTV(L, L->top, co->top); L->top++;
  587. if (tvisstr(L->top-1))
  588. lj_err_callermsg(L, strVdata(L->top-1));
  589. else
  590. lj_err_run(L);
  591. }
  592. /* Forward declaration. */
  593. static void setpc_wrap_aux(lua_State *L, GCfunc *fn);
  594. LJLIB_CF(coroutine_wrap)
  595. {
  596. GCfunc *fn;
  597. lj_cf_coroutine_create(L);
  598. fn = lj_lib_pushcc(L, lj_ffh_coroutine_wrap_aux, FF_coroutine_wrap_aux, 1);
  599. setpc_wrap_aux(L, fn);
  600. return 1;
  601. }
  602. #include "lj_libdef.h"
  603. /* Fix the PC of wrap_aux. Really ugly workaround. */
  604. static void setpc_wrap_aux(lua_State *L, GCfunc *fn)
  605. {
  606. setmref(fn->c.pc, &L2GG(L)->bcff[lj_lib_init_coroutine[1]+2]);
  607. }
  608. /* ------------------------------------------------------------------------ */
  609. static void newproxy_weaktable(lua_State *L)
  610. {
  611. /* NOBARRIER: The table is new (marked white). */
  612. GCtab *t = lj_tab_new(L, 0, 1);
  613. settabV(L, L->top++, t);
  614. setgcref(t->metatable, obj2gco(t));
  615. setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "__mode")),
  616. lj_str_newlit(L, "kv"));
  617. t->nomm = (uint8_t)(~(1u<<MM_mode));
  618. }
  619. LUALIB_API int luaopen_base(lua_State *L)
  620. {
  621. /* NOBARRIER: Table and value are the same. */
  622. GCtab *env = tabref(L->env);
  623. settabV(L, lj_tab_setstr(L, env, lj_str_newlit(L, "_G")), env);
  624. lua_pushliteral(L, LUA_VERSION); /* top-3. */
  625. newproxy_weaktable(L); /* top-2. */
  626. LJ_LIB_REG(L, "_G", base);
  627. LJ_LIB_REG(L, LUA_COLIBNAME, coroutine);
  628. return 2;
  629. }