Browse Source

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

Andreas Kling 5 years ago
parent
commit
7dc78b5e38

+ 4 - 4
Libraries/LibJS/Runtime/Array.cpp

@@ -59,7 +59,7 @@ void Array::visit_children(Cell::Visitor& visitor)
         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;
     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())
             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);
 }
 
 }

+ 2 - 2
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<Value> get_own_property(const FlyString& property_name) const override;
-    virtual bool put_own_property(const FlyString& property_name, Value) override;
+    virtual Optional<Value> 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<Value> m_elements;
 };

+ 7 - 7
Libraries/LibJS/Runtime/Object.cpp

@@ -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);
     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;
 }
 
-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<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 {
         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<NativeProperty*>(value_here.value().as_object())->set(const_cast<Object*>(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<Value(Object*, Vector<Value>)> native_function)

+ 2 - 2
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<Value> get_own_property(const FlyString& property_name) const;
-    virtual bool put_own_property(const FlyString& property_name, Value);
+    virtual Optional<Value> 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<Value(Object*, Vector<Value>)>);
     void put_native_property(const FlyString& property_name, AK::Function<Value(Object*)> getter, AK::Function<void(Object*, Value)> setter);