1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:07:34 +00:00

LibJS: Add Object::get_enumerable_own_property_names() and use it

Object::get_own_properties() is a bit unwieldy to use - especially as
StringOnly is about to no longer be the default value. The spec has an
abstract operation specifically for this (EnumerateObjectProperties),
so let's use that. No functionality change.
This commit is contained in:
Linus Groh 2021-04-05 19:05:15 +02:00 committed by Andreas Kling
parent afc86abe24
commit 1416027486
4 changed files with 13 additions and 4 deletions

View file

@ -479,7 +479,7 @@ Value ForInStatement::execute(Interpreter& interpreter, GlobalObject& global_obj
return {};
auto* object = rhs_result.to_object(global_object);
while (object) {
auto property_names = object->get_own_properties(Object::PropertyKind::Key, true);
auto property_names = object->get_enumerable_own_property_names(Object::PropertyKind::Key);
for (auto& property_name : property_names.as_object().indexed_properties()) {
interpreter.vm().set_variable(variable_name, property_name.value_and_attributes(object).value, global_object, has_declaration);
if (interpreter.exception())

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2021, Linus Groh <mail@linusgroh.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -262,6 +263,12 @@ Value Object::get_own_properties(PropertyKind kind, bool only_enumerable_propert
return properties_array;
}
// 7.3.23 EnumerableOwnPropertyNames, https://tc39.es/ecma262/#sec-enumerableownpropertynames
Value Object::get_enumerable_own_property_names(PropertyKind kind) const
{
return get_own_properties(kind, true, Object::GetOwnPropertyReturnType::StringOnly);
}
Optional<PropertyDescriptor> Object::get_own_property_descriptor(const PropertyName& property_name) const
{
VERIFY(property_name.is_valid());

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2021, Linus Groh <mail@linusgroh.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -96,6 +97,7 @@ public:
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_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;

View file

@ -209,7 +209,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::keys)
if (vm.exception())
return {};
return obj_arg->get_own_properties(PropertyKind::Key, true);
return obj_arg->get_enumerable_own_property_names(PropertyKind::Key);
}
JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::values)
@ -222,7 +222,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::values)
if (vm.exception())
return {};
return obj_arg->get_own_properties(PropertyKind::Value, true);
return obj_arg->get_enumerable_own_property_names(PropertyKind::Value);
}
JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::entries)
@ -235,7 +235,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::entries)
if (vm.exception())
return {};
return obj_arg->get_own_properties(PropertyKind::KeyAndValue, true);
return obj_arg->get_enumerable_own_property_names(PropertyKind::KeyAndValue);
}
}