diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-03-29 17:19:20 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-03-29 17:19:20 -0300 |
commit | a69356e9e0a7525b1cebadc928a0efcce8c39b46 (patch) | |
tree | c676ee2997c699d3e0b036323ecbafa7ea0d786f /ldo.c | |
parent | b53dc0c4853c56694dda727793e5f6188de39dd8 (diff) | |
download | lua-a69356e9e0a7525b1cebadc928a0efcce8c39b46.tar.gz lua-a69356e9e0a7525b1cebadc928a0efcce8c39b46.tar.bz2 lua-a69356e9e0a7525b1cebadc928a0efcce8c39b46.zip |
no more special cases for closures with 0 upvalues (performance is the same,
memory use a little higher, code much simpler).
Diffstat (limited to 'ldo.c')
-rw-r--r-- | ldo.c | 63 |
1 files changed, 23 insertions, 40 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldo.c,v 1.68 2000/03/03 14:58:26 roberto Exp roberto $ | 2 | ** $Id: ldo.c,v 1.69 2000/03/10 18:37:44 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 | */ |
@@ -119,7 +119,7 @@ void luaD_lineHook (lua_State *L, StkId func, int line) { | |||
119 | } | 119 | } |
120 | 120 | ||
121 | 121 | ||
122 | void luaD_callHook (lua_State *L, StkId func, lua_Dbghook callhook, | 122 | static void luaD_callHook (lua_State *L, StkId func, lua_Dbghook callhook, |
123 | const char *event) { | 123 | const char *event) { |
124 | if (L->allowhooks) { | 124 | if (L->allowhooks) { |
125 | lua_Dbgactreg ar; | 125 | lua_Dbgactreg ar; |
@@ -137,40 +137,31 @@ void luaD_callHook (lua_State *L, StkId func, lua_Dbghook callhook, | |||
137 | } | 137 | } |
138 | 138 | ||
139 | 139 | ||
140 | /* | 140 | static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) { |
141 | ** Call a C function. | 141 | int nup = cl->nelems; /* number of upvalues */ |
142 | ** Cstack.num is the number of arguments; Cstack.lua2C points to the | 142 | int numarg = L->top-base; |
143 | ** first argument. Returns an index to the first result from C. | ||
144 | */ | ||
145 | static StkId callC (lua_State *L, lua_CFunction f, StkId base) { | ||
146 | struct C_Lua_Stack oldCLS = L->Cstack; | 143 | struct C_Lua_Stack oldCLS = L->Cstack; |
147 | StkId firstResult; | 144 | StkId firstResult; |
148 | int numarg = L->top - base; | 145 | if (nup > 0) { |
146 | int n = numarg; | ||
147 | luaD_checkstack(L, nup); | ||
148 | /* open space for upvalues as extra arguments */ | ||
149 | while (n--) *(base+nup+n) = *(base+n); | ||
150 | L->top += nup; | ||
151 | numarg += nup; | ||
152 | /* copy upvalues into stack */ | ||
153 | while (nup--) *(base+nup) = cl->consts[nup]; | ||
154 | } | ||
149 | L->Cstack.num = numarg; | 155 | L->Cstack.num = numarg; |
150 | L->Cstack.lua2C = base; | 156 | L->Cstack.lua2C = base; |
151 | L->Cstack.base = L->top; | 157 | L->Cstack.base = L->top; |
152 | if (L->callhook) | 158 | (*cl->f.c)(L); /* do the actual call */ |
153 | luaD_callHook(L, base-1, L->callhook, "call"); | ||
154 | (*f)(L); /* do the actual call */ | ||
155 | firstResult = L->Cstack.base; | 159 | firstResult = L->Cstack.base; |
156 | L->Cstack = oldCLS; | 160 | L->Cstack = oldCLS; |
157 | return firstResult; | 161 | return firstResult; |
158 | } | 162 | } |
159 | 163 | ||
160 | 164 | ||
161 | static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) { | ||
162 | int nup = cl->nelems; /* number of upvalues */ | ||
163 | int n = L->top-base; /* number of arguments (to move up) */ | ||
164 | luaD_checkstack(L, nup); | ||
165 | /* open space for upvalues as extra arguments */ | ||
166 | while (n--) *(base+nup+n) = *(base+n); | ||
167 | L->top += nup; | ||
168 | /* copy upvalues into stack */ | ||
169 | while (nup--) *(base+nup) = cl->consts[nup+1]; | ||
170 | return callC(L, fvalue(cl->consts), base); | ||
171 | } | ||
172 | |||
173 | |||
174 | void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults) { | 165 | void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults) { |
175 | StkId base = L->top - nParams; | 166 | StkId base = L->top - nParams; |
176 | luaD_openstack(L, base); | 167 | luaD_openstack(L, base); |
@@ -191,24 +182,18 @@ void luaD_call (lua_State *L, StkId func, int nResults) { | |||
191 | lua_Dbghook callhook = L->callhook; | 182 | lua_Dbghook callhook = L->callhook; |
192 | retry: /* for `function' tag method */ | 183 | retry: /* for `function' tag method */ |
193 | switch (ttype(func)) { | 184 | switch (ttype(func)) { |
194 | case TAG_CPROTO: | ||
195 | ttype(func) = TAG_CMARK; | ||
196 | firstResult = callC(L, fvalue(func), func+1); | ||
197 | break; | ||
198 | case TAG_LPROTO: | ||
199 | ttype(func) = TAG_LMARK; | ||
200 | firstResult = luaV_execute(L, NULL, tfvalue(func), func+1); | ||
201 | break; | ||
202 | case TAG_LCLOSURE: { | 185 | case TAG_LCLOSURE: { |
203 | Closure *c = clvalue(func); | ||
204 | ttype(func) = TAG_LCLMARK; | 186 | ttype(func) = TAG_LCLMARK; |
205 | firstResult = luaV_execute(L, c, tfvalue(c->consts), func+1); | 187 | if (callhook) |
188 | luaD_callHook(L, func, callhook, "call"); | ||
189 | firstResult = luaV_execute(L, clvalue(func), func+1); | ||
206 | break; | 190 | break; |
207 | } | 191 | } |
208 | case TAG_CCLOSURE: { | 192 | case TAG_CCLOSURE: { |
209 | Closure *c = clvalue(func); | ||
210 | ttype(func) = TAG_CCLMARK; | 193 | ttype(func) = TAG_CCLMARK; |
211 | firstResult = callCclosure(L, c, func+1); | 194 | if (callhook) |
195 | luaD_callHook(L, func, callhook, "call"); | ||
196 | firstResult = callCclosure(L, clvalue(func), func+1); | ||
212 | break; | 197 | break; |
213 | } | 198 | } |
214 | default: { /* `func' is not a function; check the `function' tag method */ | 199 | default: { /* `func' is not a function; check the `function' tag method */ |
@@ -316,9 +301,7 @@ static int protectedparser (lua_State *L, ZIO *z, int bin) { | |||
316 | L->errorJmp = oldErr; | 301 | L->errorJmp = oldErr; |
317 | if (status) return 1; /* error code */ | 302 | if (status) return 1; /* error code */ |
318 | if (tf == NULL) return 2; /* `natural' end */ | 303 | if (tf == NULL) return 2; /* `natural' end */ |
319 | L->top->ttype = TAG_LPROTO; /* push new function on the stack */ | 304 | luaV_Lclosure(L, tf, 0); |
320 | L->top->value.tf = tf; | ||
321 | incr_top; | ||
322 | return 0; | 305 | return 0; |
323 | } | 306 | } |
324 | 307 | ||