diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp index d2744b8270..2fca5dc5da 100644 --- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp @@ -598,13 +598,13 @@ Value canonical_numeric_index_string(GlobalObject& global_object, PropertyName c } // 22.1.3.17.1 GetSubstitution ( matched, str, position, captures, namedCaptures, replacement ), https://tc39.es/ecma262/#sec-getsubstitution -String get_substitution(GlobalObject& global_object, Utf16View const& matched, Utf16View const& str, size_t position, Span captures, Value named_captures, Value replacement) +ThrowCompletionOr get_substitution(GlobalObject& global_object, Utf16View const& matched, Utf16View const& str, size_t position, Span captures, Value named_captures, Value replacement) { auto& vm = global_object.vm(); auto replace_string = replacement.to_utf16_string(global_object); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); auto replace_view = replace_string.view(); StringBuilder result; @@ -647,8 +647,8 @@ String get_substitution(GlobalObject& global_object, Utf16View const& matched, U if (!value.is_undefined()) { auto value_string = value.to_string(global_object); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); result.append(value_string); } @@ -675,13 +675,13 @@ String get_substitution(GlobalObject& global_object, Utf16View const& matched, U auto group_name = group_name_view.to_utf8(Utf16View::AllowInvalidCodeUnits::Yes); auto capture = named_captures.as_object().get(group_name); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); if (!capture.is_undefined()) { auto capture_string = capture.to_string(global_object); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); result.append(capture_string); } diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.h b/Userland/Libraries/LibJS/Runtime/AbstractOperations.h index 2fff8e266e..d588e31f68 100644 --- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.h +++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.h @@ -30,7 +30,7 @@ ThrowCompletionOr get_prototype_from_constructor(GlobalObject&, Functio Object* create_unmapped_arguments_object(GlobalObject&, Span arguments); Object* create_mapped_arguments_object(GlobalObject&, FunctionObject&, Vector const&, Span arguments, Environment&); Value canonical_numeric_index_string(GlobalObject&, PropertyName const&); -String get_substitution(GlobalObject&, Utf16View const& matched, Utf16View const& str, size_t position, Span captures, Value named_captures, Value replacement); +ThrowCompletionOr get_substitution(GlobalObject&, Utf16View const& matched, Utf16View const& str, size_t position, Span captures, Value named_captures, Value replacement); enum class CallerMode { Strict, diff --git a/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp b/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp index c20ec59a91..c848fc28cb 100644 --- a/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp @@ -700,9 +700,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_replace) return {}; } - replacement = get_substitution(global_object, matched.view(), string_view, position, captures, named_captures_object, replace_value); - if (vm.exception()) - return {}; + replacement = TRY_OR_DISCARD(get_substitution(global_object, matched.view(), string_view, position, captures, named_captures_object, replace_value)); } if (position >= next_source_position) { diff --git a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp index ef3a040299..606343611b 100644 --- a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp @@ -966,9 +966,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::replace) if (vm.exception()) return {}; } else { - replacement = get_substitution(global_object, search_string.view(), string.view(), *position, {}, js_undefined(), replace_value); - if (vm.exception()) - return {}; + replacement = TRY_OR_DISCARD(get_substitution(global_object, search_string.view(), string.view(), *position, {}, js_undefined(), replace_value)); } StringBuilder builder; @@ -1060,9 +1058,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::replace_all) if (vm.exception()) return {}; } else { - replacement = get_substitution(global_object, search_string.view(), string.view(), position, {}, js_undefined(), replace_value); - if (vm.exception()) - return {}; + replacement = TRY_OR_DISCARD(get_substitution(global_object, search_string.view(), string.view(), position, {}, js_undefined(), replace_value)); } result.append(preserved);