1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 21:37:35 +00:00

LibJS: Let Object::get_own_properties() return both strings and symbols

The new default return_type argument is GetOwnPropertyReturnType::All,
which returns properties with both string and symbol keys (which is also
the default for [[OwnPropertyKeys]]). This means that in some cases we
need to iterate the ordered property table twice, as we don't store
string and symbol properties separately but symbols must - there's
certainly room for (performance) improvements here. On the other hand
this makes Reflect.ownKeys() return symbol properties now :^)
This commit is contained in:
Linus Groh 2021-04-05 19:17:40 +02:00 committed by Andreas Kling
parent 1416027486
commit abc7b31079
3 changed files with 38 additions and 20 deletions

View file

@ -235,29 +235,44 @@ Value Object::get_own_properties(PropertyKind kind, bool only_enumerable_propert
++property_index;
}
for (auto& it : shape().property_table_ordered()) {
if (only_enumerable_properties && !it.value.attributes.is_enumerable())
continue;
if (return_type == GetOwnPropertyReturnType::StringOnly && it.key.is_symbol())
continue;
if (return_type == GetOwnPropertyReturnType::SymbolOnly && it.key.is_string())
continue;
auto add_property_to_results = [&](auto& property) {
if (kind == PropertyKind::Key) {
properties_array->define_property(property_index, it.key.to_value(vm()));
properties_array->define_property(property_index, property.key.to_value(vm()));
} else if (kind == PropertyKind::Value) {
properties_array->define_property(property_index, get(it.key));
properties_array->define_property(property_index, get(property.key));
} else {
auto* entry_array = Array::create(global_object());
entry_array->define_property(0, it.key.to_value(vm()));
entry_array->define_property(1, get(it.key));
entry_array->define_property(0, property.key.to_value(vm()));
entry_array->define_property(1, get(property.key));
properties_array->define_property(property_index, entry_array);
}
if (vm().exception())
return {};
};
++property_index;
// NOTE: Most things including for..in/of and Object.{keys,values,entries}() use StringOnly, and in those
// cases we won't be iterating the ordered property table twice. We can certainly improve this though.
if (return_type == GetOwnPropertyReturnType::All || return_type == GetOwnPropertyReturnType::StringOnly) {
for (auto& it : shape().property_table_ordered()) {
if (only_enumerable_properties && !it.value.attributes.is_enumerable())
continue;
if (it.key.is_symbol())
continue;
add_property_to_results(it);
if (vm().exception())
return {};
++property_index;
}
}
if (return_type == GetOwnPropertyReturnType::All || return_type == GetOwnPropertyReturnType::SymbolOnly) {
for (auto& it : shape().property_table_ordered()) {
if (only_enumerable_properties && !it.value.attributes.is_enumerable())
continue;
if (it.key.is_string())
continue;
add_property_to_results(it);
if (vm().exception())
return {};
++property_index;
}
}
return properties_array;

View file

@ -74,6 +74,7 @@ public:
};
enum class GetOwnPropertyReturnType {
All,
StringOnly,
SymbolOnly,
};
@ -96,7 +97,7 @@ public:
virtual bool put(const PropertyName&, Value, Value receiver = {});
Value get_own_property(const PropertyName&, Value receiver) const;
Value get_own_properties(PropertyKind, bool only_enumerable_properties = false, GetOwnPropertyReturnType = GetOwnPropertyReturnType::StringOnly) const;
Value get_own_properties(PropertyKind, bool only_enumerable_properties = false, GetOwnPropertyReturnType = GetOwnPropertyReturnType::All) const;
Value get_enumerable_own_property_names(PropertyKind) const;
virtual Optional<PropertyDescriptor> get_own_property_descriptor(const PropertyName&) const;
Value get_own_property_descriptor_object(const PropertyName&) const;