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