1 /* 2 ** $Id: lobject.h $ 3 ** Type definitions for Lua objects 4 ** See Copyright Notice in lua.h 5 */ 6 7 8 #ifndef lobject_h 9 #define lobject_h 10 11 12 #include <stdarg.h> 13 14 15 #include "llimits.h" 16 #include "lua.h" 17 18 19 /* 20 ** Extra types for collectable non-values 21 */ 22 #define LUA_TUPVAL LUA_NUMTYPES /* upvalues */ 23 #define LUA_TPROTO (LUA_NUMTYPES+1) /* function prototypes */ 24 #define LUA_TDEADKEY (LUA_NUMTYPES+2) /* removed keys in tables */ 25 26 27 28 /* 29 ** number of all possible types (including LUA_TNONE but excluding DEADKEY) 30 */ 31 #define LUA_TOTALTYPES (LUA_TPROTO + 2) 32 33 34 /* 35 ** tags for Tagged Values have the following use of bits: 36 ** bits 0-3: actual tag (a LUA_T* constant) 37 ** bits 4-5: variant bits 38 ** bit 6: whether value is collectable 39 */ 40 41 /* add variant bits to a type */ 42 #define makevariant(t,v) ((t) | ((v) << 4)) 43 44 45 46 /* 47 ** Union of all Lua values 48 */ 49 typedef union Value { 50 struct GCObject *gc; /* collectable objects */ 51 void *p; /* light userdata */ 52 lua_CFunction f; /* light C functions */ 53 lua_Integer i; /* integer numbers */ 54 lua_Number n; /* float numbers */ 55 /* not used, but may avoid warnings for uninitialized value */ 56 lu_byte ub; 57 } Value; 58 59 60 /* 61 ** Tagged Values. This is the basic representation of values in Lua: 62 ** an actual value plus a tag with its type. 63 */ 64 65 #define TValuefields Value value_; lu_byte tt_ 66 67 typedef struct TValue { 68 TValuefields; 69 } TValue; 70 71 72 #define val_(o) ((o)->value_) 73 #define valraw(o) (val_(o)) 74 75 76 /* raw type tag of a TValue */ 77 #define rawtt(o) ((o)->tt_) 78 79 /* tag with no variants (bits 0-3) */ 80 #define novariant(t) ((t) & 0x0F) 81 82 /* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */ 83 #define withvariant(t) ((t) & 0x3F) 84 #define ttypetag(o) withvariant(rawtt(o)) 85 86 /* type of a TValue */ 87 #define ttype(o) (novariant(rawtt(o))) 88 89 90 /* Macros to test type */ 91 #define checktag(o,t) (rawtt(o) == (t)) 92 #define checktype(o,t) (ttype(o) == (t)) 93 94 95 /* Macros for internal tests */ 96 97 /* collectable object has the same tag as the original value */ 98 #define righttt(obj) (ttypetag(obj) == gcvalue(obj)->tt) 99 100 /* 101 ** Any value being manipulated by the program either is non 102 ** collectable, or the collectable object has the right tag 103 ** and it is not dead. The option 'L == NULL' allows other 104 ** macros using this one to be used where L is not available. 105 */ 106 #define checkliveness(L,obj) \ 107 ((void)L, lua_longassert(!iscollectable(obj) || \ 108 (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj)))))) 109 110 111 /* Macros to set values */ 112 113 /* set a value's tag */ 114 #define settt_(o,t) ((o)->tt_=(t)) 115 116 117 /* main macro to copy values (from 'obj2' to 'obj1') */ 118 #define setobj(L,obj1,obj2) \ 119 { TValue *io1=(obj1); const TValue *io2=(obj2); \ 120 io1->value_ = io2->value_; settt_(io1, io2->tt_); \ 121 checkliveness(L,io1); lua_assert(!isnonstrictnil(io1)); } 122 123 /* 124 ** Different types of assignments, according to source and destination. 125 ** (They are mostly equal now, but may be different in the future.) 126 */ 127 128 /* from stack to stack */ 129 #define setobjs2s(L,o1,o2) setobj(L,s2v(o1),s2v(o2)) 130 /* to stack (not from same stack) */ 131 #define setobj2s(L,o1,o2) setobj(L,s2v(o1),o2) 132 /* from table to same table */ 133 #define setobjt2t setobj 134 /* to new object */ 135 #define setobj2n setobj 136 /* to table */ 137 #define setobj2t setobj 138 139 140 /* 141 ** Entries in a Lua stack. Field 'tbclist' forms a list of all 142 ** to-be-closed variables active in this stack. Dummy entries are 143 ** used when the distance between two tbc variables does not fit 144 ** in an unsigned short. They are represented by delta==0, and 145 ** their real delta is always the maximum value that fits in 146 ** that field. 147 */ 148 typedef union StackValue { 149 TValue val; 150 struct { 151 TValuefields; 152 unsigned short delta; 153 } tbclist; 154 } StackValue; 155 156 157 /* index to stack elements */ 158 typedef StackValue *StkId; 159 160 161 /* 162 ** When reallocating the stack, change all pointers to the stack into 163 ** proper offsets. 164 */ 165 typedef union { 166 StkId p; /* actual pointer */ 167 ptrdiff_t offset; /* used while the stack is being reallocated */ 168 } StkIdRel; 169 170 171 /* convert a 'StackValue' to a 'TValue' */ 172 #define s2v(o) (&(o)->val) 173 174 175 176 /* 177 ** {================================================================== 178 ** Nil 179 ** =================================================================== 180 */ 181 182 /* Standard nil */ 183 #define LUA_VNIL makevariant(LUA_TNIL, 0) 184 185 /* Empty slot (which might be different from a slot containing nil) */ 186 #define LUA_VEMPTY makevariant(LUA_TNIL, 1) 187 188 /* Value returned for a key not found in a table (absent key) */ 189 #define LUA_VABSTKEY makevariant(LUA_TNIL, 2) 190 191 192 /* macro to test for (any kind of) nil */ 193 #define ttisnil(v) checktype((v), LUA_TNIL) 194 195 196 /* macro to test for a standard nil */ 197 #define ttisstrictnil(o) checktag((o), LUA_VNIL) 198 199 200 #define setnilvalue(obj) settt_(obj, LUA_VNIL) 201 202 203 #define isabstkey(v) checktag((v), LUA_VABSTKEY) 204 205 206 /* 207 ** macro to detect non-standard nils (used only in assertions) 208 */ 209 #define isnonstrictnil(v) (ttisnil(v) && !ttisstrictnil(v)) 210 211 212 /* 213 ** By default, entries with any kind of nil are considered empty. 214 ** (In any definition, values associated with absent keys must also 215 ** be accepted as empty.) 216 */ 217 #define isempty(v) ttisnil(v) 218 219 220 /* macro defining a value corresponding to an absent key */ 221 #define ABSTKEYCONSTANT {NULL}, LUA_VABSTKEY 222 223 224 /* mark an entry as empty */ 225 #define setempty(v) settt_(v, LUA_VEMPTY) 226 227 228 229 /* }================================================================== */ 230 231 232 /* 233 ** {================================================================== 234 ** Booleans 235 ** =================================================================== 236 */ 237 238 239 #define LUA_VFALSE makevariant(LUA_TBOOLEAN, 0) 240 #define LUA_VTRUE makevariant(LUA_TBOOLEAN, 1) 241 242 #define ttisboolean(o) checktype((o), LUA_TBOOLEAN) 243 #define ttisfalse(o) checktag((o), LUA_VFALSE) 244 #define ttistrue(o) checktag((o), LUA_VTRUE) 245 246 247 #define l_isfalse(o) (ttisfalse(o) || ttisnil(o)) 248 249 250 #define setbfvalue(obj) settt_(obj, LUA_VFALSE) 251 #define setbtvalue(obj) settt_(obj, LUA_VTRUE) 252 253 /* }================================================================== */ 254 255 256 /* 257 ** {================================================================== 258 ** Threads 259 ** =================================================================== 260 */ 261 262 #define LUA_VTHREAD makevariant(LUA_TTHREAD, 0) 263 264 #define ttisthread(o) checktag((o), ctb(LUA_VTHREAD)) 265 266 #define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc)) 267 268 #define setthvalue(L,obj,x) \ 269 { TValue *io = (obj); lua_State *x_ = (x); \ 270 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTHREAD)); \ 271 checkliveness(L,io); } 272 273 #define setthvalue2s(L,o,t) setthvalue(L,s2v(o),t) 274 275 /* }================================================================== */ 276 277 278 /* 279 ** {================================================================== 280 ** Collectable Objects 281 ** =================================================================== 282 */ 283 284 /* 285 ** Common Header for all collectable objects (in macro form, to be 286 ** included in other objects) 287 */ 288 #define CommonHeader struct GCObject *next; lu_byte tt; lu_byte marked 289 290 291 /* Common type for all collectable objects */ 292 typedef struct GCObject { 293 CommonHeader; 294 } GCObject; 295 296 297 /* Bit mark for collectable types */ 298 #define BIT_ISCOLLECTABLE (1 << 6) 299 300 #define iscollectable(o) (rawtt(o) & BIT_ISCOLLECTABLE) 301 302 /* mark a tag as collectable */ 303 #define ctb(t) ((t) | BIT_ISCOLLECTABLE) 304 305 #define gcvalue(o) check_exp(iscollectable(o), val_(o).gc) 306 307 #define gcvalueraw(v) ((v).gc) 308 309 #define setgcovalue(L,obj,x) \ 310 { TValue *io = (obj); GCObject *i_g=(x); \ 311 val_(io).gc = i_g; settt_(io, ctb(i_g->tt)); } 312 313 /* }================================================================== */ 314 315 316 /* 317 ** {================================================================== 318 ** Numbers 319 ** =================================================================== 320 */ 321 322 /* Variant tags for numbers */ 323 #define LUA_VNUMINT makevariant(LUA_TNUMBER, 0) /* integer numbers */ 324 #define LUA_VNUMFLT makevariant(LUA_TNUMBER, 1) /* float numbers */ 325 326 #define ttisnumber(o) checktype((o), LUA_TNUMBER) 327 #define ttisfloat(o) checktag((o), LUA_VNUMFLT) 328 #define ttisinteger(o) checktag((o), LUA_VNUMINT) 329 330 #define nvalue(o) check_exp(ttisnumber(o), \ 331 (ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o))) 332 #define fltvalue(o) check_exp(ttisfloat(o), val_(o).n) 333 #define ivalue(o) check_exp(ttisinteger(o), val_(o).i) 334 335 #define fltvalueraw(v) ((v).n) 336 #define ivalueraw(v) ((v).i) 337 338 #define setfltvalue(obj,x) \ 339 { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_VNUMFLT); } 340 341 #define chgfltvalue(obj,x) \ 342 { TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); } 343 344 #define setivalue(obj,x) \ 345 { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_VNUMINT); } 346 347 #define chgivalue(obj,x) \ 348 { TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); } 349 350 /* }================================================================== */ 351 352 353 /* 354 ** {================================================================== 355 ** Strings 356 ** =================================================================== 357 */ 358 359 /* Variant tags for strings */ 360 #define LUA_VSHRSTR makevariant(LUA_TSTRING, 0) /* short strings */ 361 #define LUA_VLNGSTR makevariant(LUA_TSTRING, 1) /* long strings */ 362 363 #define ttisstring(o) checktype((o), LUA_TSTRING) 364 #define ttisshrstring(o) checktag((o), ctb(LUA_VSHRSTR)) 365 #define ttislngstring(o) checktag((o), ctb(LUA_VLNGSTR)) 366 367 #define tsvalueraw(v) (gco2ts((v).gc)) 368 369 #define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc)) 370 371 #define setsvalue(L,obj,x) \ 372 { TValue *io = (obj); TString *x_ = (x); \ 373 val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \ 374 checkliveness(L,io); } 375 376 /* set a string to the stack */ 377 #define setsvalue2s(L,o,s) setsvalue(L,s2v(o),s) 378 379 /* set a string to a new object */ 380 #define setsvalue2n setsvalue 381 382 383 /* 384 ** Header for a string value. 385 */ 386 typedef struct TString { 387 CommonHeader; 388 lu_byte extra; /* reserved words for short strings; "has hash" for longs */ 389 lu_byte shrlen; /* length for short strings, 0xFF for long strings */ 390 unsigned int hash; 391 union { 392 size_t lnglen; /* length for long strings */ 393 struct TString *hnext; /* linked list for hash table */ 394 } u; 395 char contents[1]; 396 } TString; 397 398 399 400 /* 401 ** Get the actual string (array of bytes) from a 'TString'. (Generic 402 ** version and specialized versions for long and short strings.) 403 */ 404 #define getstr(ts) ((ts)->contents) 405 #define getlngstr(ts) check_exp((ts)->shrlen == 0xFF, (ts)->contents) 406 #define getshrstr(ts) check_exp((ts)->shrlen != 0xFF, (ts)->contents) 407 408 409 /* get string length from 'TString *s' */ 410 #define tsslen(s) \ 411 ((s)->shrlen != 0xFF ? (s)->shrlen : (s)->u.lnglen) 412 413 /* }================================================================== */ 414 415 416 /* 417 ** {================================================================== 418 ** Userdata 419 ** =================================================================== 420 */ 421 422 423 /* 424 ** Light userdata should be a variant of userdata, but for compatibility 425 ** reasons they are also different types. 426 */ 427 #define LUA_VLIGHTUSERDATA makevariant(LUA_TLIGHTUSERDATA, 0) 428 429 #define LUA_VUSERDATA makevariant(LUA_TUSERDATA, 0) 430 431 #define ttislightuserdata(o) checktag((o), LUA_VLIGHTUSERDATA) 432 #define ttisfulluserdata(o) checktag((o), ctb(LUA_VUSERDATA)) 433 434 #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p) 435 #define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc)) 436 437 #define pvalueraw(v) ((v).p) 438 439 #define setpvalue(obj,x) \ 440 { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_VLIGHTUSERDATA); } 441 442 #define setuvalue(L,obj,x) \ 443 { TValue *io = (obj); Udata *x_ = (x); \ 444 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VUSERDATA)); \ 445 checkliveness(L,io); } 446 447 448 /* Ensures that addresses after this type are always fully aligned. */ 449 typedef union UValue { 450 TValue uv; 451 LUAI_MAXALIGN; /* ensures maximum alignment for udata bytes */ 452 } UValue; 453 454 455 /* 456 ** Header for userdata with user values; 457 ** memory area follows the end of this structure. 458 */ 459 typedef struct Udata { 460 CommonHeader; 461 unsigned short nuvalue; /* number of user values */ 462 size_t len; /* number of bytes */ 463 struct Table *metatable; 464 GCObject *gclist; 465 UValue uv[1]; /* user values */ 466 } Udata; 467 468 469 /* 470 ** Header for userdata with no user values. These userdata do not need 471 ** to be gray during GC, and therefore do not need a 'gclist' field. 472 ** To simplify, the code always use 'Udata' for both kinds of userdata, 473 ** making sure it never accesses 'gclist' on userdata with no user values. 474 ** This structure here is used only to compute the correct size for 475 ** this representation. (The 'bindata' field in its end ensures correct 476 ** alignment for binary data following this header.) 477 */ 478 typedef struct Udata0 { 479 CommonHeader; 480 unsigned short nuvalue; /* number of user values */ 481 size_t len; /* number of bytes */ 482 struct Table *metatable; 483 union {LUAI_MAXALIGN;} bindata; 484 } Udata0; 485 486 487 /* compute the offset of the memory area of a userdata */ 488 #define udatamemoffset(nuv) \ 489 ((nuv) == 0 ? offsetof(Udata0, bindata) \ 490 : offsetof(Udata, uv) + (sizeof(UValue) * (nuv))) 491 492 /* get the address of the memory block inside 'Udata' */ 493 #define getudatamem(u) (cast_charp(u) + udatamemoffset((u)->nuvalue)) 494 495 /* compute the size of a userdata */ 496 #define sizeudata(nuv,nb) (udatamemoffset(nuv) + (nb)) 497 498 /* }================================================================== */ 499 500 501 /* 502 ** {================================================================== 503 ** Prototypes 504 ** =================================================================== 505 */ 506 507 #define LUA_VPROTO makevariant(LUA_TPROTO, 0) 508 509 510 /* 511 ** Description of an upvalue for function prototypes 512 */ 513 typedef struct Upvaldesc { 514 TString *name; /* upvalue name (for debug information) */ 515 lu_byte instack; /* whether it is in stack (register) */ 516 lu_byte idx; /* index of upvalue (in stack or in outer function's list) */ 517 lu_byte kind; /* kind of corresponding variable */ 518 } Upvaldesc; 519 520 521 /* 522 ** Description of a local variable for function prototypes 523 ** (used for debug information) 524 */ 525 typedef struct LocVar { 526 TString *varname; 527 int startpc; /* first point where variable is active */ 528 int endpc; /* first point where variable is dead */ 529 } LocVar; 530 531 532 /* 533 ** Associates the absolute line source for a given instruction ('pc'). 534 ** The array 'lineinfo' gives, for each instruction, the difference in 535 ** lines from the previous instruction. When that difference does not 536 ** fit into a byte, Lua saves the absolute line for that instruction. 537 ** (Lua also saves the absolute line periodically, to speed up the 538 ** computation of a line number: we can use binary search in the 539 ** absolute-line array, but we must traverse the 'lineinfo' array 540 ** linearly to compute a line.) 541 */ 542 typedef struct AbsLineInfo { 543 int pc; 544 int line; 545 } AbsLineInfo; 546 547 /* 548 ** Function Prototypes 549 */ 550 typedef struct Proto { 551 CommonHeader; 552 lu_byte numparams; /* number of fixed (named) parameters */ 553 lu_byte is_vararg; 554 lu_byte maxstacksize; /* number of registers needed by this function */ 555 int sizeupvalues; /* size of 'upvalues' */ 556 int sizek; /* size of 'k' */ 557 int sizecode; 558 int sizelineinfo; 559 int sizep; /* size of 'p' */ 560 int sizelocvars; 561 int sizeabslineinfo; /* size of 'abslineinfo' */ 562 int linedefined; /* debug information */ 563 int lastlinedefined; /* debug information */ 564 TValue *k; /* constants used by the function */ 565 Instruction *code; /* opcodes */ 566 struct Proto **p; /* functions defined inside the function */ 567 Upvaldesc *upvalues; /* upvalue information */ 568 ls_byte *lineinfo; /* information about source lines (debug information) */ 569 AbsLineInfo *abslineinfo; /* idem */ 570 LocVar *locvars; /* information about local variables (debug information) */ 571 TString *source; /* used for debug information */ 572 GCObject *gclist; 573 } Proto; 574 575 /* }================================================================== */ 576 577 578 /* 579 ** {================================================================== 580 ** Functions 581 ** =================================================================== 582 */ 583 584 #define LUA_VUPVAL makevariant(LUA_TUPVAL, 0) 585 586 587 /* Variant tags for functions */ 588 #define LUA_VLCL makevariant(LUA_TFUNCTION, 0) /* Lua closure */ 589 #define LUA_VLCF makevariant(LUA_TFUNCTION, 1) /* light C function */ 590 #define LUA_VCCL makevariant(LUA_TFUNCTION, 2) /* C closure */ 591 592 #define ttisfunction(o) checktype(o, LUA_TFUNCTION) 593 #define ttisLclosure(o) checktag((o), ctb(LUA_VLCL)) 594 #define ttislcf(o) checktag((o), LUA_VLCF) 595 #define ttisCclosure(o) checktag((o), ctb(LUA_VCCL)) 596 #define ttisclosure(o) (ttisLclosure(o) || ttisCclosure(o)) 597 598 599 #define isLfunction(o) ttisLclosure(o) 600 601 #define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc)) 602 #define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc)) 603 #define fvalue(o) check_exp(ttislcf(o), val_(o).f) 604 #define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc)) 605 606 #define fvalueraw(v) ((v).f) 607 608 #define setclLvalue(L,obj,x) \ 609 { TValue *io = (obj); LClosure *x_ = (x); \ 610 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VLCL)); \ 611 checkliveness(L,io); } 612 613 #define setclLvalue2s(L,o,cl) setclLvalue(L,s2v(o),cl) 614 615 #define setfvalue(obj,x) \ 616 { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_VLCF); } 617 618 #define setclCvalue(L,obj,x) \ 619 { TValue *io = (obj); CClosure *x_ = (x); \ 620 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VCCL)); \ 621 checkliveness(L,io); } 622 623 624 /* 625 ** Upvalues for Lua closures 626 */ 627 typedef struct UpVal { 628 CommonHeader; 629 union { 630 TValue *p; /* points to stack or to its own value */ 631 ptrdiff_t offset; /* used while the stack is being reallocated */ 632 } v; 633 union { 634 struct { /* (when open) */ 635 struct UpVal *next; /* linked list */ 636 struct UpVal **previous; 637 } open; 638 TValue value; /* the value (when closed) */ 639 } u; 640 } UpVal; 641 642 643 644 #define ClosureHeader \ 645 CommonHeader; lu_byte nupvalues; GCObject *gclist 646 647 typedef struct CClosure { 648 ClosureHeader; 649 lua_CFunction f; 650 TValue upvalue[1]; /* list of upvalues */ 651 } CClosure; 652 653 654 typedef struct LClosure { 655 ClosureHeader; 656 struct Proto *p; 657 UpVal *upvals[1]; /* list of upvalues */ 658 } LClosure; 659 660 661 typedef union Closure { 662 CClosure c; 663 LClosure l; 664 } Closure; 665 666 667 #define getproto(o) (clLvalue(o)->p) 668 669 /* }================================================================== */ 670 671 672 /* 673 ** {================================================================== 674 ** Tables 675 ** =================================================================== 676 */ 677 678 #define LUA_VTABLE makevariant(LUA_TTABLE, 0) 679 680 #define ttistable(o) checktag((o), ctb(LUA_VTABLE)) 681 682 #define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc)) 683 684 #define sethvalue(L,obj,x) \ 685 { TValue *io = (obj); Table *x_ = (x); \ 686 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTABLE)); \ 687 checkliveness(L,io); } 688 689 #define sethvalue2s(L,o,h) sethvalue(L,s2v(o),h) 690 691 692 /* 693 ** Nodes for Hash tables: A pack of two TValue's (key-value pairs) 694 ** plus a 'next' field to link colliding entries. The distribution 695 ** of the key's fields ('key_tt' and 'key_val') not forming a proper 696 ** 'TValue' allows for a smaller size for 'Node' both in 4-byte 697 ** and 8-byte alignments. 698 */ 699 typedef union Node { 700 struct NodeKey { 701 TValuefields; /* fields for value */ 702 lu_byte key_tt; /* key type */ 703 int next; /* for chaining */ 704 Value key_val; /* key value */ 705 } u; 706 TValue i_val; /* direct access to node's value as a proper 'TValue' */ 707 } Node; 708 709 710 /* copy a value into a key */ 711 #define setnodekey(L,node,obj) \ 712 { Node *n_=(node); const TValue *io_=(obj); \ 713 n_->u.key_val = io_->value_; n_->u.key_tt = io_->tt_; \ 714 checkliveness(L,io_); } 715 716 717 /* copy a value from a key */ 718 #define getnodekey(L,obj,node) \ 719 { TValue *io_=(obj); const Node *n_=(node); \ 720 io_->value_ = n_->u.key_val; io_->tt_ = n_->u.key_tt; \ 721 checkliveness(L,io_); } 722 723 724 /* 725 ** About 'alimit': if 'isrealasize(t)' is true, then 'alimit' is the 726 ** real size of 'array'. Otherwise, the real size of 'array' is the 727 ** smallest power of two not smaller than 'alimit' (or zero iff 'alimit' 728 ** is zero); 'alimit' is then used as a hint for #t. 729 */ 730 731 #define BITRAS (1 << 7) 732 #define isrealasize(t) (!((t)->flags & BITRAS)) 733 #define setrealasize(t) ((t)->flags &= cast_byte(~BITRAS)) 734 #define setnorealasize(t) ((t)->flags |= BITRAS) 735 736 737 typedef struct Table { 738 CommonHeader; 739 lu_byte flags; /* 1<<p means tagmethod(p) is not present */ 740 lu_byte lsizenode; /* log2 of size of 'node' array */ 741 unsigned int alimit; /* "limit" of 'array' array */ 742 TValue *array; /* array part */ 743 Node *node; 744 Node *lastfree; /* any free position is before this position */ 745 struct Table *metatable; 746 GCObject *gclist; 747 } Table; 748 749 750 /* 751 ** Macros to manipulate keys inserted in nodes 752 */ 753 #define keytt(node) ((node)->u.key_tt) 754 #define keyval(node) ((node)->u.key_val) 755 756 #define keyisnil(node) (keytt(node) == LUA_TNIL) 757 #define keyisinteger(node) (keytt(node) == LUA_VNUMINT) 758 #define keyival(node) (keyval(node).i) 759 #define keyisshrstr(node) (keytt(node) == ctb(LUA_VSHRSTR)) 760 #define keystrval(node) (gco2ts(keyval(node).gc)) 761 762 #define setnilkey(node) (keytt(node) = LUA_TNIL) 763 764 #define keyiscollectable(n) (keytt(n) & BIT_ISCOLLECTABLE) 765 766 #define gckey(n) (keyval(n).gc) 767 #define gckeyN(n) (keyiscollectable(n) ? gckey(n) : NULL) 768 769 770 /* 771 ** Dead keys in tables have the tag DEADKEY but keep their original 772 ** gcvalue. This distinguishes them from regular keys but allows them to 773 ** be found when searched in a special way. ('next' needs that to find 774 ** keys removed from a table during a traversal.) 775 */ 776 #define setdeadkey(node) (keytt(node) = LUA_TDEADKEY) 777 #define keyisdead(node) (keytt(node) == LUA_TDEADKEY) 778 779 /* }================================================================== */ 780 781 782 783 /* 784 ** 'module' operation for hashing (size is always a power of 2) 785 */ 786 #define lmod(s,size) \ 787 (check_exp((size&(size-1))==0, (cast_int((s) & ((size)-1))))) 788 789 790 #define twoto(x) (1<<(x)) 791 #define sizenode(t) (twoto((t)->lsizenode)) 792 793 794 /* size of buffer for 'luaO_utf8esc' function */ 795 #define UTF8BUFFSZ 8 796 797 LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x); 798 LUAI_FUNC int luaO_ceillog2 (unsigned int x); 799 LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1, 800 const TValue *p2, TValue *res); 801 LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1, 802 const TValue *p2, StkId res); 803 LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o); 804 LUAI_FUNC int luaO_hexavalue (int c); 805 LUAI_FUNC void luaO_tostring (lua_State *L, TValue *obj); 806 LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, 807 va_list argp); 808 LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...); 809 LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t srclen); 810 811 812 #endif 813 814