diff options
-rw-r--r-- | components/script/dom/bindings/codegen/CodegenRust.py | 4 | ||||
-rw-r--r-- | components/script/dom/bindings/typedarrays.rs | 38 | ||||
-rw-r--r-- | components/script/dom/dommatrixreadonly.rs | 4 | ||||
-rw-r--r-- | components/script/dom/textencoder.rs | 19 | ||||
-rw-r--r-- | third_party/WebIDL/WebIDL.py | 3 |
5 files changed, 36 insertions, 32 deletions
diff --git a/components/script/dom/bindings/codegen/CodegenRust.py b/components/script/dom/bindings/codegen/CodegenRust.py index 2fdc879e62e..10ccc0ef898 100644 --- a/components/script/dom/bindings/codegen/CodegenRust.py +++ b/components/script/dom/bindings/codegen/CodegenRust.py @@ -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', diff --git a/components/script/dom/bindings/typedarrays.rs b/components/script/dom/bindings/typedarrays.rs index 4fa675942f3..ddda71911bf 100644 --- a/components/script/dom/bindings/typedarrays.rs +++ b/components/script/dom/bindings/typedarrays.rs @@ -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()) + } +} diff --git a/components/script/dom/dommatrixreadonly.rs b/components/script/dom/dommatrixreadonly.rs index 9e3bd868a5f..d859137a546 100644 --- a/components/script/dom/dommatrixreadonly.rs +++ b/components/script/dom/dommatrixreadonly.rs @@ -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; @@ -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") } diff --git a/components/script/dom/textencoder.rs b/components/script/dom/textencoder.rs index f81679fe298..20c9116f04b 100644 --- a/components/script/dom/textencoder.rs +++ b/components/script/dom/textencoder.rs @@ -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; @@ -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") } } diff --git a/third_party/WebIDL/WebIDL.py b/third_party/WebIDL/WebIDL.py index 20b47d3ad71..6afdfc33959 100644 --- a/third_party/WebIDL/WebIDL.py +++ b/third_party/WebIDL/WebIDL.py @@ -2402,6 +2402,7 @@ class IDLType(IDLObject): # Funny stuff "interface", "float32array", + "uint8array", "dictionary", "enum", "callback", @@ -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, |