Browse Source

other access method for C upvalues (as arguments)

v5-2
Roberto Ierusalimschy 27 years ago
parent
commit
5482992dec
  1. 32
      lapi.c
  2. 8
      lbuiltin.c
  3. 39
      ldo.c
  4. 5
      lua.h

32
lapi.c

@ -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
** 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)
{
if (function == LUA_NOOBJECT)
@ -317,17 +305,13 @@ void lua_pushstring (char *s)
void lua_pushCclosure (lua_CFunction fn, int n)
{
if (fn == NULL) {
ttype(L->stack.top) = LUA_T_NIL;
incr_top;
}
else {
checkCparams(n);
ttype(L->stack.top) = LUA_T_CPROTO;
fvalue(L->stack.top) = fn;
incr_top;
luaV_closure(n);
}
if (fn == NULL)
lua_error("API error - attempt to push a NULL Cfunction");
checkCparams(n);
ttype(L->stack.top) = LUA_T_CPROTO;
fvalue(L->stack.top) = fn;
incr_top;
luaV_closure(n);
}
void lua_pushusertag (void *u, int tag)

8
lbuiltin.c

@ -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
** See Copyright Notice in lua.h
*/
@ -399,7 +399,10 @@ static void testC (void)
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 'g': { int n=getnum(s); reg[n]=lua_getglobal(getname(s)); break; }
case 'G': { int n = getnum(s);
@ -419,7 +422,6 @@ static void testC (void)
case 'I': reg[getnum(s)] = lua_rawgettable(); break;
case 't': lua_settable(); 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));
}
if (*s == 0) return;

39
ldo.c

@ -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
** 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,
** and L->Cstack.num is the number of parameters. Returns an index
** to the first result from C.
** Call a C function.
** Cstack.num is the number of arguments; Cstack.lua2C points to the
** 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;
L->Cstack.num = (L->stack.top-L->stack.stack) - base;
/* incorporate parameters on the L->stack.stack */
L->Cstack.lua2C = base;
L->Cstack.base = base+L->Cstack.num; /* == top-stack */
int numarg = (L->stack.top-L->stack.stack) - base;
if (cl->nelems > 0) { /* are there upvalues? */
int i;
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)
luaD_callHook(base, LUA_T_CPROTO, 0);
(*func)();
(*(fvalue(cl->consts)))(); /* do the actual call */
if (lua_callhook) /* func may have changed lua_callhook */
luaD_callHook(base, LUA_T_CPROTO, 1);
firstResult = L->Cstack.base;
L->Cstack = oldCLS;
firstResult = CS->base;
*CS = oldCLS;
return firstResult;
}
@ -188,7 +199,7 @@ void luaD_call (StkId base, int nResults)
if (ttype(func) == LUA_T_FUNCTION) {
TObject *proto = protovalue(func);
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);
}
else { /* func is not a function */

5
lua.h

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

Loading…
Cancel
Save