Skip to content

Commit

Permalink
Use Uint8Array for TextEncoder
Browse files Browse the repository at this point in the history
Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com>
  • Loading branch information
Taym95 committed Jan 23, 2024
1 parent bd25c07 commit 611d253
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 32 deletions.
4 changes: 3 additions & 1 deletion components/script/dom/bindings/codegen/CodegenRust.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ def wrapInNativeContainerType(type, inner):
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 = [
Expand Down Expand Up @@ -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',
Expand Down
38 changes: 23 additions & 15 deletions components/script/dom/bindings/typedarrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>,
Expand All @@ -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(())
}
Expand Down Expand Up @@ -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())
}
}
4 changes: 2 additions & 2 deletions components/script/dom/dommatrixreadonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::{reflect_dom_object_with_proto, DomObject, Reflector};
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::dompoint::DOMPoint;
use crate::dom::globalscope::GlobalScope;
Expand Down Expand Up @@ -686,7 +686,7 @@ impl DOMMatrixReadOnlyMethods for DOMMatrixReadOnly {
.map(|&x| x as f32)
.collect();
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")
}

Expand Down
19 changes: 6 additions & 13 deletions components/script/dom/textencoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use std::ptr;
use std::ptr::NonNull;

use dom_struct::dom_struct;
use js::jsapi::JSObject;
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::error::Fallible;
use crate::dom::bindings::reflector::{reflect_dom_object_with_proto, Reflector};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::{DOMString, USVString};
use crate::dom::bindings::typedarrays::create_typed_array;
use crate::dom::globalscope::GlobalScope;
use crate::script_runtime::JSContext;

Expand Down Expand Up @@ -50,19 +50,12 @@ impl TextEncoderMethods for TextEncoder {
DOMString::from("utf-8")
}

#[allow(unsafe_code)]
// 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();

unsafe {
rooted!(in(*cx) let mut js_object = ptr::null_mut::<JSObject>());
assert!(
Uint8Array::create(*cx, CreateWith::Slice(&encoded), js_object.handle_mut())
.is_ok()
);

NonNull::new_unchecked(js_object.get())
}
rooted!(in(*cx) let mut js_object = ptr::null_mut::<JSObject>());
create_typed_array(cx, &encoded, js_object.handle_mut())
.expect("Converting input to uint8 array should never fail")
}
}
3 changes: 2 additions & 1 deletion third_party/WebIDL/WebIDL.py
Original file line number Diff line number Diff line change
Expand Up @@ -2402,6 +2402,7 @@ class IDLType(IDLObject):
# Funny stuff
"interface",
"float32array",
"uint8array",
"dictionary",
"enum",
"callback",
Expand Down Expand Up @@ -3635,7 +3636,7 @@ class IDLBuiltinType(IDLType):
Types.ArrayBuffer: IDLType.Tags.interface,
Types.ArrayBufferView: IDLType.Tags.interface,
Types.Int8Array: IDLType.Tags.interface,
Types.Uint8Array: IDLType.Tags.interface,
Types.Uint8Array: IDLType.Tags.uint8array,
Types.Uint8ClampedArray: IDLType.Tags.interface,
Types.Int16Array: IDLType.Tags.interface,
Types.Uint16Array: IDLType.Tags.interface,
Expand Down

0 comments on commit 611d253

Please sign in to comment.