diff options
author | Linus Groh <mail@linusgroh.de> | 2022-01-04 22:33:30 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-01-04 23:37:26 +0000 |
commit | 29e96eceebbe7f214c735a76bc58a188a66db649 (patch) | |
tree | 14068aa1ae93bfe0a8ef5e82930af149cedc0acb | |
parent | 62356cff409134f60a2d7ca890fa63fca9718477 (diff) | |
download | serenity-29e96eceebbe7f214c735a76bc58a188a66db649.zip |
LibJS: Convert PropertyKey::from_value() to ThrowCompletionOr
Lots of MUST() - perhaps we'll eventually come up with a better API for
the common case where it can't fail.
7 files changed, 23 insertions, 25 deletions
diff --git a/Userland/Libraries/LibJS/AST.cpp b/Userland/Libraries/LibJS/AST.cpp index a706574d4a..27122eff6d 100644 --- a/Userland/Libraries/LibJS/AST.cpp +++ b/Userland/Libraries/LibJS/AST.cpp @@ -1286,9 +1286,7 @@ ThrowCompletionOr<Reference> MemberExpression::to_reference(Interpreter& interpr TRY(require_object_coercible(global_object, base_value)); - property_name = PropertyKey::from_value(global_object, value); - if (auto* exception = interpreter.exception()) - return throw_completion(exception->value()); + property_name = TRY(PropertyKey::from_value(global_object, value)); } else if (is<PrivateIdentifier>(*m_property)) { auto& private_identifier = static_cast<PrivateIdentifier const&>(*m_property); return make_private_reference(interpreter.vm(), base_value, private_identifier.string()); @@ -1380,9 +1378,7 @@ static ThrowCompletionOr<ClassElement::ClassElementName> class_key_to_property_n if (prop_key.is_object()) prop_key = TRY(prop_key.to_primitive(global_object, Value::PreferredType::String)); - auto property_key = PropertyKey::from_value(global_object, prop_key); - if (auto* exception = interpreter.exception()) - return throw_completion(exception->value()); + auto property_key = TRY(PropertyKey::from_value(global_object, prop_key)); return ClassElement::ClassElementName { property_key }; } @@ -2858,14 +2854,14 @@ Completion ObjectExpression::execute(Interpreter& interpreter, GlobalObject& glo switch (property.type()) { case ObjectProperty::Type::Getter: VERIFY(value.is_function()); - object->define_direct_accessor(PropertyKey::from_value(global_object, key), &value.as_function(), nullptr, Attribute::Configurable | Attribute::Enumerable); + object->define_direct_accessor(TRY(PropertyKey::from_value(global_object, key)), &value.as_function(), nullptr, Attribute::Configurable | Attribute::Enumerable); break; case ObjectProperty::Type::Setter: VERIFY(value.is_function()); - object->define_direct_accessor(PropertyKey::from_value(global_object, key), nullptr, &value.as_function(), Attribute::Configurable | Attribute::Enumerable); + object->define_direct_accessor(TRY(PropertyKey::from_value(global_object, key)), nullptr, &value.as_function(), Attribute::Configurable | Attribute::Enumerable); break; case ObjectProperty::Type::KeyValue: - object->define_direct_property(PropertyKey::from_value(global_object, key), value, JS::default_attributes); + object->define_direct_property(TRY(PropertyKey::from_value(global_object, key)), value, JS::default_attributes); break; case ObjectProperty::Type::Spread: default: diff --git a/Userland/Libraries/LibJS/Runtime/Object.cpp b/Userland/Libraries/LibJS/Runtime/Object.cpp index 092caea399..ce7aab7eb2 100644 --- a/Userland/Libraries/LibJS/Runtime/Object.cpp +++ b/Userland/Libraries/LibJS/Runtime/Object.cpp @@ -296,7 +296,7 @@ ThrowCompletionOr<bool> Object::set_integrity_level(IntegrityLevel level) if (level == IntegrityLevel::Sealed) { // a. For each element k of keys, do for (auto& key : keys) { - auto property_name = PropertyKey::from_value(global_object, key); + auto property_name = MUST(PropertyKey::from_value(global_object, key)); // i. Perform ? DefinePropertyOrThrow(O, k, PropertyDescriptor { [[Configurable]]: false }). TRY(define_property_or_throw(property_name, { .configurable = false })); @@ -308,7 +308,7 @@ ThrowCompletionOr<bool> Object::set_integrity_level(IntegrityLevel level) // b. For each element k of keys, do for (auto& key : keys) { - auto property_name = PropertyKey::from_value(global_object, key); + auto property_name = MUST(PropertyKey::from_value(global_object, key)); // i. Let currentDesc be ? O.[[GetOwnProperty]](k). auto current_descriptor = TRY(internal_get_own_property(property_name)); @@ -360,7 +360,7 @@ ThrowCompletionOr<bool> Object::test_integrity_level(IntegrityLevel level) const // 7. For each element k of keys, do for (auto& key : keys) { - auto property_name = PropertyKey::from_value(global_object(), key); + auto property_name = MUST(PropertyKey::from_value(global_object(), key)); // a. Let currentDesc be ? O.[[GetOwnProperty]](k). auto current_descriptor = TRY(internal_get_own_property(property_name)); @@ -405,7 +405,7 @@ ThrowCompletionOr<MarkedValueList> Object::enumerable_own_property_names(Propert // a. If Type(key) is String, then if (!key.is_string()) continue; - auto property_name = PropertyKey::from_value(global_object, key); + auto property_name = MUST(PropertyKey::from_value(global_object, key)); // i. Let desc be ? O.[[GetOwnProperty]](key). auto descriptor = TRY(internal_get_own_property(property_name)); @@ -453,7 +453,7 @@ ThrowCompletionOr<Object*> Object::copy_data_properties(Value source, HashTable< auto* from_object = MUST(source.to_object(global_object)); for (auto& next_key_value : TRY(from_object->internal_own_property_keys())) { - auto next_key = PropertyKey::from_value(global_object, next_key_value); + auto next_key = MUST(PropertyKey::from_value(global_object, next_key_value)); if (seen_names.contains(next_key)) continue; @@ -1151,7 +1151,7 @@ ThrowCompletionOr<Object*> Object::define_properties(Value properties) // 5. For each element nextKey of keys, do for (auto& next_key : keys) { - auto property_name = PropertyKey::from_value(global_object, next_key); + auto property_name = MUST(PropertyKey::from_value(global_object, next_key)); // a. Let propDesc be ? props.[[GetOwnProperty]](nextKey). auto property_descriptor = TRY(props->internal_get_own_property(property_name)); diff --git a/Userland/Libraries/LibJS/Runtime/ObjectConstructor.cpp b/Userland/Libraries/LibJS/Runtime/ObjectConstructor.cpp index a2851992d0..ba7506740f 100644 --- a/Userland/Libraries/LibJS/Runtime/ObjectConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/ObjectConstructor.cpp @@ -277,7 +277,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_descriptors) // 4. For each element key of ownKeys, do for (auto& key : own_keys) { - auto property_name = PropertyKey::from_value(global_object, key); + auto property_name = MUST(PropertyKey::from_value(global_object, key)); // a. Let desc be ? obj.[[GetOwnProperty]](key). auto desc = TRY(object->internal_get_own_property(property_name)); @@ -411,7 +411,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::assign) // iii. For each element nextKey of keys, do for (auto& next_key : keys) { - auto property_name = PropertyKey::from_value(global_object, next_key); + auto property_name = MUST(PropertyKey::from_value(global_object, next_key)); // 1. Let desc be ? from.[[GetOwnProperty]](nextKey). auto desc = TRY(from->internal_get_own_property(property_name)); diff --git a/Userland/Libraries/LibJS/Runtime/PropertyKey.h b/Userland/Libraries/LibJS/Runtime/PropertyKey.h index f74cf56bc2..0e76b3b300 100644 --- a/Userland/Libraries/LibJS/Runtime/PropertyKey.h +++ b/Userland/Libraries/LibJS/Runtime/PropertyKey.h @@ -26,15 +26,15 @@ public: No, }; - static PropertyKey from_value(GlobalObject& global_object, Value value) + static ThrowCompletionOr<PropertyKey> from_value(GlobalObject& global_object, Value value) { if (value.is_empty()) - return {}; + return PropertyKey {}; if (value.is_symbol()) - return value.as_symbol(); + return PropertyKey { value.as_symbol() }; if (value.is_integral_number() && value.as_double() >= 0 && value.as_double() < NumericLimits<u32>::max()) return value.as_u32(); - return TRY_OR_DISCARD(value.to_string(global_object)); + return TRY(value.to_string(global_object)); } PropertyKey() { } diff --git a/Userland/Libraries/LibJS/Runtime/ProxyObject.cpp b/Userland/Libraries/LibJS/Runtime/ProxyObject.cpp index 70480ce8f8..901ae4b18f 100644 --- a/Userland/Libraries/LibJS/Runtime/ProxyObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/ProxyObject.cpp @@ -699,8 +699,10 @@ ThrowCompletionOr<MarkedValueList> ProxyObject::internal_own_property_keys() con // 16. For each element key of targetKeys, do for (auto& key : target_keys) { + auto property_key = MUST(PropertyKey::from_value(global_object, key)); + // a. Let desc be ? target.[[GetOwnProperty]](key). - auto descriptor = TRY(m_target.internal_get_own_property(PropertyKey::from_value(global_object, key))); + auto descriptor = TRY(m_target.internal_get_own_property(property_key)); // b. If desc is not undefined and desc.[[Configurable]] is false, then if (descriptor.has_value() && !*descriptor->configurable) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp index 1ae79dc945..4045c90d69 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp @@ -810,7 +810,7 @@ ThrowCompletionOr<Object*> merge_largest_unit_option(GlobalObject& global_object // 3. For each element nextKey of keys, do for (auto& key : keys) { - auto next_key = PropertyKey::from_value(global_object, key); + auto next_key = MUST(PropertyKey::from_value(global_object, key)); // a. Let propValue be ? Get(options, nextKey). auto prop_value = TRY(options.get(next_key)); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp index 0745f86a00..f615f17de4 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp @@ -995,7 +995,7 @@ ThrowCompletionOr<Object*> default_merge_fields(GlobalObject& global_object, Obj for (auto& next_key : original_keys) { // a. If nextKey is not "month" or "monthCode", then if (next_key.as_string().string() != vm.names.month.as_string() && next_key.as_string().string() != vm.names.monthCode.as_string()) { - auto property_name = PropertyKey::from_value(global_object, next_key); + auto property_name = MUST(PropertyKey::from_value(global_object, next_key)); // i. Let propValue be ? Get(fields, nextKey). auto prop_value = TRY(fields.get(property_name)); @@ -1016,7 +1016,7 @@ ThrowCompletionOr<Object*> default_merge_fields(GlobalObject& global_object, Obj // 5. For each element nextKey of newKeys, do for (auto& next_key : new_keys) { - auto property_name = PropertyKey::from_value(global_object, next_key); + auto property_name = MUST(PropertyKey::from_value(global_object, next_key)); // a. Let propValue be ? Get(additionalFields, nextKey). auto prop_value = TRY(additional_fields.get(property_name)); |