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 Pr |