1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 07:17:35 +00:00

LibJS: Convert get_substitution to ThrowCompletionOr

This commit is contained in:
Idan Horowitz 2021-09-21 22:05:21 +03:00
parent 69430855e0
commit f302b114f3
4 changed files with 13 additions and 19 deletions

View file

@ -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 // 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<Value> captures, Value named_captures, Value replacement) ThrowCompletionOr<String> get_substitution(GlobalObject& global_object, Utf16View const& matched, Utf16View const& str, size_t position, Span<Value> captures, Value named_captures, Value replacement)
{ {
auto& vm = global_object.vm(); auto& vm = global_object.vm();
auto replace_string = replacement.to_utf16_string(global_object); auto replace_string = replacement.to_utf16_string(global_object);
if (vm.exception()) if (auto* exception = vm.exception())
return {}; return throw_completion(exception->value());
auto replace_view = replace_string.view(); auto replace_view = replace_string.view();
StringBuilder result; StringBuilder result;
@ -647,8 +647,8 @@ String get_substitution(GlobalObject& global_object, Utf16View const& matched, U
if (!value.is_undefined()) { if (!value.is_undefined()) {
auto value_string = value.to_string(global_object); auto value_string = value.to_string(global_object);
if (vm.exception()) if (auto* exception = vm.exception())
return {}; return throw_completion(exception->value());
result.append(value_string); 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 group_name = group_name_view.to_utf8(Utf16View::AllowInvalidCodeUnits::Yes);
auto capture = named_captures.as_object().get(group_name); auto capture = named_captures.as_object().get(group_name);
if (vm.exception()) if (auto* exception = vm.exception())
return {}; return throw_completion(exception->value());
if (!capture.is_undefined()) { if (!capture.is_undefined()) {
auto capture_string = capture.to_string(global_object); auto capture_string = capture.to_string(global_object);
if (vm.exception()) if (auto* exception = vm.exception())
return {}; return throw_completion(exception->value());
result.append(capture_string); result.append(capture_string);
} }

View file

@ -30,7 +30,7 @@ ThrowCompletionOr<Object*> get_prototype_from_constructor(GlobalObject&, Functio
Object* create_unmapped_arguments_object(GlobalObject&, Span<Value> arguments); Object* create_unmapped_arguments_object(GlobalObject&, Span<Value> arguments);
Object* create_mapped_arguments_object(GlobalObject&, FunctionObject&, Vector<FunctionNode::Parameter> const&, Span<Value> arguments, Environment&); Object* create_mapped_arguments_object(GlobalObject&, FunctionObject&, Vector<FunctionNode::Parameter> const&, Span<Value> arguments, Environment&);
Value canonical_numeric_index_string(GlobalObject&, PropertyName const&); Value canonical_numeric_index_string(GlobalObject&, PropertyName const&);
String get_substitution(GlobalObject&, Utf16View const& matched, Utf16View const& str, size_t position, Span<Value> captures, Value named_captures, Value replacement); ThrowCompletionOr<String> get_substitution(GlobalObject&, Utf16View const& matched, Utf16View const& str, size_t position, Span<Value> captures, Value named_captures, Value replacement);
enum class CallerMode { enum class CallerMode {
Strict, Strict,

View file

@ -700,9 +700,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_replace)
return {}; return {};
} }
replacement = get_substitution(global_object, matched.view(), string_view, position, captures, named_captures_object, replace_value); replacement = TRY_OR_DISCARD(get_substitution(global_object, matched.view(), string_view, position, captures, named_captures_object, replace_value));
if (vm.exception())
return {};
} }
if (position >= next_source_position) { if (position >= next_source_position) {

View file

@ -966,9 +966,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::replace)
if (vm.exception()) if (vm.exception())
return {}; return {};
} else { } else {
replacement = get_substitution(global_object, search_string.view(), string.view(), *position, {}, js_undefined(), replace_value); replacement = TRY_OR_DISCARD(get_substitution(global_object, search_string.view(), string.view(), *position, {}, js_undefined(), replace_value));
if (vm.exception())
return {};
} }
StringBuilder builder; StringBuilder builder;
@ -1060,9 +1058,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::replace_all)
if (vm.exception()) if (vm.exception())
return {}; return {};
} else { } else {
replacement = get_substitution(global_object, search_string.view(), string.view(), position, {}, js_undefined(), replace_value); replacement = TRY_OR_DISCARD(get_substitution(global_object, search_string.view(), string.view(), position, {}, js_undefined(), replace_value));
if (vm.exception())
return {};
} }
result.append(preserved); result.append(preserved);