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