1 /* $NetBSD: lapi.c,v 1.7 2016/09/08 02:21:31 salazar Exp $ */ 2 3 /* 4 ** Id: lapi.c,v 2.259 2016/02/29 14:27:14 roberto Exp 5 ** Lua API 6 ** See Copyright Notice in lua.h 7 */ 8 9 #define lapi_c 10 #define LUA_CORE 11 12 #include "lprefix.h" 13 14 15 #include <stdarg.h> 16 #ifndef _KERNEL 17 #include <string.h> 18 #endif /* _KERNEL */ 19 20 #include "lua.h" 21 22 #include "lapi.h" 23 #include "ldebug.h" 24 #include "ldo.h" 25 #include "lfunc.h" 26 #include "lgc.h" 27 #include "lmem.h" 28 #include "lobject.h" 29 #include "lstate.h" 30 #include "lstring.h" 31 #include "ltable.h" 32 #include "ltm.h" 33 #include "lundump.h" 34 #include "lvm.h" 35 36 37 38 const char lua_ident[] = 39 "$LuaVersion: " LUA_COPYRIGHT " $" 40 "$LuaAuthors: " LUA_AUTHORS " $"; 41 42 43 /* value at a non-valid index */ 44 #define NONVALIDVALUE cast(TValue *, luaO_nilobject) 45 46 /* corresponding test */ 47 #define isvalid(o) ((o) != luaO_nilobject) 48 49 /* test for pseudo index */ 50 #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX) 51 52 /* test for upvalue */ 53 #define isupvalue(i) ((i) < LUA_REGISTRYINDEX) 54 55 /* test for valid but not pseudo index */ 56 #define isstackindex(i, o) (isvalid(o) && !ispseudo(i)) 57 58 #define api_checkvalidindex(l,o) api_check(l, isvalid(o), "invalid index") 59 60 #define api_checkstackindex(l, i, o) \ 61 api_check(l, isstackindex(i, o), "index not in the stack") 62 63 64 static TValue *index2addr (lua_State *L, int idx) { 65 CallInfo *ci = L->ci; 66 if (idx > 0) { 67 TValue *o = ci->func + idx; 68 api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index"); 69 if (o >= L->top) return NONVALIDVALUE; 70 else return o; 71 } 72 else if (!ispseudo(idx)) { /* negative index */ 73 api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); 74 return L->top + idx; 75 } 76 else if (idx == LUA_REGISTRYINDEX) 77 return &G(L)->l_registry; 78 else { /* upvalues */ 79 idx = LUA_REGISTRYINDEX - idx; 80 api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large"); 81 if (ttislcf(ci->func)) /* light C function? */ 82 return NONVALIDVALUE; /* it has no upvalues */ 83 else { 84 CClosure *func = clCvalue(ci->func); 85 return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE; 86 } 87 } 88 } 89 90 91 /* 92 ** to be called by 'lua_checkstack' in protected mode, to grow stack 93 ** capturing memory errors 94 */ 95 static void growstack (lua_State *L, void *ud) { 96 int size = *(int *)ud; 97 luaD_growstack(L, size); 98 } 99 100 101 LUA_API int lua_checkstack (lua_State *L, int n) { 102 int res; 103 CallInfo *ci = L->ci; 104 lua_lock(L); 105 api_check(L, n >= 0, "negative 'n'"); 106 if (L->stack_last - L->top > n) /* stack large enough? */ 107 res = 1; /* yes; check is OK */ 108 else { /* no; need to grow stack */ 109 int inuse = cast_int(L->top - L->stack) + EXTRA_STACK; 110 if (inuse > LUAI_MAXSTACK - n) /* can grow without overflow? */ 111 res = 0; /* no */ 112 else /* try to grow stack */ 113 res = (luaD_rawrunprotected(L, &growstack, &n) == LUA_OK); 114 } 115 if (res && ci->top < L->top + n) 116 ci->top = L->top + n; /* adjust frame top */ 117 lua_unlock(L); 118 return res; 119 } 120 121 122 LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) { 123 int i; 124 if (from == to) return; 125 lua_lock(to); 126 api_checknelems(from, n); 127 api_check(from, G(from) == G(to), "moving among independent states"); 128 api_check(from, to->ci->top - to->top >= n, "stack overflow"); 129 from->top -= n; 130 for (i = 0; i < n; i++) { 131 setobj2s(to, to->top, from->top + i); 132 to->top++; /* stack already checked by previous 'api_check' */ 133 } 134 lua_unlock(to); 135 } 136 137 138 LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) { 139 lua_CFunction old; 140 lua_lock(L); 141 old = G(L)->panic; 142 G(L)->panic = panicf; 143 lua_unlock(L); 144 return old; 145 } 146 147 148 LUA_API const lua_Number *lua_version (lua_State *L) { 149 static const lua_Number version = LUA_VERSION_NUM; 150 if (L == NULL) return &version; 151 else return G(L)->version; 152 } 153 154 155 156 /* 157 ** basic stack manipulation 158 */ 159 160 161 /* 162 ** convert an acceptable stack index into an absolute index 163 */ 164 LUA_API int lua_absindex (lua_State *L, int idx) { 165 return (idx > 0 || ispseudo(idx)) 166 ? idx 167 : cast_int(L->top - L->ci->func) + idx; 168 } 169 170 171 LUA_API int lua_gettop (lua_State *L) { 172 return cast_int(L->top - (L->ci->func + 1)); 173 } 174 175 176 LUA_API void lua_settop (lua_State *L, int idx) { 177 StkId func = L->ci->func; 178 lua_lock(L); 179 if (idx >= 0) { 180 api_check(L, idx <= L->stack_last - (func + 1), "new top too large"); 181 while (L->top < (func + 1) + idx) 182 setnilvalue(L->top++); 183 L->top = (func + 1) + idx; 184 } 185 else { 186 api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top"); 187 L->top += idx+1; /* 'subtract' index (index is negative) */ 188 } 189 lua_unlock(L); 190 } 191 192 193 /* 194 ** Reverse the stack segment from 'from' to 'to' 195 ** (auxiliary to 'lua_rotate') 196 */ 197 static void reverse (lua_State *L, StkId from, StkId to) { 198 for (; from < to; from++, to--) { 199 TValue temp; 200 setobj(L, &temp, from); 201 setobjs2s(L, from, to); 202 setobj2s(L, to, &temp); 203 } 204 } 205 206 207 /* 208 ** Let x = AB, where A is a prefix of length 'n'. Then, 209 ** rotate x n == BA. But BA == (A^r . B^r)^r. 210 */ 211 LUA_API void lua_rotate (lua_State *L, int idx, int n) { 212 StkId p, t, m; 213 lua_lock(L); 214 t = L->top - 1; /* end of stack segment being rotated */ 215 p = index2addr(L, idx); /* start of segment */ 216 api_checkstackindex(L, idx, p); 217 api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'"); 218 m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */ 219 reverse(L, p, m); /* reverse the prefix with length 'n' */ 220 reverse(L, m + 1, t); /* reverse the suffix */ 221 reverse(L, p, t); /* reverse the entire segment */ 222 lua_unlock(L); 223 } 224 225 226 LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { 227 TValue *fr, *to; 228 lua_lock(L); 229 fr = index2addr(L, fromidx); 230 to = index2addr(L, toidx); 231 api_checkvalidindex(L, to); 232 setobj(L, to, fr); 233 if (isupvalue(toidx)) /* function upvalue? */ 234 luaC_barrier(L, clCvalue(L->ci->func), fr); 235 /* LUA_REGISTRYINDEX does not need gc barrier 236 (collector revisits it before finishing collection) */ 237 lua_unlock(L); 238 } 239 240 241 LUA_API void lua_pushvalue (lua_State *L, int idx) { 242 lua_lock(L); 243 setobj2s(L, L->top, index2addr(L, idx)); 244 api_incr_top(L); 245 lua_unlock(L); 246 } 247 248 249 250 /* 251 ** access functions (stack -> C) 252 */ 253 254 255 LUA_API int lua_type (lua_State *L, int idx) { 256 StkId o = index2addr(L, idx); 257 return (isvalid(o) ? ttnov(o) : LUA_TNONE); 258 } 259 260 261 LUA_API const char *lua_typename (lua_State *L, int t) { 262 UNUSED(L); 263 api_check(L, LUA_TNONE <= t && t < LUA_NUMTAGS, "invalid tag"); 264 return ttypename(t); 265 } 266 267 268 LUA_API int lua_iscfunction (lua_State *L, int idx) { 269 StkId o = index2addr(L, idx); 270 return (ttislcf(o) || (ttisCclosure(o))); 271 } 272 273 274 LUA_API int lua_isinteger (lua_State *L, int idx) { 275 StkId o = index2addr(L, idx); 276 return ttisinteger(o); 277 } 278 279 280 LUA_API int lua_isnumber (lua_State *L, int idx) { 281 lua_Number n; 282 const TValue *o = index2addr(L, idx); 283 return tonumber(o, &n); 284 } 285 286 287 LUA_API int lua_isstring (lua_State *L, int idx) { 288 const TValue *o = index2addr(L, idx); 289 return (ttisstring(o) || cvt2str(o)); 290 } 291 292 293 LUA_API int lua_isuserdata (lua_State *L, int idx) { 294 const TValue *o = index2addr(L, idx); 295 return (ttisfulluserdata(o) || ttislightuserdata(o)); 296 } 297 298 299 LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { 300 StkId o1 = index2addr(L, index1); 301 StkId o2 = index2addr(L, index2); 302 return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0; 303 } 304 305 306 LUA_API void lua_arith (lua_State *L, int op) { 307 lua_lock(L); 308 if (op != LUA_OPUNM && op != LUA_OPBNOT) 309 api_checknelems(L, 2); /* all other operations expect two operands */ 310 else { /* for unary operations, add fake 2nd operand */ 311 api_checknelems(L, 1); 312 setobjs2s(L, L->top, L->top - 1); 313 api_incr_top(L); 314 } 315 /* first operand at top - 2, second at top - 1; result go to top - 2 */ 316 luaO_arith(L, op, L->top - 2, L->top - 1, L->top - 2); 317 L->top--; /* remove second operand */ 318 lua_unlock(L); 319 } 320 321 322 LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { 323 StkId o1, o2; 324 int i = 0; 325 lua_lock(L); /* may call tag method */ 326 o1 = index2addr(L, index1); 327 o2 = index2addr(L, index2); 328 if (isvalid(o1) && isvalid(o2)) { 329 switch (op) { 330 case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break; 331 case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break; 332 case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break; 333 default: api_check(L, 0, "invalid option"); 334 } 335 } 336 lua_unlock(L); 337 return i; 338 } 339 340 341 LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) { 342 size_t sz = luaO_str2num(s, L->top); 343 if (sz != 0) 344 api_incr_top(L); 345 return sz; 346 } 347 348 349 #ifndef _KERNEL 350 LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) { 351 lua_Number n; 352 const TValue *o = index2addr(L, idx); 353 int isnum = tonumber(o, &n); 354 if (!isnum) 355 n = 0; /* call to 'tonumber' may change 'n' even if it fails */ 356 if (pisnum) *pisnum = isnum; 357 return n; 358 } 359 #endif /* _KERNEL */ 360 361 362 LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) { 363 lua_Integer res; 364 const TValue *o = index2addr(L, idx); 365 int isnum = tointeger(o, &res); 366 if (!isnum) 367 res = 0; /* call to 'tointeger' may change 'n' even if it fails */ 368 if (pisnum) *pisnum = isnum; 369 return res; 370 } 371 372 373 LUA_API int lua_toboolean (lua_State *L, int idx) { 374 const TValue *o = index2addr(L, idx); 375 return !l_isfalse(o); 376 } 377 378 379 LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { 380 StkId o = index2addr(L, idx); 381 if (!ttisstring(o)) { 382 if (!cvt2str(o)) { /* not convertible? */ 383 if (len != NULL) *len = 0; 384 return NULL; 385 } 386 lua_lock(L); /* 'luaO_tostring' may create a new string */ 387 luaO_tostring(L, o); 388 luaC_checkGC(L); 389 o = index2addr(L, idx); /* previous call may reallocate the stack */ 390 lua_unlock(L); 391 } 392 if (len != NULL) 393 *len = vslen(o); 394 return svalue(o); 395 } 396 397 398 LUA_API size_t lua_rawlen (lua_State *L, int idx) { 399 StkId o = index2addr(L, idx); 400 switch (ttype(o)) { 401 case LUA_TSHRSTR: return tsvalue(o)->shrlen; 402 case LUA_TLNGSTR: return tsvalue(o)->u.lnglen; 403 case LUA_TUSERDATA: return uvalue(o)->len; 404 case LUA_TTABLE: return luaH_getn(hvalue(o)); 405 default: return 0; 406 } 407 } 408 409 410 LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { 411 StkId o = index2addr(L, idx); 412 if (ttislcf(o)) return fvalue(o); 413 else if (ttisCclosure(o)) 414 return clCvalue(o)->f; 415 else return NULL; /* not a C function */ 416 } 417 418 419 LUA_API void *lua_touserdata (lua_State *L, int idx) { 420 StkId o = index2addr(L, idx); 421 switch (ttnov(o)) { 422 case LUA_TUSERDATA: return getudatamem(uvalue(o)); 423 case LUA_TLIGHTUSERDATA: return pvalue(o); 424 default: return NULL; 425 } 426 } 427 428 429 LUA_API lua_State *lua_tothread (lua_State *L, int idx) { 430 StkId o = index2addr(L, idx); 431 return (!ttisthread(o)) ? NULL : thvalue(o); 432 } 433 434 435 LUA_API const void *lua_topointer (lua_State *L, int idx) { 436 StkId o = index2addr(L, idx); 437 switch (ttype(o)) { 438 case LUA_TTABLE: return hvalue(o); 439 case LUA_TLCL: return clLvalue(o); 440 case LUA_TCCL: return clCvalue(o); 441 case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o))); 442 case LUA_TTHREAD: return thvalue(o); 443 case LUA_TUSERDATA: return getudatamem(uvalue(o)); 444 case LUA_TLIGHTUSERDATA: return pvalue(o); 445 default: return NULL; 446 } 447 } 448 449 450 451 /* 452 ** push functions (C -> stack) 453 */ 454 455 456 LUA_API void lua_pushnil (lua_State *L) { 457 lua_lock(L); 458 setnilvalue(L->top); 459 api_incr_top(L); 460 lua_unlock(L); 461 } 462 463 464 #ifndef _KERNEL 465 LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { 466 lua_lock(L); 467 setfltvalue(L->top, n); 468 api_incr_top(L); 469 lua_unlock(L); 470 } 471 #endif /* _KERNEL */ 472 473 474 LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { 475 lua_lock(L); 476 setivalue(L->top, n); 477 api_incr_top(L); 478 lua_unlock(L); 479 } 480 481 482 /* 483 ** Pushes on the stack a string with given length. Avoid using 's' when 484 ** 'len' == 0 (as 's' can be NULL in that case), due to later use of 485 ** 'memcmp' and 'memcpy'. 486 */ 487 LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { 488 TString *ts; 489 lua_lock(L); 490 ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len); 491 setsvalue2s(L, L->top, ts); 492 api_incr_top(L); 493 luaC_checkGC(L); 494 lua_unlock(L); 495 return getstr(ts); 496 } 497 498 499 LUA_API const char *lua_pushstring (lua_State *L, const char *s) { 500 lua_lock(L); 501 if (s == NULL) 502 setnilvalue(L->top); 503 else { 504 TString *ts; 505 ts = luaS_new(L, s); 506 setsvalue2s(L, L->top, ts); 507 s = getstr(ts); /* internal copy's address */ 508 } 509 api_incr_top(L); 510 luaC_checkGC(L); 511 lua_unlock(L); 512 return s; 513 } 514 515 516 LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt, 517 va_list argp) { 518 const char *ret; 519 lua_lock(L); 520 ret = luaO_pushvfstring(L, fmt, argp); 521 luaC_checkGC(L); 522 lua_unlock(L); 523 return ret; 524 } 525 526 527 LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { 528 const char *ret; 529 va_list argp; 530 lua_lock(L); 531 va_start(argp, fmt); 532 ret = luaO_pushvfstring(L, fmt, argp); 533 va_end(argp); 534 luaC_checkGC(L); 535 lua_unlock(L); 536 return ret; 537 } 538 539 540 LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { 541 lua_lock(L); 542 if (n == 0) { 543 setfvalue(L->top, fn); 544 } 545 else { 546 CClosure *cl; 547 api_checknelems(L, n); 548 api_check(L, n <= MAXUPVAL, "upvalue index too large"); 549 cl = luaF_newCclosure(L, n); 550 cl->f = fn; 551 L->top -= n; 552 while (n--) { 553 setobj2n(L, &cl->upvalue[n], L->top + n); 554 /* does not need barrier because closure is white */ 555 } 556 setclCvalue(L, L->top, cl); 557 } 558 api_incr_top(L); 559 luaC_checkGC(L); 560 lua_unlock(L); 561 } 562 563 564 LUA_API void lua_pushboolean (lua_State *L, int b) { 565 lua_lock(L); 566 setbvalue(L->top, (b != 0)); /* ensure that true is 1 */ 567 api_incr_top(L); 568 lua_unlock(L); 569 } 570 571 572 LUA_API void lua_pushlightuserdata (lua_State *L, void *p) { 573 lua_lock(L); 574 setpvalue(L->top, p); 575 api_incr_top(L); 576 lua_unlock(L); 577 } 578 579 580 LUA_API int lua_pushthread (lua_State *L) { 581 lua_lock(L); 582 setthvalue(L, L->top, L); 583 api_incr_top(L); 584 lua_unlock(L); 585 return (G(L)->mainthread == L); 586 } 587 588 589 590 /* 591 ** get functions (Lua -> stack) 592 */ 593 594 595 static int auxgetstr (lua_State *L, const TValue *t, const char *k) { 596 const TValue *slot; 597 TString *str = luaS_new(L, k); 598 if (luaV_fastget(L, t, str, slot, luaH_getstr)) { 599 setobj2s(L, L->top, slot); 600 api_incr_top(L); 601 } 602 else { 603 setsvalue2s(L, L->top, str); 604 api_incr_top(L); 605 luaV_finishget(L, t, L->top - 1, L->top - 1, slot); 606 } 607 lua_unlock(L); 608 return ttnov(L->top - 1); 609 } 610 611 612 LUA_API int lua_getglobal (lua_State *L, const char *name) { 613 Table *reg = hvalue(&G(L)->l_registry); 614 lua_lock(L); 615 return auxgetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); 616 } 617 618 619 LUA_API int lua_gettable (lua_State *L, int idx) { 620 StkId t; 621 lua_lock(L); 622 t = index2addr(L, idx); 623 luaV_gettable(L, t, L->top - 1, L->top - 1); 624 lua_unlock(L); 625 return ttnov(L->top - 1); 626 } 627 628 629 LUA_API int lua_getfield (lua_State *L, int idx, const char *k) { 630 lua_lock(L); 631 return auxgetstr(L, index2addr(L, idx), k); 632 } 633 634 635 LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { 636 StkId t; 637 const TValue *slot; 638 lua_lock(L); 639 t = index2addr(L, idx); 640 if (luaV_fastget(L, t, n, slot, luaH_getint)) { 641 setobj2s(L, L->top, slot); 642 api_incr_top(L); 643 } 644 else { 645 setivalue(L->top, n); 646 api_incr_top(L); 647 luaV_finishget(L, t, L->top - 1, L->top - 1, slot); 648 } 649 lua_unlock(L); 650 return ttnov(L->top - 1); 651 } 652 653 654 LUA_API int lua_rawget (lua_State *L, int idx) { 655 StkId t; 656 lua_lock(L); 657 t = index2addr(L, idx); 658 api_check(L, ttistable(t), "table expected"); 659 setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1)); 660 lua_unlock(L); 661 return ttnov(L->top - 1); 662 } 663 664 665 LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { 666 StkId t; 667 lua_lock(L); 668 t = index2addr(L, idx); 669 api_check(L, ttistable(t), "table expected"); 670 setobj2s(L, L->top, luaH_getint(hvalue(t), n)); 671 api_incr_top(L); 672 lua_unlock(L); 673 return ttnov(L->top - 1); 674 } 675 676 677 LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { 678 StkId t; 679 TValue k; 680 lua_lock(L); 681 t = index2addr(L, idx); 682 api_check(L, ttistable(t), "table expected"); 683 setpvalue(&k, cast(void *, p)); 684 setobj2s(L, L->top, luaH_get(hvalue(t), &k)); 685 api_incr_top(L); 686 lua_unlock(L); 687 return ttnov(L->top - 1); 688 } 689 690 691 LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { 692 Table *t; 693 lua_lock(L); 694 t = luaH_new(L); 695 sethvalue(L, L->top, t); 696 api_incr_top(L); 697 if (narray > 0 || nrec > 0) 698 luaH_resize(L, t, narray, nrec); 699 luaC_checkGC(L); 700 lua_unlock(L); 701 } 702 703 704 LUA_API int lua_getmetatable (lua_State *L, int objindex) { 705 const TValue *obj; 706 Table *mt; 707 int res = 0; 708 lua_lock(L); 709 obj = index2addr(L, objindex); 710 switch (ttnov(obj)) { 711 case LUA_TTABLE: 712 mt = hvalue(obj)->metatable; 713 break; 714 case LUA_TUSERDATA: 715 mt = uvalue(obj)->metatable; 716 break; 717 default: 718 mt = G(L)->mt[ttnov(obj)]; 719 break; 720 } 721 if (mt != NULL) { 722 sethvalue(L, L->top, mt); 723 api_incr_top(L); 724 res = 1; 725 } 726 lua_unlock(L); 727 return res; 728 } 729 730 731 LUA_API int lua_getuservalue (lua_State *L, int idx) { 732 StkId o; 733 lua_lock(L); 734 o = index2addr(L, idx); 735 api_check(L, ttisfulluserdata(o), "full userdata expected"); 736 getuservalue(L, uvalue(o), L->top); 737 api_incr_top(L); 738 lua_unlock(L); 739 return ttnov(L->top - 1); 740 } 741 742 743 /* 744 ** set functions (stack -> Lua) 745 */ 746 747 /* 748 ** t[k] = value at the top of the stack (where 'k' is a string) 749 */ 750 static void auxsetstr (lua_State *L, const TValue *t, const char *k) { 751 const TValue *slot; 752 TString *str = luaS_new(L, k); 753 api_checknelems(L, 1); 754 if (luaV_fastset(L, t, str, slot, luaH_getstr, L->top - 1)) 755 L->top--; /* pop value */ 756 else { 757 setsvalue2s(L, L->top, str); /* push 'str' (to make it a TValue) */ 758 api_incr_top(L); 759 luaV_finishset(L, t, L->top - 1, L->top - 2, slot); 760 L->top -= 2; /* pop value and key */ 761 } 762 lua_unlock(L); /* lock done by caller */ 763 } 764 765 766 LUA_API void lua_setglobal (lua_State *L, const char *name) { 767 Table *reg = hvalue(&G(L)->l_registry); 768 lua_lock(L); /* unlock done in 'auxsetstr' */ 769 auxsetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); 770 } 771 772 773 LUA_API void lua_settable (lua_State *L, int idx) { 774 StkId t; 775 lua_lock(L); 776 api_checknelems(L, 2); 777 t = index2addr(L, idx); 778 luaV_settable(L, t, L->top - 2, L->top - 1); 779 L->top -= 2; /* pop index and value */ 780 lua_unlock(L); 781 } 782 783 784 LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { 785 lua_lock(L); /* unlock done in 'auxsetstr' */ 786 auxsetstr(L, index2addr(L, idx), k); 787 } 788 789 790 LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { 791 StkId t; 792 const TValue *slot; 793 lua_lock(L); 794 api_checknelems(L, 1); 795 t = index2addr(L, idx); 796 if (luaV_fastset(L, t, n, slot, luaH_getint, L->top - 1)) 797 L->top--; /* pop value */ 798 else { 799 setivalue(L->top, n); 800 api_incr_top(L); 801 luaV_finishset(L, t, L->top - 1, L->top - 2, slot); 802 L->top -= 2; /* pop value and key */ 803 } 804 lua_unlock(L); 805 } 806 807 808 LUA_API void lua_rawset (lua_State *L, int idx) { 809 StkId o; 810 TValue *slot; 811 lua_lock(L); 812 api_checknelems(L, 2); 813 o = index2addr(L, idx); 814 api_check(L, ttistable(o), "table expected"); 815 slot = luaH_set(L, hvalue(o), L->top - 2); 816 setobj2t(L, slot, L->top - 1); 817 invalidateTMcache(hvalue(o)); 818 luaC_barrierback(L, hvalue(o), L->top-1); 819 L->top -= 2; 820 lua_unlock(L); 821 } 822 823 824 LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { 825 StkId o; 826 lua_lock(L); 827 api_checknelems(L, 1); 828 o = index2addr(L, idx); 829 api_check(L, ttistable(o), "table expected"); 830 luaH_setint(L, hvalue(o), n, L->top - 1); 831 luaC_barrierback(L, hvalue(o), L->top-1); 832 L->top--; 833 lua_unlock(L); 834 } 835 836 837 LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { 838 StkId o; 839 TValue k, *slot; 840 lua_lock(L); 841 api_checknelems(L, 1); 842 o = index2addr(L, idx); 843 api_check(L, ttistable(o), "table expected"); 844 setpvalue(&k, cast(void *, p)); 845 slot = luaH_set(L, hvalue(o), &k); 846 setobj2t(L, slot, L->top - 1); 847 luaC_barrierback(L, hvalue(o), L->top - 1); 848 L->top--; 849 lua_unlock(L); 850 } 851 852 853 LUA_API int lua_setmetatable (lua_State *L, int objindex) { 854 TValue *obj; 855 Table *mt; 856 lua_lock(L); 857 api_checknelems(L, 1); 858 obj = index2addr(L, objindex); 859 if (ttisnil(L->top - 1)) 860 mt = NULL; 861 else { 862 api_check(L, ttistable(L->top - 1), "table expected"); 863 mt = hvalue(L->top - 1); 864 } 865 switch (ttnov(obj)) { 866 case LUA_TTABLE: { 867 hvalue(obj)->metatable = mt; 868 if (mt) { 869 luaC_objbarrier(L, gcvalue(obj), mt); 870 luaC_checkfinalizer(L, gcvalue(obj), mt); 871 } 872 break; 873 } 874 case LUA_TUSERDATA: { 875 uvalue(obj)->metatable = mt; 876 if (mt) { 877 luaC_objbarrier(L, uvalue(obj), mt); 878 luaC_checkfinalizer(L, gcvalue(obj), mt); 879 } 880 break; 881 } 882 default: { 883 G(L)->mt[ttnov(obj)] = mt; 884 break; 885 } 886 } 887 L->top--; 888 lua_unlock(L); 889 return 1; 890 } 891 892 893 LUA_API void lua_setuservalue (lua_State *L, int idx) { 894 StkId o; 895 lua_lock(L); 896 api_checknelems(L, 1); 897 o = index2addr(L, idx); 898 api_check(L, ttisfulluserdata(o), "full userdata expected"); 899 setuservalue(L, uvalue(o), L->top - 1); 900 luaC_barrier(L, gcvalue(o), L->top - 1); 901 L->top--; 902 lua_unlock(L); 903 } 904 905 906 /* 907 ** 'load' and 'call' functions (run Lua code) 908 */ 909 910 911 #define checkresults(L,na,nr) \ 912 api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \ 913 "results from function overflow current stack size") 914 915 916 LUA_API void lua_callk (lua_State *L, int nargs, int nresults, 917 lua_KContext ctx, lua_KFunction k) { 918 StkId func; 919 lua_lock(L); 920 api_check(L, k == NULL || !isLua(L->ci), 921 "cannot use continuations inside hooks"); 922 api_checknelems(L, nargs+1); 923 api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); 924 checkresults(L, nargs, nresults); 925 func = L->top - (nargs+1); 926 if (k != NULL && L->nny == 0) { /* need to prepare continuation? */ 927 L->ci->u.c.k = k; /* save continuation */ 928 L->ci->u.c.ctx = ctx; /* save context */ 929 luaD_call(L, func, nresults); /* do the call */ 930 } 931 else /* no continuation or no yieldable */ 932 luaD_callnoyield(L, func, nresults); /* just do the call */ 933 adjustresults(L, nresults); 934 lua_unlock(L); 935 } 936 937 938 939 /* 940 ** Execute a protected call. 941 */ 942 struct CallS { /* data to 'f_call' */ 943 StkId func; 944 int nresults; 945 }; 946 947 948 static void f_call (lua_State *L, void *ud) { 949 struct CallS *c = cast(struct CallS *, ud); 950 luaD_callnoyield(L, c->func, c->nresults); 951 } 952 953 954 955 LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, 956 lua_KContext ctx, lua_KFunction k) { 957 struct CallS c; 958 int status; 959 ptrdiff_t func; 960 lua_lock(L); 961 api_check(L, k == NULL || !isLua(L->ci), 962 "cannot use continuations inside hooks"); 963 api_checknelems(L, nargs+1); 964 api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); 965 checkresults(L, nargs, nresults); 966 if (errfunc == 0) 967 func = 0; 968 else { 969 StkId o = index2addr(L, errfunc); 970 api_checkstackindex(L, errfunc, o); 971 func = savestack(L, o); 972 } 973 c.func = L->top - (nargs+1); /* function to be called */ 974 if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */ 975 c.nresults = nresults; /* do a 'conventional' protected call */ 976 status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func); 977 } 978 else { /* prepare continuation (call is already protected by 'resume') */ 979 CallInfo *ci = L->ci; 980 ci->u.c.k = k; /* save continuation */ 981 ci->u.c.ctx = ctx; /* save context */ 982 /* save information for error recovery */ 983 ci->extra = savestack(L, c.func); 984 ci->u.c.old_errfunc = L->errfunc; 985 L->errfunc = func; 986 setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */ 987 ci->callstatus |= CIST_YPCALL; /* function can do error recovery */ 988 luaD_call(L, c.func, nresults); /* do the call */ 989 ci->callstatus &= ~CIST_YPCALL; 990 L->errfunc = ci->u.c.old_errfunc; 991 status = LUA_OK; /* if it is here, there were no errors */ 992 } 993 adjustresults(L, nresults); 994 lua_unlock(L); 995 return status; 996 } 997 998 999 LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, 1000 const char *chunkname, const char *mode) { 1001 ZIO z; 1002 int status; 1003 lua_lock(L); 1004 if (!chunkname) chunkname = "?"; 1005 luaZ_init(L, &z, reader, data); 1006 status = luaD_protectedparser(L, &z, chunkname, mode); 1007 if (status == LUA_OK) { /* no errors? */ 1008 LClosure *f = clLvalue(L->top - 1); /* get newly created function */ 1009 if (f->nupvalues >= 1) { /* does it have an upvalue? */ 1010 /* get global table from registry */ 1011 Table *reg = hvalue(&G(L)->l_registry); 1012 const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS); 1013 /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ 1014 setobj(L, f->upvals[0]->v, gt); 1015 luaC_upvalbarrier(L, f->upvals[0]); 1016 } 1017 } 1018 lua_unlock(L); 1019 return status; 1020 } 1021 1022 1023 LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) { 1024 int status; 1025 TValue *o; 1026 lua_lock(L); 1027 api_checknelems(L, 1); 1028 o = L->top - 1; 1029 if (isLfunction(o)) 1030 status = luaU_dump(L, getproto(o), writer, data, strip); 1031 else 1032 status = 1; 1033 lua_unlock(L); 1034 return status; 1035 } 1036 1037 1038 LUA_API int lua_status (lua_State *L) { 1039 return L->status; 1040 } 1041 1042 1043 /* 1044 ** Garbage-collection function 1045 */ 1046 1047 LUA_API int lua_gc (lua_State *L, int what, int data) { 1048 int res = 0; 1049 global_State *g; 1050 lua_lock(L); 1051 g = G(L); 1052 switch (what) { 1053 case LUA_GCSTOP: { 1054 g->gcrunning = 0; 1055 break; 1056 } 1057 case LUA_GCRESTART: { 1058 luaE_setdebt(g, 0); 1059 g->gcrunning = 1; 1060 break; 1061 } 1062 case LUA_GCCOLLECT: { 1063 luaC_fullgc(L, 0); 1064 break; 1065 } 1066 case LUA_GCCOUNT: { 1067 /* GC values are expressed in Kbytes: #bytes/2^10 */ 1068 res = cast_int(gettotalbytes(g) >> 10); 1069 break; 1070 } 1071 case LUA_GCCOUNTB: { 1072 res = cast_int(gettotalbytes(g) & 0x3ff); 1073 break; 1074 } 1075 case LUA_GCSTEP: { 1076 l_mem debt = 1; /* =1 to signal that it did an actual step */ 1077 lu_byte oldrunning = g->gcrunning; 1078 g->gcrunning = 1; /* allow GC to run */ 1079 if (data == 0) { 1080 luaE_setdebt(g, -GCSTEPSIZE); /* to do a "small" step */ 1081 luaC_step(L); 1082 } 1083 else { /* add 'data' to total debt */ 1084 debt = cast(l_mem, data) * 1024 + g->GCdebt; 1085 luaE_setdebt(g, debt); 1086 luaC_checkGC(L); 1087 } 1088 g->gcrunning = oldrunning; /* restore previous state */ 1089 if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */ 1090 res = 1; /* signal it */ 1091 break; 1092 } 1093 case LUA_GCSETPAUSE: { 1094 res = g->gcpause; 1095 g->gcpause = data; 1096 break; 1097 } 1098 case LUA_GCSETSTEPMUL: { 1099 res = g->gcstepmul; 1100 if (data < 40) data = 40; /* avoid ridiculous low values (and 0) */ 1101 g->gcstepmul = data; 1102 break; 1103 } 1104 case LUA_GCISRUNNING: { 1105 res = g->gcrunning; 1106 break; 1107 } 1108 default: res = -1; /* invalid option */ 1109 } 1110 lua_unlock(L); 1111 return res; 1112 } 1113 1114 1115 1116 /* 1117 ** miscellaneous functions 1118 */ 1119 1120 1121 LUA_API int lua_error (lua_State *L) { 1122 lua_lock(L); 1123 api_checknelems(L, 1); 1124 luaG_errormsg(L); 1125 /* code unreachable; will unlock when control actually leaves the kernel */ 1126 return 0; /* to avoid warnings */ 1127 } 1128 1129 1130 LUA_API int lua_next (lua_State *L, int idx) { 1131 StkId t; 1132 int more; 1133 lua_lock(L); 1134 t = index2addr(L, idx); 1135 api_check(L, ttistable(t), "table expected"); 1136 more = luaH_next(L, hvalue(t), L->top - 1); 1137 if (more) { 1138 api_incr_top(L); 1139 } 1140 else /* no more elements */ 1141 L->top -= 1; /* remove key */ 1142 lua_unlock(L); 1143 return more; 1144 } 1145 1146 1147 LUA_API void lua_concat (lua_State *L, int n) { 1148 lua_lock(L); 1149 api_checknelems(L, n); 1150 if (n >= 2) { 1151 luaV_concat(L, n); 1152 } 1153 else if (n == 0) { /* push empty string */ 1154 setsvalue2s(L, L->top, luaS_newlstr(L, "", 0)); 1155 api_incr_top(L); 1156 } 1157 /* else n == 1; nothing to do */ 1158 luaC_checkGC(L); 1159 lua_unlock(L); 1160 } 1161 1162 1163 LUA_API void lua_len (lua_State *L, int idx) { 1164 StkId t; 1165 lua_lock(L); 1166 t = index2addr(L, idx); 1167 luaV_objlen(L, L->top, t); 1168 api_incr_top(L); 1169 lua_unlock(L); 1170 } 1171 1172 1173 LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) { 1174 lua_Alloc f; 1175 lua_lock(L); 1176 if (ud) *ud = G(L)->ud; 1177 f = G(L)->frealloc; 1178 lua_unlock(L); 1179 return f; 1180 } 1181 1182 1183 LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) { 1184 lua_lock(L); 1185 G(L)->ud = ud; 1186 G(L)->frealloc = f; 1187 lua_unlock(L); 1188 } 1189 1190 1191 LUA_API void *lua_newuserdata (lua_State *L, size_t size) { 1192 Udata *u; 1193 lua_lock(L); 1194 u = luaS_newudata(L, size); 1195 setuvalue(L, L->top, u); 1196 api_incr_top(L); 1197 luaC_checkGC(L); 1198 lua_unlock(L); 1199 return getudatamem(u); 1200 } 1201 1202 1203 1204 static const char *aux_upvalue (StkId fi, int n, TValue **val, 1205 CClosure **owner, UpVal **uv) { 1206 switch (ttype(fi)) { 1207 case LUA_TCCL: { /* C closure */ 1208 CClosure *f = clCvalue(fi); 1209 if (!(1 <= n && n <= f->nupvalues)) return NULL; 1210 *val = &f->upvalue[n-1]; 1211 if (owner) *owner = f; 1212 return ""; 1213 } 1214 case LUA_TLCL: { /* Lua closure */ 1215 LClosure *f = clLvalue(fi); 1216 TString *name; 1217 Proto *p = f->p; 1218 if (!(1 <= n && n <= p->sizeupvalues)) return NULL; 1219 *val = f->upvals[n-1]->v; 1220 if (uv) *uv = f->upvals[n - 1]; 1221 name = p->upvalues[n-1].name; 1222 return (name == NULL) ? "(*no name)" : getstr(name); 1223 } 1224 default: return NULL; /* not a closure */ 1225 } 1226 } 1227 1228 1229 LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { 1230 const char *name; 1231 TValue *val = NULL; /* to avoid warnings */ 1232 lua_lock(L); 1233 name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL, NULL); 1234 if (name) { 1235 setobj2s(L, L->top, val); 1236 api_incr_top(L); 1237 } 1238 lua_unlock(L); 1239 return name; 1240 } 1241 1242 1243 LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { 1244 const char *name; 1245 TValue *val = NULL; /* to avoid warnings */ 1246 CClosure *owner = NULL; 1247 UpVal *uv = NULL; 1248 StkId fi; 1249 lua_lock(L); 1250 fi = index2addr(L, funcindex); 1251 api_checknelems(L, 1); 1252 name = aux_upvalue(fi, n, &val, &owner, &uv); 1253 if (name) { 1254 L->top--; 1255 setobj(L, val, L->top); 1256 if (owner) { luaC_barrier(L, owner, L->top); } 1257 else if (uv) { luaC_upvalbarrier(L, uv); } 1258 } 1259 lua_unlock(L); 1260 return name; 1261 } 1262 1263 1264 static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) { 1265 LClosure *f; 1266 StkId fi = index2addr(L, fidx); 1267 api_check(L, ttisLclosure(fi), "Lua function expected"); 1268 f = clLvalue(fi); 1269 api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index"); 1270 if (pf) *pf = f; 1271 return &f->upvals[n - 1]; /* get its upvalue pointer */ 1272 } 1273 1274 1275 LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) { 1276 StkId fi = index2addr(L, fidx); 1277 switch (ttype(fi)) { 1278 case LUA_TLCL: { /* lua closure */ 1279 return *getupvalref(L, fidx, n, NULL); 1280 } 1281 case LUA_TCCL: { /* C closure */ 1282 CClosure *f = clCvalue(fi); 1283 api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index"); 1284 return &f->upvalue[n - 1]; 1285 } 1286 default: { 1287 api_check(L, 0, "closure expected"); 1288 return NULL; 1289 } 1290 } 1291 } 1292 1293 1294 LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1, 1295 int fidx2, int n2) { 1296 LClosure *f1; 1297 UpVal **up1 = getupvalref(L, fidx1, n1, &f1); 1298 UpVal **up2 = getupvalref(L, fidx2, n2, NULL); 1299 luaC_upvdeccount(L, *up1); 1300 *up1 = *up2; 1301 (*up1)->refcount++; 1302 if (upisopen(*up1)) (*up1)->u.open.touched = 1; 1303 luaC_upvalbarrier(L, *up1); 1304 } 1305 1306 1307