diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-03-07 15:14:29 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-03-07 15:14:29 -0300 |
commit | 8f837e83b20f3c409ba187765c2bf5aefc111923 (patch) | |
tree | fdd9128cd1401155708f6f31df9dea4e0f0d51ad /ldo.c | |
parent | 6658b9588faa3dae1747f2b705e99e96c6cf0e31 (diff) | |
download | lua-8f837e83b20f3c409ba187765c2bf5aefc111923.tar.gz lua-8f837e83b20f3c409ba187765c2bf5aefc111923.tar.bz2 lua-8f837e83b20f3c409ba187765c2bf5aefc111923.zip |
using `ci->top' to control acceptable indices in C calls
Diffstat (limited to 'ldo.c')
-rw-r--r-- | ldo.c | 36 |
1 files changed, 21 insertions, 15 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); |