script: refactor create_buffer_source_with_constructor (#35952)

Signed-off-by: Taym <haddadi.taym@gmail.com>
This commit is contained in:
Taym Haddadi 2025-03-13 06:28:24 -04:00 committed by GitHub
parent bb0d08432e
commit 2ccb818a60
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -682,27 +682,24 @@ pub(crate) fn create_buffer_source_with_constructor(
byte_offset: usize, byte_offset: usize,
byte_length: usize, byte_length: usize,
) -> Fallible<HeapBufferSource<ArrayBufferViewU8>> { ) -> Fallible<HeapBufferSource<ArrayBufferViewU8>> {
let buffer = unsafe { match &buffer_source.buffer_source {
Heap::boxed( BufferSource::ArrayBuffer(heap) => match constructor {
*buffer_source Constructor::DataView => Ok(HeapBufferSource::new(BufferSource::ArrayBufferView(
.get_typed_array() Heap::boxed(unsafe {
.expect("Failed to get typed array") JS_NewDataView(*cx, heap.handle(), byte_offset, byte_length)
.underlying_object(), }),
) ))),
.handle() Constructor::Name(name_type) => construct_typed_array(
}; cx,
name_type,
match constructor { buffer_source,
Constructor::DataView => Ok(HeapBufferSource::new(BufferSource::ArrayBufferView( byte_offset,
Heap::boxed(unsafe { JS_NewDataView(*cx, buffer, byte_offset, byte_length) }), byte_length as i64,
))), ),
Constructor::Name(name_type) => construct_typed_array( },
cx, BufferSource::ArrayBufferView(_) => {
name_type, unreachable!("Can not create a new ArrayBufferView from an existing ArrayBufferView");
buffer_source, },
byte_offset,
byte_length as i64,
),
} }
} }
@ -714,52 +711,63 @@ fn construct_typed_array(
byte_offset: usize, byte_offset: usize,
byte_length: i64, byte_length: i64,
) -> Fallible<HeapBufferSource<ArrayBufferViewU8>> { ) -> Fallible<HeapBufferSource<ArrayBufferViewU8>> {
let buffer = unsafe { match &buffer_source.buffer_source {
Heap::boxed( BufferSource::ArrayBuffer(heap) => {
*buffer_source let array_view = unsafe {
.get_typed_array() match name_type {
.expect("Failed to get typed array") Type::Int8 => {
.underlying_object(), JS_NewInt8ArrayWithBuffer(*cx, heap.handle(), byte_offset, byte_length)
) },
.handle() Type::Uint8 => {
}; JS_NewUint8ArrayWithBuffer(*cx, heap.handle(), byte_offset, byte_length)
let array_view = match name_type { },
Type::Int8 => unsafe { JS_NewInt8ArrayWithBuffer(*cx, buffer, byte_offset, byte_length) }, Type::Uint16 => {
Type::Uint8 => unsafe { JS_NewUint8ArrayWithBuffer(*cx, buffer, byte_offset, byte_length) }, JS_NewUint16ArrayWithBuffer(*cx, heap.handle(), byte_offset, byte_length)
Type::Uint16 => unsafe { },
JS_NewUint16ArrayWithBuffer(*cx, buffer, byte_offset, byte_length) Type::Int16 => {
}, JS_NewInt16ArrayWithBuffer(*cx, heap.handle(), byte_offset, byte_length)
Type::Int16 => unsafe { JS_NewInt16ArrayWithBuffer(*cx, buffer, byte_offset, byte_length) }, },
Type::Int32 => unsafe { JS_NewInt32ArrayWithBuffer(*cx, buffer, byte_offset, byte_length) }, Type::Int32 => {
Type::Uint32 => unsafe { JS_NewInt32ArrayWithBuffer(*cx, heap.handle(), byte_offset, byte_length)
JS_NewUint32ArrayWithBuffer(*cx, buffer, byte_offset, byte_length) },
}, Type::Uint32 => {
Type::Float32 => unsafe { JS_NewUint32ArrayWithBuffer(*cx, heap.handle(), byte_offset, byte_length)
JS_NewFloat32ArrayWithBuffer(*cx, buffer, byte_offset, byte_length) },
}, Type::Float32 => {
Type::Float64 => unsafe { JS_NewFloat32ArrayWithBuffer(*cx, heap.handle(), byte_offset, byte_length)
JS_NewFloat64ArrayWithBuffer(*cx, buffer, byte_offset, byte_length) },
}, Type::Float64 => {
Type::Uint8Clamped => unsafe { JS_NewFloat64ArrayWithBuffer(*cx, heap.handle(), byte_offset, byte_length)
JS_NewUint8ClampedArrayWithBuffer(*cx, buffer, byte_offset, byte_length) },
}, Type::Uint8Clamped => JS_NewUint8ClampedArrayWithBuffer(
Type::BigInt64 => unsafe { *cx,
JS_NewBigInt64ArrayWithBuffer(*cx, buffer, byte_offset, byte_length) heap.handle(),
}, byte_offset,
Type::BigUint64 => unsafe { byte_length,
JS_NewBigUint64ArrayWithBuffer(*cx, buffer, byte_offset, byte_length) ),
}, Type::BigInt64 => {
Type::Float16 => unsafe { JS_NewBigInt64ArrayWithBuffer(*cx, heap.handle(), byte_offset, byte_length)
JS_NewFloat16ArrayWithBuffer(*cx, buffer, byte_offset, byte_length) },
}, Type::BigUint64 => {
Type::Int64 | Type::Simd128 | Type::MaxTypedArrayViewType => { JS_NewBigUint64ArrayWithBuffer(*cx, heap.handle(), byte_offset, byte_length)
unreachable!("Invalid TypedArray type") },
}, Type::Float16 => {
}; JS_NewFloat16ArrayWithBuffer(*cx, heap.handle(), byte_offset, byte_length)
},
Type::Int64 | Type::Simd128 | Type::MaxTypedArrayViewType => {
unreachable!("Invalid TypedArray type")
},
}
};
Ok(HeapBufferSource::new(BufferSource::ArrayBufferView( Ok(HeapBufferSource::new(BufferSource::ArrayBufferView(
Heap::boxed(array_view), Heap::boxed(array_view),
))) )))
},
BufferSource::ArrayBufferView(_) => {
unreachable!("Can not create a new ArrayBufferView from an existing ArrayBufferView");
},
}
} }
pub(crate) fn create_array_buffer_with_size( pub(crate) fn create_array_buffer_with_size(