diff --git a/src/binding.cc b/src/binding.cc index b17b8027..53d0d008 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -1164,6 +1164,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::Name& key) { + return maybe_to_maybe_bool( + 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, const v8::Value& key) { return maybe_to_maybe_bool( diff --git a/src/object.rs b/src/object.rs index 8d512f70..4fe77e06 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 Name, + ) -> MaybeBool; fn v8__Object__Delete( this: *const Object, context: *const Context, @@ -446,6 +451,18 @@ impl Object { .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, scope: &mut HandleScope<'s>, diff --git a/tests/test_api.rs b/tests/test_api.rs index 05cd1324..0ac9aeb4 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -1550,10 +1550,13 @@ fn object() { assert_ne!(id, 0); assert!(object.has(scope, n1.into()).unwrap()); - let n_unused = v8::String::new(scope, "unused").unwrap().into(); - assert!(!object.has(scope, n_unused).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).unwrap()); let global = context.global(scope); let object_string = v8::String::new(scope, "o").unwrap().into();