Skip to content

Commit

Permalink
Implement *WasmAbi for arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
c410-f3r committed Feb 25, 2020
1 parent d26068d commit 5a4521d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/cli-support/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub enum Descriptor {
U64,
F32,
F64,
Array,
Boolean,
Function(Box<Function>),
Closure(Box<Closure>),
Expand Down
60 changes: 60 additions & 0 deletions src/convert/slices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,63 @@ if_std! {
fn is_none(slice: &WasmSlice) -> bool { slice.ptr == 0 }
}
}


macro_rules! array_impls {
($($N:expr),+) => {
$(
impl<T> FromWasmAbi for [T; $N]
where
T: Copy + FromWasmAbi<Abi = WasmSlice>
{
type Abi = WasmSlice;

#[inline]
unsafe fn from_abi(js: Self::Abi) -> Self {
use core::convert::TryInto;
let slice = slice::from_raw_parts(
<*const T>::from_abi(js.ptr),
js.len as usize,
);
slice.try_into().unwrap()
}
}

impl<T> IntoWasmAbi for [T; $N]
where
T: Copy + IntoWasmAbi<Abi = WasmSlice>
{
type Abi = WasmSlice;

#[inline]
fn into_abi(self) -> Self::Abi {
WasmSlice {
ptr: self.as_ptr().into_abi(),
len: self.len() as u32,
}
}
}

impl<T> OptionFromWasmAbi for [T; $N]
where
T: Copy + FromWasmAbi<Abi = WasmSlice>
{
#[inline]
fn is_none(slice: &WasmSlice) -> bool { slice.ptr == 0 }
}

impl<T> OptionIntoWasmAbi for [T; $N]
where
T: Copy + IntoWasmAbi<Abi = WasmSlice>
{
#[inline]
fn none() -> WasmSlice { null_slice() }
}
)+
}
}

array_impls!(
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
);
19 changes: 19 additions & 0 deletions src/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ tys! {
U64
F32
F64
ARRAY
BOOLEAN
FUNCTION
CLOSURE
Expand Down Expand Up @@ -184,3 +185,21 @@ impl<T: WasmDescribe> WasmDescribe for Clamped<T> {
T::describe();
}
}

macro_rules! array_impls {
($($N:expr),+) => {
$(
impl<T: WasmDescribe> WasmDescribe for [T; $N] {
fn describe() {
inform(ARRAY);
T::describe();
}
}
)+
}
}

array_impls!(
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
);

0 comments on commit 5a4521d

Please sign in to comment.