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