diff --git a/Userland/Libraries/LibJS/Runtime/ArrayBufferPrototype.cpp b/Userland/Libraries/LibJS/Runtime/ArrayBufferPrototype.cpp index cf75b6cac9..8370667975 100644 --- a/Userland/Libraries/LibJS/Runtime/ArrayBufferPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/ArrayBufferPrototype.cpp @@ -49,9 +49,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayBufferPrototype::slice) auto length = array_buffer_object->byte_length(); - auto relative_start = vm.argument(0).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto relative_start = TRY_OR_DISCARD(vm.argument(0).to_integer_or_infinity(global_object)); double first; if (relative_start < 0) @@ -59,9 +57,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayBufferPrototype::slice) else first = min(relative_start, (double)length); - auto relative_end = vm.argument(1).is_undefined() ? length : vm.argument(1).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto relative_end = vm.argument(1).is_undefined() ? length : TRY_OR_DISCARD(vm.argument(1).to_integer_or_infinity(global_object)); double final; if (relative_end < 0) diff --git a/Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp index 6a18ab85ea..390f5e4cee 100644 --- a/Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/ArrayPrototype.cpp @@ -569,9 +569,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::slice) auto initial_length = TRY_OR_DISCARD(length_of_array_like(global_object, *this_object)); - auto relative_start = vm.argument(0).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto relative_start = TRY_OR_DISCARD(vm.argument(0).to_integer_or_infinity(global_object)); double actual_start; @@ -584,13 +582,10 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::slice) double relative_end; - if (vm.argument(1).is_undefined() || vm.argument(1).is_empty()) { + if (vm.argument(1).is_undefined() || vm.argument(1).is_empty()) relative_end = (double)initial_length; - } else { - relative_end = vm.argument(1).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; - } + else + relative_end = TRY_OR_DISCARD(vm.argument(1).to_integer_or_infinity(global_object)); double final; @@ -642,9 +637,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::index_of) return Value(-1); // 4. Let n be ? ToIntegerOrInfinity(fromIndex). - auto n = from_index.to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto n = TRY_OR_DISCARD(from_index.to_integer_or_infinity(global_object)); // 5. Assert: If fromIndex is undefined, then n is 0. if (from_index.is_undefined()) @@ -1086,13 +1079,10 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::last_index_of) double n; // 4. If fromIndex is present, let n be ? ToIntegerOrInfinity(fromIndex); else let n be len - 1. - if (vm.argument_count() >= 2) { - n = from_index.to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; - } else { + if (vm.argument_count() >= 2) + n = TRY_OR_DISCARD(from_index.to_integer_or_infinity(global_object)); + else n = (double)length - 1; - } // 5. If n is -∞, return -1𝔽. if (Value(n).is_negative_infinity()) @@ -1147,9 +1137,8 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::includes) return Value(false); u64 from_index = 0; if (vm.argument_count() >= 2) { - auto from_argument = vm.argument(1).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto from_argument = TRY_OR_DISCARD(vm.argument(1).to_integer_or_infinity(global_object)); + if (Value(from_argument).is_positive_infinity() || from_argument >= length) return Value(false); @@ -1435,9 +1424,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::splice) auto initial_length = TRY_OR_DISCARD(length_of_array_like(global_object, *this_object)); - auto relative_start = vm.argument(0).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto relative_start = TRY_OR_DISCARD(vm.argument(0).to_integer_or_infinity(global_object)); if (Value(relative_start).is_negative_infinity()) relative_start = 0; @@ -1456,9 +1443,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::splice) actual_delete_count = initial_length - actual_start; } else if (vm.argument_count() >= 2) { insert_count = vm.argument_count() - 2; - auto delete_count = vm.argument(1).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto delete_count = TRY_OR_DISCARD(vm.argument(1).to_integer_or_infinity(global_object)); auto temp = max(delete_count, 0); actual_delete_count = min(temp, initial_length - actual_start); } @@ -1539,18 +1524,14 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::fill) double relative_end = length; if (vm.argument_count() >= 2) { - relative_start = vm.argument(1).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + relative_start = TRY_OR_DISCARD(vm.argument(1).to_integer_or_infinity(global_object)); if (Value(relative_start).is_negative_infinity()) relative_start = 0; } // If end is undefined, let relativeEnd be len; else let relativeEnd be ? ToIntegerOrInfinity(end). if (vm.argument_count() >= 3 && !vm.argument(2).is_undefined()) { - relative_end = vm.argument(2).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + relative_end = TRY_OR_DISCARD(vm.argument(2).to_integer_or_infinity(global_object)); if (Value(relative_end).is_negative_infinity()) relative_end = 0; } @@ -1647,9 +1628,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::flat) double depth = 1; if (!vm.argument(0).is_undefined()) { - auto depth_num = vm.argument(0).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto depth_num = TRY_OR_DISCARD(vm.argument(0).to_integer_or_infinity(global_object)); depth = max(depth_num, 0.0); } @@ -1701,9 +1680,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::copy_within) auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object)); auto length = TRY_OR_DISCARD(length_of_array_like(global_object, *this_object)); - auto relative_target = vm.argument(0).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto relative_target = TRY_OR_DISCARD(vm.argument(0).to_integer_or_infinity(global_object)); double to; if (relative_target < 0) @@ -1711,9 +1688,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::copy_within) else to = min(relative_target, (double)length); - auto relative_start = vm.argument(1).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto relative_start = TRY_OR_DISCARD(vm.argument(1).to_integer_or_infinity(global_object)); double from; if (relative_start < 0) @@ -1721,9 +1696,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::copy_within) else from = min(relative_start, (double)length); - auto relative_end = vm.argument(2).is_undefined() ? length : vm.argument(2).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto relative_end = vm.argument(2).is_undefined() ? length : TRY_OR_DISCARD(vm.argument(2).to_integer_or_infinity(global_object)); double final; if (relative_end < 0) @@ -1772,9 +1745,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::at) { auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object)); auto length = TRY_OR_DISCARD(length_of_array_like(global_object, *this_object)); - auto relative_index = vm.argument(0).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto relative_index = TRY_OR_DISCARD(vm.argument(0).to_integer_or_infinity(global_object)); if (Value(relative_index).is_infinity()) return js_undefined(); Checked index { 0 }; diff --git a/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp b/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp index 46a05dd0b6..bfb3b5801c 100644 --- a/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp @@ -93,15 +93,11 @@ static ThrowCompletionOr atomic_read_modify_write(GlobalObject& global_ob Value value_to_set; // 4. If typedArray.[[ContentType]] is BigInt, let v be ? ToBigInt(value). - if (typed_array.content_type() == TypedArrayBase::ContentType::BigInt) { + if (typed_array.content_type() == TypedArrayBase::ContentType::BigInt) value_to_set = TRY(value.to_bigint(global_object)); - } // 5. Otherwise, let v be 𝔽(? ToIntegerOrInfinity(value)). - else { - value_to_set = Value(value.to_integer_or_infinity(global_object)); - if (auto* exception = vm.exception()) - return throw_completion(exception->value()); - } + else + value_to_set = Value(TRY(value.to_integer_or_infinity(global_object))); // 6. If IsDetachedBuffer(buffer) is true, throw a TypeError exception. if (buffer->is_detached()) @@ -232,14 +228,10 @@ static ThrowCompletionOr atomic_compare_exchange_impl(GlobalObject& globa // 6. Else, else { // a. Let expected be 𝔽(? ToIntegerOrInfinity(expectedValue)). - expected = Value(vm.argument(2).to_integer_or_infinity(global_object)); - if (auto* exception = vm.exception()) - return throw_completion(exception->value()); + expected = Value(TRY(vm.argument(2).to_integer_or_infinity(global_object))); // b. Let replacement be 𝔽(? ToIntegerOrInfinity(replacementValue)). - replacement = Value(vm.argument(3).to_integer_or_infinity(global_object)); - if (auto* exception = vm.exception()) - return throw_completion(exception->value()); + replacement = Value(TRY(vm.argument(3).to_integer_or_infinity(global_object))); } // 7. If IsDetachedBuffer(buffer) is true, throw a TypeError exception. @@ -322,10 +314,7 @@ JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::exchange) // 25.4.7 Atomics.isLockFree ( size ), https://tc39.es/ecma262/#sec-atomics.islockfree JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::is_lock_free) { - auto size = vm.argument(0).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; - + auto size = TRY_OR_DISCARD(vm.argument(0).to_integer_or_infinity(global_object)); if (size == 1) return Value(AK::atomic_is_lock_free()); if (size == 2) @@ -387,13 +376,10 @@ JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::store) auto value = vm.argument(2); Value value_to_set; - if (typed_array->content_type() == TypedArrayBase::ContentType::BigInt) { + if (typed_array->content_type() == TypedArrayBase::ContentType::BigInt) value_to_set = TRY_OR_DISCARD(value.to_bigint(global_object)); - } else { - value_to_set = Value(value.to_integer_or_infinity(global_object)); - if (vm.exception()) - return {}; - } + else + value_to_set = Value(TRY_OR_DISCARD(value.to_integer_or_infinity(global_object))); if (typed_array->viewed_array_buffer()->is_detached()) { vm.throw_exception(global_object, ErrorType::DetachedArrayBuffer); diff --git a/Userland/Libraries/LibJS/Runtime/BigIntPrototype.cpp b/Userland/Libraries/LibJS/Runtime/BigIntPrototype.cpp index 0e646f9fae..2617759d80 100644 --- a/Userland/Libraries/LibJS/Runtime/BigIntPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/BigIntPrototype.cpp @@ -55,9 +55,7 @@ JS_DEFINE_NATIVE_FUNCTION(BigIntPrototype::to_string) return {}; double radix = 10; if (!vm.argument(0).is_undefined()) { - radix = vm.argument(0).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + radix = TRY_OR_DISCARD(vm.argument(0).to_integer_or_infinity(global_object)); if (radix < 2 || radix > 36) { vm.throw_exception(global_object, ErrorType::InvalidRadix); return {}; diff --git a/Userland/Libraries/LibJS/Runtime/Date.cpp b/Userland/Libraries/LibJS/Runtime/Date.cpp index b51808dd99..35f9327931 100644 --- a/Userland/Libraries/LibJS/Runtime/Date.cpp +++ b/Userland/Libraries/LibJS/Runtime/Date.cpp @@ -297,13 +297,13 @@ Value make_time(GlobalObject& global_object, Value hour, Value min, Value sec, V return js_nan(); // 2. Let h be 𝔽(! ToIntegerOrInfinity(hour)). - auto h = hour.to_integer_or_infinity(global_object); + auto h = MUST(hour.to_integer_or_infinity(global_object)); // 3. Let m be 𝔽(! ToIntegerOrInfinity(min)). - auto m = min.to_integer_or_infinity(global_object); + auto m = MUST(min.to_integer_or_infinity(global_object)); // 4. Let s be 𝔽(! ToIntegerOrInfinity(sec)). - auto s = sec.to_integer_or_infinity(global_object); + auto s = MUST(sec.to_integer_or_infinity(global_object)); // 5. Let milli be 𝔽(! ToIntegerOrInfinity(ms)). - auto milli = ms.to_integer_or_infinity(global_object); + auto milli = MUST(ms.to_integer_or_infinity(global_object)); // 6. Let t be ((h * msPerHour + m * msPerMinute) + s * msPerSecond) + milli, performing the arithmetic according to IEEE 754-2019 rules (that is, as if using the ECMAScript operators * and +). // NOTE: C++ arithmetic abides by IEEE 754 rules auto t = ((h * MS_PER_HOUR + m * MS_PER_MINUTE) + s * MS_PER_SECOND) + milli; @@ -325,11 +325,11 @@ Value make_day(GlobalObject& global_object, Value year, Value month, Value date) return js_nan(); // 2. Let y be 𝔽(! ToIntegerOrInfinity(year)). - auto y = year.to_integer_or_infinity(global_object); + auto y = MUST(year.to_integer_or_infinity(global_object)); // 3. Let m be 𝔽(! ToIntegerOrInfinity(month)). - auto m = month.to_integer_or_infinity(global_object); + auto m = MUST(month.to_integer_or_infinity(global_object)); // 4. Let dt be 𝔽(! ToIntegerOrInfinity(date)). - auto dt = date.to_integer_or_infinity(global_object); + auto dt = MUST(date.to_integer_or_infinity(global_object)); // 5. Let ym be y + 𝔽(floor(ℝ(m) / 12)). auto ym = Value(y + floor(m / 12)); // 6. If ym is not finite, return NaN. diff --git a/Userland/Libraries/LibJS/Runtime/JSONObject.cpp b/Userland/Libraries/LibJS/Runtime/JSONObject.cpp index bcf0a07b15..f008eb06a8 100644 --- a/Userland/Libraries/LibJS/Runtime/JSONObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/JSONObject.cpp @@ -89,7 +89,7 @@ String JSONObject::stringify_impl(GlobalObject& global_object, Value value, Valu } if (space.is_number()) { - auto space_mv = space.to_integer_or_infinity(global_object); + auto space_mv = MUST(space.to_integer_or_infinity(global_object)); space_mv = min(10, space_mv); state.gap = space_mv < 1 ? String::empty() : String::repeated(' ', space_mv); } else if (space.is_string()) { diff --git a/Userland/Libraries/LibJS/Runtime/NumberPrototype.cpp b/Userland/Libraries/LibJS/Runtime/NumberPrototype.cpp index 57309411ba..7947a7e9bb 100644 --- a/Userland/Libraries/LibJS/Runtime/NumberPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/NumberPrototype.cpp @@ -63,10 +63,7 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_fixed) if (vm.exception()) return {}; - auto fraction_digits = vm.argument(0).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; - + auto fraction_digits = TRY_OR_DISCARD(vm.argument(0).to_integer_or_infinity(global_object)); if (!vm.argument(0).is_finite_number()) { vm.throw_exception(global_object, ErrorType::InvalidFractionDigits); return {}; @@ -96,11 +93,8 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_string) double radix_argument = 10; auto argument = vm.argument(0); - if (!vm.argument(0).is_undefined()) { - radix_argument = argument.to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; - } + if (!vm.argument(0).is_undefined()) + radix_argument = TRY_OR_DISCARD(argument.to_integer_or_infinity(global_object)); int radix = (int)radix_argument; if (vm.exception() || radix < 2 || radix > 36) { diff --git a/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp b/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp index 1afc7448e0..5a0b61b225 100644 --- a/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp @@ -523,9 +523,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_replace) auto matched_length = matched.length_in_code_units(); auto position_value = TRY_OR_DISCARD(result.get(vm.names.index)); - double position = position_value.to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + double position = TRY_OR_DISCARD(position_value.to_integer_or_infinity(global_object)); position = clamp(position, static_cast(0), static_cast(string.length_in_code_units())); diff --git a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp index a67e61f3b0..b95f2697cf 100644 --- a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp @@ -181,10 +181,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::char_at) auto string = utf16_string_from(vm, global_object); if (vm.exception()) return {}; - auto position = vm.argument(0).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; - + auto position = TRY_OR_DISCARD(vm.argument(0).to_integer_or_infinity(global_object)); if (position < 0 || position >= string.length_in_code_units()) return js_string(vm, String::empty()); @@ -197,10 +194,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::char_code_at) auto string = utf16_string_from(vm, global_object); if (vm.exception()) return {}; - auto position = vm.argument(0).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; - + auto position = TRY_OR_DISCARD(vm.argument(0).to_integer_or_infinity(global_object)); if (position < 0 || position >= string.length_in_code_units()) return js_nan(); @@ -213,10 +207,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::code_point_at) auto string = utf16_string_from(vm, global_object); if (vm.exception()) return {}; - auto position = vm.argument(0).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; - + auto position = TRY_OR_DISCARD(vm.argument(0).to_integer_or_infinity(global_object)); if (position < 0 || position >= string.length_in_code_units()) return js_undefined(); @@ -231,9 +222,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::repeat) if (!string.has_value()) return {}; - auto n = vm.argument(0).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto n = TRY_OR_DISCARD(vm.argument(0).to_integer_or_infinity(global_object)); if (n < 0) { vm.throw_exception(global_object, ErrorType::StringRepeatCountMustBe, "positive"); @@ -279,9 +268,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::starts_with) size_t start = 0; if (!vm.argument(1).is_undefined()) { - auto position = vm.argument(1).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto position = TRY_OR_DISCARD(vm.argument(1).to_integer_or_infinity(global_object)); start = clamp(position, static_cast(0), static_cast(string_length)); } @@ -317,9 +304,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::ends_with) size_t end = string_length; if (!vm.argument(1).is_undefined()) { - auto position = vm.argument(1).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto position = TRY_OR_DISCARD(vm.argument(1).to_integer_or_infinity(global_object)); end = clamp(position, static_cast(0), static_cast(string_length)); } @@ -347,9 +332,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::index_of) size_t start = 0; if (vm.argument_count() > 1) { - auto position = vm.argument(1).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto position = TRY_OR_DISCARD(vm.argument(1).to_integer_or_infinity(global_object)); start = clamp(position, static_cast(0), static_cast(utf16_string_view.length_in_code_units())); } @@ -565,16 +548,10 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::substring) return {}; auto string_length = static_cast(string.length_in_code_units()); - auto start = vm.argument(0).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; - + auto start = TRY_OR_DISCARD(vm.argument(0).to_integer_or_infinity(global_object)); auto end = string_length; - if (!vm.argument(1).is_undefined()) { - end = vm.argument(1).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; - } + if (!vm.argument(1).is_undefined()) + end = TRY_OR_DISCARD(vm.argument(1).to_integer_or_infinity(global_object)); size_t final_start = clamp(start, static_cast(0), string_length); size_t final_end = clamp(end, static_cast(0), string_length); @@ -593,9 +570,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::substr) return {}; auto size = string.length_in_code_units(); - auto int_start = vm.argument(0).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto int_start = TRY_OR_DISCARD(vm.argument(0).to_integer_or_infinity(global_object)); if (Value(int_start).is_negative_infinity()) int_start = 0; if (int_start < 0) @@ -603,10 +578,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::substr) auto length = vm.argument(1); - auto int_length = length.is_undefined() ? size : length.to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; - + auto int_length = length.is_undefined() ? size : TRY_OR_DISCARD(length.to_integer_or_infinity(global_object)); if (Value(int_start).is_positive_infinity() || (int_length <= 0) || Value(int_length).is_positive_infinity()) return js_string(vm, String::empty()); @@ -637,9 +609,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::includes) size_t start = 0; if (!vm.argument(1).is_undefined()) { - auto position = vm.argument(1).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto position = TRY_OR_DISCARD(vm.argument(1).to_integer_or_infinity(global_object)); start = clamp(position, static_cast(0), static_cast(string.length_in_code_units())); } @@ -655,9 +625,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::slice) return {}; auto string_length = static_cast(string.length_in_code_units()); - auto int_start = vm.argument(0).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto int_start = TRY_OR_DISCARD(vm.argument(0).to_integer_or_infinity(global_object)); if (Value(int_start).is_negative_infinity()) int_start = 0; else if (int_start < 0) @@ -667,9 +635,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::slice) auto int_end = string_length; if (!vm.argument(1).is_undefined()) { - int_end = vm.argument(1).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + int_end = TRY_OR_DISCARD(vm.argument(1).to_integer_or_infinity(global_object)); if (Value(int_end).is_negative_infinity()) int_end = 0; else if (int_end < 0) @@ -762,9 +728,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::last_index_of) auto search_length = search_string.length_in_code_units(); auto position = TRY_OR_DISCARD(vm.argument(1).to_number(global_object)); - double pos = position.is_nan() ? static_cast(INFINITY) : position.to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + double pos = position.is_nan() ? static_cast(INFINITY) : TRY_OR_DISCARD(position.to_integer_or_infinity(global_object)); size_t start = clamp(pos, static_cast(0), static_cast(string_length)); Optional last_index; @@ -794,9 +758,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::at) return {}; auto length = string.length_in_code_units(); - auto relative_index = vm.argument(0).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto relative_index = TRY_OR_DISCARD(vm.argument(0).to_integer_or_infinity(global_object)); if (Value(relative_index).is_infinity()) return js_undefined(); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp index 55f7eb1f35..5f85fdc877 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp @@ -751,7 +751,7 @@ ThrowCompletionOr parse_iso_date_time(GlobalObject& global_object, normalized_year = year_part.value_or(""); // 5. Set year to ! ToIntegerOrInfinity(year). - i32 year = Value(js_string(vm, normalized_year)).to_integer_or_infinity(global_object); + i32 year = MUST(Value(js_string(vm, normalized_year)).to_integer_or_infinity(global_object)); u8 month; // 6. If month is undefined, then @@ -984,7 +984,7 @@ ThrowCompletionOr parse_temporal_time_zone_string(GlobalObject VERIFY(sign_part.has_value()); // b. Set hours to ! ToIntegerOrInfinity(hours). - u8 hours = Value(js_string(vm, *hours_part)).to_integer_or_infinity(global_object); + u8 hours = MUST(Value(js_string(vm, *hours_part)).to_integer_or_infinity(global_object)); u8 sign; // c. If sign is the code unit 0x002D (HYPHEN-MINUS) or the code unit 0x2212 (MINUS SIGN), then @@ -999,10 +999,10 @@ ThrowCompletionOr parse_temporal_time_zone_string(GlobalObject } // e. Set minutes to ! ToIntegerOrInfinity(minutes). - u8 minutes = Value(js_string(vm, minutes_part.value_or(""sv))).to_integer_or_infinity(global_object); + u8 minutes = MUST(Value(js_string(vm, minutes_part.value_or(""sv))).to_integer_or_infinity(global_object)); // f. Set seconds to ! ToIntegerOrInfinity(seconds). - u8 seconds = Value(js_string(vm, seconds_part.value_or(""sv))).to_integer_or_infinity(global_object); + u8 seconds = MUST(Value(js_string(vm, seconds_part.value_or(""sv))).to_integer_or_infinity(global_object)); i32 nanoseconds; // g. If fraction is not undefined, then @@ -1011,7 +1011,7 @@ ThrowCompletionOr parse_temporal_time_zone_string(GlobalObject auto fraction = String::formatted("{}000000000", *fraction_part); // ii. Let nanoseconds be the String value equal to the substring of fraction from 0 to 9. // iii. Set nanoseconds to ! ToIntegerOrInfinity(nanoseconds). - nanoseconds = Value(js_string(vm, fraction.substring(0, 9))).to_integer_or_infinity(global_object); + nanoseconds = MUST(Value(js_string(vm, fraction.substring(0, 9))).to_integer_or_infinity(global_object)); } // h. Else, else { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h index f372a8d604..86de950d1c 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h @@ -123,9 +123,7 @@ ThrowCompletionOr to_integer_throw_on_infinity(GlobalObject& global_obje auto& vm = global_object.vm(); // 1. Let integer be ? ToIntegerOrInfinity(argument). - auto integer = argument.to_integer_or_infinity(global_object); - if (auto* exception = vm.exception()) - return throw_completion(exception->value()); + auto integer = TRY(argument.to_integer_or_infinity(global_object)); // 2. If integer is −∞ or +∞ , then if (Value(integer).is_infinity()) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp index 5a621c72b7..94fc8acc11 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp @@ -711,7 +711,7 @@ ThrowCompletionOr resolve_iso_month(GlobalObject& global_object, Object auto number_part = month_code_string.substring(1); // 8. Set numberPart to ! ToIntegerOrInfinity(numberPart). - auto number_part_integer = Value(js_string(vm, move(number_part))).to_integer_or_infinity(global_object); + auto number_part_integer = MUST(Value(js_string(vm, move(number_part))).to_integer_or_infinity(global_object)); // 9. If numberPart < 1 or numberPart > 12, throw a RangeError exception. if (number_part_integer < 1 || number_part_integer > 12) diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp index c88f0ff05d..79f75351f1 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp @@ -237,11 +237,11 @@ ThrowCompletionOr parse_time_zone_offset_string(GlobalObject& global_obj } // 7. Set hours to ! ToIntegerOrInfinity(hours). - auto hours = Value(js_string(vm, hours_part)).to_integer_or_infinity(global_object); + auto hours = MUST(Value(js_string(vm, hours_part)).to_integer_or_infinity(global_object)); // 8. Set minutes to ! ToIntegerOrInfinity(minutes). - auto minutes = Value(js_string(vm, minutes_part.value_or(""sv))).to_integer_or_infinity(global_object); + auto minutes = MUST(Value(js_string(vm, minutes_part.value_or(""sv))).to_integer_or_infinity(global_object)); // 9. Set seconds to ! ToIntegerOrInfinity(seconds). - auto seconds = Value(js_string(vm, seconds_part.value_or(""sv))).to_integer_or_infinity(global_object); + auto seconds = MUST(Value(js_string(vm, seconds_part.value_or(""sv))).to_integer_or_infinity(global_object)); double nanoseconds; // 10. If fraction is not undefined, then @@ -250,7 +250,7 @@ ThrowCompletionOr parse_time_zone_offset_string(GlobalObject& global_obj auto fraction = String::formatted("{}000000000", *fraction_part); // b. Let nanoseconds be the String value equal to the substring of fraction consisting of the code units with indices 0 (inclusive) through 9 (exclusive). // c. Set nanoseconds to ! ToIntegerOrInfinity(nanoseconds). - nanoseconds = Value(js_string(vm, fraction_part->substring_view(0, 9))).to_integer_or_infinity(global_object); + nanoseconds = MUST(Value(js_string(vm, fraction_part->substring_view(0, 9))).to_integer_or_infinity(global_object)); } // 11. Else, else { diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp index b8446a8943..00403a7eb7 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp @@ -207,9 +207,7 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::at) if (!typed_array) return {}; auto length = typed_array->array_length(); - auto relative_index = vm.argument(0).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto relative_index = TRY_OR_DISCARD(vm.argument(0).to_integer_or_infinity(global_object)); if (Value(relative_index).is_infinity()) return js_undefined(); Checked index { 0 }; @@ -253,9 +251,7 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::fill) else value = TRY_OR_DISCARD(vm.argument(0).to_number(global_object)); - auto relative_start = vm.argument(1).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto relative_start = TRY_OR_DISCARD(vm.argument(1).to_integer_or_infinity(global_object)); u32 k; if (Value(relative_start).is_negative_infinity()) @@ -266,13 +262,10 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::fill) k = min(relative_start, length); double relative_end; - if (vm.argument(2).is_undefined()) { + if (vm.argument(2).is_undefined()) relative_end = length; - } else { - relative_end = vm.argument(2).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; - } + else + relative_end = TRY_OR_DISCARD(vm.argument(2).to_integer_or_infinity(global_object)); u32 final; if (Value(relative_end).is_negative_infinity()) @@ -370,9 +363,7 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::includes) if (length == 0) return Value(false); - auto n = vm.argument(1).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto n = TRY_OR_DISCARD(vm.argument(1).to_integer_or_infinity(global_object)); auto value_n = Value(n); if (value_n.is_positive_infinity()) @@ -413,9 +404,7 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::index_of) if (length == 0) return Value(-1); - auto n = vm.argument(1).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto n = TRY_OR_DISCARD(vm.argument(1).to_integer_or_infinity(global_object)); auto value_n = Value(n); if (value_n.is_positive_infinity()) @@ -460,13 +449,10 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::last_index_of) return Value(-1); double n; - if (vm.argument_count() > 1) { - n = vm.argument(1).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; - } else { + if (vm.argument_count() > 1) + n = TRY_OR_DISCARD(vm.argument(1).to_integer_or_infinity(global_object)); + else n = length - 1; - } if (Value(n).is_negative_infinity()) return Value(-1); @@ -691,10 +677,7 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::set) auto source = vm.argument(0); - auto target_offset = vm.argument(1).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; - + auto target_offset = TRY_OR_DISCARD(vm.argument(1).to_integer_or_infinity(global_object)); if (target_offset < 0) { vm.throw_exception(global_object, "Invalid target offset"); return {}; @@ -848,9 +831,7 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::slice) auto length = typed_array->array_length(); - auto relative_start = vm.argument(0).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto relative_start = TRY_OR_DISCARD(vm.argument(0).to_integer_or_infinity(global_object)); i32 k; if (Value(relative_start).is_negative_infinity()) @@ -861,13 +842,10 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::slice) k = min(relative_start, length); double relative_end; - if (vm.argument(1).is_undefined()) { + if (vm.argument(1).is_undefined()) relative_end = length; - } else { - relative_end = vm.argument(1).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; - } + else + relative_end = TRY_OR_DISCARD(vm.argument(1).to_integer_or_infinity(global_object)); i32 final; if (Value(relative_end).is_negative_infinity()) @@ -1073,9 +1051,7 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::subarray) auto length = typed_array->array_length(); - auto relative_begin = vm.argument(0).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto relative_begin = TRY_OR_DISCARD(vm.argument(0).to_integer_or_infinity(global_object)); i32 begin_index; if (Value(relative_begin).is_negative_infinity()) @@ -1086,13 +1062,10 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::subarray) begin_index = min(relative_begin, length); double relative_end; - if (vm.argument(1).is_undefined()) { + if (vm.argument(1).is_undefined()) relative_end = length; - } else { - relative_end = vm.argument(1).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; - } + else + relative_end = TRY_OR_DISCARD(vm.argument(1).to_integer_or_infinity(global_object)); i32 end_index; if (Value(relative_end).is_negative_infinity()) @@ -1174,9 +1147,7 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::copy_within) auto length = typed_array->array_length(); // 4. Let relativeTarget be ? ToIntegerOrInfinity(target). - auto relative_target = vm.argument(0).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto relative_target = TRY_OR_DISCARD(vm.argument(0).to_integer_or_infinity(global_object)); double to; if (Value(relative_target).is_negative_infinity()) { @@ -1191,9 +1162,7 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::copy_within) } // 8. Let relativeStart be ? ToIntegerOrInfinity(start). - auto relative_start = vm.argument(1).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + auto relative_start = TRY_OR_DISCARD(vm.argument(1).to_integer_or_infinity(global_object)); double from; if (Value(relative_start).is_negative_infinity()) { @@ -1210,13 +1179,10 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::copy_within) double relative_end; // 12. If end is undefined, let relativeEnd be len; else let relativeEnd be ? ToIntegerOrInfinity(end). - if (vm.argument(2).is_undefined()) { + if (vm.argument(2).is_undefined()) relative_end = length; - } else { - relative_end = vm.argument(2).to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; - } + else + relative_end = TRY_OR_DISCARD(vm.argument(2).to_integer_or_infinity(global_object)); double final; if (Value(relative_end).is_negative_infinity()) { diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp index 5fb6efbe11..873428de97 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.cpp +++ b/Userland/Libraries/LibJS/Runtime/Value.cpp @@ -694,11 +694,7 @@ ThrowCompletionOr Value::to_u8_clamp(GlobalObject& global_object) const // 7.1.20 ToLength ( argument ), https://tc39.es/ecma262/#sec-tolength ThrowCompletionOr Value::to_length(GlobalObject& global_object) const { - auto& vm = global_object.vm(); - - auto len = to_integer_or_infinity(global_object); - if (auto* exception = vm.exception()) - return throw_completion(exception->value()); + auto len = TRY(to_integer_or_infinity(global_object)); if (len <= 0) return 0; // FIXME: The spec says that this function's output range is 0 - 2^53-1. But we don't want to overflow the size_t. @@ -713,9 +709,7 @@ ThrowCompletionOr Value::to_index(GlobalObject& global_object) const if (is_undefined()) return 0; - auto integer_index = to_integer_or_infinity(global_object); - if (auto* exception = vm.exception()) - return throw_completion(exception->value()); + auto integer_index = TRY(to_integer_or_infinity(global_object)); if (integer_index < 0) return vm.throw_completion(global_object, ErrorType::InvalidIndex); auto index = MUST(Value(integer_index).to_length(global_object)); @@ -725,9 +719,9 @@ ThrowCompletionOr Value::to_index(GlobalObject& global_object) const } // 7.1.5 ToIntegerOrInfinity ( argument ), https://tc39.es/ecma262/#sec-tointegerorinfinity -double Value::to_integer_or_infinity(GlobalObject& global_object) const +ThrowCompletionOr Value::to_integer_or_infinity(GlobalObject& global_object) const { - auto number = TRY_OR_DISCARD(to_number(global_object)); + auto number = TRY(to_number(global_object)); if (number.is_nan() || number.as_double() == 0) return 0; if (number.is_infinity()) diff --git a/Userland/Libraries/LibJS/Runtime/Value.h b/Userland/Libraries/LibJS/Runtime/Value.h index 60d29430a4..99bcccdeae 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.h +++ b/Userland/Libraries/LibJS/Runtime/Value.h @@ -324,7 +324,7 @@ public: ThrowCompletionOr to_u8_clamp(GlobalObject&) const; ThrowCompletionOr to_length(GlobalObject&) const; ThrowCompletionOr to_index(GlobalObject&) const; - double to_integer_or_infinity(GlobalObject&) const; + ThrowCompletionOr to_integer_or_infinity(GlobalObject&) const; bool to_boolean() const; Value get(GlobalObject&, PropertyName const&) const;