2 ** $Id: lobject.h,v 2.71.1.2 2014/05/07 14:14:58 roberto Exp $
3 ** Type definitions for Lua objects
4 ** See Copyright Notice in lua.h
12 #include <sys/zfs_context.h>
19 ** Extra tags for non-values
21 #define LUA_TPROTO LUA_NUMTAGS
22 #define LUA_TUPVAL (LUA_NUMTAGS+1)
23 #define LUA_TDEADKEY (LUA_NUMTAGS+2)
26 ** number of all possible tags (including LUA_TNONE but excluding DEADKEY)
28 #define LUA_TOTALTAGS (LUA_TUPVAL+2)
32 ** tags for Tagged Values have the following use of bits:
33 ** bits 0-3: actual tag (a LUA_T* value)
34 ** bits 4-5: variant bits
35 ** bit 6: whether value is collectable
38 #define VARBITS (3 << 4)
42 ** LUA_TFUNCTION variants:
44 ** 1 - light C function
45 ** 2 - regular C function (closure)
48 /* Variant tags for functions */
49 #define LUA_TLCL (LUA_TFUNCTION | (0 << 4)) /* Lua closure */
50 #define LUA_TLCF (LUA_TFUNCTION | (1 << 4)) /* light C function */
51 #define LUA_TCCL (LUA_TFUNCTION | (2 << 4)) /* C closure */
54 /* Variant tags for strings */
55 #define LUA_TSHRSTR (LUA_TSTRING | (0 << 4)) /* short strings */
56 #define LUA_TLNGSTR (LUA_TSTRING | (1 << 4)) /* long strings */
59 /* Bit mark for collectable types */
60 #define BIT_ISCOLLECTABLE (1 << 6)
62 /* mark a tag as collectable */
63 #define ctb(t) ((t) | BIT_ISCOLLECTABLE)
67 ** Union of all collectable objects
69 typedef union GCObject GCObject
;
73 ** Common Header for all collectable objects (in macro form, to be
74 ** included in other objects)
76 #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
80 ** Common header in struct form
82 typedef struct GCheader
{
89 ** Union of all Lua values
91 typedef union Value Value
;
94 #define numfield lua_Number n; /* numbers */
99 ** Tagged Values. This is the basic representation of values in Lua,
100 ** an actual value plus a tag with its type.
103 #define TValuefields Value value_; int tt_
105 typedef struct lua_TValue TValue
;
108 /* macro defining a nil value */
109 #define NILCONSTANT {NULL}, LUA_TNIL
112 #define val_(o) ((o)->value_)
113 #define num_(o) (val_(o).n)
116 /* raw type tag of a TValue */
117 #define rttype(o) ((o)->tt_)
119 /* tag with no variants (bits 0-3) */
120 #define novariant(x) ((x) & 0x0F)
122 /* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */
123 #define ttype(o) (rttype(o) & 0x3F)
125 /* type tag of a TValue with no variants (bits 0-3) */
126 #define ttypenv(o) (novariant(rttype(o)))
129 /* Macros to test type */
130 #define checktag(o,t) (rttype(o) == (t))
131 #define checktype(o,t) (ttypenv(o) == (t))
132 #define ttisnumber(o) checktag((o), LUA_TNUMBER)
133 #define ttisnil(o) checktag((o), LUA_TNIL)
134 #define ttisboolean(o) checktag((o), LUA_TBOOLEAN)
135 #define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA)
136 #define ttisstring(o) checktype((o), LUA_TSTRING)
137 #define ttisshrstring(o) checktag((o), ctb(LUA_TSHRSTR))
138 #define ttislngstring(o) checktag((o), ctb(LUA_TLNGSTR))
139 #define ttistable(o) checktag((o), ctb(LUA_TTABLE))
140 #define ttisfunction(o) checktype(o, LUA_TFUNCTION)
141 #define ttisclosure(o) ((rttype(o) & 0x1F) == LUA_TFUNCTION)
142 #define ttisCclosure(o) checktag((o), ctb(LUA_TCCL))
143 #define ttisLclosure(o) checktag((o), ctb(LUA_TLCL))
144 #define ttislcf(o) checktag((o), LUA_TLCF)
145 #define ttisuserdata(o) checktag((o), ctb(LUA_TUSERDATA))
146 #define ttisthread(o) checktag((o), ctb(LUA_TTHREAD))
147 #define ttisdeadkey(o) checktag((o), LUA_TDEADKEY)
149 #define ttisequal(o1,o2) (rttype(o1) == rttype(o2))
151 /* Macros to access values */
152 #define nvalue(o) check_exp(ttisnumber(o), num_(o))
153 #define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
154 #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
155 #define rawtsvalue(o) check_exp(ttisstring(o), &val_(o).gc->ts)
156 #define tsvalue(o) (&rawtsvalue(o)->tsv)
157 #define rawuvalue(o) check_exp(ttisuserdata(o), &val_(o).gc->u)
158 #define uvalue(o) (&rawuvalue(o)->uv)
159 #define clvalue(o) check_exp(ttisclosure(o), &val_(o).gc->cl)
160 #define clLvalue(o) check_exp(ttisLclosure(o), &val_(o).gc->cl.l)
161 #define clCvalue(o) check_exp(ttisCclosure(o), &val_(o).gc->cl.c)
162 #define fvalue(o) check_exp(ttislcf(o), val_(o).f)
163 #define hvalue(o) check_exp(ttistable(o), &val_(o).gc->h)
164 #define bvalue(o) check_exp(ttisboolean(o), val_(o).b)
165 #define thvalue(o) check_exp(ttisthread(o), &val_(o).gc->th)
166 /* a dead value may get the 'gc' field, but cannot access its contents */
167 #define deadvalue(o) check_exp(ttisdeadkey(o), cast(void *, val_(o).gc))
169 #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
172 #define iscollectable(o) (rttype(o) & BIT_ISCOLLECTABLE)
175 /* Macros for internal tests */
176 #define righttt(obj) (ttype(obj) == gcvalue(obj)->gch.tt)
178 #define checkliveness(g,obj) \
179 lua_longassert(!iscollectable(obj) || \
180 (righttt(obj) && !isdead(g,gcvalue(obj))))
183 /* Macros to set values */
184 #define settt_(o,t) ((o)->tt_=(t))
186 #define setnvalue(obj,x) \
187 { TValue *io=(obj); num_(io)=(x); settt_(io, LUA_TNUMBER); }
189 #define setnilvalue(obj) settt_(obj, LUA_TNIL)
191 #define setfvalue(obj,x) \
192 { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); }
194 #define setpvalue(obj,x) \
195 { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); }
197 #define setbvalue(obj,x) \
198 { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); }
200 #define setgcovalue(L,obj,x) \
201 { TValue *io=(obj); GCObject *i_g=(x); \
202 val_(io).gc=i_g; settt_(io, ctb(gch(i_g)->tt)); }
204 #define setsvalue(L,obj,x) \
205 { TValue *io=(obj); \
207 val_(io).gc=cast(GCObject *, x_); settt_(io, ctb(x_->tsv.tt)); \
208 checkliveness(G(L),io); }
210 #define setuvalue(L,obj,x) \
211 { TValue *io=(obj); \
212 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TUSERDATA)); \
213 checkliveness(G(L),io); }
215 #define setthvalue(L,obj,x) \
216 { TValue *io=(obj); \
217 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTHREAD)); \
218 checkliveness(G(L),io); }
220 #define setclLvalue(L,obj,x) \
221 { TValue *io=(obj); \
222 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TLCL)); \
223 checkliveness(G(L),io); }
225 #define setclCvalue(L,obj,x) \
226 { TValue *io=(obj); \
227 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TCCL)); \
228 checkliveness(G(L),io); }
230 #define sethvalue(L,obj,x) \
231 { TValue *io=(obj); \
232 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTABLE)); \
233 checkliveness(G(L),io); }
235 #define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY)
239 #define setobj(L,obj1,obj2) \
240 { const TValue *io2=(obj2); TValue *io1=(obj1); \
241 io1->value_ = io2->value_; io1->tt_ = io2->tt_; \
242 checkliveness(G(L),io1); }
246 ** different types of assignments, according to destination
249 /* from stack to (same) stack */
250 #define setobjs2s setobj
251 /* to stack (not from same stack) */
252 #define setobj2s setobj
253 #define setsvalue2s setsvalue
254 #define sethvalue2s sethvalue
255 #define setptvalue2s setptvalue
256 /* from table to same table */
257 #define setobjt2t setobj
259 #define setobj2t setobj
261 #define setobj2n setobj
262 #define setsvalue2n setsvalue
265 /* check whether a number is valid (useful only for NaN trick) */
266 #define luai_checknum(L,o,c) { /* empty */ }
270 ** {======================================================
272 ** =======================================================
274 #if defined(LUA_NANTRICK)
277 ** numbers are represented in the 'd_' field. All other values have the
278 ** value (NNMARK | tag) in 'tt__'. A number with such pattern would be
279 ** a "signaled NaN", which is never generated by regular operations by
280 ** the CPU (nor by 'strtod')
283 /* allows for external implementation for part of the trick */
284 #if !defined(NNMARK) /* { */
287 #if !defined(LUA_IEEEENDIAN)
288 #error option 'LUA_NANTRICK' needs 'LUA_IEEEENDIAN'
292 #define NNMARK 0x7FF7A500
293 #define NNMASK 0x7FFFFF00
298 #if (LUA_IEEEENDIAN == 0) /* { */
301 #define TValuefields \
302 union { struct { Value v__; int tt__; } i; double d__; } u
303 #define NILCONSTANT {{{NULL}, tag2tt(LUA_TNIL)}}
304 /* field-access macros */
305 #define v_(o) ((o)->u.i.v__)
306 #define d_(o) ((o)->u.d__)
307 #define tt_(o) ((o)->u.i.tt__)
312 #define TValuefields \
313 union { struct { int tt__; Value v__; } i; double d__; } u
314 #define NILCONSTANT {{tag2tt(LUA_TNIL), {NULL}}}
315 /* field-access macros */
316 #define v_(o) ((o)->u.i.v__)
317 #define d_(o) ((o)->u.d__)
318 #define tt_(o) ((o)->u.i.tt__)
325 /* correspondence with standard representation */
327 #define val_(o) v_(o)
329 #define num_(o) d_(o)
333 #define numfield /* no such field; numbers are the entire struct */
335 /* basic check to distinguish numbers from non-numbers */
337 #define ttisnumber(o) ((tt_(o) & NNMASK) != NNMARK)
339 #define tag2tt(t) (NNMARK | (t))
342 #define rttype(o) (ttisnumber(o) ? LUA_TNUMBER : tt_(o) & 0xff)
345 #define settt_(o,t) (tt_(o) = tag2tt(t))
348 #define setnvalue(obj,x) \
349 { TValue *io_=(obj); num_(io_)=(x); lua_assert(ttisnumber(io_)); }
352 #define setobj(L,obj1,obj2) \
353 { const TValue *o2_=(obj2); TValue *o1_=(obj1); \
355 checkliveness(G(L),o1_); }
359 ** these redefinitions are not mandatory, but these forms are more efficient
364 #define checktag(o,t) (tt_(o) == tag2tt(t))
365 #define checktype(o,t) (ctb(tt_(o) | VARBITS) == ctb(tag2tt(t) | VARBITS))
368 #define ttisequal(o1,o2) \
369 (ttisnumber(o1) ? ttisnumber(o2) : (tt_(o1) == tt_(o2)))
373 #define luai_checknum(L,o,c) { if (!ttisnumber(o)) c; }
376 /* }====================================================== */
381 ** {======================================================
382 ** types and prototypes
383 ** =======================================================
388 GCObject
*gc
; /* collectable objects */
389 void *p
; /* light userdata */
390 int b
; /* booleans */
391 lua_CFunction f
; /* light C functions */
392 numfield
/* numbers */
401 typedef TValue
*StkId
; /* index to stack elements */
407 ** Header for string value; string bytes follow the end of this structure
409 typedef union TString
{
410 L_Umaxalign dummy
; /* ensures maximum alignment for strings */
413 lu_byte extra
; /* reserved words for short strings; "has hash" for longs */
415 size_t len
; /* number of characters in string */
420 /* get the actual string (array of bytes) from a TString */
421 #define getstr(ts) cast(const char *, (ts) + 1)
423 /* get the actual string (array of bytes) from a Lua value */
424 #define svalue(o) getstr(rawtsvalue(o))
428 ** Header for userdata; memory area follows the end of this structure
430 typedef union Udata
{
431 L_Umaxalign dummy
; /* ensures maximum alignment for `local' udata */
434 struct Table
*metatable
;
436 size_t len
; /* number of bytes */
443 ** Description of an upvalue for function prototypes
445 typedef struct Upvaldesc
{
446 TString
*name
; /* upvalue name (for debug information) */
447 lu_byte instack
; /* whether it is in stack */
448 lu_byte idx
; /* index of upvalue (in stack or in outer function's list) */
453 ** Description of a local variable for function prototypes
454 ** (used for debug information)
456 typedef struct LocVar
{
458 int startpc
; /* first point where variable is active */
459 int endpc
; /* first point where variable is dead */
464 ** Function Prototypes
466 typedef struct Proto
{
468 TValue
*k
; /* constants used by the function */
470 struct Proto
**p
; /* functions defined inside the function */
471 int *lineinfo
; /* map from opcodes to source lines (debug information) */
472 LocVar
*locvars
; /* information about local variables (debug information) */
473 Upvaldesc
*upvalues
; /* upvalue information */
474 union Closure
*cache
; /* last created closure with this prototype */
475 TString
*source
; /* used for debug information */
476 int sizeupvalues
; /* size of 'upvalues' */
477 int sizek
; /* size of `k' */
480 int sizep
; /* size of `p' */
485 lu_byte numparams
; /* number of fixed parameters */
487 lu_byte maxstacksize
; /* maximum stack used by this function */
495 typedef struct UpVal
{
497 TValue
*v
; /* points to stack or to its own value */
499 TValue value
; /* the value (when closed) */
500 struct { /* double linked list (when open) */
512 #define ClosureHeader \
513 CommonHeader; lu_byte nupvalues; GCObject *gclist
515 typedef struct CClosure
{
518 TValue upvalue
[1]; /* list of upvalues */
522 typedef struct LClosure
{
525 UpVal
*upvals
[1]; /* list of upvalues */
529 typedef union Closure
{
535 #define isLfunction(o) ttisLclosure(o)
537 #define getproto(o) (clLvalue(o)->p)
547 struct Node
*next
; /* for chaining */
553 typedef struct Node
{
559 typedef struct Table
{
561 lu_byte flags
; /* 1<<p means tagmethod(p) is not present */
562 lu_byte lsizenode
; /* log2 of size of `node' array */
563 int sizearray
; /* size of `array' array */
564 TValue
*array
; /* array part */
566 Node
*lastfree
; /* any free position is before this position */
567 struct Table
*metatable
;
574 ** `module' operation for hashing (size is always a power of 2)
576 #define lmod(s,size) \
577 (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
580 #define twoto(x) (1<<(x))
581 #define sizenode(t) (twoto((t)->lsizenode))
585 ** (address of) a fixed nil value
587 #define luaO_nilobject (&luaO_nilobject_)
590 LUAI_DDEC
const TValue luaO_nilobject_
;
593 LUAI_FUNC
int luaO_int2fb (unsigned int x
);
594 LUAI_FUNC
int luaO_fb2int (int x
);
595 LUAI_FUNC
int luaO_ceillog2 (unsigned int x
);
596 LUAI_FUNC lua_Number
luaO_arith (int op
, lua_Number v1
, lua_Number v2
);
597 LUAI_FUNC
int luaO_str2d (const char *s
, size_t len
, lua_Number
*result
);
598 LUAI_FUNC
int luaO_hexavalue (int c
);
599 LUAI_FUNC
const char *luaO_pushvfstring (lua_State
*L
, const char *fmt
,
601 LUAI_FUNC
const char *luaO_pushfstring (lua_State
*L
, const char *fmt
, ...);
602 LUAI_FUNC
void luaO_chunkid (char *out
, const char *source
, size_t len
);