Revision: 16924
Author:   [email protected]
Date:     Tue Sep 24 16:51:43 2013 UTC
Log:      Expose SameValue equality comparison algorithm

Since SameValue algorithm is defined formally in ECMA262 and V8 already
exported Equals and StrictEquals algorithms, SameValue should be exposed.
And in this issue, we fix the issue of Object::SameValue implementation,
SameValue(0.0, -0.0) returnes true.

BUG=v8:2909
TEST=cctest/test-api/Equality
[email protected]

Review URL: https://codereview.chromium.org/24360017

Patch from Yusuke Suzuki <[email protected]>.
http://code.google.com/p/v8/source/detail?r=16924

Modified:
 /branches/bleeding_edge/include/v8.h
 /branches/bleeding_edge/src/api.cc
 /branches/bleeding_edge/src/objects.cc
 /branches/bleeding_edge/test/cctest/test-api.cc

=======================================
--- /branches/bleeding_edge/include/v8.h        Mon Sep 23 14:43:41 2013 UTC
+++ /branches/bleeding_edge/include/v8.h        Tue Sep 24 16:51:43 2013 UTC
@@ -1479,6 +1479,7 @@
   /** JS == */
   bool Equals(Handle<Value> that) const;
   bool StrictEquals(Handle<Value> that) const;
+  bool SameValue(Handle<Value> that) const;

   template <class T> V8_INLINE static Value* Cast(T* value);

=======================================
--- /branches/bleeding_edge/src/api.cc  Tue Sep 24 11:40:28 2013 UTC
+++ /branches/bleeding_edge/src/api.cc  Tue Sep 24 16:51:43 2013 UTC
@@ -3053,6 +3053,19 @@
     return false;
   }
 }
+
+
+bool Value::SameValue(Handle<Value> that) const {
+  i::Isolate* isolate = i::Isolate::Current();
+  if (EmptyCheck("v8::Value::SameValue()", this) ||
+      EmptyCheck("v8::Value::SameValue()", that)) {
+    return false;
+  }
+  LOG_API(isolate, "SameValue");
+  i::Handle<i::Object> obj = Utils::OpenHandle(this);
+  i::Handle<i::Object> other = Utils::OpenHandle(*that);
+  return obj->SameValue(*other);
+}


 uint32_t Value::Uint32Value() const {
=======================================
--- /branches/bleeding_edge/src/objects.cc      Mon Sep 23 19:56:36 2013 UTC
+++ /branches/bleeding_edge/src/objects.cc      Tue Sep 24 16:51:43 2013 UTC
@@ -1005,8 +1005,11 @@
   if (IsNumber() && other->IsNumber()) {
     double this_value = Number();
     double other_value = other->Number();
-    return (this_value == other_value) ||
-        (std::isnan(this_value) && std::isnan(other_value));
+    bool equal = this_value == other_value;
+    // SameValue(NaN, NaN) is true.
+    if (!equal) return std::isnan(this_value) && std::isnan(other_value);
+    // SameValue(0.0, -0.0) is false.
+    return (this_value != 0) || ((1 / this_value) == (1 / other_value));
   }
   if (IsString() && other->IsString()) {
     return String::cast(this)->Equals(String::cast(other));
=======================================
--- /branches/bleeding_edge/test/cctest/test-api.cc Mon Sep 23 11:27:07 2013 UTC +++ /branches/bleeding_edge/test/cctest/test-api.cc Tue Sep 24 16:51:43 2013 UTC
@@ -4973,7 +4973,7 @@
   CHECK(!v8_str("5")->StrictEquals(v8_num(5)));
   CHECK(v8_num(1)->StrictEquals(v8_num(1)));
   CHECK(!v8_num(1)->StrictEquals(v8_num(2)));
-  CHECK(v8_num(0)->StrictEquals(v8_num(-0)));
+  CHECK(v8_num(0.0)->StrictEquals(v8_num(-0.0)));
   Local<Value> not_a_number = v8_num(i::OS::nan_value());
   CHECK(!not_a_number->StrictEquals(not_a_number));
   CHECK(v8::False()->StrictEquals(v8::False()));
@@ -4983,6 +4983,16 @@
   v8::Persistent<v8::Object> alias(isolate, obj);
   CHECK(v8::Local<v8::Object>::New(isolate, alias)->StrictEquals(obj));
   alias.Dispose();
+
+  CHECK(v8_str("a")->SameValue(v8_str("a")));
+  CHECK(!v8_str("a")->SameValue(v8_str("b")));
+  CHECK(!v8_str("5")->SameValue(v8_num(5)));
+  CHECK(v8_num(1)->SameValue(v8_num(1)));
+  CHECK(!v8_num(1)->SameValue(v8_num(2)));
+  CHECK(!v8_num(0.0)->SameValue(v8_num(-0.0)));
+  CHECK(not_a_number->SameValue(not_a_number));
+  CHECK(v8::False()->SameValue(v8::False()));
+  CHECK(!v8::False()->SameValue(v8::Undefined()));
 }


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to