diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-10-06 09:45:25 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-10-06 09:45:25 -0300 |
commit | ad3816d0d15b9e681a39dfe90b78ecd8e68aaec7 (patch) | |
tree | b79618aaceaa71a89f2dfe4a571d34ed55d78c83 /ldo.c | |
parent | 046a3d6173792b7d4d4d26a4e063e2fe383c10a7 (diff) | |
download | lua-ad3816d0d15b9e681a39dfe90b78ecd8e68aaec7.tar.gz lua-ad3816d0d15b9e681a39dfe90b78ecd8e68aaec7.tar.bz2 lua-ad3816d0d15b9e681a39dfe90b78ecd8e68aaec7.zip |
luaD_call is more uniform
Diffstat (limited to 'ldo.c')
-rw-r--r-- | ldo.c | 46 |
1 files changed, 25 insertions, 21 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldo.c,v 1.102 2000/10/05 12:14:08 roberto Exp roberto $ | 2 | ** $Id: ldo.c,v 1.103 2000/10/05 13:00:17 roberto Exp roberto $ |
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 | */ |
@@ -41,7 +41,7 @@ void luaD_init (lua_State *L, int stacksize) { | |||
41 | 41 | ||
42 | 42 | ||
43 | void luaD_checkstack (lua_State *L, int n) { | 43 | void luaD_checkstack (lua_State *L, int n) { |
44 | if (L->stack_last-L->top <= n) { /* stack overflow? */ | 44 | if (L->stack_last - L->top <= n) { /* stack overflow? */ |
45 | if (L->stack_last-L->stack > (L->stacksize-1)) { | 45 | if (L->stack_last-L->stack > (L->stacksize-1)) { |
46 | /* overflow while handling overflow: do what?? */ | 46 | /* overflow while handling overflow: do what?? */ |
47 | L->top -= EXTRA_STACK; | 47 | L->top -= EXTRA_STACK; |
@@ -112,19 +112,19 @@ void luaD_lineHook (lua_State *L, StkId func, int line, lua_Hook linehook) { | |||
112 | } | 112 | } |
113 | 113 | ||
114 | 114 | ||
115 | void luaD_callHook (lua_State *L, StkId func, lua_Hook callhook, | 115 | static void luaD_callHook (lua_State *L, StkId func, lua_Hook callhook, |
116 | const char *event) { | 116 | const char *event) { |
117 | if (L->allowhooks) { | 117 | if (L->allowhooks) { |
118 | lua_Debug ar; | 118 | lua_Debug ar; |
119 | ar._func = func; | 119 | ar._func = func; |
120 | ar.event = event; | 120 | ar.event = event; |
121 | infovalue(func)->pc = NULL; /* function is not active */ | ||
121 | dohook(L, &ar, callhook); | 122 | dohook(L, &ar, callhook); |
122 | } | 123 | } |
123 | } | 124 | } |
124 | 125 | ||
125 | 126 | ||
126 | static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) { | 127 | static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) { |
127 | lua_Hook callhook = L->callhook; | ||
128 | int nup = cl->nupvalues; /* number of upvalues */ | 128 | int nup = cl->nupvalues; /* number of upvalues */ |
129 | StkId old_Cbase = L->Cbase; | 129 | StkId old_Cbase = L->Cbase; |
130 | int n; | 130 | int n; |
@@ -132,11 +132,7 @@ static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) { | |||
132 | luaD_checkstack(L, nup+LUA_MINSTACK); /* ensure minimum stack size */ | 132 | luaD_checkstack(L, nup+LUA_MINSTACK); /* ensure minimum stack size */ |
133 | for (n=0; n<nup; n++) /* copy upvalues as extra arguments */ | 133 | for (n=0; n<nup; n++) /* copy upvalues as extra arguments */ |
134 | *(L->top++) = cl->upvalue[n]; | 134 | *(L->top++) = cl->upvalue[n]; |
135 | if (callhook) | ||
136 | luaD_callHook(L, base-1, callhook, "call"); | ||
137 | n = (*cl->f.c)(L); /* do the actual call */ | 135 | n = (*cl->f.c)(L); /* do the actual call */ |
138 | if (callhook) /* same hook that was active at entry */ | ||
139 | luaD_callHook(L, base-1, callhook, "return"); | ||
140 | L->Cbase = old_Cbase; /* restore old C base */ | 136 | L->Cbase = old_Cbase; /* restore old C base */ |
141 | return L->top - n; /* return index of first result */ | 137 | return L->top - n; /* return index of first result */ |
142 | } | 138 | } |
@@ -159,6 +155,7 @@ void luaD_callTM (lua_State *L, Closure *f, int nParams, int nResults) { | |||
159 | ** The number of results is nResults, unless nResults=LUA_MULTRET. | 155 | ** The number of results is nResults, unless nResults=LUA_MULTRET. |
160 | */ | 156 | */ |
161 | void luaD_call (lua_State *L, StkId func, int nResults) { | 157 | void luaD_call (lua_State *L, StkId func, int nResults) { |
158 | lua_Hook callhook; | ||
162 | StkId firstResult; | 159 | StkId firstResult; |
163 | CallInfo ci; | 160 | CallInfo ci; |
164 | Closure *cl; | 161 | Closure *cl; |
@@ -175,22 +172,29 @@ void luaD_call (lua_State *L, StkId func, int nResults) { | |||
175 | ci.func = cl; | 172 | ci.func = cl; |
176 | infovalue(func) = &ci; | 173 | infovalue(func) = &ci; |
177 | ttype(func) = LUA_TMARK; | 174 | ttype(func) = LUA_TMARK; |
178 | if (cl->isC) | 175 | callhook = L->callhook; |
179 | firstResult = callCclosure(L, cl, func+1); | 176 | if (callhook) |
180 | else | 177 | luaD_callHook(L, func, callhook, "call"); |
181 | firstResult = luaV_execute(L, cl, func+1); | 178 | firstResult = (cl->isC ? callCclosure(L, cl, func+1) : |
179 | luaV_execute(L, cl, func+1)); | ||
180 | if (callhook) /* same hook that was active at entry */ | ||
181 | luaD_callHook(L, func, callhook, "return"); | ||
182 | LUA_ASSERT(ttype(func) == LUA_TMARK, "invalid tag"); | 182 | LUA_ASSERT(ttype(func) == LUA_TMARK, "invalid tag"); |
183 | /* adjust the number of results */ | ||
184 | if (nResults == LUA_MULTRET) | ||
185 | nResults = L->top - firstResult; | ||
186 | else | ||
187 | luaD_adjusttop(L, firstResult, nResults); | ||
188 | /* move results to `func' (to erase parameters and function) */ | 183 | /* move results to `func' (to erase parameters and function) */ |
189 | while (nResults) { | 184 | if (nResults == LUA_MULTRET) { |
190 | *func++ = *(L->top - nResults); | 185 | while (firstResult < L->top) /* copy all results */ |
191 | nResults--; | 186 | *func++ = *firstResult++; |
187 | L->top = func; | ||
188 | } | ||
189 | else { /* copy at most `nResults' */ | ||
190 | for (; nResults > 0 && firstResult < L->top; nResults--) | ||
191 | *func++ = *firstResult++; | ||
192 | L->top = func; | ||
193 | for (; nResults > 0; nResults--) { /* if there are not enough results */ | ||
194 | ttype(L->top) = LUA_TNIL; /* adjust the stack */ | ||
195 | incr_top; /* must check stack space */ | ||
196 | } | ||
192 | } | 197 | } |
193 | L->top = func; | ||
194 | luaC_checkGC(L); | 198 | luaC_checkGC(L); |
195 | } | 199 | } |
196 | 200 | ||