xref: /freebsd/sys/contrib/openzfs/module/lua/lobject.h (revision eda14cbc264d6969b02f2b1994cef11148e914f1)
1*eda14cbcSMatt Macy /* BEGIN CSTYLED */
2*eda14cbcSMatt Macy /*
3*eda14cbcSMatt Macy ** $Id: lobject.h,v 2.71.1.2 2014/05/07 14:14:58 roberto Exp $
4*eda14cbcSMatt Macy ** Type definitions for Lua objects
5*eda14cbcSMatt Macy ** See Copyright Notice in lua.h
6*eda14cbcSMatt Macy */
7*eda14cbcSMatt Macy 
8*eda14cbcSMatt Macy 
9*eda14cbcSMatt Macy #ifndef lobject_h
10*eda14cbcSMatt Macy #define lobject_h
11*eda14cbcSMatt Macy 
12*eda14cbcSMatt Macy 
13*eda14cbcSMatt Macy #include "llimits.h"
14*eda14cbcSMatt Macy #include <sys/lua/lua.h>
15*eda14cbcSMatt Macy 
16*eda14cbcSMatt Macy 
17*eda14cbcSMatt Macy /*
18*eda14cbcSMatt Macy ** Extra tags for non-values
19*eda14cbcSMatt Macy */
20*eda14cbcSMatt Macy #define LUA_TPROTO	LUA_NUMTAGS
21*eda14cbcSMatt Macy #define LUA_TUPVAL	(LUA_NUMTAGS+1)
22*eda14cbcSMatt Macy #define LUA_TDEADKEY	(LUA_NUMTAGS+2)
23*eda14cbcSMatt Macy 
24*eda14cbcSMatt Macy /*
25*eda14cbcSMatt Macy ** number of all possible tags (including LUA_TNONE but excluding DEADKEY)
26*eda14cbcSMatt Macy */
27*eda14cbcSMatt Macy #define LUA_TOTALTAGS	(LUA_TUPVAL+2)
28*eda14cbcSMatt Macy 
29*eda14cbcSMatt Macy 
30*eda14cbcSMatt Macy /*
31*eda14cbcSMatt Macy ** tags for Tagged Values have the following use of bits:
32*eda14cbcSMatt Macy ** bits 0-3: actual tag (a LUA_T* value)
33*eda14cbcSMatt Macy ** bits 4-5: variant bits
34*eda14cbcSMatt Macy ** bit 6: whether value is collectable
35*eda14cbcSMatt Macy */
36*eda14cbcSMatt Macy 
37*eda14cbcSMatt Macy #define VARBITS		(3 << 4)
38*eda14cbcSMatt Macy 
39*eda14cbcSMatt Macy 
40*eda14cbcSMatt Macy /*
41*eda14cbcSMatt Macy ** LUA_TFUNCTION variants:
42*eda14cbcSMatt Macy ** 0 - Lua function
43*eda14cbcSMatt Macy ** 1 - light C function
44*eda14cbcSMatt Macy ** 2 - regular C function (closure)
45*eda14cbcSMatt Macy */
46*eda14cbcSMatt Macy 
47*eda14cbcSMatt Macy /* Variant tags for functions */
48*eda14cbcSMatt Macy #define LUA_TLCL	(LUA_TFUNCTION | (0 << 4))  /* Lua closure */
49*eda14cbcSMatt Macy #define LUA_TLCF	(LUA_TFUNCTION | (1 << 4))  /* light C function */
50*eda14cbcSMatt Macy #define LUA_TCCL	(LUA_TFUNCTION | (2 << 4))  /* C closure */
51*eda14cbcSMatt Macy 
52*eda14cbcSMatt Macy 
53*eda14cbcSMatt Macy /* Variant tags for strings */
54*eda14cbcSMatt Macy #define LUA_TSHRSTR	(LUA_TSTRING | (0 << 4))  /* short strings */
55*eda14cbcSMatt Macy #define LUA_TLNGSTR	(LUA_TSTRING | (1 << 4))  /* long strings */
56*eda14cbcSMatt Macy 
57*eda14cbcSMatt Macy 
58*eda14cbcSMatt Macy /* Bit mark for collectable types */
59*eda14cbcSMatt Macy #define BIT_ISCOLLECTABLE	(1 << 6)
60*eda14cbcSMatt Macy 
61*eda14cbcSMatt Macy /* mark a tag as collectable */
62*eda14cbcSMatt Macy #define ctb(t)			((t) | BIT_ISCOLLECTABLE)
63*eda14cbcSMatt Macy 
64*eda14cbcSMatt Macy 
65*eda14cbcSMatt Macy /*
66*eda14cbcSMatt Macy ** Union of all collectable objects
67*eda14cbcSMatt Macy */
68*eda14cbcSMatt Macy typedef union GCObject GCObject;
69*eda14cbcSMatt Macy 
70*eda14cbcSMatt Macy 
71*eda14cbcSMatt Macy /*
72*eda14cbcSMatt Macy ** Common Header for all collectable objects (in macro form, to be
73*eda14cbcSMatt Macy ** included in other objects)
74*eda14cbcSMatt Macy */
75*eda14cbcSMatt Macy #define CommonHeader	GCObject *next; lu_byte tt; lu_byte marked
76*eda14cbcSMatt Macy 
77*eda14cbcSMatt Macy 
78*eda14cbcSMatt Macy /*
79*eda14cbcSMatt Macy ** Common header in struct form
80*eda14cbcSMatt Macy */
81*eda14cbcSMatt Macy typedef struct GCheader {
82*eda14cbcSMatt Macy   CommonHeader;
83*eda14cbcSMatt Macy } GCheader;
84*eda14cbcSMatt Macy 
85*eda14cbcSMatt Macy 
86*eda14cbcSMatt Macy 
87*eda14cbcSMatt Macy /*
88*eda14cbcSMatt Macy ** Union of all Lua values
89*eda14cbcSMatt Macy */
90*eda14cbcSMatt Macy typedef union Value Value;
91*eda14cbcSMatt Macy 
92*eda14cbcSMatt Macy 
93*eda14cbcSMatt Macy #define numfield	lua_Number n;    /* numbers */
94*eda14cbcSMatt Macy 
95*eda14cbcSMatt Macy 
96*eda14cbcSMatt Macy 
97*eda14cbcSMatt Macy /*
98*eda14cbcSMatt Macy ** Tagged Values. This is the basic representation of values in Lua,
99*eda14cbcSMatt Macy ** an actual value plus a tag with its type.
100*eda14cbcSMatt Macy */
101*eda14cbcSMatt Macy 
102*eda14cbcSMatt Macy #define TValuefields	Value value_; int tt_
103*eda14cbcSMatt Macy 
104*eda14cbcSMatt Macy typedef struct lua_TValue TValue;
105*eda14cbcSMatt Macy 
106*eda14cbcSMatt Macy 
107*eda14cbcSMatt Macy /* macro defining a nil value */
108*eda14cbcSMatt Macy #define NILCONSTANT	{NULL}, LUA_TNIL
109*eda14cbcSMatt Macy 
110*eda14cbcSMatt Macy 
111*eda14cbcSMatt Macy #define val_(o)		((o)->value_)
112*eda14cbcSMatt Macy #define num_(o)		(val_(o).n)
113*eda14cbcSMatt Macy 
114*eda14cbcSMatt Macy 
115*eda14cbcSMatt Macy /* raw type tag of a TValue */
116*eda14cbcSMatt Macy #define rttype(o)	((o)->tt_)
117*eda14cbcSMatt Macy 
118*eda14cbcSMatt Macy /* tag with no variants (bits 0-3) */
119*eda14cbcSMatt Macy #define novariant(x)	((x) & 0x0F)
120*eda14cbcSMatt Macy 
121*eda14cbcSMatt Macy /* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */
122*eda14cbcSMatt Macy #define ttype(o)	(rttype(o) & 0x3F)
123*eda14cbcSMatt Macy 
124*eda14cbcSMatt Macy /* type tag of a TValue with no variants (bits 0-3) */
125*eda14cbcSMatt Macy #define ttypenv(o)	(novariant(rttype(o)))
126*eda14cbcSMatt Macy 
127*eda14cbcSMatt Macy 
128*eda14cbcSMatt Macy /* Macros to test type */
129*eda14cbcSMatt Macy #define checktag(o,t)		(rttype(o) == (t))
130*eda14cbcSMatt Macy #define checktype(o,t)		(ttypenv(o) == (t))
131*eda14cbcSMatt Macy #define ttisnumber(o)		checktag((o), LUA_TNUMBER)
132*eda14cbcSMatt Macy #define ttisnil(o)		checktag((o), LUA_TNIL)
133*eda14cbcSMatt Macy #define ttisboolean(o)		checktag((o), LUA_TBOOLEAN)
134*eda14cbcSMatt Macy #define ttislightuserdata(o)	checktag((o), LUA_TLIGHTUSERDATA)
135*eda14cbcSMatt Macy #define ttisstring(o)		checktype((o), LUA_TSTRING)
136*eda14cbcSMatt Macy #define ttisshrstring(o)	checktag((o), ctb(LUA_TSHRSTR))
137*eda14cbcSMatt Macy #define ttislngstring(o)	checktag((o), ctb(LUA_TLNGSTR))
138*eda14cbcSMatt Macy #define ttistable(o)		checktag((o), ctb(LUA_TTABLE))
139*eda14cbcSMatt Macy #define ttisfunction(o)		checktype(o, LUA_TFUNCTION)
140*eda14cbcSMatt Macy #define ttisclosure(o)		((rttype(o) & 0x1F) == LUA_TFUNCTION)
141*eda14cbcSMatt Macy #define ttisCclosure(o)		checktag((o), ctb(LUA_TCCL))
142*eda14cbcSMatt Macy #define ttisLclosure(o)		checktag((o), ctb(LUA_TLCL))
143*eda14cbcSMatt Macy #define ttislcf(o)		checktag((o), LUA_TLCF)
144*eda14cbcSMatt Macy #define ttisuserdata(o)		checktag((o), ctb(LUA_TUSERDATA))
145*eda14cbcSMatt Macy #define ttisthread(o)		checktag((o), ctb(LUA_TTHREAD))
146*eda14cbcSMatt Macy #define ttisdeadkey(o)		checktag((o), LUA_TDEADKEY)
147*eda14cbcSMatt Macy 
148*eda14cbcSMatt Macy #define ttisequal(o1,o2)	(rttype(o1) == rttype(o2))
149*eda14cbcSMatt Macy 
150*eda14cbcSMatt Macy /* Macros to access values */
151*eda14cbcSMatt Macy #define nvalue(o)	check_exp(ttisnumber(o), num_(o))
152*eda14cbcSMatt Macy #define gcvalue(o)	check_exp(iscollectable(o), val_(o).gc)
153*eda14cbcSMatt Macy #define pvalue(o)	check_exp(ttislightuserdata(o), val_(o).p)
154*eda14cbcSMatt Macy #define rawtsvalue(o)	check_exp(ttisstring(o), &val_(o).gc->ts)
155*eda14cbcSMatt Macy #define tsvalue(o)	(&rawtsvalue(o)->tsv)
156*eda14cbcSMatt Macy #define rawuvalue(o)	check_exp(ttisuserdata(o), &val_(o).gc->u)
157*eda14cbcSMatt Macy #define uvalue(o)	(&rawuvalue(o)->uv)
158*eda14cbcSMatt Macy #define clvalue(o)	check_exp(ttisclosure(o), &val_(o).gc->cl)
159*eda14cbcSMatt Macy #define clLvalue(o)	check_exp(ttisLclosure(o), &val_(o).gc->cl.l)
160*eda14cbcSMatt Macy #define clCvalue(o)	check_exp(ttisCclosure(o), &val_(o).gc->cl.c)
161*eda14cbcSMatt Macy #define fvalue(o)	check_exp(ttislcf(o), val_(o).f)
162*eda14cbcSMatt Macy #define hvalue(o)	check_exp(ttistable(o), &val_(o).gc->h)
163*eda14cbcSMatt Macy #define bvalue(o)	check_exp(ttisboolean(o), val_(o).b)
164*eda14cbcSMatt Macy #define thvalue(o)	check_exp(ttisthread(o), &val_(o).gc->th)
165*eda14cbcSMatt Macy /* a dead value may get the 'gc' field, but cannot access its contents */
166*eda14cbcSMatt Macy #define deadvalue(o)	check_exp(ttisdeadkey(o), cast(void *, val_(o).gc))
167*eda14cbcSMatt Macy 
168*eda14cbcSMatt Macy #define l_isfalse(o)	(ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
169*eda14cbcSMatt Macy 
170*eda14cbcSMatt Macy 
171*eda14cbcSMatt Macy #define iscollectable(o)	(rttype(o) & BIT_ISCOLLECTABLE)
172*eda14cbcSMatt Macy 
173*eda14cbcSMatt Macy 
174*eda14cbcSMatt Macy /* Macros for internal tests */
175*eda14cbcSMatt Macy #define righttt(obj)		(ttype(obj) == gcvalue(obj)->gch.tt)
176*eda14cbcSMatt Macy 
177*eda14cbcSMatt Macy #define checkliveness(g,obj) \
178*eda14cbcSMatt Macy 	lua_longassert(!iscollectable(obj) || \
179*eda14cbcSMatt Macy 			(righttt(obj) && !isdead(g,gcvalue(obj))))
180*eda14cbcSMatt Macy 
181*eda14cbcSMatt Macy 
182*eda14cbcSMatt Macy /* Macros to set values */
183*eda14cbcSMatt Macy #define settt_(o,t)	((o)->tt_=(t))
184*eda14cbcSMatt Macy 
185*eda14cbcSMatt Macy #define setnvalue(obj,x) \
186*eda14cbcSMatt Macy   { TValue *io=(obj); num_(io)=(x); settt_(io, LUA_TNUMBER); }
187*eda14cbcSMatt Macy 
188*eda14cbcSMatt Macy #define setnilvalue(obj) settt_(obj, LUA_TNIL)
189*eda14cbcSMatt Macy 
190*eda14cbcSMatt Macy #define setfvalue(obj,x) \
191*eda14cbcSMatt Macy   { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); }
192*eda14cbcSMatt Macy 
193*eda14cbcSMatt Macy #define setpvalue(obj,x) \
194*eda14cbcSMatt Macy   { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); }
195*eda14cbcSMatt Macy 
196*eda14cbcSMatt Macy #define setbvalue(obj,x) \
197*eda14cbcSMatt Macy   { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); }
198*eda14cbcSMatt Macy 
199*eda14cbcSMatt Macy #define setgcovalue(L,obj,x) \
200*eda14cbcSMatt Macy   { TValue *io=(obj); GCObject *i_g=(x); \
201*eda14cbcSMatt Macy     val_(io).gc=i_g; settt_(io, ctb(gch(i_g)->tt)); }
202*eda14cbcSMatt Macy 
203*eda14cbcSMatt Macy #define setsvalue(L,obj,x) \
204*eda14cbcSMatt Macy   { TValue *io=(obj); \
205*eda14cbcSMatt Macy     TString *x_ = (x); \
206*eda14cbcSMatt Macy     val_(io).gc=cast(GCObject *, x_); settt_(io, ctb(x_->tsv.tt)); \
207*eda14cbcSMatt Macy     checkliveness(G(L),io); }
208*eda14cbcSMatt Macy 
209*eda14cbcSMatt Macy #define setuvalue(L,obj,x) \
210*eda14cbcSMatt Macy   { TValue *io=(obj); \
211*eda14cbcSMatt Macy     val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TUSERDATA)); \
212*eda14cbcSMatt Macy     checkliveness(G(L),io); }
213*eda14cbcSMatt Macy 
214*eda14cbcSMatt Macy #define setthvalue(L,obj,x) \
215*eda14cbcSMatt Macy   { TValue *io=(obj); \
216*eda14cbcSMatt Macy     val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTHREAD)); \
217*eda14cbcSMatt Macy     checkliveness(G(L),io); }
218*eda14cbcSMatt Macy 
219*eda14cbcSMatt Macy #define setclLvalue(L,obj,x) \
220*eda14cbcSMatt Macy   { TValue *io=(obj); \
221*eda14cbcSMatt Macy     val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TLCL)); \
222*eda14cbcSMatt Macy     checkliveness(G(L),io); }
223*eda14cbcSMatt Macy 
224*eda14cbcSMatt Macy #define setclCvalue(L,obj,x) \
225*eda14cbcSMatt Macy   { TValue *io=(obj); \
226*eda14cbcSMatt Macy     val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TCCL)); \
227*eda14cbcSMatt Macy     checkliveness(G(L),io); }
228*eda14cbcSMatt Macy 
229*eda14cbcSMatt Macy #define sethvalue(L,obj,x) \
230*eda14cbcSMatt Macy   { TValue *io=(obj); \
231*eda14cbcSMatt Macy     val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTABLE)); \
232*eda14cbcSMatt Macy     checkliveness(G(L),io); }
233*eda14cbcSMatt Macy 
234*eda14cbcSMatt Macy #define setdeadvalue(obj)	settt_(obj, LUA_TDEADKEY)
235*eda14cbcSMatt Macy 
236*eda14cbcSMatt Macy 
237*eda14cbcSMatt Macy 
238*eda14cbcSMatt Macy #define setobj(L,obj1,obj2) \
239*eda14cbcSMatt Macy 	{ const TValue *io2=(obj2); TValue *io1=(obj1); \
240*eda14cbcSMatt Macy 	  io1->value_ = io2->value_; io1->tt_ = io2->tt_; \
241*eda14cbcSMatt Macy 	  checkliveness(G(L),io1); }
242*eda14cbcSMatt Macy 
243*eda14cbcSMatt Macy 
244*eda14cbcSMatt Macy /*
245*eda14cbcSMatt Macy ** different types of assignments, according to destination
246*eda14cbcSMatt Macy */
247*eda14cbcSMatt Macy 
248*eda14cbcSMatt Macy /* from stack to (same) stack */
249*eda14cbcSMatt Macy #define setobjs2s	setobj
250*eda14cbcSMatt Macy /* to stack (not from same stack) */
251*eda14cbcSMatt Macy #define setobj2s	setobj
252*eda14cbcSMatt Macy #define setsvalue2s	setsvalue
253*eda14cbcSMatt Macy #define sethvalue2s	sethvalue
254*eda14cbcSMatt Macy #define setptvalue2s	setptvalue
255*eda14cbcSMatt Macy /* from table to same table */
256*eda14cbcSMatt Macy #define setobjt2t	setobj
257*eda14cbcSMatt Macy /* to table */
258*eda14cbcSMatt Macy #define setobj2t	setobj
259*eda14cbcSMatt Macy /* to new object */
260*eda14cbcSMatt Macy #define setobj2n	setobj
261*eda14cbcSMatt Macy #define setsvalue2n	setsvalue
262*eda14cbcSMatt Macy 
263*eda14cbcSMatt Macy 
264*eda14cbcSMatt Macy /* check whether a number is valid (useful only for NaN trick) */
265*eda14cbcSMatt Macy #define luai_checknum(L,o,c)	{ /* empty */ }
266*eda14cbcSMatt Macy 
267*eda14cbcSMatt Macy 
268*eda14cbcSMatt Macy /*
269*eda14cbcSMatt Macy ** {======================================================
270*eda14cbcSMatt Macy ** NaN Trick
271*eda14cbcSMatt Macy ** =======================================================
272*eda14cbcSMatt Macy */
273*eda14cbcSMatt Macy #if defined(LUA_NANTRICK)
274*eda14cbcSMatt Macy 
275*eda14cbcSMatt Macy /*
276*eda14cbcSMatt Macy ** numbers are represented in the 'd_' field. All other values have the
277*eda14cbcSMatt Macy ** value (NNMARK | tag) in 'tt__'. A number with such pattern would be
278*eda14cbcSMatt Macy ** a "signaled NaN", which is never generated by regular operations by
279*eda14cbcSMatt Macy ** the CPU (nor by 'strtod')
280*eda14cbcSMatt Macy */
281*eda14cbcSMatt Macy 
282*eda14cbcSMatt Macy /* allows for external implementation for part of the trick */
283*eda14cbcSMatt Macy #if !defined(NNMARK)	/* { */
284*eda14cbcSMatt Macy 
285*eda14cbcSMatt Macy 
286*eda14cbcSMatt Macy #if !defined(LUA_IEEEENDIAN)
287*eda14cbcSMatt Macy #error option 'LUA_NANTRICK' needs 'LUA_IEEEENDIAN'
288*eda14cbcSMatt Macy #endif
289*eda14cbcSMatt Macy 
290*eda14cbcSMatt Macy 
291*eda14cbcSMatt Macy #define NNMARK		0x7FF7A500
292*eda14cbcSMatt Macy #define NNMASK		0x7FFFFF00
293*eda14cbcSMatt Macy 
294*eda14cbcSMatt Macy #undef TValuefields
295*eda14cbcSMatt Macy #undef NILCONSTANT
296*eda14cbcSMatt Macy 
297*eda14cbcSMatt Macy #if (LUA_IEEEENDIAN == 0)	/* { */
298*eda14cbcSMatt Macy 
299*eda14cbcSMatt Macy /* little endian */
300*eda14cbcSMatt Macy #define TValuefields  \
301*eda14cbcSMatt Macy 	union { struct { Value v__; int tt__; } i; double d__; } u
302*eda14cbcSMatt Macy #define NILCONSTANT	{{{NULL}, tag2tt(LUA_TNIL)}}
303*eda14cbcSMatt Macy /* field-access macros */
304*eda14cbcSMatt Macy #define v_(o)		((o)->u.i.v__)
305*eda14cbcSMatt Macy #define d_(o)		((o)->u.d__)
306*eda14cbcSMatt Macy #define tt_(o)		((o)->u.i.tt__)
307*eda14cbcSMatt Macy 
308*eda14cbcSMatt Macy #else				/* }{ */
309*eda14cbcSMatt Macy 
310*eda14cbcSMatt Macy /* big endian */
311*eda14cbcSMatt Macy #define TValuefields  \
312*eda14cbcSMatt Macy 	union { struct { int tt__; Value v__; } i; double d__; } u
313*eda14cbcSMatt Macy #define NILCONSTANT	{{tag2tt(LUA_TNIL), {NULL}}}
314*eda14cbcSMatt Macy /* field-access macros */
315*eda14cbcSMatt Macy #define v_(o)		((o)->u.i.v__)
316*eda14cbcSMatt Macy #define d_(o)		((o)->u.d__)
317*eda14cbcSMatt Macy #define tt_(o)		((o)->u.i.tt__)
318*eda14cbcSMatt Macy 
319*eda14cbcSMatt Macy #endif				/* } */
320*eda14cbcSMatt Macy 
321*eda14cbcSMatt Macy #endif			/* } */
322*eda14cbcSMatt Macy 
323*eda14cbcSMatt Macy 
324*eda14cbcSMatt Macy /* correspondence with standard representation */
325*eda14cbcSMatt Macy #undef val_
326*eda14cbcSMatt Macy #define val_(o)		v_(o)
327*eda14cbcSMatt Macy #undef num_
328*eda14cbcSMatt Macy #define num_(o)		d_(o)
329*eda14cbcSMatt Macy 
330*eda14cbcSMatt Macy 
331*eda14cbcSMatt Macy #undef numfield
332*eda14cbcSMatt Macy #define numfield	/* no such field; numbers are the entire struct */
333*eda14cbcSMatt Macy 
334*eda14cbcSMatt Macy /* basic check to distinguish numbers from non-numbers */
335*eda14cbcSMatt Macy #undef ttisnumber
336*eda14cbcSMatt Macy #define ttisnumber(o)	((tt_(o) & NNMASK) != NNMARK)
337*eda14cbcSMatt Macy 
338*eda14cbcSMatt Macy #define tag2tt(t)	(NNMARK | (t))
339*eda14cbcSMatt Macy 
340*eda14cbcSMatt Macy #undef rttype
341*eda14cbcSMatt Macy #define rttype(o)	(ttisnumber(o) ? LUA_TNUMBER : tt_(o) & 0xff)
342*eda14cbcSMatt Macy 
343*eda14cbcSMatt Macy #undef settt_
344*eda14cbcSMatt Macy #define settt_(o,t)	(tt_(o) = tag2tt(t))
345*eda14cbcSMatt Macy 
346*eda14cbcSMatt Macy #undef setnvalue
347*eda14cbcSMatt Macy #define setnvalue(obj,x) \
348*eda14cbcSMatt Macy 	{ TValue *io_=(obj); num_(io_)=(x); lua_assert(ttisnumber(io_)); }
349*eda14cbcSMatt Macy 
350*eda14cbcSMatt Macy #undef setobj
351*eda14cbcSMatt Macy #define setobj(L,obj1,obj2) \
352*eda14cbcSMatt Macy 	{ const TValue *o2_=(obj2); TValue *o1_=(obj1); \
353*eda14cbcSMatt Macy 	  o1_->u = o2_->u; \
354*eda14cbcSMatt Macy 	  checkliveness(G(L),o1_); }
355*eda14cbcSMatt Macy 
356*eda14cbcSMatt Macy 
357*eda14cbcSMatt Macy /*
358*eda14cbcSMatt Macy ** these redefinitions are not mandatory, but these forms are more efficient
359*eda14cbcSMatt Macy */
360*eda14cbcSMatt Macy 
361*eda14cbcSMatt Macy #undef checktag
362*eda14cbcSMatt Macy #undef checktype
363*eda14cbcSMatt Macy #define checktag(o,t)	(tt_(o) == tag2tt(t))
364*eda14cbcSMatt Macy #define checktype(o,t)	(ctb(tt_(o) | VARBITS) == ctb(tag2tt(t) | VARBITS))
365*eda14cbcSMatt Macy 
366*eda14cbcSMatt Macy #undef ttisequal
367*eda14cbcSMatt Macy #define ttisequal(o1,o2)  \
368*eda14cbcSMatt Macy 	(ttisnumber(o1) ? ttisnumber(o2) : (tt_(o1) == tt_(o2)))
369*eda14cbcSMatt Macy 
370*eda14cbcSMatt Macy 
371*eda14cbcSMatt Macy #undef luai_checknum
372*eda14cbcSMatt Macy #define luai_checknum(L,o,c)	{ if (!ttisnumber(o)) c; }
373*eda14cbcSMatt Macy 
374*eda14cbcSMatt Macy #endif
375*eda14cbcSMatt Macy /* }====================================================== */
376*eda14cbcSMatt Macy 
377*eda14cbcSMatt Macy 
378*eda14cbcSMatt Macy 
379*eda14cbcSMatt Macy /*
380*eda14cbcSMatt Macy ** {======================================================
381*eda14cbcSMatt Macy ** types and prototypes
382*eda14cbcSMatt Macy ** =======================================================
383*eda14cbcSMatt Macy */
384*eda14cbcSMatt Macy 
385*eda14cbcSMatt Macy 
386*eda14cbcSMatt Macy union Value {
387*eda14cbcSMatt Macy   GCObject *gc;    /* collectable objects */
388*eda14cbcSMatt Macy   void *p;         /* light userdata */
389*eda14cbcSMatt Macy   int b;           /* booleans */
390*eda14cbcSMatt Macy   lua_CFunction f; /* light C functions */
391*eda14cbcSMatt Macy   numfield         /* numbers */
392*eda14cbcSMatt Macy };
393*eda14cbcSMatt Macy 
394*eda14cbcSMatt Macy 
395*eda14cbcSMatt Macy struct lua_TValue {
396*eda14cbcSMatt Macy   TValuefields;
397*eda14cbcSMatt Macy };
398*eda14cbcSMatt Macy 
399*eda14cbcSMatt Macy 
400*eda14cbcSMatt Macy typedef TValue *StkId;  /* index to stack elements */
401*eda14cbcSMatt Macy 
402*eda14cbcSMatt Macy 
403*eda14cbcSMatt Macy 
404*eda14cbcSMatt Macy 
405*eda14cbcSMatt Macy /*
406*eda14cbcSMatt Macy ** Header for string value; string bytes follow the end of this structure
407*eda14cbcSMatt Macy */
408*eda14cbcSMatt Macy typedef union TString {
409*eda14cbcSMatt Macy   L_Umaxalign dummy;  /* ensures maximum alignment for strings */
410*eda14cbcSMatt Macy   struct {
411*eda14cbcSMatt Macy     CommonHeader;
412*eda14cbcSMatt Macy     lu_byte extra;  /* reserved words for short strings; "has hash" for longs */
413*eda14cbcSMatt Macy     unsigned int hash;
414*eda14cbcSMatt Macy     size_t len;  /* number of characters in string */
415*eda14cbcSMatt Macy   } tsv;
416*eda14cbcSMatt Macy } TString;
417*eda14cbcSMatt Macy 
418*eda14cbcSMatt Macy 
419*eda14cbcSMatt Macy /* get the actual string (array of bytes) from a TString */
420*eda14cbcSMatt Macy #define getstr(ts)	cast(const char *, (ts) + 1)
421*eda14cbcSMatt Macy 
422*eda14cbcSMatt Macy /* get the actual string (array of bytes) from a Lua value */
423*eda14cbcSMatt Macy #define svalue(o)       getstr(rawtsvalue(o))
424*eda14cbcSMatt Macy 
425*eda14cbcSMatt Macy 
426*eda14cbcSMatt Macy /*
427*eda14cbcSMatt Macy ** Header for userdata; memory area follows the end of this structure
428*eda14cbcSMatt Macy */
429*eda14cbcSMatt Macy typedef union Udata {
430*eda14cbcSMatt Macy   L_Umaxalign dummy;  /* ensures maximum alignment for `local' udata */
431*eda14cbcSMatt Macy   struct {
432*eda14cbcSMatt Macy     CommonHeader;
433*eda14cbcSMatt Macy     struct Table *metatable;
434*eda14cbcSMatt Macy     struct Table *env;
435*eda14cbcSMatt Macy     size_t len;  /* number of bytes */
436*eda14cbcSMatt Macy   } uv;
437*eda14cbcSMatt Macy } Udata;
438*eda14cbcSMatt Macy 
439*eda14cbcSMatt Macy 
440*eda14cbcSMatt Macy 
441*eda14cbcSMatt Macy /*
442*eda14cbcSMatt Macy ** Description of an upvalue for function prototypes
443*eda14cbcSMatt Macy */
444*eda14cbcSMatt Macy typedef struct Upvaldesc {
445*eda14cbcSMatt Macy   TString *name;  /* upvalue name (for debug information) */
446*eda14cbcSMatt Macy   lu_byte instack;  /* whether it is in stack */
447*eda14cbcSMatt Macy   lu_byte idx;  /* index of upvalue (in stack or in outer function's list) */
448*eda14cbcSMatt Macy } Upvaldesc;
449*eda14cbcSMatt Macy 
450*eda14cbcSMatt Macy 
451*eda14cbcSMatt Macy /*
452*eda14cbcSMatt Macy ** Description of a local variable for function prototypes
453*eda14cbcSMatt Macy ** (used for debug information)
454*eda14cbcSMatt Macy */
455*eda14cbcSMatt Macy typedef struct LocVar {
456*eda14cbcSMatt Macy   TString *varname;
457*eda14cbcSMatt Macy   int startpc;  /* first point where variable is active */
458*eda14cbcSMatt Macy   int endpc;    /* first point where variable is dead */
459*eda14cbcSMatt Macy } LocVar;
460*eda14cbcSMatt Macy 
461*eda14cbcSMatt Macy 
462*eda14cbcSMatt Macy /*
463*eda14cbcSMatt Macy ** Function Prototypes
464*eda14cbcSMatt Macy */
465*eda14cbcSMatt Macy typedef struct Proto {
466*eda14cbcSMatt Macy   CommonHeader;
467*eda14cbcSMatt Macy   TValue *k;  /* constants used by the function */
468*eda14cbcSMatt Macy   Instruction *code;
469*eda14cbcSMatt Macy   struct Proto **p;  /* functions defined inside the function */
470*eda14cbcSMatt Macy   int *lineinfo;  /* map from opcodes to source lines (debug information) */
471*eda14cbcSMatt Macy   LocVar *locvars;  /* information about local variables (debug information) */
472*eda14cbcSMatt Macy   Upvaldesc *upvalues;  /* upvalue information */
473*eda14cbcSMatt Macy   union Closure *cache;  /* last created closure with this prototype */
474*eda14cbcSMatt Macy   TString  *source;  /* used for debug information */
475*eda14cbcSMatt Macy   int sizeupvalues;  /* size of 'upvalues' */
476*eda14cbcSMatt Macy   int sizek;  /* size of `k' */
477*eda14cbcSMatt Macy   int sizecode;
478*eda14cbcSMatt Macy   int sizelineinfo;
479*eda14cbcSMatt Macy   int sizep;  /* size of `p' */
480*eda14cbcSMatt Macy   int sizelocvars;
481*eda14cbcSMatt Macy   int linedefined;
482*eda14cbcSMatt Macy   int lastlinedefined;
483*eda14cbcSMatt Macy   GCObject *gclist;
484*eda14cbcSMatt Macy   lu_byte numparams;  /* number of fixed parameters */
485*eda14cbcSMatt Macy   lu_byte is_vararg;
486*eda14cbcSMatt Macy   lu_byte maxstacksize;  /* maximum stack used by this function */
487*eda14cbcSMatt Macy } Proto;
488*eda14cbcSMatt Macy 
489*eda14cbcSMatt Macy 
490*eda14cbcSMatt Macy 
491*eda14cbcSMatt Macy /*
492*eda14cbcSMatt Macy ** Lua Upvalues
493*eda14cbcSMatt Macy */
494*eda14cbcSMatt Macy typedef struct UpVal {
495*eda14cbcSMatt Macy   CommonHeader;
496*eda14cbcSMatt Macy   TValue *v;  /* points to stack or to its own value */
497*eda14cbcSMatt Macy   union {
498*eda14cbcSMatt Macy     TValue value;  /* the value (when closed) */
499*eda14cbcSMatt Macy     struct {  /* double linked list (when open) */
500*eda14cbcSMatt Macy       struct UpVal *prev;
501*eda14cbcSMatt Macy       struct UpVal *next;
502*eda14cbcSMatt Macy     } l;
503*eda14cbcSMatt Macy   } u;
504*eda14cbcSMatt Macy } UpVal;
505*eda14cbcSMatt Macy 
506*eda14cbcSMatt Macy 
507*eda14cbcSMatt Macy /*
508*eda14cbcSMatt Macy ** Closures
509*eda14cbcSMatt Macy */
510*eda14cbcSMatt Macy 
511*eda14cbcSMatt Macy #define ClosureHeader \
512*eda14cbcSMatt Macy 	CommonHeader; lu_byte nupvalues; GCObject *gclist
513*eda14cbcSMatt Macy 
514*eda14cbcSMatt Macy typedef struct CClosure {
515*eda14cbcSMatt Macy   ClosureHeader;
516*eda14cbcSMatt Macy   lua_CFunction f;
517*eda14cbcSMatt Macy   TValue upvalue[1];  /* list of upvalues */
518*eda14cbcSMatt Macy } CClosure;
519*eda14cbcSMatt Macy 
520*eda14cbcSMatt Macy 
521*eda14cbcSMatt Macy typedef struct LClosure {
522*eda14cbcSMatt Macy   ClosureHeader;
523*eda14cbcSMatt Macy   struct Proto *p;
524*eda14cbcSMatt Macy   UpVal *upvals[1];  /* list of upvalues */
525*eda14cbcSMatt Macy } LClosure;
526*eda14cbcSMatt Macy 
527*eda14cbcSMatt Macy 
528*eda14cbcSMatt Macy typedef union Closure {
529*eda14cbcSMatt Macy   CClosure c;
530*eda14cbcSMatt Macy   LClosure l;
531*eda14cbcSMatt Macy } Closure;
532*eda14cbcSMatt Macy 
533*eda14cbcSMatt Macy 
534*eda14cbcSMatt Macy #define isLfunction(o)	ttisLclosure(o)
535*eda14cbcSMatt Macy 
536*eda14cbcSMatt Macy #define getproto(o)	(clLvalue(o)->p)
537*eda14cbcSMatt Macy 
538*eda14cbcSMatt Macy 
539*eda14cbcSMatt Macy /*
540*eda14cbcSMatt Macy ** Tables
541*eda14cbcSMatt Macy */
542*eda14cbcSMatt Macy 
543*eda14cbcSMatt Macy typedef union TKey {
544*eda14cbcSMatt Macy   struct {
545*eda14cbcSMatt Macy     TValuefields;
546*eda14cbcSMatt Macy     struct Node *next;  /* for chaining */
547*eda14cbcSMatt Macy   } nk;
548*eda14cbcSMatt Macy   TValue tvk;
549*eda14cbcSMatt Macy } TKey;
550*eda14cbcSMatt Macy 
551*eda14cbcSMatt Macy 
552*eda14cbcSMatt Macy typedef struct Node {
553*eda14cbcSMatt Macy   TValue i_val;
554*eda14cbcSMatt Macy   TKey i_key;
555*eda14cbcSMatt Macy } Node;
556*eda14cbcSMatt Macy 
557*eda14cbcSMatt Macy 
558*eda14cbcSMatt Macy typedef struct Table {
559*eda14cbcSMatt Macy   CommonHeader;
560*eda14cbcSMatt Macy   lu_byte flags;  /* 1<<p means tagmethod(p) is not present */
561*eda14cbcSMatt Macy   lu_byte lsizenode;  /* log2 of size of `node' array */
562*eda14cbcSMatt Macy   int sizearray;  /* size of `array' array */
563*eda14cbcSMatt Macy   TValue *array;  /* array part */
564*eda14cbcSMatt Macy   Node *node;
565*eda14cbcSMatt Macy   Node *lastfree;  /* any free position is before this position */
566*eda14cbcSMatt Macy   struct Table *metatable;
567*eda14cbcSMatt Macy   GCObject *gclist;
568*eda14cbcSMatt Macy } Table;
569*eda14cbcSMatt Macy 
570*eda14cbcSMatt Macy 
571*eda14cbcSMatt Macy 
572*eda14cbcSMatt Macy /*
573*eda14cbcSMatt Macy ** `module' operation for hashing (size is always a power of 2)
574*eda14cbcSMatt Macy */
575*eda14cbcSMatt Macy #define lmod(s,size) \
576*eda14cbcSMatt Macy 	(check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
577*eda14cbcSMatt Macy 
578*eda14cbcSMatt Macy 
579*eda14cbcSMatt Macy #define twoto(x)	(1<<(x))
580*eda14cbcSMatt Macy #define sizenode(t)	(twoto((t)->lsizenode))
581*eda14cbcSMatt Macy 
582*eda14cbcSMatt Macy 
583*eda14cbcSMatt Macy /*
584*eda14cbcSMatt Macy ** (address of) a fixed nil value
585*eda14cbcSMatt Macy */
586*eda14cbcSMatt Macy #define luaO_nilobject		(&luaO_nilobject_)
587*eda14cbcSMatt Macy 
588*eda14cbcSMatt Macy 
589*eda14cbcSMatt Macy LUAI_DDEC const TValue luaO_nilobject_;
590*eda14cbcSMatt Macy 
591*eda14cbcSMatt Macy 
592*eda14cbcSMatt Macy LUAI_FUNC int luaO_int2fb (unsigned int x);
593*eda14cbcSMatt Macy LUAI_FUNC int luaO_fb2int (int x);
594*eda14cbcSMatt Macy LUAI_FUNC int luaO_ceillog2 (unsigned int x);
595*eda14cbcSMatt Macy LUAI_FUNC lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2);
596*eda14cbcSMatt Macy LUAI_FUNC int luaO_str2d (const char *s, size_t len, lua_Number *result);
597*eda14cbcSMatt Macy LUAI_FUNC int luaO_hexavalue (int c);
598*eda14cbcSMatt Macy LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
599*eda14cbcSMatt Macy                                                        va_list argp);
600*eda14cbcSMatt Macy LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
601*eda14cbcSMatt Macy LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
602*eda14cbcSMatt Macy 
603*eda14cbcSMatt Macy 
604*eda14cbcSMatt Macy #endif
605*eda14cbcSMatt Macy /* END CSTYLED */
606