aboutsummaryrefslogtreecommitdiff
path: root/ldo.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-12-23 16:19:57 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-12-23 16:19:57 -0200
commitb1b0c219f5255a0cd0921ebc0a77a81f99b72532 (patch)
tree7cb4d9cbbdb1309b94794eb75694b02f2b08f75a /ldo.c
parentbe3212de781786c0a68365dee1d3510407b5c325 (diff)
downloadlua-b1b0c219f5255a0cd0921ebc0a77a81f99b72532.tar.gz
lua-b1b0c219f5255a0cd0921ebc0a77a81f99b72532.tar.bz2
lua-b1b0c219f5255a0cd0921ebc0a77a81f99b72532.zip
new ttypes to distinguish between C closures and Lua closures.
Diffstat (limited to 'ldo.c')
-rw-r--r--ldo.c71
1 files changed, 40 insertions, 31 deletions
diff --git a/ldo.c b/ldo.c
index 60b9b354..3fde714b 100644
--- a/ldo.c
+++ b/ldo.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldo.c,v 1.58 1999/12/06 12:03:45 roberto Exp roberto $ 2** $Id: ldo.c,v 1.59 1999/12/21 18:04:41 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*/
@@ -93,7 +93,8 @@ void luaD_adjusttop (lua_State *L, StkId base, int extra) {
93** Open a hole inside the stack at `pos' 93** Open a hole inside the stack at `pos'
94*/ 94*/
95void luaD_openstack (lua_State *L, StkId pos) { 95void luaD_openstack (lua_State *L, StkId pos) {
96 luaO_memup(pos+1, pos, (L->top-pos)*sizeof(TObject)); 96 int i = L->top-pos;
97 while (i--) pos[i+1] = pos[i];
97 incr_top; 98 incr_top;
98} 99}
99 100
@@ -122,15 +123,18 @@ static void luaD_callHook (lua_State *L, StkId func, lua_CHFunction callhook,
122 if (isreturn) 123 if (isreturn)
123 callhook(L, LUA_NOOBJECT, "(return)", 0); 124 callhook(L, LUA_NOOBJECT, "(return)", 0);
124 else { 125 else {
125 if (ttype(func) == LUA_T_PROTO) 126 switch (ttype(func)) {
126 callhook(L, func, tfvalue(func)->source->str, 127 case LUA_T_LPROTO:
127 tfvalue(func)->lineDefined); 128 callhook(L, func, tfvalue(func)->source->str,
128 else if (ttype(func) == LUA_T_CLOSURE && 129 tfvalue(func)->lineDefined);
129 ttype(clvalue(func)->consts) == LUA_T_PROTO) 130 break;
130 callhook(L, func, tfvalue(protovalue(func))->source->str, 131 case LUA_T_LCLOSURE:
131 tfvalue(protovalue(func))->lineDefined); 132 callhook(L, func, tfvalue(protovalue(func))->source->str,
132 else 133 tfvalue(protovalue(func))->lineDefined);
133 callhook(L, func, "(C)", -1); 134 break;
135 default:
136 callhook(L, func, "(C)", -1);
137 }
134 } 138 }
135 L->allowhooks = 1; 139 L->allowhooks = 1;
136 L->top = old_top; 140 L->top = old_top;
@@ -158,16 +162,16 @@ static StkId callC (lua_State *L, lua_CFunction f, StkId base) {
158} 162}
159 163
160 164
161static StkId callCclosure (lua_State *L, const struct Closure *cl, 165static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) {
162 lua_CFunction f, StkId base) {
163 int nup = cl->nelems; /* number of upvalues */ 166 int nup = cl->nelems; /* number of upvalues */
167 int n = L->top-base; /* number of arguments (to move up) */
164 luaD_checkstack(L, nup); 168 luaD_checkstack(L, nup);
165 /* open space for upvalues as extra arguments */ 169 /* open space for upvalues as extra arguments */
166 luaO_memup(base+nup, base, (L->top-base)*sizeof(TObject)); 170 while (n--) *(base+nup+n) = *(base+n);
167 /* copy upvalues into stack */
168 memcpy(base, cl->consts+1, nup*sizeof(TObject));
169 L->top += nup; 171 L->top += nup;
170 return callC(L, f, base); 172 /* copy upvalues into stack */
173 while (nup--) *(base+nup) = cl->consts[nup+1];
174 return callC(L, fvalue(cl->consts), base);
171} 175}
172 176
173 177
@@ -197,17 +201,20 @@ void luaD_call (lua_State *L, StkId func, int nResults) {
197 ttype(func) = LUA_T_CMARK; 201 ttype(func) = LUA_T_CMARK;
198 firstResult = callC(L, fvalue(func), func+1); 202 firstResult = callC(L, fvalue(func), func+1);
199 break; 203 break;
200 case LUA_T_PROTO: 204 case LUA_T_LPROTO:
201 ttype(func) = LUA_T_PMARK; 205 ttype(func) = LUA_T_LMARK;
202 firstResult = luaV_execute(L, NULL, tfvalue(func), func+1); 206 firstResult = luaV_execute(L, NULL, tfvalue(func), func+1);
203 break; 207 break;
204 case LUA_T_CLOSURE: { 208 case LUA_T_LCLOSURE: {
205 Closure *c = clvalue(func); 209 Closure *c = clvalue(func);
206 TObject *proto = c->consts; 210 ttype(func) = LUA_T_LCLMARK;
207 ttype(func) = LUA_T_CLMARK; 211 firstResult = luaV_execute(L, c, tfvalue(c->consts), func+1);
208 firstResult = (ttype(proto) == LUA_T_CPROTO) ? 212 break;
209 callCclosure(L, c, fvalue(proto), func+1) : 213 }
210 luaV_execute(L, c, tfvalue(proto), func+1); 214 case LUA_T_CCLOSURE: {
215 Closure *c = clvalue(func);
216 ttype(func) = LUA_T_CCLMARK;
217 firstResult = callCclosure(L, c, func+1);
211 break; 218 break;
212 } 219 }
213 default: { /* `func' is not a function; check the `function' tag method */ 220 default: { /* `func' is not a function; check the `function' tag method */
@@ -226,16 +233,18 @@ void luaD_call (lua_State *L, StkId func, int nResults) {
226 nResults = L->top - firstResult; 233 nResults = L->top - firstResult;
227 else 234 else
228 luaD_adjusttop(L, firstResult, nResults); 235 luaD_adjusttop(L, firstResult, nResults);
229 /* move results to func (to erase parameters and function) */ 236 /* move results to `func' (to erase parameters and function) */
230 luaO_memdown(func, firstResult, nResults*sizeof(TObject)); 237 while (nResults) {
231 L->top = func+nResults; 238 *func++ = *(L->top - nResults);
239 nResults--;
240 }
241 L->top = func;
232} 242}
233 243
234 244
235static void message (lua_State *L, const char *s) { 245static void message (lua_State *L, const char *s) {
236 const TObject *em = &(luaS_assertglobalbyname(L, "_ERRORMESSAGE")->value); 246 const TObject *em = &(luaS_assertglobalbyname(L, "_ERRORMESSAGE")->value);
237 if (ttype(em) == LUA_T_PROTO || ttype(em) == LUA_T_CPROTO || 247 if (*luaO_typename(em) == 'f') {
238 ttype(em) == LUA_T_CLOSURE) {
239 *L->top = *em; 248 *L->top = *em;
240 incr_top; 249 incr_top;
241 lua_pushstring(L, s); 250 lua_pushstring(L, s);
@@ -313,7 +322,7 @@ static int protectedparser (lua_State *L, ZIO *z, int bin) {
313 L->errorJmp = oldErr; 322 L->errorJmp = oldErr;
314 if (status) return 1; /* error code */ 323 if (status) return 1; /* error code */
315 if (tf == NULL) return 2; /* `natural' end */ 324 if (tf == NULL) return 2; /* `natural' end */
316 L->top->ttype = LUA_T_PROTO; /* push new function on the stack */ 325 L->top->ttype = LUA_T_LPROTO; /* push new function on the stack */
317 L->top->value.tf = tf; 326 L->top->value.tf = tf;
318 incr_top; 327 incr_top;
319 return 0; 328 return 0;