diff --git a/Userland/Applications/Spreadsheet/JSIntegration.cpp b/Userland/Applications/Spreadsheet/JSIntegration.cpp index 578cb9b8b3..463973d8e9 100644 --- a/Userland/Applications/Spreadsheet/JSIntegration.cpp +++ b/Userland/Applications/Spreadsheet/JSIntegration.cpp @@ -425,9 +425,7 @@ JS_DEFINE_NATIVE_FUNCTION(WorkbookObject::sheet) return JS::Value(&sheet.global_object()); } } else { - auto index = name_value.to_length(global_object); - if (vm.exception()) - return {}; + auto index = TRY_OR_DISCARD(name_value.to_length(global_object)); if (index < workbook.sheets().size()) return JS::Value(&workbook.sheets()[index].global_object()); } diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp index 19fb39a31f..116c41a909 100644 --- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp @@ -79,10 +79,7 @@ ThrowCompletionOr length_of_array_like(GlobalObject& global_object, Obje { auto& vm = global_object.vm(); auto result = TRY(object.get(vm.names.length)); - auto length = result.to_length(global_object); - if (auto* exception = vm.exception()) - return throw_completion(exception->value()); - return length; + return result.to_length(global_object); } // 7.3.19 CreateListFromArrayLike ( obj [ , elementTypes ] ), https://tc39.es/ecma262/#sec-createlistfromarraylike diff --git a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp index c665978060..69eec04981 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp @@ -212,9 +212,8 @@ ThrowCompletionOr> canonicalize_locale_list(GlobalObject& global_ } // 5. Let len be ? ToLength(? Get(O, "length")). - auto length = TRY(object->get(vm.names.length)).to_length(global_object); - if (auto* exception = vm.exception()) - return throw_completion(exception->value()); + auto length_value = TRY(object->get(vm.names.length)); + auto length = TRY(length_value.to_length(global_object)); // 6. Let k be 0. // 7. Repeat, while k < len, diff --git a/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp b/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp index 20a9d5170d..1afc7448e0 100644 --- a/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp @@ -72,9 +72,8 @@ static ThrowCompletionOr increment_last_index(GlobalObject& global_object, { auto& vm = global_object.vm(); - auto last_index = TRY(regexp_object.get(vm.names.lastIndex)).to_length(global_object); - if (auto* exception = vm.exception()) - return throw_completion(exception->value()); + auto last_index_value = TRY(regexp_object.get(vm.names.lastIndex)); + auto last_index = TRY(last_index_value.to_length(global_object)); last_index = advance_string_index(string, last_index, unicode); TRY(regexp_object.set(vm.names.lastIndex, Value(last_index), Object::ShouldThrowExceptions::Yes)); @@ -154,9 +153,8 @@ static Value regexp_builtin_exec(GlobalObject& global_object, RegExpObject& rege // FIXME: This should try using internal slots [[RegExpMatcher]], [[OriginalFlags]], etc. auto& vm = global_object.vm(); - auto last_index = TRY_OR_DISCARD(regexp_object.get(vm.names.lastIndex)).to_length(global_object); - if (vm.exception()) - return {}; + auto last_index_value = TRY_OR_DISCARD(regexp_object.get(vm.names.lastIndex)); + auto last_index = TRY_OR_DISCARD(last_index_value.to_length(global_object)); auto& regex = regexp_object.regex(); bool global = regex.options().has_flag_set(ECMAScriptFlags::Global); @@ -457,9 +455,8 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_match_all) if (vm.exception()) return {}; auto* matcher = TRY_OR_DISCARD(matcher_value.to_object(global_object)); - auto last_index = TRY_OR_DISCARD(regexp_object->get(vm.names.lastIndex)).to_length(global_object); - if (vm.exception()) - return {}; + auto last_index_value = TRY_OR_DISCARD(regexp_object->get(vm.names.lastIndex)); + auto last_index = TRY_OR_DISCARD(last_index_value.to_length(global_object)); TRY_OR_DISCARD(matcher->set(vm.names.lastIndex, Value(last_index), Object::ShouldThrowExceptions::Yes)); @@ -668,9 +665,8 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_split) continue; } - auto last_index = TRY_OR_DISCARD(splitter->get(vm.names.lastIndex)).to_length(global_object); // 'e' in the spec. - if (vm.exception()) - return {}; + auto last_index_value = TRY_OR_DISCARD(splitter->get(vm.names.lastIndex)); + auto last_index = TRY_OR_DISCARD(last_index_value.to_length(global_object)); // 'e' in the spec. last_index = min(last_index, string_view.length_in_code_units()); if (last_index == last_match_end) { diff --git a/Userland/Libraries/LibJS/Runtime/RegExpStringIteratorPrototype.cpp b/Userland/Libraries/LibJS/Runtime/RegExpStringIteratorPrototype.cpp index 0bf0c03255..289720ac1f 100644 --- a/Userland/Libraries/LibJS/Runtime/RegExpStringIteratorPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/RegExpStringIteratorPrototype.cpp @@ -58,9 +58,8 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpStringIteratorPrototype::next) auto match_string_value = TRY_OR_DISCARD(match_object->get(0)); auto match_string = TRY_OR_DISCARD(match_string_value.to_string(global_object)); if (match_string.is_empty()) { - auto last_index = TRY_OR_DISCARD(iterator->regexp_object().get(vm.names.lastIndex)).to_length(global_object); - if (vm.exception()) - return {}; + auto last_index_value = TRY_OR_DISCARD(iterator->regexp_object().get(vm.names.lastIndex)); + auto last_index = TRY_OR_DISCARD(last_index_value.to_length(global_object)); last_index = advance_string_index(iterator->string().view(), last_index, iterator->unicode()); diff --git a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp index 36ee5a65e3..a67e61f3b0 100644 --- a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp @@ -468,9 +468,7 @@ static Value pad_string(GlobalObject& global_object, Utf16String string, PadPlac auto& vm = global_object.vm(); auto string_length = string.length_in_code_units(); - auto max_length = vm.argument(0).to_length(global_object); - if (vm.exception()) - return {}; + auto max_length = TRY_OR_DISCARD(vm.argument(0).to_length(global_object)); if (max_length <= string_length) return js_string(vm, move(string)); diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp index d16ac860e6..de5199d617 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.cpp +++ b/Userland/Libraries/LibJS/Runtime/Value.cpp @@ -692,13 +692,13 @@ ThrowCompletionOr Value::to_u8_clamp(GlobalObject& global_object) const } // 7.1.20 ToLength ( argument ), https://tc39.es/ecma262/#sec-tolength -size_t Value::to_length(GlobalObject& global_object) const +ThrowCompletionOr Value::to_length(GlobalObject& global_object) const { auto& vm = global_object.vm(); auto len = to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); 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. @@ -720,8 +720,7 @@ size_t Value::to_index(GlobalObject& global_object) const vm.throw_exception(global_object, ErrorType::InvalidIndex); return {}; } - auto index = Value(integer_index).to_length(global_object); - VERIFY(!vm.exception()); + auto index = MUST(Value(integer_index).to_length(global_object)); if (integer_index != index) { vm.throw_exception(global_object, ErrorType::InvalidIndex); return {}; diff --git a/Userland/Libraries/LibJS/Runtime/Value.h b/Userland/Libraries/LibJS/Runtime/Value.h index 7696fcbc60..49f7cc836b 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.h +++ b/Userland/Libraries/LibJS/Runtime/Value.h @@ -322,7 +322,7 @@ public: ThrowCompletionOr to_i8(GlobalObject&) const; ThrowCompletionOr to_u8(GlobalObject&) const; ThrowCompletionOr to_u8_clamp(GlobalObject&) const; - size_t to_length(GlobalObject&) const; + ThrowCompletionOr to_length(GlobalObject&) const; size_t to_index(GlobalObject&) const; double to_integer_or_infinity(GlobalObject&) const; bool to_boolean() const;