From 0dbf0c5953a3d72deebc7e41840a0e73b46de8bc Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 6 May 2002 12:51:41 -0300 Subject: new format for test intructions (handle NaN correctly) --- lvm.c | 68 ++++++++++++++++++++++++++++--------------------------------------- 1 file changed, 28 insertions(+), 40 deletions(-) (limited to 'lvm.c') diff --git a/lvm.c b/lvm.c index e44bc7dd..e867d870 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 1.227 2002/04/24 20:07:46 roberto Exp roberto $ +** $Id: lvm.c,v 1.228 2002/05/02 13:06:20 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -189,20 +189,21 @@ static void call_arith (lua_State *L, StkId p1, const TObject *p2, } -static int luaV_strlessthan (const TString *ls, const TString *rs) { +static int luaV_strcmp (const TString *ls, const TString *rs) { const char *l = getstr(ls); size_t ll = ls->tsv.len; const char *r = getstr(rs); size_t lr = rs->tsv.len; for (;;) { int temp = strcoll(l, r); - if (temp != 0) return (temp < 0); + if (temp < 0) return CMP_LT; + else if (temp > 0) return CMP_GT; else { /* strings are equal up to a `\0' */ size_t len = strlen(l); /* index of first `\0' in both strings */ if (len == lr) /* r is finished? */ - return 0; /* l is equal or greater than r */ + return (len == ll) ? CMP_EQ : CMP_GT; /* l is eq. or gt. than r */ else if (len == ll) /* l is finished? */ - return 1; /* l is smaller than r (because r is not finished) */ + return CMP_LT; /* l is smaller than r (because r is not finished) */ /* both strings longer than `len'; go on comparing (after the `\0') */ len++; l += len; ll -= len; r += len; lr -= len; @@ -211,15 +212,24 @@ static int luaV_strlessthan (const TString *ls, const TString *rs) { } -int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r) { - if (ttype(l) == LUA_TNUMBER && ttype(r) == LUA_TNUMBER) - return (nvalue(l) < nvalue(r)); +int luaV_cmp (lua_State *L, const TObject *l, const TObject *r, int cond) { + if (ttype(l) == LUA_TNUMBER && ttype(r) == LUA_TNUMBER) { + lua_Number n1 = nvalue(l); + lua_Number n2 = nvalue(r); + if (n1 < n2) return (cond & CMP_LT); + else if (n1 > n2) return (cond & CMP_GT); + else if (n1 == n2) return (cond & CMP_EQ); + else return (cond & CMP_N); + } else if (ttype(l) == LUA_TSTRING && ttype(r) == LUA_TSTRING) - return luaV_strlessthan(tsvalue(l), tsvalue(r)); + return luaV_strcmp(tsvalue(l), tsvalue(r)) & cond; else { /* try TM */ + if (cond & CMP_EQ ? cond & CMP_LT : cond & CMP_GT) { /* `<=' or `>' ? */ + const TObject *temp = l; l = r; r = temp; /* exchange terms */ + } if (!call_binTM(L, l, r, L->top, TM_LT)) luaG_ordererror(L, l, r); - return !l_isfalse(L->top); + return (cond & CMP_EQ) ? l_isfalse(L->top) : !l_isfalse(L->top); } } @@ -437,40 +447,18 @@ StkId luaV_execute (lua_State *L) { dojump(pc, GETARG_sBx(i)); break; } - case OP_TESTEQ: { /* skip next instruction if test fails */ - if (!luaO_equalObj(ra, RKC(i))) pc++; - break; - } - case OP_TESTNE: { - if (luaO_equalObj(ra, RKC(i))) pc++; - break; - } - case OP_TESTLT: { - if (!luaV_lessthan(L, ra, RKC(i))) pc++; - break; - } - case OP_TESTLE: { /* b <= c === !(c c === (c= c === !(b