mirror of
https://github.com/servo/servo.git
synced 2025-08-07 14:35:33 +01:00
WebIDL: Use Uint8ClampedArray instead of raw JSObject in bindings (#31317)
* WebIDL: Use Uint8ClampedArray instead of raw JSObject in bindings Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com> * fmt Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com> * introduce new_initialized_heap_typed_array function Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com> * Remove unsed unsafe_code Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com> * Use doc comments for ImageData Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com> * Use get_internal instead of acquire_data Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com> * Handle JS errors in ImageData GetData and new_initialized_heap_typed_array Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com> * Fix wrong assert that causes CRASH in test Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com> * Early return for error Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com> * Address review comments Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com> --------- Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com>
This commit is contained in:
parent
7e9be5ae9f
commit
328c376ff1
6 changed files with 118 additions and 55 deletions
|
@ -130,6 +130,7 @@ builtinNames = {
|
|||
IDLType.Tags.float64array: 'Float64Array',
|
||||
IDLType.Tags.arrayBuffer: 'ArrayBuffer',
|
||||
IDLType.Tags.arrayBufferView: 'ArrayBufferView',
|
||||
IDLType.Tags.uint8clampedarray: 'Uint8ClampedArray',
|
||||
}
|
||||
|
||||
numericTags = [
|
||||
|
@ -6520,6 +6521,7 @@ def generate_imports(config, cgthings, descriptors, callbacks=None, dictionaries
|
|||
'js::typedarray::Float64Array',
|
||||
'js::typedarray::ArrayBuffer',
|
||||
'js::typedarray::ArrayBufferView',
|
||||
'js::typedarray::Uint8ClampedArray',
|
||||
'crate::dom',
|
||||
'crate::dom::bindings',
|
||||
'crate::dom::bindings::codegen::InterfaceObjectMap',
|
||||
|
|
|
@ -31,6 +31,38 @@ unsafe impl<T> crate::dom::bindings::trace::JSTraceable for HeapTypedArray<T> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn new_initialized_heap_typed_array<T>(
|
||||
init: HeapTypedArrayInit,
|
||||
) -> Result<HeapTypedArray<T>, ()>
|
||||
where
|
||||
T: TypedArrayElement + TypedArrayElementCreator,
|
||||
T::Element: Clone + Copy,
|
||||
{
|
||||
let heap_typed_array = match init {
|
||||
HeapTypedArrayInit::Object(js_object) => HeapTypedArray {
|
||||
internal: Heap::boxed(js_object),
|
||||
phantom: PhantomData::default(),
|
||||
},
|
||||
HeapTypedArrayInit::Info { len, cx } => {
|
||||
rooted!(in (*cx) let mut array = ptr::null_mut::<JSObject>());
|
||||
let typed_array_result =
|
||||
create_typed_array_with_length::<T>(cx, len as usize, array.handle_mut());
|
||||
if typed_array_result.is_err() {
|
||||
return Err(());
|
||||
}
|
||||
let heap_typed_array = HeapTypedArray::<T>::default();
|
||||
heap_typed_array.internal.set(*array);
|
||||
heap_typed_array
|
||||
},
|
||||
};
|
||||
Ok(heap_typed_array)
|
||||
}
|
||||
|
||||
pub enum HeapTypedArrayInit {
|
||||
Object(*mut JSObject),
|
||||
Info { len: u32, cx: JSContext },
|
||||
}
|
||||
|
||||
impl<T> HeapTypedArray<T>
|
||||
where
|
||||
T: TypedArrayElement + TypedArrayElementCreator,
|
||||
|
@ -161,6 +193,23 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
fn create_typed_array_with_length<T>(
|
||||
cx: JSContext,
|
||||
len: usize,
|
||||
dest: MutableHandleObject,
|
||||
) -> Result<TypedArray<T, *mut JSObject>, ()>
|
||||
where
|
||||
T: TypedArrayElement + TypedArrayElementCreator,
|
||||
{
|
||||
let res = unsafe { TypedArray::<T, *mut JSObject>::create(*cx, CreateWith::Length(len), dest) };
|
||||
|
||||
if res.is_err() {
|
||||
Err(())
|
||||
} else {
|
||||
TypedArray::from(dest.get())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_new_external_array_buffer<T>(
|
||||
cx: JSContext,
|
||||
mapping: Arc<Mutex<Vec<T::Element>>>,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue