Sfoglia il codice sorgente

LibJS: Add explicit ErrorType values for TypedArray prototype exceptions

Timothy Flynn 3 anni fa
parent
commit
0174993bea

+ 4 - 0
Userland/Libraries/LibJS/Runtime/ErrorTypes.h

@@ -277,8 +277,12 @@
     M(TypedArrayContentTypeMismatch, "Can't create {} from {}")                                                                         \
     M(TypedArrayContentTypeMismatch, "Can't create {} from {}")                                                                         \
     M(TypedArrayInvalidBufferLength, "Invalid buffer length for {}: must be a multiple of {}, got {}")                                  \
     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(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(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(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(TypedArrayPrototypeOneArg, "TypedArray.prototype.{}() requires at least one argument")                                            \
     M(TypedArrayFailedSettingIndex, "Failed setting value of index {} of typed array")                                                  \
     M(TypedArrayFailedSettingIndex, "Failed setting value of index {} of typed array")                                                  \
     M(TypedArrayTypeIsNot, "Typed array {} element type is not {}")                                                                     \
     M(TypedArrayTypeIsNot, "Typed array {} element type is not {}")                                                                     \

+ 8 - 8
Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp

@@ -647,17 +647,17 @@ static ThrowCompletionOr<void> set_typed_array_from_typed_array(GlobalObject& gl
 
 
     // 15. If targetOffset is +∞, throw a RangeError exception.
     // 15. If targetOffset is +∞, throw a RangeError exception.
     if (isinf(target_offset))
     if (isinf(target_offset))
-        return vm.throw_completion<RangeError>(global_object, "Invalid target offset");
+        return vm.throw_completion<RangeError>(global_object, ErrorType::TypedArrayInvalidTargetOffset, "finite");
 
 
     // 16. If srcLength + targetOffset > targetLength, throw a RangeError exception.
     // 16. If srcLength + targetOffset > targetLength, throw a RangeError exception.
     Checked<size_t> checked = source_length;
     Checked<size_t> checked = source_length;
     checked += static_cast<u32>(target_offset);
     checked += static_cast<u32>(target_offset);
     if (checked.has_overflow() || checked.value() > target_length)
     if (checked.has_overflow() || checked.value() > target_length)
-        return vm.throw_completion<RangeError>(global_object, "Overflow or out of bounds in target length");
+        return vm.throw_completion<RangeError>(global_object, ErrorType::TypedArrayOverflowOrOutOfBounds, "target length");
 
 
     // 17. If target.[[ContentType]] ≠ source.[[ContentType]], throw a TypeError exception.
     // 17. If target.[[ContentType]] ≠ source.[[ContentType]], throw a TypeError exception.
     if (target.content_type() != source.content_type())
     if (target.content_type() != source.content_type())
-        return vm.throw_completion<TypeError>(global_object, "Copy between arrays of different content types is prohibited");
+        return vm.throw_completion<TypeError>(global_object, ErrorType::TypedArrayInvalidCopy, target.class_name(), source.class_name());
 
 
     // FIXME: 18. If both IsSharedArrayBuffer(srcBuffer) and IsSharedArrayBuffer(targetBuffer) are true, then
     // 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.
     // 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<void> set_typed_array_from_typed_array(GlobalObject& gl
     checked_target_byte_index *= target_element_size;
     checked_target_byte_index *= target_element_size;
     checked_target_byte_index += target_byte_offset;
     checked_target_byte_index += target_byte_offset;
     if (checked_target_byte_index.has_overflow())
     if (checked_target_byte_index.has_overflow())
-        return vm.throw_completion<RangeError>(global_object, "Overflow in target byte index");
+        return vm.throw_completion<RangeError>(global_object, ErrorType::TypedArrayOverflow, "target byte index");
     auto target_byte_index = checked_target_byte_index.value();
     auto target_byte_index = checked_target_byte_index.value();
 
 
     // 23. Let limit be targetByteIndex + targetElementSize × srcLength.
     // 23. Let limit be targetByteIndex + targetElementSize × srcLength.
@@ -696,7 +696,7 @@ static ThrowCompletionOr<void> set_typed_array_from_typed_array(GlobalObject& gl
     checked_limit *= target_element_size;
     checked_limit *= target_element_size;
     checked_limit += target_byte_index;
     checked_limit += target_byte_index;
     if (checked_limit.has_overflow())
     if (checked_limit.has_overflow())
-        return vm.throw_completion<RangeError>(global_object, "Overflow in target limit");
+        return vm.throw_completion<RangeError>(global_object, ErrorType::TypedArrayOverflow, "target limit");
     auto limit = checked_limit.value();
     auto limit = checked_limit.value();
 
 
     // 24. If srcType is the same as targetType, then
     // 24. If srcType is the same as targetType, then
@@ -748,13 +748,13 @@ static ThrowCompletionOr<void> set_typed_array_from_array_like(GlobalObject& glo
 
 
     // 6. If targetOffset is +∞, throw a RangeError exception.
     // 6. If targetOffset is +∞, throw a RangeError exception.
     if (isinf(target_offset))
     if (isinf(target_offset))
-        return vm.throw_completion<RangeError>(global_object, "Invalid target offset");
+        return vm.throw_completion<RangeError>(global_object, ErrorType::TypedArrayInvalidTargetOffset, "finite");
 
 
     // 7. If srcLength + targetOffset > targetLength, throw a RangeError exception.
     // 7. If srcLength + targetOffset > targetLength, throw a RangeError exception.
     Checked<size_t> checked = source_length;
     Checked<size_t> checked = source_length;
     checked += static_cast<u32>(target_offset);
     checked += static_cast<u32>(target_offset);
     if (checked.has_overflow() || checked.value() > target_length)
     if (checked.has_overflow() || checked.value() > target_length)
-        return vm.throw_completion<RangeError>(global_object, "Overflow or out of bounds in target length");
+        return vm.throw_completion<RangeError>(global_object, ErrorType::TypedArrayOverflowOrOutOfBounds, "target length");
 
 
     // 8. Let k be 0.
     // 8. Let k be 0.
     size_t k = 0;
     size_t k = 0;
@@ -800,7 +800,7 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::set)
 
 
     // 5. If targetOffset < 0, throw a RangeError exception.
     // 5. If targetOffset < 0, throw a RangeError exception.
     if (target_offset < 0)
     if (target_offset < 0)
-        return vm.throw_completion<RangeError>(global_object, "Invalid target offset");
+        return vm.throw_completion<RangeError>(global_object, ErrorType::TypedArrayInvalidTargetOffset, "positive");
 
 
     // 6. If source is an Object that has a [[TypedArrayName]] internal slot, then
     // 6. If source is an Object that has a [[TypedArrayName]] internal slot, then
     if (source.is_object() && is<TypedArrayBase>(source.as_object())) {
     if (source.is_object() && is<TypedArrayBase>(source.as_object())) {