lib_base.c 17 KB

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