Browse Source

LibJS: Convert coerce_options_to_object() to ThrowCompletionOr

Idan Horowitz 3 năm trước cách đây
mục cha
commit
b9c7a629f8

+ 8 - 5
Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp

@@ -573,9 +573,7 @@ ThrowCompletionOr<Array*> supported_locales(GlobalObject& global_object, Vector<
     auto& vm = global_object.vm();
 
     // 1. Set options to ? CoerceOptionsToObject(options).
-    auto* options_object = coerce_options_to_object(global_object, options);
-    if (vm.exception())
-        return {};
+    auto* options_object = TRY(coerce_options_to_object(global_object, options));
 
     // 2. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit").
     auto matcher = TRY(get_option(global_object, *options_object, vm.names.localeMatcher, Value::Type::String, { "lookup"sv, "best fit"sv }, "best fit"sv));
@@ -598,8 +596,10 @@ ThrowCompletionOr<Array*> supported_locales(GlobalObject& global_object, Vector<
 }
 
 // 9.2.12 CoerceOptionsToObject ( options ), https://tc39.es/ecma402/#sec-coerceoptionstoobject
-Object* coerce_options_to_object(GlobalObject& global_object, Value options)
+ThrowCompletionOr<Object*> coerce_options_to_object(GlobalObject& global_object, Value options)
 {
+    auto& vm = global_object.vm();
+
     // 1. If options is undefined, then
     if (options.is_undefined()) {
         // a. Return ! OrdinaryObjectCreate(null).
@@ -607,7 +607,10 @@ Object* coerce_options_to_object(GlobalObject& global_object, Value options)
     }
 
     // 2. Return ? ToObject(options).
-    return options.to_object(global_object);
+    auto result = options.to_object(global_object);
+    if (auto* exception = vm.exception())
+        return throw_completion(exception->value());
+    return result;
 }
 
 // 9.2.13 GetOption ( options, property, type, values, fallback ), https://tc39.es/ecma402/#sec-getoption

+ 1 - 1
Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.h

@@ -45,7 +45,7 @@ LocaleResult resolve_locale(Vector<String> const& requested_locales, LocaleOptio
 Vector<String> lookup_supported_locales(Vector<String> const& requested_locales);
 Vector<String> best_fit_supported_locales(Vector<String> const& requested_locales);
 ThrowCompletionOr<Array*> supported_locales(GlobalObject&, Vector<String> const& requested_locales, Value options);
-Object* coerce_options_to_object(GlobalObject& global_object, Value options);
+ThrowCompletionOr<Object*> coerce_options_to_object(GlobalObject& global_object, Value options);
 ThrowCompletionOr<Value> get_option(GlobalObject& global_object, Object const& options, PropertyName const& property, Value::Type type, Vector<StringView> const& values, Fallback fallback);
 Optional<int> default_number_option(GlobalObject& global_object, Value value, int minimum, int maximum, Optional<int> fallback);
 Optional<int> get_number_option(GlobalObject& global_object, Object const& options, PropertyName const& property, int minimum, int maximum, Optional<int> fallback);

+ 1 - 3
Userland/Libraries/LibJS/Runtime/Intl/LocaleConstructor.cpp

@@ -298,9 +298,7 @@ Value LocaleConstructor::construct(FunctionObject& new_target)
     }
 
     // 10. Set options to ? CoerceOptionsToObject(options).
-    auto* options = coerce_options_to_object(global_object, options_value);
-    if (vm.exception())
-        return {};
+    auto* options = TRY_OR_DISCARD(coerce_options_to_object(global_object, options_value));
 
     // 11. Set tag to ? ApplyOptionsToTag(tag, options).
     if (auto applied_tag = apply_options_to_tag(global_object, tag, *options); applied_tag.has_value())

+ 1 - 3
Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp

@@ -360,9 +360,7 @@ NumberFormat* initialize_number_format(GlobalObject& global_object, NumberFormat
     auto requested_locales = TRY_OR_DISCARD(canonicalize_locale_list(global_object, locales_value));
 
     // 2. Set options to ? CoerceOptionsToObject(options).
-    auto* options = coerce_options_to_object(global_object, options_value);
-    if (vm.exception())
-        return {};
+    auto* options = TRY_OR_DISCARD(coerce_options_to_object(global_object, options_value));
 
     // 3. Let opt be a new Record.
     LocaleOptions opt {};