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:
parent
69430855e0
commit
f302b114f3
4 changed files with 13 additions and 19 deletions
|
@ -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<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 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);
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ ThrowCompletionOr<Object*> get_prototype_from_constructor(GlobalObject&, Functio
|
|||
Object* create_unmapped_arguments_object(GlobalObject&, Span<Value> arguments);
|
||||
Object* create_mapped_arguments_object(GlobalObject&, FunctionObject&, Vector<FunctionNode::Parameter> const&, Span<Value> arguments, Environment&);
|
||||
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 {
|
||||
Strict,
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue