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

Improve Object::get_property_names() and Object::get_own_property_names() #1049

Merged
merged 3 commits into from
Sep 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 12 additions & 6 deletions src/get_property_names_args_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ pub struct GetPropertyNamesArgs {
pub key_conversion: KeyConversionMode,
}

impl Default for GetPropertyNamesArgs {
fn default() -> Self {
GetPropertyNamesArgs {
mode: KeyCollectionMode::IncludePrototypes,
property_filter: ONLY_ENUMERABLE | SKIP_SYMBOLS,
index_filter: IndexFilter::IncludeIndices,
key_conversion: KeyConversionMode::KeepNumbers,
}
}
}

pub struct GetPropertyNamesArgsBuilder {
mode: KeyCollectionMode,
property_filter: PropertyFilter,
Expand All @@ -67,12 +78,7 @@ impl GetPropertyNamesArgsBuilder {
}

pub fn build(&self) -> GetPropertyNamesArgs {
GetPropertyNamesArgs {
mode: self.mode,
property_filter: self.property_filter,
index_filter: self.index_filter,
key_conversion: self.key_conversion,
}
Default::default()
RaisinTen marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn mode(
Expand Down
24 changes: 5 additions & 19 deletions tests/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4846,26 +4846,18 @@ fn test_object_get_property_names() {
obj.set_prototype(scope, proto_obj.into());

let own_props = obj
.get_own_property_names(
scope,
v8::GetPropertyNamesArgsBuilder::new().build(),
)
.get_own_property_names(scope, Default::default())
.unwrap();
assert_eq!(own_props.length(), 1);
assert!(own_props.get_index(scope, 0).unwrap() == js_test_str);

let proto_props = proto_obj
.get_own_property_names(
scope,
v8::GetPropertyNamesArgsBuilder::new().build(),
)
.get_own_property_names(scope, Default::default())
.unwrap();
assert_eq!(proto_props.length(), 1);
assert!(proto_props.get_index(scope, 0).unwrap() == js_proto_test_str);

let all_props = obj
.get_property_names(scope, v8::GetPropertyNamesArgsBuilder::new().build())
.unwrap();
let all_props = obj.get_property_names(scope, Default::default()).unwrap();
js_sort_fn.call(scope, all_props.into(), &[]).unwrap();
assert_eq!(all_props.length(), 2);
assert!(all_props.get_index(scope, 0).unwrap() == js_proto_test_str);
Expand All @@ -4878,10 +4870,7 @@ fn test_object_get_property_names() {
obj.set(scope, js_test_symbol, js_null);

let own_props = obj
.get_own_property_names(
scope,
v8::GetPropertyNamesArgsBuilder::new().build(),
)
.get_own_property_names(scope, Default::default())
.unwrap();
assert_eq!(own_props.length(), 1);
assert!(own_props.get_index(scope, 0).unwrap() == js_test_str);
Expand Down Expand Up @@ -4916,10 +4905,7 @@ fn test_object_get_property_names() {

{
let own_props = obj
.get_own_property_names(
scope,
v8::GetPropertyNamesArgsBuilder::new().build(),
)
.get_own_property_names(scope, Default::default())
.unwrap();

assert_eq!(own_props.length(), 3);
Expand Down