From cc9d3b97bc71bb55dae898dc3f495bbeaa76a0c7 Mon Sep 17 00:00:00 2001 From: Sebastian Moser Date: Fri, 29 Nov 2024 15:47:12 +0100 Subject: [PATCH] removed dead code for wasm spawn --- src/core/instance/mod.rs | 5 +-- src/platform/wasm/mod.rs | 74 ---------------------------------------- 2 files changed, 3 insertions(+), 76 deletions(-) diff --git a/src/core/instance/mod.rs b/src/core/instance/mod.rs index c28af95..4186493 100644 --- a/src/core/instance/mod.rs +++ b/src/core/instance/mod.rs @@ -433,8 +433,9 @@ impl Instance { #[cfg(feature = "os-target")] thread::spawn(move || to_spawn()); - #[cfg(feature = "wasm-target")] - crate::platform::wasm::wasm_spawn(to_spawn)?; + //#[cfg(feature = "wasm-target")] + //NOT WELL SUPPORTED + //crate::platform::wasm::wasm_spawn(to_spawn)?; Ok(()) } diff --git a/src/platform/wasm/mod.rs b/src/platform/wasm/mod.rs index 9e731fb..2cf6b2f 100644 --- a/src/platform/wasm/mod.rs +++ b/src/platform/wasm/mod.rs @@ -36,80 +36,6 @@ macro_rules! console_log { 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<()> { - console_log!("no threading in the browser....."); - return Ok(()); - - // 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 - // I hate the js ecosystem.... - // those urls then become base64 data urls containing the js code...... i hate my life - // import worker from './worker.js?worker' ..... does however crate a worker-hash.js file in - // the dist dir...... - // this is then however a function, that creates the worker.... so put that at - // window.mize_worker_fn and call that here - - - //let js_fn_code = r#" - //return window.mize_worker_url - //"#; - //let js_fn = Function::new_no_args(js_fn_code); - //let res = js_fn.call0(&web_sys::wasm_bindgen::JsValue::NULL).unwrap(); - - - let window_obj = JsValue::from(web_sys::window().unwrap()); - 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(); - - let mut worker_options = WorkerOptions::new(); - worker_options.set_type(WorkerType::Module); - let worker = web_sys::Worker::new_with_options( - worker_url.as_str(), - &worker_options - ).map_err(|e| mize_err!("From JsValue: {:?}", e))?; - */ - - // Double-boxing because `dyn FnOnce` is unsized and so `Box` is a fat pointer. - // But `Box>` is just a plain pointer, and since wasm has 32-bit pointers, - // we can cast it to a `u32` and back. - let ptr = Box::into_raw(Box::new(Box::new(f) as Box MizeResult<()>>)); - let msg = js_sys::Array::new(); - // Send the worker a reference to our memory chunk, so it can initialize a wasm module - // using the same memory. - 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.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(()) -} - -#[wasm_bindgen] -// This function is here for `worker.js` to call. -pub fn worker_entry_point(addr: u32) { - // Interpret the address we were given as a pointer to a closure to call. - let closure = unsafe { Box::from_raw(addr as *mut Box) }; - (*closure)(); -} - pub fn wasm_instance_init(instance: &mut Instance) -> MizeResult<()> { console_log!("Hello world from wasm_instance_init!!!!!!!!!!");