xref: /minix3/external/mit/lua/dist/src/lobject.h (revision b5e2faaaaf60a8b9a02f8d72f64caa56a87eb312)
1 /*	$NetBSD: lobject.h,v 1.1.1.2 2012/03/15 00:08:07 alnsn Exp $	*/
2 
3 /*
4 ** $Id: lobject.h,v 1.1.1.2 2012/03/15 00:08:07 alnsn Exp $
5 ** Type definitions for Lua objects
6 ** See Copyright Notice in lua.h
7 */
8 
9 
10 #ifndef lobject_h
11 #define lobject_h
12 
13 
14 #include <stdarg.h>
15 
16 
17 #include "llimits.h"
18 #include "lua.h"
19 
20 
21 /* tags for values visible from Lua */
22 #define LAST_TAG	LUA_TTHREAD
23 
24 #define NUM_TAGS	(LAST_TAG+1)
25 
26 
27 /*
28 ** Extra tags for non-values
29 */
30 #define LUA_TPROTO	(LAST_TAG+1)
31 #define LUA_TUPVAL	(LAST_TAG+2)
32 #define LUA_TDEADKEY	(LAST_TAG+3)
33 
34 
35 /*
36 ** Union of all collectable objects
37 */
38 typedef union GCObject GCObject;
39 
40 
41 /*
42 ** Common Header for all collectable objects (in macro form, to be
43 ** included in other objects)
44 */
45 #define CommonHeader	GCObject *next; lu_byte tt; lu_byte marked
46 
47 
48 /*
49 ** Common header in struct form
50 */
51 typedef struct GCheader {
52   CommonHeader;
53 } GCheader;
54 
55 
56 
57 
58 /*
59 ** Union of all Lua values
60 */
61 typedef union {
62   GCObject *gc;
63   void *p;
64   lua_Number n;
65   int b;
66 } Value;
67 
68 
69 /*
70 ** Tagged Values
71 */
72 
73 #define TValuefields	Value value; int tt
74 
75 typedef struct lua_TValue {
76   TValuefields;
77 } TValue;
78 
79 
80 /* Macros to test type */
81 #define ttisnil(o)	(ttype(o) == LUA_TNIL)
82 #define ttisnumber(o)	(ttype(o) == LUA_TNUMBER)
83 #define ttisstring(o)	(ttype(o) == LUA_TSTRING)
84 #define ttistable(o)	(ttype(o) == LUA_TTABLE)
85 #define ttisfunction(o)	(ttype(o) == LUA_TFUNCTION)
86 #define ttisboolean(o)	(ttype(o) == LUA_TBOOLEAN)
87 #define ttisuserdata(o)	(ttype(o) == LUA_TUSERDATA)
88 #define ttisthread(o)	(ttype(o) == LUA_TTHREAD)
89 #define ttislightuserdata(o)	(ttype(o) == LUA_TLIGHTUSERDATA)
90 
91 /* Macros to access values */
92 #define ttype(o)	((o)->tt)
93 #define gcvalue(o)	check_exp(iscollectable(o), (o)->value.gc)
94 #define pvalue(o)	check_exp(ttislightuserdata(o), (o)->value.p)
95 #define nvalue(o)	check_exp(ttisnumber(o), (o)->value.n)
96 #define rawtsvalue(o)	check_exp(ttisstring(o), &(o)->value.gc->ts)
97 #define tsvalue(o)	(&rawtsvalue(o)->tsv)
98 #define rawuvalue(o)	check_exp(ttisuserdata(o), &(o)->value.gc->u)
99 #define uvalue(o)	(&rawuvalue(o)->uv)
100 #define clvalue(o)	check_exp(ttisfunction(o), &(o)->value.gc->cl)
101 #define hvalue(o)	check_exp(ttistable(o), &(o)->value.gc->h)
102 #define bvalue(o)	check_exp(ttisboolean(o), (o)->value.b)
103 #define thvalue(o)	check_exp(ttisthread(o), &(o)->value.gc->th)
104 
105 #define l_isfalse(o)	(ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
106 
107 /*
108 ** for internal debug only
109 */
110 #define checkconsistency(obj) \
111   lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt))
112 
113 #define checkliveness(g,obj) \
114   lua_assert(!iscollectable(obj) || \
115   ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc)))
116 
117 
118 /* Macros to set values */
119 #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
120 
121 #define setnvalue(obj,x) \
122   { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; }
123 
124 #define setpvalue(obj,x) \
125   { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; }
126 
127 #define setbvalue(obj,x) \
128   { TValue *i_o=(obj); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; }
129 
130 #define setsvalue(L,obj,x) \
131   { TValue *i_o=(obj); \
132     i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TSTRING; \
133     checkliveness(G(L),i_o); }
134 
135 #define setuvalue(L,obj,x) \
136   { TValue *i_o=(obj); \
137     i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TUSERDATA; \
138     checkliveness(G(L),i_o); }
139 
140 #define setthvalue(L,obj,x) \
141   { TValue *i_o=(obj); \
142     i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTHREAD; \
143     checkliveness(G(L),i_o); }
144 
145 #define setclvalue(L,obj,x) \
146   { TValue *i_o=(obj); \
147     i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TFUNCTION; \
148     checkliveness(G(L),i_o); }
149 
150 #define sethvalue(L,obj,x) \
151   { TValue *i_o=(obj); \
152     i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTABLE; \
153     checkliveness(G(L),i_o); }
154 
155 #define setptvalue(L,obj,x) \
156   { TValue *i_o=(obj); \
157     i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \
158     checkliveness(G(L),i_o); }
159 
160 
161 
162 
163 #define setobj(L,obj1,obj2) \
164   { const TValue *o2=(obj2); TValue *o1=(obj1); \
165     o1->value = o2->value; o1->tt=o2->tt; \
166     checkliveness(G(L),o1); }
167 
168 
169 /*
170 ** different types of sets, according to destination
171 */
172 
173 /* from stack to (same) stack */
174 #define setobjs2s	setobj
175 /* to stack (not from same stack) */
176 #define setobj2s	setobj
177 #define setsvalue2s	setsvalue
178 #define sethvalue2s	sethvalue
179 #define setptvalue2s	setptvalue
180 /* from table to same table */
181 #define setobjt2t	setobj
182 /* to table */
183 #define setobj2t	setobj
184 /* to new object */
185 #define setobj2n	setobj
186 #define setsvalue2n	setsvalue
187 
188 #define setttype(obj, tt) (ttype(obj) = (tt))
189 
190 
191 #define iscollectable(o)	(ttype(o) >= LUA_TSTRING)
192 
193 
194 
195 typedef TValue *StkId;  /* index to stack elements */
196 
197 
198 /*
199 ** String headers for string table
200 */
201 typedef union TString {
202   L_Umaxalign dummy;  /* ensures maximum alignment for strings */
203   struct {
204     CommonHeader;
205     lu_byte reserved;
206     unsigned int hash;
207     size_t len;
208   } tsv;
209 } TString;
210 
211 
212 #define getstr(ts)	cast(const char *, (ts) + 1)
213 #define svalue(o)       getstr(rawtsvalue(o))
214 
215 
216 
217 typedef union Udata {
218   L_Umaxalign dummy;  /* ensures maximum alignment for `local' udata */
219   struct {
220     CommonHeader;
221     struct Table *metatable;
222     struct Table *env;
223     size_t len;
224   } uv;
225 } Udata;
226 
227 
228 
229 
230 /*
231 ** Function Prototypes
232 */
233 typedef struct Proto {
234   CommonHeader;
235   TValue *k;  /* constants used by the function */
236   Instruction *code;
237   struct Proto **p;  /* functions defined inside the function */
238   int *lineinfo;  /* map from opcodes to source lines */
239   struct LocVar *locvars;  /* information about local variables */
240   TString **upvalues;  /* upvalue names */
241   TString  *source;
242   int sizeupvalues;
243   int sizek;  /* size of `k' */
244   int sizecode;
245   int sizelineinfo;
246   int sizep;  /* size of `p' */
247   int sizelocvars;
248   int linedefined;
249   int lastlinedefined;
250   GCObject *gclist;
251   lu_byte nups;  /* number of upvalues */
252   lu_byte numparams;
253   lu_byte is_vararg;
254   lu_byte maxstacksize;
255 } Proto;
256 
257 
258 /* masks for new-style vararg */
259 #define VARARG_HASARG		1
260 #define VARARG_ISVARARG		2
261 #define VARARG_NEEDSARG		4
262 
263 
264 typedef struct LocVar {
265   TString *varname;
266   int startpc;  /* first point where variable is active */
267   int endpc;    /* first point where variable is dead */
268 } LocVar;
269 
270 
271 
272 /*
273 ** Upvalues
274 */
275 
276 typedef struct UpVal {
277   CommonHeader;
278   TValue *v;  /* points to stack or to its own value */
279   union {
280     TValue value;  /* the value (when closed) */
281     struct {  /* double linked list (when open) */
282       struct UpVal *prev;
283       struct UpVal *next;
284     } l;
285   } u;
286 } UpVal;
287 
288 
289 /*
290 ** Closures
291 */
292 
293 #define ClosureHeader \
294 	CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \
295 	struct Table *env
296 
297 typedef struct CClosure {
298   ClosureHeader;
299   lua_CFunction f;
300   TValue upvalue[1];
301 } CClosure;
302 
303 
304 typedef struct LClosure {
305   ClosureHeader;
306   struct Proto *p;
307   UpVal *upvals[1];
308 } LClosure;
309 
310 
311 typedef union Closure {
312   CClosure c;
313   LClosure l;
314 } Closure;
315 
316 
317 #define iscfunction(o)	(ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)
318 #define isLfunction(o)	(ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)
319 
320 
321 /*
322 ** Tables
323 */
324 
325 typedef union TKey {
326   struct {
327     TValuefields;
328     struct Node *next;  /* for chaining */
329   } nk;
330   TValue tvk;
331 } TKey;
332 
333 
334 typedef struct Node {
335   TValue i_val;
336   TKey i_key;
337 } Node;
338 
339 
340 typedef struct Table {
341   CommonHeader;
342   lu_byte flags;  /* 1<<p means tagmethod(p) is not present */
343   lu_byte lsizenode;  /* log2 of size of `node' array */
344   struct Table *metatable;
345   TValue *array;  /* array part */
346   Node *node;
347   Node *lastfree;  /* any free position is before this position */
348   GCObject *gclist;
349   int sizearray;  /* size of `array' array */
350 } Table;
351 
352 
353 
354 /*
355 ** `module' operation for hashing (size is always a power of 2)
356 */
357 #define lmod(s,size) \
358 	(check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
359 
360 
361 #define twoto(x)	(1<<(x))
362 #define sizenode(t)	(twoto((t)->lsizenode))
363 
364 
365 #define luaO_nilobject		(&luaO_nilobject_)
366 
367 LUAI_DATA const TValue luaO_nilobject_;
368 
369 #define ceillog2(x)	(luaO_log2((x)-1) + 1)
370 
371 LUAI_FUNC int luaO_log2 (unsigned int x);
372 LUAI_FUNC int luaO_int2fb (unsigned int x);
373 LUAI_FUNC int luaO_fb2int (int x);
374 LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
375 LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result);
376 LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
377                                                        va_list argp);
378 LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
379 LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
380 
381 
382 #endif
383 
384