| 1 |
#![allow(clippy::fn_to_numeric_cast)] |
| 2 |
|
| 3 |
use core::mem; |
| 4 |
|
| 5 |
use crate::convert::slices::WasmSlice; |
| 6 |
use crate::convert::RefFromWasmAbi; |
| 7 |
use crate::convert::{FromWasmAbi, IntoWasmAbi, ReturnWasmAbi, WasmAbi, WasmRet}; |
| 8 |
use crate::describe::{inform, WasmDescribe, FUNCTION}; |
| 9 |
use crate::throw_str; |
| 10 |
|
| 11 |
macro_rules! stack_closures { |
| 12 |
($( ($cnt:tt $invoke:ident $invoke_mut:ident $($var:ident $arg1:ident $arg2:ident $arg3:ident $arg4:ident)*) )*) => ($( |
| 13 |
impl<'a, 'b, $($var,)* R> IntoWasmAbi for &'a (dyn Fn($($var),*) -> R + 'b) |
| 14 |
where $($var: FromWasmAbi,)* |
| 15 |
R: ReturnWasmAbi |
| 16 |
{ |
| 17 |
type Abi = WasmSlice; |
| 18 |
|
| 19 |
fn into_abi(self) -> WasmSlice { |
| 20 |
unsafe { |
| 21 |
let (a, b): (usize, usize) = mem::transmute(self); |
| 22 |
WasmSlice { ptr: a as u32, len: b as u32 } |
| 23 |
} |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
#[allow(non_snake_case)] |
| 28 |
unsafe extern "C" fn $invoke<$($var: FromWasmAbi,)* R: ReturnWasmAbi>( |
| 29 |
a: usize, |
| 30 |
b: usize, |
| 31 |
$( |
| 32 |
$arg1: <$var::Abi as WasmAbi>::Prim1, |
| 33 |
$arg2: <$var::Abi as WasmAbi>::Prim2, |
| 34 |
$arg3: <$var::Abi as WasmAbi>::Prim3, |
| 35 |
$arg4: <$var::Abi as WasmAbi>::Prim4, |
| 36 |
)* |
| 37 |
) -> WasmRet<R::Abi> { |
| 38 |
if a == 0 { |
| 39 |
throw_str("closure invoked after being dropped"); |
| 40 |
} |
| 41 |
// Scope all local variables before we call `return_abi` to |
| 42 |
// ensure they're all destroyed as `return_abi` may throw |
| 43 |
let ret = { |
| 44 |
let f: &dyn Fn($($var),*) -> R = mem::transmute((a, b)); |
| 45 |
$( |
| 46 |
let $var = <$var as FromWasmAbi>::from_abi($var::Abi::join($arg1, $arg2, $arg3, $arg4)); |
| 47 |
)* |
| 48 |
f($($var),*) |
| 49 |
}; |
| 50 |
ret.return_abi().into() |
| 51 |
} |
| 52 |
|
| 53 |
impl<'a, $($var,)* R> WasmDescribe for dyn Fn($($var),*) -> R + 'a |
| 54 |
where $($var: FromWasmAbi,)* |
| 55 |
R: ReturnWasmAbi |
| 56 |
{ |
| 57 |
fn describe() { |
| 58 |
inform(FUNCTION); |
| 59 |
inform($invoke::<$($var,)* R> as u32); |
| 60 |
inform($cnt); |
| 61 |
$(<$var as WasmDescribe>::describe();)* |
| 62 |
<R as WasmDescribe>::describe(); |
| 63 |
<R as WasmDescribe>::describe(); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
impl<'a, 'b, $($var,)* R> IntoWasmAbi for &'a mut (dyn FnMut($($var),*) -> R + 'b) |
| 68 |
where $($var: FromWasmAbi,)* |
| 69 |
R: ReturnWasmAbi |
| 70 |
{ |
| 71 |
type Abi = WasmSlice; |
| 72 |
|
| 73 |
fn into_abi(self) -> WasmSlice { |
| 74 |
unsafe { |
| 75 |
let (a, b): (usize, usize) = mem::transmute(self); |
| 76 |
WasmSlice { ptr: a as u32, len: b as u32 } |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
#[allow(non_snake_case)] |
| 82 |
unsafe extern "C" fn $invoke_mut<$($var: FromWasmAbi,)* R: ReturnWasmAbi>( |
| 83 |
a: usize, |
| 84 |
b: usize, |
| 85 |
$( |
| 86 |
$arg1: <$var::Abi as WasmAbi>::Prim1, |
| 87 |
$arg2: <$var::Abi as WasmAbi>::Prim2, |
| 88 |
$arg3: <$var::Abi as WasmAbi>::Prim3, |
| 89 |
$arg4: <$var::Abi as WasmAbi>::Prim4, |
| 90 |
)* |
| 91 |
) -> WasmRet<R::Abi> { |
| 92 |
if a == 0 { |
| 93 |
throw_str("closure invoked recursively or after being dropped"); |
| 94 |
} |
| 95 |
// Scope all local variables before we call `return_abi` to |
| 96 |
// ensure they're all destroyed as `return_abi` may throw |
| 97 |
let ret = { |
| 98 |
let f: &mut dyn FnMut($($var),*) -> R = mem::transmute((a, b)); |
| 99 |
$( |
| 100 |
let $var = <$var as FromWasmAbi>::from_abi($var::Abi::join($arg1, $arg2, $arg3, $arg4)); |
| 101 |
)* |
| 102 |
f($($var),*) |
| 103 |
}; |
| 104 |
ret.return_abi().into() |
| 105 |
} |
| 106 |
|
| 107 |
impl<'a, $($var,)* R> WasmDescribe for dyn FnMut($($var),*) -> R + 'a |
| 108 |
where $($var: FromWasmAbi,)* |
| 109 |
R: ReturnWasmAbi |
| 110 |
{ |
| 111 |
fn describe() { |
| 112 |
inform(FUNCTION); |
| 113 |
inform($invoke_mut::<$($var,)* R> as u32); |
| 114 |
inform($cnt); |
| 115 |
$(<$var as WasmDescribe>::describe();)* |
| 116 |
<R as WasmDescribe>::describe(); |
| 117 |
<R as WasmDescribe>::describe(); |
| 118 |
} |
| 119 |
} |
| 120 |
)*) |
| 121 |
} |
| 122 |
|
| 123 |
stack_closures! { |
| 124 |
(0 invoke0 invoke0_mut) |
| 125 |
(1 invoke1 invoke1_mut A a1 a2 a3 a4) |
| 126 |
(2 invoke2 invoke2_mut A a1 a2 a3 a4 B b1 b2 b3 b4) |
| 127 |
(3 invoke3 invoke3_mut A a1 a2 a3 a4 B b1 b2 b3 b4 C c1 c2 c3 c4) |
| 128 |
(4 invoke4 invoke4_mut A a1 a2 a3 a4 B b1 b2 b3 b4 C c1 c2 c3 c4 D d1 d2 d3 d4) |
| 129 |
(5 invoke5 invoke5_mut A a1 a2 a3 a4 B b1 b2 b3 b4 C c1 c2 c3 c4 D d1 d2 d3 d4 E e1 e2 e3 e4) |
| 130 |
(6 invoke6 invoke6_mut A a1 a2 a3 a4 B b1 b2 b3 b4 C c1 c2 c3 c4 D d1 d2 d3 d4 E e1 e2 e3 e4 F f1 f2 f3 f4) |
| 131 |
(7 invoke7 invoke7_mut A a1 a2 a3 a4 B b1 b2 b3 b4 C c1 c2 c3 c4 D d1 d2 d3 d4 E e1 e2 e3 e4 F f1 f2 f3 f4 G g1 g2 g3 g4) |
| 132 |
(8 invoke8 invoke8_mut A a1 a2 a3 a4 B b1 b2 b3 b4 C c1 c2 c3 c4 D d1 d2 d3 d4 E e1 e2 e3 e4 F f1 f2 f3 f4 G g1 g2 g3 g4 H h1 h2 h3 h4) |
| 133 |
} |
| 134 |
|
| 135 |
impl<'a, 'b, A, R> IntoWasmAbi for &'a (dyn Fn(&A) -> R + 'b) |
| 136 |
where |
| 137 |
A: RefFromWasmAbi, |
| 138 |
R: ReturnWasmAbi, |
| 139 |
{ |
| 140 |
type Abi = WasmSlice; |
| 141 |
|
| 142 |
fn into_abi(self) -> WasmSlice { |
| 143 |
unsafe { |
| 144 |
let (a, b): (usize, usize) = mem::transmute(self); |
| 145 |
WasmSlice { |
| 146 |
ptr: a as u32, |
| 147 |
len: b as u32, |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
#[allow(non_snake_case)] |
| 154 |
unsafe extern "C" fn invoke1_ref<A: RefFromWasmAbi, R: ReturnWasmAbi>( |
| 155 |
a: usize, |
| 156 |
b: usize, |
| 157 |
arg1: <A::Abi as WasmAbi>::Prim1, |
| 158 |
arg2: <A::Abi as WasmAbi>::Prim2, |
| 159 |
arg3: <A::Abi as WasmAbi>::Prim3, |
| 160 |
arg4: <A::Abi as WasmAbi>::Prim4, |
| 161 |
) -> WasmRet<R::Abi> { |
| 162 |
if a == 0 { |
| 163 |
throw_str("closure invoked after being dropped"); |
| 164 |
} |
| 165 |
// Scope all local variables before we call `return_abi` to |
| 166 |
// ensure they're all destroyed as `return_abi` may throw |
| 167 |
let ret = { |
| 168 |
let f: &dyn Fn(&A) -> R = mem::transmute((a, b)); |
| 169 |
let arg = <A as RefFromWasmAbi>::ref_from_abi(A::Abi::join(arg1, arg2, arg3, arg4)); |
| 170 |
f(&*arg) |
| 171 |
}; |
| 172 |
ret.return_abi().into() |
| 173 |
} |
| 174 |
|
| 175 |
impl<'a, A, R> WasmDescribe for dyn Fn(&A) -> R + 'a |
| 176 |
where |
| 177 |
A: RefFromWasmAbi, |
| 178 |
R: ReturnWasmAbi, |
| 179 |
{ |
| 180 |
fn describe() { |
| 181 |
inform(FUNCTION); |
| 182 |
inform(invoke1_ref::<A, R> as u32); |
| 183 |
inform(1); |
| 184 |
<&A as WasmDescribe>::describe(); |
| 185 |
<R as WasmDescribe>::describe(); |
| 186 |
<R as WasmDescribe>::describe(); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
impl<'a, 'b, A, R> IntoWasmAbi for &'a mut (dyn FnMut(&A) -> R + 'b) |
| 191 |
where |
| 192 |
A: RefFromWasmAbi, |
| 193 |
R: ReturnWasmAbi, |
| 194 |
{ |
| 195 |
type Abi = WasmSlice; |
| 196 |
|
| 197 |
fn into_abi(self) -> WasmSlice { |
| 198 |
unsafe { |
| 199 |
let (a, b): (usize, usize) = mem::transmute(self); |
| 200 |
WasmSlice { |
| 201 |
ptr: a as u32, |
| 202 |
len: b as u32, |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
#[allow(non_snake_case)] |
| 209 |
unsafe extern "C" fn invoke1_mut_ref<A: RefFromWasmAbi, R: ReturnWasmAbi>( |
| 210 |
a: usize, |
| 211 |
b: usize, |
| 212 |
arg1: <A::Abi as WasmAbi>::Prim1, |
| 213 |
arg2: <A::Abi as WasmAbi>::Prim2, |
| 214 |
arg3: <A::Abi as WasmAbi>::Prim3, |
| 215 |
arg4: <A::Abi as WasmAbi>::Prim4, |
| 216 |
) -> WasmRet<R::Abi> { |
| 217 |
if a == 0 { |
| 218 |
throw_str("closure invoked recursively or after being dropped"); |
| 219 |
} |
| 220 |
// Scope all local variables before we call `return_abi` to |
| 221 |
// ensure they're all destroyed as `return_abi` may throw |
| 222 |
let ret = { |
| 223 |
let f: &mut dyn FnMut(&A) -> R = mem::transmute((a, b)); |
| 224 |
let arg = <A as RefFromWasmAbi>::ref_from_abi(A::Abi::join(arg1, arg2, arg3, arg4)); |
| 225 |
f(&*arg) |
| 226 |
}; |
| 227 |
ret.return_abi().into() |
| 228 |
} |
| 229 |
|
| 230 |
impl<'a, A, R> WasmDescribe for dyn FnMut(&A) -> R + 'a |
| 231 |
where |
| 232 |
A: RefFromWasmAbi, |
| 233 |
R: ReturnWasmAbi, |
| 234 |
{ |
| 235 |
fn describe() { |
| 236 |
inform(FUNCTION); |
| 237 |
inform(invoke1_mut_ref::<A, R> as u32); |
| 238 |
inform(1); |
| 239 |
<&A as WasmDescribe>::describe(); |
| 240 |
<R as WasmDescribe>::describe(); |
| 241 |
<R as WasmDescribe>::describe(); |
| 242 |
} |
| 243 |
} |