From 034f16892eb49361ee66f8d89aec26b071c98f57 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 11 Dec 1997 12:48:46 -0200 Subject: [PATCH] better treatment of MARKs and DEBUG cases. --- lapi.c | 52 +++++++++++++++++++++++++++++++++------------------- lgc.c | 10 +++++++--- lstate.c | 4 ++-- ltm.c | 8 ++++++-- lua.c | 4 ++-- 5 files changed, 50 insertions(+), 28 deletions(-) diff --git a/lapi.c b/lapi.c index 63409950..51b6159b 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 1.11 1997/11/28 16:56:05 roberto Exp roberto $ +** $Id: lapi.c,v 1.12 1997/12/09 13:35:19 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -35,6 +35,25 @@ TObject *luaA_Address (lua_Object o) } +static int normalized_type (TObject *o) +{ + int t = ttype(o); + switch (t) { + case LUA_T_MARK: + return LUA_T_FUNCTION; + default: + return t; + } +} + + +static void set_normalized (TObject *d, TObject *s) +{ + d->value = s->value; + d->ttype = normalized_type(s); +} + + void luaA_packresults (void) { luaV_pack(L->Cstack.lua2C, L->Cstack.num, L->stack.top); @@ -101,7 +120,7 @@ int lua_callfunction (lua_Object function) return 1; else { luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base); - L->stack.stack[L->Cstack.base] = *Address(function); + set_normalized(L->stack.stack+L->Cstack.base, Address(function)); return luaD_protectedrun(MULT_RET); } } @@ -244,8 +263,7 @@ int lua_isstring (lua_Object o) int lua_isfunction (lua_Object o) { - return (o != LUA_NOOBJECT) && ((ttype(Address(o)) == LUA_T_FUNCTION) || - (ttype(Address(o)) == LUA_T_MARK)); + return (o != LUA_NOOBJECT) && (normalized_type(Address(o)) == LUA_T_FUNCTION); } @@ -334,10 +352,10 @@ void lua_pushobject (lua_Object o) { if (o == LUA_NOOBJECT) lua_error("API error - attempt to push a NOOBJECT"); - *L->stack.top = *Address(o); - if (ttype(L->stack.top) == LUA_T_MARK) - ttype(L->stack.top) = LUA_T_FUNCTION; - incr_top; + else { + set_normalized(L->stack.top, Address(o)); + incr_top; + } } @@ -406,11 +424,11 @@ int lua_currentline (lua_Function func) lua_Object lua_getlocal (lua_Function func, int local_number, char **name) { - TObject *f = luaA_Address(func); - /* check whether func is a Lua function */ - if (!(f->ttype == LUA_T_MARK || f->ttype == LUA_T_FUNCTION)) + /* check whether func is a function */ + if (!lua_isfunction(func)) return LUA_NOOBJECT; else { + TObject *f = luaA_Address(func); TProtoFunc *fp = protovalue(f)->value.tf; *name = luaF_getlocalname(fp, local_number, lua_currentline(func)); if (*name) { @@ -444,11 +462,10 @@ int lua_setlocal (lua_Function func, int local_number) void lua_funcinfo (lua_Object func, char **filename, int *linedefined) { - TObject *f = Address(func); - if (!(ttype(f) == LUA_T_MARK || ttype(f) == LUA_T_FUNCTION)) + if (!lua_isfunction(func)) lua_error("API - `funcinfo' called with a non-function value"); else { - f = protovalue(f); + TObject *f = protovalue(Address(func)); if (ttype(f) == LUA_T_PROTO) { *filename = tfvalue(f)->fileName->str; *linedefined = tfvalue(f)->lineDefined; @@ -463,16 +480,13 @@ void lua_funcinfo (lua_Object func, char **filename, int *linedefined) static int checkfunc (TObject *o) { - return o->ttype == LUA_T_FUNCTION && - (ttype(L->stack.top) == LUA_T_FUNCTION || - ttype(L->stack.top) == LUA_T_MARK) && - clvalue(L->stack.top) == o->value.cl; + return luaO_equalObj(o, L->stack.top); } char *lua_getobjname (lua_Object o, char **name) { /* try to find a name for given function */ - *(L->stack.top) = *Address(o); /* to be accessed by "checkfunc */ + set_normalized(L->stack.top, Address(o)); /* to be accessed by "checkfunc */ if ((*name = luaT_travtagmethods(checkfunc)) != NULL) return "tag-method"; else if ((*name = luaS_travsymbol(checkfunc)) != NULL) diff --git a/lgc.c b/lgc.c index dac3b632..19f8623d 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 1.10 1997/12/01 20:31:25 roberto Exp roberto $ +** $Id: lgc.c,v 1.11 1997/12/09 13:35:19 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -83,15 +83,19 @@ static void travlock (void) static int ismarked (TObject *o) { + /* valid only for locked objects */ switch (o->ttype) { case LUA_T_STRING: case LUA_T_USERDATA: return o->value.ts->head.marked; case LUA_T_FUNCTION: return o->value.cl->head.marked; - case LUA_T_PROTO: - return o->value.tf->head.marked; case LUA_T_ARRAY: return o->value.a->head.marked; +#ifdef DEBUG + case LUA_T_LINE: case LUA_T_MARK: + case LUA_T_PROTO: case LUA_T_CPROTO: + lua_error("internal error"); +#endif default: /* nil, number or cproto */ return 1; } diff --git a/lstate.c b/lstate.c index 9fd27212..e1011a1a 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 1.2 1997/11/27 15:59:25 roberto Exp roberto $ +** $Id: lstate.c,v 1.3 1997/12/01 20:31:25 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -69,7 +69,7 @@ void lua_close (void) luaM_free(L->Mbuffer); luaM_free(L); L = NULL; -#if DEBUG +#ifdef DEBUG printf("total de blocos: %ld\n", numblocks); printf("total de memoria: %ld\n", totalmem); #endif diff --git a/ltm.c b/ltm.c index 49ecfc01..ac2160ac 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 1.8 1997/11/19 17:29:23 roberto Exp roberto $ +** $Id: ltm.c,v 1.9 1997/11/19 18:16:33 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -106,8 +106,12 @@ int luaT_efectivetag (TObject *o) } case LUA_T_ARRAY: return o->value.a->htag; - case LUA_T_FUNCTION: case LUA_T_MARK: + case LUA_T_FUNCTION: case LUA_T_MARK: return o->value.cl->consts[0].ttype; +#ifdef DEBUG + case LUA_T_LINE: case LUA_T_PROTO: case LUA_T_CPROTO: + lua_error("internal error"); +#endif default: return t; } diff --git a/lua.c b/lua.c index d2749ca4..8221e5bb 100644 --- a/lua.c +++ b/lua.c @@ -1,5 +1,5 @@ /* -** $Id: lua.c,v 1.6 1997/12/01 20:31:25 roberto Exp roberto $ +** $Id: lua.c,v 1.7 1997/12/03 19:57:54 roberto Exp roberto $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ @@ -100,7 +100,7 @@ int main (int argc, char *argv[]) } } } -#if DEBUG +#ifdef DEBUG lua_close(); #endif return 0;