1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 08:27:45 +00:00

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) Value ArrayBufferConstructor::construct(FunctionObject& new_target)
{ {
auto& vm = this->vm(); auto& vm = this->vm();
auto byte_length = vm.argument(0).to_index(global_object()); auto byte_length_or_error = vm.argument(0).to_index(global_object());
if (vm.exception()) { if (byte_length_or_error.is_error()) {
if (vm.exception()->value().is_object() && is<RangeError>(vm.exception()->value().as_object())) { auto error = byte_length_or_error.release_error();
if (error.value().is_object() && is<RangeError>(error.value().as_object())) {
// Re-throw more specific RangeError // Re-throw more specific RangeError
vm.clear_exception(); vm.clear_exception();
vm.throw_exception<RangeError>(global_object(), ErrorType::InvalidLength, "array buffer"); vm.throw_exception<RangeError>(global_object(), ErrorType::InvalidLength, "array buffer");
} }
return {}; 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 // 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(); auto length = typed_array.array_length();
// 2. Let accessIndex be ? ToIndex(requestIndex). // 2. Let accessIndex be ? ToIndex(requestIndex).
auto access_index = request_index.to_index(global_object); auto access_index = TRY(request_index.to_index(global_object));
if (auto* exception = vm.exception())
return throw_completion(exception->value());
// 3. Assert: accessIndex ≥ 0. // 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& array_buffer = static_cast<ArrayBuffer&>(buffer.as_object());
auto offset = vm.argument(1).to_index(global_object); auto offset = TRY_OR_DISCARD(vm.argument(1).to_index(global_object));
if (vm.exception())
return {};
if (array_buffer.is_detached()) { if (array_buffer.is_detached()) {
vm.throw_exception<TypeError>(global_object, ErrorType::DetachedArrayBuffer); vm.throw_exception<TypeError>(global_object, ErrorType::DetachedArrayBuffer);
@ -72,9 +70,7 @@ Value DataViewConstructor::construct(FunctionObject& new_target)
if (vm.argument(2).is_undefined()) { if (vm.argument(2).is_undefined()) {
view_byte_length = buffer_byte_length - offset; view_byte_length = buffer_byte_length - offset;
} else { } else {
view_byte_length = vm.argument(2).to_index(global_object); view_byte_length = TRY_OR_DISCARD(vm.argument(2).to_index(global_object));
if (vm.exception())
return {};
if (offset + view_byte_length > buffer_byte_length) { if (offset + view_byte_length > buffer_byte_length) {
vm.throw_exception<RangeError>(global_object, ErrorType::InvalidLength, vm.names.DataView); vm.throw_exception<RangeError>(global_object, ErrorType::InvalidLength, vm.names.DataView);
return {}; return {};

View file

@ -63,9 +63,7 @@ static Value get_view_value(GlobalObject& global_object, Value request_index, Va
if (!view) if (!view)
return {}; return {};
auto get_index = request_index.to_index(global_object); auto get_index = TRY_OR_DISCARD(request_index.to_index(global_object));
if (vm.exception())
return {};
auto little_endian = is_little_endian.to_boolean(); auto little_endian = is_little_endian.to_boolean();
auto buffer = view->viewed_array_buffer(); auto buffer = view->viewed_array_buffer();
@ -102,9 +100,7 @@ static Value set_view_value(GlobalObject& global_object, Value request_index, Va
if (!view) if (!view)
return {}; return {};
auto get_index = request_index.to_index(global_object); auto get_index = TRY_OR_DISCARD(request_index.to_index(global_object));
if (vm.exception())
return {};
Value number_value; Value number_value;
if constexpr (IsIntegral<T> && sizeof(T) == 8) 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(); auto element_size = typed_array.element_size();
// 3. Let offset be ? ToIndex(byteOffset). // 3. Let offset be ? ToIndex(byteOffset).
auto offset = byte_offset.to_index(global_object); auto offset = TRY(byte_offset.to_index(global_object));
if (auto* exception = vm.exception())
return throw_completion(exception->value());
// 4. If offset modulo elementSize ≠ 0, throw a RangeError exception. // 4. If offset modulo elementSize ≠ 0, throw a RangeError exception.
if (offset % element_size != 0) 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 // 5. If length is not undefined, then
if (!length.is_undefined()) { if (!length.is_undefined()) {
// a. Let newLength be ? ToIndex(length). // a. Let newLength be ? ToIndex(length).
new_length = length.to_index(global_object); new_length = TRY(length.to_index(global_object));
if (auto* exception = vm.exception())
return throw_completion(exception->value());
} }
// 6. If IsDetachedBuffer(buffer) is true, throw a TypeError exception. // 6. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
@ -482,15 +478,17 @@ void TypedArrayBase::visit_edges(Visitor& visitor)
return typed_array; \ return typed_array; \
} \ } \
\ \
auto array_length = first_argument.to_index(global_object()); \ auto array_length_or_error = first_argument.to_index(global_object()); \
if (vm.exception()) { \ if (array_length_or_error.is_error()) { \
if (vm.exception()->value().is_object() && is<RangeError>(vm.exception()->value().as_object())) { \ auto error = array_length_or_error.release_error(); \
if (error.value().is_object() && is<RangeError>(error.value().as_object())) { \
/* Re-throw more specific RangeError */ \ /* Re-throw more specific RangeError */ \
vm.clear_exception(); \ vm.clear_exception(); \
vm.throw_exception<RangeError>(global_object(), ErrorType::InvalidLength, "typed array"); \ vm.throw_exception<RangeError>(global_object(), ErrorType::InvalidLength, "typed array"); \
} \ } \
return {}; \ return {}; \
} \ } \
auto array_length = array_length_or_error.release_value(); \
if (array_length > NumericLimits<i32>::max() / sizeof(Type)) { \ if (array_length > NumericLimits<i32>::max() / sizeof(Type)) { \
vm.throw_exception<RangeError>(global_object(), ErrorType::InvalidLength, "typed array"); \ vm.throw_exception<RangeError>(global_object(), ErrorType::InvalidLength, "typed array"); \
return {}; \ 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 // 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(); auto& vm = global_object.vm();
if (is_undefined()) if (is_undefined())
return 0; return 0;
auto integer_index = to_integer_or_infinity(global_object); auto integer_index = to_integer_or_infinity(global_object);
if (vm.exception()) if (auto* exception = vm.exception())
return {}; return throw_completion(exception->value());
if (integer_index < 0) { if (integer_index < 0)
vm.throw_exception<RangeError>(global_object, ErrorType::InvalidIndex); return vm.throw_completion<RangeError>(global_object, ErrorType::InvalidIndex);
return {};
}
auto index = MUST(Value(integer_index).to_length(global_object)); auto index = MUST(Value(integer_index).to_length(global_object));
if (integer_index != index) { if (integer_index != index)
vm.throw_exception<RangeError>(global_object, ErrorType::InvalidIndex); return vm.throw_completion<RangeError>(global_object, ErrorType::InvalidIndex);
return {};
}
return index; return index;
} }

View file

@ -323,7 +323,7 @@ public:
ThrowCompletionOr<u8> to_u8(GlobalObject&) const; ThrowCompletionOr<u8> to_u8(GlobalObject&) const;
ThrowCompletionOr<u8> to_u8_clamp(GlobalObject&) const; ThrowCompletionOr<u8> to_u8_clamp(GlobalObject&) const;
ThrowCompletionOr<size_t> to_length(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; double to_integer_or_infinity(GlobalObject&) const;
bool to_boolean() const; bool to_boolean() const;