Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add v8::Value::InstanceOf bindings #879

Merged
merged 11 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>* out) {
v8::Value* self_non_const = const_cast<v8::Value*>(&self);
*out = self_non_const.InstanceOf(ptr_to_local(&context), ptr_to_local(&object));
littledivy marked this conversation as resolved.
Show resolved Hide resolved
}

void v8__Value__NumberValue(const v8::Value& self, const v8::Context& context,
v8::Maybe<double>* out) {
*out = self.NumberValue(ptr_to_local(&context));
Expand Down
1 change: 1 addition & 0 deletions src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::hash::Hasher;
use std::marker::PhantomData;
use std::mem::transmute;
use std::ops::Deref;
use std::ops::DerefMut;
littledivy marked this conversation as resolved.
Show resolved Hide resolved
use std::ptr::NonNull;

use crate::Data;
Expand Down
24 changes: 23 additions & 1 deletion src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>,
);
fn v8__Value__ToBigInt(
this: *const Value,
context: *const Context,
Expand Down Expand Up @@ -551,6 +556,23 @@ impl Value {
.unwrap()
}

pub fn instance_of<'s>(
&self,
scope: &mut HandleScope<'s>,
object: Local<Object>,
) -> Option<bool> {
let mut out = Maybe::<bool>::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<f64> {
let mut out = Maybe::<f64>::default();
unsafe {
Expand Down
19 changes: 19 additions & 0 deletions tests/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<v8::Object>::try_from(array_constructor).unwrap();
let mut array: v8::Local<v8::Value> =
v8::Array::new_with_elements(&mut scope, &[]).into();

assert!(array.instance_of(&mut scope, array_constructor).unwrap());
}