lj_api.c (38974B)
1 /* 2 ** Public Lua/C API. 3 ** Copyright (C) 2005-2016 Mike Pall. See Copyright Notice in luajit.h 4 ** 5 ** Adaption to Lua 5.2 6 ** Copyright (C) 2014 Karel Tuma. See Copyright Notice in luajit.h 7 ** 8 ** Major portions taken verbatim or adapted from the Lua interpreter. 9 ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h 10 */ 11 12 #define lj_api_c 13 #define LUA_CORE 14 15 #include "lj_obj.h" 16 #include "lj_gc.h" 17 #include "lj_err.h" 18 #include "lj_debug.h" 19 #include "lj_str.h" 20 #include "lj_tab.h" 21 #include "lj_func.h" 22 #include "lj_udata.h" 23 #include "lj_meta.h" 24 #include "lj_state.h" 25 #include "lj_bc.h" 26 #include "lj_frame.h" 27 #include "lj_trace.h" 28 #include "lj_vm.h" 29 #include "lj_strscan.h" 30 #include "lj_strfmt.h" 31 #include "lj_char.h" 32 33 /* -- Common helper functions --------------------------------------------- */ 34 35 #define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base)) 36 #define api_checkvalidindex(L, i) api_check(L, (i) != niltv(L)) 37 38 static TValue *index2adr(lua_State *L, int idx) 39 { 40 #if LJ_ABIVER!=51 41 if (idx == LUA_GLOBALSINDEX) { 42 return (TValue*)lj_tab_getint(tabV(registry(L)), LUA_RIDX_GLOBALS); 43 } else 44 #endif 45 if (idx > 0) { 46 TValue *o = L->base + (idx - 1); 47 return o < L->top ? o : niltv(L); 48 } else if (idx > LUAI_FIRSTPSEUDOIDX) { 49 api_check(L, idx != 0 && -idx <= L->top - L->base); 50 return L->top + idx; 51 } 52 #if LJ_ABIVER==51 53 else if (idx == LUA_GLOBALSINDEX) { 54 TValue *o = &G(L)->tmptv; 55 settabV(L, o, tabref(L->env)); 56 return o; 57 } 58 #endif 59 else if (idx == LUA_REGISTRYINDEX) { 60 return registry(L); 61 } else { 62 GCfunc *fn = curr_func(L); 63 api_check(L, fn->c.gct == ~LJ_TFUNC && !isluafunc(fn)); 64 if (idx == LUA_ENVIRONINDEX) { 65 TValue *o = &G(L)->tmptv; 66 settabV(L, o, tabref(fn->c.env)); 67 return o; 68 } else 69 { 70 idx = LUA_UVINDEX - idx; 71 return idx <= fn->c.nupvalues ? &fn->c.upvalue[idx-1] : niltv(L); 72 } 73 } 74 } 75 76 static TValue *stkindex2adr(lua_State *L, int idx) 77 { 78 if (idx > 0) { 79 TValue *o = L->base + (idx - 1); 80 return o < L->top ? o : niltv(L); 81 } else { 82 api_check(L, idx != 0 && -idx <= L->top - L->base); 83 return L->top + idx; 84 } 85 } 86 87 static GCtab *getcurrenv(lua_State *L) 88 { 89 GCfunc *fn = curr_func(L); 90 return fn->c.gct == ~LJ_TFUNC ? tabref(fn->c.env) : tabref(L->env); 91 } 92 93 /* -- Miscellaneous API functions ----------------------------------------- */ 94 95 LUA_API int lua_status(lua_State *L) 96 { 97 return L->status; 98 } 99 100 LUA_API int lua_checkstack(lua_State *L, int size) 101 { 102 if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK) { 103 return 0; /* Stack overflow. */ 104 } else if (size > 0) { 105 lj_state_checkstack(L, (MSize)size); 106 } 107 return 1; 108 } 109 110 LUALIB_API void luaL_checkstack(lua_State *L, int size, const char *msg) 111 { 112 if (!lua_checkstack(L, size)) 113 lj_err_callerv(L, LJ_ERR_STKOVM, msg); 114 } 115 116 LUA_API void lua_xmove(lua_State *from, lua_State *to, int n) 117 { 118 TValue *f, *t; 119 if (from == to) return; 120 api_checknelems(from, n); 121 api_check(from, G(from) == G(to)); 122 lj_state_checkstack(to, (MSize)n); 123 f = from->top; 124 t = to->top = to->top + n; 125 while (--n >= 0) copyTV(to, --t, --f); 126 from->top = f; 127 } 128 129 /* -- Stack manipulation -------------------------------------------------- */ 130 131 LUA_API int lua_absindex(lua_State *L, int idx) 132 { 133 if ((idx > 0) || (idx <= LUAI_FIRSTPSEUDOIDX)) 134 return idx; 135 return (int)(L->top - L->base + idx + 1); 136 } 137 138 static void reverse(lua_State *L, TValue *from, TValue *to) 139 { 140 for (; from < to; from++, to--) { 141 TValue temp; 142 copyTV(L, &temp, from); 143 copyTV(L, from, to); 144 copyTV(L, to, &temp); 145 } 146 } 147 148 LUA_API void lua_rotate (lua_State *L, int idx, int n) { 149 TValue *p, *t, *m; 150 t = L->top - 1; /* end of stack segment being rotated */ 151 p = stkindex2adr(L, idx); /* start of segment */ 152 api_checkvalidindex(L, p); 153 api_check(L, (n >= 0 ? n : -n) <= ((t - p) + 1)); 154 m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */ 155 reverse(L, p, m); /* reverse the prefix with length 'n' */ 156 reverse(L, m + 1, t); /* reverse the suffix */ 157 reverse(L, p, t); /* reverse the entire segment */ 158 } 159 160 LUA_API int lua_gettop(lua_State *L) 161 { 162 return (int)(L->top - L->base); 163 } 164 165 LUA_API void lua_settop(lua_State *L, int idx) 166 { 167 if (idx >= 0) { 168 api_check(L, idx <= tvref(L->maxstack) - L->base); 169 if (L->base + idx > L->top) { 170 if (L->base + idx >= tvref(L->maxstack)) 171 lj_state_growstack(L, (MSize)idx - (MSize)(L->top - L->base)); 172 do { setnilV(L->top++); } while (L->top < L->base + idx); 173 } else { 174 L->top = L->base + idx; 175 } 176 } else { 177 api_check(L, -(idx+1) <= (L->top - L->base)); 178 L->top += idx+1; /* Shrinks top (idx < 0). */ 179 } 180 } 181 182 LUA_API void lua_remove(lua_State *L, int idx) 183 { 184 TValue *p = stkindex2adr(L, idx); 185 api_checkvalidindex(L, p); 186 while (++p < L->top) copyTV(L, p-1, p); 187 L->top--; 188 } 189 190 LUA_API void lua_insert(lua_State *L, int idx) 191 { 192 TValue *q, *p = stkindex2adr(L, idx); 193 api_checkvalidindex(L, p); 194 for (q = L->top; q > p; q--) copyTV(L, q, q-1); 195 copyTV(L, p, L->top); 196 } 197 198 LUA_API void lua_replace(lua_State *L, int idx) 199 { 200 api_checknelems(L, 1); 201 #if LJ_ABIVER==51 202 /* XXX there are probably no 5.2 users of this */ 203 if (idx == LUA_GLOBALSINDEX) { 204 api_check(L, tvistab(L->top-1)); 205 /* NOBARRIER: A thread (i.e. L) is never black. */ 206 setgcref(L->env, obj2gco(tabV(L->top-1))); 207 } else if (idx == LUA_ENVIRONINDEX) { 208 GCfunc *fn = curr_func(L); 209 if (fn->c.gct != ~LJ_TFUNC) 210 lj_err_msg(L, LJ_ERR_NOENV); 211 api_check(L, tvistab(L->top-1)); 212 setgcref(fn->c.env, obj2gco(tabV(L->top-1))); 213 lj_gc_barrier(L, fn, L->top-1); 214 } else 215 #endif 216 { 217 TValue *o = index2adr(L, idx); 218 api_checkvalidindex(L, o); 219 copyTV(L, o, L->top-1); 220 if (idx < LUA_UVINDEX) /* Need a barrier for upvalues. */ 221 lj_gc_barrier(L, curr_func(L), L->top-1); 222 } 223 L->top--; 224 } 225 226 LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) 227 { 228 TValue *src = index2adr(L, fromidx); 229 TValue *dst = index2adr(L, toidx); 230 api_checkvalidindex(L, src); 231 api_checkvalidindex(L, dst); 232 copyTV(L, dst, src); 233 if (toidx < LUA_UVINDEX) /* Need a barrier for upvalues. */ 234 lj_gc_barrier(L, curr_func(L), src); 235 } 236 237 LUA_API void lua_pushvalue(lua_State *L, int idx) 238 { 239 copyTV(L, L->top, index2adr(L, idx)); 240 incr_top(L); 241 } 242 243 LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) 244 { 245 char* endptr; 246 lua_Number n = lua_str2number(s, &endptr); 247 if (endptr != s) { 248 while (*endptr != '\0' && lj_char_isspace((unsigned char)*endptr)) 249 ++endptr; 250 if (*endptr == '\0') { 251 lua_pushnumber(L, n); 252 return endptr - s + 1; 253 } 254 } 255 return 0; 256 } 257 258 /* -- Stack getters ------------------------------------------------------- */ 259 260 static int ljx_tv2type(lua_State *L, cTValue *o) 261 { 262 if (!o) return LUA_TNIL; 263 if (tvisnumber(o)) { 264 return LUA_TNUMBER; 265 #if LJ_64 && !LJ_GC64 266 } else if (tvislightud(o)) { 267 return LUA_TLIGHTUSERDATA; 268 #endif 269 } else if (o == niltv(L)) { 270 return LUA_TNONE; 271 } else { /* Magic internal/external tag conversion. ORDER LJ_T */ 272 uint32_t t = ~itype(o); 273 #if LJ_64 274 int tt = (int)((U64x(75a06,98042110) >> 4*t) & 15u); 275 #else 276 int tt = (int)(((t < 8 ? 0x98042110u : 0x75a06u) >> 4*(t&7)) & 15u); 277 #endif 278 lua_assert(tt != LUA_TNIL || tvisnil(o)); 279 return tt; 280 } 281 } 282 283 LUA_API int lua_type(lua_State *L, int idx) 284 { 285 return ljx_tv2type(L, index2adr(L, idx)); 286 } 287 288 LUALIB_API void luaL_checktype(lua_State *L, int idx, int tt) 289 { 290 if (lua_type(L, idx) != tt) 291 lj_err_argt(L, idx, tt); 292 } 293 294 LUALIB_API void luaL_checkany(lua_State *L, int idx) 295 { 296 if (index2adr(L, idx) == niltv(L)) 297 lj_err_arg(L, idx, LJ_ERR_NOVAL); 298 } 299 300 LUA_API const char *lua_typename(lua_State *L, int t) 301 { 302 UNUSED(L); 303 return lj_obj_typename[t+1]; 304 } 305 306 LUA_API int lua_iscfunction(lua_State *L, int idx) 307 { 308 cTValue *o = index2adr(L, idx); 309 return tvisfunc(o) && !isluafunc(funcV(o)); 310 } 311 312 LUA_API int lua_isnumber(lua_State *L, int idx) 313 { 314 cTValue *o = index2adr(L, idx); 315 TValue tmp; 316 return (tvisnumber(o) || (tvisstr(o) && lj_strscan_number(strV(o), &tmp))); 317 } 318 319 LUA_API int lua_isstring(lua_State *L, int idx) 320 { 321 cTValue *o = index2adr(L, idx); 322 return (tvisstr(o) || tvisnumber(o)); 323 } 324 325 LUA_API int lua_isuserdata(lua_State *L, int idx) 326 { 327 cTValue *o = index2adr(L, idx); 328 return (tvisudata(o) || tvislightud(o)); 329 } 330 331 LUA_API int lua_rawequal(lua_State *L, int idx1, int idx2) 332 { 333 cTValue *o1 = index2adr(L, idx1); 334 cTValue *o2 = index2adr(L, idx2); 335 return (o1 == niltv(L) || o2 == niltv(L)) ? 0 : lj_obj_equal(o1, o2); 336 } 337 338 LUA_API int lua_equal(lua_State *L, int idx1, int idx2) 339 { 340 cTValue *o1 = index2adr(L, idx1); 341 cTValue *o2 = index2adr(L, idx2); 342 if (tvisint(o1) && tvisint(o2)) { 343 return intV(o1) == intV(o2); 344 } else if (tvisnumber(o1) && tvisnumber(o2)) { 345 return numberVnum(o1) == numberVnum(o2); 346 } else if (itype(o1) != itype(o2)) { 347 return 0; 348 } else if (tvispri(o1)) { 349 return o1 != niltv(L) && o2 != niltv(L); 350 #if LJ_64 && !LJ_GC64 351 } else if (tvislightud(o1)) { 352 return o1->u64 == o2->u64; 353 #endif 354 } else if (gcrefeq(o1->gcr, o2->gcr)) { 355 return 1; 356 } else if (!tvistabud(o1)) { 357 return 0; 358 } else { 359 TValue *base = lj_meta_equal(L, gcV(o1), gcV(o2), 0); 360 if ((uintptr_t)base <= 1) { 361 return (int)(uintptr_t)base; 362 } else { 363 L->top = base+2; 364 lj_vm_call(L, base, 1+1); 365 L->top -= 2+LJ_FR2; 366 return tvistruecond(L->top+1+LJ_FR2); 367 } 368 } 369 } 370 371 LUA_API int lua_lessthan(lua_State *L, int idx1, int idx2) 372 { 373 cTValue *o1 = index2adr(L, idx1); 374 cTValue *o2 = index2adr(L, idx2); 375 if (o1 == niltv(L) || o2 == niltv(L)) { 376 return 0; 377 } else if (tvisint(o1) && tvisint(o2)) { 378 return intV(o1) < intV(o2); 379 } else if (tvisnumber(o1) && tvisnumber(o2)) { 380 return numberVnum(o1) < numberVnum(o2); 381 } else { 382 TValue *base = lj_meta_comp(L, o1, o2, 0); 383 if ((uintptr_t)base <= 1) { 384 return (int)(uintptr_t)base; 385 } else { 386 L->top = base+2; 387 lj_vm_call(L, base, 1+1); 388 L->top -= 2+LJ_FR2; 389 return tvistruecond(L->top+1+LJ_FR2); 390 } 391 } 392 } 393 394 LUA_API int lua_compare(lua_State *L, int index1, int index2, int op) 395 { 396 switch (op) { 397 case LUA_OPEQ: return lua_equal(L, index1, index2); 398 case LUA_OPLT: return lua_lessthan(L, index1, index2); 399 case LUA_OPLE: return lua_lessthan(L, index1, index2) || lua_equal(L, index1, index2); 400 } 401 api_check(L, 0); 402 return 0; /* Should not be reached. */ 403 } 404 405 LUA_API lua_Number lua_tonumberx(lua_State *L, int idx, int *succ) 406 { 407 cTValue *o = index2adr(L, idx); 408 TValue tmp; 409 if (succ) *succ = 1; 410 if (LJ_LIKELY(tvisnumber(o))) 411 return numberVnum(o); 412 else if (tvisstr(o) && lj_strscan_num(strV(o), &tmp)) 413 return numV(&tmp); 414 if (succ) *succ = 0; 415 return 0; 416 } 417 418 LUA_API lua_Number lua_tonumber(lua_State *L, int idx) 419 { 420 return lua_tonumberx(L, idx, NULL); 421 } 422 423 LUALIB_API lua_Number luaL_checknumber(lua_State *L, int idx) 424 { 425 cTValue *o = index2adr(L, idx); 426 TValue tmp; 427 if (LJ_LIKELY(tvisnumber(o))) 428 return numberVnum(o); 429 else if (!(tvisstr(o) && lj_strscan_num(strV(o), &tmp))) 430 lj_err_argt(L, idx, LUA_TNUMBER); 431 return numV(&tmp); 432 } 433 434 LUALIB_API lua_Number luaL_optnumber(lua_State *L, int idx, lua_Number def) 435 { 436 cTValue *o = index2adr(L, idx); 437 TValue tmp; 438 if (LJ_LIKELY(tvisnumber(o))) 439 return numberVnum(o); 440 else if (tvisnil(o)) 441 return def; 442 else if (!(tvisstr(o) && lj_strscan_num(strV(o), &tmp))) 443 lj_err_argt(L, idx, LUA_TNUMBER); 444 return numV(&tmp); 445 } 446 447 LUA_API lua_Integer lua_tointegerx(lua_State *L, int idx, int *succ) 448 { 449 cTValue *o = index2adr(L, idx); 450 TValue tmp; 451 lua_Number n; 452 if (succ) *succ = 1; 453 if (LJ_LIKELY(tvisint(o))) { 454 return intV(o); 455 } else if (LJ_LIKELY(tvisnum(o))) { 456 n = numV(o); 457 } else { 458 if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp))) { 459 if (succ) *succ = 0; 460 return 0; 461 } 462 if (tvisint(&tmp)) { 463 return (lua_Integer)intV(&tmp); 464 } 465 n = numV(&tmp); 466 } 467 #if LJ_64 468 return (lua_Integer)n; 469 #else 470 return lj_num2int(n); 471 #endif 472 } 473 LUA_API lua_Integer lua_tointeger(lua_State *L, int idx) 474 { 475 return lua_tointegerx(L, idx, NULL); 476 } 477 478 /* TBD: check casts */ 479 LUA_API lua_Unsigned lua_tounsigned(lua_State *L, int idx) 480 { 481 return (lua_Unsigned) lua_tointeger(L, idx); 482 } 483 LUA_API lua_Unsigned lua_tounsignedx(lua_State *L, int idx, int *succ) 484 { 485 return (lua_Unsigned) lua_tointegerx(L, idx, succ); 486 } 487 488 LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int idx) 489 { 490 cTValue *o = index2adr(L, idx); 491 TValue tmp; 492 lua_Number n; 493 if (LJ_LIKELY(tvisint(o))) { 494 return intV(o); 495 } else if (LJ_LIKELY(tvisnum(o))) { 496 n = numV(o); 497 } else { 498 if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp))) 499 lj_err_argt(L, idx, LUA_TNUMBER); 500 if (tvisint(&tmp)) 501 return (lua_Integer)intV(&tmp); 502 n = numV(&tmp); 503 } 504 #if LJ_64 505 return (lua_Integer)n; 506 #else 507 return lj_num2int(n); 508 #endif 509 } 510 511 LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int idx, lua_Integer def) 512 { 513 cTValue *o = index2adr(L, idx); 514 TValue tmp; 515 lua_Number n; 516 if (LJ_LIKELY(tvisint(o))) { 517 return intV(o); 518 } else if (LJ_LIKELY(tvisnum(o))) { 519 n = numV(o); 520 } else if (tvisnil(o)) { 521 return def; 522 } else { 523 if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp))) 524 lj_err_argt(L, idx, LUA_TNUMBER); 525 if (tvisint(&tmp)) 526 return (lua_Integer)intV(&tmp); 527 n = numV(&tmp); 528 } 529 #if LJ_64 530 return (lua_Integer)n; 531 #else 532 return lj_num2int(n); 533 #endif 534 } 535 536 LUALIB_API lua_Unsigned luaL_checkunsigned (lua_State *L, int narg) { 537 int isnum; 538 lua_Unsigned d = lua_tounsignedx(L, narg, &isnum); 539 if (!isnum) 540 lj_err_argt(L, narg, LUA_TNUMBER); 541 return d; 542 } 543 544 /* TBD: check cast semantics */ 545 LUALIB_API lua_Unsigned luaL_optunsigned(lua_State *L, int idx, lua_Unsigned def) 546 { 547 return (lua_Unsigned) luaL_optinteger(L, idx, def); 548 } 549 550 LUA_API int lua_toboolean(lua_State *L, int idx) 551 { 552 cTValue *o = index2adr(L, idx); 553 return tvistruecond(o); 554 } 555 556 LUA_API const char *lua_tolstring(lua_State *L, int idx, size_t *len) 557 { 558 TValue *o = index2adr(L, idx); 559 GCstr *s; 560 if (LJ_LIKELY(tvisstr(o))) { 561 s = strV(o); 562 } else if (tvisnumber(o)) { 563 lj_gc_check(L); 564 o = index2adr(L, idx); /* GC may move the stack. */ 565 s = lj_strfmt_number(L, o); 566 setstrV(L, o, s); 567 } else { 568 if (len != NULL) *len = 0; 569 return NULL; 570 } 571 if (len != NULL) *len = s->len; 572 return strdata(s); 573 } 574 575 LUALIB_API const char *luaL_checklstring(lua_State *L, int idx, size_t *len) 576 { 577 const char *res = lua_tolstring(L, idx, len); 578 if (!res) lj_err_argt(L, idx, LUA_TSTRING); 579 return res; 580 } 581 582 583 LUALIB_API const char *luaL_optlstring(lua_State *L, int idx, 584 const char *def, size_t *len) 585 { 586 TValue *o = index2adr(L, idx); 587 GCstr *s; 588 if (LJ_LIKELY(tvisstr(o))) { 589 s = strV(o); 590 } else if (tvisnil(o)) { 591 if (len != NULL) *len = def ? strlen(def) : 0; 592 return def; 593 } else if (tvisnumber(o)) { 594 lj_gc_check(L); 595 o = index2adr(L, idx); /* GC may move the stack. */ 596 s = lj_strfmt_number(L, o); 597 setstrV(L, o, s); 598 } else { 599 lj_err_argt(L, idx, LUA_TSTRING); 600 } 601 if (len != NULL) *len = s->len; 602 return strdata(s); 603 } 604 605 LUALIB_API int luaL_checkoption(lua_State *L, int idx, const char *def, 606 const char *const lst[]) 607 { 608 ptrdiff_t i; 609 const char *s = lua_tolstring(L, idx, NULL); 610 if (s == NULL && (s = def) == NULL) 611 lj_err_argt(L, idx, LUA_TSTRING); 612 for (i = 0; lst[i]; i++) 613 if (strcmp(lst[i], s) == 0) 614 return (int)i; 615 lj_err_argv(L, idx, LJ_ERR_INVOPTM, s); 616 } 617 618 LUA_API size_t lua_rawlen(lua_State *L, int idx) 619 { 620 TValue *o = index2adr(L, idx); 621 if (tvisstr(o)) { 622 return strV(o)->len; 623 } else if (tvistab(o)) { 624 return (size_t)lj_tab_len(tabV(o)); 625 } else if (tvisudata(o)) { 626 return udataV(o)->len; 627 } 628 return 0; 629 } 630 631 LUA_API size_t lua_objlen(lua_State *L, int idx) 632 { 633 TValue *o = index2adr(L, idx); 634 if (tvisnumber(o)) { 635 GCstr *s = lj_strfmt_number(L, o); 636 setstrV(L, o, s); 637 return s->len; 638 } 639 return lua_rawlen(L, idx); 640 } 641 642 LUA_API lua_CFunction lua_tocfunction(lua_State *L, int idx) 643 { 644 cTValue *o = index2adr(L, idx); 645 if (tvisfunc(o)) { 646 BCOp op = bc_op(*mref(funcV(o)->c.pc, BCIns)); 647 if (op == BC_FUNCC || op == BC_FUNCCW) 648 return funcV(o)->c.f; 649 } 650 return NULL; 651 } 652 653 LUA_API void *lua_touserdata(lua_State *L, int idx) 654 { 655 cTValue *o = index2adr(L, idx); 656 if (tvisudata(o)) 657 return uddata(udataV(o)); 658 else if (tvislightud(o)) 659 return lightudV(o); 660 else 661 return NULL; 662 } 663 664 LUA_API lua_State *lua_tothread(lua_State *L, int idx) 665 { 666 cTValue *o = index2adr(L, idx); 667 return (!tvisthread(o)) ? NULL : threadV(o); 668 } 669 670 LUA_API const void *lua_topointer(lua_State *L, int idx) 671 { 672 return lj_obj_ptr(index2adr(L, idx)); 673 } 674 675 /* -- Stack setters (object creation) ------------------------------------- */ 676 677 LUA_API void lua_pushnil(lua_State *L) 678 { 679 setnilV(L->top); 680 incr_top(L); 681 } 682 683 LUA_API void lua_pushnumber(lua_State *L, lua_Number n) 684 { 685 setnumV(L->top, n); 686 if (LJ_UNLIKELY(tvisnan(L->top))) 687 setnanV(L->top); /* Canonicalize injected NaNs. */ 688 incr_top(L); 689 } 690 691 LUA_API void lua_pushinteger(lua_State *L, lua_Integer n) 692 { 693 setintptrV(L->top, n); 694 incr_top(L); 695 } 696 697 /* TBD: casts */ 698 LUA_API void lua_pushunsigned(lua_State *L, lua_Unsigned n) 699 { 700 setint64V(L->top, n); 701 incr_top(L); 702 } 703 704 705 LUA_API void lua_pushlstring(lua_State *L, const char *str, size_t len) 706 { 707 GCstr *s; 708 lj_gc_check(L); 709 s = lj_str_new(L, str, len); 710 setstrV(L, L->top, s); 711 incr_top(L); 712 } 713 714 LUA_API void lua_pushstring(lua_State *L, const char *str) 715 { 716 if (str == NULL) { 717 setnilV(L->top); 718 } else { 719 GCstr *s; 720 lj_gc_check(L); 721 s = lj_str_newz(L, str); 722 setstrV(L, L->top, s); 723 } 724 incr_top(L); 725 } 726 727 LUA_API const char *lua_pushvfstring(lua_State *L, const char *fmt, 728 va_list argp) 729 { 730 lj_gc_check(L); 731 return lj_strfmt_pushvf(L, fmt, argp); 732 } 733 734 LUA_API const char *lua_pushfstring(lua_State *L, const char *fmt, ...) 735 { 736 const char *ret; 737 va_list argp; 738 lj_gc_check(L); 739 va_start(argp, fmt); 740 ret = lj_strfmt_pushvf(L, fmt, argp); 741 va_end(argp); 742 return ret; 743 } 744 745 LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction f, int n) 746 { 747 GCfunc *fn; 748 lj_gc_check(L); 749 api_checknelems(L, n); 750 fn = lj_func_newC(L, (MSize)n, getcurrenv(L)); 751 fn->c.f = f; 752 L->top -= n; 753 while (n--) 754 copyTV(L, &fn->c.upvalue[n], L->top+n); 755 setfuncV(L, L->top, fn); 756 lua_assert(iswhite(obj2gco(fn))); 757 incr_top(L); 758 } 759 760 LUA_API void lua_pushboolean(lua_State *L, int b) 761 { 762 setboolV(L->top, (b != 0)); 763 incr_top(L); 764 } 765 766 LUA_API void lua_pushlightuserdata(lua_State *L, void *p) 767 { 768 setlightudV(L->top, checklightudptr(L, p)); 769 incr_top(L); 770 } 771 772 LUA_API void lua_pushuserdata_native(lua_State *L, void *p) 773 { 774 setudataV(L, L->top, checklightudptr(L, p)); /* Checks 1<<47 range, not type. */ 775 incr_top(L); 776 } 777 778 LUA_API void lua_createtable(lua_State *L, int narray, int nrec) 779 { 780 lj_gc_check(L); 781 settabV(L, L->top, lj_tab_new_ah(L, narray, nrec)); 782 incr_top(L); 783 } 784 785 LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname) 786 { 787 GCtab *regt = tabV(registry(L)); 788 TValue *tv = lj_tab_setstr(L, regt, lj_str_newz(L, tname)); 789 if (tvisnil(tv)) { 790 GCtab *mt = lj_tab_new(L, 0, 1); 791 settabV(L, tv, mt); 792 settabV(L, L->top++, mt); 793 lj_gc_anybarriert(L, regt); 794 return 1; 795 } else { 796 copyTV(L, L->top++, tv); 797 return 0; 798 } 799 } 800 801 LUA_API int lua_pushthread(lua_State *L) 802 { 803 setthreadV(L, L->top, L); 804 incr_top(L); 805 return (mainthread(G(L)) == L); 806 } 807 808 LUA_API lua_State *lua_newthread(lua_State *L) 809 { 810 lua_State *L1; 811 lj_gc_check(L); 812 L1 = lj_state_new(L); 813 setthreadV(L, L->top, L1); 814 incr_top(L); 815 return L1; 816 } 817 818 LUA_API void *lua_newuserdata(lua_State *L, size_t size) 819 { 820 GCudata *ud; 821 lj_gc_check(L); 822 if (size > LJ_MAX_UDATA) 823 lj_err_msg(L, LJ_ERR_UDATAOV); 824 ud = lj_udata_new(L, (MSize)size, getcurrenv(L)); 825 setudataV(L, L->top, ud); 826 incr_top(L); 827 return uddata(ud); 828 } 829 830 LUA_API void lua_concat(lua_State *L, int n) 831 { 832 api_checknelems(L, n); 833 if (n >= 2) { 834 n--; 835 do { 836 TValue *top = lj_meta_cat(L, L->top-1, -n); 837 if (top == NULL) { 838 L->top -= n; 839 break; 840 } 841 n -= (int)(L->top - top); 842 L->top = top+2; 843 lj_vm_call(L, top, 1+1); 844 L->top -= 1+LJ_FR2; 845 copyTV(L, L->top-1, L->top+LJ_FR2); 846 } while (--n > 0); 847 } else if (n == 0) { /* Push empty string. */ 848 setstrV(L, L->top, &G(L)->strempty); 849 incr_top(L); 850 } 851 /* else n == 1: nothing to do. */ 852 } 853 854 /* -- Object getters ------------------------------------------------------ */ 855 856 LUA_API int lua_gettable(lua_State *L, int idx) 857 { 858 cTValue *v, *t = index2adr(L, idx); 859 api_checkvalidindex(L, t); 860 v = lj_meta_tget(L, t, L->top-1); 861 if (v == NULL) { 862 L->top += 2; 863 lj_vm_call(L, L->top-2, 1+1); 864 L->top -= 2+LJ_FR2; 865 v = L->top+1+LJ_FR2; 866 } 867 copyTV(L, L->top-1, v); 868 return ljx_tv2type(L, v); 869 } 870 871 LUA_API int lua_geti(lua_State *L, int idx, lua_Integer i) 872 { 873 cTValue *v, *t = index2adr(L, idx); 874 if (!(v = lj_tab_getint(tabV(t), i))) { 875 TValue k; 876 setnumV(&k, (lua_Number)i); 877 v = lj_meta_tget(L, t, &k); 878 } 879 if (v == NULL) { 880 L->top += 2; 881 lj_vm_call(L, L->top-2, 1+1); 882 L->top -= 2+LJ_FR2; 883 v = L->top+1+LJ_FR2; 884 } 885 copyTV(L, L->top-1, v); 886 return ljx_tv2type(L, v); 887 } 888 889 LUA_API int lua_getfield(lua_State *L, int idx, const char *k) 890 { 891 cTValue *v, *t = index2adr(L, idx); 892 TValue key; 893 api_checkvalidindex(L, t); 894 setstrV(L, &key, lj_str_newz(L, k)); 895 v = lj_meta_tget(L, t, &key); 896 if (v == NULL) { 897 L->top += 2; 898 lj_vm_call(L, L->top-2, 1+1); 899 L->top -= 2+LJ_FR2; 900 v = L->top+1+LJ_FR2; 901 } 902 copyTV(L, L->top, v); 903 incr_top(L); 904 return ljx_tv2type(L, v); 905 } 906 907 LUA_API int lua_rawget(lua_State *L, int idx) 908 { 909 cTValue *v, *t = index2adr(L, idx); 910 api_check(L, tvistab(t)); 911 copyTV(L, L->top-1, (v = lj_tab_get(L, tabV(t), L->top-1))); 912 return ljx_tv2type(L, v); 913 } 914 915 916 LUA_API int lua_rawgeti(lua_State *L, int idx, int n) 917 { 918 cTValue *v, *t = index2adr(L, idx); 919 api_check(L, tvistab(t)); 920 v = lj_tab_getint(tabV(t), n); 921 if (v) { 922 copyTV(L, L->top, v); 923 } else { 924 setnilV(L->top); 925 } 926 incr_top(L); 927 return ljx_tv2type(L, v); 928 } 929 930 LUA_API int lua_rawgetp(lua_State *L, int idx, const void *p) 931 { 932 cTValue *v, *t = index2adr(L, idx); 933 TValue key; 934 api_check(L, tvistab(t)); 935 setlightudV(&key, (void*)p); 936 v = lj_tab_get(L, tabV(t), &key); 937 if (v) { 938 copyTV(L, L->top, v); 939 } else { 940 setnilV(L->top); 941 } 942 incr_top(L); 943 return ljx_tv2type(L, v); 944 } 945 946 947 LUA_API int lua_getmetatable(lua_State *L, int idx) 948 { 949 cTValue *o = index2adr(L, idx); 950 GCtab *mt = NULL; 951 if (tvistab(o)) 952 mt = tabref(tabV(o)->metatable); 953 else if (tvisudata(o)) 954 mt = tabref(udataV(o)->metatable); 955 else 956 mt = tabref(basemt_obj(G(L), o)); 957 if (mt == NULL) 958 return 0; 959 settabV(L, L->top, mt); 960 incr_top(L); 961 return 1; 962 } 963 964 LUALIB_API int luaL_getmetafield(lua_State *L, int idx, const char *field) 965 { 966 if (lua_getmetatable(L, idx)) { 967 cTValue *tv = lj_tab_getstr(tabV(L->top-1), lj_str_newz(L, field)); 968 if (tv && !tvisnil(tv)) { 969 copyTV(L, L->top-1, tv); 970 return ljx_tv2type(L, tv); 971 } 972 L->top--; 973 } 974 return LUA_TNIL; 975 } 976 977 LUA_API void lua_getfenv(lua_State *L, int idx) 978 { 979 cTValue *o = index2adr(L, idx); 980 api_checkvalidindex(L, o); 981 if (tvisfunc(o)) { 982 settabV(L, L->top, tabref(funcV(o)->c.env)); 983 } else if (tvisudata(o)) { 984 getuservalue(L, udataV(o), L->top); 985 } else if (tvisthread(o)) { 986 settabV(L, L->top, tabref(threadV(o)->env)); 987 } else { 988 setnilV(L->top); 989 } 990 incr_top(L); 991 } 992 993 LUA_API int lua_getuservalue(lua_State *L, int idx) 994 { 995 if (lua_isuserdata(L, idx)) { 996 lua_getfenv(L, idx); 997 } else { 998 cTValue *s, *o = index2adr(L, idx); 999 api_checkvalidindex(L, o); 1000 s = lj_tab_get(L, tabV(lj_tab_getint(tabV(registry(L)), LUA_RIDX_USERVAL)), o); 1001 if (!s) 1002 setnilV(L->top); 1003 else 1004 copyTV(L, L->top, s); 1005 incr_top(L); 1006 } 1007 return ljx_tv2type(L, L->top-1); 1008 } 1009 1010 LUA_API int lua_next(lua_State *L, int idx) 1011 { 1012 cTValue *t = index2adr(L, idx); 1013 int more; 1014 api_check(L, tvistab(t)); 1015 more = lj_tab_next(L, tabV(t), L->top-1); 1016 if (more) { 1017 incr_top(L); /* Return new key and value slot. */ 1018 } else { /* End of traversal. */ 1019 L->top--; /* Remove key slot. */ 1020 } 1021 return more; 1022 } 1023 1024 LUA_API const char *lua_getupvalue(lua_State *L, int idx, int n) 1025 { 1026 TValue *val; 1027 const char *name = lj_debug_uvnamev(index2adr(L, idx), (uint32_t)(n-1), &val); 1028 if (name) { 1029 copyTV(L, L->top, val); 1030 incr_top(L); 1031 } 1032 return name; 1033 } 1034 1035 LUA_API void *lua_upvalueid(lua_State *L, int idx, int n) 1036 { 1037 GCfunc *fn = funcV(index2adr(L, idx)); 1038 n--; 1039 api_check(L, (uint32_t)n < fn->l.nupvalues); 1040 return isluafunc(fn) ? (void *)gcref(fn->l.uvptr[n]) : 1041 (void *)&fn->c.upvalue[n]; 1042 } 1043 1044 LUA_API void lua_upvaluejoin(lua_State *L, int idx1, int n1, int idx2, int n2) 1045 { 1046 GCfunc *fn1 = funcV(index2adr(L, idx1)); 1047 GCfunc *fn2 = funcV(index2adr(L, idx2)); 1048 n1--; n2--; 1049 api_check(L, isluafunc(fn1) && (uint32_t)n1 < fn1->l.nupvalues); 1050 api_check(L, isluafunc(fn2) && (uint32_t)n2 < fn2->l.nupvalues); 1051 setgcrefr(fn1->l.uvptr[n1], fn2->l.uvptr[n2]); 1052 lj_gc_objbarrier(L, fn1, gcref(fn1->l.uvptr[n1])); 1053 } 1054 1055 LUALIB_API void *luaL_testudata(lua_State *L, int idx, const char *tname) 1056 { 1057 cTValue *o = index2adr(L, idx); 1058 if (tvisudata(o)) { 1059 GCudata *ud = udataV(o); 1060 cTValue *tv = lj_tab_getstr(tabV(registry(L)), lj_str_newz(L, tname)); 1061 if (tv && tvistab(tv) && tabV(tv) == tabref(ud->metatable)) 1062 return uddata(ud); 1063 } 1064 return NULL; 1065 } 1066 1067 LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { 1068 void *p = luaL_testudata(L, ud, tname); 1069 if (p == NULL) lj_err_argtype(L, ud, tname); 1070 return p; 1071 } 1072 1073 /* -- Object setters ------------------------------------------------------ */ 1074 LUA_API void lua_settable(lua_State *L, int idx) 1075 { 1076 TValue *o; 1077 cTValue *t = index2adr(L, idx); 1078 api_checknelems(L, 2); 1079 api_checkvalidindex(L, t); 1080 o = lj_meta_tset(L, t, L->top-2); 1081 if (o) { 1082 /* NOBARRIER: lj_meta_tset ensures the table is not black. */ 1083 L->top -= 2; 1084 copyTV(L, o, L->top+1); 1085 } else { 1086 TValue *base = L->top; 1087 copyTV(L, base+2, base-3-2*LJ_FR2); 1088 L->top = base+3; 1089 lj_vm_call(L, base, 0+1); 1090 L->top -= 3+LJ_FR2; 1091 } 1092 } 1093 1094 LUA_API void lua_setfield(lua_State *L, int idx, const char *k) 1095 { 1096 TValue *o; 1097 TValue key; 1098 cTValue *t = index2adr(L, idx); 1099 api_checknelems(L, 1); 1100 api_checkvalidindex(L, t); 1101 setstrV(L, &key, lj_str_newz(L, k)); 1102 o = lj_meta_tset(L, t, &key); 1103 if (o) { 1104 /* NOBARRIER: lj_meta_tset ensures the table is not black. */ 1105 copyTV(L, o, --L->top); 1106 } else { 1107 TValue *base = L->top; 1108 copyTV(L, base+2, base-3-2*LJ_FR2); 1109 L->top = base+3; 1110 lj_vm_call(L, base, 0+1); 1111 L->top -= 2+LJ_FR2; 1112 } 1113 } 1114 1115 LUA_API void lua_seti(lua_State *L, int idx, lua_Integer i) 1116 { 1117 TValue *o, k; 1118 cTValue *t = index2adr(L, idx); 1119 api_checknelems(L, 1); 1120 api_checkvalidindex(L, t); 1121 setnumV(&k, (lua_Number)i); 1122 o = lj_meta_tset(L, t, &k); 1123 if (o) { 1124 /* NOBARRIER: lj_meta_tset ensures the table is not black. */ 1125 copyTV(L, o, --L->top); 1126 } else { 1127 TValue *base = L->top; 1128 copyTV(L, base+2, base-3-2*LJ_FR2); 1129 L->top = base+3; 1130 lj_vm_call(L, base, 0+1); 1131 L->top -= 2+LJ_FR2; 1132 } 1133 } 1134 1135 #if LJ_ABIVER!=51 1136 LUA_API void lua_setglobal (lua_State *L, const char *var) 1137 { 1138 return lua_setfield(L, LUA_GLOBALSINDEX, var); 1139 } 1140 1141 LUA_API int lua_getglobal (lua_State *L, const char *var) 1142 { 1143 return lua_getfield(L, LUA_GLOBALSINDEX, var); 1144 } 1145 #endif 1146 1147 LUA_API void lua_rawset(lua_State *L, int idx) 1148 { 1149 GCtab *t = tabV(index2adr(L, idx)); 1150 TValue *dst, *key; 1151 api_checknelems(L, 2); 1152 key = L->top-2; 1153 dst = lj_tab_set(L, t, key); 1154 copyTV(L, dst, key+1); 1155 lj_gc_anybarriert(L, t); 1156 L->top = key; 1157 } 1158 1159 LUA_API void lua_rawsetp(lua_State *L, int idx, const void *p) 1160 { 1161 GCtab *t = tabV(index2adr(L, idx)); 1162 TValue *dst, key; 1163 api_checknelems(L, 1); 1164 setlightudV(&key, (void*)p); 1165 dst = lj_tab_set(L, t, &key); 1166 copyTV(L, dst, L->top-1); 1167 lj_gc_anybarriert(L, t); 1168 L->top--; 1169 } 1170 1171 LUA_API void lua_rawseti(lua_State *L, int idx, int n) 1172 { 1173 GCtab *t = tabV(index2adr(L, idx)); 1174 TValue *dst, *src; 1175 api_checknelems(L, 1); 1176 dst = lj_tab_setint(L, t, n); 1177 src = L->top-1; 1178 copyTV(L, dst, src); 1179 lj_gc_barriert(L, t, dst); 1180 L->top = src; 1181 } 1182 1183 LUA_API int lua_setmetatable(lua_State *L, int idx) 1184 { 1185 global_State *g; 1186 GCtab *mt; 1187 cTValue *o = index2adr(L, idx); 1188 api_checknelems(L, 1); 1189 api_checkvalidindex(L, o); 1190 if (tvisnil(L->top-1)) { 1191 mt = NULL; 1192 } else { 1193 api_check(L, tvistab(L->top-1)); 1194 mt = tabV(L->top-1); 1195 } 1196 g = G(L); 1197 if (tvistab(o)) { 1198 setgcref(tabV(o)->metatable, obj2gco(mt)); 1199 if (mt) { 1200 #if !LJ_51 1201 if (lj_meta_fast(L, mt, MM_gc)) 1202 lj_gc_tab_finalized(L, gcval(o)); 1203 #endif 1204 lj_gc_objbarriert(L, tabV(o), mt); 1205 } 1206 } else if (tvisudata(o)) { 1207 setgcref(udataV(o)->metatable, obj2gco(mt)); 1208 if (mt) { 1209 /* Only 5.3 has ressurections */ 1210 #if LJ_53 1211 if (lj_meta_fast(L, mt, MM_gc)) 1212 clearfinalized(gcval(o)); /* Resurrect. */ 1213 #endif 1214 lj_gc_objbarrier(L, gcV(o), mt); 1215 } 1216 } else { 1217 /* Flush cache, since traces specialize to basemt. But not during __gc. */ 1218 if (lj_trace_flushall(L)) 1219 lj_err_caller(L, LJ_ERR_NOGCMM); 1220 if (tvisbool(o)) { 1221 /* NOBARRIER: basemt is a GC root. */ 1222 setgcref(basemt_it(g, LJ_TTRUE), obj2gco(mt)); 1223 setgcref(basemt_it(g, LJ_TFALSE), obj2gco(mt)); 1224 } else { 1225 /* NOBARRIER: basemt is a GC root. */ 1226 setgcref(basemt_obj(g, o), obj2gco(mt)); 1227 } 1228 } 1229 L->top--; 1230 return 1; 1231 } 1232 1233 LUA_API int lua_setfenv(lua_State *L, int idx) 1234 { 1235 cTValue *o = index2adr(L, idx); 1236 GCtab *t; 1237 api_checknelems(L, 1); 1238 api_checkvalidindex(L, o); 1239 if (tvisudata(o)) { 1240 api_check(L, tvisgcv(L->top-1)); 1241 setuservalue(L, udataV(o), L->top-1); 1242 } else { 1243 api_check(L, tvistab(L->top-1)); 1244 t = tabV(L->top-1); 1245 if (tvisfunc(o)) { 1246 setgcref(funcV(o)->c.env, obj2gco(t)); 1247 } else if (tvisthread(o)) { 1248 setgcref(threadV(o)->env, obj2gco(t)); 1249 } else { 1250 L->top--; 1251 return 0; 1252 } 1253 } 1254 lj_gc_objbarrier(L, gcV(o), gcV(L->top-1)); 1255 L->top--; 1256 return 1; 1257 } 1258 1259 /* Set user value for userdata or table. Any value can be 1260 * associated; not just tables like in Lua 5.2. */ 1261 LUA_API void lua_setuservalue(lua_State *L, int idx) 1262 { 1263 if (lua_isuserdata(L, idx)) { 1264 lua_setfenv(L, idx); 1265 } else { 1266 /* Tables don't have room for env field (occupied by array pointer instead). 1267 * Introducing one would be wasteful - instead, we keep uservalues in a weak 1268 * side-table. */ 1269 api_checknelems(L, 1); 1270 api_check(L, lua_istable(L, idx)); 1271 cTValue *o = index2adr(L, idx); 1272 GCtab *userval = tabV(lj_tab_getint(tabV(registry(L)), LUA_RIDX_USERVAL)); 1273 TValue *d = lj_tab_set(L, userval, o); 1274 copyTV(L, d, L->top-1); 1275 lj_gc_anybarriert(L, userval); 1276 L->top--; 1277 } 1278 } 1279 1280 LUA_API const char *lua_setupvalue(lua_State *L, int idx, int n) 1281 { 1282 cTValue *f = index2adr(L, idx); 1283 GCfunc *fn = funcV(f); 1284 TValue *val; 1285 const char *name; 1286 api_checknelems(L, 1); 1287 name = lj_debug_uvnamev(f, (uint32_t)(n-1), &val); 1288 if (name) { 1289 L->top--; 1290 /* Is it global env uv? XXX move to lj_func */ 1291 if (isluafunc(fn) && tvistab(L->top) && (gcref(fn->l.uvptr[n-1])->uv.flags & UV_ENV)) { 1292 GCfunc *pfn; 1293 /* Then scan linked closures and set their _ENV too */ 1294 for (pfn = &obj2gco(gcref(fn->l.next_ENV))->fn; pfn != fn; pfn = &obj2gco(gcref(pfn->l.next_ENV))->fn) { 1295 setgcref(pfn->l.env, obj2gco(tabV(L->top))); 1296 lj_gc_barrier(L, pfn, L->top); 1297 } 1298 } 1299 copyTV(L, val, L->top); 1300 lj_gc_barrier(L, funcV(f), L->top); 1301 } 1302 return name; 1303 } 1304 1305 /* -- Calls --------------------------------------------------------------- */ 1306 1307 #if LJ_FR2 1308 static TValue *api_call_base(lua_State *L, int nargs) 1309 { 1310 TValue *o = L->top, *base = o - nargs; 1311 L->top = o+1; 1312 for (; o > base; o--) copyTV(L, o, o-1); 1313 setnilV(o); 1314 return o+1; 1315 } 1316 #else 1317 #define api_call_base(L, nargs) (L->top - (nargs)) 1318 #endif 1319 1320 LUA_API void lua_call(lua_State *L, int nargs, int nresults) 1321 { 1322 api_check(L, L->status == 0 || L->status == LUA_ERRERR); 1323 api_checknelems(L, nargs+1); 1324 lj_vm_call(L, api_call_base(L, nargs), nresults+1); 1325 } 1326 1327 /* XXX TBD these are not good at all. */ 1328 LUA_API void lua_callk(lua_State *L, int nargs, int nresults, 1329 lua_KContext ctx, lua_KFunction k) 1330 { 1331 lua_call(L, nargs, nresults); 1332 } 1333 1334 LUA_API int lua_pcallk(lua_State *L, int nargs, int nresults, int errfunc, 1335 lua_KContext ctx, lua_KFunction k) 1336 { 1337 return lua_pcall(L, nargs, nresults, errfunc); 1338 } 1339 LUA_API int (lua_yieldk)(lua_State *L, int nresults, lua_KContext ctx, 1340 lua_KFunction k) 1341 { 1342 return lua_yield(L, nresults); 1343 } 1344 1345 1346 LUA_API int lua_pcall(lua_State *L, int nargs, int nresults, int errfunc) 1347 { 1348 global_State *g = G(L); 1349 uint8_t oldh = hook_save(g); 1350 ptrdiff_t ef; 1351 int status; 1352 api_check(L, L->status == 0 || L->status == LUA_ERRERR); 1353 api_checknelems(L, nargs+1); 1354 if (errfunc == 0) { 1355 ef = 0; 1356 } else { 1357 cTValue *o = stkindex2adr(L, errfunc); 1358 api_checkvalidindex(L, o); 1359 ef = savestack(L, o); 1360 } 1361 status = lj_vm_pcall(L, api_call_base(L, nargs), nresults+1, ef); 1362 if (status) hook_restore(g, oldh); 1363 return status; 1364 } 1365 1366 static TValue *cpcall(lua_State *L, lua_CFunction func, void *ud) 1367 { 1368 GCfunc *fn = lj_func_newC(L, 0, getcurrenv(L)); 1369 TValue *top = L->top; 1370 fn->c.f = func; 1371 setfuncV(L, top++, fn); 1372 if (LJ_FR2) setnilV(top++); 1373 setlightudV(top++, checklightudptr(L, ud)); 1374 cframe_nres(L->cframe) = 1+0; /* Zero results. */ 1375 L->top = top; 1376 return top-1; /* Now call the newly allocated C function. */ 1377 } 1378 1379 LUA_API int lua_cpcall(lua_State *L, lua_CFunction func, void *ud) 1380 { 1381 global_State *g = G(L); 1382 uint8_t oldh = hook_save(g); 1383 int status; 1384 api_check(L, L->status == 0 || L->status == LUA_ERRERR); 1385 status = lj_vm_cpcall(L, func, ud, cpcall); 1386 if (status) hook_restore(g, oldh); 1387 return status; 1388 } 1389 1390 LUALIB_API int luaL_callmeta(lua_State *L, int idx, const char *field) 1391 { 1392 if (luaL_getmetafield(L, idx, field)) { 1393 TValue *top = L->top--; 1394 if (LJ_FR2) setnilV(top++); 1395 copyTV(L, top++, index2adr(L, idx)); 1396 L->top = top; 1397 lj_vm_call(L, top-1, 1+1); 1398 return 1; 1399 } 1400 return 0; 1401 } 1402 1403 LUALIB_API void lua_len(lua_State *L, int i) { 1404 switch (lua_type(L, i)) { 1405 case LUA_TSTRING: /* fall through */ 1406 case LUA_TTABLE: 1407 #if !LJ_51 1408 if (luaL_callmeta(L, i, "__len")) 1409 break; 1410 #endif 1411 lua_pushunsigned(L, lua_objlen(L, i)); 1412 break; 1413 case LUA_TUSERDATA: 1414 if (luaL_callmeta(L, i, "__len")) 1415 break; 1416 /* maybe fall through */ 1417 default: 1418 lj_err_callerv(L, LJ_ERR_BADLEN, lua_typename(L, lua_type(L, i))); 1419 } 1420 } 1421 1422 LUALIB_API int luaL_len(lua_State *L, int i) { 1423 int res = 0, isnum = 0; 1424 lj_gc_check(L); 1425 lua_len(L, i); 1426 res = (int)lua_tointegerx(L, -1, &isnum); 1427 lua_pop(L, 1); 1428 if (!isnum) 1429 lj_err_callerv(L, LJ_ERR_BADLENNUM, lua_typename(L, lua_type(L, i))); 1430 return res; 1431 } 1432 1433 1434 1435 1436 LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { 1437 if (!luaL_callmeta(L, idx, "__tostring")) { /* no metafield? */ 1438 switch (lua_type(L, idx)) { 1439 case LUA_TNUMBER: 1440 case LUA_TSTRING: 1441 lua_pushvalue(L, idx); 1442 break; 1443 case LUA_TBOOLEAN: 1444 lua_pushstring(L, (lua_toboolean(L, idx) ? "true" : "false")); 1445 break; 1446 case LUA_TNIL: 1447 lua_pushliteral(L, "nil"); 1448 break; 1449 default: 1450 lua_pushfstring(L, "%s: %p", lua_typename(L, idx), lua_topointer(L, idx)); 1451 break; 1452 } 1453 } 1454 return lua_tolstring(L, -1, len); 1455 } 1456 1457 1458 1459 /* -- Coroutine yield and resume ------------------------------------------ */ 1460 1461 LUA_API int lua_yield(lua_State *L, int nresults) 1462 { 1463 void *cf = L->cframe; 1464 global_State *g = G(L); 1465 if (cframe_canyield(cf)) { 1466 cf = cframe_raw(cf); 1467 if (!hook_active(g)) { /* Regular yield: move results down if needed. */ 1468 cTValue *f = L->top - nresults; 1469 if (f > L->base) { 1470 TValue *t = L->base; 1471 while (--nresults >= 0) copyTV(L, t++, f++); 1472 L->top = t; 1473 } 1474 L->cframe = NULL; 1475 L->status = LUA_YIELD; 1476 return -1; 1477 } else { /* Yield from hook: add a pseudo-frame. */ 1478 TValue *top = L->top; 1479 hook_leave(g); 1480 (top++)->u64 = cframe_multres(cf); 1481 setcont(top, lj_cont_hook); 1482 if (LJ_FR2) top++; 1483 setframe_pc(top, cframe_pc(cf)-1); 1484 if (LJ_FR2) top++; 1485 setframe_gc(top, obj2gco(L), LJ_TTHREAD); 1486 setframe_ftsz(top, ((char *)(top+1)-(char *)L->base)+FRAME_CONT); 1487 L->top = L->base = top+1; 1488 #if LJ_TARGET_X64 1489 lj_err_throw(L, LUA_YIELD); 1490 #else 1491 L->cframe = NULL; 1492 L->status = LUA_YIELD; 1493 lj_vm_unwind_c(cf, LUA_YIELD); 1494 #endif 1495 } 1496 } 1497 lj_err_msg(L, LJ_ERR_CYIELD); 1498 return 0; /* unreachable */ 1499 } 1500 1501 LUA_API int lua_resume(lua_State *L, int nargs) 1502 { 1503 if (L->cframe == NULL && L->status <= LUA_YIELD) 1504 return lj_vm_resume(L, 1505 L->status == 0 ? api_call_base(L, nargs) : L->top - nargs, 1506 0, 0); 1507 L->top = L->base; 1508 setstrV(L, L->top, lj_err_str(L, LJ_ERR_COSUSP)); 1509 incr_top(L); 1510 return LUA_ERRRUN; 1511 } 1512 1513 1514 LUA_API void lua_setlevel(lua_State *from, lua_State *to) 1515 { 1516 UNUSED(from); 1517 UNUSED(to); 1518 } 1519 1520 /* -- GC and memory management -------------------------------------------- */ 1521 1522 LUA_API int lua_gc(lua_State *L, int what, int data) 1523 { 1524 global_State *g = G(L); 1525 int res = 0; 1526 switch (what) { 1527 case LUA_GCSTOP: 1528 g->gc.threshold = LJ_MAX_MEM; 1529 break; 1530 case LUA_GCRESTART: 1531 g->gc.threshold = data == -1 ? (g->gc.total/100)*g->gc.pause : g->gc.total; 1532 break; 1533 case LUA_GCCOLLECT: 1534 lj_gc_fullgc(L); 1535 break; 1536 case LUA_GCCOUNT: 1537 res = (int)(g->gc.total >> 10); 1538 break; 1539 case LUA_GCCOUNTB: 1540 res = (int)(g->gc.total & 0x3ff); 1541 break; 1542 case LUA_GCSTEP: { 1543 GCSize a = (GCSize)data << 10; 1544 g->gc.threshold = (a <= g->gc.total) ? (g->gc.total - a) : 0; 1545 while (g->gc.total >= g->gc.threshold) 1546 if (lj_gc_step(L) > 0) { 1547 res = 1; 1548 break; 1549 } 1550 break; 1551 } 1552 case LUA_GCSETPAUSE: 1553 res = (int)(g->gc.pause); 1554 g->gc.pause = (MSize)data; 1555 break; 1556 case LUA_GCSETSTEPMUL: 1557 res = (int)(g->gc.stepmul); 1558 g->gc.stepmul = (MSize)data; 1559 break; 1560 case LUA_GCISRUNNING: 1561 res = (g->gc.threshold != LJ_MAX_MEM); 1562 break; 1563 default: 1564 res = -1; /* Invalid option. */ 1565 } 1566 return res; 1567 } 1568 1569 LUA_API lua_Alloc lua_getallocf(lua_State *L, void **ud) 1570 { 1571 global_State *g = G(L); 1572 if (ud) *ud = g->allocd; 1573 return g->allocf; 1574 } 1575 1576 LUA_API void lua_setallocf(lua_State *L, lua_Alloc f, void *ud) 1577 { 1578 global_State *g = G(L); 1579 g->allocd = ud; 1580 g->allocf = f; 1581 } 1582 1583 LUA_API const lua_Number *lua_version (lua_State *L) { 1584 static const lua_Number version = LUA_VERSION_NUM; 1585 if (L == NULL) return &version; 1586 else return G(L)->version; 1587 } 1588