1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-20 11:55:08 +00:00

LibJS: Add a number-indexed property storage to all Objects

Objects can have both named and indexed properties. Previously we kept
all property names as strings. This patch separates named and indexed
properties and splits them between Object::m_storage and m_elements.

This allows us to do much faster array-style access using numeric
indices. It also makes the Array class much less special, since all
Objects now have number-indexed storage. :^)
This commit is contained in:
Andreas Kling 2020-04-06 16:53:02 +02:00
parent 65dd9d5ad3
commit 90ba0145f6
6 changed files with 69 additions and 50 deletions

View file

@ -43,52 +43,21 @@ Array::~Array()
Value Array::shift()
{
if (m_elements.size() == 0)
if (elements().size() == 0)
return js_undefined();
return Value(m_elements.take_first());
return Value(elements().take_first());
}
Value Array::pop()
{
if (m_elements.size() == 0)
if (elements().size() == 0)
return js_undefined();
return Value(m_elements.take_last());
return Value(elements().take_last());
}
void Array::push(Value value)
{
m_elements.append(value);
}
void Array::visit_children(Cell::Visitor& visitor)
{
Object::visit_children(visitor);
for (auto& element : m_elements)
visitor.visit(element);
}
Optional<Value> Array::get_own_property(const Object& this_object, const FlyString& property_name) const
{
bool ok;
i32 index = property_name.to_int(ok);
if (ok) {
if (index >= 0 && index < length())
return m_elements[index];
}
return Object::get_own_property(this_object, property_name);
}
bool Array::put_own_property(Object& this_object, const FlyString& property_name, Value value)
{
bool ok;
i32 index = property_name.to_int(ok);
if (ok && index >= 0) {
if (index >= length())
m_elements.resize(index + 1);
m_elements[index] = value;
return true;
}
return Object::put_own_property(this_object, property_name, value);
elements().append(value);
}
Value Array::length_getter(Interpreter& interpreter)