Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4680 | right-hear | 1 | /* |
2 | ** $Id: lobject.h,v 2.42 2010/07/26 15:53:23 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 |
||
13 | |||
14 | |||
15 | #include "llimits.h" |
||
16 | #include "lua.h" |
||
17 | |||
18 | |||
19 | /* |
||
20 | ** Extra tags for non-values |
||
21 | */ |
||
22 | #define LUA_TPROTO LUA_NUMTAGS |
||
23 | #define LUA_TUPVAL (LUA_NUMTAGS+1) |
||
24 | #define LUA_TDEADKEY (LUA_NUMTAGS+2) |
||
25 | |||
26 | |||
27 | /* |
||
28 | ** Variant tag for light C functions (negative to be considered |
||
29 | ** non collectable by 'iscollectable') |
||
30 | */ |
||
31 | #define LUA_TLCF (~0x0F | LUA_TFUNCTION) |
||
32 | |||
33 | /* |
||
34 | ** Union of all collectable objects |
||
35 | */ |
||
36 | typedef union GCObject GCObject; |
||
37 | |||
38 | |||
39 | /* |
||
40 | ** Common Header for all collectable objects (in macro form, to be |
||
41 | ** included in other objects) |
||
42 | */ |
||
43 | #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked |
||
44 | |||
45 | |||
46 | /* |
||
47 | ** Common header in struct form |
||
48 | */ |
||
49 | typedef struct GCheader { |
||
50 | CommonHeader; |
||
51 | } GCheader; |
||
52 | |||
53 | |||
54 | |||
55 | /* |
||
56 | ** Union of all Lua values |
||
57 | */ |
||
58 | typedef union { |
||
59 | GCObject *gc; /* collectable objects */ |
||
60 | void *p; /* light userdata */ |
||
61 | lua_Number n; /* numbers */ |
||
62 | int b; /* booleans */ |
||
63 | lua_CFunction f; /* light C functions */ |
||
64 | } Value; |
||
65 | |||
66 | |||
67 | |||
68 | /* |
||
69 | ** Tagged Values. This is the basic representation of values in Lua, |
||
70 | ** an actual value plus a tag with its type. |
||
71 | */ |
||
72 | |||
73 | #define TValuefields Value value_; int tt_ |
||
74 | |||
75 | typedef struct lua_TValue { |
||
76 | TValuefields; |
||
77 | } TValue; |
||
78 | |||
79 | |||
80 | /* macro defining a nil value */ |
||
81 | #define NILCONSTANT {NULL}, LUA_TNIL |
||
82 | |||
83 | |||
84 | /* |
||
85 | ** type tag of a TValue |
||
86 | */ |
||
87 | #define ttype(o) ((o)->tt_) |
||
88 | |||
89 | |||
90 | /* |
||
91 | ** type tag of a TValue with no variants |
||
92 | */ |
||
93 | #define ttypenv(o) (ttype(o) & 0x0F) |
||
94 | |||
95 | |||
96 | /* Macros to test type */ |
||
97 | #define ttisnil(o) (ttype(o) == LUA_TNIL) |
||
98 | #define ttisnumber(o) (ttype(o) == LUA_TNUMBER) |
||
99 | #define ttisstring(o) (ttype(o) == LUA_TSTRING) |
||
100 | #define ttistable(o) (ttype(o) == LUA_TTABLE) |
||
101 | #define ttisfunction(o) (ttypenv(o) == LUA_TFUNCTION) |
||
102 | #define ttisclosure(o) (ttype(o) == LUA_TFUNCTION) |
||
103 | #define ttislcf(o) (ttype(o) == LUA_TLCF) |
||
104 | #define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN) |
||
105 | #define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA) |
||
106 | #define ttisthread(o) (ttype(o) == LUA_TTHREAD) |
||
107 | #define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA) |
||
108 | #define ttisdeadkey(o) (ttype(o) == LUA_TDEADKEY) |
||
109 | |||
110 | /* Macros to access values */ |
||
111 | #define gcvalue(o) check_exp(iscollectable(o), (o)->value_.gc) |
||
112 | #define pvalue(o) check_exp(ttislightuserdata(o), (o)->value_.p) |
||
113 | #define nvalue(o) check_exp(ttisnumber(o), (o)->value_.n) |
||
114 | #define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value_.gc->ts) |
||
115 | #define tsvalue(o) (&rawtsvalue(o)->tsv) |
||
116 | #define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value_.gc->u) |
||
117 | #define uvalue(o) (&rawuvalue(o)->uv) |
||
118 | #define clvalue(o) check_exp(ttisclosure(o), &(o)->value_.gc->cl) |
||
119 | #define fvalue(o) check_exp(ttislcf(o), (o)->value_.f) |
||
120 | #define hvalue(o) check_exp(ttistable(o), &(o)->value_.gc->h) |
||
121 | #define bvalue(o) check_exp(ttisboolean(o), (o)->value_.b) |
||
122 | #define thvalue(o) check_exp(ttisthread(o), &(o)->value_.gc->th) |
||
123 | |||
124 | #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0)) |
||
125 | |||
126 | |||
127 | #define iscollectable(o) (ttype(o) >= LUA_TSTRING) |
||
128 | |||
129 | |||
130 | /* Macros for internal tests */ |
||
131 | #define righttt(obj) (ttype(obj) == gcvalue(obj)->gch.tt) |
||
132 | |||
133 | #define checkconsistency(obj) lua_assert(!iscollectable(obj) || righttt(obj)) |
||
134 | |||
135 | #define checkliveness(g,obj) \ |
||
136 | lua_assert(!iscollectable(obj) || (righttt(obj) && !isdead(g,gcvalue(obj)))) |
||
137 | |||
138 | |||
139 | /* Macros to set values */ |
||
140 | #define setnilvalue(obj) ((obj)->tt_=LUA_TNIL) |
||
141 | |||
142 | #define setnvalue(obj,x) \ |
||
143 | { TValue *i_o=(obj); i_o->value_.n=(x); i_o->tt_=LUA_TNUMBER; } |
||
144 | |||
145 | #define setfvalue(obj,x) \ |
||
146 | { TValue *i_o=(obj); i_o->value_.f=(x); i_o->tt_=LUA_TLCF; } |
||
147 | |||
148 | #define changenvalue(o,x) check_exp((o)->tt_==LUA_TNUMBER, (o)->value_.n=(x)) |
||
149 | |||
150 | #define setpvalue(obj,x) \ |
||
151 | { TValue *i_o=(obj); i_o->value_.p=(x); i_o->tt_=LUA_TLIGHTUSERDATA; } |
||
152 | |||
153 | #define setbvalue(obj,x) \ |
||
154 | { TValue *i_o=(obj); i_o->value_.b=(x); i_o->tt_=LUA_TBOOLEAN; } |
||
155 | |||
156 | #define setsvalue(L,obj,x) \ |
||
157 | { TValue *i_o=(obj); \ |
||
158 | i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TSTRING; \ |
||
159 | checkliveness(G(L),i_o); } |
||
160 | |||
161 | #define setuvalue(L,obj,x) \ |
||
162 | { TValue *i_o=(obj); \ |
||
163 | i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TUSERDATA; \ |
||
164 | checkliveness(G(L),i_o); } |
||
165 | |||
166 | #define setthvalue(L,obj,x) \ |
||
167 | { TValue *i_o=(obj); \ |
||
168 | i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TTHREAD; \ |
||
169 | checkliveness(G(L),i_o); } |
||
170 | |||
171 | #define setclvalue(L,obj,x) \ |
||
172 | { TValue *i_o=(obj); \ |
||
173 | i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TFUNCTION; \ |
||
174 | checkliveness(G(L),i_o); } |
||
175 | |||
176 | #define sethvalue(L,obj,x) \ |
||
177 | { TValue *i_o=(obj); \ |
||
178 | i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TTABLE; \ |
||
179 | checkliveness(G(L),i_o); } |
||
180 | |||
181 | #define setptvalue(L,obj,x) \ |
||
182 | { TValue *i_o=(obj); \ |
||
183 | i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TPROTO; \ |
||
184 | checkliveness(G(L),i_o); } |
||
185 | |||
186 | #define setdeadvalue(obj) ((obj)->tt_=LUA_TDEADKEY) |
||
187 | |||
188 | |||
189 | |||
190 | #define setobj(L,obj1,obj2) \ |
||
191 | { const TValue *o2=(obj2); TValue *o1=(obj1); \ |
||
192 | o1->value_ = o2->value_; o1->tt_=o2->tt_; \ |
||
193 | checkliveness(G(L),o1); } |
||
194 | |||
195 | |||
196 | /* |
||
197 | ** different types of assignments, according to destination |
||
198 | */ |
||
199 | |||
200 | /* from stack to (same) stack */ |
||
201 | #define setobjs2s setobj |
||
202 | /* to stack (not from same stack) */ |
||
203 | #define setobj2s setobj |
||
204 | #define setsvalue2s setsvalue |
||
205 | #define sethvalue2s sethvalue |
||
206 | #define setptvalue2s setptvalue |
||
207 | /* from table to same table */ |
||
208 | #define setobjt2t setobj |
||
209 | /* to table */ |
||
210 | #define setobj2t setobj |
||
211 | /* to new object */ |
||
212 | #define setobj2n setobj |
||
213 | #define setsvalue2n setsvalue |
||
214 | |||
215 | |||
216 | |||
217 | typedef TValue *StkId; /* index to stack elements */ |
||
218 | |||
219 | |||
220 | /* |
||
221 | ** Header for string value; string bytes follow the end of this structure |
||
222 | */ |
||
223 | typedef union TString { |
||
224 | L_Umaxalign dummy; /* ensures maximum alignment for strings */ |
||
225 | struct { |
||
226 | CommonHeader; |
||
227 | lu_byte reserved; |
||
228 | unsigned int hash; |
||
229 | size_t len; |
||
230 | } tsv; |
||
231 | } TString; |
||
232 | |||
233 | |||
234 | /* get the actual string (array of bytes) from a TString */ |
||
235 | #define getstr(ts) cast(const char *, (ts) + 1) |
||
236 | |||
237 | /* get the actual string (array of bytes) from a Lua value */ |
||
238 | #define svalue(o) getstr(rawtsvalue(o)) |
||
239 | |||
240 | |||
241 | /* |
||
242 | ** Header for userdata; memory area follows the end of this structure |
||
243 | */ |
||
244 | typedef union Udata { |
||
245 | L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */ |
||
246 | struct { |
||
247 | CommonHeader; |
||
248 | struct Table *metatable; |
||
249 | struct Table *env; |
||
250 | size_t len; |
||
251 | } uv; |
||
252 | } Udata; |
||
253 | |||
254 | |||
255 | |||
256 | /* |
||
257 | ** Description of an upvalue for function prototypes |
||
258 | */ |
||
259 | typedef struct Upvaldesc { |
||
260 | TString *name; /* upvalue name (for debug information) */ |
||
261 | lu_byte instack; /* whether it is in stack */ |
||
262 | lu_byte idx; /* index of upvalue (in stack or in outer function's list) */ |
||
263 | } Upvaldesc; |
||
264 | |||
265 | |||
266 | /* |
||
267 | ** Description of a local variable for function prototypes |
||
268 | ** (used for debug information) |
||
269 | */ |
||
270 | typedef struct LocVar { |
||
271 | TString *varname; |
||
272 | int startpc; /* first point where variable is active */ |
||
273 | int endpc; /* first point where variable is dead */ |
||
274 | } LocVar; |
||
275 | |||
276 | |||
277 | /* |
||
278 | ** Function Prototypes |
||
279 | */ |
||
280 | typedef struct Proto { |
||
281 | CommonHeader; |
||
282 | TValue *k; /* constants used by the function */ |
||
283 | Instruction *code; |
||
284 | struct Proto **p; /* functions defined inside the function */ |
||
285 | int *lineinfo; /* map from opcodes to source lines */ |
||
286 | LocVar *locvars; /* information about local variables */ |
||
287 | Upvaldesc *upvalues; /* upvalue information */ |
||
288 | union Closure *cache; /* last created closure with this prototype */ |
||
289 | TString *source; |
||
290 | int sizeupvalues; /* size of 'upvalues' */ |
||
291 | int sizek; /* size of `k' */ |
||
292 | int sizecode; |
||
293 | int sizelineinfo; |
||
294 | int sizep; /* size of `p' */ |
||
295 | int sizelocvars; |
||
296 | int linedefined; |
||
297 | int lastlinedefined; |
||
298 | GCObject *gclist; |
||
299 | lu_byte numparams; /* number of fixed parameters */ |
||
300 | lu_byte is_vararg; |
||
301 | lu_byte maxstacksize; /* maximum stack used by this function */ |
||
302 | } Proto; |
||
303 | |||
304 | |||
305 | |||
306 | /* |
||
307 | ** Lua Upvalues |
||
308 | */ |
||
309 | typedef struct UpVal { |
||
310 | CommonHeader; |
||
311 | TValue *v; /* points to stack or to its own value */ |
||
312 | union { |
||
313 | TValue value; /* the value (when closed) */ |
||
314 | struct { /* double linked list (when open) */ |
||
315 | struct UpVal *prev; |
||
316 | struct UpVal *next; |
||
317 | } l; |
||
318 | } u; |
||
319 | } UpVal; |
||
320 | |||
321 | |||
322 | /* |
||
323 | ** Closures |
||
324 | */ |
||
325 | |||
326 | #define ClosureHeader \ |
||
327 | CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist |
||
328 | |||
329 | typedef struct CClosure { |
||
330 | ClosureHeader; |
||
331 | lua_CFunction f; |
||
332 | TValue upvalue[1]; /* list of upvalues */ |
||
333 | } CClosure; |
||
334 | |||
335 | |||
336 | typedef struct LClosure { |
||
337 | ClosureHeader; |
||
338 | struct Proto *p; |
||
339 | UpVal *upvals[1]; /* list of upvalues */ |
||
340 | } LClosure; |
||
341 | |||
342 | |||
343 | typedef union Closure { |
||
344 | CClosure c; |
||
345 | LClosure l; |
||
346 | } Closure; |
||
347 | |||
348 | |||
349 | #define isLfunction(o) (ttisclosure(o) && !clvalue(o)->c.isC) |
||
350 | |||
351 | #define getproto(o) (clvalue(o)->l.p) |
||
352 | |||
353 | |||
354 | /* |
||
355 | ** Tables |
||
356 | */ |
||
357 | |||
358 | typedef union TKey { |
||
359 | struct { |
||
360 | TValuefields; |
||
361 | struct Node *next; /* for chaining */ |
||
362 | } nk; |
||
363 | TValue tvk; |
||
364 | } TKey; |
||
365 | |||
366 | |||
367 | typedef struct Node { |
||
368 | TValue i_val; |
||
369 | TKey i_key; |
||
370 | } Node; |
||
371 | |||
372 | |||
373 | typedef struct Table { |
||
374 | CommonHeader; |
||
375 | lu_byte flags; /* 1< |
||
376 | lu_byte lsizenode; /* log2 of size of `node' array */ |
||
377 | struct Table *metatable; |
||
378 | TValue *array; /* array part */ |
||
379 | Node *node; |
||
380 | Node *lastfree; /* any free position is before this position */ |
||
381 | GCObject *gclist; |
||
382 | int sizearray; /* size of `array' array */ |
||
383 | } Table; |
||
384 | |||
385 | |||
386 | |||
387 | /* |
||
388 | ** `module' operation for hashing (size is always a power of 2) |
||
389 | */ |
||
390 | #define lmod(s,size) \ |
||
391 | (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1))))) |
||
392 | |||
393 | |||
394 | #define twoto(x) (1<<(x)) |
||
395 | #define sizenode(t) (twoto((t)->lsizenode)) |
||
396 | |||
397 | |||
398 | /* |
||
399 | ** (address of) a fixed nil value |
||
400 | */ |
||
401 | #define luaO_nilobject (&luaO_nilobject_) |
||
402 | |||
403 | |||
404 | LUAI_DDEC const TValue luaO_nilobject_; |
||
405 | |||
406 | LUAI_FUNC int luaO_int2fb (unsigned int x); |
||
407 | LUAI_FUNC int luaO_fb2int (int x); |
||
408 | LUAI_FUNC int luaO_ceillog2 (lu_int32 x); |
||
409 | LUAI_FUNC lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2); |
||
410 | LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2); |
||
411 | LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result); |
||
412 | LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, |
||
413 | va_list argp); |
||
414 | LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...); |
||
415 | LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len); |
||
416 | |||
417 | |||
418 | #endif(x)) |
||
419 |