forked from servo/servo
/
typedarrays.rs
171 lines (152 loc) · 5.25 KB
/
typedarrays.rs
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
170
171
/* 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::marker::PhantomData;
use std::ptr;
use js::jsapi::{Heap, JSObject, JS_GetArrayBufferViewBuffer};
use js::rust::wrappers::DetachArrayBuffer;
use js::rust::{CustomAutoRooterGuard, MutableHandleObject};
use js::typedarray::{CreateWith, TypedArray, TypedArrayElement, TypedArrayElementCreator};
use crate::script_runtime::JSContext;
pub struct HeapTypedArray<T> {
internal: Box<Heap<*mut JSObject>>,
phantom: PhantomData<T>,
}
unsafe impl<T> crate::dom::bindings::trace::JSTraceable for HeapTypedArray<T> {
#[inline]
unsafe fn trace(&self, tracer: *mut js::jsapi::JSTracer) {
self.internal.trace(tracer);
}
}
impl<T> HeapTypedArray<T>
where
T: TypedArrayElement + TypedArrayElementCreator,
T::Element: Clone + Copy,
{
pub fn new_initialized(js_object: Option<*mut JSObject>) -> HeapTypedArray<T> {
match js_object {
Some(inner_js_object) => HeapTypedArray {
internal: Heap::boxed(inner_js_object),
phantom: PhantomData::default(),
},
None => Self::default(),
}
}
pub fn default() -> HeapTypedArray<T> {
HeapTypedArray {
internal: Box::new(Heap::default()),
phantom: PhantomData::default(),
}
}
pub fn set_data(&self, cx: JSContext, data: &[T::Element]) -> Result<(), ()> {
rooted!(in (*cx) let mut array = ptr::null_mut::<JSObject>());
let _: TypedArray<T, *mut JSObject> = create_typed_array(cx, data, array.handle_mut())?;
self.internal.set(*array);
Ok(())
}
pub fn acquire_data(&self, cx: JSContext) -> Result<Vec<T::Element>, ()> {
assert!(self.is_initialized());
typedarray!(in(*cx) let array: TypedArray = self.internal.get());
let data = if let Ok(array) =
array as Result<CustomAutoRooterGuard<'_, TypedArray<T, *mut JSObject>>, &mut ()>
{
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 [T::Element],
source_start: usize,
length: usize,
) -> Result<(), ()> {
assert!(self.is_initialized());
typedarray!(in(*cx) let array: TypedArray = self.internal.get());
let Ok(array) = array as Result<CustomAutoRooterGuard<'_, TypedArray<T, *mut JSObject>>, &mut ()> 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<TypedArray<T, *mut JSObject>>,
dest_start: usize,
length: usize,
) -> Result<(), ()> {
assert!(self.is_initialized());
typedarray!(in(*cx) let mut array: TypedArray = self.internal.get());
let Ok(mut array) = array as Result<CustomAutoRooterGuard<'_, TypedArray<T, *mut JSObject>>, &mut ()> 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<TypedArray<T, *mut JSObject>, ()> {
TypedArray::from(self.internal.get())
}
pub fn internal_to_option(&self) -> Option<TypedArray<T, *mut JSObject>> {
if self.is_initialized() {
Some(self.get_internal().expect("Failed to get internal."))
} else {
warn!("Internal not initialized.");
None
}
}
}
pub fn create_typed_array<T>(
cx: JSContext,
data: &[T::Element],
dest: MutableHandleObject,
) -> Result<TypedArray<T, *mut JSObject>, ()>
where
T: TypedArrayElement + TypedArrayElementCreator,
{
let res = unsafe { TypedArray::<T, *mut JSObject>::create(*cx, CreateWith::Slice(data), dest) };
if res.is_err() {
Err(())
} else {
TypedArray::from(dest.get())
}
}
pub fn create_typed_array_with_length<T>(
cx: JSContext,
len: usize,
dest: MutableHandleObject,
) -> Result<TypedArray<T, *mut JSObject>, ()>
where
T: TypedArrayElement + TypedArrayElementCreator,
{
let res = unsafe { TypedArray::<T, *mut JSObject>::create(*cx, CreateWith::Length(len), dest) };
if res.is_err() {
Err(())
} else {
TypedArray::from(dest.get())
}
}