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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
#[cfg(feature = "std")] use std::prelude::v1::*; use core::slice; use core::str; use convert::{FromWasmAbi, IntoWasmAbi, RefFromWasmAbi, RefMutFromWasmAbi, WasmAbi}; use convert::{OptionIntoWasmAbi, Stack}; if_std! { use core::mem; use convert::OptionFromWasmAbi; } #[repr(C)] pub struct WasmSlice { pub ptr: u32, pub len: u32, } unsafe impl WasmAbi for WasmSlice {} #[inline] fn null_slice() -> WasmSlice { WasmSlice { ptr: 0, len: 0 } } macro_rules! vectors { ($($t:ident)*) => ($( if_std! { impl IntoWasmAbi for Box<[$t]> { type Abi = WasmSlice; #[inline] fn into_abi(self, extra: &mut Stack) -> WasmSlice { let ptr = self.as_ptr(); let len = self.len(); mem::forget(self); WasmSlice { ptr: ptr.into_abi(extra), len: len as u32, } } } impl OptionIntoWasmAbi for Box<[$t]> { fn none() -> WasmSlice { null_slice() } } impl FromWasmAbi for Box<[$t]> { type Abi = WasmSlice; #[inline] unsafe fn from_abi(js: WasmSlice, extra: &mut Stack) -> Self { let ptr = <*mut $t>::from_abi(js.ptr, extra); let len = js.len as usize; Vec::from_raw_parts(ptr, len, len).into_boxed_slice() } } impl OptionFromWasmAbi for Box<[$t]> { fn is_none(slice: &WasmSlice) -> bool { slice.ptr == 0 } } } impl<'a> IntoWasmAbi for &'a [$t] { type Abi = WasmSlice; #[inline] fn into_abi(self, extra: &mut Stack) -> WasmSlice { WasmSlice { ptr: self.as_ptr().into_abi(extra), len: self.len() as u32, } } } impl<'a> OptionIntoWasmAbi for &'a [$t] { fn none() -> WasmSlice { null_slice() } } impl<'a> IntoWasmAbi for &'a mut [$t] { type Abi = WasmSlice; #[inline] fn into_abi(self, extra: &mut Stack) -> WasmSlice { (&*self).into_abi(extra) } } impl<'a> OptionIntoWasmAbi for &'a mut [$t] { fn none() -> WasmSlice { null_slice() } } impl RefFromWasmAbi for [$t] { type Abi = WasmSlice; type Anchor = &'static [$t]; #[inline] unsafe fn ref_from_abi(js: WasmSlice, extra: &mut Stack) -> &'static [$t] { slice::from_raw_parts( <*const $t>::from_abi(js.ptr, extra), js.len as usize, ) } } impl RefMutFromWasmAbi for [$t] { type Abi = WasmSlice; type Anchor = &'static mut [$t]; #[inline] unsafe fn ref_mut_from_abi(js: WasmSlice, extra: &mut Stack) -> &'static mut [$t] { slice::from_raw_parts_mut( <*mut $t>::from_abi(js.ptr, extra), js.len as usize, ) } } )*) } vectors! { u8 i8 u16 i16 u32 i32 u64 i64 f32 f64 } if_std! { impl<T> IntoWasmAbi for Vec<T> where Box<[T]>: IntoWasmAbi<Abi = WasmSlice> { type Abi = <Box<[T]> as IntoWasmAbi>::Abi; fn into_abi(self, extra: &mut Stack) -> Self::Abi { self.into_boxed_slice().into_abi(extra) } } impl<T> OptionIntoWasmAbi for Vec<T> where Box<[T]>: IntoWasmAbi<Abi = WasmSlice> { fn none() -> WasmSlice { null_slice() } } impl<T> FromWasmAbi for Vec<T> where Box<[T]>: FromWasmAbi<Abi = WasmSlice> { type Abi = <Box<[T]> as FromWasmAbi>::Abi; unsafe fn from_abi(js: Self::Abi, extra: &mut Stack) -> Self { <Box<[T]>>::from_abi(js, extra).into() } } impl<T> OptionFromWasmAbi for Vec<T> where Box<[T]>: FromWasmAbi<Abi = WasmSlice> { fn is_none(abi: &WasmSlice) -> bool { abi.ptr == 0 } } impl IntoWasmAbi for String { type Abi = <Vec<u8> as IntoWasmAbi>::Abi; #[inline] fn into_abi(self, extra: &mut Stack) -> Self::Abi { self.into_bytes().into_abi(extra) } } impl OptionIntoWasmAbi for String { fn none() -> WasmSlice { null_slice() } } impl FromWasmAbi for String { type Abi = <Vec<u8> as FromWasmAbi>::Abi; #[inline] unsafe fn from_abi(js: Self::Abi, extra: &mut Stack) -> Self { String::from_utf8_unchecked(<Vec<u8>>::from_abi(js, extra)) } } impl OptionFromWasmAbi for String { fn is_none(slice: &WasmSlice) -> bool { slice.ptr == 0 } } } impl<'a> IntoWasmAbi for &'a str { type Abi = <&'a [u8] as IntoWasmAbi>::Abi; #[inline] fn into_abi(self, extra: &mut Stack) -> Self::Abi { self.as_bytes().into_abi(extra) } } impl<'a> OptionIntoWasmAbi for &'a str { fn none() -> WasmSlice { null_slice() } } impl RefFromWasmAbi for str { type Abi = <[u8] as RefFromWasmAbi>::Abi; type Anchor = &'static str; #[inline] unsafe fn ref_from_abi(js: Self::Abi, extra: &mut Stack) -> Self::Anchor { str::from_utf8_unchecked(<[u8]>::ref_from_abi(js, extra)) } } if_std! { use JsValue; impl IntoWasmAbi for Box<[JsValue]> { type Abi = WasmSlice; #[inline] fn into_abi(self, extra: &mut Stack) -> WasmSlice { let ptr = self.as_ptr(); let len = self.len(); mem::forget(self); WasmSlice { ptr: ptr.into_abi(extra), len: len as u32, } } } impl OptionIntoWasmAbi for Box<[JsValue]> { fn none() -> WasmSlice { null_slice() } } impl FromWasmAbi for Box<[JsValue]> { type Abi = WasmSlice; #[inline] unsafe fn from_abi(js: WasmSlice, extra: &mut Stack) -> Self { let ptr = <*mut JsValue>::from_abi(js.ptr, extra); let len = js.len as usize; Vec::from_raw_parts(ptr, len, len).into_boxed_slice() } } impl OptionFromWasmAbi for Box<[JsValue]> { fn is_none(slice: &WasmSlice) -> bool { slice.ptr == 0 } } }