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