mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
WebIDL: use FLoat32Array (#30990)
* inital * audiobuffer: return float 32 array as channel data * add on heap float 32 array type * fix warnings * add list of webidl interfaces to ignore for float 32 * codegen: remove duplication of builtin return type handling * bindings: derive default for float 32 array wrapper * bindings: allow unsafe code in typedarrays module * bindings: rename float 32 array wrapper * bindings: rename HeapFloat32Array is_set method to is_initialized * bindings: assert float 32 array is initialized before data can be acquired * bindings: use let syntax for error handling in float 32 array wrapper * bindings: use copy_from_slice where possible in float 32 array wrapper * bindings: rename args in typedarray copy methods * codegen: use idl type in builtin names for float 32 array * bindings: add a util to create float 32 arrays, use in dom matrix readonly * codegen: tidy * bindings: box the heap inside heaped float 32 arrays
This commit is contained in:
parent
90f70e3408
commit
e145c51234
6 changed files with 159 additions and 87 deletions
|
@ -119,7 +119,8 @@ builtinNames = {
|
|||
IDLType.Tags.unrestricted_float: 'f32',
|
||||
IDLType.Tags.float: 'Finite<f32>',
|
||||
IDLType.Tags.unrestricted_double: 'f64',
|
||||
IDLType.Tags.double: 'Finite<f64>'
|
||||
IDLType.Tags.double: 'Finite<f64>',
|
||||
IDLType.Tags.float32array: 'Float32Array'
|
||||
}
|
||||
|
||||
numericTags = [
|
||||
|
@ -1459,16 +1460,26 @@ def getConversionConfigForType(type, isEnforceRange, isClamp, treatNullAs):
|
|||
return "()"
|
||||
|
||||
|
||||
def todo_switch_float_32(des):
|
||||
return des.interface.identifier.name in ['XRView', 'XRRigidTransform', 'XRRay', 'GamepadPose']
|
||||
|
||||
|
||||
def builtin_return_type(returnType):
|
||||
result = CGGeneric(builtinNames[returnType.tag()])
|
||||
if returnType.nullable():
|
||||
result = CGWrapper(result, pre="Option<", post=">")
|
||||
return result
|
||||
|
||||
|
||||
# Returns a CGThing containing the type of the return value.
|
||||
def getRetvalDeclarationForType(returnType, descriptorProvider):
|
||||
if returnType is None or returnType.isUndefined():
|
||||
# Nothing to declare
|
||||
return CGGeneric("()")
|
||||
if returnType.isPrimitive() and returnType.tag() in builtinNames:
|
||||
result = CGGeneric(builtinNames[returnType.tag()])
|
||||
if returnType.nullable():
|
||||
result = CGWrapper(result, pre="Option<", post=">")
|
||||
return result
|
||||
return builtin_return_type(returnType)
|
||||
if returnType.isTypedArray() and returnType.tag() in builtinNames and not todo_switch_float_32(descriptorProvider):
|
||||
return builtin_return_type(returnType)
|
||||
if returnType.isDOMString():
|
||||
result = CGGeneric("DOMString")
|
||||
if returnType.nullable():
|
||||
|
@ -6494,6 +6505,7 @@ def generate_imports(config, cgthings, descriptors, callbacks=None, dictionaries
|
|||
'js::rust::define_properties',
|
||||
'js::rust::get_object_class',
|
||||
'js::typedarray',
|
||||
'js::typedarray::Float32Array',
|
||||
'crate::dom',
|
||||
'crate::dom::bindings',
|
||||
'crate::dom::bindings::codegen::InterfaceObjectMap',
|
||||
|
|
|
@ -158,6 +158,7 @@ pub mod str;
|
|||
pub mod structuredclone;
|
||||
pub mod trace;
|
||||
pub mod transferable;
|
||||
pub mod typedarrays;
|
||||
pub mod utils;
|
||||
pub mod weakref;
|
||||
pub mod xmlname;
|
||||
|
|
105
components/script/dom/bindings/typedarrays.rs
Normal file
105
components/script/dom/bindings/typedarrays.rs
Normal file
|
@ -0,0 +1,105 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#![allow(unsafe_code)]
|
||||
|
||||
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 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>>,
|
||||
}
|
||||
|
||||
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())?;
|
||||
self.internal.set(*array);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn acquire_data(&self, cx: JSContext) -> Result<Vec<f32>, ()> {
|
||||
assert!(self.is_initialized());
|
||||
typedarray!(in(*cx) let array: Float32Array = self.internal.get());
|
||||
let data = if let Ok(array) = array {
|
||||
let data = array.to_vec();
|
||||
let mut is_shared = false;
|
||||
unsafe {
|
||||
rooted!(in (*cx) let view_buffer =
|
||||
JS_GetArrayBufferViewBuffer(*cx, self.internal.handle(), &mut is_shared));
|
||||
// This buffer is always created unshared
|
||||
debug_assert!(!is_shared);
|
||||
let _ = DetachArrayBuffer(*cx, view_buffer.handle());
|
||||
}
|
||||
Ok(data)
|
||||
} else {
|
||||
Err(())
|
||||
};
|
||||
self.internal.set(ptr::null_mut());
|
||||
data
|
||||
}
|
||||
|
||||
pub fn copy_data_to(
|
||||
&self,
|
||||
cx: JSContext,
|
||||
dest: &mut [f32],
|
||||
source_start: usize,
|
||||
length: usize,
|
||||
) -> Result<(), ()> {
|
||||
assert!(self.is_initialized());
|
||||
typedarray!(in(*cx) let array: Float32Array = self.internal.get());
|
||||
let Ok(array) = array else { return Err(()) };
|
||||
unsafe {
|
||||
let slice = (*array).as_slice();
|
||||
dest.copy_from_slice(&slice[source_start..length]);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn copy_data_from(
|
||||
&self,
|
||||
cx: JSContext,
|
||||
source: CustomAutoRooterGuard<Float32Array>,
|
||||
dest_start: usize,
|
||||
length: usize,
|
||||
) -> Result<(), ()> {
|
||||
assert!(self.is_initialized());
|
||||
typedarray!(in(*cx) let mut array: Float32Array = self.internal.get());
|
||||
let Ok(mut array) = array else { return Err(()) };
|
||||
unsafe {
|
||||
let slice = (*array).as_mut_slice();
|
||||
let (_, dest) = slice.split_at_mut(dest_start);
|
||||
dest[0..length].copy_from_slice(&source.as_slice()[0..length])
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn is_initialized(&self) -> bool {
|
||||
!self.internal.get().is_null()
|
||||
}
|
||||
|
||||
pub fn get_internal(&self) -> Result<Float32Array, ()> {
|
||||
Float32Array::from(self.internal.get())
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue