use std::fmt;
#[cfg(feature = "nightly")]
use std::marker::Unsize;
use std::mem::{self, ManuallyDrop};
use std::prelude::v1::*;
use crate::convert::*;
use crate::describe::*;
use crate::throw_str;
use crate::JsValue;
use crate::UnwrapThrowExt;
pub struct Closure<T: ?Sized> {
js: ManuallyDrop<JsValue>,
data: ManuallyDrop<Box<T>>,
}
union FatPtr<T: ?Sized> {
ptr: *mut T,
fields: (usize, usize),
}
impl<T> Closure<T>
where
T: ?Sized + WasmClosure,
{
#[cfg(feature = "nightly")]
pub fn new<F>(t: F) -> Closure<T>
where
F: Unsize<T> + 'static,
{
Closure::wrap(Box::new(t) as Box<T>)
}
pub fn wrap(mut data: Box<T>) -> Closure<T> {
assert_eq!(mem::size_of::<*const T>(), mem::size_of::<FatPtr<T>>());
let (a, b) = unsafe {
FatPtr {
ptr: &mut *data as *mut T,
}
.fields
};
extern "C" fn describe<T: WasmClosure + ?Sized>() {
inform(CLOSURE);
T::describe()
}
#[inline(never)]
unsafe fn breaks_if_inlined<T: WasmClosure + ?Sized>(a: usize, b: usize) -> u32 {
super::__wbindgen_describe_closure(a as u32, b as u32, describe::<T> as u32)
}
let idx = unsafe { breaks_if_inlined::<T>(a, b) };
Closure {
js: ManuallyDrop::new(JsValue::_new(idx)),
data: ManuallyDrop::new(data),
}
}
pub fn into_js_value(self) -> JsValue {
let idx = self.js.idx;
mem::forget(self);
JsValue::_new(idx)
}
pub fn forget(self) {
drop(self.into_js_value());
}
}
impl Closure<dyn FnOnce()> {
pub fn once<F, A, R>(fn_once: F) -> Closure<F::FnMut>
where
F: 'static + WasmClosureFnOnce<A, R>,
{
Closure::wrap(fn_once.into_fn_mut())
}
pub fn once_into_js<F, A, R>(fn_once: F) -> JsValue
where
F: 'static + WasmClosureFnOnce<A, R>,
{
fn_once.into_js_function()
}
}
#[doc(hidden)]
pub trait WasmClosureFnOnce<A, R>: 'static {
type FnMut: ?Sized + 'static + WasmClosure;
fn into_fn_mut(self) -> Box<Self::FnMut>;
fn into_js_function(self) -> JsValue;
}
impl<T: ?Sized> AsRef<JsValue> for Closure<T> {
fn as_ref(&self) -> &JsValue {
&self.js
}
}
impl<T> WasmDescribe for Closure<T>
where
T: WasmClosure + ?Sized,
{
fn describe() {
inform(EXTERNREF);
}
}
impl<'a, T> IntoWasmAbi for &'a Closure<T>
where
T: WasmClosure + ?Sized,
{
type Abi = u32;
fn into_abi(self) -> u32 {
(&*self.js).into_abi()
}
}
fn _check() {
fn _assert<T: IntoWasmAbi>() {}
_assert::<&Closure<dyn Fn()>>();
_assert::<&Closure<dyn Fn(String)>>();
_assert::<&Closure<dyn Fn() -> String>>();
_assert::<&Closure<dyn FnMut()>>();
_assert::<&Closure<dyn FnMut(String)>>();
_assert::<&Closure<dyn FnMut() -> String>>();
}
impl<T> fmt::Debug for Closure<T>
where
T: ?Sized,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Closure {{ ... }}")
}
}
impl<T> Drop for Closure<T>
where
T: ?Sized,
{
fn drop(&mut self) {
unsafe {
if super::__wbindgen_cb_drop(self.js.idx) != 0 {
ManuallyDrop::drop(&mut self.data);
}
}
}
}
#[doc(hidden)]
pub unsafe trait WasmClosure {
fn describe();
}
macro_rules! doit {
($(
($($var:ident)*)
)*) => ($(
unsafe impl<$($var,)* R> WasmClosure for dyn Fn($($var),*) -> R + 'static
where $($var: FromWasmAbi + 'static,)*
R: ReturnWasmAbi + 'static,
{
fn describe() {
#[allow(non_snake_case)]
unsafe extern "C" fn invoke<$($var: FromWasmAbi,)* R: ReturnWasmAbi>(
a: usize,
b: usize,
$($var: <$var as FromWasmAbi>::Abi),*
) -> <R as ReturnWasmAbi>::Abi {
if a == 0 {
throw_str("closure invoked recursively or destroyed already");
}
let ret = {
let f: *const dyn Fn($($var),*) -> R =
FatPtr { fields: (a, b) }.ptr;
$(
let $var = <$var as FromWasmAbi>::from_abi($var);
)*
(*f)($($var),*)
};
ret.return_abi()
}
inform(invoke::<$($var,)* R> as u32);
unsafe extern fn destroy<$($var: FromWasmAbi,)* R: ReturnWasmAbi>(
a: usize,
b: usize,
) {
if a == 0 {
return;
}
drop(Box::from_raw(FatPtr::<dyn Fn($($var,)*) -> R> {
fields: (a, b)
}.ptr));
}
inform(destroy::<$($var,)* R> as u32);
<&Self>::describe();
}
}
unsafe impl<$($var,)* R> WasmClosure for dyn FnMut($($var),*) -> R + 'static
where $($var: FromWasmAbi + 'static,)*
R: ReturnWasmAbi + 'static,
{
fn describe() {
#[allow(non_snake_case)]
unsafe extern "C" fn invoke<$($var: FromWasmAbi,)* R: ReturnWasmAbi>(
a: usize,
b: usize,
$($var: <$var as FromWasmAbi>::Abi),*
) -> <R as ReturnWasmAbi>::Abi {
if a == 0 {
throw_str("closure invoked recursively or destroyed already");
}
let ret = {
let f: *const dyn FnMut($($var),*) -> R =
FatPtr { fields: (a, b) }.ptr;
let f = f as *mut dyn FnMut($($var),*) -> R;
$(
let $var = <$var as FromWasmAbi>::from_abi($var);
)*
(*f)($($var),*)
};
ret.return_abi()
}
inform(invoke::<$($var,)* R> as u32);
unsafe extern fn destroy<$($var: FromWasmAbi,)* R: ReturnWasmAbi>(
a: usize,
b: usize,
) {
if a == 0 {
return;
}
drop(Box::from_raw(FatPtr::<dyn FnMut($($var,)*) -> R> {
fields: (a, b)
}.ptr));
}
inform(destroy::<$($var,)* R> as u32);
<&mut Self>::describe();
}
}
#[allow(non_snake_case, unused_parens)]
impl<T, $($var,)* R> WasmClosureFnOnce<($($var),*), R> for T
where T: 'static + FnOnce($($var),*) -> R,
$($var: FromWasmAbi + 'static,)*
R: ReturnWasmAbi + 'static
{
type FnMut = dyn FnMut($($var),*) -> R;
fn into_fn_mut(self) -> Box<Self::FnMut> {
let mut me = Some(self);
Box::new(move |$($var),*| {
let me = me.take().expect_throw("FnOnce called more than once");
me($($var),*)
})
}
fn into_js_function(self) -> JsValue {
use std::rc::Rc;
use crate::__rt::WasmRefCell;
let mut me = Some(self);
let rc1 = Rc::new(WasmRefCell::new(None));
let rc2 = rc1.clone();
let closure = Closure::wrap(Box::new(move |$($var),*| {
let me = me.take().expect_throw("FnOnce called more than once");
let result = me($($var),*);
debug_assert_eq!(Rc::strong_count(&rc2), 1);
let option_closure = rc2.borrow_mut().take();
debug_assert!(option_closure.is_some());
drop(option_closure);
result
}) as Box<dyn FnMut($($var),*) -> R>);
let js_val = closure.as_ref().clone();
*rc1.borrow_mut() = Some(closure);
debug_assert_eq!(Rc::strong_count(&rc1), 2);
drop(rc1);
js_val
}
}
)*)
}
doit! {
()
(A)
(A B)
(A B C)
(A B C D)
(A B C D E)
(A B C D E F)
(A B C D E F G)
(A B C D E F G H)
}
unsafe impl<A, R> WasmClosure for dyn Fn(&A) -> R
where
A: RefFromWasmAbi,
R: ReturnWasmAbi + 'static,
{
fn describe() {
#[allow(non_snake_case)]
unsafe extern "C" fn invoke<A: RefFromWasmAbi, R: ReturnWasmAbi>(
a: usize,
b: usize,
arg: <A as RefFromWasmAbi>::Abi,
) -> <R as ReturnWasmAbi>::Abi {
if a == 0 {
throw_str("closure invoked recursively or destroyed already");
}
let ret = {
let f: *const dyn Fn(&A) -> R = FatPtr { fields: (a, b) }.ptr;
let arg = <A as RefFromWasmAbi>::ref_from_abi(arg);
(*f)(&*arg)
};
ret.return_abi()
}
inform(invoke::<A, R> as u32);
unsafe extern "C" fn destroy<A: RefFromWasmAbi, R: ReturnWasmAbi>(a: usize, b: usize) {
if a == 0 {
return;
}
drop(Box::from_raw(
FatPtr::<dyn Fn(&A) -> R> { fields: (a, b) }.ptr,
));
}
inform(destroy::<A, R> as u32);
<&Self>::describe();
}
}
unsafe impl<A, R> WasmClosure for dyn FnMut(&A) -> R
where
A: RefFromWasmAbi,
R: ReturnWasmAbi + 'static,
{
fn describe() {
#[allow(non_snake_case)]
unsafe extern "C" fn invoke<A: RefFromWasmAbi, R: ReturnWasmAbi>(
a: usize,
b: usize,
arg: <A as RefFromWasmAbi>::Abi,
) -> <R as ReturnWasmAbi>::Abi {
if a == 0 {
throw_str("closure invoked recursively or destroyed already");
}
let ret = {
let f: *const dyn FnMut(&A) -> R = FatPtr { fields: (a, b) }.ptr;
let f = f as *mut dyn FnMut(&A) -> R;
let arg = <A as RefFromWasmAbi>::ref_from_abi(arg);
(*f)(&*arg)
};
ret.return_abi()
}
inform(invoke::<A, R> as u32);
unsafe extern "C" fn destroy<A: RefFromWasmAbi, R: ReturnWasmAbi>(a: usize, b: usize) {
if a == 0 {
return;
}
drop(Box::from_raw(
FatPtr::<dyn FnMut(&A) -> R> { fields: (a, b) }.ptr,
));
}
inform(destroy::<A, R> as u32);
<&mut Self>::describe();
}
}
#[allow(non_snake_case)]
impl<T, A, R> WasmClosureFnOnce<(&A,), R> for T
where
T: 'static + FnOnce(&A) -> R,
A: RefFromWasmAbi + 'static,
R: ReturnWasmAbi + 'static,
{
type FnMut = dyn FnMut(&A) -> R;
fn into_fn_mut(self) -> Box<Self::FnMut> {
let mut me = Some(self);
Box::new(move |arg| {
let me = me.take().expect_throw("FnOnce called more than once");
me(arg)
})
}
fn into_js_function(self) -> JsValue {
use crate::__rt::WasmRefCell;
use std::rc::Rc;
let mut me = Some(self);
let rc1 = Rc::new(WasmRefCell::new(None));
let rc2 = rc1.clone();
let closure = Closure::wrap(Box::new(move |arg: &A| {
let me = me.take().expect_throw("FnOnce called more than once");
let result = me(arg);
debug_assert_eq!(Rc::strong_count(&rc2), 1);
let option_closure = rc2.borrow_mut().take();
debug_assert!(option_closure.is_some());
drop(option_closure);
result
}) as Box<dyn FnMut(&A) -> R>);
let js_val = closure.as_ref().clone();
*rc1.borrow_mut() = Some(closure);
debug_assert_eq!(Rc::strong_count(&rc1), 2);
drop(rc1);
js_val
}
}