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

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