From 7dc78b5e38b366502e1b0e514d4329d6d36fc317 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 24 Mar 2020 14:46:05 +0100 Subject: [PATCH] LibJS: Use correct |this| value when getting/setting native properties --- Libraries/LibJS/Runtime/Array.cpp | 8 ++++---- Libraries/LibJS/Runtime/Array.h | 4 ++-- Libraries/LibJS/Runtime/Object.cpp | 14 +++++++------- Libraries/LibJS/Runtime/Object.h | 4 ++-- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/Libraries/LibJS/Runtime/Array.cpp b/Libraries/LibJS/Runtime/Array.cpp index cab66eb3a5..df048c2389 100644 --- a/Libraries/LibJS/Runtime/Array.cpp +++ b/Libraries/LibJS/Runtime/Array.cpp @@ -59,7 +59,7 @@ void Array::visit_children(Cell::Visitor& visitor) visitor.visit(element); } -Optional Array::get_own_property(const FlyString& property_name) const +Optional Array::get_own_property(const Object& this_object, const FlyString& property_name) const { bool ok; i32 index = property_name.to_int(ok); @@ -67,10 +67,10 @@ Optional Array::get_own_property(const FlyString& property_name) const if (index >= 0 && index < length()) 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; 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; return true; } - return Object::put_own_property(property_name, value); + return Object::put_own_property(this_object, property_name, value); } } diff --git a/Libraries/LibJS/Runtime/Array.h b/Libraries/LibJS/Runtime/Array.h index 930b7a586c..4e9a56abae 100644 --- a/Libraries/LibJS/Runtime/Array.h +++ b/Libraries/LibJS/Runtime/Array.h @@ -45,8 +45,8 @@ private: virtual const char* class_name() const override { return "Array"; } virtual void visit_children(Cell::Visitor&) override; virtual bool is_array() const override { return true; } - virtual Optional get_own_property(const FlyString& property_name) const override; - virtual bool put_own_property(const FlyString& property_name, Value) override; + virtual Optional get_own_property(const Object& this_object, const FlyString& property_name) const override; + virtual bool put_own_property(Object& this_object, const FlyString& property_name, Value) override; Vector m_elements; }; diff --git a/Libraries/LibJS/Runtime/Object.cpp b/Libraries/LibJS/Runtime/Object.cpp index d72851ad50..75c11b767b 100644 --- a/Libraries/LibJS/Runtime/Object.cpp +++ b/Libraries/LibJS/Runtime/Object.cpp @@ -44,19 +44,19 @@ Object::~Object() { } -Optional Object::get_own_property(const FlyString& property_name) const +Optional Object::get_own_property(const Object& this_object, const FlyString& property_name) const { 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()) - return static_cast(value_here.value().as_object())->get(const_cast(this)); + return static_cast(value_here.value().as_object())->get(&const_cast(this_object)); 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); if (value_here.has_value() && value_here.value().is_object() && value_here.value().as_object()->is_native_property()) { - static_cast(value_here.value().as_object())->set(const_cast(this), value); + static_cast(value_here.value().as_object())->set(&this_object, value); } else { m_properties.set(property_name, value); } @@ -67,7 +67,7 @@ Value Object::get(const FlyString& property_name) const { const Object* object = this; while (object) { - auto value = object->get_own_property(property_name); + auto value = object->get_own_property(*this, property_name); if (value.has_value()) return value.value(); object = object->prototype(); @@ -85,12 +85,12 @@ void Object::put(const FlyString& property_name, Value value) static_cast(value_here.value().as_object())->set(const_cast(this), value); return; } - if (object->put_own_property(property_name, value)) + if (object->put_own_property(*this, property_name, value)) return; } 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)> native_function) diff --git a/Libraries/LibJS/Runtime/Object.h b/Libraries/LibJS/Runtime/Object.h index af11e35af5..8b68469b08 100644 --- a/Libraries/LibJS/Runtime/Object.h +++ b/Libraries/LibJS/Runtime/Object.h @@ -43,8 +43,8 @@ public: Value get(const FlyString& property_name) const; void put(const FlyString& property_name, Value); - virtual Optional get_own_property(const FlyString& property_name) const; - virtual bool put_own_property(const FlyString& property_name, Value); + virtual Optional get_own_property(const Object& this_object, const FlyString& property_name) const; + virtual bool put_own_property(Object& this_object, const FlyString& property_name, Value); void put_native_function(const FlyString& property_name, AK::Function)>); void put_native_property(const FlyString& property_name, AK::Function getter, AK::Function setter);