From da59c77fe3d865b3cf2620d52ab2416fa8cb78bd Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Tue, 12 Oct 2021 18:01:17 +0100 Subject: [PATCH] LibJS: Convert to_utf16_string() to ThrowCompletionOr --- .../LibJS/Runtime/AbstractOperations.cpp | 6 +- .../LibJS/Runtime/RegExpPrototype.cpp | 32 ++------ .../LibJS/Runtime/StringPrototype.cpp | 76 +++++-------------- Userland/Libraries/LibJS/Runtime/Value.cpp | 4 +- Userland/Libraries/LibJS/Runtime/Value.h | 2 +- 5 files changed, 30 insertions(+), 90 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp index 0ae5327b40..7ca678c43f 100644 --- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp @@ -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 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 (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; diff --git a/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp b/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp index ec4fd53561..3a8b0a374a 100644 --- a/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp @@ -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())); diff --git a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp index a505eb0ade..ae2249dc2b 100644 --- a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp @@ -37,7 +37,7 @@ static Optional 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 { 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 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) diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp index 47c8bfc829..ba19443b58 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.cpp +++ b/Userland/Libraries/LibJS/Runtime/Value.cpp @@ -363,12 +363,12 @@ ThrowCompletionOr Value::to_string(GlobalObject& global_object) const } } -Utf16String Value::to_utf16_string(GlobalObject& global_object) const +ThrowCompletionOr 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); } diff --git a/Userland/Libraries/LibJS/Runtime/Value.h b/Userland/Libraries/LibJS/Runtime/Value.h index 77556e96e5..97b478ba5f 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.h +++ b/Userland/Libraries/LibJS/Runtime/Value.h @@ -304,7 +304,7 @@ public: u64 encoded() const { return m_value.encoded; } ThrowCompletionOr to_string(GlobalObject&) const; - Utf16String to_utf16_string(GlobalObject&) const; + ThrowCompletionOr 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;