fixed stack; first version.
This commit is contained in:
103
lapi.c
103
lapi.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lapi.c,v 1.59 1999/11/29 19:11:36 roberto Exp roberto $
|
||||
** $Id: lapi.c,v 1.60 1999/11/29 19:31:29 roberto Exp roberto $
|
||||
** Lua API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -63,28 +63,28 @@ static const TObject *luaA_protovalue (const TObject *o) {
|
||||
|
||||
|
||||
static void checkCparams (lua_State *L, int nParams) {
|
||||
if (L->stack.top-L->stack.stack < L->Cstack.base+nParams)
|
||||
if (nParams > L->top-L->Cstack.base)
|
||||
lua_error(L, "API error - wrong number of arguments in C2lua stack");
|
||||
}
|
||||
|
||||
|
||||
static lua_Object put_luaObject (lua_State *L, const TObject *o) {
|
||||
luaD_openstack(L, (L->stack.top-L->stack.stack)-L->Cstack.base);
|
||||
L->stack.stack[L->Cstack.base++] = *o;
|
||||
return L->Cstack.base; /* this is +1 real position (see Ref) */
|
||||
luaD_openstack(L, L->Cstack.base);
|
||||
*L->Cstack.base++ = *o;
|
||||
return Ref(L, L->Cstack.base-1);
|
||||
}
|
||||
|
||||
|
||||
lua_Object luaA_putObjectOnTop (lua_State *L) {
|
||||
luaD_openstack(L, (L->stack.top-L->stack.stack)-L->Cstack.base);
|
||||
L->stack.stack[L->Cstack.base++] = *(--L->stack.top);
|
||||
return L->Cstack.base; /* this is +1 real position (see Ref) */
|
||||
luaD_openstack(L, L->Cstack.base);
|
||||
*L->Cstack.base++ = *(--L->top);
|
||||
return Ref(L, L->Cstack.base-1);
|
||||
}
|
||||
|
||||
|
||||
static void top2LC (lua_State *L, int n) {
|
||||
/* Put the 'n' elements on the top as the Lua2C contents */
|
||||
L->Cstack.base = (L->stack.top-L->stack.stack); /* new base */
|
||||
L->Cstack.base = L->top; /* new base */
|
||||
L->Cstack.lua2C = L->Cstack.base-n; /* position of the new results */
|
||||
L->Cstack.num = n; /* number of results */
|
||||
}
|
||||
@@ -98,13 +98,11 @@ lua_Object lua_pop (lua_State *L) {
|
||||
|
||||
/*
|
||||
** Get a parameter, returning the object handle or LUA_NOOBJECT on error.
|
||||
** 'number' must be 1 to get the first parameter.
|
||||
** `number' must be 1 to get the first parameter.
|
||||
*/
|
||||
lua_Object lua_lua2C (lua_State *L, int number) {
|
||||
if (number <= 0 || number > L->Cstack.num) return LUA_NOOBJECT;
|
||||
/* Ref(L, L->stack.stack+(L->Cstack.lua2C+number-1)) ==
|
||||
L->stack.stack+(L->Cstack.lua2C+number-1)-L->stack.stack+1 == */
|
||||
return L->Cstack.lua2C+number;
|
||||
return Ref(L, L->Cstack.lua2C+number-1);
|
||||
}
|
||||
|
||||
|
||||
@@ -112,8 +110,8 @@ int lua_callfunction (lua_State *L, lua_Object function) {
|
||||
if (function == LUA_NOOBJECT)
|
||||
return 1;
|
||||
else {
|
||||
luaD_openstack(L, (L->stack.top-L->stack.stack)-L->Cstack.base);
|
||||
set_normalized(L->stack.stack+L->Cstack.base, Address(L, function));
|
||||
luaD_openstack(L, L->Cstack.base);
|
||||
set_normalized(L->Cstack.base, Address(L, function));
|
||||
return luaD_protectedrun(L);
|
||||
}
|
||||
}
|
||||
@@ -126,7 +124,7 @@ lua_Object lua_gettagmethod (lua_State *L, int tag, const char *event) {
|
||||
|
||||
lua_Object lua_settagmethod (lua_State *L, int tag, const char *event) {
|
||||
checkCparams(L, 1);
|
||||
luaT_settagmethod(L, tag, event, L->stack.top-1);
|
||||
luaT_settagmethod(L, tag, event, L->top-1);
|
||||
return luaA_putObjectOnTop(L);
|
||||
}
|
||||
|
||||
@@ -149,24 +147,24 @@ lua_Object lua_gettable (lua_State *L) {
|
||||
|
||||
lua_Object lua_rawgettable (lua_State *L) {
|
||||
checkCparams(L, 2);
|
||||
if (ttype(L->stack.top-2) != LUA_T_ARRAY)
|
||||
if (ttype(L->top-2) != LUA_T_ARRAY)
|
||||
lua_error(L, "indexed expression not a table in rawgettable");
|
||||
*(L->stack.top-2) = *luaH_get(L, avalue(L->stack.top-2), L->stack.top-1);
|
||||
--L->stack.top;
|
||||
*(L->top-2) = *luaH_get(L, avalue(L->top-2), L->top-1);
|
||||
--L->top;
|
||||
return luaA_putObjectOnTop(L);
|
||||
}
|
||||
|
||||
|
||||
void lua_settable (lua_State *L) {
|
||||
checkCparams(L, 3);
|
||||
luaV_settable(L, L->stack.top-3);
|
||||
L->stack.top -= 2; /* pop table and index */
|
||||
luaV_settable(L, L->top-3);
|
||||
L->top -= 2; /* pop table and index */
|
||||
}
|
||||
|
||||
|
||||
void lua_rawsettable (lua_State *L) {
|
||||
checkCparams(L, 3);
|
||||
luaV_rawsettable(L, L->stack.top-3);
|
||||
luaV_rawsettable(L, L->top-3);
|
||||
}
|
||||
|
||||
|
||||
@@ -202,7 +200,7 @@ void lua_setglobal (lua_State *L, const char *name) {
|
||||
void lua_rawsetglobal (lua_State *L, const char *name) {
|
||||
GlobalVar *gv = luaS_assertglobalbyname(L, name);
|
||||
checkCparams(L, 1);
|
||||
gv->value = *(--L->stack.top);
|
||||
gv->value = *(--L->top);
|
||||
}
|
||||
|
||||
|
||||
@@ -280,19 +278,19 @@ lua_CFunction lua_getcfunction (lua_State *L, lua_Object obj) {
|
||||
|
||||
|
||||
void lua_pushnil (lua_State *L) {
|
||||
ttype(L->stack.top) = LUA_T_NIL;
|
||||
ttype(L->top) = LUA_T_NIL;
|
||||
incr_top;
|
||||
}
|
||||
|
||||
void lua_pushnumber (lua_State *L, double n) {
|
||||
ttype(L->stack.top) = LUA_T_NUMBER;
|
||||
nvalue(L->stack.top) = n;
|
||||
ttype(L->top) = LUA_T_NUMBER;
|
||||
nvalue(L->top) = n;
|
||||
incr_top;
|
||||
}
|
||||
|
||||
void lua_pushlstring (lua_State *L, const char *s, long len) {
|
||||
tsvalue(L->stack.top) = luaS_newlstr(L, s, len);
|
||||
ttype(L->stack.top) = LUA_T_STRING;
|
||||
tsvalue(L->top) = luaS_newlstr(L, s, len);
|
||||
ttype(L->top) = LUA_T_STRING;
|
||||
incr_top;
|
||||
luaC_checkGC(L);
|
||||
}
|
||||
@@ -308,8 +306,8 @@ void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
|
||||
if (fn == NULL)
|
||||
lua_error(L, "API error - attempt to push a NULL Cfunction");
|
||||
checkCparams(L, n);
|
||||
ttype(L->stack.top) = LUA_T_CPROTO;
|
||||
fvalue(L->stack.top) = fn;
|
||||
ttype(L->top) = LUA_T_CPROTO;
|
||||
fvalue(L->top) = fn;
|
||||
incr_top;
|
||||
luaV_closure(L, n);
|
||||
luaC_checkGC(L);
|
||||
@@ -318,21 +316,21 @@ void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
|
||||
void lua_pushusertag (lua_State *L, void *u, int tag) {
|
||||
if (tag < 0 && tag != LUA_ANYTAG)
|
||||
luaT_realtag(L, tag); /* error if tag is not valid */
|
||||
tsvalue(L->stack.top) = luaS_createudata(L, u, tag);
|
||||
ttype(L->stack.top) = LUA_T_USERDATA;
|
||||
tsvalue(L->top) = luaS_createudata(L, u, tag);
|
||||
ttype(L->top) = LUA_T_USERDATA;
|
||||
incr_top;
|
||||
luaC_checkGC(L);
|
||||
}
|
||||
|
||||
void luaA_pushobject (lua_State *L, const TObject *o) {
|
||||
*L->stack.top = *o;
|
||||
*L->top = *o;
|
||||
incr_top;
|
||||
}
|
||||
|
||||
void lua_pushobject (lua_State *L, lua_Object o) {
|
||||
if (o == LUA_NOOBJECT)
|
||||
lua_error(L, "API error - attempt to push a NOOBJECT");
|
||||
set_normalized(L->stack.top, Address(L, o));
|
||||
set_normalized(L->top, Address(L, o));
|
||||
incr_top;
|
||||
}
|
||||
|
||||
@@ -368,18 +366,18 @@ int lua_tag (lua_State *L, lua_Object lo) {
|
||||
void lua_settag (lua_State *L, int tag) {
|
||||
checkCparams(L, 1);
|
||||
luaT_realtag(L, tag);
|
||||
switch (ttype(L->stack.top-1)) {
|
||||
switch (ttype(L->top-1)) {
|
||||
case LUA_T_ARRAY:
|
||||
(L->stack.top-1)->value.a->htag = tag;
|
||||
(L->top-1)->value.a->htag = tag;
|
||||
break;
|
||||
case LUA_T_USERDATA:
|
||||
(L->stack.top-1)->value.ts->u.d.tag = tag;
|
||||
(L->top-1)->value.ts->u.d.tag = tag;
|
||||
break;
|
||||
default:
|
||||
luaL_verror(L, "cannot change the tag of a %.20s",
|
||||
luaO_typename(L, L->stack.top-1));
|
||||
luaO_typename(L, L->top-1));
|
||||
}
|
||||
L->stack.top--;
|
||||
L->top--;
|
||||
}
|
||||
|
||||
|
||||
@@ -395,7 +393,7 @@ GlobalVar *luaA_nextvar (lua_State *L, TaggedString *ts) {
|
||||
while (gv && gv->value.ttype == LUA_T_NIL) /* skip globals with nil */
|
||||
gv = gv->next;
|
||||
if (gv) {
|
||||
ttype(L->stack.top) = LUA_T_STRING; tsvalue(L->stack.top) = gv->name;
|
||||
ttype(L->top) = LUA_T_STRING; tsvalue(L->top) = gv->name;
|
||||
incr_top;
|
||||
luaA_pushobject(L, &gv->value);
|
||||
}
|
||||
@@ -479,12 +477,12 @@ int lua_setdebug (lua_State *L, int debug) {
|
||||
|
||||
|
||||
lua_Function lua_stackedfunction (lua_State *L, int level) {
|
||||
StkId i;
|
||||
for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--) {
|
||||
int t = L->stack.stack[i].ttype;
|
||||
int i;
|
||||
for (i = (L->top-1)-L->stack; i>=0; i--) {
|
||||
int t = L->stack[i].ttype;
|
||||
if (t == LUA_T_CLMARK || t == LUA_T_PMARK || t == LUA_T_CMARK)
|
||||
if (level-- == 0)
|
||||
return Ref(L, L->stack.stack+i);
|
||||
return Ref(L, L->stack+i);
|
||||
}
|
||||
return LUA_NOOBJECT;
|
||||
}
|
||||
@@ -498,8 +496,7 @@ int lua_nups (lua_State *L, lua_Function func) {
|
||||
|
||||
int lua_currentline (lua_State *L, lua_Function func) {
|
||||
const TObject *f = Address(L, func);
|
||||
return (f+1 < L->stack.top && (f+1)->ttype == LUA_T_LINE) ?
|
||||
(f+1)->value.i : -1;
|
||||
return (f+1 < L->top && (f+1)->ttype == LUA_T_LINE) ? (f+1)->value.i : -1;
|
||||
}
|
||||
|
||||
|
||||
@@ -533,11 +530,11 @@ int lua_setlocal (lua_State *L, lua_Function func, int local_number) {
|
||||
const char *name = luaF_getlocalname(fp, local_number,
|
||||
lua_currentline(L, func));
|
||||
checkCparams(L, 1);
|
||||
--L->stack.top;
|
||||
--L->top;
|
||||
if (name) {
|
||||
/* if "name", there must be a LUA_T_LINE */
|
||||
/* therefore, f+2 points to function base */
|
||||
*((f+2)+(local_number-1)) = *L->stack.top;
|
||||
*((f+2)+(local_number-1)) = *L->top;
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
@@ -565,14 +562,14 @@ void lua_funcinfo (lua_State *L, lua_Object func,
|
||||
|
||||
|
||||
static int checkfunc (lua_State *L, TObject *o) {
|
||||
return luaO_equalObj(o, L->stack.top);
|
||||
return luaO_equalObj(o, L->top);
|
||||
}
|
||||
|
||||
|
||||
const char *lua_getobjname (lua_State *L, lua_Object o, const char **name) {
|
||||
/* try to find a name for given function */
|
||||
GlobalVar *g;
|
||||
set_normalized(L->stack.top, Address(L, o)); /* to be used by `checkfunc' */
|
||||
set_normalized(L->top, Address(L, o)); /* to be used by `checkfunc' */
|
||||
for (g=L->rootglobal; g; g=g->next) {
|
||||
if (checkfunc(L, &g->value)) {
|
||||
*name = g->name->str;
|
||||
@@ -610,7 +607,7 @@ void lua_beginblock (lua_State *L) {
|
||||
void lua_endblock (lua_State *L) {
|
||||
--L->numCblocks;
|
||||
L->Cstack = L->Cblocks[L->numCblocks];
|
||||
luaD_adjusttop(L, L->Cstack.base);
|
||||
L->top = L->Cstack.base;
|
||||
}
|
||||
|
||||
|
||||
@@ -618,8 +615,8 @@ void lua_endblock (lua_State *L) {
|
||||
int lua_ref (lua_State *L, int lock) {
|
||||
int ref;
|
||||
checkCparams(L, 1);
|
||||
ref = luaR_ref(L, L->stack.top-1, lock);
|
||||
L->stack.top--;
|
||||
ref = luaR_ref(L, L->top-1, lock);
|
||||
L->top--;
|
||||
return ref;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user