diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-08-30 16:09:21 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-08-30 16:09:21 -0300 |
commit | fdafd4f4a88d1584dea900517445a17d87f8b82f (patch) | |
tree | 52e38e886d569e77c1df82ebe2bcfbaafd4c6f75 /lobject.h | |
parent | beeff4ccafe5877d00119cb3d93f1f937d46dcfb (diff) | |
download | lua-fdafd4f4a88d1584dea900517445a17d87f8b82f.tar.gz lua-fdafd4f4a88d1584dea900517445a17d87f8b82f.tar.bz2 lua-fdafd4f4a88d1584dea900517445a17d87f8b82f.zip |
new structure for collectable objects, sharing a common header
Diffstat (limited to 'lobject.h')
-rw-r--r-- | lobject.h | 125 |
1 files changed, 92 insertions, 33 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lobject.h,v 1.142 2002/08/06 17:06:56 roberto Exp roberto $ | 2 | ** $Id: lobject.h,v 1.143 2002/08/16 14:45:55 roberto Exp roberto $ |
3 | ** Type definitions for Lua objects | 3 | ** Type definitions for Lua objects |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -13,20 +13,42 @@ | |||
13 | 13 | ||
14 | 14 | ||
15 | /* tags for values visible from Lua */ | 15 | /* tags for values visible from Lua */ |
16 | #define NUM_TAGS LUA_TFUNCTION | 16 | #define NUM_TAGS LUA_TUSERDATA |
17 | 17 | ||
18 | 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 | ** Common header for all collectable objects | ||
28 | */ | ||
29 | typedef struct GCheader { | ||
30 | union GCObject *next; /* pointer to next object */ | ||
31 | lu_byte tt; /* object type */ | ||
32 | lu_byte marked; /* GC informations */ | ||
33 | } GCheader; | ||
34 | |||
35 | |||
36 | |||
37 | |||
38 | /* | ||
39 | ** Union of all Lua values | ||
40 | */ | ||
19 | typedef union { | 41 | typedef union { |
42 | union GCObject *gc; | ||
20 | void *p; | 43 | void *p; |
21 | union TString *ts; | ||
22 | union Udata *u; | ||
23 | union Closure *cl; | ||
24 | struct Table *h; | ||
25 | lua_Number n; | 44 | lua_Number n; |
26 | int b; | 45 | int b; |
27 | } Value; | 46 | } Value; |
28 | 47 | ||
29 | 48 | ||
49 | /* | ||
50 | ** Lua values (or `tagged objects') | ||
51 | */ | ||
30 | typedef struct lua_TObject { | 52 | typedef struct lua_TObject { |
31 | int tt; | 53 | int tt; |
32 | Value value; | 54 | Value value; |
@@ -47,10 +69,10 @@ typedef struct lua_TObject { | |||
47 | #define ttype(o) ((o)->tt) | 69 | #define ttype(o) ((o)->tt) |
48 | #define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p) | 70 | #define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p) |
49 | #define nvalue(o) check_exp(ttisnumber(o), (o)->value.n) | 71 | #define nvalue(o) check_exp(ttisnumber(o), (o)->value.n) |
50 | #define tsvalue(o) check_exp(ttisstring(o), (o)->value.ts) | 72 | #define tsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts) |
51 | #define uvalue(o) check_exp(ttisuserdata(o), (o)->value.u) | 73 | #define uvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u) |
52 | #define clvalue(o) check_exp(ttisfunction(o), (o)->value.cl) | 74 | #define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl) |
53 | #define hvalue(o) check_exp(ttistable(o), (o)->value.h) | 75 | #define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h) |
54 | #define bvalue(o) check_exp(ttisboolean(o), (o)->value.b) | 76 | #define bvalue(o) check_exp(ttisboolean(o), (o)->value.b) |
55 | 77 | ||
56 | #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0)) | 78 | #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0)) |
@@ -69,26 +91,43 @@ typedef struct lua_TObject { | |||
69 | { TObject *i_o=(obj); i_o->tt=LUA_TBOOLEAN; i_o->value.b=(x); } | 91 | { TObject *i_o=(obj); i_o->tt=LUA_TBOOLEAN; i_o->value.b=(x); } |
70 | 92 | ||
71 | #define setsvalue(obj,x) \ | 93 | #define setsvalue(obj,x) \ |
72 | { TObject *i_o=(obj); i_o->tt=LUA_TSTRING; i_o->value.ts=(x); } | 94 | { TObject *i_o=(obj); i_o->tt=LUA_TSTRING; \ |
95 | i_o->value.gc=cast(GCObject *, (x)); \ | ||
96 | lua_assert(i_o->value.gc->gch.tt == LUA_TSTRING); } | ||
73 | 97 | ||
74 | #define setuvalue(obj,x) \ | 98 | #define setuvalue(obj,x) \ |
75 | { TObject *i_o=(obj); i_o->tt=LUA_TUSERDATA; i_o->value.u=(x); } | 99 | { TObject *i_o=(obj); i_o->tt=LUA_TUSERDATA; \ |
100 | i_o->value.gc=cast(GCObject *, (x)); \ | ||
101 | lua_assert(i_o->value.gc->gch.tt == LUA_TUSERDATA); } | ||
76 | 102 | ||
77 | #define setclvalue(obj,x) \ | 103 | #define setclvalue(obj,x) \ |
78 | { TObject *i_o=(obj); i_o->tt=LUA_TFUNCTION; i_o->value.cl=(x); } | 104 | { TObject *i_o=(obj); i_o->tt=LUA_TFUNCTION; \ |
105 | i_o->value.gc=cast(GCObject *, (x)); \ | ||
106 | lua_assert(i_o->value.gc->gch.tt == LUA_TFUNCTION); } | ||
79 | 107 | ||
80 | #define sethvalue(obj,x) \ | 108 | #define sethvalue(obj,x) \ |
81 | { TObject *i_o=(obj); i_o->tt=LUA_TTABLE; i_o->value.h=(x); } | 109 | { TObject *i_o=(obj); i_o->tt=LUA_TTABLE; \ |
110 | i_o->value.gc=cast(GCObject *, (x)); \ | ||
111 | lua_assert(i_o->value.gc->gch.tt == LUA_TTABLE); } | ||
82 | 112 | ||
83 | #define setnilvalue(obj) ((obj)->tt=LUA_TNIL) | 113 | #define setnilvalue(obj) ((obj)->tt=LUA_TNIL) |
84 | 114 | ||
115 | |||
116 | #define checkconsistency(obj) \ | ||
117 | lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt)) | ||
118 | |||
119 | |||
85 | #define setobj(obj1,obj2) \ | 120 | #define setobj(obj1,obj2) \ |
86 | { const TObject *o2=(obj2); TObject *o1=(obj1); \ | 121 | { const TObject *o2=(obj2); TObject *o1=(obj1); \ |
122 | checkconsistency(o2); \ | ||
87 | o1->tt=o2->tt; o1->value = o2->value; } | 123 | o1->tt=o2->tt; o1->value = o2->value; } |
88 | 124 | ||
89 | #define setttype(obj, tt) (ttype(obj) = (tt)) | 125 | #define setttype(obj, tt) (ttype(obj) = (tt)) |
90 | 126 | ||
91 | 127 | ||
128 | #define iscollectable(o) (ttype(o) >= LUA_TSTRING) | ||
129 | |||
130 | |||
92 | 131 | ||
93 | typedef TObject *StkId; /* index to stack elements */ | 132 | typedef TObject *StkId; /* index to stack elements */ |
94 | 133 | ||
@@ -99,11 +138,12 @@ typedef TObject *StkId; /* index to stack elements */ | |||
99 | typedef union TString { | 138 | typedef union TString { |
100 | union L_Umaxalign dummy; /* ensures maximum alignment for strings */ | 139 | union L_Umaxalign dummy; /* ensures maximum alignment for strings */ |
101 | struct { | 140 | struct { |
141 | union GCObject *next; /* pointer to next object */ | ||
142 | lu_byte tt; /* object type */ | ||
143 | lu_byte marked; /* GC informations */ | ||
144 | lu_byte reserved; | ||
102 | lu_hash hash; | 145 | lu_hash hash; |
103 | size_t len; | 146 | size_t len; |
104 | union TString *nexthash; /* chain for hash table */ | ||
105 | lu_byte marked; | ||
106 | lu_byte reserved; | ||
107 | } tsv; | 147 | } tsv; |
108 | } TString; | 148 | } TString; |
109 | 149 | ||
@@ -116,10 +156,11 @@ typedef union TString { | |||
116 | typedef union Udata { | 156 | typedef union Udata { |
117 | union L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */ | 157 | union L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */ |
118 | struct { | 158 | struct { |
159 | union GCObject *next; /* pointer to next object */ | ||
160 | lu_byte tt; /* object type */ | ||
161 | lu_byte marked; /* GC informations */ | ||
119 | struct Table *metatable; | 162 | struct Table *metatable; |
120 | union Udata *next; /* chain for list of all udata */ | ||
121 | size_t len; | 163 | size_t len; |
122 | lu_byte marked; | ||
123 | } uv; | 164 | } uv; |
124 | } Udata; | 165 | } Udata; |
125 | 166 | ||
@@ -130,10 +171,12 @@ typedef union Udata { | |||
130 | ** Function Prototypes | 171 | ** Function Prototypes |
131 | */ | 172 | */ |
132 | typedef struct Proto { | 173 | typedef struct Proto { |
174 | union GCObject *next; /* pointer to next object */ | ||
175 | lu_byte tt; /* object type */ | ||
176 | lu_byte marked; /* GC informations */ | ||
133 | TObject *k; /* constants used by the function */ | 177 | TObject *k; /* constants used by the function */ |
134 | Instruction *code; | 178 | Instruction *code; |
135 | struct Proto **p; /* functions defined inside the function */ | 179 | struct Proto **p; /* functions defined inside the function */ |
136 | struct Proto *next; | ||
137 | int *lineinfo; /* map from opcodes to source lines */ | 180 | int *lineinfo; /* map from opcodes to source lines */ |
138 | struct LocVar *locvars; /* information about local variables */ | 181 | struct LocVar *locvars; /* information about local variables */ |
139 | TString *source; | 182 | TString *source; |
@@ -146,7 +189,6 @@ typedef struct Proto { | |||
146 | lu_byte numparams; | 189 | lu_byte numparams; |
147 | lu_byte is_vararg; | 190 | lu_byte is_vararg; |
148 | lu_byte maxstacksize; | 191 | lu_byte maxstacksize; |
149 | lu_byte marked; | ||
150 | } Proto; | 192 | } Proto; |
151 | 193 | ||
152 | 194 | ||
@@ -163,10 +205,11 @@ typedef struct LocVar { | |||
163 | */ | 205 | */ |
164 | 206 | ||
165 | typedef struct UpVal { | 207 | typedef struct UpVal { |
208 | union GCObject *next; /* pointer to next object */ | ||
209 | lu_byte tt; /* object type */ | ||
210 | lu_byte marked; /* GC informations */ | ||
166 | TObject *v; /* points to stack or to its own value */ | 211 | TObject *v; /* points to stack or to its own value */ |
167 | struct UpVal *next; | ||
168 | TObject value; /* the value (when closed) */ | 212 | TObject value; /* the value (when closed) */ |
169 | lu_byte marked; | ||
170 | } UpVal; | 213 | } UpVal; |
171 | 214 | ||
172 | 215 | ||
@@ -175,20 +218,22 @@ typedef struct UpVal { | |||
175 | */ | 218 | */ |
176 | 219 | ||
177 | typedef struct CClosure { | 220 | typedef struct CClosure { |
221 | union GCObject *next; /* pointer to next object */ | ||
222 | lu_byte tt; /* object type */ | ||
223 | lu_byte marked; /* GC informations */ | ||
178 | lu_byte isC; /* 0 for Lua functions, 1 for C functions */ | 224 | lu_byte isC; /* 0 for Lua functions, 1 for C functions */ |
179 | lu_byte nupvalues; | 225 | lu_byte nupvalues; |
180 | lu_byte marked; | ||
181 | union Closure *next; | ||
182 | lua_CFunction f; | 226 | lua_CFunction f; |
183 | TObject upvalue[1]; | 227 | TObject upvalue[1]; |
184 | } CClosure; | 228 | } CClosure; |
185 | 229 | ||
186 | 230 | ||
187 | typedef struct LClosure { | 231 | typedef struct LClosure { |
232 | union GCObject *next; /* pointer to next object */ | ||
233 | lu_byte tt; /* object type */ | ||
234 | lu_byte marked; /* GC informations */ | ||
188 | lu_byte isC; | 235 | lu_byte isC; |
189 | lu_byte nupvalues; | 236 | lu_byte nupvalues; /* first five fields must be equal to CClosure!! */ |
190 | lu_byte marked; | ||
191 | union Closure *next; /* first four fields must be equal to CClosure!! */ | ||
192 | struct Proto *p; | 237 | struct Proto *p; |
193 | TObject g; /* global table for this closure */ | 238 | TObject g; /* global table for this closure */ |
194 | UpVal *upvals[1]; | 239 | UpVal *upvals[1]; |
@@ -217,17 +262,18 @@ typedef struct Node { | |||
217 | 262 | ||
218 | 263 | ||
219 | typedef struct Table { | 264 | typedef struct Table { |
265 | union GCObject *next; /* pointer to next object */ | ||
266 | lu_byte tt; /* object type */ | ||
267 | lu_byte marked; /* GC informations */ | ||
268 | lu_byte flags; /* 1<<p means tagmethod(p) is not present */ | ||
269 | lu_byte mode; | ||
270 | lu_byte lsizenode; /* log2 of size of `node' array */ | ||
220 | struct Table *metatable; | 271 | struct Table *metatable; |
221 | TObject *array; /* array part */ | 272 | TObject *array; /* array part */ |
222 | Node *node; | 273 | Node *node; |
223 | Node *firstfree; /* this position is free; all positions after it are full */ | 274 | Node *firstfree; /* this position is free; all positions after it are full */ |
224 | struct Table *next; | ||
225 | struct Table *gclist; | 275 | struct Table *gclist; |
226 | int sizearray; /* size of `array' array */ | 276 | int sizearray; /* size of `array' array */ |
227 | lu_byte flags; /* 1<<p means tagmethod(p) is not present */ | ||
228 | lu_byte lsizenode; /* log2 of size of `node' array */ | ||
229 | lu_byte mode; | ||
230 | lu_byte marked; | ||
231 | } Table; | 277 | } Table; |
232 | 278 | ||
233 | /* bit masks for `mode' */ | 279 | /* bit masks for `mode' */ |
@@ -247,6 +293,19 @@ typedef struct Table { | |||
247 | #define sizearray(t) ((t)->sizearray) | 293 | #define sizearray(t) ((t)->sizearray) |
248 | 294 | ||
249 | 295 | ||
296 | /* | ||
297 | ** Union of all collectable objects | ||
298 | */ | ||
299 | typedef union GCObject { | ||
300 | GCheader gch; | ||
301 | union TString ts; | ||
302 | union Udata u; | ||
303 | union Closure cl; | ||
304 | struct Table h; | ||
305 | struct Proto p; | ||
306 | struct UpVal uv; | ||
307 | } GCObject; | ||
308 | |||
250 | 309 | ||
251 | extern const TObject luaO_nilobject; | 310 | extern const TObject luaO_nilobject; |
252 | 311 | ||