wasm_bindgen/convert/
closures.rs

1use alloc::boxed::Box;
2use core::mem;
3
4#[cfg(all(target_arch = "wasm32", feature = "std", panic = "unwind"))]
5use crate::__rt::maybe_catch_unwind;
6#[cfg(all(target_arch = "wasm32", feature = "std", panic = "unwind"))]
7use std::panic::AssertUnwindSafe;
8
9use crate::closure::{Closure, IntoWasmClosure, WasmClosure, WasmClosureFnOnce};
10use crate::convert::slices::WasmSlice;
11use crate::convert::RefFromWasmAbi;
12use crate::convert::{FromWasmAbi, IntoWasmAbi, ReturnWasmAbi, WasmAbi, WasmRet};
13use crate::describe::{inform, WasmDescribe, FUNCTION};
14use crate::throw_str;
15use crate::JsValue;
16use crate::UnwrapThrowExt;
17
18macro_rules! closures {
19    // A counter helper to count number of arguments.
20    (@count_one $ty:ty) => (1);
21
22    (@describe ( $($ty:ty),* )) => {
23        // Needs to be a constant so that interpreter doesn't crash on
24        // unsupported operations in debug mode.
25        const ARG_COUNT: u32 = 0 $(+ closures!(@count_one $ty))*;
26        inform(ARG_COUNT);
27        $(<$ty>::describe();)*
28    };
29
30    // This silly helper is because by default Rust infers `|var_with_ref_type| ...` closure
31    // as `impl Fn(&'outer_lifetime A)` instead of `impl for<'temp_lifetime> Fn(&'temp_lifetime A)`
32    // while `|var_with_ref_type: &A|` makes it use the higher-order generic as expected.
33    (@closure ($($ty:ty),*) $($var:ident)* $body:block) => (move |$($var: $ty),*| $body);
34
35    (@impl_for_fn $is_mut:literal [$($mut:ident)?] $Fn:ident $FnArgs:tt $FromWasmAbi:ident $($var_expr:expr => $var:ident $arg1:ident $arg2:ident $arg3:ident $arg4:ident)*) => (const _: () = {
36        impl<$($var,)* R> IntoWasmAbi for &'_ $($mut)? (dyn $Fn $FnArgs -> R + '_)
37        where
38            Self: WasmDescribe,
39        {
40            type Abi = WasmSlice;
41
42            fn into_abi(self) -> WasmSlice {
43                unsafe {
44                    let (a, b): (usize, usize) = mem::transmute(self);
45                    WasmSlice { ptr: a as u32, len: b as u32 }
46                }
47            }
48        }
49
50        #[allow(non_snake_case)]
51        unsafe extern "C-unwind" fn invoke<$($var: $FromWasmAbi,)* R: ReturnWasmAbi>(
52            a: usize,
53            b: usize,
54            $(
55            $arg1: <$var::Abi as WasmAbi>::Prim1,
56            $arg2: <$var::Abi as WasmAbi>::Prim2,
57            $arg3: <$var::Abi as WasmAbi>::Prim3,
58            $arg4: <$var::Abi as WasmAbi>::Prim4,
59            )*
60        ) -> WasmRet<R::Abi> {
61            if a == 0 {
62                throw_str("closure invoked recursively or after being dropped");
63            }
64            // Scope all local variables before we call `return_abi` to
65            // ensure they're all destroyed as `return_abi` may throw
66            let ret = {
67                let f: & $($mut)? dyn $Fn $FnArgs -> R = mem::transmute((a, b));
68                $(
69                    let $var = $var::Abi::join($arg1, $arg2, $arg3, $arg4);
70                )*
71
72                #[cfg(all(target_arch = "wasm32", feature = "std", panic = "unwind"))]
73                {
74                    maybe_catch_unwind(AssertUnwindSafe(|| f($($var_expr),*)))
75                }
76
77                #[cfg(not(all(target_arch = "wasm32", feature = "std", panic = "unwind")))]
78                {
79                    f($($var_expr),*)
80                }
81            };
82            ret.return_abi().into()
83        }
84
85        #[allow(clippy::fn_to_numeric_cast)]
86        impl<$($var,)* R> WasmDescribe for dyn $Fn $FnArgs -> R + '_
87        where
88            $($var: $FromWasmAbi,)*
89            R: ReturnWasmAbi,
90        {
91            #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
92            fn describe() {
93                inform(FUNCTION);
94                inform(invoke::<$($var,)* R> as *const () as usize as u32);
95                closures!(@describe $FnArgs);
96                R::describe();
97                R::describe();
98            }
99        }
100
101        unsafe impl<$($var,)* R> WasmClosure for dyn $Fn $FnArgs -> R + '_
102        where
103            Self: WasmDescribe,
104        {
105            const IS_MUT: bool = $is_mut;
106        }
107
108        impl<T, $($var,)* R> IntoWasmClosure<dyn $Fn $FnArgs -> R> for T
109        where
110            T: 'static + $Fn $FnArgs -> R,
111        {
112            fn unsize(self: Box<Self>) -> Box<dyn $Fn $FnArgs -> R> { self }
113        }
114    };);
115
116    (@impl_for_args $FnArgs:tt $FromWasmAbi:ident $($var_expr:expr => $var:ident $arg1:ident $arg2:ident $arg3:ident $arg4:ident)*) => {
117        closures!(@impl_for_fn false [] Fn $FnArgs $FromWasmAbi $($var_expr => $var $arg1 $arg2 $arg3 $arg4)*);
118        closures!(@impl_for_fn true [mut] FnMut $FnArgs $FromWasmAbi $($var_expr => $var $arg1 $arg2 $arg3 $arg4)*);
119
120        // The memory safety here in these implementations below is a bit tricky. We
121        // want to be able to drop the `Closure` object from within the invocation of a
122        // `Closure` for cases like promises. That means that while it's running we
123        // might drop the `Closure`, but that shouldn't invalidate the environment yet.
124        //
125        // Instead what we do is to wrap closures in `Rc` variables. The main `Closure`
126        // has a strong reference count which keeps the trait object alive. Each
127        // invocation of a closure then *also* clones this and gets a new reference
128        // count. When the closure returns it will release the reference count.
129        //
130        // This means that if the main `Closure` is dropped while it's being invoked
131        // then destruction is deferred until execution returns. Otherwise it'll
132        // deallocate data immediately.
133
134        #[allow(non_snake_case, unused_parens)]
135        impl<T, $($var,)* R> WasmClosureFnOnce<dyn FnMut $FnArgs -> R, $FnArgs, R> for T
136        where
137            T: 'static + FnOnce $FnArgs -> R,
138            $($var: $FromWasmAbi + 'static,)*
139            R: ReturnWasmAbi + 'static,
140        {
141            fn into_fn_mut(self) -> Box<dyn FnMut $FnArgs -> R> {
142                let mut me = Some(self);
143                Box::new(move |$($var),*| {
144                    let me = me.take().expect_throw("FnOnce called more than once");
145                    me($($var),*)
146                })
147            }
148
149            fn into_js_function(self) -> JsValue {
150                use alloc::rc::Rc;
151                use crate::__rt::WasmRefCell;
152
153                let rc1 = Rc::new(WasmRefCell::new(None));
154                let rc2 = rc1.clone();
155
156                let closure = Closure::once(closures!(@closure $FnArgs $($var)* {
157                    let result = self($($var),*);
158
159                    // And then drop the `Rc` holding this function's `Closure`
160                    // alive.
161                    debug_assert_eq!(Rc::strong_count(&rc2), 1);
162                    let option_closure = rc2.borrow_mut().take();
163                    debug_assert!(option_closure.is_some());
164                    drop(option_closure);
165
166                    result
167                }));
168
169                let js_val = closure.as_ref().clone();
170
171                *rc1.borrow_mut() = Some(closure);
172                debug_assert_eq!(Rc::strong_count(&rc1), 2);
173                drop(rc1);
174
175                js_val
176            }
177        }
178    };
179
180    ($( ($($var:ident $arg1:ident $arg2:ident $arg3:ident $arg4:ident)*) )*) => ($(
181        closures!(@impl_for_args ($($var),*) FromWasmAbi $($var::from_abi($var) => $var $arg1 $arg2 $arg3 $arg4)*);
182    )*);
183}
184
185closures! {
186    ()
187    (A a1 a2 a3 a4)
188    (A a1 a2 a3 a4 B b1 b2 b3 b4)
189    (A a1 a2 a3 a4 B b1 b2 b3 b4 C c1 c2 c3 c4)
190    (A a1 a2 a3 a4 B b1 b2 b3 b4 C c1 c2 c3 c4 D d1 d2 d3 d4)
191    (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)
192    (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)
193    (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)
194    (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)
195}
196
197// Copy the above impls down here for where there's only one argument and it's a
198// reference. We could add more impls for more kinds of references, but it
199// becomes a combinatorial explosion quickly. Let's see how far we can get with
200// just this one! Maybe someone else can figure out voodoo so we don't have to
201// duplicate.
202
203// We need to allow coherence leak check just for these traits because we're providing separate implementation for `Fn(&A)` variants when `Fn(A)` one already exists.
204#[allow(coherence_leak_check)]
205const _: () = {
206    closures!(@impl_for_args (&A) RefFromWasmAbi &*A::ref_from_abi(A) => A a1 a2 a3 a4);
207};