Skip to content

Commit 9ed3400

Browse files
committed
Implement *WasmAbi for arrays
1 parent d26068d commit 9ed3400

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

crates/cli-support/src/descriptor.rs

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub enum Descriptor {
5353
U64,
5454
F32,
5555
F64,
56+
Array,
5657
Boolean,
5758
Function(Box<Function>),
5859
Closure(Box<Closure>),

src/convert/slices.rs

+60
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,63 @@ if_std! {
271271
fn is_none(slice: &WasmSlice) -> bool { slice.ptr == 0 }
272272
}
273273
}
274+
275+
276+
macro_rules! array_impls {
277+
($($N:expr),+) => {
278+
$(
279+
impl<T> FromWasmAbi for [T; $N]
280+
where
281+
T: Copy + FromWasmAbi<Abi = WasmSlice>
282+
{
283+
type Abi = WasmSlice;
284+
285+
#[inline]
286+
unsafe fn from_abi(js: Self::Abi) -> Self {
287+
use core::convert::TryInto;
288+
let slice = slice::from_raw_parts(
289+
<*const T>::from_abi(js.ptr),
290+
js.len as usize,
291+
);
292+
slice.try_into().unwrap()
293+
}
294+
}
295+
296+
impl<T> IntoWasmAbi for [T; $N]
297+
where
298+
T: Copy + IntoWasmAbi<Abi = WasmSlice>
299+
{
300+
type Abi = WasmSlice;
301+
302+
#[inline]
303+
fn into_abi(self) -> Self::Abi {
304+
WasmSlice {
305+
ptr: self.as_ptr().into_abi(),
306+
len: self.len() as u32,
307+
}
308+
}
309+
}
310+
311+
impl<T> OptionFromWasmAbi for [T; $N]
312+
where
313+
T: Copy + FromWasmAbi<Abi = WasmSlice>
314+
{
315+
#[inline]
316+
fn is_none(slice: &WasmSlice) -> bool { slice.ptr == 0 }
317+
}
318+
319+
impl<T> OptionIntoWasmAbi for [T; $N]
320+
where
321+
T: Copy + IntoWasmAbi<Abi = WasmSlice>
322+
{
323+
#[inline]
324+
fn none() -> WasmSlice { null_slice() }
325+
}
326+
)+
327+
}
328+
}
329+
330+
array_impls!(
331+
0, 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,
332+
26, 27, 28, 29, 30, 31, 32
333+
);

src/describe.rs

+19
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ tys! {
2727
U64
2828
F32
2929
F64
30+
ARRAY
3031
BOOLEAN
3132
FUNCTION
3233
CLOSURE
@@ -184,3 +185,21 @@ impl<T: WasmDescribe> WasmDescribe for Clamped<T> {
184185
T::describe();
185186
}
186187
}
188+
189+
macro_rules! array_impls {
190+
($($N:expr),+) => {
191+
$(
192+
impl<T: WasmDescribe> WasmDescribe for [T; $N] {
193+
fn describe() {
194+
inform(ARRAY);
195+
T::describe();
196+
}
197+
}
198+
)+
199+
}
200+
}
201+
202+
array_impls!(
203+
0, 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,
204+
26, 27, 28, 29, 30, 31, 32
205+
);

0 commit comments

Comments
 (0)