threading would maybe work

This commit is contained in:
Sebastian Moser
2024-09-19 09:52:39 +02:00
parent e04b955ae0
commit c9c8e9291f
3 changed files with 17 additions and 5 deletions

View File

@@ -20,9 +20,10 @@ path = "src/platform/os/main.rs"
required-features = [ "os-target" ] required-features = [ "os-target" ]
[features] [features]
we_alloc = []
default = [ "os-target" ] default = [ "os-target" ]
os-target = [ "clap", "home", "nix", "sysinfo", "ciborium/default", "tracing-subscriber", "async", "tracing-subscriber/env-filter", "libloading", "ciborium-io", "tracing-core" ] 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"] async = ["tokio/net", "tokio", "tokio/rt-multi-thread", "tokio/io-util"]

View File

@@ -1,5 +1,5 @@
[toolchain] [toolchain]
channel = "nightly-2024-07-30" channel = "nightly-2024-07-30"
components = [ "cargo", "rustc-dev" ] components = [ "cargo", "rustc-dev", "rust-src" ]
targets = [ "wasm32-unknown-unknown" ] targets = [ "wasm32-unknown-unknown" ]
profile = "minimal" profile = "full"

View File

@@ -37,7 +37,6 @@ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
// A function imitating `std::thread::spawn`. // A function imitating `std::thread::spawn`.
// thanks to: https://www.tweag.io/blog/2022-11-24-wasm-threads-and-messages/ // 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<()> { 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 // 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 // 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_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: 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_href = js_sys::Reflect::get(&worker_url_obj, &("href".into())).unwrap();
let worker_url = worker_url_href.as_string().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()); msg.push(&wasm_bindgen::memory());
// Also send the worker the address of the closure we want to execute. // Also send the worker the address of the closure we want to execute.
msg.push(&JsValue::from(ptr as u32)); 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(()) Ok(())
} }