new lua_Objects are created below the C2lua stack, so most API functions

don't need to adjust stack.
This commit is contained in:
Roberto Ierusalimschy
1997-06-06 17:54:40 -03:00
parent d68d8287aa
commit 5fdcfeb353

View File

@@ -3,7 +3,7 @@
** TecCGraf - PUC-Rio ** TecCGraf - PUC-Rio
*/ */
char *rcs_opcode="$Id: opcode.c,v 4.4 1997/04/24 22:59:57 roberto Exp roberto $"; char *rcs_opcode="$Id: opcode.c,v 4.5 1997/05/26 14:23:55 roberto Exp roberto $";
#include <setjmp.h> #include <setjmp.h>
#include <stdio.h> #include <stdio.h>
@@ -193,6 +193,13 @@ static void adjust_top (StkId newtop)
#define adjustC(nParams) adjust_top(CLS_current.base+nParams) #define adjustC(nParams) adjust_top(CLS_current.base+nParams)
static void checkCparams (int nParams)
{
if (top-stack < CLS_current.base+nParams)
lua_error("API error - wrong number of arguments in C2lua stack");
}
/* /*
** Open a hole below "nelems" from the top. ** Open a hole below "nelems" from the top.
*/ */
@@ -205,6 +212,23 @@ static void open_stack (int nelems)
} }
static lua_Object put_luaObject (TObject *o)
{
open_stack((top-stack)-CLS_current.base);
stack[CLS_current.base++] = *o;
return CLS_current.base; /* this is +1 real position (see Ref) */
}
static lua_Object put_luaObjectonTop (void)
{
open_stack((top-stack)-CLS_current.base);
stack[CLS_current.base++] = *(--top);
return CLS_current.base; /* this is +1 real position (see Ref) */
}
/* /*
** call Line hook ** call Line hook
*/ */
@@ -352,7 +376,7 @@ static void pushsubscript (void)
lua_Object lua_rawgettable (void) lua_Object lua_rawgettable (void)
{ {
adjustC(2); checkCparams(2);
if (ttype(top-2) != LUA_T_ARRAY) if (ttype(top-2) != LUA_T_ARRAY)
lua_error("indexed expression not a table in raw gettable"); lua_error("indexed expression not a table in raw gettable");
else { else {
@@ -363,8 +387,7 @@ lua_Object lua_rawgettable (void)
else else
ttype(top-1) = LUA_T_NIL; ttype(top-1) = LUA_T_NIL;
} }
CLS_current.base++; /* incorporate object in the stack */ return put_luaObjectonTop();
return (Ref(top-1));
} }
@@ -697,10 +720,9 @@ void lua_seterrormethod (lua_CFunction method)
*/ */
lua_Object lua_gettable (void) lua_Object lua_gettable (void)
{ {
adjustC(2); checkCparams(2);
pushsubscript(); pushsubscript();
CLS_current.base++; /* incorporate object in the stack */ return put_luaObjectonTop();
return (Ref(top-1));
} }
@@ -732,7 +754,7 @@ void lua_endblock (void)
void lua_settag (int tag) void lua_settag (int tag)
{ {
adjustC(1); checkCparams(1);
luaI_settag(tag, --top); luaI_settag(tag, --top);
} }
@@ -741,13 +763,13 @@ void lua_settag (int tag)
*/ */
void lua_settable (void) void lua_settable (void)
{ {
adjustC(3); checkCparams(3);
storesubscript(top-3, 1); storesubscript(top-3, 1);
} }
void lua_rawsettable (void) void lua_rawsettable (void)
{ {
adjustC(3); checkCparams(3);
storesubscript(top-3, 0); storesubscript(top-3, 0);
} }
@@ -756,12 +778,10 @@ void lua_rawsettable (void)
*/ */
lua_Object lua_createtable (void) lua_Object lua_createtable (void)
{ {
adjustC(0); TObject o;
avalue(top) = lua_createarray(0); avalue(&o) = lua_createarray(0);
ttype(top) = LUA_T_ARRAY; ttype(&o) = LUA_T_ARRAY;
incr_top; return put_luaObject(&o);
CLS_current.base++; /* incorporate object in the stack */
return Ref(top-1);
} }
/* /*
@@ -874,25 +894,13 @@ lua_Object lua_getref (int ref)
TObject *o = luaI_getref(ref); TObject *o = luaI_getref(ref);
if (o == NULL) if (o == NULL)
return LUA_NOOBJECT; return LUA_NOOBJECT;
adjustC(0); return put_luaObject(o);
luaI_pushobject(o);
CLS_current.base++; /* incorporate object in the stack */
return Ref(top-1);
}
void lua_pushref (int ref)
{
TObject *o = luaI_getref(ref);
if (o == NULL)
lua_error("access to invalid reference (possibly garbage collected)");
luaI_pushobject(o);
} }
int lua_ref (int lock) int lua_ref (int lock)
{ {
adjustC(1); checkCparams(1);
return luaI_ref(--top, lock); return luaI_ref(--top, lock);
} }
@@ -903,20 +911,14 @@ int lua_ref (int lock)
*/ */
lua_Object lua_getglobal (char *name) lua_Object lua_getglobal (char *name)
{ {
adjustC(0);
getglobal(luaI_findsymbolbyname(name)); getglobal(luaI_findsymbolbyname(name));
CLS_current.base++; /* incorporate object in the stack */ return put_luaObjectonTop();
return Ref(top-1);
} }
lua_Object lua_rawgetglobal (char *name) lua_Object lua_rawgetglobal (char *name)
{ {
adjustC(0); return put_luaObject(&lua_table[luaI_findsymbolbyname(name)].object);
*top = lua_table[luaI_findsymbolbyname(name)].object;
incr_top;
CLS_current.base++; /* incorporate object in the stack */
return Ref(top-1);
} }
@@ -944,14 +946,14 @@ static void setglobal (Word n)
void lua_setglobal (char *name) void lua_setglobal (char *name)
{ {
adjustC(1); checkCparams(1);
setglobal(luaI_findsymbolbyname(name)); setglobal(luaI_findsymbolbyname(name));
} }
void lua_rawsetglobal (char *name) void lua_rawsetglobal (char *name)
{ {
Word n = luaI_findsymbolbyname(name); Word n = luaI_findsymbolbyname(name);
adjustC(1); checkCparams(1);
s_object(n) = *(--top); s_object(n) = *(--top);
} }
@@ -987,8 +989,7 @@ void lua_pushstring (char *s)
} }
incr_top; incr_top;
} }
/*>>>>>>>>>#undef lua_pushliteral
void lua_pushliteral(char *s) { lua_pushstring(s); }*/
/* /*
** Push an object (ttype=cfunction) to stack. ** Push an object (ttype=cfunction) to stack.
@@ -1035,7 +1036,7 @@ void luaI_pushobject (TObject *o)
void lua_pushobject (lua_Object o) void lua_pushobject (lua_Object o)
{ {
if (o == LUA_NOOBJECT) if (o == LUA_NOOBJECT)
lua_error("attempt to push a NOOBJECT"); lua_error("API error - attempt to push a NOOBJECT");
*top = *Address(o); *top = *Address(o);
if (ttype(top) == LUA_T_MARK) ttype(top) = LUA_T_FUNCTION; if (ttype(top) == LUA_T_MARK) ttype(top) = LUA_T_FUNCTION;
else if (ttype(top) == LUA_T_CMARK) ttype(top) = LUA_T_CFUNCTION; else if (ttype(top) == LUA_T_CMARK) ttype(top) = LUA_T_CFUNCTION;