From 28e1e03234cce96fe1f130a28010bdda5e064837 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Sat, 23 Oct 2021 02:54:12 +0300 Subject: [PATCH] LibJS: Convert StringConstructor functions to ThrowCompletionOr --- .../LibJS/Runtime/StringConstructor.cpp | 44 +++++++++---------- .../LibJS/Runtime/StringConstructor.h | 6 +-- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/StringConstructor.cpp b/Userland/Libraries/LibJS/Runtime/StringConstructor.cpp index b6576656e6d..35b792bfaab 100644 --- a/Userland/Libraries/LibJS/Runtime/StringConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/StringConstructor.cpp @@ -31,9 +31,9 @@ void StringConstructor::initialize(GlobalObject& global_object) define_direct_property(vm.names.prototype, global_object.string_prototype(), 0); u8 attr = Attribute::Writable | Attribute::Configurable; - define_old_native_function(vm.names.raw, raw, 1, attr); - define_old_native_function(vm.names.fromCharCode, from_char_code, 1, attr); - define_old_native_function(vm.names.fromCodePoint, from_code_point, 1, attr); + define_native_function(vm.names.raw, raw, 1, attr); + define_native_function(vm.names.fromCharCode, from_char_code, 1, attr); + define_native_function(vm.names.fromCodePoint, from_code_point, 1, attr); define_direct_property(vm.names.length, Value(1), Attribute::Configurable); } @@ -67,12 +67,12 @@ ThrowCompletionOr StringConstructor::construct(FunctionObject& new_targ } // 22.1.2.4 String.raw ( template, ...substitutions ), https://tc39.es/ecma262/#sec-string.raw -JS_DEFINE_OLD_NATIVE_FUNCTION(StringConstructor::raw) +JS_DEFINE_NATIVE_FUNCTION(StringConstructor::raw) { - auto* cooked = TRY_OR_DISCARD(vm.argument(0).to_object(global_object)); - auto raw_value = TRY_OR_DISCARD(cooked->get(vm.names.raw)); - auto* raw = TRY_OR_DISCARD(raw_value.to_object(global_object)); - auto literal_segments = TRY_OR_DISCARD(length_of_array_like(global_object, *raw)); + auto* cooked = TRY(vm.argument(0).to_object(global_object)); + auto raw_value = TRY(cooked->get(vm.names.raw)); + auto* raw = TRY(raw_value.to_object(global_object)); + auto literal_segments = TRY(length_of_array_like(global_object, *raw)); if (literal_segments == 0) return js_string(vm, ""); @@ -82,8 +82,8 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(StringConstructor::raw) StringBuilder builder; for (size_t i = 0; i < literal_segments; ++i) { auto next_key = String::number(i); - auto next_segment_value = TRY_OR_DISCARD(raw->get(next_key)); - auto next_segment = TRY_OR_DISCARD(next_segment_value.to_string(global_object)); + auto next_segment_value = TRY(raw->get(next_key)); + auto next_segment = TRY(next_segment_value.to_string(global_object)); builder.append(next_segment); @@ -92,7 +92,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(StringConstructor::raw) if (i < number_of_substituions) { auto next = vm.argument(i + 1); - auto next_sub = TRY_OR_DISCARD(next.to_string(global_object)); + auto next_sub = TRY(next.to_string(global_object)); builder.append(next_sub); } } @@ -100,34 +100,30 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(StringConstructor::raw) } // 22.1.2.1 String.fromCharCode ( ...codeUnits ), https://tc39.es/ecma262/#sec-string.fromcharcode -JS_DEFINE_OLD_NATIVE_FUNCTION(StringConstructor::from_char_code) +JS_DEFINE_NATIVE_FUNCTION(StringConstructor::from_char_code) { Vector string; string.ensure_capacity(vm.argument_count()); for (size_t i = 0; i < vm.argument_count(); ++i) - string.append(TRY_OR_DISCARD(vm.argument(i).to_u16(global_object))); + string.append(TRY(vm.argument(i).to_u16(global_object))); return js_string(vm, Utf16String(move(string))); } // 22.1.2.2 String.fromCodePoint ( ...codePoints ), https://tc39.es/ecma262/#sec-string.fromcodepoint -JS_DEFINE_OLD_NATIVE_FUNCTION(StringConstructor::from_code_point) +JS_DEFINE_NATIVE_FUNCTION(StringConstructor::from_code_point) { Vector string; string.ensure_capacity(vm.argument_count()); // This will be an under-estimate if any code point is > 0xffff. for (size_t i = 0; i < vm.argument_count(); ++i) { - auto next_code_point = TRY_OR_DISCARD(vm.argument(i).to_number(global_object)); - if (!next_code_point.is_integral_number()) { - vm.throw_exception(global_object, ErrorType::InvalidCodePoint, next_code_point.to_string_without_side_effects()); - return {}; - } - auto code_point = TRY_OR_DISCARD(next_code_point.to_i32(global_object)); - if (code_point < 0 || code_point > 0x10FFFF) { - vm.throw_exception(global_object, ErrorType::InvalidCodePoint, next_code_point.to_string_without_side_effects()); - return {}; - } + auto next_code_point = TRY(vm.argument(i).to_number(global_object)); + if (!next_code_point.is_integral_number()) + return vm.throw_completion(global_object, ErrorType::InvalidCodePoint, next_code_point.to_string_without_side_effects()); + auto code_point = TRY(next_code_point.to_i32(global_object)); + if (code_point < 0 || code_point > 0x10FFFF) + return vm.throw_completion(global_object, ErrorType::InvalidCodePoint, next_code_point.to_string_without_side_effects()); AK::code_point_to_utf16(string, static_cast(code_point)); } diff --git a/Userland/Libraries/LibJS/Runtime/StringConstructor.h b/Userland/Libraries/LibJS/Runtime/StringConstructor.h index 053ba074d4c..4b083c9a341 100644 --- a/Userland/Libraries/LibJS/Runtime/StringConstructor.h +++ b/Userland/Libraries/LibJS/Runtime/StringConstructor.h @@ -24,9 +24,9 @@ public: private: virtual bool has_constructor() const override { return true; } - JS_DECLARE_OLD_NATIVE_FUNCTION(raw); - JS_DECLARE_OLD_NATIVE_FUNCTION(from_char_code); - JS_DECLARE_OLD_NATIVE_FUNCTION(from_code_point); + JS_DECLARE_NATIVE_FUNCTION(raw); + JS_DECLARE_NATIVE_FUNCTION(from_char_code); + JS_DECLARE_NATIVE_FUNCTION(from_code_point); }; }