summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lapi.c32
-rw-r--r--lbuiltin.c8
-rw-r--r--ldo.c39
-rw-r--r--lua.h5
4 files changed, 40 insertions, 44 deletions
diff --git a/lapi.c b/lapi.c
index 068b536e..ebbae483 100644
--- a/lapi.c
+++ b/lapi.c
@@ -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
98lua_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;
103if (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
110int lua_callfunction (lua_Object function) 98int 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
318void lua_pushCclosure (lua_CFunction fn, int n) 306void 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
333void lua_pushusertag (void *u, int tag) 317void lua_pushusertag (void *u, int tag)
diff --git a/lbuiltin.c b/lbuiltin.c
index 53dd4032..48acfa42 100644
--- a/lbuiltin.c
+++ b/lbuiltin.c
@@ -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;
diff --git a/ldo.c b/ldo.c
index 2318dd81..4a37fe5d 100644
--- a/ldo.c
+++ b/ldo.c
@@ -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*/
150static StkId callC (lua_CFunction func, StkId base) 150static 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 */
diff --git a/lua.h b/lua.h
index 4549f927..7d05b2e9 100644
--- a/lua.h
+++ b/lua.h
@@ -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);
75lua_Object lua_lua2C (int number); 75lua_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(_)
78lua_Object lua_upvalue (int n);
79 78
80int lua_isnil (lua_Object object); 79int lua_isnil (lua_Object object);
81int lua_istable (lua_Object object); 80int lua_istable (lua_Object object);