1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
/* 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/. */
use crate::dom::bindings::codegen::Bindings::VRPoseBinding;
use crate::dom::bindings::codegen::Bindings::VRPoseBinding::VRPoseMethods;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector};
use crate::dom::bindings::root::DomRoot;
use crate::dom::globalscope::GlobalScope;
use dom_struct::dom_struct;
use js::jsapi::{Heap, JSContext, JSObject};
use js::typedarray::{CreateWith, Float32Array};
use std::ptr;
use std::ptr::NonNull;
use webvr_traits::webvr;
#[dom_struct]
pub struct VRPose {
reflector_: Reflector,
#[ignore_malloc_size_of = "mozjs"]
position: Heap<*mut JSObject>,
#[ignore_malloc_size_of = "mozjs"]
orientation: Heap<*mut JSObject>,
#[ignore_malloc_size_of = "mozjs"]
linear_vel: Heap<*mut JSObject>,
#[ignore_malloc_size_of = "mozjs"]
angular_vel: Heap<*mut JSObject>,
#[ignore_malloc_size_of = "mozjs"]
linear_acc: Heap<*mut JSObject>,
#[ignore_malloc_size_of = "mozjs"]
angular_acc: Heap<*mut JSObject>,
}
#[allow(unsafe_code)]
unsafe fn update_or_create_typed_array(
cx: *mut 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 _ = 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 {
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)) }
}
}
impl VRPose {
fn new_inherited() -> VRPose {
VRPose {
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(),
}
}
pub fn new(global: &GlobalScope, pose: &webvr::VRPose) -> DomRoot<VRPose> {
let root = reflect_dom_object(
Box::new(VRPose::new_inherited()),
global,
VRPoseBinding::Wrap,
);
root.update(&pose);
root
}
#[allow(unsafe_code)]
pub fn update(&self, pose: &webvr::VRPose) {
let cx = self.global().get_cx();
unsafe {
update_or_create_typed_array(
cx,
pose.position.as_ref().map(|v| &v[..]),
&self.position,
);
update_or_create_typed_array(
cx,
pose.orientation.as_ref().map(|v| &v[..]),
&self.orientation,
);
update_or_create_typed_array(
cx,
pose.linear_velocity.as_ref().map(|v| &v[..]),
&self.linear_vel,
);
update_or_create_typed_array(
cx,
pose.angular_velocity.as_ref().map(|v| &v[..]),
&self.angular_vel,
);
update_or_create_typed_array(
cx,
pose.linear_acceleration.as_ref().map(|v| &v[..]),
&self.linear_acc,
);
update_or_create_typed_array(
cx,
pose.angular_acceleration.as_ref().map(|v| &v[..]),
&self.angular_acc,
);
}
}
}
impl VRPoseMethods for VRPose {
#[allow(unsafe_code)]
// https://w3c.github.io/webvr/#dom-vrpose-position
unsafe fn GetPosition(&self, _cx: *mut JSContext) -> Option<NonNull<JSObject>> {
heap_to_option(&self.position)
}
#[allow(unsafe_code)]
// https://w3c.github.io/webvr/#dom-vrpose-linearvelocity
unsafe fn GetLinearVelocity(&self, _cx: *mut JSContext) -> Option<NonNull<JSObject>> {
heap_to_option(&self.linear_vel)
}
#[allow(unsafe_code)]
// https://w3c.github.io/webvr/#dom-vrpose-linearacceleration
unsafe fn GetLinearAcceleration(&self, _cx: *mut JSContext) -> Option<NonNull<JSObject>> {
heap_to_option(&self.linear_acc)
}
#[allow(unsafe_code)]
// https://w3c.github.io/webvr/#dom-vrpose-orientation
unsafe fn GetOrientation(&self, _cx: *mut JSContext) -> Option<NonNull<JSObject>> {
heap_to_option(&self.orientation)
}
#[allow(unsafe_code)]
// https://w3c.github.io/webvr/#dom-vrpose-angularvelocity
unsafe fn GetAngularVelocity(&self, _cx: *mut JSContext) -> Option<NonNull<JSObject>> {
heap_to_option(&self.angular_vel)
}
#[allow(unsafe_code)]
// https://w3c.github.io/webvr/#dom-vrpose-angularacceleration
unsafe fn GetAngularAcceleration(&self, _cx: *mut JSContext) -> Option<NonNull<JSObject>> {
heap_to_option(&self.angular_acc)
}
}
|