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::Object::HasOwnProperty bindings #897

Merged
merged 4 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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::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(
Expand Down
17 changes: 17 additions & 0 deletions src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<Name>,
) -> Option<bool> {
unsafe {
v8__Object__HasOwnProperty(self, &*scope.get_current_context(), &*key)
}
.into()
}

pub fn delete<'s>(
&self,
scope: &mut HandleScope<'s>,
Expand Down
7 changes: 5 additions & 2 deletions tests/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down