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