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

LibJS: Use correct |this| value when getting/setting native properties

This commit is contained in:
Andreas Kling 2020-03-24 14:46:05 +01:00
parent 8705c5ffeb
commit 7dc78b5e38
4 changed files with 15 additions and 15 deletions

View file

@ -59,7 +59,7 @@ void Array::visit_children(Cell::Visitor& visitor)
visitor.visit(element); visitor.visit(element);
} }
Optional<Value> Array::get_own_property(const FlyString& property_name) const Optional<Value> Array::get_own_property(const Object& this_object, const FlyString& property_name) const
{ {
bool ok; bool ok;
i32 index = property_name.to_int(ok); i32 index = property_name.to_int(ok);
@ -67,10 +67,10 @@ Optional<Value> Array::get_own_property(const FlyString& property_name) const
if (index >= 0 && index < length()) if (index >= 0 && index < length())
return m_elements[index]; return m_elements[index];
} }
return Object::get_own_property(property_name); return Object::get_own_property(this_object, property_name);
} }
bool Array::put_own_property(const FlyString& property_name, Value value) bool Array::put_own_property(Object& this_object, const FlyString& property_name, Value value)
{ {
bool ok; bool ok;
i32 index = property_name.to_int(ok); i32 index = property_name.to_int(ok);
@ -80,7 +80,7 @@ bool Array::put_own_property(const FlyString& property_name, Value value)
m_elements[index] = value; m_elements[index] = value;
return true; return true;
} }
return Object::put_own_property(property_name, value); return Object::put_own_property(this_object, property_name, value);
} }
} }

View file

@ -45,8 +45,8 @@ private:
virtual const char* class_name() const override { return "Array"; } virtual const char* class_name() const override { return "Array"; }
virtual void visit_children(Cell::Visitor&) override; virtual void visit_children(Cell::Visitor&) override;
virtual bool is_array() const override { return true; } virtual bool is_array() const override { return true; }
virtual Optional<Value> get_own_property(const FlyString& property_name) const override; virtual Optional<Value> get_own_property(const Object& this_object, const FlyString& property_name) const override;
virtual bool put_own_property(const FlyString& property_name, Value) override; virtual bool put_own_property(Object& this_object, const FlyString& property_name, Value) override;
Vector<Value> m_elements; Vector<Value> m_elements;
}; };

View file

@ -44,19 +44,19 @@ Object::~Object()
{ {
} }
Optional<Value> Object::get_own_property(const FlyString& property_name) const Optional<Value> Object::get_own_property(const Object& this_object, const FlyString& property_name) const
{ {
auto value_here = m_properties.get(property_name); auto value_here = m_properties.get(property_name);
if (value_here.has_value() && value_here.value().is_object() && value_here.value().as_object()->is_native_property()) if (value_here.has_value() && value_here.value().is_object() && value_here.value().as_object()->is_native_property())
return static_cast<NativeProperty*>(value_here.value().as_object())->get(const_cast<Object*>(this)); return static_cast<NativeProperty*>(value_here.value().as_object())->get(&const_cast<Object&>(this_object));
return value_here; return value_here;
} }
bool Object::put_own_property(const FlyString& property_name, Value value) bool Object::put_own_property(Object& this_object, const FlyString& property_name, Value value)
{ {
auto value_here = m_properties.get(property_name); auto value_here = m_properties.get(property_name);
if (value_here.has_value() && value_here.value().is_object() && value_here.value().as_object()->is_native_property()) { if (value_here.has_value() && value_here.value().is_object() && value_here.value().as_object()->is_native_property()) {
static_cast<NativeProperty*>(value_here.value().as_object())->set(const_cast<Object*>(this), value); static_cast<NativeProperty*>(value_here.value().as_object())->set(&this_object, value);
} else { } else {
m_properties.set(property_name, value); m_properties.set(property_name, value);
} }
@ -67,7 +67,7 @@ Value Object::get(const FlyString& property_name) const
{ {
const Object* object = this; const Object* object = this;
while (object) { while (object) {
auto value = object->get_own_property(property_name); auto value = object->get_own_property(*this, property_name);
if (value.has_value()) if (value.has_value())
return value.value(); return value.value();
object = object->prototype(); object = object->prototype();
@ -85,12 +85,12 @@ void Object::put(const FlyString& property_name, Value value)
static_cast<NativeProperty*>(value_here.value().as_object())->set(const_cast<Object*>(this), value); static_cast<NativeProperty*>(value_here.value().as_object())->set(const_cast<Object*>(this), value);
return; return;
} }
if (object->put_own_property(property_name, value)) if (object->put_own_property(*this, property_name, value))
return; return;
} }
object = object->prototype(); object = object->prototype();
} }
put_own_property(property_name, value); put_own_property(*this, property_name, value);
} }
void Object::put_native_function(const FlyString& property_name, AK::Function<Value(Object*, Vector<Value>)> native_function) void Object::put_native_function(const FlyString& property_name, AK::Function<Value(Object*, Vector<Value>)> native_function)

View file

@ -43,8 +43,8 @@ public:
Value get(const FlyString& property_name) const; Value get(const FlyString& property_name) const;
void put(const FlyString& property_name, Value); void put(const FlyString& property_name, Value);
virtual Optional<Value> get_own_property(const FlyString& property_name) const; virtual Optional<Value> get_own_property(const Object& this_object, const FlyString& property_name) const;
virtual bool put_own_property(const FlyString& property_name, Value); virtual bool put_own_property(Object& this_object, const FlyString& property_name, Value);
void put_native_function(const FlyString& property_name, AK::Function<Value(Object*, Vector<Value>)>); void put_native_function(const FlyString& property_name, AK::Function<Value(Object*, Vector<Value>)>);
void put_native_property(const FlyString& property_name, AK::Function<Value(Object*)> getter, AK::Function<void(Object*, Value)> setter); void put_native_property(const FlyString& property_name, AK::Function<Value(Object*)> getter, AK::Function<void(Object*, Value)> setter);