From 1d027f08c545f8ce4967c7db2b556f933521b506 Mon Sep 17 00:00:00 2001 From: Ry Dahl Date: Mon, 23 Dec 2019 07:32:45 -0500 Subject: [PATCH] Add Value::strict_equals and Value::same_value (#121) --- src/binding.cc | 8 ++++++++ src/value.rs | 11 +++++++++++ tests/test_api.rs | 25 +++++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/src/binding.cc b/src/binding.cc index 4e04e01f..2d265bcb 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -202,6 +202,14 @@ bool v8__Value__IsString(const v8::Value& self) { return self.IsString(); } bool v8__Value__IsNumber(const v8::Value& self) { return self.IsNumber(); } +bool v8__Value__StrictEquals(const v8::Value& self, v8::Value* that) { + return self.StrictEquals(ptr_to_local(that)); +} + +bool v8__Value__SameValue(const v8::Value& self, v8::Value* that) { + return self.SameValue(ptr_to_local(that)); +} + v8::Primitive* v8__Null(v8::Isolate* isolate) { return local_to_ptr(v8::Null(isolate)); } diff --git a/src/value.rs b/src/value.rs index c27ee8ac..5ae20e66 100644 --- a/src/value.rs +++ b/src/value.rs @@ -1,4 +1,5 @@ use crate::support; +use crate::Local; extern "C" { fn v8__Value__IsUndefined(this: &Value) -> bool; @@ -6,6 +7,8 @@ extern "C" { fn v8__Value__IsNullOrUndefined(this: &Value) -> bool; fn v8__Value__IsString(this: &Value) -> bool; fn v8__Value__IsNumber(this: &Value) -> bool; + fn v8__Value__StrictEquals(this: &Value, that: &Value) -> bool; + fn v8__Value__SameValue(this: &Value, that: &Value) -> bool; } /// The superclass of all JavaScript values and objects. @@ -39,4 +42,12 @@ impl Value { pub fn is_number(&self) -> bool { unsafe { v8__Value__IsNumber(self) } } + + pub fn strict_equals<'sc>(&self, that: Local<'sc, Value>) -> bool { + unsafe { v8__Value__StrictEquals(self, &that) } + } + + pub fn same_value<'sc>(&self, that: Local<'sc, Value>) -> bool { + unsafe { v8__Value__SameValue(self, &that) } + } } diff --git a/tests/test_api.rs b/tests/test_api.rs index 1d541862..2567d287 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -872,3 +872,28 @@ fn ui() { let t = trybuild::TestCases::new(); t.compile_fail("tests/compile_fail/*.rs"); } + +#[test] +fn equality() { + let g = setup(); + let mut params = v8::Isolate::create_params(); + params.set_array_buffer_allocator(v8::Allocator::new_default_allocator()); + let mut isolate = v8::Isolate::new(params); + isolate.enter(); + let mut locker = v8::Locker::new(&isolate); + v8::HandleScope::enter(&mut locker, |scope| { + let mut context = v8::Context::new(scope); + context.enter(); + + assert!(v8_str(scope, "a").strict_equals(v8_str(scope, "a").into())); + assert!(!v8_str(scope, "a").strict_equals(v8_str(scope, "b").into())); + + assert!(v8_str(scope, "a").same_value(v8_str(scope, "a").into())); + assert!(!v8_str(scope, "a").same_value(v8_str(scope, "b").into())); + + context.exit(); + }); + drop(locker); + isolate.exit(); + drop(g); +}