Use FLoat32Array in GamepadPose (#31106)

* Use FLoat32Array in GamepadPose

Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com>

* Remove unused create_typed_array

Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com>

---------

Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com>
This commit is contained in:
Taym Haddadi 2024-01-19 05:39:09 +01:00 committed by GitHub
parent 9a698b7bfb
commit 9d2c102fa0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 38 additions and 84 deletions

View file

@ -1460,10 +1460,6 @@ def getConversionConfigForType(type, isEnforceRange, isClamp, treatNullAs):
return "()"
def todo_switch_float_32(des):
return des.interface.identifier.name in ['GamepadPose']
def builtin_return_type(returnType):
result = CGGeneric(builtinNames[returnType.tag()])
if returnType.nullable():
@ -1478,7 +1474,7 @@ def getRetvalDeclarationForType(returnType, descriptorProvider):
return CGGeneric("()")
if returnType.isPrimitive() and returnType.tag() in builtinNames:
return builtin_return_type(returnType)
if returnType.isTypedArray() and returnType.tag() in builtinNames and not todo_switch_float_32(descriptorProvider):
if returnType.isTypedArray() and returnType.tag() in builtinNames:
return builtin_return_type(returnType)
if returnType.isDOMString():
result = CGGeneric("DOMString")

View file

@ -102,4 +102,13 @@ impl HeapFloat32Array {
pub fn get_internal(&self) -> Result<Float32Array, ()> {
Float32Array::from(self.internal.get())
}
pub fn internal_to_option(&self) -> Option<Float32Array> {
if self.is_initialized() {
Some(self.get_internal().expect("Failed to get internal."))
} else {
warn!("Internal not initialized.");
None
}
}
}

View file

@ -30,7 +30,6 @@ use js::rust::{
get_object_class, is_dom_class, GCMethods, Handle, HandleId, HandleObject, HandleValue,
MutableHandleValue, ToString,
};
use js::typedarray::{CreateWith, Float32Array};
use js::JS_CALLEE;
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
@ -133,15 +132,6 @@ pub fn to_frozen_array<T: ToJSValConvertible>(convertibles: &[T], cx: SafeJSCont
*ports
}
/// Creates a Float32 array
pub fn create_typed_array(cx: SafeJSContext, src: &[f32], dst: &Heap<*mut JSObject>) {
rooted!(in (*cx) let mut array = ptr::null_mut::<JSObject>());
unsafe {
let _ = Float32Array::create(*cx, CreateWith::Slice(src), array.handle_mut());
}
(*dst).set(array.get());
}
/// Returns the ProtoOrIfaceArray for the given global object.
/// Fails if `global` is not a DOM global object.
pub fn get_proto_or_iface_array(global: *mut JSObject) -> *mut ProtoOrIfaceArray {

View file

@ -2,13 +2,10 @@
* 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/. */
use std::ptr;
use std::ptr::NonNull;
use dom_struct::dom_struct;
use js::jsapi::{Heap, JSObject};
use js::typedarray::{CreateWith, Float32Array};
use js::typedarray::Float32Array;
use super::bindings::typedarrays::HeapFloat32Array;
use crate::dom::bindings::codegen::Bindings::GamepadPoseBinding::GamepadPoseMethods;
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::root::DomRoot;
@ -19,55 +16,17 @@ use crate::script_runtime::JSContext;
pub struct GamepadPose {
reflector_: Reflector,
#[ignore_malloc_size_of = "mozjs"]
position: Heap<*mut JSObject>,
position: HeapFloat32Array,
#[ignore_malloc_size_of = "mozjs"]
orientation: Heap<*mut JSObject>,
orientation: HeapFloat32Array,
#[ignore_malloc_size_of = "mozjs"]
linear_vel: Heap<*mut JSObject>,
linear_vel: HeapFloat32Array,
#[ignore_malloc_size_of = "mozjs"]
angular_vel: Heap<*mut JSObject>,
angular_vel: HeapFloat32Array,
#[ignore_malloc_size_of = "mozjs"]
linear_acc: Heap<*mut JSObject>,
linear_acc: HeapFloat32Array,
#[ignore_malloc_size_of = "mozjs"]
angular_acc: Heap<*mut JSObject>,
}
// TODO: support gamepad discovery
#[allow(dead_code)]
#[allow(unsafe_code)]
fn update_or_create_typed_array(cx: JSContext, src: Option<&[f32]>, dst: &Heap<*mut JSObject>) {
match src {
Some(data) => {
if dst.get().is_null() {
rooted!(in (*cx) let mut array = ptr::null_mut::<JSObject>());
let _ = unsafe {
Float32Array::create(*cx, CreateWith::Slice(data), array.handle_mut())
};
(*dst).set(array.get());
} else {
typedarray!(in(*cx) let array: Float32Array = dst.get());
if let Ok(mut array) = array {
unsafe { array.update(data) };
}
}
},
None => {
if !dst.get().is_null() {
dst.set(ptr::null_mut());
}
},
}
}
#[inline]
#[allow(unsafe_code)]
fn heap_to_option(heap: &Heap<*mut JSObject>) -> Option<NonNull<JSObject>> {
let js_object = heap.get();
if js_object.is_null() {
None
} else {
unsafe { Some(NonNull::new_unchecked(js_object)) }
}
angular_acc: HeapFloat32Array,
}
// TODO: support gamepad discovery
@ -76,12 +35,12 @@ impl GamepadPose {
fn new_inherited() -> GamepadPose {
GamepadPose {
reflector_: Reflector::new(),
position: Heap::default(),
orientation: Heap::default(),
linear_vel: Heap::default(),
angular_vel: Heap::default(),
linear_acc: Heap::default(),
angular_acc: Heap::default(),
position: HeapFloat32Array::default(),
orientation: HeapFloat32Array::default(),
linear_vel: HeapFloat32Array::default(),
angular_vel: HeapFloat32Array::default(),
linear_acc: HeapFloat32Array::default(),
angular_acc: HeapFloat32Array::default(),
}
}
@ -92,42 +51,42 @@ impl GamepadPose {
impl GamepadPoseMethods for GamepadPose {
// https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-position
fn GetPosition(&self, _cx: JSContext) -> Option<NonNull<JSObject>> {
heap_to_option(&self.position)
fn GetPosition(&self, _cx: JSContext) -> Option<Float32Array> {
self.position.internal_to_option()
}
// https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-hasposition
fn HasPosition(&self) -> bool {
!self.position.get().is_null()
self.position.is_initialized()
}
// https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-linearvelocity
fn GetLinearVelocity(&self, _cx: JSContext) -> Option<NonNull<JSObject>> {
heap_to_option(&self.linear_vel)
fn GetLinearVelocity(&self, _cx: JSContext) -> Option<Float32Array> {
self.linear_vel.internal_to_option()
}
// https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-linearacceleration
fn GetLinearAcceleration(&self, _cx: JSContext) -> Option<NonNull<JSObject>> {
heap_to_option(&self.linear_acc)
fn GetLinearAcceleration(&self, _cx: JSContext) -> Option<Float32Array> {
self.linear_acc.internal_to_option()
}
// https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-orientation
fn GetOrientation(&self, _cx: JSContext) -> Option<NonNull<JSObject>> {
heap_to_option(&self.orientation)
fn GetOrientation(&self, _cx: JSContext) -> Option<Float32Array> {
self.orientation.internal_to_option()
}
// https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-orientation
fn HasOrientation(&self) -> bool {
!self.orientation.get().is_null()
self.orientation.is_initialized()
}
// https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-angularvelocity
fn GetAngularVelocity(&self, _cx: JSContext) -> Option<NonNull<JSObject>> {
heap_to_option(&self.angular_vel)
fn GetAngularVelocity(&self, _cx: JSContext) -> Option<Float32Array> {
self.angular_vel.internal_to_option()
}
// https://w3c.github.io/gamepad/extensions.html#dom-gamepadpose-angularacceleration
fn GetAngularAcceleration(&self, _cx: JSContext) -> Option<NonNull<JSObject>> {
heap_to_option(&self.angular_acc)
fn GetAngularAcceleration(&self, _cx: JSContext) -> Option<Float32Array> {
self.angular_acc.internal_to_option()
}
}