#[macro_export]
macro_rules! bind {
(async $file_path:literal::$fn_ident:ident($($arg_ident:ident: $arg_ty:ty),*) -> $ret_ty:ty) => {
paste::paste!(
mod [< __ $fn_ident >] {
use ::wasm_bindgen::prelude::*;
#[wasm_bindgen(module = $file_path)]
extern "C" {
#[wasm_bindgen(catch)]
pub async fn $fn_ident($($arg_ident: JsValue),*) -> ::core::result::Result<JsValue, JsValue>;
}
}
#[inline]
pub async fn $fn_ident($($arg_ident: $arg_ty),*) -> ::core::result::Result<$ret_ty, ::wasm_bindgen::JsValue> {
let ret: ::wasm_bindgen::JsValue = [< __ $fn_ident >]::$fn_ident($(::serde_wasm_bindgen::to_value(&$arg_ident).map_err(|e| {
::wasm_bindgen::JsValue::from_str(&e.to_string())
})?),*).await?;
::serde_wasm_bindgen::from_value(ret).map_err(|e| {
::wasm_bindgen::JsValue::from_str(&e.to_string())
})
}
);
};
(async $file_path:literal::$fn_ident:ident($($arg_ident:ident: $arg_ty:ty),*)) => {
paste::paste!(
mod [< __ $fn_ident >] {
use wasm_bindgen::prelude::*;
#[wasm_bindgen(module = $file_path)]
extern "C" {
#[wasm_bindgen(catch)]
pub async fn $fn_ident($($arg_ident: JsValue),*) -> Result<JsValue, JsValue>;
}
}
#[inline]
pub async fn $fn_ident($($arg_ident: $arg_ty),*) -> Result<(), ::wasm_bindgen::JsValue> {
[< __ $fn_ident >]::$fn_ident($(::serde_wasm_bindgen::to_value(&$arg_ident).map_err(|e| {
::wasm_bindgen::JsValue::from_str(&e.to_string())
})?),*).await?;
Ok(())
}
);
};
(async $file_path:literal::$fn_ident:ident() -> $ret_ty:ty) => {
paste::paste!(
mod [< __ $fn_ident >] {
use ::wasm_bindgen::prelude::*;
#[wasm_bindgen(module = $file_path)]
extern "C" {
#[wasm_bindgen(catch)]
pub async fn $fn_ident() -> Result<JsValue, JsValue>;
}
}
#[inline]
pub async fn $fn_ident() -> ::core::result::Result<$ret_ty, ::wasm_bindgen::JsValue> {
::serde_wasm_bindgen::from_value([< __ $fn_ident >]::$fn_ident().await?).map_err(|e| {
::wasm_bindgen::JsValue::from_str(&e.to_string())
})
}
);
};
(async $file_path:literal::$fn_ident:ident()) => {
paste::paste!(
mod [< __ $fn_ident >] {
use ::wasm_bindgen::prelude::*;
#[wasm_bindgen(module = $file_path)]
extern "C" {
#[wasm_bindgen(catch)]
pub async fn $fn_ident() -> Result<JsValue, JsValue>;
}
}
#[inline]
pub async fn $fn_ident() -> Result<(), JsValue> {
[< __ $fn_ident >]::$fn_ident().await?;
Ok(())
}
);
};
($file_path:literal::$fn_ident:ident($($arg_ident:ident: $arg_ty:ty),*) -> $ret_ty:ty) => {
paste::paste!(
mod [< __ $fn_ident >] {
use ::wasm_bindgen::prelude::*;
#[wasm_bindgen(module = $file_path)]
extern "C" {
#[wasm_bindgen(catch)]
pub fn $fn_ident($($arg_ident: JsValue),*) -> ::core::result::Result<JsValue, JsValue>;
}
}
pub fn $fn_ident($($arg_ident: $arg_ty),*) -> ::core::result::Result<$ret_ty, ::wasm_bindgen::JsValue> {
::serde_wasm_bindgen::from_value([< __ $fn_ident >]::$fn_ident($(::serde_wasm_bindgen::to_value(&$arg_ident).map_err(|e| {
::wasm_bindgen::JsValue::from_str(&e.to_string())
})?),*)?).map_err(|e| {
::wasm_bindgen::JsValue::from_str(&e.to_string())
})
}
);
};
($file_path:literal::$fn_ident:ident($($arg_ident:ident: $arg_ty:ty),*)) => {
paste::paste!(
mod [< __ $fn_ident >] {
use ::wasm_bindgen::prelude::*;
#[wasm_bindgen(module = $file_path)]
extern "C" {
#[wasm_bindgen(catch)]
pub fn $fn_ident($($arg_ident: JsValue),*) -> ::core::result::Result<JsValue, JsValue>;
}
}
pub fn $fn_ident($($arg_ident: $arg_ty),*) -> ::core::result::Result<(), ::wasm_bindgen::JsValue> {
[< __ $fn_ident >]::$fn_ident($(::serde_wasm_bindgen::to_value(&$arg_ident).map_err(|e| {
::wasm_bindgen::JsValue::from_str(&e.to_string())
})?),*)?;
Ok(())
}
);
};
($file_path:literal::$fn_ident:ident() -> $ret_ty:ty) => {
paste::paste!(
mod [< __ $fn_ident >] {
use ::wasm_bindgen::prelude::*;
#[wasm_bindgen(module = $file_path)]
extern "C" {
#[wasm_bindgen(catch)]
pub fn $fn_ident() -> Result<JsValue, JsValue>;
}
}
#[inline]
pub fn $fn_ident() -> Result<$ret_ty, ::wasm_bindgen::JsValue> {
::serde_wasm_bindgen::from_value([< __ $fn_ident >]::$fn_ident()?).map_err(|e| {
::wasm_bindgen::JsValue::from_str(&e.to_string())
})
}
);
};
($file_path:literal::$fn_ident:ident()) => {
paste::paste!(
mod [< __ $fn_ident >] {
use ::wasm_bindgen::prelude::*;
#[wasm_bindgen(module = $file_path)]
extern "C" {
#[wasm_bindgen(catch)]
pub fn $fn_ident() -> Result<JsValue, JsValue>;
}
}
#[inline]
pub fn $fn_ident() -> Result<(), JsValue> {
[< __ $fn_ident >]::$fn_ident()?;
Ok(())
}
);
};
}
#[macro_export]
macro_rules! function {
(move || -> $ret_ty:ty $block:block) => {
::wasm_bindgen::closure::Closure::wrap(::std::boxed::Box::new(move || -> $ret_ty {
$block
}) as ::std::boxed::Box<dyn ::core::ops::FnMut() -> $ret_ty>)
};
(move || $block:block) => {
::wasm_bindgen::closure::Closure::wrap(::std::boxed::Box::new(move || {
$block
}) as ::std::boxed::Box<dyn ::core::ops::FnMut()>)
};
(move |$($arg_ident:ident: $arg_ty:ty),*| -> $ret_ty:ty $block:block) => {
::wasm_bindgen::closure::Closure::wrap(::std::boxed::Box::new(move |$($arg_ident: $arg_ty),*| -> $ret_ty {
$block
}) as ::std::boxed::Box<dyn ::core::ops::FnMut($($arg_ty),*) -> $ret_ty>)
};
(move |$($arg_ident:ident: $arg_ty:ty),*| $block:block) => {
::wasm_bindgen::closure::Closure::wrap(::std::boxed::Box::new(move |$($arg_ident: $arg_ty),*| {
$block
}) as ::std::boxed::Box<dyn ::core::ops::FnMut($($arg_ty),*)>)
};
(|| -> $ret_ty:ty $block:block) => {
::wasm_bindgen::closure::Closure::wrap(::std::boxed::Box::new(|| -> $ret_ty {
$block
}) as ::std::boxed::Box<dyn ::core::ops::FnMut() -> $ret_ty>)
};
(|| $block:block) => {
::wasm_bindgen::closure::Closure::wrap(::std::boxed::Box::new(|| {
$block
}) as ::std::boxed::Box<dyn ::core::ops::FnMut()>)
};
(|$($arg_ident:ident: $arg_ty:ty),*| -> $ret_ty:ty $block:block) => {
::wasm_bindgen::closure::Closure::wrap(::std::boxed::Box::new(|$($arg_ident: $arg_ty),*| -> $ret_ty {
$block
}) as ::std::boxed::Box<dyn ::core::ops::FnMut($($arg_ty),*) -> $ret_ty>)
};
(|$($arg_ident:ident: $arg_ty:ty),*| $block:block) => {
::wasm_bindgen::closure::Closure::wrap(::std::boxed::Box::new(|$($arg_ident: $arg_ty),*| {
$block
}) as ::std::boxed::Box<dyn ::core::ops::FnMut($($arg_ty),*)>)
};
}
#[cfg(test)]
#[allow(unused)]
mod sig_test {
use super::*;
fn success() {
bind!(async "/src/binding.js"::test_0(a: u8, b: u8, c: u8) -> u8);
bind!(async "/src/binding.js"::test_1(a: u8, b: u8, c: u8));
bind!(async "/src/binding.js"::test_2() -> u8);
bind!(async "/src/binding.js"::test_3());
bind!("/src/binding.js"::test_4(a: u8, b: u8, c: u8) -> u8);
bind!("/src/binding.js"::test_5(a: u8, b: u8, c: u8));
bind!("/src/binding.js"::test_6() -> u8);
bind!("/src/binding.js"::test_7());
function!(move || -> u8 {
200
});
function!(move || {
});
function!(move |foo: u8| -> u8 {
foo
});
function!(move |_foo: u8| {
});
function!(|| -> u8 {
200
});
function!(|| {
});
function!(|foo: u8| -> u8 {
foo
});
function!(|_foo: u8| {
});
}
}