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