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