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:
Andreas Kling 2020-03-15 15:01:10 +01:00
parent d02c37f3e3
commit f7c15d00c9
Notes: sideshowbarker 2024-07-19 08:18:01 +09:00
5 changed files with 42 additions and 2 deletions

View file

@ -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);
}