From e5b85447625088375b6e92dfefbe696246a1626c Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 3 Oct 2021 02:25:28 +0100 Subject: [PATCH] LibJS: Convert enumerable_own_property_names() to ThrowCompletionOr --- Userland/Libraries/LibJS/AST.cpp | 2 +- .../Libraries/LibJS/Runtime/JSONObject.cpp | 8 ++------ Userland/Libraries/LibJS/Runtime/Object.cpp | 19 +++++-------------- Userland/Libraries/LibJS/Runtime/Object.h | 2 +- .../LibJS/Runtime/ObjectConstructor.cpp | 12 +++--------- .../LibJS/Runtime/Temporal/Calendar.cpp | 8 ++------ 6 files changed, 14 insertions(+), 37 deletions(-) diff --git a/Userland/Libraries/LibJS/AST.cpp b/Userland/Libraries/LibJS/AST.cpp index d818aa60ea7fe6..883ba0259f678b 100644 --- a/Userland/Libraries/LibJS/AST.cpp +++ b/Userland/Libraries/LibJS/AST.cpp @@ -825,7 +825,7 @@ Value ForInStatement::execute(Interpreter& interpreter, GlobalObject& global_obj auto last_value = js_undefined(); while (object) { - auto property_names = object->enumerable_own_property_names(Object::PropertyKind::Key); + auto property_names = TRY_OR_DISCARD(object->enumerable_own_property_names(Object::PropertyKind::Key)); for (auto& value : property_names) { TRY_OR_DISCARD(for_in_head_state.execute_head(interpreter, global_object, value)); last_value = m_body->execute(interpreter, global_object).value_or(last_value); diff --git a/Userland/Libraries/LibJS/Runtime/JSONObject.cpp b/Userland/Libraries/LibJS/Runtime/JSONObject.cpp index 97bc10513898e9..dd55f0bb71513b 100644 --- a/Userland/Libraries/LibJS/Runtime/JSONObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/JSONObject.cpp @@ -238,9 +238,7 @@ String JSONObject::serialize_json_object(GlobalObject& global_object, StringifyS return {}; } } else { - auto property_list = object.enumerable_own_property_names(PropertyKind::Key); - if (vm.exception()) - return {}; + auto property_list = TRY_OR_DISCARD(object.enumerable_own_property_names(PropertyKind::Key)); for (auto& property : property_list) { process_property(property.as_string().string()); if (vm.exception()) @@ -480,9 +478,7 @@ Value JSONObject::internalize_json_property(GlobalObject& global_object, Object* for (size_t i = 0; i < length; ++i) TRY_OR_DISCARD(process_property(i)); } else { - auto property_list = value_object.enumerable_own_property_names(Object::PropertyKind::Key); - if (vm.exception()) - return {}; + auto property_list = TRY_OR_DISCARD(value_object.enumerable_own_property_names(Object::PropertyKind::Key)); for (auto& property_name : property_list) TRY_OR_DISCARD(process_property(property_name.as_string().string())); } diff --git a/Userland/Libraries/LibJS/Runtime/Object.cpp b/Userland/Libraries/LibJS/Runtime/Object.cpp index 60afb0a7e73549..31af0fa9d3067e 100644 --- a/Userland/Libraries/LibJS/Runtime/Object.cpp +++ b/Userland/Libraries/LibJS/Runtime/Object.cpp @@ -385,7 +385,7 @@ ThrowCompletionOr Object::test_integrity_level(IntegrityLevel level) const } // 7.3.23 EnumerableOwnPropertyNames ( O, kind ), https://tc39.es/ecma262/#sec-enumerableownpropertynames -MarkedValueList Object::enumerable_own_property_names(PropertyKind kind) const +ThrowCompletionOr Object::enumerable_own_property_names(PropertyKind kind) const { // NOTE: This has been flattened for readability, so some `else` branches in the // spec text have been replaced with `continue`s in the loop below. @@ -395,10 +395,7 @@ MarkedValueList Object::enumerable_own_property_names(PropertyKind kind) const // 1. Assert: Type(O) is Object. // 2. Let ownKeys be ? O.[[OwnPropertyKeys]](). - auto own_keys_or_error = internal_own_property_keys(); - if (own_keys_or_error.is_error()) - return MarkedValueList { heap() }; - auto own_keys = own_keys_or_error.release_value(); + auto own_keys = TRY(internal_own_property_keys()); // 3. Let properties be a new empty List. auto properties = MarkedValueList { heap() }; @@ -411,10 +408,7 @@ MarkedValueList Object::enumerable_own_property_names(PropertyKind kind) const auto property_name = PropertyName::from_value(global_object, key); // i. Let desc be ? O.[[GetOwnProperty]](key). - auto descriptor_or_error = internal_get_own_property(property_name); - if (descriptor_or_error.is_error()) - return MarkedValueList { heap() }; - auto descriptor = descriptor_or_error.release_value(); + auto descriptor = TRY(internal_get_own_property(property_name)); // ii. If desc is not undefined and desc.[[Enumerable]] is true, then if (descriptor.has_value() && *descriptor->enumerable) { @@ -426,10 +420,7 @@ MarkedValueList Object::enumerable_own_property_names(PropertyKind kind) const // 2. Else, // a. Let value be ? Get(O, key). - auto value_or_error = get(property_name); - if (value_or_error.is_error()) - return MarkedValueList { heap() }; - auto value = value_or_error.release_value(); + auto value = TRY(get(property_name)); // b. If kind is value, append value to properties. if (kind == PropertyKind::Value) { @@ -450,7 +441,7 @@ MarkedValueList Object::enumerable_own_property_names(PropertyKind kind) const } // 5. Return properties. - return properties; + return { move(properties) }; } // 7.3.25 CopyDataProperties ( target, source, excludedItems ), https://tc39.es/ecma262/#sec-copydataproperties diff --git a/Userland/Libraries/LibJS/Runtime/Object.h b/Userland/Libraries/LibJS/Runtime/Object.h index b985d2d14d195e..ad1b142174d9f0 100644 --- a/Userland/Libraries/LibJS/Runtime/Object.h +++ b/Userland/Libraries/LibJS/Runtime/Object.h @@ -87,7 +87,7 @@ class Object : public Cell { ThrowCompletionOr has_own_property(PropertyName const&) const; ThrowCompletionOr set_integrity_level(IntegrityLevel); ThrowCompletionOr test_integrity_level(IntegrityLevel) const; - MarkedValueList enumerable_own_property_names(PropertyKind kind) const; + ThrowCompletionOr enumerable_own_property_names(PropertyKind kind) const; ThrowCompletionOr copy_data_properties(Value source, HashTable const& seen_names, GlobalObject& global_object); // 10.1 Ordinary Object Internal Methods and Internal Slots, https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots diff --git a/Userland/Libraries/LibJS/Runtime/ObjectConstructor.cpp b/Userland/Libraries/LibJS/Runtime/ObjectConstructor.cpp index 73f51ccad06ada..6be1d4b86227be 100644 --- a/Userland/Libraries/LibJS/Runtime/ObjectConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/ObjectConstructor.cpp @@ -369,9 +369,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::keys) auto* object = vm.argument(0).to_object(global_object); if (vm.exception()) return {}; - auto name_list = object->enumerable_own_property_names(PropertyKind::Key); - if (vm.exception()) - return {}; + auto name_list = TRY_OR_DISCARD(object->enumerable_own_property_names(PropertyKind::Key)); return Array::create_from(global_object, name_list); } @@ -381,9 +379,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::values) auto* object = vm.argument(0).to_object(global_object); if (vm.exception()) return {}; - auto name_list = object->enumerable_own_property_names(PropertyKind::Value); - if (vm.exception()) - return {}; + auto name_list = TRY_OR_DISCARD(object->enumerable_own_property_names(PropertyKind::Value)); return Array::create_from(global_object, name_list); } @@ -393,9 +389,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::entries) auto* object = vm.argument(0).to_object(global_object); if (vm.exception()) return {}; - auto name_list = object->enumerable_own_property_names(PropertyKind::KeyAndValue); - if (vm.exception()) - return {}; + auto name_list = TRY_OR_DISCARD(object->enumerable_own_property_names(PropertyKind::KeyAndValue)); return Array::create_from(global_object, name_list); } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp index e4968a99f997cc..2777d9b79a0126 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp @@ -933,9 +933,7 @@ ThrowCompletionOr default_merge_fields(GlobalObject& global_object, Obj auto* merged = Object::create(global_object, global_object.object_prototype()); // 2. Let originalKeys be ? EnumerableOwnPropertyNames(fields, key). - auto original_keys = fields.enumerable_own_property_names(Object::PropertyKind::Key); - if (auto* exception = vm.exception()) - return throw_completion(exception->value()); + auto original_keys = TRY(fields.enumerable_own_property_names(Object::PropertyKind::Key)); // 3. For each element nextKey of originalKeys, do for (auto& next_key : original_keys) { @@ -955,9 +953,7 @@ ThrowCompletionOr default_merge_fields(GlobalObject& global_object, Obj } // 4. Let newKeys be ? EnumerableOwnPropertyNames(additionalFields, key). - auto new_keys = additional_fields.enumerable_own_property_names(Object::PropertyKind::Key); - if (auto* exception = vm.exception()) - return throw_completion(exception->value()); + auto new_keys = TRY(additional_fields.enumerable_own_property_names(Object::PropertyKind::Key)); // IMPLEMENTATION DEFINED: This is an optimization, so we don't have to iterate new_keys three times (worst case), but only once. bool new_keys_contains_month_or_month_code_property = false;