Use Uint8Array for TextEncoder (#31145)

Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com>
This commit is contained in:
Taym Haddadi 2024-01-23 13:15:42 +01:00 committed by GitHub
parent 7de0486e2e
commit 890588945d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 36 additions and 32 deletions

View file

@ -120,7 +120,8 @@ builtinNames = {
IDLType.Tags.float: 'Finite<f32>',
IDLType.Tags.unrestricted_double: 'f64',
IDLType.Tags.double: 'Finite<f64>',
IDLType.Tags.float32array: 'Float32Array'
IDLType.Tags.float32array: 'Float32Array',
IDLType.Tags.uint8array: 'Uint8Array'
}
numericTags = [
@ -6502,6 +6503,7 @@ def generate_imports(config, cgthings, descriptors, callbacks=None, dictionaries
'js::rust::get_object_class',
'js::typedarray',
'js::typedarray::Float32Array',
'js::typedarray::Uint8Array',
'crate::dom',
'crate::dom::bindings',
'crate::dom::bindings::codegen::InterfaceObjectMap',

View file

@ -9,23 +9,13 @@ use std::ptr;
use js::jsapi::{Heap, JSObject, JS_GetArrayBufferViewBuffer};
use js::rust::wrappers::DetachArrayBuffer;
use js::rust::{CustomAutoRooterGuard, MutableHandleObject};
use js::typedarray::{CreateWith, Float32Array};
use js::typedarray::{
CreateWith, Float32Array, JSObjectStorage, TypedArray, TypedArrayElement,
TypedArrayElementCreator,
};
use crate::script_runtime::JSContext;
pub fn create_float32_array(
cx: JSContext,
data: &[f32],
dest: MutableHandleObject,
) -> Result<Float32Array, ()> {
let res = unsafe { Float32Array::create(*cx, CreateWith::Slice(data), dest) };
if res.is_err() {
Err(())
} else {
Float32Array::from(dest.get())
}
}
#[derive(Default, JSTraceable)]
pub struct HeapFloat32Array {
internal: Box<Heap<*mut JSObject>>,
@ -34,7 +24,7 @@ pub struct HeapFloat32Array {
impl HeapFloat32Array {
pub fn set_data(&self, cx: JSContext, data: &[f32]) -> Result<(), ()> {
rooted!(in (*cx) let mut array = ptr::null_mut::<JSObject>());
let _ = create_float32_array(cx, data, array.handle_mut())?;
let _: Float32Array = create_typed_array(cx, data, array.handle_mut())?;
self.internal.set(*array);
Ok(())
}
@ -112,3 +102,21 @@ impl HeapFloat32Array {
}
}
}
pub fn create_typed_array<T, S>(
cx: JSContext,
data: &[T::Element],
dest: MutableHandleObject,
) -> Result<TypedArray<T, S>, ()>
where
T: TypedArrayElementCreator + TypedArrayElement,
S: JSObjectStorage,
{
let res = unsafe { TypedArray::<T, S>::create(*cx, CreateWith::Slice(data), dest) };
if res.is_err() {
Err(())
} else {
TypedArray::from(dest.get())
}
}