mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 22:07:35 +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:
parent
58f6f50de4
commit
95abcc3722
9 changed files with 41 additions and 9 deletions
|
@ -56,6 +56,11 @@ Interpreter& Cell::interpreter()
|
|||
return heap().interpreter();
|
||||
}
|
||||
|
||||
Interpreter& Cell::interpreter() const
|
||||
{
|
||||
return heap().interpreter();
|
||||
}
|
||||
|
||||
const LogStream& operator<<(const LogStream& stream, const Cell* cell)
|
||||
{
|
||||
if (!cell)
|
||||
|
|
|
@ -60,6 +60,7 @@ public:
|
|||
|
||||
Heap& heap() const;
|
||||
Interpreter& interpreter();
|
||||
Interpreter& interpreter() const;
|
||||
|
||||
protected:
|
||||
Cell() {}
|
||||
|
|
|
@ -34,6 +34,7 @@
|
|||
#include <LibJS/Runtime/NativeProperty.h>
|
||||
#include <LibJS/Runtime/Object.h>
|
||||
#include <LibJS/Runtime/Shape.h>
|
||||
#include <LibJS/Runtime/StringObject.h>
|
||||
#include <LibJS/Runtime/Value.h>
|
||||
|
||||
namespace JS {
|
||||
|
|
|
@ -76,7 +76,7 @@ Value ObjectConstructor::get_own_property_names(Interpreter& interpreter)
|
|||
result->elements().append(js_string(interpreter, String::number(i)));
|
||||
}
|
||||
|
||||
for (auto& it : object->shape().property_table()) {
|
||||
for (auto& it : object->shape().property_table_ordered()) {
|
||||
result->elements().append(js_string(interpreter, it.key));
|
||||
}
|
||||
return result;
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -88,6 +88,13 @@ public:
|
|||
const HashMap<FlyString, PropertyMetadata>& property_table() const;
|
||||
size_t property_count() const;
|
||||
|
||||
struct Property {
|
||||
FlyString key;
|
||||
PropertyMetadata value;
|
||||
};
|
||||
|
||||
Vector<Property> property_table_ordered() const;
|
||||
|
||||
void set_prototype_without_transition(Object* new_prototype) { m_prototype = new_prototype; }
|
||||
|
||||
void remove_property_from_unique_shape(const FlyString&, size_t offset);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue