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
19 /* tags for values visible from Lua */
20 #if defined(LUA_TINT) && (LUA_TINT > LUA_TTHREAD)
21 # define LAST_TAG LUA_TINT
23 # define LAST_TAG LUA_TTHREAD
26 #define NUM_TAGS (LAST_TAG+1)
30 ** Extra tags for non-values
32 #define LUA_TPROTO (LAST_TAG+1)
33 #define LUA_TUPVAL (LAST_TAG+2)
34 #define LUA_TDEADKEY (LAST_TAG+3)
38 ** Union of all collectable objects
40 typedef union GCObject GCObject;
44 ** Common Header for all collectable objects (in macro form, to be
45 ** included in other objects)
47 #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
51 ** Common header in struct form
53 typedef struct GCheader {
61 ** Union of all Lua values
82 #define TValuefields Value value; int tt
84 typedef struct lua_TValue {
89 /* Macros to test type */
90 #define ttisnil(o) (ttype(o) == LUA_TNIL)
93 # if (LUA_TINT & 0x0f) == LUA_TNUMBER
94 # define ttisnumber(o) ((ttype(o) & 0x0f) == LUA_TNUMBER)
96 # define ttisnumber(o) ((ttype(o) == LUA_TINT) || (ttype(o) == LUA_TNUMBER))
98 # define ttisint(o) (ttype(o) == LUA_TINT)
100 # define ttisnumber(o) (ttype(o) == LUA_TNUMBER)
104 # define ttiscomplex(o) ((ttype(o) == LUA_TNUMBER) && (nvalue_img_fast(o)!=0))
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)
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)
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) )
125 # define ttype_ext(o) ttype(o)
128 /* '_fast' variants are for cases where 'ttype(o)' is known to be LUA_TNUMBER
132 # error "LNUM_COMPLEX only allowed with LNUM_INTxx defined (can be changed)"
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) )
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 )
145 # define nvalue(o) check_exp( ttisnumber(o), (o)->value.n )
146 # define nvalue_fast(o) nvalue(o)
150 # define ivalue(o) check_exp( ttype(o)==LUA_TINT, (o)->value.i )
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)
162 #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
165 ** for internal debug only
167 #define checkconsistency(obj) \
168 lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt))
170 #define checkliveness(g,obj) \
171 lua_assert(!iscollectable(obj) || \
172 ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc)))
175 /* Macros to set values */
176 #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
178 /* Must not have side effects, 'x' may be expression.
180 #define setnvalue(obj,x) { TValue *i_o=(obj); i_o->value.n= (x); i_o->tt=LUA_TNUMBER; }
183 # define setivalue(obj,x) { TValue *i_o=(obj); i_o->value.i=(x); i_o->tt=LUA_TINT; }
185 # define setivalue(obj,x) setnvalue(obj,cast_num(x))
188 /* Note: Complex always has "inline", both are C99.
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;
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; }
202 #define setpvalue(obj,x) \
203 { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; }
205 #define setbvalue(obj,x) \
206 { TValue *i_o=(obj); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
245 ** different types of sets, according to destination
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
258 #define setobj2t setobj
260 #define setobj2n setobj
261 #define setsvalue2n setsvalue
263 #define setttype(obj, tt) (ttype(obj) = (tt))
265 #if defined(LUA_TINT) && (LUA_TINT >= LUA_TSTRING)
266 # define iscollectable(o) ((ttype(o) >= LUA_TSTRING) && (ttype(o) != LUA_TINT))
268 # define iscollectable(o) (ttype(o) >= LUA_TSTRING)
273 typedef TValue *StkId; /* index to stack elements */
277 ** String headers for string table
279 typedef union TString {
280 L_Umaxalign dummy; /* ensures maximum alignment for strings */
290 #define getstr(ts) cast(const char *, (ts) + 1)
291 #define svalue(o) getstr(rawtsvalue(o))
295 typedef union Udata {
296 L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
299 struct Table *metatable;
309 ** Function Prototypes
311 typedef struct Proto {
313 TValue *k; /* constants used by the function */
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 */
321 int sizek; /* size of `k' */
324 int sizep; /* size of `p' */
329 lu_byte nups; /* number of upvalues */
332 lu_byte maxstacksize;
336 /* masks for new-style vararg */
337 #define VARARG_HASARG 1
338 #define VARARG_ISVARARG 2
339 #define VARARG_NEEDSARG 4
342 typedef struct LocVar {
344 int startpc; /* first point where variable is active */
345 int endpc; /* first point where variable is dead */
354 typedef struct UpVal {
356 TValue *v; /* points to stack or to its own value */
358 TValue value; /* the value (when closed) */
359 struct { /* double linked list (when open) */
371 #define ClosureHeader \
372 CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \
375 typedef struct CClosure {
382 typedef struct LClosure {
389 typedef union Closure {
395 #define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)
396 #define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)
406 struct Node *next; /* for chaining */
412 typedef struct Node {
418 typedef struct Table {
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 */
425 Node *lastfree; /* any free position is before this position */
427 int sizearray; /* size of `array' array */
433 ** `module' operation for hashing (size is always a power of 2)
435 #define lmod(s,size) \
436 (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
439 #define twoto(x) (1<<(x))
440 #define sizenode(t) (twoto((t)->lsizenode))
443 #define luaO_nilobject (&luaO_nilobject_)
445 LUAI_DATA const TValue luaO_nilobject_;
447 #define ceillog2(x) (luaO_log2((x)-1) + 1)
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,
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);