mirror of
https://github.com/fergalmoran/ladybird.git
synced 2026-01-05 16:14:38 +00:00
LibJS: Add basic prototype support
Object will now traverse up the prototype chain when doing a get(). When a function is called on an object, that object will now also be the "this" value inside the function. This stuff is probably not very correct, but we will improve things as we go! :^)
This commit is contained in:
@@ -42,7 +42,14 @@ Object::~Object()
|
||||
|
||||
Value Object::get(String property_name) const
|
||||
{
|
||||
return m_properties.get(property_name).value_or(js_undefined());
|
||||
const Object* object = this;
|
||||
while (object) {
|
||||
auto value = object->m_properties.get(property_name);
|
||||
if (value.has_value())
|
||||
return value.value();
|
||||
object = object->prototype();
|
||||
}
|
||||
return js_undefined();
|
||||
}
|
||||
|
||||
void Object::put(String property_name, Value value)
|
||||
@@ -58,6 +65,8 @@ void Object::put_native_function(String property_name, AK::Function<Value(Interp
|
||||
void Object::visit_children(Cell::Visitor& visitor)
|
||||
{
|
||||
Cell::visit_children(visitor);
|
||||
if (m_prototype)
|
||||
visitor.visit(m_prototype);
|
||||
for (auto& it : m_properties)
|
||||
visitor.visit(it.value);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user