From 0f34359fb4aaae06a4fdffad51ebdb1d12123517 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Fri, 11 Feb 2022 08:27:56 +0530 Subject: [PATCH] Add v8::Value::InstanceOf bindings (#879) --- src/binding.cc | 6 ++++++ src/value.rs | 24 +++++++++++++++++++++++- tests/test_api.rs | 19 +++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/binding.cc b/src/binding.cc index 35996fbe..b17b8027 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -682,6 +682,12 @@ const v8::Boolean* v8__Value__ToBoolean(const v8::Value& self, return local_to_ptr(self.ToBoolean(isolate)); } +void v8__Value__InstanceOf(const v8::Value& self, const v8::Context& context, + const v8::Object& object, v8::Maybe* out) { + v8::Value* self_non_const = const_cast(&self); + *out = self_non_const->InstanceOf(ptr_to_local(&context), ptr_to_local(&object)); +} + void v8__Value__NumberValue(const v8::Value& self, const v8::Context& context, v8::Maybe* out) { *out = self.NumberValue(ptr_to_local(&context)); diff --git a/src/value.rs b/src/value.rs index 34c4ac61..38e12453 100644 --- a/src/value.rs +++ b/src/value.rs @@ -72,7 +72,12 @@ extern "C" { fn v8__Value__IsModuleNamespaceObject(this: *const Value) -> bool; fn v8__Value__StrictEquals(this: *const Value, that: *const Value) -> bool; fn v8__Value__SameValue(this: *const Value, that: *const Value) -> bool; - + fn v8__Value__InstanceOf( + this: *const Value, + context: *const Context, + object: *const Object, + out: *mut Maybe, + ); fn v8__Value__ToBigInt( this: *const Value, context: *const Context, @@ -551,6 +556,23 @@ impl Value { .unwrap() } + pub fn instance_of<'s>( + &self, + scope: &mut HandleScope<'s>, + object: Local, + ) -> Option { + let mut out = Maybe::::default(); + unsafe { + v8__Value__InstanceOf( + self, + &*scope.get_current_context(), + &*object, + &mut out, + ); + } + out.into() + } + pub fn number_value<'s>(&self, scope: &mut HandleScope<'s>) -> Option { let mut out = Maybe::::default(); unsafe { diff --git a/tests/test_api.rs b/tests/test_api.rs index a644cd59..05cd1324 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -5884,3 +5884,22 @@ fn current_stack_trace() { .unwrap(); assert_eq!(too_deep, 5); } + +#[test] +fn instance_of() { + let _setup_guard = setup(); + + let mut isolate = v8::Isolate::new(Default::default()); + let mut scope = v8::HandleScope::new(&mut isolate); + let context = v8::Context::new(&mut scope); + let mut scope = v8::ContextScope::new(&mut scope, context); + let global = context.global(&mut scope); + let array_name = v8::String::new(&mut scope, "Array").unwrap(); + let array_constructor = global.get(&mut scope, array_name.into()).unwrap(); + let array_constructor = + v8::Local::::try_from(array_constructor).unwrap(); + let array: v8::Local = + v8::Array::new_with_elements(&mut scope, &[]).into(); + + assert!(array.instance_of(&mut scope, array_constructor).unwrap()); +}