diff --git a/Cargo.toml b/Cargo.toml index 4933e74..8a8b273 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,9 +20,10 @@ path = "src/platform/os/main.rs" required-features = [ "os-target" ] [features] +we_alloc = [] default = [ "os-target" ] os-target = [ "clap", "home", "nix", "sysinfo", "ciborium/default", "tracing-subscriber", "async", "tracing-subscriber/env-filter", "libloading", "ciborium-io", "tracing-core" ] -wasm-target = [ "wasm-bindgen", "console_error_panic_hook", "wasm-bindgen-cli", "web-sys", "web-sys/Worker", "web-sys/Window", "web-sys/WorkerOptions", "web-sys/WorkerType" ] +wasm-target = [ "wasm-bindgen", "console_error_panic_hook", "wasm-bindgen-cli", "web-sys", "web-sys/Worker", "web-sys/Window", "web-sys/WorkerOptions", "web-sys/WorkerType", "we_alloc" ] async = ["tokio/net", "tokio", "tokio/rt-multi-thread", "tokio/io-util"] diff --git a/rust-toolchain.toml b/rust-toolchain.toml index c8f5e77..3bbe5d4 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,5 +1,5 @@ [toolchain] channel = "nightly-2024-07-30" -components = [ "cargo", "rustc-dev" ] +components = [ "cargo", "rustc-dev", "rust-src" ] targets = [ "wasm32-unknown-unknown" ] -profile = "minimal" +profile = "full" diff --git a/src/platform/wasm/mod.rs b/src/platform/wasm/mod.rs index 0c8203c..948d439 100644 --- a/src/platform/wasm/mod.rs +++ b/src/platform/wasm/mod.rs @@ -37,7 +37,6 @@ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; // A function imitating `std::thread::spawn`. // thanks to: https://www.tweag.io/blog/2022-11-24-wasm-threads-and-messages/ pub fn wasm_spawn(f: impl FnOnce() -> MizeResult<()> + Send + 'static) -> MizeResult<()> { - // to get window.mize_worker_url, which is set by some js code, which gets "compiled" by a // bundler, which sets window.mize_worker_url to the url of ./npm_pkg/worker.js @@ -60,6 +59,7 @@ pub fn wasm_spawn(f: impl FnOnce() -> MizeResult<()> + Send + 'static) -> MizeRe let worker_fn: Function = js_sys::Reflect::get(&window_obj, &("mize_worker_fn".into())).unwrap().into(); let worker: Worker = worker_fn.call0(&web_sys::wasm_bindgen::JsValue::NULL).unwrap().into(); + /* let worker_url_href = js_sys::Reflect::get(&worker_url_obj, &("href".into())).unwrap(); let worker_url = worker_url_href.as_string().unwrap(); @@ -82,7 +82,18 @@ pub fn wasm_spawn(f: impl FnOnce() -> MizeResult<()> + Send + 'static) -> MizeRe msg.push(&wasm_bindgen::memory()); // Also send the worker the address of the closure we want to execute. msg.push(&JsValue::from(ptr as u32)); - worker.post_message(&msg); + //worker.post_message(&(msg.into())).expect("post message failed"); + + let js_fn_code = r#" + console.log("hi from js_fn_code", msg) + worker.postMessage(msg) + "#; + let js_fn = Function::new_with_args("worker, msg", js_fn_code); + let res = js_fn.call2( + &web_sys::wasm_bindgen::JsValue::NULL, + &worker, + &msg + ).unwrap(); Ok(()) }