other access method for C upvalues (as arguments)

This commit is contained in:
Roberto Ierusalimschy 1997-11-27 16:25:14 -02:00
parent 024528e0c2
commit 5482992dec
4 changed files with 40 additions and 44 deletions

22
lapi.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lapi.c,v 1.8 1997/11/26 18:53:45 roberto Exp roberto $ ** $Id: lapi.c,v 1.9 1997/11/27 15:59:25 roberto Exp roberto $
** Lua API ** Lua API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -95,18 +95,6 @@ lua_Object lua_lua2C (int number)
} }
lua_Object lua_upvalue (int n)
{
TObject *f = L->stack.stack+L->Cstack.lua2C-1;
if (ttype(f) != LUA_T_MARK || n <= 0 || n > clvalue(f)->nelems)
return LUA_NOOBJECT;
if (ttype(protovalue(f)) != LUA_T_CPROTO) lua_error("BAD!!");
*L->stack.top = clvalue(f)->consts[n];
incr_top;
return put_luaObjectonTop();
}
int lua_callfunction (lua_Object function) int lua_callfunction (lua_Object function)
{ {
if (function == LUA_NOOBJECT) if (function == LUA_NOOBJECT)
@ -317,17 +305,13 @@ void lua_pushstring (char *s)
void lua_pushCclosure (lua_CFunction fn, int n) void lua_pushCclosure (lua_CFunction fn, int n)
{ {
if (fn == NULL) { if (fn == NULL)
ttype(L->stack.top) = LUA_T_NIL; lua_error("API error - attempt to push a NULL Cfunction");
incr_top;
}
else {
checkCparams(n); checkCparams(n);
ttype(L->stack.top) = LUA_T_CPROTO; ttype(L->stack.top) = LUA_T_CPROTO;
fvalue(L->stack.top) = fn; fvalue(L->stack.top) = fn;
incr_top; incr_top;
luaV_closure(n); luaV_closure(n);
}
} }
void lua_pushusertag (void *u, int tag) void lua_pushusertag (void *u, int tag)

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lbuiltin.c,v 1.10 1997/11/26 19:40:27 roberto Exp roberto $ ** $Id: lbuiltin.c,v 1.11 1997/11/27 15:59:44 roberto Exp roberto $
** Built-in functions ** Built-in functions
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -399,7 +399,10 @@ static void testC (void)
break; break;
case 'c': reg[getnum(s)] = lua_createtable(); break; case 'c': reg[getnum(s)] = lua_createtable(); break;
case 'C': lua_pushCclosure(testC, getnum(s)); break; case 'C': { lua_CFunction f = lua_getcfunction(lua_getglobal(getname(s)));
lua_pushCclosure(f, getnum(s));
break;
}
case 'P': reg[getnum(s)] = lua_pop(); break; case 'P': reg[getnum(s)] = lua_pop(); break;
case 'g': { int n=getnum(s); reg[n]=lua_getglobal(getname(s)); break; } case 'g': { int n=getnum(s); reg[n]=lua_getglobal(getname(s)); break; }
case 'G': { int n = getnum(s); case 'G': { int n = getnum(s);
@ -419,7 +422,6 @@ static void testC (void)
case 'I': reg[getnum(s)] = lua_rawgettable(); break; case 'I': reg[getnum(s)] = lua_rawgettable(); break;
case 't': lua_settable(); break; case 't': lua_settable(); break;
case 'T': lua_rawsettable(); break; case 'T': lua_rawsettable(); break;
case 'U': { int n=getnum(s); reg[n]=lua_upvalue(getnum(s)); break; }
default: luaL_verror("unknown command in `testC': %c", *(s-1)); default: luaL_verror("unknown command in `testC': %c", *(s-1));
} }
if (*s == 0) return; if (*s == 0) return;

39
ldo.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldo.c,v 1.11 1997/11/26 20:28:22 roberto Exp $ ** $Id: ldo.c,v 1.12 1997/11/26 20:44:52 roberto Exp roberto $
** Stack and Call structure of Lua ** Stack and Call structure of Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -143,25 +143,36 @@ void luaD_callHook (StkId base, lua_Type type, int isreturn)
/* /*
** Call a C function. L->Cstack.base will point to the top of the stack, ** Call a C function.
** and L->Cstack.num is the number of parameters. Returns an index ** Cstack.num is the number of arguments; Cstack.lua2C points to the
** to the first result from C. ** first argument. Returns an index to the first result from C.
*/ */
static StkId callC (lua_CFunction func, StkId base) static StkId callC (struct Closure *cl, StkId base)
{ {
struct C_Lua_Stack oldCLS = L->Cstack; struct C_Lua_Stack *CS = &L->Cstack;
struct C_Lua_Stack oldCLS = *CS;
StkId firstResult; StkId firstResult;
L->Cstack.num = (L->stack.top-L->stack.stack) - base; int numarg = (L->stack.top-L->stack.stack) - base;
/* incorporate parameters on the L->stack.stack */ if (cl->nelems > 0) { /* are there upvalues? */
L->Cstack.lua2C = base; int i;
L->Cstack.base = base+L->Cstack.num; /* == top-stack */ luaD_checkstack(cl->nelems);
for (i=1; i<=numarg; i++) /* open space */
*(L->stack.top+cl->nelems-i) = *(L->stack.top-i);
/* copy upvalues to stack */
memcpy(L->stack.top-numarg, cl->consts+1, cl->nelems*sizeof(TObject));
L->stack.top += cl->nelems;
numarg += cl->nelems;
}
CS->num = numarg;
CS->lua2C = base;
CS->base = base+numarg; /* == top-stack */
if (lua_callhook) if (lua_callhook)
luaD_callHook(base, LUA_T_CPROTO, 0); luaD_callHook(base, LUA_T_CPROTO, 0);
(*func)(); (*(fvalue(cl->consts)))(); /* do the actual call */
if (lua_callhook) /* func may have changed lua_callhook */ if (lua_callhook) /* func may have changed lua_callhook */
luaD_callHook(base, LUA_T_CPROTO, 1); luaD_callHook(base, LUA_T_CPROTO, 1);
firstResult = L->Cstack.base; firstResult = CS->base;
L->Cstack = oldCLS; *CS = oldCLS;
return firstResult; return firstResult;
} }
@ -188,7 +199,7 @@ void luaD_call (StkId base, int nResults)
if (ttype(func) == LUA_T_FUNCTION) { if (ttype(func) == LUA_T_FUNCTION) {
TObject *proto = protovalue(func); TObject *proto = protovalue(func);
ttype(func) = LUA_T_MARK; ttype(func) = LUA_T_MARK;
firstResult = (ttype(proto) == LUA_T_CPROTO) ? callC(fvalue(proto), base) firstResult = (ttype(proto) == LUA_T_CPROTO) ? callC(clvalue(func), base)
: luaV_execute(func->value.cl, base); : luaV_execute(func->value.cl, base);
} }
else { /* func is not a function */ else { /* func is not a function */

5
lua.h
View File

@ -1,6 +1,6 @@
/* /*
** $Id: lua.h,v 1.5 1997/11/26 18:53:45 roberto Exp roberto $ ** $Id: lua.h,v 1.6 1997/11/27 15:59:25 roberto Exp roberto $
** LUA - An Extensible Extension Language ** Lua - An Extensible Extension Language
** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
** e-mail: lua@tecgraf.puc-rio.br ** e-mail: lua@tecgraf.puc-rio.br
*/ */
@ -75,7 +75,6 @@ void lua_endblock (void);
lua_Object lua_lua2C (int number); lua_Object lua_lua2C (int number);
#define lua_getparam(_) lua_lua2C(_) #define lua_getparam(_) lua_lua2C(_)
#define lua_getresult(_) lua_lua2C(_) #define lua_getresult(_) lua_lua2C(_)
lua_Object lua_upvalue (int n);
int lua_isnil (lua_Object object); int lua_isnil (lua_Object object);
int lua_istable (lua_Object object); int lua_istable (lua_Object object);