1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#![feature(async_closure)]
use wasm_mt::{WasmMt, utils};
use wasm_bindgen::prelude::*;
use js_sys::ArrayBuffer;
use web_sys::TextEncoder;
mod transform;
use transform::swc_transform;
pub fn get_pkg_js_uri() -> String {
let href = utils::run_js("return location.href;").unwrap().as_string().unwrap();
format!("{}wasm-bindgen-test", href)
}
pub async fn create_ab_init(pkg_js_uri: &str) -> Result<ArrayBuffer, JsValue> {
let pkg_js = utils::fetch_as_text(pkg_js_uri).await?;
let pkg_js = pkg_js.replace("import.meta.url", "''");
let pkg_js = swc_transform(&pkg_js).unwrap();
let mut init_js = String::new();
init_js.push_str("
return () => {
const exports = {};
");
init_js.push_str(&pkg_js);
init_js.push_str("
return Object.assign(init, exports);
};
");
let ab_init = utils::u8arr_from_vec(
&TextEncoder::new()?.encode_with_input(&init_js)).buffer();
Ok(ab_init)
}
pub async fn create_mt(pkg_js_uri: &str) -> WasmMt {
let mt = WasmMt::new(&pkg_js_uri).and_init().await.unwrap();
let ab = create_ab_init(&pkg_js_uri).await.unwrap();
mt.set_ab_init(ab);
mt
}