diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp index 7ca678c43f..a345067b0c 100644 --- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp @@ -873,7 +873,7 @@ Value canonical_numeric_index_string(GlobalObject& global_object, PropertyName c auto n = argument.to_number(global_object); // 4. If SameValue(! ToString(n), argument) is false, return undefined. - if (!same_value(n.to_primitive_string(global_object), argument)) + if (!same_value(MUST(n.to_primitive_string(global_object)), argument)) return js_undefined(); // 5. Return n. diff --git a/Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp index 69750f46d5..de38032171 100644 --- a/Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp @@ -1009,12 +1009,15 @@ static void array_merge_sort(VM& vm, GlobalObject& global_object, FunctionObject // the Abstract Relational Comparison in line once iterating over code points, rather // than calling it twice after creating two primitive strings. - auto x_string = x.to_primitive_string(global_object); - if (vm.exception()) + auto x_string_or_error = x.to_primitive_string(global_object); + if (x_string_or_error.is_error()) return; - auto y_string = y.to_primitive_string(global_object); - if (vm.exception()) + auto x_string = x_string_or_error.release_value(); + + auto y_string_or_error = y.to_primitive_string(global_object); + if (y_string_or_error.is_error()) return; + auto y_string = y_string_or_error.release_value(); auto x_string_value = Value(x_string); auto y_string_value = Value(y_string); diff --git a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp index 3343b78271..c5abe697a6 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp @@ -633,9 +633,7 @@ ThrowCompletionOr get_option(GlobalObject& global_object, Object const& o // 6. If type is "string", then else { // a. Let value be ? ToString(value). - value = value.to_primitive_string(global_object); - if (auto* exception = vm.exception()) - return throw_completion(exception->value()); + value = TRY(value.to_primitive_string(global_object)); } // 7. If values is not undefined and values does not contain an element equal to value, throw a RangeError exception. diff --git a/Userland/Libraries/LibJS/Runtime/JSONObject.cpp b/Userland/Libraries/LibJS/Runtime/JSONObject.cpp index 25700a74f2..818864a878 100644 --- a/Userland/Libraries/LibJS/Runtime/JSONObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/JSONObject.cpp @@ -87,9 +87,7 @@ String JSONObject::stringify_impl(GlobalObject& global_object, Value value, Valu if (vm.exception()) return {}; } else if (is(space_object)) { - space = space.to_primitive_string(global_object); - if (vm.exception()) - return {}; + space = TRY_OR_DISCARD(space.to_primitive_string(global_object)); } } @@ -157,9 +155,7 @@ String JSONObject::serialize_json_property(GlobalObject& global_object, Stringif if (vm.exception()) return {}; } else if (is(value_object)) { - value = value.to_primitive_string(global_object); - if (vm.exception()) - return {}; + value = TRY_OR_DISCARD(value.to_primitive_string(global_object)); } else if (is(value_object)) { value = static_cast(value_object).value_of(); } else if (is(value_object)) { diff --git a/Userland/Libraries/LibJS/Runtime/StringConstructor.cpp b/Userland/Libraries/LibJS/Runtime/StringConstructor.cpp index 74d79441dc..151d46b439 100644 --- a/Userland/Libraries/LibJS/Runtime/StringConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/StringConstructor.cpp @@ -49,10 +49,7 @@ Value StringConstructor::call() return js_string(heap(), ""); if (vm().argument(0).is_symbol()) return js_string(heap(), vm().argument(0).as_symbol().to_string()); - auto* string = vm().argument(0).to_primitive_string(global_object()); - if (vm().exception()) - return {}; - return string; + return TRY_OR_DISCARD(vm().argument(0).to_primitive_string(global_object())); } // 22.1.1.1 String ( value ), https://tc39.es/ecma262/#sec-string-constructor-string-value @@ -64,9 +61,7 @@ Value StringConstructor::construct(FunctionObject& new_target) if (!vm.argument_count()) primitive_string = js_string(vm, ""); else - primitive_string = vm.argument(0).to_primitive_string(global_object()); - if (!primitive_string) - return {}; + primitive_string = TRY_OR_DISCARD(vm.argument(0).to_primitive_string(global_object())); auto* prototype = TRY_OR_DISCARD(get_prototype_from_constructor(global_object(), new_target, &GlobalObject::string_prototype)); return StringObject::create(global_object(), *primitive_string, *prototype); } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp index e9483f1e81..d335c38a1a 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp @@ -150,9 +150,7 @@ ThrowCompletionOr get_option(GlobalObject& global_object, Object const& o // 9. Else, else { // a. Set value to ? ToString(value). - value = value.to_primitive_string(global_object); - if (auto* exception = vm.exception()) - return throw_completion(exception->value()); + value = TRY(value.to_primitive_string(global_object)); } // 10. If values is not empty, then @@ -1117,9 +1115,7 @@ ThrowCompletionOr prepare_temporal_fields(GlobalObject& global_object, } else if (property.is_one_of("month", "day")) { value = Value(TRY(to_positive_integer(global_object, value))); } else if (property.is_one_of("monthCode", "offset", "era")) { - value = value.to_primitive_string(global_object); - if (auto* exception = vm.exception()) - return throw_completion(exception->value()); + value = TRY(value.to_primitive_string(global_object)); } } diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp index ba19443b58..920cc88932 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.cpp +++ b/Userland/Libraries/LibJS/Runtime/Value.cpp @@ -323,11 +323,11 @@ String Value::to_string_without_side_effects() const } } -PrimitiveString* Value::to_primitive_string(GlobalObject& global_object) +ThrowCompletionOr Value::to_primitive_string(GlobalObject& global_object) { if (is_string()) return &as_string(); - auto string = TRY_OR_DISCARD(to_string(global_object)); + auto string = TRY(to_string(global_object)); return js_string(global_object.heap(), string); } diff --git a/Userland/Libraries/LibJS/Runtime/Value.h b/Userland/Libraries/LibJS/Runtime/Value.h index 97b478ba5f..8b296c9781 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.h +++ b/Userland/Libraries/LibJS/Runtime/Value.h @@ -305,7 +305,7 @@ public: ThrowCompletionOr to_string(GlobalObject&) const; ThrowCompletionOr to_utf16_string(GlobalObject&) const; - PrimitiveString* to_primitive_string(GlobalObject&); + ThrowCompletionOr to_primitive_string(GlobalObject&); Value to_primitive(GlobalObject&, PreferredType preferred_type = PreferredType::Default) const; Object* to_object(GlobalObject&) const; Value to_numeric(GlobalObject&) const;