3 ** Type definitions for Lua objects
4 ** See Copyright Notice in lua.h
20 ** Extra types for collectable non-values
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 */
29 ** number of all possible types (including LUA_TNONE but excluding DEADKEY)
31 #define LUA_TOTALTYPES (LUA_TPROTO + 2)
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
41 /* add variant bits to a type */
42 #define makevariant(t,v) ((t) | ((v) << 4))
47 ** Union of all Lua values
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 */
61 ** Tagged Values. This is the basic representation of values in Lua:
62 ** an actual value plus a tag with its type.
65 #define TValuefields Value value_; lu_byte tt_
67 typedef struct TValue
{
72 #define val_(o) ((o)->value_)
73 #define valraw(o) (val_(o))
76 /* raw type tag of a TValue */
77 #define rawtt(o) ((o)->tt_)
79 /* tag with no variants (bits 0-3) */
80 #define novariant(t) ((t) & 0x0F)
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))
86 /* type of a TValue */
87 #define ttype(o) (novariant(rawtt(o)))
90 /* Macros to test type */
91 #define checktag(o,t) (rawtt(o) == (t))
92 #define checktype(o,t) (ttype(o) == (t))
95 /* Macros for internal tests */
97 /* collectable object has the same tag as the original value */
98 #define righttt(obj) (ttypetag(obj) == gcvalue(obj)->tt)
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.
106 #define checkliveness(L,obj) \
107 ((void)L, lua_longassert(!iscollectable(obj) || \
108 (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj))))))
111 /* Macros to set values */
113 /* set a value's tag */
114 #define settt_(o,t) ((o)->tt_=(t))
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)); }
124 ** Different types of assignments, according to source and destination.
125 ** (They are mostly equal now, but may be different in the future.)
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
135 #define setobj2n setobj
137 #define setobj2t setobj
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
148 typedef union StackValue
{
152 unsigned short delta
;
157 /* index to stack elements */
158 typedef StackValue
*StkId
;
162 ** When reallocating the stack, change all pointers to the stack into
166 StkId p
; /* actual pointer */
167 ptrdiff_t offset
; /* used while the stack is being reallocated */
171 /* convert a 'StackValue' to a 'TValue' */
172 #define s2v(o) (&(o)->val)
177 ** {==================================================================
179 ** ===================================================================
183 #define LUA_VNIL makevariant(LUA_TNIL, 0)
185 /* Empty slot (which might be different from a slot containing nil) */
186 #define LUA_VEMPTY makevariant(LUA_TNIL, 1)
188 /* Value returned for a key not found in a table (absent key) */
189 #define LUA_VABSTKEY makevariant(LUA_TNIL, 2)
192 /* macro to test for (any kind of) nil */
193 #define ttisnil(v) checktype((v), LUA_TNIL)
196 /* macro to test for a standard nil */
197 #define ttisstrictnil(o) checktag((o), LUA_VNIL)
200 #define setnilvalue(obj) settt_(obj, LUA_VNIL)
203 #define isabstkey(v) checktag((v), LUA_VABSTKEY)
207 ** macro to detect non-standard nils (used only in assertions)
209 #define isnonstrictnil(v) (ttisnil(v) && !ttisstrictnil(v))
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.)
217 #define isempty(v) ttisnil(v)
220 /* macro defining a value corresponding to an absent key */
221 #define ABSTKEYCONSTANT {NULL}, LUA_VABSTKEY
224 /* mark an entry as empty */
225 #define setempty(v) settt_(v, LUA_VEMPTY)
229 /* }================================================================== */
233 ** {==================================================================
235 ** ===================================================================
239 #define LUA_VFALSE makevariant(LUA_TBOOLEAN, 0)
240 #define LUA_VTRUE makevariant(LUA_TBOOLEAN, 1)
242 #define ttisboolean(o) checktype((o), LUA_TBOOLEAN)
243 #define ttisfalse(o) checktag((o), LUA_VFALSE)
244 #define ttistrue(o) checktag((o), LUA_VTRUE)
247 #define l_isfalse(o) (ttisfalse(o) || ttisnil(o))
250 #define setbfvalue(obj) settt_(obj, LUA_VFALSE)
251 #define setbtvalue(obj) settt_(obj, LUA_VTRUE)
253 /* }================================================================== */
257 ** {==================================================================
259 ** ===================================================================
262 #define LUA_VTHREAD makevariant(LUA_TTHREAD, 0)
264 #define ttisthread(o) checktag((o), ctb(LUA_VTHREAD))
266 #define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc))
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); }
273 #define setthvalue2s(L,o,t) setthvalue(L,s2v(o),t)
275 /* }================================================================== */
279 ** {==================================================================
280 ** Collectable Objects
281 ** ===================================================================
285 ** Common Header for all collectable objects (in macro form, to be
286 ** included in other objects)
288 #define CommonHeader struct GCObject *next; lu_byte tt; lu_byte marked
291 /* Common type for all collectable objects */
292 typedef struct GCObject
{
297 /* Bit mark for collectable types */
298 #define BIT_ISCOLLECTABLE (1 << 6)
300 #define iscollectable(o) (rawtt(o) & BIT_ISCOLLECTABLE)
302 /* mark a tag as collectable */
303 #define ctb(t) ((t) | BIT_ISCOLLECTABLE)
305 #define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
307 #define gcvalueraw(v) ((v).gc)
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)); }
313 /* }================================================================== */
317 ** {==================================================================
319 ** ===================================================================
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 */
326 #define ttisnumber(o) checktype((o), LUA_TNUMBER)
327 #define ttisfloat(o) checktag((o), LUA_VNUMFLT)
328 #define ttisinteger(o) checktag((o), LUA_VNUMINT)
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)
335 #define fltvalueraw(v) ((v).n)
336 #define ivalueraw(v) ((v).i)
338 #define setfltvalue(obj,x) \
339 { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_VNUMFLT); }
341 #define chgfltvalue(obj,x) \
342 { TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); }
344 #define setivalue(obj,x) \
345 { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_VNUMINT); }
347 #define chgivalue(obj,x) \
348 { TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); }
350 /* }================================================================== */
354 ** {==================================================================
356 ** ===================================================================
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 */
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))
367 #define tsvalueraw(v) (gco2ts((v).gc))
369 #define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc))
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); }
376 /* set a string to the stack */
377 #define setsvalue2s(L,o,s) setsvalue(L,s2v(o),s)
379 /* set a string to a new object */
380 #define setsvalue2n setsvalue
384 ** Header for a string value.
386 typedef struct TString
{
388 lu_byte extra
; /* reserved words for short strings; "has hash" for longs */
389 lu_byte shrlen
; /* length for short strings, 0xFF for long strings */
392 size_t lnglen
; /* length for long strings */
393 struct TString
*hnext
; /* linked list for hash table */
401 ** Get the actual string (array of bytes) from a 'TString'. (Generic
402 ** version and specialized versions for long and short strings.)
404 #define getstr(ts) ((ts)->contents)
405 #define getlngstr(ts) check_exp((ts)->shrlen == 0xFF, (ts)->contents)
406 #define getshrstr(ts) check_exp((ts)->shrlen != 0xFF, (ts)->contents)
409 /* get string length from 'TString *s' */
411 ((s)->shrlen != 0xFF ? (s)->shrlen : (s)->u.lnglen)
413 /* }================================================================== */
417 ** {==================================================================
419 ** ===================================================================
424 ** Light userdata should be a variant of userdata, but for compatibility
425 ** reasons they are also different types.
427 #define LUA_VLIGHTUSERDATA makevariant(LUA_TLIGHTUSERDATA, 0)
429 #define LUA_VUSERDATA makevariant(LUA_TUSERDATA, 0)
431 #define ttislightuserdata(o) checktag((o), LUA_VLIGHTUSERDATA)
432 #define ttisfulluserdata(o) checktag((o), ctb(LUA_VUSERDATA))
434 #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
435 #define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc))
437 #define pvalueraw(v) ((v).p)
439 #define setpvalue(obj,x) \
440 { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_VLIGHTUSERDATA); }
442 #define setuvalue(L,obj,x) \
443 { TValue *io = (obj); Udata *x_ = (x); \
444 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VUSERDATA)); \
445 checkliveness(L,io); }
448 /* Ensures that addresses after this type are always fully aligned. */
449 typedef union UValue
{
451 LUAI_MAXALIGN
; /* ensures maximum alignment for udata bytes */
456 ** Header for userdata with user values;
457 ** memory area follows the end of this structure.
459 typedef struct Udata
{
461 unsigned short nuvalue
; /* number of user values */
462 size_t len
; /* number of bytes */
463 struct Table
*metatable
;
465 UValue uv
[1]; /* user values */
470 ** Header for userdata with no user values. These userdata do not need
471 ** to be gray during GC, and therefore do not need a 'gclist' field.
472 ** To simplify, the code always use 'Udata' for both kinds of userdata,
473 ** making sure it never accesses 'gclist' on userdata with no user values.
474 ** This structure here is used only to compute the correct size for
475 ** this representation. (The 'bindata' field in its end ensures correct
476 ** alignment for binary data following this header.)
478 typedef struct Udata0
{
480 unsigned short nuvalue
; /* number of user values */
481 size_t len
; /* number of bytes */
482 struct Table
*metatable
;
483 union {LUAI_MAXALIGN
;} bindata
;
487 /* compute the offset of the memory area of a userdata */
488 #define udatamemoffset(nuv) \
489 ((nuv) == 0 ? offsetof(Udata0, bindata) \
490 : offsetof(Udata, uv) + (sizeof(UValue) * (nuv)))
492 /* get the address of the memory block inside 'Udata' */
493 #define getudatamem(u) (cast_charp(u) + udatamemoffset((u)->nuvalue))
495 /* compute the size of a userdata */
496 #define sizeudata(nuv,nb) (udatamemoffset(nuv) + (nb))
498 /* }================================================================== */
502 ** {==================================================================
504 ** ===================================================================
507 #define LUA_VPROTO makevariant(LUA_TPROTO, 0)
511 ** Description of an upvalue for function prototypes
513 typedef struct Upvaldesc
{
514 TString
*name
; /* upvalue name (for debug information) */
515 lu_byte instack
; /* whether it is in stack (register) */
516 lu_byte idx
; /* index of upvalue (in stack or in outer function's list) */
517 lu_byte kind
; /* kind of corresponding variable */
522 ** Description of a local variable for function prototypes
523 ** (used for debug information)
525 typedef struct LocVar
{
527 int startpc
; /* first point where variable is active */
528 int endpc
; /* first point where variable is dead */
533 ** Associates the absolute line source for a given instruction ('pc').
534 ** The array 'lineinfo' gives, for each instruction, the difference in
535 ** lines from the previous instruction. When that difference does not
536 ** fit into a byte, Lua saves the absolute line for that instruction.
537 ** (Lua also saves the absolute line periodically, to speed up the
538 ** computation of a line number: we can use binary search in the
539 ** absolute-line array, but we must traverse the 'lineinfo' array
540 ** linearly to compute a line.)
542 typedef struct AbsLineInfo
{
548 ** Function Prototypes
550 typedef struct Proto
{
552 lu_byte numparams
; /* number of fixed (named) parameters */
554 lu_byte maxstacksize
; /* number of registers needed by this function */
555 int sizeupvalues
; /* size of 'upvalues' */
556 int sizek
; /* size of 'k' */
559 int sizep
; /* size of 'p' */
561 int sizeabslineinfo
; /* size of 'abslineinfo' */
562 int linedefined
; /* debug information */
563 int lastlinedefined
; /* debug information */
564 TValue
*k
; /* constants used by the function */
565 Instruction
*code
; /* opcodes */
566 struct Proto
**p
; /* functions defined inside the function */
567 Upvaldesc
*upvalues
; /* upvalue information */
568 ls_byte
*lineinfo
; /* information about source lines (debug information) */
569 AbsLineInfo
*abslineinfo
; /* idem */
570 LocVar
*locvars
; /* information about local variables (debug information) */
571 TString
*source
; /* used for debug information */
575 /* }================================================================== */
579 ** {==================================================================
581 ** ===================================================================
584 #define LUA_VUPVAL makevariant(LUA_TUPVAL, 0)
587 /* Variant tags for functions */
588 #define LUA_VLCL makevariant(LUA_TFUNCTION, 0) /* Lua closure */
589 #define LUA_VLCF makevariant(LUA_TFUNCTION, 1) /* light C function */
590 #define LUA_VCCL makevariant(LUA_TFUNCTION, 2) /* C closure */
592 #define ttisfunction(o) checktype(o, LUA_TFUNCTION)
593 #define ttisLclosure(o) checktag((o), ctb(LUA_VLCL))
594 #define ttislcf(o) checktag((o), LUA_VLCF)
595 #define ttisCclosure(o) checktag((o), ctb(LUA_VCCL))
596 #define ttisclosure(o) (ttisLclosure(o) || ttisCclosure(o))
599 #define isLfunction(o) ttisLclosure(o)
601 #define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc))
602 #define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc))
603 #define fvalue(o) check_exp(ttislcf(o), val_(o).f)
604 #define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc))
606 #define fvalueraw(v) ((v).f)
608 #define setclLvalue(L,obj,x) \
609 { TValue *io = (obj); LClosure *x_ = (x); \
610 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VLCL)); \
611 checkliveness(L,io); }
613 #define setclLvalue2s(L,o,cl) setclLvalue(L,s2v(o),cl)
615 #define setfvalue(obj,x) \
616 { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_VLCF); }
618 #define setclCvalue(L,obj,x) \
619 { TValue *io = (obj); CClosure *x_ = (x); \
620 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VCCL)); \
621 checkliveness(L,io); }
625 ** Upvalues for Lua closures
627 typedef struct UpVal
{
630 TValue
*p
; /* points to stack or to its own value */
631 ptrdiff_t offset
; /* used while the stack is being reallocated */
634 struct { /* (when open) */
635 struct UpVal
*next
; /* linked list */
636 struct UpVal
**previous
;
638 TValue value
; /* the value (when closed) */
644 #define ClosureHeader \
645 CommonHeader; lu_byte nupvalues; GCObject *gclist
647 typedef struct CClosure
{
650 TValue upvalue
[1]; /* list of upvalues */
654 typedef struct LClosure
{
657 UpVal
*upvals
[1]; /* list of upvalues */
661 typedef union Closure
{
667 #define getproto(o) (clLvalue(o)->p)
669 /* }================================================================== */
673 ** {==================================================================
675 ** ===================================================================
678 #define LUA_VTABLE makevariant(LUA_TTABLE, 0)
680 #define ttistable(o) checktag((o), ctb(LUA_VTABLE))
682 #define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc))
684 #define sethvalue(L,obj,x) \
685 { TValue *io = (obj); Table *x_ = (x); \
686 val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTABLE)); \
687 checkliveness(L,io); }
689 #define sethvalue2s(L,o,h) sethvalue(L,s2v(o),h)
693 ** Nodes for Hash tables: A pack of two TValue's (key-value pairs)
694 ** plus a 'next' field to link colliding entries. The distribution
695 ** of the key's fields ('key_tt' and 'key_val') not forming a proper
696 ** 'TValue' allows for a smaller size for 'Node' both in 4-byte
697 ** and 8-byte alignments.
701 TValuefields
; /* fields for value */
702 lu_byte key_tt
; /* key type */
703 int next
; /* for chaining */
704 Value key_val
; /* key value */
706 TValue i_val
; /* direct access to node's value as a proper 'TValue' */
710 /* copy a value into a key */
711 #define setnodekey(L,node,obj) \
712 { Node *n_=(node); const TValue *io_=(obj); \
713 n_->u.key_val = io_->value_; n_->u.key_tt = io_->tt_; \
714 checkliveness(L,io_); }
717 /* copy a value from a key */
718 #define getnodekey(L,obj,node) \
719 { TValue *io_=(obj); const Node *n_=(node); \
720 io_->value_ = n_->u.key_val; io_->tt_ = n_->u.key_tt; \
721 checkliveness(L,io_); }
725 ** About 'alimit': if 'isrealasize(t)' is true, then 'alimit' is the
726 ** real size of 'array'. Otherwise, the real size of 'array' is the
727 ** smallest power of two not smaller than 'alimit' (or zero iff 'alimit'
728 ** is zero); 'alimit' is then used as a hint for #t.
731 #define BITRAS (1 << 7)
732 #define isrealasize(t) (!((t)->flags & BITRAS))
733 #define setrealasize(t) ((t)->flags &= cast_byte(~BITRAS))
734 #define setnorealasize(t) ((t)->flags |= BITRAS)
737 typedef struct Table
{
739 lu_byte flags
; /* 1<<p means tagmethod(p) is not present */
740 lu_byte lsizenode
; /* log2 of size of 'node' array */
741 unsigned int alimit
; /* "limit" of 'array' array */
742 TValue
*array
; /* array part */
744 Node
*lastfree
; /* any free position is before this position */
745 struct Table
*metatable
;
751 ** Macros to manipulate keys inserted in nodes
753 #define keytt(node) ((node)->u.key_tt)
754 #define keyval(node) ((node)->u.key_val)
756 #define keyisnil(node) (keytt(node) == LUA_TNIL)
757 #define keyisinteger(node) (keytt(node) == LUA_VNUMINT)
758 #define keyival(node) (keyval(node).i)
759 #define keyisshrstr(node) (keytt(node) == ctb(LUA_VSHRSTR))
760 #define keystrval(node) (gco2ts(keyval(node).gc))
762 #define setnilkey(node) (keytt(node) = LUA_TNIL)
764 #define keyiscollectable(n) (keytt(n) & BIT_ISCOLLECTABLE)
766 #define gckey(n) (keyval(n).gc)
767 #define gckeyN(n) (keyiscollectable(n) ? gckey(n) : NULL)
771 ** Dead keys in tables have the tag DEADKEY but keep their original
772 ** gcvalue. This distinguishes them from regular keys but allows them to
773 ** be found when searched in a special way. ('next' needs that to find
774 ** keys removed from a table during a traversal.)
776 #define setdeadkey(node) (keytt(node) = LUA_TDEADKEY)
777 #define keyisdead(node) (keytt(node) == LUA_TDEADKEY)
779 /* }================================================================== */
784 ** 'module' operation for hashing (size is always a power of 2)
786 #define lmod(s,size) \
787 (check_exp((size&(size-1))==0, (cast_int((s) & ((size)-1)))))
790 #define twoto(x) (1<<(x))
791 #define sizenode(t) (twoto((t)->lsizenode))
794 /* size of buffer for 'luaO_utf8esc' function */
797 LUAI_FUNC
int luaO_utf8esc(char *buff
, unsigned long x
);
798 LUAI_FUNC
int luaO_ceillog2(unsigned int x
);
799 LUAI_FUNC
int luaO_rawarith(lua_State
*L
, int op
, const TValue
*p1
,
800 const TValue
*p2
, TValue
*res
);
801 LUAI_FUNC
void luaO_arith(lua_State
*L
, int op
, const TValue
*p1
,
802 const TValue
*p2
, StkId res
);
803 LUAI_FUNC
size_t luaO_str2num(const char *s
, TValue
*o
);
804 LUAI_FUNC
int luaO_hexavalue(int c
);
805 LUAI_FUNC
void luaO_tostring(lua_State
*L
, TValue
*obj
);
806 LUAI_FUNC
const char *luaO_pushvfstring(lua_State
*L
, const char *fmt
,
808 LUAI_FUNC
const char *luaO_pushfstring(lua_State
*L
, const char *fmt
, ...);
809 LUAI_FUNC
void luaO_chunkid(char *out
, const char *source
, size_t srclen
);