1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 13:17:35 +00:00

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.
This commit is contained in:
Linus Groh 2021-04-05 18:04:55 +02:00 committed by Andreas Kling
parent 5de0e0068c
commit afc86abe24
5 changed files with 25 additions and 25 deletions

View file

@ -479,7 +479,7 @@ Value ForInStatement::execute(Interpreter& interpreter, GlobalObject& global_obj
return {}; return {};
auto* object = rhs_result.to_object(global_object); auto* object = rhs_result.to_object(global_object);
while (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()) { 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); interpreter.vm().set_variable(variable_name, property_name.value_and_attributes(object).value, global_object, has_declaration);
if (interpreter.exception()) if (interpreter.exception())

View file

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

View file

@ -95,7 +95,7 @@ public:
virtual bool put(const PropertyName&, Value, Value receiver = {}); virtual bool put(const PropertyName&, Value, Value receiver = {});
Value get_own_property(const PropertyName&, Value receiver) const; 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; virtual Optional<PropertyDescriptor> get_own_property_descriptor(const PropertyName&) const;
Value get_own_property_descriptor_object(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); virtual bool put_by_index(u32 property_index, Value);
private: 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(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_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; Value call_native_property_getter(NativeProperty& property, Value this_value) const;
void call_native_property_setter(NativeProperty& property, Value this_value, Value) const; void call_native_property_setter(NativeProperty& property, Value this_value, Value) const;

View file

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

View file

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