[njs] Fixed equality operator with object and string.

Valentin Bartenev vbart at nginx.com
Fri Nov 9 16:23:45 UTC 2018


details:   http://hg.nginx.org/njs/rev/7d06ada396d7
branches:  
changeset: 646:7d06ada396d7
user:      Valentin Bartenev <vbart at nginx.com>
date:      Fri Nov 09 18:22:01 2018 +0300
description:
Fixed equality operator with object and string.

diffstat:

 njs/njs_vm.c             |  25 ++++++++++++++++++++++---
 njs/test/njs_unit_test.c |  19 +++++++++++++++++++
 2 files changed, 41 insertions(+), 3 deletions(-)

diffs (81 lines):

diff -r 025b22bf8c49 -r 7d06ada396d7 njs/njs_vm.c
--- a/njs/njs_vm.c	Thu Nov 08 20:32:45 2018 +0300
+++ b/njs/njs_vm.c	Fri Nov 09 18:22:01 2018 +0300
@@ -1519,7 +1519,8 @@ njs_vmcode_not_equal(njs_vm_t *vm, njs_v
 static nxt_noinline njs_ret_t
 njs_values_equal(njs_vm_t *vm, const njs_value_t *val1, const njs_value_t *val2)
 {
-    nxt_bool_t  nv1, nv2;
+    nxt_bool_t         nv1, nv2;
+    const njs_value_t  *hv, *lv;
 
     nv1 = njs_is_null_or_void(val1);
     nv2 = njs_is_null_or_void(val2);
@@ -1543,11 +1544,29 @@ njs_values_equal(njs_vm_t *vm, const njs
         return (val1->data.u.object == val2->data.u.object);
     }
 
-    if (njs_is_object(val1) && njs_is_object(val2)) {
+    /* Sort values as: numeric < string < objects. */
+
+    if (val1->type > val2->type) {
+        hv = val1;
+        lv = val2;
+
+    } else {
+        hv = val2;
+        lv = val1;
+    }
+
+    /* If "lv" is an object then "hv" can only be another object. */
+    if (njs_is_object(lv)) {
         return 0;
     }
 
-    return njs_trap(vm, NJS_TRAP_NUMBERS);
+    /* If "hv" is a string then "lv" can only be a numeric. */
+    if (njs_is_string(hv)) {
+        return (lv->data.u.number == njs_string_to_number(hv, 0));
+    }
+
+    /* "hv" is an object and "lv" is either a string or a numeric. */
+    return njs_trap(vm, NJS_TRAP_COMPARISON);
 }
 
 
diff -r 025b22bf8c49 -r 7d06ada396d7 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Thu Nov 08 20:32:45 2018 +0300
+++ b/njs/test/njs_unit_test.c	Fri Nov 09 18:22:01 2018 +0300
@@ -1142,6 +1142,15 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("var a = Object; a == Object"),
       nxt_string("true") },
 
+    { nxt_string("'1' == new Number(1)"),
+      nxt_string("true") },
+
+    { nxt_string("new String('abc') == 'abc'"),
+      nxt_string("true") },
+
+    { nxt_string("false == new String('0')"),
+      nxt_string("true") },
+
     { nxt_string("var a = { valueOf: function() { return 5 } };   a == 5"),
       nxt_string("true") },
 
@@ -1151,6 +1160,16 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("var a = { valueOf: function() { return '5' } }; a == '5'"),
       nxt_string("true") },
 
+    { nxt_string("var a = { valueOf: function() { return 5 } }; a == '5'"),
+      nxt_string("true") },
+
+    { nxt_string("var a = { toString: function() { return true } }; '1' == a"),
+      nxt_string("true") },
+
+    { nxt_string("var a = { valueOf: function() { return 'b' },"
+                 "          toString: function() { return 'a' } }; a == 'a'"),
+      nxt_string("false") },
+
     /* Comparisions. */
 
     { nxt_string("1 < 2"),


More information about the nginx-devel mailing list