LibJS: Allow fast conversions between Uint8Array and Uint8ClampedArray

These two typed array kinds have the same exact underlying data type and
differ only in how assignment works.

We can allow them to take the fast paths for same-type conversions.
This commit is contained in:
Andreas Kling 2025-12-01 10:50:33 +01:00 committed by Jelle Raaijmakers
parent 096eddfd5a
commit 4d7d8aa827
Notes: github-actions[bot] 2025-12-01 14:14:12 +00:00

View file

@ -1493,7 +1493,9 @@ static ThrowCompletionOr<void> set_typed_array_from_typed_array(VM& vm, TypedArr
auto limit = checked_limit.value();
// 23. If srcType is targetType, then
if (source.element_name() == target.element_name()) {
if (source.kind() == target.kind()
|| (source.kind() == TypedArrayBase::Kind::Uint8Array && target.kind() == TypedArrayBase::Kind::Uint8ClampedArray)
|| (source.kind() == TypedArrayBase::Kind::Uint8ClampedArray && target.kind() == TypedArrayBase::Kind::Uint8Array)) {
// a. NOTE: The transfer must be performed in a manner that preserves the bit-level encoding of the source data.
// b. Repeat, while targetByteIndex < limit,
// i. Let value be GetValueFromBuffer(srcBuffer, srcByteIndex, Uint8, true, Unordered).
@ -1700,7 +1702,9 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::slice)
// g. Let targetType be TypedArrayElementType(A).
// h. If srcType is targetType, then
if (typed_array->element_name() == array->element_name()) {
if (typed_array->kind() == array->kind()
|| (typed_array->kind() == TypedArrayBase::Kind::Uint8Array && array->kind() == TypedArrayBase::Kind::Uint8ClampedArray)
|| (typed_array->kind() == TypedArrayBase::Kind::Uint8ClampedArray && array->kind() == TypedArrayBase::Kind::Uint8Array)) {
// i. NOTE: The transfer must be performed in a manner that preserves the bit-level encoding of the source data.
// ii. Let srcBuffer be O.[[ViewedArrayBuffer]].