root/lib_base.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. LJLIB_ASM
  2. LJLIB_ASM
  3. ffh_pairs
  4. LJLIB_PUSH
  5. LJLIB_ASM
  6. LJLIB_PUSH
  7. LJLIB_ASM_
  8. LJLIB_CF
  9. LJLIB_CF
  10. LJLIB_ASM
  11. LJLIB_CF
  12. LJLIB_CF
  13. LJLIB_CF
  14. LJLIB_CF
  15. LJLIB_CF
  16. LJLIB_ASM
  17. LJLIB_ASM
  18. LJLIB_CF
  19. LJLIB_ASM
  20. LJLIB_ASM_
  21. LJLIB_CF
  22. reader_func
  23. LJLIB_CF
  24. LJLIB_CF
  25. LJLIB_CF
  26. LJLIB_CF
  27. LJLIB_CF
  28. LJLIB_PUSH
  29. LJLIB_PUSH
  30. LJLIB_PUSH
  31. LJLIB_CF
  32. LJLIB_CF
  33. LJLIB_CF
  34. LJLIB_ASM
  35. ffh_resume
  36. LJLIB_ASM
  37. LJLIB_ASM
  38. lj_ffh_coroutine_wrap_err
  39. LJLIB_CF
  40. setpc_wrap_aux
  41. newproxy_weaktable
  42. luaopen_base

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

/* [<][>][^][v][top][bottom][index][help] */