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