From fb8556733717b0376f9355348caf7618934d9c2b Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Fri, 11 Feb 2022 08:21:39 +0530 Subject: [PATCH 1/4] Add V8::Object::HasOwnProperty bindings --- src/binding.cc | 6 ++++++ src/object.rs | 15 +++++++++++++++ tests/test_api.rs | 1 + 3 files changed, 22 insertions(+) diff --git a/src/binding.cc b/src/binding.cc index 35996fbebe..717906ab32 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -1158,6 +1158,12 @@ MaybeBool v8__Object__HasIndex(const v8::Object& self, ptr_to_local(&self)->Has(ptr_to_local(&context), index)); } +MaybeBool v8__Object__HasOwnProperty(const v8::Object& self, const v8::Context& context, + const v8::Value& key) { + return maybe_to_maybe_bool( + ptr_to_local(&self)->HasOwnProperty(ptr_to_local(&context), ptr_to_local(&key).As())); +} + MaybeBool v8__Object__Delete(const v8::Object& self, const v8::Context& context, const v8::Value& key) { return maybe_to_maybe_bool( diff --git a/src/object.rs b/src/object.rs index 8d512f7052..c3bc9c6512 100644 --- a/src/object.rs +++ b/src/object.rs @@ -99,6 +99,11 @@ extern "C" { context: *const Context, index: u32, ) -> MaybeBool; + fn v8__Object__HasOwnProperty( + this: *const Object, + context: *const Context, + key: *const Value, + ) -> MaybeBool; fn v8__Object__Delete( this: *const Object, context: *const Context, @@ -445,6 +450,16 @@ impl Object { unsafe { v8__Object__HasIndex(self, &*scope.get_current_context(), index) } .into() } + + /// HasOwnProperty() is like JavaScript's Object.prototype.hasOwnProperty(). + pub fn has_own_property<'s>( + &self, + scope: &mut HandleScope<'s>, + key: Local, + ) -> Option { + unsafe { v8__Object__HasOwnProperty(self, &*scope.get_current_context(), &*key) } + .into() + } pub fn delete<'s>( &self, diff --git a/tests/test_api.rs b/tests/test_api.rs index a644cd59fd..a1f4685184 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -1550,6 +1550,7 @@ fn object() { assert_ne!(id, 0); assert!(object.has(scope, n1.into()).unwrap()); + assert!(object.has_own_property(scope, n1.into()).unwrap()); let n_unused = v8::String::new(scope, "unused").unwrap().into(); assert!(!object.has(scope, n_unused).unwrap()); assert!(object.delete(scope, n1.into()).unwrap()); From 0f707524b21fd6227571e4661c26e82cb82c2190 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Fri, 11 Feb 2022 08:28:26 +0530 Subject: [PATCH 2/4] fmt --- src/object.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/object.rs b/src/object.rs index c3bc9c6512..0dc8e9a7f4 100644 --- a/src/object.rs +++ b/src/object.rs @@ -450,15 +450,17 @@ impl Object { unsafe { v8__Object__HasIndex(self, &*scope.get_current_context(), index) } .into() } - + /// HasOwnProperty() is like JavaScript's Object.prototype.hasOwnProperty(). pub fn has_own_property<'s>( &self, scope: &mut HandleScope<'s>, key: Local, ) -> Option { - unsafe { v8__Object__HasOwnProperty(self, &*scope.get_current_context(), &*key) } - .into() + unsafe { + v8__Object__HasOwnProperty(self, &*scope.get_current_context(), &*key) + } + .into() } pub fn delete<'s>( From 51b426e058bf1c7123b20978b454605369d8b265 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Fri, 11 Feb 2022 09:32:58 +0530 Subject: [PATCH 3/4] review --- src/binding.cc | 4 ++-- src/object.rs | 4 ++-- tests/test_api.rs | 6 ++++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/binding.cc b/src/binding.cc index 717906ab32..0d3c38f703 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -1159,9 +1159,9 @@ MaybeBool v8__Object__HasIndex(const v8::Object& self, } MaybeBool v8__Object__HasOwnProperty(const v8::Object& self, const v8::Context& context, - const v8::Value& key) { + const v8::Name& key) { return maybe_to_maybe_bool( - ptr_to_local(&self)->HasOwnProperty(ptr_to_local(&context), ptr_to_local(&key).As())); + ptr_to_local(&self)->HasOwnProperty(ptr_to_local(&context), ptr_to_local(&key))); } MaybeBool v8__Object__Delete(const v8::Object& self, const v8::Context& context, diff --git a/src/object.rs b/src/object.rs index 0dc8e9a7f4..4fe77e0605 100644 --- a/src/object.rs +++ b/src/object.rs @@ -102,7 +102,7 @@ extern "C" { fn v8__Object__HasOwnProperty( this: *const Object, context: *const Context, - key: *const Value, + key: *const Name, ) -> MaybeBool; fn v8__Object__Delete( this: *const Object, @@ -455,7 +455,7 @@ impl Object { pub fn has_own_property<'s>( &self, scope: &mut HandleScope<'s>, - key: Local, + key: Local, ) -> Option { unsafe { v8__Object__HasOwnProperty(self, &*scope.get_current_context(), &*key) diff --git a/tests/test_api.rs b/tests/test_api.rs index a1f4685184..e32a49e97b 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -1551,10 +1551,12 @@ fn object() { assert!(object.has(scope, n1.into()).unwrap()); assert!(object.has_own_property(scope, n1.into()).unwrap()); - let n_unused = v8::String::new(scope, "unused").unwrap().into(); - assert!(!object.has(scope, n_unused).unwrap()); + let n_unused = v8::String::new(scope, "unused").unwrap(); + assert!(!object.has(scope, n_unused.into()).unwrap()); + assert!(!object.has_own_property(scope, n_unused.into()).unwrap()); assert!(object.delete(scope, n1.into()).unwrap()); assert!(!object.has(scope, n1.into()).unwrap()); + assert!(!object.has_own_property(scope, n1.into()).unwrap()); let global = context.global(scope); let object_string = v8::String::new(scope, "o").unwrap().into(); From 7537d8009381e80a6a031ba84053cbe757aff3b6 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Fri, 11 Feb 2022 09:41:19 +0530 Subject: [PATCH 4/4] lint --- tests/test_api.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_api.rs b/tests/test_api.rs index e32a49e97b..20e9b0a171 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -1550,13 +1550,13 @@ fn object() { assert_ne!(id, 0); assert!(object.has(scope, n1.into()).unwrap()); - assert!(object.has_own_property(scope, n1.into()).unwrap()); + assert!(object.has_own_property(scope, n1).unwrap()); let n_unused = v8::String::new(scope, "unused").unwrap(); assert!(!object.has(scope, n_unused.into()).unwrap()); assert!(!object.has_own_property(scope, n_unused.into()).unwrap()); assert!(object.delete(scope, n1.into()).unwrap()); assert!(!object.has(scope, n1.into()).unwrap()); - assert!(!object.has_own_property(scope, n1.into()).unwrap()); + assert!(!object.has_own_property(scope, n1).unwrap()); let global = context.global(scope); let object_string = v8::String::new(scope, "o").unwrap().into();