diff options
-rw-r--r-- | lgc.c | 114 | ||||
-rw-r--r-- | lvm.c | 72 |
2 files changed, 97 insertions, 89 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lgc.c,v 1.103 2001/06/12 18:43:13 roberto Exp roberto $ | 2 | ** $Id: lgc.c,v 1.104 2001/06/13 18:51:20 roberto Exp roberto $ |
3 | ** Garbage Collector | 3 | ** Garbage Collector |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -62,7 +62,7 @@ static void markclosure (GCState *st, Closure *cl) { | |||
62 | 62 | ||
63 | static void marktable (GCState *st, Hash *h) { | 63 | static void marktable (GCState *st, Hash *h) { |
64 | if (!ismarked(h)) { | 64 | if (!ismarked(h)) { |
65 | h->mark = st->tmark; /* chain it in list of marked */ | 65 | h->mark = st->tmark; /* chain it for later traversal */ |
66 | st->tmark = h; | 66 | st->tmark = h; |
67 | } | 67 | } |
68 | } | 68 | } |
@@ -127,8 +127,9 @@ static void traverseclosure (GCState *st, Closure *f) { | |||
127 | 127 | ||
128 | 128 | ||
129 | static void removekey (Node *n) { | 129 | static void removekey (Node *n) { |
130 | lua_assert(ttype(val(n)) == LUA_TNIL); | ||
130 | if (ttype_key(n) != LUA_TNIL && ttype_key(n) != LUA_TNUMBER) | 131 | if (ttype_key(n) != LUA_TNIL && ttype_key(n) != LUA_TNUMBER) |
131 | n->key_value.ts = NULL; /* dead key; remove it */ | 132 | ttype_key(n) = LUA_TNONE; /* dead key; remove it */ |
132 | } | 133 | } |
133 | 134 | ||
134 | 135 | ||
@@ -180,30 +181,29 @@ static void markall (lua_State *L) { | |||
180 | } | 181 | } |
181 | 182 | ||
182 | 183 | ||
183 | static int hasmark (const TObject *o) { | 184 | static int hasmark (int tt, Value *v) { |
184 | switch (ttype(o)) { | 185 | switch (tt) { |
185 | case LUA_TSTRING: | 186 | case LUA_TSTRING: |
186 | return tsvalue(o)->marked; | 187 | return v->ts->marked; |
187 | case LUA_TUSERDATA: | 188 | case LUA_TUSERDATA: |
188 | return ismarkedudata(uvalue(o)); | 189 | return ismarkedudata(v->u); |
189 | case LUA_TTABLE: | 190 | case LUA_TTABLE: |
190 | return ismarked(hvalue(o)); | 191 | return ismarked(v->h); |
191 | case LUA_TFUNCTION: | 192 | case LUA_TFUNCTION: |
192 | return ismarked(clvalue(o)); | 193 | return ismarked(v->cl); |
193 | default: /* number, nil */ | 194 | default: /* number, nil */ |
194 | return 1; | 195 | return 1; |
195 | } | 196 | } |
196 | } | 197 | } |
197 | 198 | ||
198 | 199 | ||
199 | static void invalidatetable (Hash *h) { | 200 | static void cleardeadnodes (Hash *h) { |
200 | int i; | 201 | int i; |
201 | for (i=0; i<h->size; i++) { | 202 | for (i=0; i<h->size; i++) { |
202 | Node *n = node(h, i); | 203 | Node *n = node(h, i); |
203 | TObject k; | ||
204 | if (ttype(val(n)) == LUA_TNIL) continue; /* empty node */ | 204 | if (ttype(val(n)) == LUA_TNIL) continue; /* empty node */ |
205 | setkey2obj(&k, n); | 205 | if (!hasmark(ttype(val(n)), &(val(n)->value)) || |
206 | if (!hasmark(val(n)) || !hasmark(&k)) { | 206 | !hasmark(ttype_key(n), &n->key_value)) { |
207 | setnilvalue(val(n)); /* remove value */ | 207 | setnilvalue(val(n)); /* remove value */ |
208 | removekey(n); | 208 | removekey(n); |
209 | } | 209 | } |
@@ -211,27 +211,26 @@ static void invalidatetable (Hash *h) { | |||
211 | } | 211 | } |
212 | 212 | ||
213 | 213 | ||
214 | static void invalidatetables (global_State *G) { | 214 | static void cleartables (global_State *G) { |
215 | Hash *h; | 215 | Hash *h; |
216 | for (h = G->roottable; h; h = h->next) { | 216 | for (h = G->roottable; h; h = h->next) { |
217 | if (ismarked(h) && h->weakmode) | 217 | if (h->weakmode && ismarked(h)) |
218 | invalidatetable(h); | 218 | cleardeadnodes(h); |
219 | } | 219 | } |
220 | } | 220 | } |
221 | 221 | ||
222 | 222 | ||
223 | |||
224 | static void collectproto (lua_State *L) { | 223 | static void collectproto (lua_State *L) { |
225 | Proto **p = &G(L)->rootproto; | 224 | Proto **p = &G(L)->rootproto; |
226 | Proto *next; | 225 | Proto *curr; |
227 | while ((next = *p) != NULL) { | 226 | while ((curr = *p) != NULL) { |
228 | if (next->marked) { | 227 | if (curr->marked) { |
229 | next->marked = 0; | 228 | curr->marked = 0; |
230 | p = &next->next; | 229 | p = &curr->next; |
231 | } | 230 | } |
232 | else { | 231 | else { |
233 | *p = next->next; | 232 | *p = curr->next; |
234 | luaF_freeproto(L, next); | 233 | luaF_freeproto(L, curr); |
235 | } | 234 | } |
236 | } | 235 | } |
237 | } | 236 | } |
@@ -239,15 +238,15 @@ static void collectproto (lua_State *L) { | |||
239 | 238 | ||
240 | static void collectclosure (lua_State *L) { | 239 | static void collectclosure (lua_State *L) { |
241 | Closure **p = &G(L)->rootcl; | 240 | Closure **p = &G(L)->rootcl; |
242 | Closure *next; | 241 | Closure *curr; |
243 | while ((next = *p) != NULL) { | 242 | while ((curr = *p) != NULL) { |
244 | if (ismarked(next)) { | 243 | if (ismarked(curr)) { |
245 | next->mark = next; /* unmark */ | 244 | curr->mark = curr; /* unmark */ |
246 | p = &next->next; | 245 | p = &curr->next; |
247 | } | 246 | } |
248 | else { | 247 | else { |
249 | *p = next->next; | 248 | *p = curr->next; |
250 | luaF_freeclosure(L, next); | 249 | luaF_freeclosure(L, curr); |
251 | } | 250 | } |
252 | } | 251 | } |
253 | } | 252 | } |
@@ -255,15 +254,15 @@ static void collectclosure (lua_State *L) { | |||
255 | 254 | ||
256 | static void collecttable (lua_State *L) { | 255 | static void collecttable (lua_State *L) { |
257 | Hash **p = &G(L)->roottable; | 256 | Hash **p = &G(L)->roottable; |
258 | Hash *next; | 257 | Hash *curr; |
259 | while ((next = *p) != NULL) { | 258 | while ((curr = *p) != NULL) { |
260 | if (ismarked(next)) { | 259 | if (ismarked(curr)) { |
261 | next->mark = next; /* unmark */ | 260 | curr->mark = curr; /* unmark */ |
262 | p = &next->next; | 261 | p = &curr->next; |
263 | } | 262 | } |
264 | else { | 263 | else { |
265 | *p = next->next; | 264 | *p = curr->next; |
266 | luaH_free(L, next); | 265 | luaH_free(L, curr); |
267 | } | 266 | } |
268 | } | 267 | } |
269 | } | 268 | } |
@@ -271,17 +270,17 @@ static void collecttable (lua_State *L) { | |||
271 | 270 | ||
272 | void luaC_collectudata (lua_State *L) { | 271 | void luaC_collectudata (lua_State *L) { |
273 | Udata **p = &G(L)->rootudata; | 272 | Udata **p = &G(L)->rootudata; |
274 | Udata *next; | 273 | Udata *curr; |
275 | while ((next = *p) != NULL) { | 274 | while ((curr = *p) != NULL) { |
276 | if (ismarkedudata(next)) { | 275 | if (ismarkedudata(curr)) { |
277 | switchudatamark(next); /* unmark */ | 276 | switchudatamark(curr); /* unmark */ |
278 | p = &next->next; | 277 | p = &curr->next; |
279 | } | 278 | } |
280 | else { /* collect */ | 279 | else { /* collect */ |
281 | int tag = next->tag; | 280 | int tag = curr->tag; |
282 | *p = next->next; | 281 | *p = curr->next; |
283 | next->next = G(L)->TMtable[tag].collected; /* chain udata */ | 282 | curr->next = G(L)->TMtable[tag].collected; /* chain udata */ |
284 | G(L)->TMtable[tag].collected = next; | 283 | G(L)->TMtable[tag].collected = curr; |
285 | } | 284 | } |
286 | } | 285 | } |
287 | } | 286 | } |
@@ -291,17 +290,17 @@ static void collectstrings (lua_State *L, int all) { | |||
291 | int i; | 290 | int i; |
292 | for (i=0; i<G(L)->strt.size; i++) { /* for each list */ | 291 | for (i=0; i<G(L)->strt.size; i++) { /* for each list */ |
293 | TString **p = &G(L)->strt.hash[i]; | 292 | TString **p = &G(L)->strt.hash[i]; |
294 | TString *next; | 293 | TString *curr; |
295 | while ((next = *p) != NULL) { | 294 | while ((curr = *p) != NULL) { |
296 | if (next->marked && !all) { /* preserve? */ | 295 | if (curr->marked && !all) { /* preserve? */ |
297 | if (next->marked < FIXMARK) /* does not change FIXMARKs */ | 296 | if (curr->marked < FIXMARK) /* does not change FIXMARKs */ |
298 | next->marked = 0; | 297 | curr->marked = 0; |
299 | p = &next->nexthash; | 298 | p = &curr->nexthash; |
300 | } | 299 | } |
301 | else { /* collect */ | 300 | else { /* collect */ |
302 | *p = next->nexthash; | 301 | *p = curr->nexthash; |
303 | G(L)->strt.nuse--; | 302 | G(L)->strt.nuse--; |
304 | luaM_free(L, next, sizestring(next->len)); | 303 | luaM_free(L, curr, sizestring(curr->len)); |
305 | } | 304 | } |
306 | } | 305 | } |
307 | } | 306 | } |
@@ -365,11 +364,12 @@ void luaC_collect (lua_State *L, int all) { | |||
365 | 364 | ||
366 | void luaC_collectgarbage (lua_State *L) { | 365 | void luaC_collectgarbage (lua_State *L) { |
367 | markall(L); | 366 | markall(L); |
368 | invalidatetables(G(L)); | 367 | cleartables(G(L)); |
369 | luaC_collect(L, 0); | 368 | luaC_collect(L, 0); |
370 | checkMbuffer(L); | 369 | checkMbuffer(L); |
371 | G(L)->GCthreshold = 2*G(L)->nblocks; /* set new threshold */ | 370 | G(L)->GCthreshold = 2*G(L)->nblocks; /* temporary threshold (for TM) */ |
372 | luaC_callgcTMudata(L); | 371 | luaC_callgcTMudata(L); |
372 | G(L)->GCthreshold = 2*G(L)->nblocks; /* new threshold */ | ||
373 | callgcTM(L, &luaO_nilobject); | 373 | callgcTM(L, &luaO_nilobject); |
374 | } | 374 | } |
375 | 375 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.c,v 1.183 2001/06/08 19:20:02 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 1.184 2001/06/11 14:56:42 roberto Exp roberto $ |
3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -106,17 +106,17 @@ void luaV_Lclosure (lua_State *L, Proto *l, int nelems) { | |||
106 | } | 106 | } |
107 | 107 | ||
108 | 108 | ||
109 | static void callTM (lua_State *L, Closure *f, const l_char *fmt, ...) { | 109 | /* maximum stack used by a call to a tag method (func + args) */ |
110 | #define MAXSTACK_TM 4 | ||
111 | |||
112 | static StkId callTM (lua_State *L, Closure *f, const l_char *fmt, ...) { | ||
110 | va_list argp; | 113 | va_list argp; |
111 | StkId base = L->top; | 114 | StkId base = L->top; |
112 | StkId result = NULL; /* result position */ | 115 | lua_assert(strlen(fmt)+1 <= MAXSTACK_TM); |
116 | luaD_checkstack(L, MAXSTACK_TM); | ||
113 | va_start(argp, fmt); | 117 | va_start(argp, fmt); |
114 | setclvalue(L->top, f); /* push function */ | 118 | setclvalue(L->top, f); /* push function */ |
115 | incr_top; | 119 | L->top++; |
116 | if (*fmt == l_c('r')) { | ||
117 | fmt++; | ||
118 | result = va_arg(argp, TObject *); /* result position */ | ||
119 | } | ||
120 | while (*fmt) { | 120 | while (*fmt) { |
121 | if (*fmt++ == l_c('o')) { | 121 | if (*fmt++ == l_c('o')) { |
122 | setobj(L->top, va_arg(argp, TObject *)); | 122 | setobj(L->top, va_arg(argp, TObject *)); |
@@ -125,17 +125,24 @@ static void callTM (lua_State *L, Closure *f, const l_char *fmt, ...) { | |||
125 | lua_assert(*(fmt-1) == l_c('s')); | 125 | lua_assert(*(fmt-1) == l_c('s')); |
126 | setsvalue(L->top, va_arg(argp, TString *)); | 126 | setsvalue(L->top, va_arg(argp, TString *)); |
127 | } | 127 | } |
128 | incr_top; | 128 | L->top++; |
129 | } | 129 | } |
130 | luaD_call(L, base); | 130 | luaD_call(L, base); |
131 | if (result) { /* need result? */ | 131 | va_end(argp); |
132 | if (L->top == base) /* are there valid results? */ | 132 | return base; |
133 | setnilvalue(result); /* function had no results */ | 133 | } |
134 | else | 134 | |
135 | setobj(result, base); /* get first result */ | 135 | |
136 | #define setTM(L, base) (L->top = (base)) | ||
137 | |||
138 | static void setTMresult (lua_State *L, TObject *result, StkId base) { | ||
139 | if (L->top == base) { /* are there valid results? */ | ||
140 | setnilvalue(result); /* function had no results */ | ||
141 | } | ||
142 | else { | ||
143 | setobj(result, base); /* get first result */ | ||
136 | } | 144 | } |
137 | L->top = base; /* restore top */ | 145 | L->top = base; /* restore top */ |
138 | va_end(argp); | ||
139 | } | 146 | } |
140 | 147 | ||
141 | 148 | ||
@@ -164,7 +171,7 @@ void luaV_gettable (lua_State *L, StkId t, TObject *key, StkId res) { | |||
164 | if (tm == NULL) /* no tag method? */ | 171 | if (tm == NULL) /* no tag method? */ |
165 | luaG_typeerror(L, t, l_s("index")); | 172 | luaG_typeerror(L, t, l_s("index")); |
166 | } | 173 | } |
167 | callTM(L, tm, l_s("roo"), res, t, key); | 174 | setTMresult(L, res, callTM(L, tm, l_s("oo"), t, key)); |
168 | } | 175 | } |
169 | 176 | ||
170 | 177 | ||
@@ -187,7 +194,7 @@ void luaV_settable (lua_State *L, StkId t, TObject *key, StkId val) { | |||
187 | if (tm == NULL) /* no tag method? */ | 194 | if (tm == NULL) /* no tag method? */ |
188 | luaG_typeerror(L, t, l_s("index")); | 195 | luaG_typeerror(L, t, l_s("index")); |
189 | } | 196 | } |
190 | callTM(L, tm, l_s("ooo"), t, key, val); | 197 | setTM(L, callTM(L, tm, l_s("ooo"), t, key, val)); |
191 | } | 198 | } |
192 | 199 | ||
193 | 200 | ||
@@ -197,8 +204,9 @@ void luaV_getglobal (lua_State *L, TString *name, StkId res) { | |||
197 | if (!HAS_TM_GETGLOBAL(L, ttype(value)) || /* is there a tag method? */ | 204 | if (!HAS_TM_GETGLOBAL(L, ttype(value)) || /* is there a tag method? */ |
198 | (tm = luaT_gettmbyObj(G(L), value, TM_GETGLOBAL)) == NULL) { | 205 | (tm = luaT_gettmbyObj(G(L), value, TM_GETGLOBAL)) == NULL) { |
199 | setobj(res, value); /* default behavior */ | 206 | setobj(res, value); /* default behavior */ |
200 | } else | 207 | } |
201 | callTM(L, tm, l_s("rso"), res, name, value); | 208 | else |
209 | setTMresult(L, res, callTM(L, tm, l_s("so"), name, value)); | ||
202 | } | 210 | } |
203 | 211 | ||
204 | 212 | ||
@@ -208,8 +216,9 @@ void luaV_setglobal (lua_State *L, TString *name, StkId val) { | |||
208 | if (!HAS_TM_SETGLOBAL(L, ttype(oldvalue)) || /* no tag methods? */ | 216 | if (!HAS_TM_SETGLOBAL(L, ttype(oldvalue)) || /* no tag methods? */ |
209 | (tm = luaT_gettmbyObj(G(L), oldvalue, TM_SETGLOBAL)) == NULL) { | 217 | (tm = luaT_gettmbyObj(G(L), oldvalue, TM_SETGLOBAL)) == NULL) { |
210 | setobj(oldvalue, val); /* raw set */ | 218 | setobj(oldvalue, val); /* raw set */ |
211 | } else | 219 | } |
212 | callTM(L, tm, l_s("soo"), name, oldvalue, val); | 220 | else |
221 | setTM(L, callTM(L, tm, l_s("soo"), name, oldvalue, val)); | ||
213 | } | 222 | } |
214 | 223 | ||
215 | 224 | ||
@@ -226,7 +235,7 @@ static int call_binTM (lua_State *L, const TObject *p1, const TObject *p2, | |||
226 | } | 235 | } |
227 | } | 236 | } |
228 | opname = luaS_new(L, luaT_eventname[event]); | 237 | opname = luaS_new(L, luaT_eventname[event]); |
229 | callTM(L, tm, l_s("roos"), res, p1, p2, opname); | 238 | setTMresult(L, res, callTM(L, tm, l_s("oos"), p1, p2, opname)); |
230 | return 1; | 239 | return 1; |
231 | } | 240 | } |
232 | 241 | ||
@@ -566,15 +575,16 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { | |||
566 | return ra; | 575 | return ra; |
567 | } | 576 | } |
568 | case OP_FORPREP: { | 577 | case OP_FORPREP: { |
569 | int jmp = GETARG_sBc(i); | ||
570 | if (luaV_tonumber(ra, ra) == NULL) | 578 | if (luaV_tonumber(ra, ra) == NULL) |
571 | luaD_error(L, l_s("`for' initial value must be a number")); | 579 | luaD_error(L, l_s("`for' initial value must be a number")); |
572 | if (luaV_tonumber(ra+1, ra+1) == NULL) | 580 | if (luaV_tonumber(ra+1, ra+1) == NULL) |
573 | luaD_error(L, l_s("`for' limit must be a number")); | 581 | luaD_error(L, l_s("`for' limit must be a number")); |
574 | if (luaV_tonumber(ra+2, ra+2) == NULL) | 582 | if (luaV_tonumber(ra+2, ra+2) == NULL) |
575 | luaD_error(L, l_s("`for' step must be a number")); | 583 | luaD_error(L, l_s("`for' step must be a number")); |
576 | pc += -jmp; /* `jump' to loop end (delta is negated here) */ | ||
577 | nvalue(ra) -= nvalue(ra+2);/* decrement index (to be incremented) */ | 584 | nvalue(ra) -= nvalue(ra+2);/* decrement index (to be incremented) */ |
585 | pc += -GETARG_sBc(i); /* `jump' to loop end (delta is negated here) */ | ||
586 | /* store in `ra+1' total number of repetitions */ | ||
587 | nvalue(ra+1) = ((nvalue(ra+1)-nvalue(ra))/nvalue(ra+2)); | ||
578 | /* go through */ | 588 | /* go through */ |
579 | } | 589 | } |
580 | case OP_FORLOOP: { | 590 | case OP_FORLOOP: { |
@@ -582,28 +592,26 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { | |||
582 | ttype(ra+2) == LUA_TNUMBER); | 592 | ttype(ra+2) == LUA_TNUMBER); |
583 | if (ttype(ra) != LUA_TNUMBER) | 593 | if (ttype(ra) != LUA_TNUMBER) |
584 | luaD_error(L, l_s("`for' index must be a number")); | 594 | luaD_error(L, l_s("`for' index must be a number")); |
585 | nvalue(ra) += nvalue(ra+2); /* increment index */ | 595 | if (--nvalue(ra+1) >= 0) { |
586 | if (nvalue(ra+2) > 0 ? | 596 | nvalue(ra) += nvalue(ra+2); /* increment index */ |
587 | nvalue(ra) <= nvalue(ra+1) : | ||
588 | nvalue(ra) >= nvalue(ra+1)) | ||
589 | dojump(pc, i); /* repeat loop */ | 597 | dojump(pc, i); /* repeat loop */ |
598 | } | ||
590 | break; | 599 | break; |
591 | } | 600 | } |
592 | case OP_TFORPREP: { | 601 | case OP_TFORPREP: { |
593 | int jmp = GETARG_sBc(i); | ||
594 | if (ttype(ra) != LUA_TTABLE) | 602 | if (ttype(ra) != LUA_TTABLE) |
595 | luaD_error(L, l_s("`for' table must be a table")); | 603 | luaD_error(L, l_s("`for' table must be a table")); |
596 | setnvalue(ra+1, -1); /* initial index */ | 604 | setnvalue(ra+1, -1); /* initial index */ |
597 | setnilvalue(ra+2); | 605 | setnilvalue(ra+2); |
598 | setnilvalue(ra+3); | 606 | setnilvalue(ra+3); |
599 | pc += -jmp; /* `jump' to loop end (delta is negated here) */ | 607 | pc += -GETARG_sBc(i); /* `jump' to loop end (delta is negated here) */ |
600 | /* go through */ | 608 | /* go through */ |
601 | } | 609 | } |
602 | case OP_TFORLOOP: { | 610 | case OP_TFORLOOP: { |
603 | Hash *t; | 611 | Hash *t; |
604 | int n; | 612 | int n; |
605 | runtime_check(L, ttype(ra) == LUA_TTABLE); | 613 | runtime_check(L, ttype(ra) == LUA_TTABLE && |
606 | runtime_check(L, ttype(ra+1) == LUA_TNUMBER); | 614 | ttype(ra+1) == LUA_TNUMBER); |
607 | t = hvalue(ra); | 615 | t = hvalue(ra); |
608 | n = (int)nvalue(ra+1); | 616 | n = (int)nvalue(ra+1); |
609 | n = luaH_nexti(t, n); | 617 | n = luaH_nexti(t, n); |