use js_sys::{Array, Function, JsString, Object};
use std::sync::atomic::{AtomicBool, Ordering};
use wasm_bindgen::prelude::*;
#[doc(hidden)]
pub mod __macrodeps {
pub use gensym::gensym;
pub use std::{concat, stringify};
}
static INIT_DONE: AtomicBool = AtomicBool::new(false);
#[cfg(feature = "auto-init")]
#[wasm_bindgen(start)]
fn main() {
wasm_init();
}
#[wasm_bindgen]
pub fn wasm_init() {
if INIT_DONE.swap(true, Ordering::Relaxed) {
return;
};
let exports: Object = wasm_bindgen::exports().into();
let entries = Object::entries(&exports);
for entry in entries {
let entry = Array::from(&entry);
let name: JsString = entry.get(0).into();
let func: Function = entry.get(1).into();
if name.starts_with("__wasm_init", 0) {
func.apply(&JsValue::undefined(), &Array::new())
.expect("func invocation failed");
}
}
}
#[doc(hidden)]
#[macro_export]
macro_rules! __wasm_init_impl {
($gensym:ident, $($input:tt)*) => {
const _: () = {
#[export_name = $crate::__macrodeps::concat!(
"__wasm_init",
$crate::__macrodeps::stringify!($gensym)
)]
pub extern "C" fn init() {
$($input)*
}
};
};
}
#[macro_export]
macro_rules! wasm_init {
($($input:tt)*) => {
$crate::__macrodeps::gensym! { $crate::__wasm_init_impl! { $($input)* } }
};
}