mirror of
https://github.com/servo/servo.git
synced 2025-06-06 16:45:39 +00:00
Use Uint8Array for TextEncoder (#31145)
Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com>
This commit is contained in:
parent
7de0486e2e
commit
890588945d
5 changed files with 36 additions and 32 deletions
|
@ -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',
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
create_typed_array(cx, &encoded, js_object.handle_mut())
|
||||
.expect("Converting input to uint8 array should never fail")
|
||||
}
|
||||
}
|
||||
|
|
3
third_party/WebIDL/WebIDL.py
vendored
3
third_party/WebIDL/WebIDL.py
vendored
|
@ -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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue