LibJS: Convert to_index() to ThrowCompletionOr

This commit is contained in:
Idan Horowitz 2021-10-18 00:05:01 +03:00
parent aad12b050b
commit 85a28a6555
7 changed files with 25 additions and 40 deletions

View file

@ -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

View file

@ -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.

View file

@ -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 {};

View file

@ -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)

View file

@ -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 {}; \

View file

@ -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;
}

View file

@ -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;