avoid gotos when possible

This commit is contained in:
Roberto Ierusalimschy 2002-06-14 14:21:32 -03:00
parent 8fd0f6a82b
commit c31494df26
1 changed files with 39 additions and 45 deletions

84
lvm.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 1.237 2002/06/12 14:51:31 roberto Exp roberto $ ** $Id: lvm.c,v 1.238 2002/06/13 13:39:55 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -118,32 +118,29 @@ static void callTM (lua_State *L, const TObject *f,
void luaV_gettable (lua_State *L, const TObject *t, TObject *key, StkId res) { void luaV_gettable (lua_State *L, const TObject *t, TObject *key, StkId res) {
const TObject *tm; const TObject *tm;
int loop = 0; int loop = 0;
init: do {
if (ttype(t) == LUA_TTABLE) { /* `t' is a table? */ if (ttype(t) == LUA_TTABLE) { /* `t' is a table? */
Table *h = hvalue(t); Table *h = hvalue(t);
Table *et = h->metatable; Table *et = h->metatable;
if ((tm = fasttm(L, et, TM_GETTABLE)) == NULL) { /* no gettable TM? */ if ((tm = fasttm(L, et, TM_GETTABLE)) == NULL) { /* no gettable TM? */
const TObject *v = luaH_get(h, key); /* do a primitive get */ const TObject *v = luaH_get(h, key); /* do a primitive get */
if (ttype(v) != LUA_TNIL || /* result is no nil ... */ if (ttype(v) != LUA_TNIL || /* result is no nil ... */
(tm = fasttm(L, et, TM_INDEX)) == NULL) { /* ... or no index TM? */ (tm = fasttm(L, et, TM_INDEX)) == NULL) { /* ... or no index TM? */
setobj(res, v); /* default get */ setobj(res, v); /* default get */
return; return;
}
} }
/* else will try the tag method */
} }
/* else will try the tag method */ else if (ttype(tm = luaT_gettmbyobj(L, t, TM_GETTABLE)) == LUA_TNIL)
} else { /* not a table; try a `gettable' tag method */
if (ttype(tm = luaT_gettmbyobj(L, t, TM_GETTABLE)) == LUA_TNIL) {
luaG_typeerror(L, t, "index"); luaG_typeerror(L, t, "index");
return; /* to avoid warnings */ if (ttype(tm) == LUA_TFUNCTION) {
callTMres(L, tm, t, key, res);
return;
} }
} t = tm; /* else repeat access with `tm' */
if (ttype(tm) == LUA_TFUNCTION) } while (++loop <= MAXTAGLOOP);
callTMres(L, tm, t, key, res); luaG_runerror(L, "loop in gettable");
else {
if (++loop == MAXTAGLOOP) luaG_runerror(L, "loop in gettable");
t = tm;
goto init; /* return luaV_gettable(L, tm, key, res); */
}
} }
@ -153,32 +150,29 @@ void luaV_gettable (lua_State *L, const TObject *t, TObject *key, StkId res) {
void luaV_settable (lua_State *L, const TObject *t, TObject *key, StkId val) { void luaV_settable (lua_State *L, const TObject *t, TObject *key, StkId val) {
const TObject *tm; const TObject *tm;
int loop = 0; int loop = 0;
init: do {
if (ttype(t) == LUA_TTABLE) { /* `t' is a table? */ if (ttype(t) == LUA_TTABLE) { /* `t' is a table? */
Table *h = hvalue(t); Table *h = hvalue(t);
Table *et = h->metatable; Table *et = h->metatable;
if ((tm = fasttm(L, et, TM_SETTABLE)) == NULL) { /* no settable TM? */ if ((tm = fasttm(L, et, TM_SETTABLE)) == NULL) { /* no settable TM? */
TObject *oldval = luaH_set(L, h, key); /* do a primitive set */ TObject *oldval = luaH_set(L, h, key); /* do a primitive set */
if (ttype(oldval) != LUA_TNIL || /* result is no nil ... */ if (ttype(oldval) != LUA_TNIL || /* result is no nil ... */
(tm = fasttm(L, et, TM_NEWINDEX)) == NULL) { /* ... or no TM? */ (tm = fasttm(L, et, TM_NEWINDEX)) == NULL) { /* ... or no TM? */
setobj(oldval, val); setobj(oldval, val);
return; return;
}
} }
/* else will try the tag method */
} }
/* else will try the tag method */ else if (ttype(tm = luaT_gettmbyobj(L, t, TM_SETTABLE)) == LUA_TNIL)
} else { /* `t' is not a table; try a `settable' tag method */
if (ttype(tm = luaT_gettmbyobj(L, t, TM_SETTABLE)) == LUA_TNIL) {
luaG_typeerror(L, t, "index"); luaG_typeerror(L, t, "index");
return; /* to avoid warnings */ if (ttype(tm) == LUA_TFUNCTION) {
callTM(L, tm, t, key, val);
return;
} }
} t = tm; /* else repeat with `tm' */
if (ttype(tm) == LUA_TFUNCTION) } while (++loop <= MAXTAGLOOP);
callTM(L, tm, t, key, val); luaG_runerror(L, "loop in settable");
else {
if (++loop == MAXTAGLOOP) luaG_runerror(L, "loop in settable");
t = tm;
goto init; /* luaV_settable(L, tm, key, val); */
}
} }