WebIDL Fix ImageData constructor to take a Uint8ClampedArray instead of js_object (#31398)

<!-- Please describe your changes on the following line: -->

Fix ImageData constructor to take a Uint8ClampedArray instead of
js_object

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by
`[X]` when the step is complete, and replace `___` with appropriate
data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix #31320

<!-- Either: -->
- [x] These changes do not require tests because there is not behavior
change.

<!-- Also, please make sure that "Allow edits from maintainers" checkbox
is checked, so that we can help you if you get stuck somewhere along the
way.-->

<!-- Pull requests that do not address these steps are welcome, but they
will require additional verification as part of the review process. -->

---------

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>
This commit is contained in:
Taym Haddadi 2025-05-09 13:05:56 +02:00 committed by GitHub
parent 53be79a5b5
commit e5347eceac
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 42 additions and 61 deletions

View file

@ -37,7 +37,7 @@ use js::rust::{
#[cfg(feature = "webgpu")]
use js::typedarray::{ArrayBuffer, HeapArrayBuffer};
use js::typedarray::{
ArrayBufferU8, ArrayBufferView, ArrayBufferViewU8, CreateWith, TypedArray, TypedArrayElement,
ArrayBufferU8, ArrayBufferViewU8, CreateWith, TypedArray, TypedArrayElement,
TypedArrayElementCreator,
};
@ -63,36 +63,25 @@ pub(crate) enum BufferSource {
ArrayBuffer(Box<Heap<*mut JSObject>>),
}
pub(crate) fn new_initialized_heap_buffer_source<T>(
init: HeapTypedArrayInit,
pub(crate) fn create_heap_buffer_source_with_length<T>(
cx: JSContext,
len: u32,
can_gc: CanGc,
) -> Result<HeapBufferSource<T>, ()>
) -> Fallible<HeapBufferSource<T>>
where
T: TypedArrayElement + TypedArrayElementCreator,
T::Element: Clone + Copy,
{
let heap_buffer_source = match init {
HeapTypedArrayInit::Buffer(buffer_source) => HeapBufferSource {
buffer_source,
phantom: PhantomData,
},
HeapTypedArrayInit::Info { len, cx } => {
rooted!(in (*cx) let mut array = ptr::null_mut::<JSObject>());
let typed_array_result =
create_buffer_source_with_length::<T>(cx, len as usize, array.handle_mut(), can_gc);
if typed_array_result.is_err() {
return Err(());
}
rooted!(in (*cx) let mut array = ptr::null_mut::<JSObject>());
let typed_array_result =
create_buffer_source_with_length::<T>(cx, len as usize, array.handle_mut(), can_gc);
if typed_array_result.is_err() {
return Err(Error::JSFailed);
}
HeapBufferSource::<T>::new(BufferSource::ArrayBufferView(Heap::boxed(*array.handle())))
},
};
Ok(heap_buffer_source)
}
pub(crate) enum HeapTypedArrayInit {
Buffer(BufferSource),
Info { len: u32, cx: JSContext },
Ok(HeapBufferSource::<T>::new(BufferSource::ArrayBufferView(
Heap::boxed(*array.handle()),
)))
}
pub(crate) struct HeapBufferSource<T> {
@ -131,11 +120,11 @@ where
}
pub(crate) fn from_view(
chunk: CustomAutoRooterGuard<ArrayBufferView>,
) -> HeapBufferSource<ArrayBufferViewU8> {
HeapBufferSource::<ArrayBufferViewU8>::new(BufferSource::ArrayBufferView(Heap::boxed(
unsafe { *chunk.underlying_object() },
)))
chunk: CustomAutoRooterGuard<TypedArray<T, *mut JSObject>>,
) -> HeapBufferSource<T> {
HeapBufferSource::<T>::new(BufferSource::ArrayBufferView(Heap::boxed(unsafe {
*chunk.underlying_object()
})))
}
pub(crate) fn default() -> Self {