瀏覽代碼

LibJS: Remove this_object parameter from get/put own property functions

Specifically:

- Object::get_own_properties()
- Object::put_own_property()
- Object::put_own_property_by_index()

These APIs make no sense (and are inconsistent, get_own_property()
didn't have this parameter, for example) - and as expected we were
always passing in the same object we were calling the method on anyway.
Linus Groh 4 年之前
父節點
當前提交
afc86abe24

+ 1 - 1
Userland/Libraries/LibJS/AST.cpp

@@ -479,7 +479,7 @@ Value ForInStatement::execute(Interpreter& interpreter, GlobalObject& global_obj
         return {};
     auto* object = rhs_result.to_object(global_object);
     while (object) {
-        auto property_names = object->get_own_properties(*object, Object::PropertyKind::Key, true);
+        auto property_names = object->get_own_properties(Object::PropertyKind::Key, true);
         for (auto& property_name : property_names.as_object().indexed_properties()) {
             interpreter.vm().set_variable(variable_name, property_name.value_and_attributes(object).value, global_object, has_declaration);
             if (interpreter.exception())

+ 17 - 17
Userland/Libraries/LibJS/Runtime/Object.cpp

@@ -186,13 +186,13 @@ Value Object::get_own_property(const PropertyName& property_name, Value receiver
     return value_here;
 }
 
-Value Object::get_own_properties(const Object& this_object, PropertyKind kind, bool only_enumerable_properties, GetOwnPropertyReturnType return_type) const
+Value Object::get_own_properties(PropertyKind kind, bool only_enumerable_properties, GetOwnPropertyReturnType return_type) const
 {
     auto* properties_array = Array::create(global_object());
 
     // FIXME: Support generic iterables
-    if (is<StringObject>(this_object)) {
-        auto str = static_cast<const StringObject&>(this_object).primitive_string().string();
+    if (is<StringObject>(*this)) {
+        auto str = static_cast<const StringObject&>(*this).primitive_string().string();
 
         for (size_t i = 0; i < str.length(); ++i) {
             if (kind == PropertyKind::Key) {
@@ -214,7 +214,7 @@ Value Object::get_own_properties(const Object& this_object, PropertyKind kind, b
 
     size_t property_index = 0;
     for (auto& entry : m_indexed_properties) {
-        auto value_and_attributes = entry.value_and_attributes(const_cast<Object*>(&this_object));
+        auto value_and_attributes = entry.value_and_attributes(const_cast<Object*>(this));
         if (only_enumerable_properties && !value_and_attributes.attributes.is_enumerable())
             continue;
 
@@ -234,7 +234,7 @@ Value Object::get_own_properties(const Object& this_object, PropertyKind kind, b
         ++property_index;
     }
 
-    for (auto& it : this_object.shape().property_table_ordered()) {
+    for (auto& it : shape().property_table_ordered()) {
         if (only_enumerable_properties && !it.value.attributes.is_enumerable())
             continue;
 
@@ -246,11 +246,11 @@ Value Object::get_own_properties(const Object& this_object, PropertyKind kind, b
         if (kind == PropertyKind::Key) {
             properties_array->define_property(property_index, it.key.to_value(vm()));
         } else if (kind == PropertyKind::Value) {
-            properties_array->define_property(property_index, this_object.get(it.key));
+            properties_array->define_property(property_index, get(it.key));
         } else {
             auto* entry_array = Array::create(global_object());
             entry_array->define_property(0, it.key.to_value(vm()));
-            entry_array->define_property(1, this_object.get(it.key));
+            entry_array->define_property(1, get(it.key));
             properties_array->define_property(property_index, entry_array);
         }
         if (vm().exception())
@@ -437,14 +437,14 @@ bool Object::define_property(const PropertyName& property_name, Value value, Pro
     VERIFY(property_name.is_valid());
 
     if (property_name.is_number())
-        return put_own_property_by_index(*this, property_name.as_number(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
+        return put_own_property_by_index(property_name.as_number(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
 
     if (property_name.is_string()) {
         i32 property_index = property_name.as_string().to_int().value_or(-1);
         if (property_index >= 0)
-            return put_own_property_by_index(*this, property_index, value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
+            return put_own_property_by_index(property_index, value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
     }
-    return put_own_property(*this, property_name.to_string_or_symbol(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
+    return put_own_property(property_name.to_string_or_symbol(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
 }
 
 bool Object::define_accessor(const PropertyName& property_name, Function& getter_or_setter, bool is_getter, PropertyAttributes attributes, bool throw_exceptions)
@@ -474,7 +474,7 @@ bool Object::define_accessor(const PropertyName& property_name, Function& getter
     return true;
 }
 
-bool Object::put_own_property(Object& this_object, const StringOrSymbol& property_name, Value value, PropertyAttributes attributes, PutOwnPropertyMode mode, bool throw_exceptions)
+bool Object::put_own_property(const StringOrSymbol& property_name, Value value, PropertyAttributes attributes, PutOwnPropertyMode mode, bool throw_exceptions)
 {
     VERIFY(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
 
@@ -561,14 +561,14 @@ bool Object::put_own_property(Object& this_object, const StringOrSymbol& propert
         return true;
 
     if (value_here.is_native_property()) {
-        call_native_property_setter(value_here.as_native_property(), &this_object, value);
+        call_native_property_setter(value_here.as_native_property(), this, value);
     } else {
         m_storage[metadata.value().offset] = value;
     }
     return true;
 }
 
-bool Object::put_own_property_by_index(Object& this_object, u32 property_index, Value value, PropertyAttributes attributes, PutOwnPropertyMode mode, bool throw_exceptions)
+bool Object::put_own_property_by_index(u32 property_index, Value value, PropertyAttributes attributes, PutOwnPropertyMode mode, bool throw_exceptions)
 {
     VERIFY(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
 
@@ -615,9 +615,9 @@ bool Object::put_own_property_by_index(Object& this_object, u32 property_index,
         return true;
 
     if (value_here.is_native_property()) {
-        call_native_property_setter(value_here.as_native_property(), &this_object, value);
+        call_native_property_setter(value_here.as_native_property(), this, value);
     } else {
-        m_indexed_properties.put(&this_object, property_index, value, attributes, mode == PutOwnPropertyMode::Put);
+        m_indexed_properties.put(this, property_index, value, attributes, mode == PutOwnPropertyMode::Put);
     }
     return true;
 }
@@ -740,7 +740,7 @@ bool Object::put_by_index(u32 property_index, Value value)
         if (vm().exception())
             return {};
     }
-    return put_own_property_by_index(*this, property_index, value, default_attributes, PutOwnPropertyMode::Put);
+    return put_own_property_by_index(property_index, value, default_attributes, PutOwnPropertyMode::Put);
 }
 
 bool Object::put(const PropertyName& property_name, Value value, Value receiver)
@@ -784,7 +784,7 @@ bool Object::put(const PropertyName& property_name, Value value, Value receiver)
         if (vm().exception())
             return false;
     }
-    return put_own_property(*this, string_or_symbol, value, default_attributes, PutOwnPropertyMode::Put);
+    return put_own_property(string_or_symbol, value, default_attributes, PutOwnPropertyMode::Put);
 }
 
 bool Object::define_native_function(const StringOrSymbol& property_name, AK::Function<Value(VM&, GlobalObject&)> native_function, i32 length, PropertyAttributes attribute)

+ 3 - 3
Userland/Libraries/LibJS/Runtime/Object.h

@@ -95,7 +95,7 @@ public:
     virtual bool put(const PropertyName&, Value, Value receiver = {});
 
     Value get_own_property(const PropertyName&, Value receiver) const;
-    Value get_own_properties(const Object& this_object, PropertyKind, bool only_enumerable_properties = false, GetOwnPropertyReturnType = GetOwnPropertyReturnType::StringOnly) const;
+    Value get_own_properties(PropertyKind, bool only_enumerable_properties = false, GetOwnPropertyReturnType = GetOwnPropertyReturnType::StringOnly) const;
     virtual Optional<PropertyDescriptor> get_own_property_descriptor(const PropertyName&) const;
     Value get_own_property_descriptor_object(const PropertyName&) const;
 
@@ -167,8 +167,8 @@ protected:
     virtual bool put_by_index(u32 property_index, Value);
 
 private:
-    bool put_own_property(Object& this_object, const StringOrSymbol& property_name, Value, PropertyAttributes attributes, PutOwnPropertyMode = PutOwnPropertyMode::Put, bool throw_exceptions = true);
-    bool put_own_property_by_index(Object& this_object, u32 property_index, Value, PropertyAttributes attributes, PutOwnPropertyMode = PutOwnPropertyMode::Put, bool throw_exceptions = true);
+    bool put_own_property(const StringOrSymbol& property_name, Value, PropertyAttributes attributes, PutOwnPropertyMode = PutOwnPropertyMode::Put, bool throw_exceptions = true);
+    bool put_own_property_by_index(u32 property_index, Value, PropertyAttributes attributes, PutOwnPropertyMode = PutOwnPropertyMode::Put, bool throw_exceptions = true);
 
     Value call_native_property_getter(NativeProperty& property, Value this_value) const;
     void call_native_property_setter(NativeProperty& property, Value this_value, Value) const;

+ 3 - 3
Userland/Libraries/LibJS/Runtime/ObjectConstructor.cpp

@@ -209,7 +209,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::keys)
     if (vm.exception())
         return {};
 
-    return obj_arg->get_own_properties(*obj_arg, PropertyKind::Key, true);
+    return obj_arg->get_own_properties(PropertyKind::Key, true);
 }
 
 JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::values)
@@ -222,7 +222,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::values)
     if (vm.exception())
         return {};
 
-    return obj_arg->get_own_properties(*obj_arg, PropertyKind::Value, true);
+    return obj_arg->get_own_properties(PropertyKind::Value, true);
 }
 
 JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::entries)
@@ -235,7 +235,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::entries)
     if (vm.exception())
         return {};
 
-    return obj_arg->get_own_properties(*obj_arg, PropertyKind::KeyAndValue, true);
+    return obj_arg->get_own_properties(PropertyKind::KeyAndValue, true);
 }
 
 }

+ 1 - 1
Userland/Libraries/LibJS/Runtime/ReflectObject.cpp

@@ -235,7 +235,7 @@ JS_DEFINE_NATIVE_FUNCTION(ReflectObject::own_keys)
     auto* target = get_target_object_from(global_object, "ownKeys");
     if (!target)
         return {};
-    return target->get_own_properties(*target, PropertyKind::Key);
+    return target->get_own_properties(PropertyKind::Key);
 }
 
 JS_DEFINE_NATIVE_FUNCTION(ReflectObject::prevent_extensions)