From 0174993bea2fd030eaf1d57dec650a213e76b489 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Fri, 15 Apr 2022 17:33:24 -0400 Subject: [PATCH] LibJS: Add explicit ErrorType values for TypedArray prototype exceptions --- Userland/Libraries/LibJS/Runtime/ErrorTypes.h | 4 ++++ .../LibJS/Runtime/TypedArrayPrototype.cpp | 16 ++++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/ErrorTypes.h b/Userland/Libraries/LibJS/Runtime/ErrorTypes.h index 64986a74d8..e1ce08bf08 100644 --- a/Userland/Libraries/LibJS/Runtime/ErrorTypes.h +++ b/Userland/Libraries/LibJS/Runtime/ErrorTypes.h @@ -277,8 +277,12 @@ M(TypedArrayContentTypeMismatch, "Can't create {} from {}") \ M(TypedArrayInvalidBufferLength, "Invalid buffer length for {}: must be a multiple of {}, got {}") \ M(TypedArrayInvalidByteOffset, "Invalid byte offset for {}: must be a multiple of {}, got {}") \ + M(TypedArrayInvalidCopy, "Copy between arrays of different content types ({} and {}) is prohibited") \ + M(TypedArrayInvalidTargetOffset, "Invalid target offset: must be {}") \ M(TypedArrayOutOfRangeByteOffset, "Typed array byte offset {} is out of range for buffer with length {}") \ M(TypedArrayOutOfRangeByteOffsetOrLength, "Typed array range {}:{} is out of range for buffer with length {}") \ + M(TypedArrayOverflow, "Overflow in {}") \ + M(TypedArrayOverflowOrOutOfBounds, "Overflow or out of bounds in {}") \ M(TypedArrayPrototypeOneArg, "TypedArray.prototype.{}() requires at least one argument") \ M(TypedArrayFailedSettingIndex, "Failed setting value of index {} of typed array") \ M(TypedArrayTypeIsNot, "Typed array {} element type is not {}") \ diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp index 79e5f392f3..e392e91163 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp @@ -647,17 +647,17 @@ static ThrowCompletionOr set_typed_array_from_typed_array(GlobalObject& gl // 15. If targetOffset is +∞, throw a RangeError exception. if (isinf(target_offset)) - return vm.throw_completion(global_object, "Invalid target offset"); + return vm.throw_completion(global_object, ErrorType::TypedArrayInvalidTargetOffset, "finite"); // 16. If srcLength + targetOffset > targetLength, throw a RangeError exception. Checked checked = source_length; checked += static_cast(target_offset); if (checked.has_overflow() || checked.value() > target_length) - return vm.throw_completion(global_object, "Overflow or out of bounds in target length"); + return vm.throw_completion(global_object, ErrorType::TypedArrayOverflowOrOutOfBounds, "target length"); // 17. If target.[[ContentType]] ≠ source.[[ContentType]], throw a TypeError exception. if (target.content_type() != source.content_type()) - return vm.throw_completion(global_object, "Copy between arrays of different content types is prohibited"); + return vm.throw_completion(global_object, ErrorType::TypedArrayInvalidCopy, target.class_name(), source.class_name()); // FIXME: 18. If both IsSharedArrayBuffer(srcBuffer) and IsSharedArrayBuffer(targetBuffer) are true, then // FIXME: a. If srcBuffer.[[ArrayBufferData]] and targetBuffer.[[ArrayBufferData]] are the same Shared Data Block values, let same be true; else let same be false. @@ -688,7 +688,7 @@ static ThrowCompletionOr set_typed_array_from_typed_array(GlobalObject& gl checked_target_byte_index *= target_element_size; checked_target_byte_index += target_byte_offset; if (checked_target_byte_index.has_overflow()) - return vm.throw_completion(global_object, "Overflow in target byte index"); + return vm.throw_completion(global_object, ErrorType::TypedArrayOverflow, "target byte index"); auto target_byte_index = checked_target_byte_index.value(); // 23. Let limit be targetByteIndex + targetElementSize × srcLength. @@ -696,7 +696,7 @@ static ThrowCompletionOr set_typed_array_from_typed_array(GlobalObject& gl checked_limit *= target_element_size; checked_limit += target_byte_index; if (checked_limit.has_overflow()) - return vm.throw_completion(global_object, "Overflow in target limit"); + return vm.throw_completion(global_object, ErrorType::TypedArrayOverflow, "target limit"); auto limit = checked_limit.value(); // 24. If srcType is the same as targetType, then @@ -748,13 +748,13 @@ static ThrowCompletionOr set_typed_array_from_array_like(GlobalObject& glo // 6. If targetOffset is +∞, throw a RangeError exception. if (isinf(target_offset)) - return vm.throw_completion(global_object, "Invalid target offset"); + return vm.throw_completion(global_object, ErrorType::TypedArrayInvalidTargetOffset, "finite"); // 7. If srcLength + targetOffset > targetLength, throw a RangeError exception. Checked checked = source_length; checked += static_cast(target_offset); if (checked.has_overflow() || checked.value() > target_length) - return vm.throw_completion(global_object, "Overflow or out of bounds in target length"); + return vm.throw_completion(global_object, ErrorType::TypedArrayOverflowOrOutOfBounds, "target length"); // 8. Let k be 0. size_t k = 0; @@ -800,7 +800,7 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::set) // 5. If targetOffset < 0, throw a RangeError exception. if (target_offset < 0) - return vm.throw_completion(global_object, "Invalid target offset"); + return vm.throw_completion(global_object, ErrorType::TypedArrayInvalidTargetOffset, "positive"); // 6. If source is an Object that has a [[TypedArrayName]] internal slot, then if (source.is_object() && is(source.as_object())) {