0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2025-01-22 23:20:03 -05:00

Add Value::strict_equals and Value::same_value (#121)

This commit is contained in:
Ry Dahl 2019-12-23 07:32:45 -05:00 committed by GitHub
parent 3ec3b07638
commit 1d027f08c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 0 deletions

View file

@ -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));
}

View file

@ -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) }
}
}

View file

@ -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);
}