From 090511f02aaec9c60bc2d741d9d39bf73c22116c Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Sat, 22 Jan 2022 22:40:48 +0530 Subject: [PATCH 01/10] Add v8::Value::InstanceOf bindings --- src/binding.cc | 4 ++++ src/value.rs | 24 +++++++++++++++++++++++- tests/test_api.rs | 18 ++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/binding.cc b/src/binding.cc index 621fe304fb..30ddb57487 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -673,6 +673,10 @@ 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) { + *out = self.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 34c4ac612c..8201c01bdd 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 ac4693de0b..edc0aa6061 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -5772,3 +5772,21 @@ 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::Array::new_with_elements(&mut scope, &[]); + + assert!(array.instance_of(&mut scope, array_constructor).unwrap()); +} From e80a8b7d791d27c9e02e248154e968ffb19f72d2 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Sat, 22 Jan 2022 22:44:26 +0530 Subject: [PATCH 02/10] clang-format --- src/binding.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/binding.cc b/src/binding.cc index 30ddb57487..e003457088 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -673,7 +673,8 @@ 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) { +void v8__Value__InstanceOf(const v8::Value& self, const v8::Context& context, + const v8::Object& object, v8::Maybe* out) { *out = self.InstanceOf(ptr_to_local(&context), ptr_to_local(&object)); } From 2a1cb7ecbb9532f49053eff925dbe06e2436df4c Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Sat, 22 Jan 2022 22:56:51 +0530 Subject: [PATCH 03/10] wut --- src/binding.cc | 6 +++--- src/value.rs | 16 ++++------------ 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/binding.cc b/src/binding.cc index e003457088..06ef2dfc23 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -673,9 +673,9 @@ 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) { - *out = self.InstanceOf(ptr_to_local(&context), ptr_to_local(&object)); +bool v8__Value__InstanceOf(const v8::Value& self, const v8::Context& context, + const v8::Object& object) { + return self.InstanceOf(ptr_to_local(&context), ptr_to_local(&object)); } void v8__Value__NumberValue(const v8::Value& self, const v8::Context& context, diff --git a/src/value.rs b/src/value.rs index 8201c01bdd..d8daabdab0 100644 --- a/src/value.rs +++ b/src/value.rs @@ -76,8 +76,7 @@ extern "C" { this: *const Value, context: *const Context, object: *const Object, - out: *mut Maybe, - ); + ) -> bool; fn v8__Value__ToBigInt( this: *const Value, context: *const Context, @@ -560,17 +559,10 @@ impl Value { &self, scope: &mut HandleScope<'s>, object: Local, - ) -> Option { - let mut out = Maybe::::default(); + ) -> bool { unsafe { - v8__Value__InstanceOf( - self, - &*scope.get_current_context(), - &*object, - &mut out, - ); - }; - out.into() + v8__Value__InstanceOf(self, &*scope.get_current_context(), &*object) + } } pub fn number_value<'s>(&self, scope: &mut HandleScope<'s>) -> Option { From f141a512beebcf0eb07154ed75cb3b2a5f48965c Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Sun, 23 Jan 2022 09:03:22 +0530 Subject: [PATCH 04/10] fix --- src/binding.cc | 2 +- src/value.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/binding.cc b/src/binding.cc index 06ef2dfc23..94bdf8e2d4 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -673,7 +673,7 @@ const v8::Boolean* v8__Value__ToBoolean(const v8::Value& self, return local_to_ptr(self.ToBoolean(isolate)); } -bool v8__Value__InstanceOf(const v8::Value& self, const v8::Context& context, +bool v8__Value__InstanceOf(v8::Value& self, const v8::Context& context, const v8::Object& object) { return self.InstanceOf(ptr_to_local(&context), ptr_to_local(&object)); } diff --git a/src/value.rs b/src/value.rs index d8daabdab0..9409ec7921 100644 --- a/src/value.rs +++ b/src/value.rs @@ -73,7 +73,7 @@ extern "C" { 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, + this: *mut Value, context: *const Context, object: *const Object, ) -> bool; @@ -556,7 +556,7 @@ impl Value { } pub fn instance_of<'s>( - &self, + &mut self, scope: &mut HandleScope<'s>, object: Local, ) -> bool { From 38bacf6790dfd36754abbcf40d076a020fc46e11 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Sun, 23 Jan 2022 09:23:57 +0530 Subject: [PATCH 05/10] maybe --- src/binding.cc | 6 +++--- src/value.rs | 14 +++++++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/binding.cc b/src/binding.cc index 94bdf8e2d4..80ae6a7ecd 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -673,9 +673,9 @@ const v8::Boolean* v8__Value__ToBoolean(const v8::Value& self, return local_to_ptr(self.ToBoolean(isolate)); } -bool v8__Value__InstanceOf(v8::Value& self, const v8::Context& context, - const v8::Object& object) { - return self.InstanceOf(ptr_to_local(&context), ptr_to_local(&object)); +void v8__Value__InstanceOf(v8::Value& self, const v8::Context& context, + const v8::Object& object, v8::Maybe* out) { + *out = self.InstanceOf(ptr_to_local(&context), ptr_to_local(&object)); } void v8__Value__NumberValue(const v8::Value& self, const v8::Context& context, diff --git a/src/value.rs b/src/value.rs index 9409ec7921..252a5148c2 100644 --- a/src/value.rs +++ b/src/value.rs @@ -76,7 +76,8 @@ extern "C" { this: *mut Value, context: *const Context, object: *const Object, - ) -> bool; + out: *mut Maybe, + ); fn v8__Value__ToBigInt( this: *const Value, context: *const Context, @@ -559,10 +560,17 @@ impl Value { &mut self, scope: &mut HandleScope<'s>, object: Local, - ) -> bool { + ) -> Option { + let mut out = Maybe::::default(); unsafe { - v8__Value__InstanceOf(self, &*scope.get_current_context(), &*object) + v8__Value__InstanceOf( + self, + &*scope.get_current_context(), + &*object, + &mut out, + ); } + out.into() } pub fn number_value<'s>(&self, scope: &mut HandleScope<'s>) -> Option { From 8db372229332abff684d805e01750c46c39ada97 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Sun, 23 Jan 2022 09:32:28 +0530 Subject: [PATCH 06/10] try --- tests/test_api.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_api.rs b/tests/test_api.rs index edc0aa6061..fd0686bd74 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -5786,7 +5786,8 @@ fn instance_of() { let array_constructor = global.get(&mut scope, array_name.into()).unwrap(); let array_constructor = v8::Local::::try_from(array_constructor).unwrap(); - let array = v8::Array::new_with_elements(&mut scope, &[]); + let mut array: v8::Local = + v8::Array::new_with_elements(&mut scope, &[]).into(); assert!(array.instance_of(&mut scope, array_constructor).unwrap()); } From 173951040fd6274b7d5733361c7563c01dc827b6 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Sun, 23 Jan 2022 10:21:46 +0530 Subject: [PATCH 07/10] deref_mut --- src/handle.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/handle.rs b/src/handle.rs index bc0aaaf6d9..fb1ca58363 100644 --- a/src/handle.rs +++ b/src/handle.rs @@ -4,6 +4,7 @@ use std::hash::Hasher; use std::marker::PhantomData; use std::mem::transmute; use std::ops::Deref; +use std::ops::DerefMut; use std::ptr::NonNull; use crate::Data; @@ -111,6 +112,12 @@ impl<'s, T> Deref for Local<'s, T> { } } +impl<'s, T> DerefMut for Local<'s, T> { + fn deref_mut(&mut self) -> &mut T { + unsafe { self.0.as_mut() } + } +} + /// An object reference that is independent of any handle scope. Where /// a Local handle only lives as long as the HandleScope in which it was /// allocated, a global handle remains valid until it is explicitly From d3e01ee9f40c9e01bfd4058148ebcccc30763556 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Thu, 10 Feb 2022 22:57:11 +0530 Subject: [PATCH 08/10] changes --- src/binding.cc | 5 +++-- src/handle.rs | 6 ------ src/value.rs | 4 ++-- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/binding.cc b/src/binding.cc index 4445ab864a..a5ab1c5ec9 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -682,9 +682,10 @@ const v8::Boolean* v8__Value__ToBoolean(const v8::Value& self, return local_to_ptr(self.ToBoolean(isolate)); } -void v8__Value__InstanceOf(v8::Value& self, const v8::Context& context, +void v8__Value__InstanceOf(const v8::Value& self, const v8::Context& context, const v8::Object& object, v8::Maybe* out) { - *out = self.InstanceOf(ptr_to_local(&context), ptr_to_local(&object)); + 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, diff --git a/src/handle.rs b/src/handle.rs index fb1ca58363..b97e55a0a1 100644 --- a/src/handle.rs +++ b/src/handle.rs @@ -112,12 +112,6 @@ impl<'s, T> Deref for Local<'s, T> { } } -impl<'s, T> DerefMut for Local<'s, T> { - fn deref_mut(&mut self) -> &mut T { - unsafe { self.0.as_mut() } - } -} - /// An object reference that is independent of any handle scope. Where /// a Local handle only lives as long as the HandleScope in which it was /// allocated, a global handle remains valid until it is explicitly diff --git a/src/value.rs b/src/value.rs index 252a5148c2..38e124536f 100644 --- a/src/value.rs +++ b/src/value.rs @@ -73,7 +73,7 @@ extern "C" { 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: *mut Value, + this: *const Value, context: *const Context, object: *const Object, out: *mut Maybe, @@ -557,7 +557,7 @@ impl Value { } pub fn instance_of<'s>( - &mut self, + &self, scope: &mut HandleScope<'s>, object: Local, ) -> Option { From 280e2cb7f95aa727722a45d480c75f24f6d309f5 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Fri, 11 Feb 2022 07:57:04 +0530 Subject: [PATCH 09/10] review --- src/binding.cc | 2 +- src/handle.rs | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/binding.cc b/src/binding.cc index a5ab1c5ec9..b17b802772 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -685,7 +685,7 @@ const v8::Boolean* v8__Value__ToBoolean(const v8::Value& self, 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)); + *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, diff --git a/src/handle.rs b/src/handle.rs index b97e55a0a1..bc0aaaf6d9 100644 --- a/src/handle.rs +++ b/src/handle.rs @@ -4,7 +4,6 @@ use std::hash::Hasher; use std::marker::PhantomData; use std::mem::transmute; use std::ops::Deref; -use std::ops::DerefMut; use std::ptr::NonNull; use crate::Data; From 072145de1652dbfb8926dc89966ea2ca79ff828e Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Fri, 11 Feb 2022 08:02:19 +0530 Subject: [PATCH 10/10] lint --- tests/test_api.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_api.rs b/tests/test_api.rs index 49c77c6a9c..05cd1324f6 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -5898,7 +5898,7 @@ fn instance_of() { let array_constructor = global.get(&mut scope, array_name.into()).unwrap(); let array_constructor = v8::Local::::try_from(array_constructor).unwrap(); - let mut array: v8::Local = + let array: v8::Local = v8::Array::new_with_elements(&mut scope, &[]).into(); assert!(array.instance_of(&mut scope, array_constructor).unwrap());