rpm  4.5
lobject.h
Go to the documentation of this file.
1 /*
2 ** $Id: lobject.h,v 1.1 2004/03/16 21:58:30 niemeyer Exp $
3 ** Type definitions for Lua objects
4 ** See Copyright Notice in lua.h
5 */
6 
7 #ifndef lobject_h
8 #define lobject_h
9 
10 
11 #include "llimits.h"
12 #include "lua.h"
13 
14 
15 /* tags for values visible from Lua */
16 #define NUM_TAGS LUA_TTHREAD
17 
18 
19 /*
20 ** Extra tags for non-values
21 */
22 #define LUA_TPROTO (NUM_TAGS+1)
23 #define LUA_TUPVAL (NUM_TAGS+2)
24 
25 
26 /*
27 ** Union of all collectable objects
28 */
29 typedef union GCObject GCObject;
30 
31 
32 /*
33 ** Common Header for all collectable objects (in macro form, to be
34 ** included in other objects)
35 */
36 #define CommonHeader /*@dependent@*/ /*@null@*/ GCObject *next; lu_byte tt; lu_byte marked
37 
38 
39 /*
40 ** Common header in struct form
41 */
42 typedef struct GCheader {
44 } GCheader;
45 
46 
47 
48 
49 /*
50 ** Union of all Lua values
51 */
52 typedef union {
53 /*@relnull@*/
55  void *p;
56  lua_Number n;
57  int b;
58 } Value;
59 
60 
61 /*
62 ** Lua values (or `tagged objects')
63 */
64 typedef struct lua_TObject {
65  int tt;
67 } TObject;
68 
69 
70 /* Macros to test type */
71 #define ttisnil(o) (ttype(o) == LUA_TNIL)
72 #define ttisnumber(o) (ttype(o) == LUA_TNUMBER)
73 #define ttisstring(o) (ttype(o) == LUA_TSTRING)
74 #define ttistable(o) (ttype(o) == LUA_TTABLE)
75 #define ttisfunction(o) (ttype(o) == LUA_TFUNCTION)
76 #define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN)
77 #define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA)
78 #define ttisthread(o) (ttype(o) == LUA_TTHREAD)
79 #define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA)
80 
81 /* Macros to access values */
82 #define ttype(o) ((o)->tt)
83 #define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc)
84 #define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p)
85 #define nvalue(o) check_exp(ttisnumber(o), (o)->value.n)
86 #define tsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)
87 #define uvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u)
88 #define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl)
89 #define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h)
90 #define bvalue(o) check_exp(ttisboolean(o), (o)->value.b)
91 #define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th)
92 
93 #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
94 
95 /* Macros to set values */
96 #define setnvalue(obj,x) \
97  { TObject *i_o=(obj); i_o->tt=LUA_TNUMBER; i_o->value.n=(x); }
98 
99 #define chgnvalue(obj,x) \
100  check_exp(ttype(obj)==LUA_TNUMBER, (obj)->value.n=(x))
101 
102 #define setpvalue(obj,x) \
103  { TObject *i_o=(obj); i_o->tt=LUA_TLIGHTUSERDATA; i_o->value.p=(x); }
104 
105 #define setbvalue(obj,x) \
106  { TObject *i_o=(obj); i_o->tt=LUA_TBOOLEAN; i_o->value.b=(x); }
107 
108 #define setsvalue(obj,x) \
109  { TObject *i_o=(obj); i_o->tt=LUA_TSTRING; \
110  i_o->value.gc=cast(GCObject *, (x)); \
111  lua_assert(i_o->value.gc->gch.tt == LUA_TSTRING); }
112 
113 #define setuvalue(obj,x) \
114  { TObject *i_o=(obj); i_o->tt=LUA_TUSERDATA; \
115  i_o->value.gc=cast(GCObject *, (x)); \
116  lua_assert(i_o->value.gc->gch.tt == LUA_TUSERDATA); }
117 
118 #define setthvalue(obj,x) \
119  { TObject *i_o=(obj); i_o->tt=LUA_TTHREAD; \
120  i_o->value.gc=cast(GCObject *, (x)); \
121  lua_assert(i_o->value.gc->gch.tt == LUA_TTHREAD); }
122 
123 #define setclvalue(obj,x) \
124  { TObject *i_o=(obj); i_o->tt=LUA_TFUNCTION; \
125  i_o->value.gc=cast(GCObject *, (x)); \
126  lua_assert(i_o->value.gc->gch.tt == LUA_TFUNCTION); }
127 
128 #define sethvalue(obj,x) \
129  { TObject *i_o=(obj); i_o->tt=LUA_TTABLE; \
130  i_o->value.gc=cast(GCObject *, (x)); \
131  lua_assert(i_o->value.gc->gch.tt == LUA_TTABLE); }
132 
133 #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
134 
135 
136 
137 /*
138 ** for internal debug only
139 */
140 #define checkconsistency(obj) \
141  lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt))
142 
143 
144 #define setobj(obj1,obj2) \
145  { const TObject *o2=(obj2); TObject *o1=(obj1); \
146  checkconsistency(o2); \
147  o1->tt=o2->tt; o1->value = o2->value; }
148 
149 
150 /*
151 ** different types of sets, according to destination
152 */
153 
154 /* from stack to (same) stack */
155 #define setobjs2s setobj
156 /* to stack (not from same stack) */
157 #define setobj2s setobj
158 #define setsvalue2s setsvalue
159 /* from table to same table */
160 #define setobjt2t setobj
161 /* to table */
162 #define setobj2t setobj
163 /* to new object */
164 #define setobj2n setobj
165 #define setsvalue2n setsvalue
166 
167 #define setttype(obj, tt) (ttype(obj) = (tt))
168 
169 
170 #define iscollectable(o) (ttype(o) >= LUA_TSTRING)
171 
172 
173 
174 typedef TObject *StkId; /* index to stack elements */
175 
176 
177 /*
178 ** String headers for string table
179 */
180 typedef union TString {
181  L_Umaxalign dummy; /* ensures maximum alignment for strings */
182  struct {
186  size_t len;
187  } tsv;
188 } TString;
189 
190 
191 #define getstr(ts) cast(const char *, (ts) + 1)
192 #define svalue(o) getstr(tsvalue(o))
193 
194 
195 
196 typedef union Udata {
197  L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
198  struct {
200  struct Table *metatable;
201  size_t len;
202  } uv;
203 } Udata;
204 
205 
206 
207 
208 /*
209 ** Function Prototypes
210 */
211 typedef struct Proto {
213 /*@relnull@*/
214  TObject *k; /* constants used by the function */
215 /*@relnull@*/
217 /*@relnull@*/
218  struct Proto **p; /* functions defined inside the function */
219 /*@relnull@*/
220  int *lineinfo; /* map from opcodes to source lines */
221 /*@relnull@*/
222  struct LocVar *locvars; /* information about local variables */
223 /*@relnull@*/
224  TString **upvalues; /* upvalue names */
225 /*@relnull@*/
228  int sizek; /* size of `k' */
229  int sizecode;
231  int sizep; /* size of `p' */
234 /*@relnull@*/
236  lu_byte nups; /* number of upvalues */
240 } Proto;
241 
242 
243 typedef struct LocVar {
244 /*@relnull@*/
246  int startpc; /* first point where variable is active */
247  int endpc; /* first point where variable is dead */
248 } LocVar;
249 
250 
251 
252 /*
253 ** Upvalues
254 */
255 
256 typedef struct UpVal {
258 /*@null@*/
259  TObject *v; /* points to stack or to its own value */
260  TObject value; /* the value (when closed) */
261 } UpVal;
262 
263 
264 /*
265 ** Closures
266 */
267 
268 #define ClosureHeader \
269  CommonHeader; lu_byte isC; lu_byte nupvalues; /*@null@*/ GCObject *gclist
270 
271 typedef struct CClosure {
273  lua_CFunction f;
275 } CClosure;
276 
277 
278 typedef struct LClosure {
280  struct Proto *p;
281  TObject g; /* global table for this closure */
283 } LClosure;
284 
285 
286 typedef union Closure {
289 } Closure;
290 
291 
292 #define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)
293 #define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)
294 
295 
296 /*
297 ** Tables
298 */
299 
300 typedef struct Node {
303  struct Node *next; /* for chaining */
304 } Node;
305 
306 
307 typedef struct Table {
309  lu_byte flags; /* 1<<p means tagmethod(p) is not present */
310  lu_byte lsizenode; /* log2 of size of `node' array */
311  struct Table *metatable;
312 /*@null@*/
313  TObject *array; /* array part */
314 /*@owned@*/ /*@null@*/
316  Node *firstfree; /* this position is free; all positions after it are full */
318  int sizearray; /* size of `array' array */
319 } Table;
320 
321 
322 
323 /*
324 ** `module' operation for hashing (size is always a power of 2)
325 */
326 #define lmod(s,size) \
327  check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1))))
328 
329 
330 #define twoto(x) (1<<(x))
331 #define sizenode(t) (twoto((t)->lsizenode))
332 
333 
334 
335 /*@unchecked@*/
336 extern const TObject luaO_nilobject;
337 
338 int luaO_log2 (unsigned int x)
339  /*@*/;
340 int luaO_int2fb (unsigned int x)
341  /*@*/;
342 #define fb2int(x) (((x) & 7) << ((x) >> 3))
343 
344 int luaO_rawequalObj (const TObject *t1, const TObject *t2)
345  /*@*/;
346 int luaO_str2d (const char *s, lua_Number *result)
347  /*@modifies *result @*/;
348 
349 /*@observer@*/
350 const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp)
351  /*@modifies L @*/;
352 /*@observer@*/
353 const char *luaO_pushfstring (lua_State *L, const char *fmt, ...)
354  /*@modifies L @*/;
355 void luaO_chunkid (char *out, const char *source, int len)
356  /*@modifies *out @*/;
357 
358 
359 #endif