/*1** $Id: lobject.h $2** Type definitions for Lua objects3** See Copyright Notice in lua.h4*/567#ifndef lobject_h8#define lobject_h91011#include <stdarg.h>121314#include "llimits.h"15#include "lua.h"161718/*19** Extra types for collectable non-values20*/21#define LUA_TUPVAL LUA_NUMTYPES /* upvalues */22#define LUA_TPROTO (LUA_NUMTYPES+1) /* function prototypes */23#define LUA_TDEADKEY (LUA_NUMTYPES+2) /* removed keys in tables */24252627/*28** number of all possible types (including LUA_TNONE but excluding DEADKEY)29*/30#define LUA_TOTALTYPES (LUA_TPROTO + 2)313233/*34** tags for Tagged Values have the following use of bits:35** bits 0-3: actual tag (a LUA_T* constant)36** bits 4-5: variant bits37** bit 6: whether value is collectable38*/3940/* add variant bits to a type */41#define makevariant(t,v) ((t) | ((v) << 4))42434445/*46** Union of all Lua values47*/48typedef union Value {49struct GCObject *gc; /* collectable objects */50void *p; /* light userdata */51lua_CFunction f; /* light C functions */52lua_Integer i; /* integer numbers */53lua_Number n; /* float numbers */54/* not used, but may avoid warnings for uninitialized value */55lu_byte ub;56} Value;575859/*60** Tagged Values. This is the basic representation of values in Lua:61** an actual value plus a tag with its type.62*/6364#define TValuefields Value value_; lu_byte tt_6566typedef struct TValue {67TValuefields;68} TValue;697071#define val_(o) ((o)->value_)72#define valraw(o) (val_(o))737475/* raw type tag of a TValue */76#define rawtt(o) ((o)->tt_)7778/* tag with no variants (bits 0-3) */79#define novariant(t) ((t) & 0x0F)8081/* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */82#define withvariant(t) ((t) & 0x3F)83#define ttypetag(o) withvariant(rawtt(o))8485/* type of a TValue */86#define ttype(o) (novariant(rawtt(o)))878889/* Macros to test type */90#define checktag(o,t) (rawtt(o) == (t))91#define checktype(o,t) (ttype(o) == (t))929394/* Macros for internal tests */9596/* collectable object has the same tag as the original value */97#define righttt(obj) (ttypetag(obj) == gcvalue(obj)->tt)9899/*100** Any value being manipulated by the program either is non101** collectable, or the collectable object has the right tag102** and it is not dead. The option 'L == NULL' allows other103** macros using this one to be used where L is not available.104*/105#define checkliveness(L,obj) \106((void)L, lua_longassert(!iscollectable(obj) || \107(righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj))))))108109110/* Macros to set values */111112/* set a value's tag */113#define settt_(o,t) ((o)->tt_=(t))114115116/* main macro to copy values (from 'obj2' to 'obj1') */117#define setobj(L,obj1,obj2) \118{ TValue *io1=(obj1); const TValue *io2=(obj2); \119io1->value_ = io2->value_; settt_(io1, io2->tt_); \120checkliveness(L,io1); lua_assert(!isnonstrictnil(io1)); }121122/*123** Different types of assignments, according to source and destination.124** (They are mostly equal now, but may be different in the future.)125*/126127/* from stack to stack */128#define setobjs2s(L,o1,o2) setobj(L,s2v(o1),s2v(o2))129/* to stack (not from same stack) */130#define setobj2s(L,o1,o2) setobj(L,s2v(o1),o2)131/* from table to same table */132#define setobjt2t setobj133/* to new object */134#define setobj2n setobj135/* to table */136#define setobj2t setobj137138139/*140** Entries in a Lua stack. Field 'tbclist' forms a list of all141** to-be-closed variables active in this stack. Dummy entries are142** used when the distance between two tbc variables does not fit143** in an unsigned short. They are represented by delta==0, and144** their real delta is always the maximum value that fits in145** that field.146*/147typedef union StackValue {148TValue val;149struct {150TValuefields;151unsigned short delta;152} tbclist;153} StackValue;154155156/* index to stack elements */157typedef StackValue *StkId;158159160/*161** When reallocating the stack, change all pointers to the stack into162** proper offsets.163*/164typedef union {165StkId p; /* actual pointer */166ptrdiff_t offset; /* used while the stack is being reallocated */167} StkIdRel;168169170/* convert a 'StackValue' to a 'TValue' */171#define s2v(o) (&(o)->val)172173174175/*176** {==================================================================177** Nil178** ===================================================================179*/180181/* Standard nil */182#define LUA_VNIL makevariant(LUA_TNIL, 0)183184/* Empty slot (which might be different from a slot containing nil) */185#define LUA_VEMPTY makevariant(LUA_TNIL, 1)186187/* Value returned for a key not found in a table (absent key) */188#define LUA_VABSTKEY makevariant(LUA_TNIL, 2)189190191/* macro to test for (any kind of) nil */192#define ttisnil(v) checktype((v), LUA_TNIL)193194195/* macro to test for a standard nil */196#define ttisstrictnil(o) checktag((o), LUA_VNIL)197198199#define setnilvalue(obj) settt_(obj, LUA_VNIL)200201202#define isabstkey(v) checktag((v), LUA_VABSTKEY)203204205/*206** macro to detect non-standard nils (used only in assertions)207*/208#define isnonstrictnil(v) (ttisnil(v) && !ttisstrictnil(v))209210211/*212** By default, entries with any kind of nil are considered empty.213** (In any definition, values associated with absent keys must also214** be accepted as empty.)215*/216#define isempty(v) ttisnil(v)217218219/* macro defining a value corresponding to an absent key */220#define ABSTKEYCONSTANT {NULL}, LUA_VABSTKEY221222223/* mark an entry as empty */224#define setempty(v) settt_(v, LUA_VEMPTY)225226227228/* }================================================================== */229230231/*232** {==================================================================233** Booleans234** ===================================================================235*/236237238#define LUA_VFALSE makevariant(LUA_TBOOLEAN, 0)239#define LUA_VTRUE makevariant(LUA_TBOOLEAN, 1)240241#define ttisboolean(o) checktype((o), LUA_TBOOLEAN)242#define ttisfalse(o) checktag((o), LUA_VFALSE)243#define ttistrue(o) checktag((o), LUA_VTRUE)244245246#define l_isfalse(o) (ttisfalse(o) || ttisnil(o))247248249#define setbfvalue(obj) settt_(obj, LUA_VFALSE)250#define setbtvalue(obj) settt_(obj, LUA_VTRUE)251252/* }================================================================== */253254255/*256** {==================================================================257** Threads258** ===================================================================259*/260261#define LUA_VTHREAD makevariant(LUA_TTHREAD, 0)262263#define ttisthread(o) checktag((o), ctb(LUA_VTHREAD))264265#define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc))266267#define setthvalue(L,obj,x) \268{ TValue *io = (obj); lua_State *x_ = (x); \269val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTHREAD)); \270checkliveness(L,io); }271272#define setthvalue2s(L,o,t) setthvalue(L,s2v(o),t)273274/* }================================================================== */275276277/*278** {==================================================================279** Collectable Objects280** ===================================================================281*/282283/*284** Common Header for all collectable objects (in macro form, to be285** included in other objects)286*/287#define CommonHeader struct GCObject *next; lu_byte tt; lu_byte marked288289290/* Common type for all collectable objects */291typedef struct GCObject {292CommonHeader;293} GCObject;294295296/* Bit mark for collectable types */297#define BIT_ISCOLLECTABLE (1 << 6)298299#define iscollectable(o) (rawtt(o) & BIT_ISCOLLECTABLE)300301/* mark a tag as collectable */302#define ctb(t) ((t) | BIT_ISCOLLECTABLE)303304#define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)305306#define gcvalueraw(v) ((v).gc)307308#define setgcovalue(L,obj,x) \309{ TValue *io = (obj); GCObject *i_g=(x); \310val_(io).gc = i_g; settt_(io, ctb(i_g->tt)); }311312/* }================================================================== */313314315/*316** {==================================================================317** Numbers318** ===================================================================319*/320321/* Variant tags for numbers */322#define LUA_VNUMINT makevariant(LUA_TNUMBER, 0) /* integer numbers */323#define LUA_VNUMFLT makevariant(LUA_TNUMBER, 1) /* float numbers */324325#define ttisnumber(o) checktype((o), LUA_TNUMBER)326#define ttisfloat(o) checktag((o), LUA_VNUMFLT)327#define ttisinteger(o) checktag((o), LUA_VNUMINT)328329#define nvalue(o) check_exp(ttisnumber(o), \330(ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o)))331#define fltvalue(o) check_exp(ttisfloat(o), val_(o).n)332#define ivalue(o) check_exp(ttisinteger(o), val_(o).i)333334#define fltvalueraw(v) ((v).n)335#define ivalueraw(v) ((v).i)336337#define setfltvalue(obj,x) \338{ TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_VNUMFLT); }339340#define chgfltvalue(obj,x) \341{ TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); }342343#define setivalue(obj,x) \344{ TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_VNUMINT); }345346#define chgivalue(obj,x) \347{ TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); }348349/* }================================================================== */350351352/*353** {==================================================================354** Strings355** ===================================================================356*/357358/* Variant tags for strings */359#define LUA_VSHRSTR makevariant(LUA_TSTRING, 0) /* short strings */360#define LUA_VLNGSTR makevariant(LUA_TSTRING, 1) /* long strings */361362#define ttisstring(o) checktype((o), LUA_TSTRING)363#define ttisshrstring(o) checktag((o), ctb(LUA_VSHRSTR))364#define ttislngstring(o) checktag((o), ctb(LUA_VLNGSTR))365366#define tsvalueraw(v) (gco2ts((v).gc))367368#define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc))369370#define setsvalue(L,obj,x) \371{ TValue *io = (obj); TString *x_ = (x); \372val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \373checkliveness(L,io); }374375/* set a string to the stack */376#define setsvalue2s(L,o,s) setsvalue(L,s2v(o),s)377378/* set a string to a new object */379#define setsvalue2n setsvalue380381382/*383** Header for a string value.384*/385typedef struct TString {386CommonHeader;387lu_byte extra; /* reserved words for short strings; "has hash" for longs */388lu_byte shrlen; /* length for short strings, 0xFF for long strings */389unsigned int hash;390union {391size_t lnglen; /* length for long strings */392struct TString *hnext; /* linked list for hash table */393} u;394char contents[1];395} TString;396397398399/*400** Get the actual string (array of bytes) from a 'TString'. (Generic401** version and specialized versions for long and short strings.)402*/403#define getstr(ts) ((ts)->contents)404#define getlngstr(ts) check_exp((ts)->shrlen == 0xFF, (ts)->contents)405#define getshrstr(ts) check_exp((ts)->shrlen != 0xFF, (ts)->contents)406407408/* get string length from 'TString *s' */409#define tsslen(s) \410((s)->shrlen != 0xFF ? (s)->shrlen : (s)->u.lnglen)411412/* }================================================================== */413414415/*416** {==================================================================417** Userdata418** ===================================================================419*/420421422/*423** Light userdata should be a variant of userdata, but for compatibility424** reasons they are also different types.425*/426#define LUA_VLIGHTUSERDATA makevariant(LUA_TLIGHTUSERDATA, 0)427428#define LUA_VUSERDATA makevariant(LUA_TUSERDATA, 0)429430#define ttislightuserdata(o) checktag((o), LUA_VLIGHTUSERDATA)431#define ttisfulluserdata(o) checktag((o), ctb(LUA_VUSERDATA))432433#define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)434#define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc))435436#define pvalueraw(v) ((v).p)437438#define setpvalue(obj,x) \439{ TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_VLIGHTUSERDATA); }440441#define setuvalue(L,obj,x) \442{ TValue *io = (obj); Udata *x_ = (x); \443val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VUSERDATA)); \444checkliveness(L,io); }445446447/* Ensures that addresses after this type are always fully aligned. */448typedef union UValue {449TValue uv;450LUAI_MAXALIGN; /* ensures maximum alignment for udata bytes */451} UValue;452453454/*455** Header for userdata with user values;456** memory area follows the end of this structure.457*/458typedef struct Udata {459CommonHeader;460unsigned short nuvalue; /* number of user values */461size_t len; /* number of bytes */462struct Table *metatable;463GCObject *gclist;464UValue uv[1]; /* user values */465} Udata;466467468/*469** Header for userdata with no user values. These userdata do not need470** to be gray during GC, and therefore do not need a 'gclist' field.471** To simplify, the code always use 'Udata' for both kinds of userdata,472** making sure it never accesses 'gclist' on userdata with no user values.473** This structure here is used only to compute the correct size for474** this representation. (The 'bindata' field in its end ensures correct475** alignment for binary data following this header.)476*/477typedef struct Udata0 {478CommonHeader;479unsigned short nuvalue; /* number of user values */480size_t len; /* number of bytes */481struct Table *metatable;482union {LUAI_MAXALIGN;} bindata;483} Udata0;484485486/* compute the offset of the memory area of a userdata */487#define udatamemoffset(nuv) \488((nuv) == 0 ? offsetof(Udata0, bindata) \489: offsetof(Udata, uv) + (sizeof(UValue) * (nuv)))490491/* get the address of the memory block inside 'Udata' */492#define getudatamem(u) (cast_charp(u) + udatamemoffset((u)->nuvalue))493494/* compute the size of a userdata */495#define sizeudata(nuv,nb) (udatamemoffset(nuv) + (nb))496497/* }================================================================== */498499500/*501** {==================================================================502** Prototypes503** ===================================================================504*/505506#define LUA_VPROTO makevariant(LUA_TPROTO, 0)507508509/*510** Description of an upvalue for function prototypes511*/512typedef struct Upvaldesc {513TString *name; /* upvalue name (for debug information) */514lu_byte instack; /* whether it is in stack (register) */515lu_byte idx; /* index of upvalue (in stack or in outer function's list) */516lu_byte kind; /* kind of corresponding variable */517} Upvaldesc;518519520/*521** Description of a local variable for function prototypes522** (used for debug information)523*/524typedef struct LocVar {525TString *varname;526int startpc; /* first point where variable is active */527int endpc; /* first point where variable is dead */528} LocVar;529530531/*532** Associates the absolute line source for a given instruction ('pc').533** The array 'lineinfo' gives, for each instruction, the difference in534** lines from the previous instruction. When that difference does not535** fit into a byte, Lua saves the absolute line for that instruction.536** (Lua also saves the absolute line periodically, to speed up the537** computation of a line number: we can use binary search in the538** absolute-line array, but we must traverse the 'lineinfo' array539** linearly to compute a line.)540*/541typedef struct AbsLineInfo {542int pc;543int line;544} AbsLineInfo;545546/*547** Function Prototypes548*/549typedef struct Proto {550CommonHeader;551lu_byte numparams; /* number of fixed (named) parameters */552lu_byte is_vararg;553lu_byte maxstacksize; /* number of registers needed by this function */554int sizeupvalues; /* size of 'upvalues' */555int sizek; /* size of 'k' */556int sizecode;557int sizelineinfo;558int sizep; /* size of 'p' */559int sizelocvars;560int sizeabslineinfo; /* size of 'abslineinfo' */561int linedefined; /* debug information */562int lastlinedefined; /* debug information */563TValue *k; /* constants used by the function */564Instruction *code; /* opcodes */565struct Proto **p; /* functions defined inside the function */566Upvaldesc *upvalues; /* upvalue information */567ls_byte *lineinfo; /* information about source lines (debug information) */568AbsLineInfo *abslineinfo; /* idem */569LocVar *locvars; /* information about local variables (debug information) */570TString *source; /* used for debug information */571GCObject *gclist;572} Proto;573574/* }================================================================== */575576577/*578** {==================================================================579** Functions580** ===================================================================581*/582583#define LUA_VUPVAL makevariant(LUA_TUPVAL, 0)584585586/* Variant tags for functions */587#define LUA_VLCL makevariant(LUA_TFUNCTION, 0) /* Lua closure */588#define LUA_VLCF makevariant(LUA_TFUNCTION, 1) /* light C function */589#define LUA_VCCL makevariant(LUA_TFUNCTION, 2) /* C closure */590591#define ttisfunction(o) checktype(o, LUA_TFUNCTION)592#define ttisLclosure(o) checktag((o), ctb(LUA_VLCL))593#define ttislcf(o) checktag((o), LUA_VLCF)594#define ttisCclosure(o) checktag((o), ctb(LUA_VCCL))595#define ttisclosure(o) (ttisLclosure(o) || ttisCclosure(o))596597598#define isLfunction(o) ttisLclosure(o)599600#define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc))601#define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc))602#define fvalue(o) check_exp(ttislcf(o), val_(o).f)603#define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc))604605#define fvalueraw(v) ((v).f)606607#define setclLvalue(L,obj,x) \608{ TValue *io = (obj); LClosure *x_ = (x); \609val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VLCL)); \610checkliveness(L,io); }611612#define setclLvalue2s(L,o,cl) setclLvalue(L,s2v(o),cl)613614#define setfvalue(obj,x) \615{ TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_VLCF); }616617#define setclCvalue(L,obj,x) \618{ TValue *io = (obj); CClosure *x_ = (x); \619val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VCCL)); \620checkliveness(L,io); }621622623/*624** Upvalues for Lua closures625*/626typedef struct UpVal {627CommonHeader;628union {629TValue *p; /* points to stack or to its own value */630ptrdiff_t offset; /* used while the stack is being reallocated */631} v;632union {633struct { /* (when open) */634struct UpVal *next; /* linked list */635struct UpVal **previous;636} open;637TValue value; /* the value (when closed) */638} u;639} UpVal;640641642643#define ClosureHeader \644CommonHeader; lu_byte nupvalues; GCObject *gclist645646typedef struct CClosure {647ClosureHeader;648lua_CFunction f;649TValue upvalue[1]; /* list of upvalues */650} CClosure;651652653typedef struct LClosure {654ClosureHeader;655struct Proto *p;656UpVal *upvals[1]; /* list of upvalues */657} LClosure;658659660typedef union Closure {661CClosure c;662LClosure l;663} Closure;664665666#define getproto(o) (clLvalue(o)->p)667668/* }================================================================== */669670671/*672** {==================================================================673** Tables674** ===================================================================675*/676677#define LUA_VTABLE makevariant(LUA_TTABLE, 0)678679#define ttistable(o) checktag((o), ctb(LUA_VTABLE))680681#define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc))682683#define sethvalue(L,obj,x) \684{ TValue *io = (obj); Table *x_ = (x); \685val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTABLE)); \686checkliveness(L,io); }687688#define sethvalue2s(L,o,h) sethvalue(L,s2v(o),h)689690691/*692** Nodes for Hash tables: A pack of two TValue's (key-value pairs)693** plus a 'next' field to link colliding entries. The distribution694** of the key's fields ('key_tt' and 'key_val') not forming a proper695** 'TValue' allows for a smaller size for 'Node' both in 4-byte696** and 8-byte alignments.697*/698typedef union Node {699struct NodeKey {700TValuefields; /* fields for value */701lu_byte key_tt; /* key type */702int next; /* for chaining */703Value key_val; /* key value */704} u;705TValue i_val; /* direct access to node's value as a proper 'TValue' */706} Node;707708709/* copy a value into a key */710#define setnodekey(L,node,obj) \711{ Node *n_=(node); const TValue *io_=(obj); \712n_->u.key_val = io_->value_; n_->u.key_tt = io_->tt_; \713checkliveness(L,io_); }714715716/* copy a value from a key */717#define getnodekey(L,obj,node) \718{ TValue *io_=(obj); const Node *n_=(node); \719io_->value_ = n_->u.key_val; io_->tt_ = n_->u.key_tt; \720checkliveness(L,io_); }721722723/*724** About 'alimit': if 'isrealasize(t)' is true, then 'alimit' is the725** real size of 'array'. Otherwise, the real size of 'array' is the726** smallest power of two not smaller than 'alimit' (or zero iff 'alimit'727** is zero); 'alimit' is then used as a hint for #t.728*/729730#define BITRAS (1 << 7)731#define isrealasize(t) (!((t)->flags & BITRAS))732#define setrealasize(t) ((t)->flags &= cast_byte(~BITRAS))733#define setnorealasize(t) ((t)->flags |= BITRAS)734735736typedef struct Table {737CommonHeader;738lu_byte flags; /* 1<<p means tagmethod(p) is not present */739lu_byte lsizenode; /* log2 of size of 'node' array */740unsigned int alimit; /* "limit" of 'array' array */741TValue *array; /* array part */742Node *node;743Node *lastfree; /* any free position is before this position */744struct Table *metatable;745GCObject *gclist;746} Table;747748749/*750** Macros to manipulate keys inserted in nodes751*/752#define keytt(node) ((node)->u.key_tt)753#define keyval(node) ((node)->u.key_val)754755#define keyisnil(node) (keytt(node) == LUA_TNIL)756#define keyisinteger(node) (keytt(node) == LUA_VNUMINT)757#define keyival(node) (keyval(node).i)758#define keyisshrstr(node) (keytt(node) == ctb(LUA_VSHRSTR))759#define keystrval(node) (gco2ts(keyval(node).gc))760761#define setnilkey(node) (keytt(node) = LUA_TNIL)762763#define keyiscollectable(n) (keytt(n) & BIT_ISCOLLECTABLE)764765#define gckey(n) (keyval(n).gc)766#define gckeyN(n) (keyiscollectable(n) ? gckey(n) : NULL)767768769/*770** Dead keys in tables have the tag DEADKEY but keep their original771** gcvalue. This distinguishes them from regular keys but allows them to772** be found when searched in a special way. ('next' needs that to find773** keys removed from a table during a traversal.)774*/775#define setdeadkey(node) (keytt(node) = LUA_TDEADKEY)776#define keyisdead(node) (keytt(node) == LUA_TDEADKEY)777778/* }================================================================== */779780781782/*783** 'module' operation for hashing (size is always a power of 2)784*/785#define lmod(s,size) \786(check_exp((size&(size-1))==0, (cast_int((s) & ((size)-1)))))787788789#define twoto(x) (1<<(x))790#define sizenode(t) (twoto((t)->lsizenode))791792793/* size of buffer for 'luaO_utf8esc' function */794#define UTF8BUFFSZ 8795796LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x);797LUAI_FUNC int luaO_ceillog2 (unsigned int x);798LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1,799const TValue *p2, TValue *res);800LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1,801const TValue *p2, StkId res);802LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o);803LUAI_FUNC int luaO_hexavalue (int c);804LUAI_FUNC void luaO_tostring (lua_State *L, TValue *obj);805LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,806va_list argp);807LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);808LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t srclen);809810811#endif812813814815