1/* 2** $Id: lobject.h,v 2.71.1.2 2014/05/07 14:14:58 roberto Exp $ 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 "llimits.h" 13#include <sys/lua/lua.h> 14 15 16/* 17** Extra tags for non-values 18*/ 19#define LUA_TPROTO LUA_NUMTAGS 20#define LUA_TUPVAL (LUA_NUMTAGS+1) 21#define LUA_TDEADKEY (LUA_NUMTAGS+2) 22 23/* 24** number of all possible tags (including LUA_TNONE but excluding DEADKEY) 25*/ 26#define LUA_TOTALTAGS (LUA_TUPVAL+2) 27 28 29/* 30** tags for Tagged Values have the following use of bits: 31** bits 0-3: actual tag (a LUA_T* value) 32** bits 4-5: variant bits 33** bit 6: whether value is collectable 34*/ 35 36#define VARBITS (3 << 4) 37 38 39/* 40** LUA_TFUNCTION variants: 41** 0 - Lua function 42** 1 - light C function 43** 2 - regular C function (closure) 44*/ 45 46/* Variant tags for functions */ 47#define LUA_TLCL (LUA_TFUNCTION | (0 << 4)) /* Lua closure */ 48#define LUA_TLCF (LUA_TFUNCTION | (1 << 4)) /* light C function */ 49#define LUA_TCCL (LUA_TFUNCTION | (2 << 4)) /* C closure */ 50 51 52/* Variant tags for strings */ 53#define LUA_TSHRSTR (LUA_TSTRING | (0 << 4)) /* short strings */ 54#define LUA_TLNGSTR (LUA_TSTRING | (1 << 4)) /* long strings */ 55 56 57/* Bit mark for collectable types */ 58#define BIT_ISCOLLECTABLE (1 << 6) 59 60/* mark a tag as collectable */ 61#define ctb(t) ((t) | BIT_ISCOLLECTABLE) 62 63 64/* 65** Union of all collectable objects 66*/ 67typedef union GCObject GCObject; 68 69 70/* 71** Common Header for all collectable objects (in macro form, to be 72** included in other objects) 73*/ 74#define CommonHeader GCObject *next; lu_byte tt; lu_byte marked 75 76 77/* 78** Common header in struct form 79*/ 80typedef struct GCheader { 81 CommonHeader; 82} GCheader; 83 84 85 86/* 87** Union of all Lua values 88*/ 89typedef union Value Value; 90 91 92#define numfield lua_Number n; /* numbers */ 93 94 95 96/* 97** Tagged Values. This is the basic representation of values in Lua, 98** an actual value plus a tag with its type. 99*/ 100 101#define TValuefields Value value_; int tt_ 102 103typedef struct lua_TValue TValue; 104 105 106/* macro defining a nil value */ 107#define NILCONSTANT {NULL}, LUA_TNIL 108 109 110#define val_(o) ((o)->value_) 111#define num_(o) (val_(o).n) 112 113 114/* raw type tag of a TValue */ 115#define rttype(o) ((o)->tt_) 116 117/* tag with no variants (bits 0-3) */ 118#define novariant(x) ((x) & 0x0F) 119 120/* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */ 121#define ttype(o) (rttype(o) & 0x3F) 122 123/* type tag of a TValue with no variants (bits 0-3) */ 124#define ttypenv(o) (novariant(rttype(o))) 125 126 127/* Macros to test type */ 128#define checktag(o,t) (rttype(o) == (t)) 129#define checktype(o,t) (ttypenv(o) == (t)) 130#define ttisnumber(o) checktag((o), LUA_TNUMBER) 131#define ttisnil(o) checktag((o), LUA_TNIL) 132#define ttisboolean(o) checktag((o), LUA_TBOOLEAN) 133#define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA) 134#define ttisstring(o) checktype((o), LUA_TSTRING) 135#define ttisshrstring(o) checktag((o), ctb(LUA_TSHRSTR)) 136#define ttislngstring(o) checktag((o), ctb(LUA_TLNGSTR)) 137#define ttistable(o) checktag((o), ctb(LUA_TTABLE)) 138#define ttisfunction(o) checktype(o, LUA_TFUNCTION) 139#define ttisclosure(o) ((rttype(o) & 0x1F) == LUA_TFUNCTION) 140#define ttisCclosure(o) checktag((o), ctb(LUA_TCCL)) 141#define ttisLclosure(o) checktag((o), ctb(LUA_TLCL)) 142#define ttislcf(o) checktag((o), LUA_TLCF) 143#define ttisuserdata(o) checktag((o), ctb(LUA_TUSERDATA)) 144#define ttisthread(o) checktag((o), ctb(LUA_TTHREAD)) 145#define ttisdeadkey(o) checktag((o), LUA_TDEADKEY) 146 147#define ttisequal(o1,o2) (rttype(o1) == rttype(o2)) 148 149/* Macros to access values */ 150#define nvalue(o) check_exp(ttisnumber(o), num_(o)) 151#define gcvalue(o) check_exp(iscollectable(o), val_(o).gc) 152#define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p) 153#define rawtsvalue(o) check_exp(ttisstring(o), &val_(o).gc->ts) 154#define tsvalue(o) (&rawtsvalue(o)->tsv) 155#define rawuvalue(o) check_exp(ttisuserdata(o), &val_(o).gc->u) 156#define uvalue(o) (&rawuvalue(o)->uv) 157#define clvalue(o) check_exp(ttisclosure(o), &val_(o).gc->cl) 158#define clLvalue(o) check_exp(ttisLclosure(o), &val_(o).gc->cl.l) 159#define clCvalue(o) check_exp(ttisCclosure(o), &val_(o).gc->cl.c) 160#define fvalue(o) check_exp(ttislcf(o), val_(o).f) 161#define hvalue(o) check_exp(ttistable(o), &val_(o).gc->h) 162#define bvalue(o) check_exp(ttisboolean(o), val_(o).b) 163#define thvalue(o) check_exp(ttisthread(o), &val_(o).gc->th) 164/* a dead value may get the 'gc' field, but cannot access its contents */ 165#define deadvalue(o) check_exp(ttisdeadkey(o), cast(void *, val_(o).gc)) 166 167#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0)) 168 169 170#define iscollectable(o) (rttype(o) & BIT_ISCOLLECTABLE) 171 172 173/* Macros for internal tests */ 174#define righttt(obj) (ttype(obj) == gcvalue(obj)->gch.tt) 175 176#define checkliveness(g,obj) \ 177 lua_longassert(!iscollectable(obj) || \ 178 (righttt(obj) && !isdead(g,gcvalue(obj)))) 179 180 181/* Macros to set values */ 182#define settt_(o,t) ((o)->tt_=(t)) 183 184#define setnvalue(obj,x) \ 185 { TValue *io=(obj); num_(io)=(x); settt_(io, LUA_TNUMBER); } 186 187#define setnilvalue(obj) settt_(obj, LUA_TNIL) 188 189#define setfvalue(obj,x) \ 190 { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); } 191 192#define setpvalue(obj,x) \ 193 { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); } 194 195#define setbvalue(obj,x) \ 196 { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); } 197 198#define setgcovalue(L,obj,x) \ 199 { TValue *io=(obj); GCObject *i_g=(x); \ 200 val_(io).gc=i_g; settt_(io, ctb(gch(i_g)->tt)); } 201 202#define setsvalue(L,obj,x) \ 203 { TValue *io=(obj); \ 204 TString *x_ = (x); \ 205 val_(io).gc=cast(GCObject *, x_); settt_(io, ctb(x_->tsv.tt)); \ 206 checkliveness(G(L),io); } 207 208#define setuvalue(L,obj,x) \ 209 { TValue *io=(obj); \ 210 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TUSERDATA)); \ 211 checkliveness(G(L),io); } 212 213#define setthvalue(L,obj,x) \ 214 { TValue *io=(obj); \ 215 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTHREAD)); \ 216 checkliveness(G(L),io); } 217 218#define setclLvalue(L,obj,x) \ 219 { TValue *io=(obj); \ 220 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TLCL)); \ 221 checkliveness(G(L),io); } 222 223#define setclCvalue(L,obj,x) \ 224 { TValue *io=(obj); \ 225 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TCCL)); \ 226 checkliveness(G(L),io); } 227 228#define sethvalue(L,obj,x) \ 229 { TValue *io=(obj); \ 230 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTABLE)); \ 231 checkliveness(G(L),io); } 232 233#define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY) 234 235 236 237#define setobj(L,obj1,obj2) \ 238 { const TValue *io2=(obj2); TValue *io1=(obj1); \ 239 io1->value_ = io2->value_; io1->tt_ = io2->tt_; \ 240 checkliveness(G(L),io1); } 241 242 243/* 244** different types of assignments, according to destination 245*/ 246 247/* from stack to (same) stack */ 248#define setobjs2s setobj 249/* to stack (not from same stack) */ 250#define setobj2s setobj 251#define setsvalue2s setsvalue 252#define sethvalue2s sethvalue 253#define setptvalue2s setptvalue 254/* from table to same table */ 255#define setobjt2t setobj 256/* to table */ 257#define setobj2t setobj 258/* to new object */ 259#define setobj2n setobj 260#define setsvalue2n setsvalue 261 262 263/* check whether a number is valid (useful only for NaN trick) */ 264#define luai_checknum(L,o,c) { /* empty */ } 265 266 267/* 268** {====================================================== 269** NaN Trick 270** ======================================================= 271*/ 272#if defined(LUA_NANTRICK) 273 274/* 275** numbers are represented in the 'd_' field. All other values have the 276** value (NNMARK | tag) in 'tt__'. A number with such pattern would be 277** a "signaled NaN", which is never generated by regular operations by 278** the CPU (nor by 'strtod') 279*/ 280 281/* allows for external implementation for part of the trick */ 282#if !defined(NNMARK) /* { */ 283 284 285#if !defined(LUA_IEEEENDIAN) 286#error option 'LUA_NANTRICK' needs 'LUA_IEEEENDIAN' 287#endif 288 289 290#define NNMARK 0x7FF7A500 291#define NNMASK 0x7FFFFF00 292 293#undef TValuefields 294#undef NILCONSTANT 295 296#if (LUA_IEEEENDIAN == 0) /* { */ 297 298/* little endian */ 299#define TValuefields \ 300 union { struct { Value v__; int tt__; } i; double d__; } u 301#define NILCONSTANT {{{NULL}, tag2tt(LUA_TNIL)}} 302/* field-access macros */ 303#define v_(o) ((o)->u.i.v__) 304#define d_(o) ((o)->u.d__) 305#define tt_(o) ((o)->u.i.tt__) 306 307#else /* }{ */ 308 309/* big endian */ 310#define TValuefields \ 311 union { struct { int tt__; Value v__; } i; double d__; } u 312#define NILCONSTANT {{tag2tt(LUA_TNIL), {NULL}}} 313/* field-access macros */ 314#define v_(o) ((o)->u.i.v__) 315#define d_(o) ((o)->u.d__) 316#define tt_(o) ((o)->u.i.tt__) 317 318#endif /* } */ 319 320#endif /* } */ 321 322 323/* correspondence with standard representation */ 324#undef val_ 325#define val_(o) v_(o) 326#undef num_ 327#define num_(o) d_(o) 328 329 330#undef numfield 331#define numfield /* no such field; numbers are the entire struct */ 332 333/* basic check to distinguish numbers from non-numbers */ 334#undef ttisnumber 335#define ttisnumber(o) ((tt_(o) & NNMASK) != NNMARK) 336 337#define tag2tt(t) (NNMARK | (t)) 338 339#undef rttype 340#define rttype(o) (ttisnumber(o) ? LUA_TNUMBER : tt_(o) & 0xff) 341 342#undef settt_ 343#define settt_(o,t) (tt_(o) = tag2tt(t)) 344 345#undef setnvalue 346#define setnvalue(obj,x) \ 347 { TValue *io_=(obj); num_(io_)=(x); lua_assert(ttisnumber(io_)); } 348 349#undef setobj 350#define setobj(L,obj1,obj2) \ 351 { const TValue *o2_=(obj2); TValue *o1_=(obj1); \ 352 o1_->u = o2_->u; \ 353 checkliveness(G(L),o1_); } 354 355 356/* 357** these redefinitions are not mandatory, but these forms are more efficient 358*/ 359 360#undef checktag 361#undef checktype 362#define checktag(o,t) (tt_(o) == tag2tt(t)) 363#define checktype(o,t) (ctb(tt_(o) | VARBITS) == ctb(tag2tt(t) | VARBITS)) 364 365#undef ttisequal 366#define ttisequal(o1,o2) \ 367 (ttisnumber(o1) ? ttisnumber(o2) : (tt_(o1) == tt_(o2))) 368 369 370#undef luai_checknum 371#define luai_checknum(L,o,c) { if (!ttisnumber(o)) c; } 372 373#endif 374/* }====================================================== */ 375 376 377 378/* 379** {====================================================== 380** types and prototypes 381** ======================================================= 382*/ 383 384 385union Value { 386 GCObject *gc; /* collectable objects */ 387 void *p; /* light userdata */ 388 int b; /* booleans */ 389 lua_CFunction f; /* light C functions */ 390 numfield /* numbers */ 391}; 392 393 394struct lua_TValue { 395 TValuefields; 396}; 397 398 399typedef TValue *StkId; /* index to stack elements */ 400 401 402 403 404/* 405** Header for string value; string bytes follow the end of this structure 406*/ 407typedef union TString { 408 L_Umaxalign dummy; /* ensures maximum alignment for strings */ 409 struct { 410 CommonHeader; 411 lu_byte extra; /* reserved words for short strings; "has hash" for longs */ 412 unsigned int hash; 413 size_t len; /* number of characters in string */ 414 } tsv; 415} TString; 416 417 418/* get the actual string (array of bytes) from a TString */ 419#define getstr(ts) cast(const char *, (ts) + 1) 420 421/* get the actual string (array of bytes) from a Lua value */ 422#define svalue(o) getstr(rawtsvalue(o)) 423 424 425/* 426** Header for userdata; memory area follows the end of this structure 427*/ 428typedef union Udata { 429 L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */ 430 struct { 431 CommonHeader; 432 struct Table *metatable; 433 struct Table *env; 434 size_t len; /* number of bytes */ 435 } uv; 436} Udata; 437 438 439 440/* 441** Description of an upvalue for function prototypes 442*/ 443typedef struct Upvaldesc { 444 TString *name; /* upvalue name (for debug information) */ 445 lu_byte instack; /* whether it is in stack */ 446 lu_byte idx; /* index of upvalue (in stack or in outer function's list) */ 447} Upvaldesc; 448 449 450/* 451** Description of a local variable for function prototypes 452** (used for debug information) 453*/ 454typedef struct LocVar { 455 TString *varname; 456 int startpc; /* first point where variable is active */ 457 int endpc; /* first point where variable is dead */ 458} LocVar; 459 460 461/* 462** Function Prototypes 463*/ 464typedef struct Proto { 465 CommonHeader; 466 TValue *k; /* constants used by the function */ 467 Instruction *code; 468 struct Proto **p; /* functions defined inside the function */ 469 int *lineinfo; /* map from opcodes to source lines (debug information) */ 470 LocVar *locvars; /* information about local variables (debug information) */ 471 Upvaldesc *upvalues; /* upvalue information */ 472 union Closure *cache; /* last created closure with this prototype */ 473 TString *source; /* used for debug information */ 474 int sizeupvalues; /* size of 'upvalues' */ 475 int sizek; /* size of `k' */ 476 int sizecode; 477 int sizelineinfo; 478 int sizep; /* size of `p' */ 479 int sizelocvars; 480 int linedefined; 481 int lastlinedefined; 482 GCObject *gclist; 483 lu_byte numparams; /* number of fixed parameters */ 484 lu_byte is_vararg; 485 lu_byte maxstacksize; /* maximum stack used by this function */ 486} Proto; 487 488 489 490/* 491** Lua Upvalues 492*/ 493typedef struct UpVal { 494 CommonHeader; 495 TValue *v; /* points to stack or to its own value */ 496 union { 497 TValue value; /* the value (when closed) */ 498 struct { /* double linked list (when open) */ 499 struct UpVal *prev; 500 struct UpVal *next; 501 } l; 502 } u; 503} UpVal; 504 505 506/* 507** Closures 508*/ 509 510#define ClosureHeader \ 511 CommonHeader; lu_byte nupvalues; GCObject *gclist 512 513typedef struct CClosure { 514 ClosureHeader; 515 lua_CFunction f; 516 TValue upvalue[]; /* list of upvalues */ 517} CClosure; 518 519 520typedef struct LClosure { 521 ClosureHeader; 522 struct Proto *p; 523 UpVal *upvals[]; /* list of upvalues */ 524} LClosure; 525 526 527typedef union Closure { 528 CClosure c; 529 LClosure l; 530} Closure; 531 532 533#define isLfunction(o) ttisLclosure(o) 534 535#define getproto(o) (clLvalue(o)->p) 536 537 538/* 539** Tables 540*/ 541 542typedef union TKey { 543 struct { 544 TValuefields; 545 struct Node *next; /* for chaining */ 546 } nk; 547 TValue tvk; 548} TKey; 549 550 551typedef struct Node { 552 TValue i_val; 553 TKey i_key; 554} Node; 555 556 557typedef struct Table { 558 CommonHeader; 559 lu_byte flags; /* 1<<p means tagmethod(p) is not present */ 560 lu_byte lsizenode; /* log2 of size of `node' array */ 561 int sizearray; /* size of `array' array */ 562 TValue *array; /* array part */ 563 Node *node; 564 Node *lastfree; /* any free position is before this position */ 565 struct Table *metatable; 566 GCObject *gclist; 567} Table; 568 569 570 571/* 572** `module' operation for hashing (size is always a power of 2) 573*/ 574#define lmod(s,size) \ 575 (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1))))) 576 577 578#define twoto(x) (1<<(x)) 579#define sizenode(t) (twoto((t)->lsizenode)) 580 581 582/* 583** (address of) a fixed nil value 584*/ 585#define luaO_nilobject (&luaO_nilobject_) 586 587 588LUAI_DDEC const TValue luaO_nilobject_; 589 590 591LUAI_FUNC int luaO_int2fb (unsigned int x); 592LUAI_FUNC int luaO_fb2int (int x); 593LUAI_FUNC int luaO_ceillog2 (unsigned int x); 594LUAI_FUNC lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2); 595LUAI_FUNC int luaO_str2d (const char *s, size_t len, lua_Number *result); 596LUAI_FUNC int luaO_hexavalue (int c); 597LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, 598 va_list argp); 599LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...); 600LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len); 601 602 603#endif 604