zombiezen.com/go/lua@v0.0.0-20231013005828-290725fb9140/internal/lua54/lobject.h (about)

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