diff options
author | Taym Haddadi <haddadi.taym@gmail.com> | 2024-01-19 05:39:09 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-19 04:39:09 +0000 |
commit | 9d2c102fa0cc034b2bde51d27cd6c0e7f3cafa30 (patch) | |
tree | 1a354ac8a080d6d1244a8dacd666082971fea3da /components/script/dom/gamepadpose.rs | |
parent | 9a698b7bfbb2d8ec72e31d103051edf1f11b3e51 (diff) | |
download | servo-9d2c102fa0cc034b2bde51d27cd6c0e7f3cafa30.tar.gz servo-9d2c102fa0cc034b2bde51d27cd6c0e7f3cafa30.zip |
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>
Diffstat (limited to 'components/script/dom/gamepadpose.rs')
-rw-r--r-- | components/script/dom/gamepadpose.rs | 97 |
1 files changed, 28 insertions, 69 deletions
diff --git a/components/script/dom/gamepadpose.rs b/components/script/dom/gamepadpose.rs index 073a326dc0a..60b2e4521a7 100644 --- a/components/script/dom/gamepadpose.rs +++ b/components/script/dom/gamepadpose.rs @@ -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() } } |