Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-07 10:37:56

0001 /*
0002 ** $Id: lobject.h $
0003 ** Type definitions for Lua objects
0004 ** See Copyright Notice in lua.h
0005 */
0006 
0007 
0008 #ifndef lobject_h
0009 #define lobject_h
0010 
0011 
0012 #include <stdarg.h>
0013 
0014 
0015 #include "llimits.h"
0016 #include "lua.h"
0017 
0018 
0019 /*
0020 ** Extra types for collectable non-values
0021 */
0022 #define LUA_TUPVAL  LUA_NUMTYPES  /* upvalues */
0023 #define LUA_TPROTO  (LUA_NUMTYPES+1)  /* function prototypes */
0024 #define LUA_TDEADKEY    (LUA_NUMTYPES+2)  /* removed keys in tables */
0025 
0026 
0027 
0028 /*
0029 ** number of all possible types (including LUA_TNONE but excluding DEADKEY)
0030 */
0031 #define LUA_TOTALTYPES      (LUA_TPROTO + 2)
0032 
0033 
0034 /*
0035 ** tags for Tagged Values have the following use of bits:
0036 ** bits 0-3: actual tag (a LUA_T* constant)
0037 ** bits 4-5: variant bits
0038 ** bit 6: whether value is collectable
0039 */
0040 
0041 /* add variant bits to a type */
0042 #define makevariant(t,v)    ((t) | ((v) << 4))
0043 
0044 
0045 
0046 /*
0047 ** Union of all Lua values
0048 */
0049 typedef union Value {
0050   struct GCObject *gc;    /* collectable objects */
0051   void *p;         /* light userdata */
0052   lua_CFunction f; /* light C functions */
0053   lua_Integer i;   /* integer numbers */
0054   lua_Number n;    /* float numbers */
0055   /* not used, but may avoid warnings for uninitialized value */
0056   lu_byte ub;
0057 } Value;
0058 
0059 
0060 /*
0061 ** Tagged Values. This is the basic representation of values in Lua:
0062 ** an actual value plus a tag with its type.
0063 */
0064 
0065 #define TValuefields    Value value_; lu_byte tt_
0066 
0067 typedef struct TValue {
0068   TValuefields;
0069 } TValue;
0070 
0071 
0072 #define val_(o)     ((o)->value_)
0073 #define valraw(o)   (val_(o))
0074 
0075 
0076 /* raw type tag of a TValue */
0077 #define rawtt(o)    ((o)->tt_)
0078 
0079 /* tag with no variants (bits 0-3) */
0080 #define novariant(t)    ((t) & 0x0F)
0081 
0082 /* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */
0083 #define withvariant(t)  ((t) & 0x3F)
0084 #define ttypetag(o) withvariant(rawtt(o))
0085 
0086 /* type of a TValue */
0087 #define ttype(o)    (novariant(rawtt(o)))
0088 
0089 
0090 /* Macros to test type */
0091 #define checktag(o,t)       (rawtt(o) == (t))
0092 #define checktype(o,t)      (ttype(o) == (t))
0093 
0094 
0095 /* Macros for internal tests */
0096 
0097 /* collectable object has the same tag as the original value */
0098 #define righttt(obj)        (ttypetag(obj) == gcvalue(obj)->tt)
0099 
0100 /*
0101 ** Any value being manipulated by the program either is non
0102 ** collectable, or the collectable object has the right tag
0103 ** and it is not dead. The option 'L == NULL' allows other
0104 ** macros using this one to be used where L is not available.
0105 */
0106 #define checkliveness(L,obj) \
0107     ((void)L, lua_longassert(!iscollectable(obj) || \
0108         (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj))))))
0109 
0110 
0111 /* Macros to set values */
0112 
0113 /* set a value's tag */
0114 #define settt_(o,t) ((o)->tt_=(t))
0115 
0116 
0117 /* main macro to copy values (from 'obj2' to 'obj1') */
0118 #define setobj(L,obj1,obj2) \
0119     { TValue *io1=(obj1); const TValue *io2=(obj2); \
0120           io1->value_ = io2->value_; settt_(io1, io2->tt_); \
0121       checkliveness(L,io1); lua_assert(!isnonstrictnil(io1)); }
0122 
0123 /*
0124 ** Different types of assignments, according to source and destination.
0125 ** (They are mostly equal now, but may be different in the future.)
0126 */
0127 
0128 /* from stack to stack */
0129 #define setobjs2s(L,o1,o2)  setobj(L,s2v(o1),s2v(o2))
0130 /* to stack (not from same stack) */
0131 #define setobj2s(L,o1,o2)   setobj(L,s2v(o1),o2)
0132 /* from table to same table */
0133 #define setobjt2t   setobj
0134 /* to new object */
0135 #define setobj2n    setobj
0136 /* to table */
0137 #define setobj2t    setobj
0138 
0139 
0140 /*
0141 ** Entries in a Lua stack. Field 'tbclist' forms a list of all
0142 ** to-be-closed variables active in this stack. Dummy entries are
0143 ** used when the distance between two tbc variables does not fit
0144 ** in an unsigned short. They are represented by delta==0, and
0145 ** their real delta is always the maximum value that fits in
0146 ** that field.
0147 */
0148 typedef union StackValue {
0149   TValue val;
0150   struct {
0151     TValuefields;
0152     unsigned short delta;
0153   } tbclist;
0154 } StackValue;
0155 
0156 
0157 /* index to stack elements */
0158 typedef StackValue *StkId;
0159 
0160 
0161 /*
0162 ** When reallocating the stack, change all pointers to the stack into
0163 ** proper offsets.
0164 */
0165 typedef union {
0166   StkId p;  /* actual pointer */
0167   ptrdiff_t offset;  /* used while the stack is being reallocated */
0168 } StkIdRel;
0169 
0170 
0171 /* convert a 'StackValue' to a 'TValue' */
0172 #define s2v(o)  (&(o)->val)
0173 
0174 
0175 
0176 /*
0177 ** {==================================================================
0178 ** Nil
0179 ** ===================================================================
0180 */
0181 
0182 /* Standard nil */
0183 #define LUA_VNIL    makevariant(LUA_TNIL, 0)
0184 
0185 /* Empty slot (which might be different from a slot containing nil) */
0186 #define LUA_VEMPTY  makevariant(LUA_TNIL, 1)
0187 
0188 /* Value returned for a key not found in a table (absent key) */
0189 #define LUA_VABSTKEY    makevariant(LUA_TNIL, 2)
0190 
0191 
0192 /* macro to test for (any kind of) nil */
0193 #define ttisnil(v)      checktype((v), LUA_TNIL)
0194 
0195 
0196 /* macro to test for a standard nil */
0197 #define ttisstrictnil(o)    checktag((o), LUA_VNIL)
0198 
0199 
0200 #define setnilvalue(obj) settt_(obj, LUA_VNIL)
0201 
0202 
0203 #define isabstkey(v)        checktag((v), LUA_VABSTKEY)
0204 
0205 
0206 /*
0207 ** macro to detect non-standard nils (used only in assertions)
0208 */
0209 #define isnonstrictnil(v)   (ttisnil(v) && !ttisstrictnil(v))
0210 
0211 
0212 /*
0213 ** By default, entries with any kind of nil are considered empty.
0214 ** (In any definition, values associated with absent keys must also
0215 ** be accepted as empty.)
0216 */
0217 #define isempty(v)      ttisnil(v)
0218 
0219 
0220 /* macro defining a value corresponding to an absent key */
0221 #define ABSTKEYCONSTANT     {NULL}, LUA_VABSTKEY
0222 
0223 
0224 /* mark an entry as empty */
0225 #define setempty(v)     settt_(v, LUA_VEMPTY)
0226 
0227 
0228 
0229 /* }================================================================== */
0230 
0231 
0232 /*
0233 ** {==================================================================
0234 ** Booleans
0235 ** ===================================================================
0236 */
0237 
0238 
0239 #define LUA_VFALSE  makevariant(LUA_TBOOLEAN, 0)
0240 #define LUA_VTRUE   makevariant(LUA_TBOOLEAN, 1)
0241 
0242 #define ttisboolean(o)      checktype((o), LUA_TBOOLEAN)
0243 #define ttisfalse(o)        checktag((o), LUA_VFALSE)
0244 #define ttistrue(o)     checktag((o), LUA_VTRUE)
0245 
0246 
0247 #define l_isfalse(o)    (ttisfalse(o) || ttisnil(o))
0248 
0249 
0250 #define setbfvalue(obj)     settt_(obj, LUA_VFALSE)
0251 #define setbtvalue(obj)     settt_(obj, LUA_VTRUE)
0252 
0253 /* }================================================================== */
0254 
0255 
0256 /*
0257 ** {==================================================================
0258 ** Threads
0259 ** ===================================================================
0260 */
0261 
0262 #define LUA_VTHREAD     makevariant(LUA_TTHREAD, 0)
0263 
0264 #define ttisthread(o)       checktag((o), ctb(LUA_VTHREAD))
0265 
0266 #define thvalue(o)  check_exp(ttisthread(o), gco2th(val_(o).gc))
0267 
0268 #define setthvalue(L,obj,x) \
0269   { TValue *io = (obj); lua_State *x_ = (x); \
0270     val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTHREAD)); \
0271     checkliveness(L,io); }
0272 
0273 #define setthvalue2s(L,o,t) setthvalue(L,s2v(o),t)
0274 
0275 /* }================================================================== */
0276 
0277 
0278 /*
0279 ** {==================================================================
0280 ** Collectable Objects
0281 ** ===================================================================
0282 */
0283 
0284 /*
0285 ** Common Header for all collectable objects (in macro form, to be
0286 ** included in other objects)
0287 */
0288 #define CommonHeader    struct GCObject *next; lu_byte tt; lu_byte marked
0289 
0290 
0291 /* Common type for all collectable objects */
0292 typedef struct GCObject {
0293   CommonHeader;
0294 } GCObject;
0295 
0296 
0297 /* Bit mark for collectable types */
0298 #define BIT_ISCOLLECTABLE   (1 << 6)
0299 
0300 #define iscollectable(o)    (rawtt(o) & BIT_ISCOLLECTABLE)
0301 
0302 /* mark a tag as collectable */
0303 #define ctb(t)          ((t) | BIT_ISCOLLECTABLE)
0304 
0305 #define gcvalue(o)  check_exp(iscollectable(o), val_(o).gc)
0306 
0307 #define gcvalueraw(v)   ((v).gc)
0308 
0309 #define setgcovalue(L,obj,x) \
0310   { TValue *io = (obj); GCObject *i_g=(x); \
0311     val_(io).gc = i_g; settt_(io, ctb(i_g->tt)); }
0312 
0313 /* }================================================================== */
0314 
0315 
0316 /*
0317 ** {==================================================================
0318 ** Numbers
0319 ** ===================================================================
0320 */
0321 
0322 /* Variant tags for numbers */
0323 #define LUA_VNUMINT makevariant(LUA_TNUMBER, 0)  /* integer numbers */
0324 #define LUA_VNUMFLT makevariant(LUA_TNUMBER, 1)  /* float numbers */
0325 
0326 #define ttisnumber(o)       checktype((o), LUA_TNUMBER)
0327 #define ttisfloat(o)        checktag((o), LUA_VNUMFLT)
0328 #define ttisinteger(o)      checktag((o), LUA_VNUMINT)
0329 
0330 #define nvalue(o)   check_exp(ttisnumber(o), \
0331     (ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o)))
0332 #define fltvalue(o) check_exp(ttisfloat(o), val_(o).n)
0333 #define ivalue(o)   check_exp(ttisinteger(o), val_(o).i)
0334 
0335 #define fltvalueraw(v)  ((v).n)
0336 #define ivalueraw(v)    ((v).i)
0337 
0338 #define setfltvalue(obj,x) \
0339   { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_VNUMFLT); }
0340 
0341 #define chgfltvalue(obj,x) \
0342   { TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); }
0343 
0344 #define setivalue(obj,x) \
0345   { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_VNUMINT); }
0346 
0347 #define chgivalue(obj,x) \
0348   { TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); }
0349 
0350 /* }================================================================== */
0351 
0352 
0353 /*
0354 ** {==================================================================
0355 ** Strings
0356 ** ===================================================================
0357 */
0358 
0359 /* Variant tags for strings */
0360 #define LUA_VSHRSTR makevariant(LUA_TSTRING, 0)  /* short strings */
0361 #define LUA_VLNGSTR makevariant(LUA_TSTRING, 1)  /* long strings */
0362 
0363 #define ttisstring(o)       checktype((o), LUA_TSTRING)
0364 #define ttisshrstring(o)    checktag((o), ctb(LUA_VSHRSTR))
0365 #define ttislngstring(o)    checktag((o), ctb(LUA_VLNGSTR))
0366 
0367 #define tsvalueraw(v)   (gco2ts((v).gc))
0368 
0369 #define tsvalue(o)  check_exp(ttisstring(o), gco2ts(val_(o).gc))
0370 
0371 #define setsvalue(L,obj,x) \
0372   { TValue *io = (obj); TString *x_ = (x); \
0373     val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \
0374     checkliveness(L,io); }
0375 
0376 /* set a string to the stack */
0377 #define setsvalue2s(L,o,s)  setsvalue(L,s2v(o),s)
0378 
0379 /* set a string to a new object */
0380 #define setsvalue2n setsvalue
0381 
0382 
0383 /*
0384 ** Header for a string value.
0385 */
0386 typedef struct TString {
0387   CommonHeader;
0388   lu_byte extra;  /* reserved words for short strings; "has hash" for longs */
0389   lu_byte shrlen;  /* length for short strings, 0xFF for long strings */
0390   unsigned int hash;
0391   union {
0392     size_t lnglen;  /* length for long strings */
0393     struct TString *hnext;  /* linked list for hash table */
0394   } u;
0395   char contents[1];
0396 } TString;
0397 
0398 
0399 
0400 /*
0401 ** Get the actual string (array of bytes) from a 'TString'. (Generic
0402 ** version and specialized versions for long and short strings.)
0403 */
0404 #define getstr(ts)  ((ts)->contents)
0405 #define getlngstr(ts)   check_exp((ts)->shrlen == 0xFF, (ts)->contents)
0406 #define getshrstr(ts)   check_exp((ts)->shrlen != 0xFF, (ts)->contents)
0407 
0408 
0409 /* get string length from 'TString *s' */
0410 #define tsslen(s)  \
0411     ((s)->shrlen != 0xFF ? (s)->shrlen : (s)->u.lnglen)
0412 
0413 /* }================================================================== */
0414 
0415 
0416 /*
0417 ** {==================================================================
0418 ** Userdata
0419 ** ===================================================================
0420 */
0421 
0422 
0423 /*
0424 ** Light userdata should be a variant of userdata, but for compatibility
0425 ** reasons they are also different types.
0426 */
0427 #define LUA_VLIGHTUSERDATA  makevariant(LUA_TLIGHTUSERDATA, 0)
0428 
0429 #define LUA_VUSERDATA       makevariant(LUA_TUSERDATA, 0)
0430 
0431 #define ttislightuserdata(o)    checktag((o), LUA_VLIGHTUSERDATA)
0432 #define ttisfulluserdata(o) checktag((o), ctb(LUA_VUSERDATA))
0433 
0434 #define pvalue(o)   check_exp(ttislightuserdata(o), val_(o).p)
0435 #define uvalue(o)   check_exp(ttisfulluserdata(o), gco2u(val_(o).gc))
0436 
0437 #define pvalueraw(v)    ((v).p)
0438 
0439 #define setpvalue(obj,x) \
0440   { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_VLIGHTUSERDATA); }
0441 
0442 #define setuvalue(L,obj,x) \
0443   { TValue *io = (obj); Udata *x_ = (x); \
0444     val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VUSERDATA)); \
0445     checkliveness(L,io); }
0446 
0447 
0448 /* Ensures that addresses after this type are always fully aligned. */
0449 typedef union UValue {
0450   TValue uv;
0451   LUAI_MAXALIGN;  /* ensures maximum alignment for udata bytes */
0452 } UValue;
0453 
0454 
0455 /*
0456 ** Header for userdata with user values;
0457 ** memory area follows the end of this structure.
0458 */
0459 typedef struct Udata {
0460   CommonHeader;
0461   unsigned short nuvalue;  /* number of user values */
0462   size_t len;  /* number of bytes */
0463   struct Table *metatable;
0464   GCObject *gclist;
0465   UValue uv[1];  /* user values */
0466 } Udata;
0467 
0468 
0469 /*
0470 ** Header for userdata with no user values. These userdata do not need
0471 ** to be gray during GC, and therefore do not need a 'gclist' field.
0472 ** To simplify, the code always use 'Udata' for both kinds of userdata,
0473 ** making sure it never accesses 'gclist' on userdata with no user values.
0474 ** This structure here is used only to compute the correct size for
0475 ** this representation. (The 'bindata' field in its end ensures correct
0476 ** alignment for binary data following this header.)
0477 */
0478 typedef struct Udata0 {
0479   CommonHeader;
0480   unsigned short nuvalue;  /* number of user values */
0481   size_t len;  /* number of bytes */
0482   struct Table *metatable;
0483   union {LUAI_MAXALIGN;} bindata;
0484 } Udata0;
0485 
0486 
0487 /* compute the offset of the memory area of a userdata */
0488 #define udatamemoffset(nuv) \
0489     ((nuv) == 0 ? offsetof(Udata0, bindata)  \
0490                     : offsetof(Udata, uv) + (sizeof(UValue) * (nuv)))
0491 
0492 /* get the address of the memory block inside 'Udata' */
0493 #define getudatamem(u)  (cast_charp(u) + udatamemoffset((u)->nuvalue))
0494 
0495 /* compute the size of a userdata */
0496 #define sizeudata(nuv,nb)   (udatamemoffset(nuv) + (nb))
0497 
0498 /* }================================================================== */
0499 
0500 
0501 /*
0502 ** {==================================================================
0503 ** Prototypes
0504 ** ===================================================================
0505 */
0506 
0507 #define LUA_VPROTO  makevariant(LUA_TPROTO, 0)
0508 
0509 
0510 /*
0511 ** Description of an upvalue for function prototypes
0512 */
0513 typedef struct Upvaldesc {
0514   TString *name;  /* upvalue name (for debug information) */
0515   lu_byte instack;  /* whether it is in stack (register) */
0516   lu_byte idx;  /* index of upvalue (in stack or in outer function's list) */
0517   lu_byte kind;  /* kind of corresponding variable */
0518 } Upvaldesc;
0519 
0520 
0521 /*
0522 ** Description of a local variable for function prototypes
0523 ** (used for debug information)
0524 */
0525 typedef struct LocVar {
0526   TString *varname;
0527   int startpc;  /* first point where variable is active */
0528   int endpc;    /* first point where variable is dead */
0529 } LocVar;
0530 
0531 
0532 /*
0533 ** Associates the absolute line source for a given instruction ('pc').
0534 ** The array 'lineinfo' gives, for each instruction, the difference in
0535 ** lines from the previous instruction. When that difference does not
0536 ** fit into a byte, Lua saves the absolute line for that instruction.
0537 ** (Lua also saves the absolute line periodically, to speed up the
0538 ** computation of a line number: we can use binary search in the
0539 ** absolute-line array, but we must traverse the 'lineinfo' array
0540 ** linearly to compute a line.)
0541 */
0542 typedef struct AbsLineInfo {
0543   int pc;
0544   int line;
0545 } AbsLineInfo;
0546 
0547 /*
0548 ** Function Prototypes
0549 */
0550 typedef struct Proto {
0551   CommonHeader;
0552   lu_byte numparams;  /* number of fixed (named) parameters */
0553   lu_byte is_vararg;
0554   lu_byte maxstacksize;  /* number of registers needed by this function */
0555   int sizeupvalues;  /* size of 'upvalues' */
0556   int sizek;  /* size of 'k' */
0557   int sizecode;
0558   int sizelineinfo;
0559   int sizep;  /* size of 'p' */
0560   int sizelocvars;
0561   int sizeabslineinfo;  /* size of 'abslineinfo' */
0562   int linedefined;  /* debug information  */
0563   int lastlinedefined;  /* debug information  */
0564   TValue *k;  /* constants used by the function */
0565   Instruction *code;  /* opcodes */
0566   struct Proto **p;  /* functions defined inside the function */
0567   Upvaldesc *upvalues;  /* upvalue information */
0568   ls_byte *lineinfo;  /* information about source lines (debug information) */
0569   AbsLineInfo *abslineinfo;  /* idem */
0570   LocVar *locvars;  /* information about local variables (debug information) */
0571   TString  *source;  /* used for debug information */
0572   GCObject *gclist;
0573 } Proto;
0574 
0575 /* }================================================================== */
0576 
0577 
0578 /*
0579 ** {==================================================================
0580 ** Functions
0581 ** ===================================================================
0582 */
0583 
0584 #define LUA_VUPVAL  makevariant(LUA_TUPVAL, 0)
0585 
0586 
0587 /* Variant tags for functions */
0588 #define LUA_VLCL    makevariant(LUA_TFUNCTION, 0)  /* Lua closure */
0589 #define LUA_VLCF    makevariant(LUA_TFUNCTION, 1)  /* light C function */
0590 #define LUA_VCCL    makevariant(LUA_TFUNCTION, 2)  /* C closure */
0591 
0592 #define ttisfunction(o)     checktype(o, LUA_TFUNCTION)
0593 #define ttisLclosure(o)     checktag((o), ctb(LUA_VLCL))
0594 #define ttislcf(o)      checktag((o), LUA_VLCF)
0595 #define ttisCclosure(o)     checktag((o), ctb(LUA_VCCL))
0596 #define ttisclosure(o)         (ttisLclosure(o) || ttisCclosure(o))
0597 
0598 
0599 #define isLfunction(o)  ttisLclosure(o)
0600 
0601 #define clvalue(o)  check_exp(ttisclosure(o), gco2cl(val_(o).gc))
0602 #define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc))
0603 #define fvalue(o)   check_exp(ttislcf(o), val_(o).f)
0604 #define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc))
0605 
0606 #define fvalueraw(v)    ((v).f)
0607 
0608 #define setclLvalue(L,obj,x) \
0609   { TValue *io = (obj); LClosure *x_ = (x); \
0610     val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VLCL)); \
0611     checkliveness(L,io); }
0612 
0613 #define setclLvalue2s(L,o,cl)   setclLvalue(L,s2v(o),cl)
0614 
0615 #define setfvalue(obj,x) \
0616   { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_VLCF); }
0617 
0618 #define setclCvalue(L,obj,x) \
0619   { TValue *io = (obj); CClosure *x_ = (x); \
0620     val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VCCL)); \
0621     checkliveness(L,io); }
0622 
0623 
0624 /*
0625 ** Upvalues for Lua closures
0626 */
0627 typedef struct UpVal {
0628   CommonHeader;
0629   union {
0630     TValue *p;  /* points to stack or to its own value */
0631     ptrdiff_t offset;  /* used while the stack is being reallocated */
0632   } v;
0633   union {
0634     struct {  /* (when open) */
0635       struct UpVal *next;  /* linked list */
0636       struct UpVal **previous;
0637     } open;
0638     TValue value;  /* the value (when closed) */
0639   } u;
0640 } UpVal;
0641 
0642 
0643 
0644 #define ClosureHeader \
0645     CommonHeader; lu_byte nupvalues; GCObject *gclist
0646 
0647 typedef struct CClosure {
0648   ClosureHeader;
0649   lua_CFunction f;
0650   TValue upvalue[1];  /* list of upvalues */
0651 } CClosure;
0652 
0653 
0654 typedef struct LClosure {
0655   ClosureHeader;
0656   struct Proto *p;
0657   UpVal *upvals[1];  /* list of upvalues */
0658 } LClosure;
0659 
0660 
0661 typedef union Closure {
0662   CClosure c;
0663   LClosure l;
0664 } Closure;
0665 
0666 
0667 #define getproto(o) (clLvalue(o)->p)
0668 
0669 /* }================================================================== */
0670 
0671 
0672 /*
0673 ** {==================================================================
0674 ** Tables
0675 ** ===================================================================
0676 */
0677 
0678 #define LUA_VTABLE  makevariant(LUA_TTABLE, 0)
0679 
0680 #define ttistable(o)        checktag((o), ctb(LUA_VTABLE))
0681 
0682 #define hvalue(o)   check_exp(ttistable(o), gco2t(val_(o).gc))
0683 
0684 #define sethvalue(L,obj,x) \
0685   { TValue *io = (obj); Table *x_ = (x); \
0686     val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTABLE)); \
0687     checkliveness(L,io); }
0688 
0689 #define sethvalue2s(L,o,h)  sethvalue(L,s2v(o),h)
0690 
0691 
0692 /*
0693 ** Nodes for Hash tables: A pack of two TValue's (key-value pairs)
0694 ** plus a 'next' field to link colliding entries. The distribution
0695 ** of the key's fields ('key_tt' and 'key_val') not forming a proper
0696 ** 'TValue' allows for a smaller size for 'Node' both in 4-byte
0697 ** and 8-byte alignments.
0698 */
0699 typedef union Node {
0700   struct NodeKey {
0701     TValuefields;  /* fields for value */
0702     lu_byte key_tt;  /* key type */
0703     int next;  /* for chaining */
0704     Value key_val;  /* key value */
0705   } u;
0706   TValue i_val;  /* direct access to node's value as a proper 'TValue' */
0707 } Node;
0708 
0709 
0710 /* copy a value into a key */
0711 #define setnodekey(L,node,obj) \
0712     { Node *n_=(node); const TValue *io_=(obj); \
0713       n_->u.key_val = io_->value_; n_->u.key_tt = io_->tt_; \
0714       checkliveness(L,io_); }
0715 
0716 
0717 /* copy a value from a key */
0718 #define getnodekey(L,obj,node) \
0719     { TValue *io_=(obj); const Node *n_=(node); \
0720       io_->value_ = n_->u.key_val; io_->tt_ = n_->u.key_tt; \
0721       checkliveness(L,io_); }
0722 
0723 
0724 /*
0725 ** About 'alimit': if 'isrealasize(t)' is true, then 'alimit' is the
0726 ** real size of 'array'. Otherwise, the real size of 'array' is the
0727 ** smallest power of two not smaller than 'alimit' (or zero iff 'alimit'
0728 ** is zero); 'alimit' is then used as a hint for #t.
0729 */
0730 
0731 #define BITRAS      (1 << 7)
0732 #define isrealasize(t)      (!((t)->flags & BITRAS))
0733 #define setrealasize(t)     ((t)->flags &= cast_byte(~BITRAS))
0734 #define setnorealasize(t)   ((t)->flags |= BITRAS)
0735 
0736 
0737 typedef struct Table {
0738   CommonHeader;
0739   lu_byte flags;  /* 1<<p means tagmethod(p) is not present */
0740   lu_byte lsizenode;  /* log2 of size of 'node' array */
0741   unsigned int alimit;  /* "limit" of 'array' array */
0742   TValue *array;  /* array part */
0743   Node *node;
0744   Node *lastfree;  /* any free position is before this position */
0745   struct Table *metatable;
0746   GCObject *gclist;
0747 } Table;
0748 
0749 
0750 /*
0751 ** Macros to manipulate keys inserted in nodes
0752 */
0753 #define keytt(node)     ((node)->u.key_tt)
0754 #define keyval(node)        ((node)->u.key_val)
0755 
0756 #define keyisnil(node)      (keytt(node) == LUA_TNIL)
0757 #define keyisinteger(node)  (keytt(node) == LUA_VNUMINT)
0758 #define keyival(node)       (keyval(node).i)
0759 #define keyisshrstr(node)   (keytt(node) == ctb(LUA_VSHRSTR))
0760 #define keystrval(node)     (gco2ts(keyval(node).gc))
0761 
0762 #define setnilkey(node)     (keytt(node) = LUA_TNIL)
0763 
0764 #define keyiscollectable(n) (keytt(n) & BIT_ISCOLLECTABLE)
0765 
0766 #define gckey(n)    (keyval(n).gc)
0767 #define gckeyN(n)   (keyiscollectable(n) ? gckey(n) : NULL)
0768 
0769 
0770 /*
0771 ** Dead keys in tables have the tag DEADKEY but keep their original
0772 ** gcvalue. This distinguishes them from regular keys but allows them to
0773 ** be found when searched in a special way. ('next' needs that to find
0774 ** keys removed from a table during a traversal.)
0775 */
0776 #define setdeadkey(node)    (keytt(node) = LUA_TDEADKEY)
0777 #define keyisdead(node)     (keytt(node) == LUA_TDEADKEY)
0778 
0779 /* }================================================================== */
0780 
0781 
0782 
0783 /*
0784 ** 'module' operation for hashing (size is always a power of 2)
0785 */
0786 #define lmod(s,size) \
0787     (check_exp((size&(size-1))==0, (cast_int((s) & ((size)-1)))))
0788 
0789 
0790 #define twoto(x)    (1<<(x))
0791 #define sizenode(t) (twoto((t)->lsizenode))
0792 
0793 
0794 /* size of buffer for 'luaO_utf8esc' function */
0795 #define UTF8BUFFSZ  8
0796 
0797 LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x);
0798 LUAI_FUNC int luaO_ceillog2 (unsigned int x);
0799 LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1,
0800                              const TValue *p2, TValue *res);
0801 LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1,
0802                            const TValue *p2, StkId res);
0803 LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o);
0804 LUAI_FUNC int luaO_hexavalue (int c);
0805 LUAI_FUNC void luaO_tostring (lua_State *L, TValue *obj);
0806 LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
0807                                                        va_list argp);
0808 LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
0809 LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t srclen);
0810 
0811 
0812 #endif
0813