diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2021-10-18 00:05:01 +0300 |
---|---|---|
committer | Idan Horowitz <idan.horowitz@gmail.com> | 2021-10-18 08:01:38 +0300 |
commit | 85a28a65550ff76fd7c352fccb7b76f576cff33f (patch) | |
tree | 808e684fc8f75633ac9c284f609218c816c6da8f | |
parent | aad12b050bc2f34e367ef95ad89728a0bd4f662e (diff) | |
download | serenity-85a28a65550ff76fd7c352fccb7b76f576cff33f.zip |
LibJS: Convert to_index() to ThrowCompletionOr
7 files changed, 25 insertions, 40 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/ArrayBufferConstructor.cpp b/Userland/Libraries/LibJS/Runtime/ArrayBufferConstructor.cpp index 89cba9b86d..0367eecdea 100644 --- a/Userland/Libraries/LibJS/Runtime/ArrayBufferConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/ArrayBufferConstructor.cpp @@ -51,16 +51,17 @@ Value ArrayBufferConstructor::call() Value ArrayBufferConstructor::construct(FunctionObject& new_target) { auto& vm = this->vm(); - auto byte_length = vm.argument(0).to_index(global_object()); - if (vm.exception()) { - if (vm.exception()->value().is_object() && is<RangeError>(vm.exception()->value().as_object())) { + auto byte_length_or_error = vm.argument(0).to_index(global_object()); + if (byte_length_or_error.is_error()) { + auto error = byte_length_or_error.release_error(); + if (error.value().is_object() && is<RangeError>(error.value().as_object())) { // Re-throw more specific RangeError vm.clear_exception(); vm.throw_exception<RangeError>(global_object(), ErrorType::InvalidLength, "array buffer"); } return {}; } - return TRY_OR_DISCARD(allocate_array_buffer(global_object(), new_target, byte_length)); + return TRY_OR_DISCARD(allocate_array_buffer(global_object(), new_target, byte_length_or_error.release_value())); } // 25.1.4.1 ArrayBuffer.isView ( arg ), https://tc39.es/ecma262/#sec-arraybuffer.isview diff --git a/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp b/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp index 2864133b33..46a05dd0b6 100644 --- a/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp @@ -58,9 +58,7 @@ static ThrowCompletionOr<size_t> validate_atomic_access(GlobalObject& global_obj auto length = typed_array.array_length(); // 2. Let accessIndex be ? ToIndex(requestIndex). - auto access_index = request_index.to_index(global_object); - if (auto* exception = vm.exception()) - return throw_completion(exception->value()); + auto access_index = TRY(request_index.to_index(global_object)); // 3. Assert: accessIndex ≥ 0. diff --git a/Userland/Libraries/LibJS/Runtime/DataViewConstructor.cpp b/Userland/Libraries/LibJS/Runtime/DataViewConstructor.cpp index d555d6ab6f..bb1133849a 100644 --- a/Userland/Libraries/LibJS/Runtime/DataViewConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/DataViewConstructor.cpp @@ -53,9 +53,7 @@ Value DataViewConstructor::construct(FunctionObject& new_target) } auto& array_buffer = static_cast<ArrayBuffer&>(buffer.as_object()); - auto offset = vm.argument(1).to_index(global_object); - if (vm.exception()) - return {}; + auto offset = TRY_OR_DISCARD(vm.argument(1).to_index(global_object)); if (array_buffer.is_detached()) { vm.throw_exception<TypeError>(global_object, ErrorType::DetachedArrayBuffer); @@ -72,9 +70,7 @@ Value DataViewConstructor::construct(FunctionObject& new_target) if (vm.argument(2).is_undefined()) { view_byte_length = buffer_byte_length - offset; } else { - view_byte_length = vm.argument(2).to_index(global_object); - if (vm.exception()) - return {}; + view_byte_length = TRY_OR_DISCARD(vm.argument(2).to_index(global_object)); if (offset + view_byte_length > buffer_byte_length) { vm.throw_exception<RangeError>(global_object, ErrorType::InvalidLength, vm.names.DataView); return {}; diff --git a/Userland/Libraries/LibJS/Runtime/DataViewPrototype.cpp b/Userland/Libraries/LibJS/Runtime/DataViewPrototype.cpp index 0db2584923..8cbe575ce4 100644 --- a/Userland/Libraries/LibJS/Runtime/DataViewPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/DataViewPrototype.cpp @@ -63,9 +63,7 @@ static Value get_view_value(GlobalObject& global_object, Value request_index, Va if (!view) return {}; - auto get_index = request_index.to_index(global_object); - if (vm.exception()) - return {}; + auto get_index = TRY_OR_DISCARD(request_index.to_index(global_object)); auto little_endian = is_little_endian.to_boolean(); auto buffer = view->viewed_array_buffer(); @@ -102,9 +100,7 @@ static Value set_view_value(GlobalObject& global_object, Value request_index, Va if (!view) return {}; - auto get_index = request_index.to_index(global_object); - if (vm.exception()) - return {}; + auto get_index = TRY_OR_DISCARD(request_index.to_index(global_object)); Value number_value; if constexpr (IsIntegral<T> && sizeof(T) == 8) diff --git a/Userland/Libraries/LibJS/Runtime/TypedArray.cpp b/Userland/Libraries/LibJS/Runtime/TypedArray.cpp index 3c92ebb935..e78aaf3740 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArray.cpp +++ b/Userland/Libraries/LibJS/Runtime/TypedArray.cpp @@ -62,9 +62,7 @@ static ThrowCompletionOr<void> initialize_typed_array_from_array_buffer(GlobalOb auto element_size = typed_array.element_size(); // 3. Let offset be ? ToIndex(byteOffset). - auto offset = byte_offset.to_index(global_object); - if (auto* exception = vm.exception()) - return throw_completion(exception->value()); + auto offset = TRY(byte_offset.to_index(global_object)); // 4. If offset modulo elementSize ≠ 0, throw a RangeError exception. if (offset % element_size != 0) @@ -75,9 +73,7 @@ static ThrowCompletionOr<void> initialize_typed_array_from_array_buffer(GlobalOb // 5. If length is not undefined, then if (!length.is_undefined()) { // a. Let newLength be ? ToIndex(length). - new_length = length.to_index(global_object); - if (auto* exception = vm.exception()) - return throw_completion(exception->value()); + new_length = TRY(length.to_index(global_object)); } // 6. If IsDetachedBuffer(buffer) is true, throw a TypeError exception. @@ -482,15 +478,17 @@ void TypedArrayBase::visit_edges(Visitor& visitor) return typed_array; \ } \ \ - auto array_length = first_argument.to_index(global_object()); \ - if (vm.exception()) { \ - if (vm.exception()->value().is_object() && is<RangeError>(vm.exception()->value().as_object())) { \ + auto array_length_or_error = first_argument.to_index(global_object()); \ + if (array_length_or_error.is_error()) { \ + auto error = array_length_or_error.release_error(); \ + if (error.value().is_object() && is<RangeError>(error.value().as_object())) { \ /* Re-throw more specific RangeError */ \ vm.clear_exception(); \ vm.throw_exception<RangeError>(global_object(), ErrorType::InvalidLength, "typed array"); \ } \ return {}; \ } \ + auto array_length = array_length_or_error.release_value(); \ if (array_length > NumericLimits<i32>::max() / sizeof(Type)) { \ vm.throw_exception<RangeError>(global_object(), ErrorType::InvalidLength, "typed array"); \ return {}; \ diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp index de5199d617..5fb6efbe11 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.cpp +++ b/Userland/Libraries/LibJS/Runtime/Value.cpp @@ -707,24 +707,20 @@ ThrowCompletionOr<size_t> Value::to_length(GlobalObject& global_object) const } // 7.1.22 ToIndex ( argument ), https://tc39.es/ecma262/#sec-toindex -size_t Value::to_index(GlobalObject& global_object) const +ThrowCompletionOr<size_t> Value::to_index(GlobalObject& global_object) const { auto& vm = global_object.vm(); if (is_undefined()) return 0; auto integer_index = to_integer_or_infinity(global_object); - if (vm.exception()) - return {}; - if (integer_index < 0) { - vm.throw_exception<RangeError>(global_object, ErrorType::InvalidIndex); - return {}; - } + if (auto* exception = vm.exception()) + return throw_completion(exception->value()); + if (integer_index < 0) + return vm.throw_completion<RangeError>(global_object, ErrorType::InvalidIndex); auto index = MUST(Value(integer_index).to_length(global_object)); - if (integer_index != index) { - vm.throw_exception<RangeError>(global_object, ErrorType::InvalidIndex); - return {}; - } + if (integer_index != index) + return vm.throw_completion<RangeError>(global_object, ErrorType::InvalidIndex); return index; } diff --git a/Userland/Libraries/LibJS/Runtime/Value.h b/Userland/Libraries/LibJS/Runtime/Value.h index 49f7cc836b..60d29430a4 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.h +++ b/Userland/Libraries/LibJS/Runtime/Value.h @@ -323,7 +323,7 @@ public: ThrowCompletionOr<u8> to_u8(GlobalObject&) const; ThrowCompletionOr<u8> to_u8_clamp(GlobalObject&) const; ThrowCompletionOr<size_t> to_length(GlobalObject&) const; - size_t to_index(GlobalObject&) const; + ThrowCompletionOr<size_t> to_index(GlobalObject&) const; double to_integer_or_infinity(GlobalObject&) const; bool to_boolean() const; |