diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-11-27 16:25:14 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-11-27 16:25:14 -0200 |
| commit | 5482992dec286ca800ffab539b0f81eddaa2665b (patch) | |
| tree | 77759747324cf65d22c9a10f81df3468ff724f36 | |
| parent | 024528e0c2ce75ac28ebbbc1220d4ae4045d3adf (diff) | |
| download | lua-5482992dec286ca800ffab539b0f81eddaa2665b.tar.gz lua-5482992dec286ca800ffab539b0f81eddaa2665b.tar.bz2 lua-5482992dec286ca800ffab539b0f81eddaa2665b.zip | |
other access method for C upvalues (as arguments)
| -rw-r--r-- | lapi.c | 32 | ||||
| -rw-r--r-- | lbuiltin.c | 8 | ||||
| -rw-r--r-- | ldo.c | 39 | ||||
| -rw-r--r-- | lua.h | 5 |
4 files changed, 40 insertions, 44 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lapi.c,v 1.8 1997/11/26 18:53:45 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.9 1997/11/27 15:59:25 roberto Exp roberto $ |
| 3 | ** Lua API | 3 | ** Lua API |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -95,18 +95,6 @@ lua_Object lua_lua2C (int number) | |||
| 95 | } | 95 | } |
| 96 | 96 | ||
| 97 | 97 | ||
| 98 | lua_Object lua_upvalue (int n) | ||
| 99 | { | ||
| 100 | TObject *f = L->stack.stack+L->Cstack.lua2C-1; | ||
| 101 | if (ttype(f) != LUA_T_MARK || n <= 0 || n > clvalue(f)->nelems) | ||
| 102 | return LUA_NOOBJECT; | ||
| 103 | if (ttype(protovalue(f)) != LUA_T_CPROTO) lua_error("BAD!!"); | ||
| 104 | *L->stack.top = clvalue(f)->consts[n]; | ||
| 105 | incr_top; | ||
| 106 | return put_luaObjectonTop(); | ||
| 107 | } | ||
| 108 | |||
| 109 | |||
| 110 | int lua_callfunction (lua_Object function) | 98 | int lua_callfunction (lua_Object function) |
| 111 | { | 99 | { |
| 112 | if (function == LUA_NOOBJECT) | 100 | if (function == LUA_NOOBJECT) |
| @@ -317,17 +305,13 @@ void lua_pushstring (char *s) | |||
| 317 | 305 | ||
| 318 | void lua_pushCclosure (lua_CFunction fn, int n) | 306 | void lua_pushCclosure (lua_CFunction fn, int n) |
| 319 | { | 307 | { |
| 320 | if (fn == NULL) { | 308 | if (fn == NULL) |
| 321 | ttype(L->stack.top) = LUA_T_NIL; | 309 | lua_error("API error - attempt to push a NULL Cfunction"); |
| 322 | incr_top; | 310 | checkCparams(n); |
| 323 | } | 311 | ttype(L->stack.top) = LUA_T_CPROTO; |
| 324 | else { | 312 | fvalue(L->stack.top) = fn; |
| 325 | checkCparams(n); | 313 | incr_top; |
| 326 | ttype(L->stack.top) = LUA_T_CPROTO; | 314 | luaV_closure(n); |
| 327 | fvalue(L->stack.top) = fn; | ||
| 328 | incr_top; | ||
| 329 | luaV_closure(n); | ||
| 330 | } | ||
| 331 | } | 315 | } |
| 332 | 316 | ||
| 333 | void lua_pushusertag (void *u, int tag) | 317 | void lua_pushusertag (void *u, int tag) |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lbuiltin.c,v 1.10 1997/11/26 19:40:27 roberto Exp roberto $ | 2 | ** $Id: lbuiltin.c,v 1.11 1997/11/27 15:59:44 roberto Exp roberto $ |
| 3 | ** Built-in functions | 3 | ** Built-in functions |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -399,7 +399,10 @@ static void testC (void) | |||
| 399 | break; | 399 | break; |
| 400 | 400 | ||
| 401 | case 'c': reg[getnum(s)] = lua_createtable(); break; | 401 | case 'c': reg[getnum(s)] = lua_createtable(); break; |
| 402 | case 'C': lua_pushCclosure(testC, getnum(s)); break; | 402 | case 'C': { lua_CFunction f = lua_getcfunction(lua_getglobal(getname(s))); |
| 403 | lua_pushCclosure(f, getnum(s)); | ||
| 404 | break; | ||
| 405 | } | ||
| 403 | case 'P': reg[getnum(s)] = lua_pop(); break; | 406 | case 'P': reg[getnum(s)] = lua_pop(); break; |
| 404 | case 'g': { int n=getnum(s); reg[n]=lua_getglobal(getname(s)); break; } | 407 | case 'g': { int n=getnum(s); reg[n]=lua_getglobal(getname(s)); break; } |
| 405 | case 'G': { int n = getnum(s); | 408 | case 'G': { int n = getnum(s); |
| @@ -419,7 +422,6 @@ static void testC (void) | |||
| 419 | case 'I': reg[getnum(s)] = lua_rawgettable(); break; | 422 | case 'I': reg[getnum(s)] = lua_rawgettable(); break; |
| 420 | case 't': lua_settable(); break; | 423 | case 't': lua_settable(); break; |
| 421 | case 'T': lua_rawsettable(); break; | 424 | case 'T': lua_rawsettable(); break; |
| 422 | case 'U': { int n=getnum(s); reg[n]=lua_upvalue(getnum(s)); break; } | ||
| 423 | default: luaL_verror("unknown command in `testC': %c", *(s-1)); | 425 | default: luaL_verror("unknown command in `testC': %c", *(s-1)); |
| 424 | } | 426 | } |
| 425 | if (*s == 0) return; | 427 | if (*s == 0) return; |
| @@ -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 */ |
| @@ -1,6 +1,6 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.h,v 1.5 1997/11/26 18:53:45 roberto Exp roberto $ | 2 | ** $Id: lua.h,v 1.6 1997/11/27 15:59:25 roberto Exp roberto $ |
| 3 | ** LUA - An Extensible Extension Language | 3 | ** Lua - An Extensible Extension Language |
| 4 | ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil | 4 | ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil |
| 5 | ** e-mail: lua@tecgraf.puc-rio.br | 5 | ** e-mail: lua@tecgraf.puc-rio.br |
| 6 | */ | 6 | */ |
| @@ -75,7 +75,6 @@ void lua_endblock (void); | |||
| 75 | lua_Object lua_lua2C (int number); | 75 | lua_Object lua_lua2C (int number); |
| 76 | #define lua_getparam(_) lua_lua2C(_) | 76 | #define lua_getparam(_) lua_lua2C(_) |
| 77 | #define lua_getresult(_) lua_lua2C(_) | 77 | #define lua_getresult(_) lua_lua2C(_) |
| 78 | lua_Object lua_upvalue (int n); | ||
| 79 | 78 | ||
| 80 | int lua_isnil (lua_Object object); | 79 | int lua_isnil (lua_Object object); |
| 81 | int lua_istable (lua_Object object); | 80 | int lua_istable (lua_Object object); |
