mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 06:48:12 +00:00
LibJS: Virtualize access to an Object's own properties
Object now has virtual get_own_property() and put_own_property() member functions that can be overridden to provide custom behavior. We use these virtuals to move Array-specific access behavior to Array.
This commit is contained in:
parent
2a7dbac0c5
commit
324b92fd06
4 changed files with 57 additions and 30 deletions
|
@ -59,4 +59,28 @@ void Array::visit_children(Cell::Visitor& visitor)
|
|||
visitor.visit(element);
|
||||
}
|
||||
|
||||
Optional<Value> Array::get_own_property(const String& 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(property_name);
|
||||
}
|
||||
|
||||
bool Array::put_own_property(const String& 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(property_name, value);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue