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

View file

@ -9,23 +9,13 @@ use std::ptr;
use js::jsapi::{Heap, JSObject, JS_GetArrayBufferViewBuffer}; use js::jsapi::{Heap, JSObject, JS_GetArrayBufferViewBuffer};
use js::rust::wrappers::DetachArrayBuffer; use js::rust::wrappers::DetachArrayBuffer;
use js::rust::{CustomAutoRooterGuard, MutableHandleObject}; use js::rust::{CustomAutoRooterGuard, MutableHandleObject};
use js::typedarray::{CreateWith, Float32Array}; use js::typedarray::{
CreateWith, Float32Array, JSObjectStorage, TypedArray, TypedArrayElement,
TypedArrayElementCreator,
};
use crate::script_runtime::JSContext; 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)] #[derive(Default, JSTraceable)]
pub struct HeapFloat32Array { pub struct HeapFloat32Array {
internal: Box<Heap<*mut JSObject>>, internal: Box<Heap<*mut JSObject>>,
@ -34,7 +24,7 @@ pub struct HeapFloat32Array {
impl HeapFloat32Array { impl HeapFloat32Array {
pub fn set_data(&self, cx: JSContext, data: &[f32]) -> Result<(), ()> { pub fn set_data(&self, cx: JSContext, data: &[f32]) -> Result<(), ()> {
rooted!(in (*cx) let mut array = ptr::null_mut::<JSObject>()); 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); self.internal.set(*array);
Ok(()) 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())
}
}

View file

@ -25,7 +25,7 @@ use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::inheritance::Castable; use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::{reflect_dom_object_with_proto, DomObject, Reflector}; use crate::dom::bindings::reflector::{reflect_dom_object_with_proto, DomObject, Reflector};
use crate::dom::bindings::root::DomRoot; use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::typedarrays::create_float32_array; use crate::dom::bindings::typedarrays::create_typed_array;
use crate::dom::dommatrix::DOMMatrix; use crate::dom::dommatrix::DOMMatrix;
use crate::dom::dompoint::DOMPoint; use crate::dom::dompoint::DOMPoint;
use crate::dom::globalscope::GlobalScope; use crate::dom::globalscope::GlobalScope;
@ -686,7 +686,7 @@ impl DOMMatrixReadOnlyMethods for DOMMatrixReadOnly {
.map(|&x| x as f32) .map(|&x| x as f32)
.collect(); .collect();
rooted!(in (*cx) let mut array = ptr::null_mut::<JSObject>()); rooted!(in (*cx) let mut array = ptr::null_mut::<JSObject>());
create_float32_array(cx, &vec, array.handle_mut()) create_typed_array(cx, &vec, array.handle_mut())
.expect("Converting matrix to float32 array should never fail") .expect("Converting matrix to float32 array should never fail")
} }

View file

@ -3,18 +3,18 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::ptr; use std::ptr;
use std::ptr::NonNull;
use dom_struct::dom_struct; use dom_struct::dom_struct;
use js::jsapi::JSObject; use js::jsapi::JSObject;
use js::rust::HandleObject; use js::rust::HandleObject;
use js::typedarray::{CreateWith, Uint8Array}; use js::typedarray::Uint8Array;
use crate::dom::bindings::codegen::Bindings::TextEncoderBinding::TextEncoderMethods; use crate::dom::bindings::codegen::Bindings::TextEncoderBinding::TextEncoderMethods;
use crate::dom::bindings::error::Fallible; use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::reflector::{reflect_dom_object_with_proto, Reflector}; use crate::dom::bindings::reflector::{reflect_dom_object_with_proto, Reflector};
use crate::dom::bindings::root::DomRoot; use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::{DOMString, USVString}; use crate::dom::bindings::str::{DOMString, USVString};
use crate::dom::bindings::typedarrays::create_typed_array;
use crate::dom::globalscope::GlobalScope; use crate::dom::globalscope::GlobalScope;
use crate::script_runtime::JSContext; use crate::script_runtime::JSContext;
@ -50,19 +50,12 @@ impl TextEncoderMethods for TextEncoder {
DOMString::from("utf-8") DOMString::from("utf-8")
} }
#[allow(unsafe_code)]
// https://encoding.spec.whatwg.org/#dom-textencoder-encode // https://encoding.spec.whatwg.org/#dom-textencoder-encode
fn Encode(&self, cx: JSContext, input: USVString) -> NonNull<JSObject> { fn Encode(&self, cx: JSContext, input: USVString) -> Uint8Array {
let encoded = input.0.as_bytes(); let encoded = input.0.as_bytes();
unsafe {
rooted!(in(*cx) let mut js_object = ptr::null_mut::<JSObject>()); rooted!(in(*cx) let mut js_object = ptr::null_mut::<JSObject>());
assert!( create_typed_array(cx, &encoded, js_object.handle_mut())
Uint8Array::create(*cx, CreateWith::Slice(&encoded), js_object.handle_mut()) .expect("Converting input to uint8 array should never fail")
.is_ok()
);
NonNull::new_unchecked(js_object.get())
}
} }
} }

View file

@ -2402,6 +2402,7 @@ class IDLType(IDLObject):
# Funny stuff # Funny stuff
"interface", "interface",
"float32array", "float32array",
"uint8array",
"dictionary", "dictionary",
"enum", "enum",
"callback", "callback",
@ -3635,7 +3636,7 @@ class IDLBuiltinType(IDLType):
Types.ArrayBuffer: IDLType.Tags.interface, Types.ArrayBuffer: IDLType.Tags.interface,
Types.ArrayBufferView: IDLType.Tags.interface, Types.ArrayBufferView: IDLType.Tags.interface,
Types.Int8Array: IDLType.Tags.interface, Types.Int8Array: IDLType.Tags.interface,
Types.Uint8Array: IDLType.Tags.interface, Types.Uint8Array: IDLType.Tags.uint8array,
Types.Uint8ClampedArray: IDLType.Tags.interface, Types.Uint8ClampedArray: IDLType.Tags.interface,
Types.Int16Array: IDLType.Tags.interface, Types.Int16Array: IDLType.Tags.interface,
Types.Uint16Array: IDLType.Tags.interface, Types.Uint16Array: IDLType.Tags.interface,