diff options
-rw-r--r-- | ldo.c | 36 | ||||
-rw-r--r-- | lgc.c | 16 | ||||
-rw-r--r-- | lstate.c | 3 |
3 files changed, 32 insertions, 23 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldo.c,v 1.154 2002/01/11 20:27:41 roberto Exp $ | 2 | ** $Id: ldo.c,v 1.160 2002/02/14 21:40:13 roberto Exp $ |
3 | ** Stack and Call structure of Lua | 3 | ** Stack and Call structure of Lua |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -86,7 +86,7 @@ void luaD_growstack (lua_State *L, int n) { | |||
86 | else { | 86 | else { |
87 | if (n <= L->stacksize && 2*L->stacksize < LUA_MAXSTACK) /* can double? */ | 87 | if (n <= L->stacksize && 2*L->stacksize < LUA_MAXSTACK) /* can double? */ |
88 | luaD_reallocstack(L, 2*L->stacksize); | 88 | luaD_reallocstack(L, 2*L->stacksize); |
89 | else if (L->stacksize+n <= LUA_MAXSTACK) /* no overflow? */ | 89 | else if ((L->top - L->stack) + n <= LUA_MAXSTACK) /* no overflow? */ |
90 | luaD_reallocstack(L, LUA_MAXSTACK); | 90 | luaD_reallocstack(L, LUA_MAXSTACK); |
91 | else { | 91 | else { |
92 | /* resize to maximum + some extra space to handle error */ | 92 | /* resize to maximum + some extra space to handle error */ |
@@ -108,7 +108,7 @@ static void luaD_openstack (lua_State *L, StkId pos) { | |||
108 | 108 | ||
109 | 109 | ||
110 | static void dohook (lua_State *L, lua_Debug *ar, lua_Hook hook) { | 110 | static void dohook (lua_State *L, lua_Debug *ar, lua_Hook hook) { |
111 | L->ci->top = L->top; | 111 | StkId top = L->top; |
112 | luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ | 112 | luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ |
113 | L->allowhooks = 0; /* cannot call hooks inside a hook */ | 113 | L->allowhooks = 0; /* cannot call hooks inside a hook */ |
114 | lua_unlock(L); | 114 | lua_unlock(L); |
@@ -116,7 +116,7 @@ static void dohook (lua_State *L, lua_Debug *ar, lua_Hook hook) { | |||
116 | lua_lock(L); | 116 | lua_lock(L); |
117 | lua_assert(L->allowhooks == 0); | 117 | lua_assert(L->allowhooks == 0); |
118 | L->allowhooks = 1; | 118 | L->allowhooks = 1; |
119 | L->top = L->ci->top; | 119 | L->top = top; |
120 | } | 120 | } |
121 | 121 | ||
122 | 122 | ||
@@ -137,6 +137,7 @@ static void luaD_callHook (lua_State *L, lua_Hook callhook, const char *event) { | |||
137 | ar.event = event; | 137 | ar.event = event; |
138 | ar._ci = L->ci - L->base_ci; | 138 | ar._ci = L->ci - L->base_ci; |
139 | L->ci->pc = NULL; /* function is not active */ | 139 | L->ci->pc = NULL; /* function is not active */ |
140 | L->ci->top = L->top + LUA_MINSTACK; | ||
140 | dohook(L, &ar, callhook); | 141 | dohook(L, &ar, callhook); |
141 | } | 142 | } |
142 | } | 143 | } |
@@ -186,6 +187,19 @@ static void adjust_varargs (lua_State *L, int nfixargs) { | |||
186 | } | 187 | } |
187 | 188 | ||
188 | 189 | ||
190 | static StkId tryfuncTM (lua_State *L, StkId func) { | ||
191 | const TObject *tm = luaT_gettmbyobj(L, func, TM_CALL); | ||
192 | if (ttype(tm) != LUA_TFUNCTION) { | ||
193 | L->ci--; /* undo increment (no function here) */ | ||
194 | luaG_typeerror(L, func, "call"); | ||
195 | } | ||
196 | luaD_openstack(L, func); | ||
197 | func = L->ci->base - 1; /* previous call may change stack */ | ||
198 | setobj(func, tm); /* tag method is the new function to be called */ | ||
199 | return func; | ||
200 | } | ||
201 | |||
202 | |||
189 | StkId luaD_precall (lua_State *L, StkId func) { | 203 | StkId luaD_precall (lua_State *L, StkId func) { |
190 | CallInfo *ci; | 204 | CallInfo *ci; |
191 | LClosure *cl; | 205 | LClosure *cl; |
@@ -193,17 +207,8 @@ StkId luaD_precall (lua_State *L, StkId func) { | |||
193 | ci = L->ci; | 207 | ci = L->ci; |
194 | ci->base = func+1; | 208 | ci->base = func+1; |
195 | ci->pc = NULL; | 209 | ci->pc = NULL; |
196 | if (ttype(func) != LUA_TFUNCTION) { | 210 | if (ttype(func) != LUA_TFUNCTION) /* `func' is not a function? */ |
197 | /* `func' is not a function; check the `function' tag method */ | 211 | func = tryfuncTM(L, func); /* check the `function' tag method */ |
198 | const TObject *tm = luaT_gettmbyobj(L, func, TM_CALL); | ||
199 | if (ttype(tm) != LUA_TFUNCTION) { | ||
200 | L->ci--; /* undo increment (no function here) */ | ||
201 | luaG_typeerror(L, func, "call"); | ||
202 | } | ||
203 | luaD_openstack(L, func); | ||
204 | func = ci->base - 1; /* previous call may change stack */ | ||
205 | setobj(func, tm); /* tag method is the new function to be called */ | ||
206 | } | ||
207 | cl = &clvalue(func)->l; | 212 | cl = &clvalue(func)->l; |
208 | if (L->callhook) { | 213 | if (L->callhook) { |
209 | luaD_callHook(L, L->callhook, "call"); | 214 | luaD_callHook(L, L->callhook, "call"); |
@@ -226,6 +231,7 @@ StkId luaD_precall (lua_State *L, StkId func) { | |||
226 | else { /* if is a C function, call it */ | 231 | else { /* if is a C function, call it */ |
227 | int n; | 232 | int n; |
228 | luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ | 233 | luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ |
234 | ci->top = L->top + LUA_MINSTACK; | ||
229 | lua_unlock(L); | 235 | lua_unlock(L); |
230 | #if LUA_COMPATUPVALUES | 236 | #if LUA_COMPATUPVALUES |
231 | lua_pushupvalues(L); | 237 | lua_pushupvalues(L); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lgc.c,v 1.128 2002/03/04 21:32:34 roberto Exp roberto $ | 2 | ** $Id: lgc.c,v 1.129 2002/03/05 16:22:54 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 | */ |
@@ -122,12 +122,12 @@ static void reallymarkobject (GCState *st, TObject *o) { | |||
122 | } | 122 | } |
123 | 123 | ||
124 | 124 | ||
125 | static void checkstacksizes (lua_State *L) { | 125 | static void checkstacksizes (lua_State *L, StkId max) { |
126 | int used = L->ci - L->base_ci; /* number of `ci' in use */ | 126 | int used = L->ci - L->base_ci; /* number of `ci' in use */ |
127 | if (4*used < L->size_ci && 2*BASIC_CI_SIZE < L->size_ci) | 127 | if (4*used < L->size_ci && 2*BASIC_CI_SIZE < L->size_ci) |
128 | luaD_reallocCI(L, L->size_ci/2); /* still big enough... */ | 128 | luaD_reallocCI(L, L->size_ci/2); /* still big enough... */ |
129 | used = L->top - L->stack; /* part of stack in use */ | 129 | used = max - L->stack; /* part of stack in use */ |
130 | if (2*(used+MAXSTACK) < L->stacksize && 2*BASIC_STACK_SIZE < L->stacksize) | 130 | if (4*used < L->stacksize && 2*BASIC_STACK_SIZE < L->stacksize) |
131 | luaD_reallocstack(L, L->stacksize/2); /* still big enough... */ | 131 | luaD_reallocstack(L, L->stacksize/2); /* still big enough... */ |
132 | } | 132 | } |
133 | 133 | ||
@@ -136,6 +136,7 @@ static void markstacks (GCState *st) { | |||
136 | lua_State *L1 = st->L; | 136 | lua_State *L1 = st->L; |
137 | do { /* for each thread */ | 137 | do { /* for each thread */ |
138 | StkId o, lim; | 138 | StkId o, lim; |
139 | CallInfo *ci; | ||
139 | if (L1->base_ci == NULL) { /* incomplete state? */ | 140 | if (L1->base_ci == NULL) { /* incomplete state? */ |
140 | lua_assert(L1 != st->L); | 141 | lua_assert(L1 != st->L); |
141 | L1 = L1->next; | 142 | L1 = L1->next; |
@@ -144,10 +145,11 @@ static void markstacks (GCState *st) { | |||
144 | } | 145 | } |
145 | for (o=L1->stack; o<L1->top; o++) | 146 | for (o=L1->stack; o<L1->top; o++) |
146 | markobject(st, o); | 147 | markobject(st, o); |
147 | lim = (L1->stack_last - L1->ci->base > MAXSTACK) ? L1->ci->base+MAXSTACK | 148 | lim = o; |
148 | : L1->stack_last; | 149 | for (ci = L1->base_ci; ci <= L1->ci; ci++) |
150 | if (lim < ci->top) lim = ci->top; | ||
149 | for (; o<=lim; o++) setnilvalue(o); | 151 | for (; o<=lim; o++) setnilvalue(o); |
150 | checkstacksizes(L1); | 152 | checkstacksizes(L1, lim); |
151 | lua_assert(L1->previous->next == L1 && L1->next->previous == L1); | 153 | lua_assert(L1->previous->next == L1 && L1->next->previous == L1); |
152 | L1 = L1->next; | 154 | L1 = L1->next; |
153 | } while (L1 != st->L); | 155 | } while (L1 != st->L); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstate.c,v 1.84 2002/02/14 21:40:13 roberto Exp roberto $ | 2 | ** $Id: lstate.c,v 1.85 2002/03/05 16:22:54 roberto Exp roberto $ |
3 | ** Global State | 3 | ** Global State |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -33,6 +33,7 @@ static void stack_init (lua_State *L, lua_State *OL) { | |||
33 | L->base_ci = luaM_newvector(OL, BASIC_CI_SIZE, CallInfo); | 33 | L->base_ci = luaM_newvector(OL, BASIC_CI_SIZE, CallInfo); |
34 | L->ci = L->base_ci; | 34 | L->ci = L->base_ci; |
35 | L->ci->base = L->top; | 35 | L->ci->base = L->top; |
36 | L->ci->top = L->top; | ||
36 | L->ci->pc = NULL; | 37 | L->ci->pc = NULL; |
37 | L->size_ci = BASIC_CI_SIZE; | 38 | L->size_ci = BASIC_CI_SIZE; |
38 | L->end_ci = L->base_ci + L->size_ci; | 39 | L->end_ci = L->base_ci + L->size_ci; |