ljx

FORK: LuaJIT with native 5.2 and 5.3 support
git clone https://git.neptards.moe/neptards/ljx.git
Log | Files | Refs | README

lib_base.c (17188B)


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