mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-23 08:00:20 +00:00
LibJS: Convert to_utf16_string() to ThrowCompletionOr
This commit is contained in:
parent
4d8912a92b
commit
da59c77fe3
Notes:
sideshowbarker
2024-07-18 02:47:42 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/da59c77fe3d Pull-request: https://github.com/SerenityOS/serenity/pull/10452 Reviewed-by: https://github.com/IdanHo ✅
5 changed files with 30 additions and 90 deletions
|
@ -883,11 +883,7 @@ 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
|
||||
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 (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
auto replace_string = TRY(replacement.to_utf16_string(global_object));
|
||||
auto replace_view = replace_string.view();
|
||||
|
||||
StringBuilder result;
|
||||
|
|
|
@ -344,9 +344,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::exec)
|
|||
if (!regexp_object)
|
||||
return {};
|
||||
|
||||
auto string = vm.argument(0).to_utf16_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto string = TRY_OR_DISCARD(vm.argument(0).to_utf16_string(global_object));
|
||||
|
||||
return regexp_builtin_exec(global_object, *regexp_object, move(string));
|
||||
}
|
||||
|
@ -358,9 +356,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::test)
|
|||
if (!regexp_object)
|
||||
return {};
|
||||
|
||||
auto string = vm.argument(0).to_utf16_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto string = TRY_OR_DISCARD(vm.argument(0).to_utf16_string(global_object));
|
||||
|
||||
auto match = regexp_exec(global_object, *regexp_object, move(string));
|
||||
if (vm.exception())
|
||||
|
@ -392,9 +388,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_match)
|
|||
if (!regexp_object)
|
||||
return {};
|
||||
|
||||
auto string = vm.argument(0).to_utf16_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto string = TRY_OR_DISCARD(vm.argument(0).to_utf16_string(global_object));
|
||||
|
||||
bool global = TRY_OR_DISCARD(regexp_object->get(vm.names.global)).to_boolean();
|
||||
|
||||
|
@ -448,9 +442,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_match_all)
|
|||
if (!regexp_object)
|
||||
return {};
|
||||
|
||||
auto string = vm.argument(0).to_utf16_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto string = TRY_OR_DISCARD(vm.argument(0).to_utf16_string(global_object));
|
||||
|
||||
auto* constructor = TRY_OR_DISCARD(species_constructor(global_object, *regexp_object, *global_object.regexp_constructor()));
|
||||
|
||||
|
@ -488,9 +480,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_replace)
|
|||
auto* regexp_object = this_object(global_object);
|
||||
if (!regexp_object)
|
||||
return {};
|
||||
auto string = string_value.to_utf16_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto string = TRY_OR_DISCARD(string_value.to_utf16_string(global_object));
|
||||
auto string_view = string.view();
|
||||
|
||||
if (!replace_value.is_function()) {
|
||||
|
@ -540,9 +530,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_replace)
|
|||
size_t n_captures = result_length == 0 ? 0 : result_length - 1;
|
||||
|
||||
auto matched_value = TRY_OR_DISCARD(result.get(0));
|
||||
auto matched = matched_value.to_utf16_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto matched = TRY_OR_DISCARD(matched_value.to_utf16_string(global_object));
|
||||
auto matched_length = matched.length_in_code_units();
|
||||
|
||||
auto position_value = TRY_OR_DISCARD(result.get(vm.names.index));
|
||||
|
@ -613,9 +601,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_search)
|
|||
if (!regexp_object)
|
||||
return {};
|
||||
|
||||
auto string = vm.argument(0).to_utf16_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto string = TRY_OR_DISCARD(vm.argument(0).to_utf16_string(global_object));
|
||||
|
||||
auto previous_last_index = TRY_OR_DISCARD(regexp_object->get(vm.names.lastIndex));
|
||||
if (!same_value(previous_last_index, Value(0)))
|
||||
|
@ -646,9 +632,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_split)
|
|||
if (!regexp_object)
|
||||
return {};
|
||||
|
||||
auto string = vm.argument(0).to_utf16_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto string = TRY_OR_DISCARD(vm.argument(0).to_utf16_string(global_object));
|
||||
auto string_view = string.view();
|
||||
|
||||
auto* constructor = TRY_OR_DISCARD(species_constructor(global_object, *regexp_object, *global_object.regexp_constructor()));
|
||||
|
|
|
@ -37,7 +37,7 @@ static Optional<String> ak_string_from(VM& vm, GlobalObject& global_object)
|
|||
static Utf16String utf16_string_from(VM& vm, GlobalObject& global_object)
|
||||
{
|
||||
auto this_value = TRY_OR_DISCARD(require_object_coercible(global_object, vm.this_value(global_object)));
|
||||
return this_value.to_utf16_string(global_object);
|
||||
return TRY_OR_DISCARD(this_value.to_utf16_string(global_object));
|
||||
}
|
||||
|
||||
// 22.1.3.21.1 SplitMatch ( S, q, R ), https://tc39.es/ecma262/#sec-splitmatch
|
||||
|
@ -273,10 +273,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::starts_with)
|
|||
return {};
|
||||
}
|
||||
|
||||
auto search_string = search_string_value.to_utf16_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
auto search_string = TRY_OR_DISCARD(search_string_value.to_utf16_string(global_object));
|
||||
auto string_length = string.length_in_code_units();
|
||||
auto search_length = search_string.length_in_code_units();
|
||||
|
||||
|
@ -314,10 +311,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::ends_with)
|
|||
return {};
|
||||
}
|
||||
|
||||
auto search_string = search_string_value.to_utf16_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
auto search_string = TRY_OR_DISCARD(search_string_value.to_utf16_string(global_object));
|
||||
auto string_length = string.length_in_code_units();
|
||||
auto search_length = search_string.length_in_code_units();
|
||||
|
||||
|
@ -347,10 +341,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::index_of)
|
|||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
auto search_string = vm.argument(0).to_utf16_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
auto search_string = TRY_OR_DISCARD(vm.argument(0).to_utf16_string(global_object));
|
||||
auto utf16_string_view = string.view();
|
||||
auto utf16_search_view = search_string.view();
|
||||
|
||||
|
@ -485,9 +476,7 @@ static Value pad_string(GlobalObject& global_object, Utf16String string, PadPlac
|
|||
|
||||
Utf16String fill_string(Vector<u16, 1> { 0x20 });
|
||||
if (!vm.argument(1).is_undefined()) {
|
||||
fill_string = vm.argument(1).to_utf16_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
fill_string = TRY_OR_DISCARD(vm.argument(1).to_utf16_string(global_object));
|
||||
if (fill_string.is_empty())
|
||||
return js_string(vm, move(string));
|
||||
}
|
||||
|
@ -646,9 +635,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::includes)
|
|||
return {};
|
||||
}
|
||||
|
||||
auto search_string = search_string_value.to_utf16_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto search_string = TRY_OR_DISCARD(search_string_value.to_utf16_string(global_object));
|
||||
|
||||
size_t start = 0;
|
||||
if (!vm.argument(1).is_undefined()) {
|
||||
|
@ -713,9 +700,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::split)
|
|||
return TRY_OR_DISCARD(vm.call(*splitter, separator_argument, object, limit_argument));
|
||||
}
|
||||
|
||||
auto string = object.to_utf16_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto string = TRY_OR_DISCARD(object.to_utf16_string(global_object));
|
||||
|
||||
auto* array = Array::create(global_object, 0);
|
||||
size_t array_length = 0;
|
||||
|
@ -727,9 +712,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::split)
|
|||
return {};
|
||||
}
|
||||
|
||||
auto separator = separator_argument.to_utf16_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto separator = TRY_OR_DISCARD(separator_argument.to_utf16_string(global_object));
|
||||
|
||||
if (limit == 0)
|
||||
return array;
|
||||
|
@ -779,10 +762,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::last_index_of)
|
|||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
auto search_string = vm.argument(0).to_utf16_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
auto search_string = TRY_OR_DISCARD(vm.argument(0).to_utf16_string(global_object));
|
||||
auto string_length = string.length_in_code_units();
|
||||
auto search_length = search_string.length_in_code_units();
|
||||
|
||||
|
@ -858,9 +838,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::match)
|
|||
return TRY_OR_DISCARD(vm.call(*matcher, regexp, this_object));
|
||||
}
|
||||
|
||||
auto string = this_object.to_utf16_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto string = TRY_OR_DISCARD(this_object.to_utf16_string(global_object));
|
||||
|
||||
auto rx = regexp_create(global_object, regexp, js_undefined());
|
||||
if (!rx)
|
||||
|
@ -888,9 +866,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::match_all)
|
|||
return TRY_OR_DISCARD(vm.call(*matcher, regexp, this_object));
|
||||
}
|
||||
|
||||
auto string = this_object.to_utf16_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto string = TRY_OR_DISCARD(this_object.to_utf16_string(global_object));
|
||||
|
||||
auto rx = regexp_create(global_object, regexp, js_string(vm, "g"));
|
||||
if (!rx)
|
||||
|
@ -910,20 +886,12 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::replace)
|
|||
return TRY_OR_DISCARD(vm.call(*replacer, search_value, this_object, replace_value));
|
||||
}
|
||||
|
||||
auto string = this_object.to_utf16_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto search_string = search_value.to_utf16_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto string = TRY_OR_DISCARD(this_object.to_utf16_string(global_object));
|
||||
auto search_string = TRY_OR_DISCARD(search_value.to_utf16_string(global_object));
|
||||
|
||||
if (!replace_value.is_function()) {
|
||||
auto replace_string = replace_value.to_utf16_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto replace_string = TRY_OR_DISCARD(replace_value.to_utf16_string(global_object));
|
||||
replace_value = js_string(vm, move(replace_string));
|
||||
if (vm.exception())
|
||||
return {};
|
||||
}
|
||||
|
||||
Optional<size_t> position = string_index_of(string.view(), search_string.view(), 0);
|
||||
|
@ -973,17 +941,11 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::replace_all)
|
|||
return TRY_OR_DISCARD(vm.call(*replacer, search_value, this_object, replace_value));
|
||||
}
|
||||
|
||||
auto string = this_object.to_utf16_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto search_string = search_value.to_utf16_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto string = TRY_OR_DISCARD(this_object.to_utf16_string(global_object));
|
||||
auto search_string = TRY_OR_DISCARD(search_value.to_utf16_string(global_object));
|
||||
|
||||
if (!replace_value.is_function()) {
|
||||
auto replace_string = replace_value.to_utf16_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto replace_string = TRY_OR_DISCARD(replace_value.to_utf16_string(global_object));
|
||||
replace_value = js_string(vm, move(replace_string));
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
@ -1037,9 +999,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::search)
|
|||
return TRY_OR_DISCARD(vm.call(*searcher, regexp, this_object));
|
||||
}
|
||||
|
||||
auto string = this_object.to_utf16_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto string = TRY_OR_DISCARD(this_object.to_utf16_string(global_object));
|
||||
|
||||
auto rx = regexp_create(global_object, regexp, js_undefined());
|
||||
if (!rx)
|
||||
|
|
|
@ -363,12 +363,12 @@ ThrowCompletionOr<String> Value::to_string(GlobalObject& global_object) const
|
|||
}
|
||||
}
|
||||
|
||||
Utf16String Value::to_utf16_string(GlobalObject& global_object) const
|
||||
ThrowCompletionOr<Utf16String> Value::to_utf16_string(GlobalObject& global_object) const
|
||||
{
|
||||
if (m_type == Type::String)
|
||||
return m_value.as_string->utf16_string();
|
||||
|
||||
auto utf8_string = TRY_OR_DISCARD(to_string(global_object));
|
||||
auto utf8_string = TRY(to_string(global_object));
|
||||
return Utf16String(utf8_string);
|
||||
}
|
||||
|
||||
|
|
|
@ -304,7 +304,7 @@ public:
|
|||
u64 encoded() const { return m_value.encoded; }
|
||||
|
||||
ThrowCompletionOr<String> to_string(GlobalObject&) const;
|
||||
Utf16String to_utf16_string(GlobalObject&) const;
|
||||
ThrowCompletionOr<Utf16String> to_utf16_string(GlobalObject&) const;
|
||||
PrimitiveString* to_primitive_string(GlobalObject&);
|
||||
Value to_primitive(GlobalObject&, PreferredType preferred_type = PreferredType::Default) const;
|
||||
Object* to_object(GlobalObject&) const;
|
||||
|
|
Loading…
Reference in a new issue