diff options
Diffstat (limited to '')
| -rw-r--r-- | ldo.c | 39 |
1 files changed, 25 insertions, 14 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ldo.c,v 1.11 1997/11/26 20:28:22 roberto Exp $ | 2 | ** $Id: ldo.c,v 1.12 1997/11/26 20:44:52 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 | */ |
| @@ -143,25 +143,36 @@ void luaD_callHook (StkId base, lua_Type type, int isreturn) | |||
| 143 | 143 | ||
| 144 | 144 | ||
| 145 | /* | 145 | /* |
| 146 | ** Call a C function. L->Cstack.base will point to the top of the stack, | 146 | ** Call a C function. |
| 147 | ** and L->Cstack.num is the number of parameters. Returns an index | 147 | ** Cstack.num is the number of arguments; Cstack.lua2C points to the |
| 148 | ** to the first result from C. | 148 | ** first argument. Returns an index to the first result from C. |
| 149 | */ | 149 | */ |
| 150 | static StkId callC (lua_CFunction func, StkId base) | 150 | static StkId callC (struct Closure *cl, StkId base) |
| 151 | { | 151 | { |
| 152 | struct C_Lua_Stack oldCLS = L->Cstack; | 152 | struct C_Lua_Stack *CS = &L->Cstack; |
| 153 | struct C_Lua_Stack oldCLS = *CS; | ||
| 153 | StkId firstResult; | 154 | StkId firstResult; |
| 154 | L->Cstack.num = (L->stack.top-L->stack.stack) - base; | 155 | int numarg = (L->stack.top-L->stack.stack) - base; |
| 155 | /* incorporate parameters on the L->stack.stack */ | 156 | if (cl->nelems > 0) { /* are there upvalues? */ |
| 156 | L->Cstack.lua2C = base; | 157 | int i; |
| 157 | L->Cstack.base = base+L->Cstack.num; /* == top-stack */ | 158 | luaD_checkstack(cl->nelems); |
| 159 | for (i=1; i<=numarg; i++) /* open space */ | ||
| 160 | *(L->stack.top+cl->nelems-i) = *(L->stack.top-i); | ||
| 161 | /* copy upvalues to stack */ | ||
| 162 | memcpy(L->stack.top-numarg, cl->consts+1, cl->nelems*sizeof(TObject)); | ||
| 163 | L->stack.top += cl->nelems; | ||
| 164 | numarg += cl->nelems; | ||
| 165 | } | ||
| 166 | CS->num = numarg; | ||
| 167 | CS->lua2C = base; | ||
| 168 | CS->base = base+numarg; /* == top-stack */ | ||
| 158 | if (lua_callhook) | 169 | if (lua_callhook) |
| 159 | luaD_callHook(base, LUA_T_CPROTO, 0); | 170 | luaD_callHook(base, LUA_T_CPROTO, 0); |
| 160 | (*func)(); | 171 | (*(fvalue(cl->consts)))(); /* do the actual call */ |
| 161 | if (lua_callhook) /* func may have changed lua_callhook */ | 172 | if (lua_callhook) /* func may have changed lua_callhook */ |
| 162 | luaD_callHook(base, LUA_T_CPROTO, 1); | 173 | luaD_callHook(base, LUA_T_CPROTO, 1); |
| 163 | firstResult = L->Cstack.base; | 174 | firstResult = CS->base; |
| 164 | L->Cstack = oldCLS; | 175 | *CS = oldCLS; |
| 165 | return firstResult; | 176 | return firstResult; |
| 166 | } | 177 | } |
| 167 | 178 | ||
| @@ -188,7 +199,7 @@ void luaD_call (StkId base, int nResults) | |||
| 188 | if (ttype(func) == LUA_T_FUNCTION) { | 199 | if (ttype(func) == LUA_T_FUNCTION) { |
| 189 | TObject *proto = protovalue(func); | 200 | TObject *proto = protovalue(func); |
| 190 | ttype(func) = LUA_T_MARK; | 201 | ttype(func) = LUA_T_MARK; |
| 191 | firstResult = (ttype(proto) == LUA_T_CPROTO) ? callC(fvalue(proto), base) | 202 | firstResult = (ttype(proto) == LUA_T_CPROTO) ? callC(clvalue(func), base) |
| 192 | : luaV_execute(func->value.cl, base); | 203 | : luaV_execute(func->value.cl, base); |
| 193 | } | 204 | } |
| 194 | else { /* func is not a function */ | 205 | else { /* func is not a function */ |
