Browse Source

small simplifications around 'luaT_callorderTM'

Roberto Ierusalimschy 7 years ago
parent
commit
73abfde2ef
2 changed files with 19 additions and 21 deletions
  1. 13 4
      ltm.c
  2. 6 17
      lvm.c

+ 13 - 4
ltm.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ltm.c,v 2.47 2017/11/07 13:25:26 roberto Exp roberto $
+** $Id: ltm.c,v 2.48 2017/11/08 14:50:23 roberto Exp $
 ** Tag methods
 ** See Copyright Notice in lua.h
 */
@@ -180,10 +180,19 @@ void luaT_trybiniTM (lua_State *L, const TValue *p1, int i2,
 
 int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
                       TMS event) {
-  if (!callbinTM(L, p1, p2, L->top, event))
-    return -1;  /* no metamethod */
-  else
+  if (callbinTM(L, p1, p2, L->top, event))  /* try original event */
     return !l_isfalse(s2v(L->top));
+  else if (event == TM_LE) {
+      /* try '!(p2 < p1)' for '(p1 <= p2)' */
+      L->ci->callstatus |= CIST_LEQ;  /* mark it is doing 'lt' for 'le' */
+      if (callbinTM(L, p2, p1, L->top, TM_LT)) {
+        L->ci->callstatus ^= CIST_LEQ;  /* clear mark */
+        return l_isfalse(s2v(L->top));
+      }
+      /* else error will remove this 'ci'; no need to clear mark */
+  }
+  luaG_ordererror(L, p1, p2);  /* no metamethod found */
+  return 0;  /* to avoid warnings */
 }
 
 

+ 6 - 17
lvm.c

@@ -1,8 +1,8 @@
 /*
 <<<<<<< lvm.c
-** $Id: lvm.c,v 2.313 2017/11/21 14:17:35 roberto Exp roberto $
+** $Id: lvm.c,v 2.316 2017/11/23 16:41:16 roberto Exp roberto $
 =======
-** $Id: lvm.c,v 2.315 2017/11/22 19:15:44 roberto Exp $
+** $Id: lvm.c,v 2.316 2017/11/23 16:41:16 roberto Exp roberto $
 >>>>>>> 2.315
 ** Lua virtual machine
 ** See Copyright Notice in lua.h
@@ -378,13 +378,11 @@ static int LEnum (const TValue *l, const TValue *r) {
 ** return 'l < r' for non-numbers.
 */
 static int lessthanothers (lua_State *L, const TValue *l, const TValue *r) {
-  int res;
   lua_assert(!ttisnumber(l) || !ttisnumber(r));
   if (ttisstring(l) && ttisstring(r))  /* both are strings? */
     return l_strcmp(tsvalue(l), tsvalue(r)) < 0;
-  else if ((res = luaT_callorderTM(L, l, r, TM_LT)) < 0)  /* no metamethod? */
-    luaG_ordererror(L, l, r);  /* error */
-  return res;
+  else
+    return luaT_callorderTM(L, l, r, TM_LT);
 }
 
 
@@ -407,20 +405,11 @@ int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
 ** keeps that information.
 */
 static int lessequalothers (lua_State *L, const TValue *l, const TValue *r) {
-  int res;
   lua_assert(!ttisnumber(l) || !ttisnumber(r));
   if (ttisstring(l) && ttisstring(r))  /* both are strings? */
     return l_strcmp(tsvalue(l), tsvalue(r)) <= 0;
-  else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0)  /* try 'le' */
-    return res;
-  else {  /* try 'lt': */
-    L->ci->callstatus |= CIST_LEQ;  /* mark it is doing 'lt' for 'le' */
-    res = luaT_callorderTM(L, r, l, TM_LT);
-    L->ci->callstatus ^= CIST_LEQ;  /* clear mark */
-    if (res < 0)
-      luaG_ordererror(L, l, r);
-    return !res;  /* result is negated */
-  }
+  else
+    return luaT_callorderTM(L, l, r, TM_LE);
 }