1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 07:38:10 +00:00

LibJS: Implement correct object property ordering

This commit introduces a way to get an object's own properties in the
correct order. The "correct order" for JS object properties is first all
array-like index properties (numeric keys) sorted by insertion order,
followed by all string properties sorted by insertion order.

Objects also now print correctly in the repl! Before this commit:

courage ~/js-tests $ js
> ({ foo: 1, bar: 2, baz: 3 })
{ bar: 2, foo: 1, baz: 3 }

After:

courage ~/js-tests $ js
> ({ foo: 1, bar: 2, baz: 3 })
{ foo: 1, bar: 2, baz: 3 }
This commit is contained in:
mattco98 2020-04-28 19:19:31 -07:00 committed by Andreas Kling
parent 58f6f50de4
commit 95abcc3722
9 changed files with 41 additions and 9 deletions

View file

@ -117,6 +117,18 @@ size_t Shape::property_count() const
return property_table().size();
}
Vector<Shape::Property> Shape::property_table_ordered() const
{
auto vec = Vector<Shape::Property>();
vec.resize(property_table().size());
for (auto& it : property_table()) {
vec[it.value.offset] = { it.key, it.value };
}
return vec;
}
void Shape::ensure_property_table() const
{
if (m_property_table)