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