removed dead code for wasm spawn

This commit is contained in:
Sebastian Moser
2024-11-29 15:47:12 +01:00
parent 23721f306f
commit cc9d3b97bc
2 changed files with 3 additions and 76 deletions

View File

@@ -433,8 +433,9 @@ impl Instance {
#[cfg(feature = "os-target")] #[cfg(feature = "os-target")]
thread::spawn(move || to_spawn()); thread::spawn(move || to_spawn());
#[cfg(feature = "wasm-target")] //#[cfg(feature = "wasm-target")]
crate::platform::wasm::wasm_spawn(to_spawn)?; //NOT WELL SUPPORTED
//crate::platform::wasm::wasm_spawn(to_spawn)?;
Ok(()) Ok(())
} }

View File

@@ -36,80 +36,6 @@ macro_rules! console_log {
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; 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<dyn FnOnce()>` is a fat pointer.
// But `Box<Box<dyn FnOnce()>>` 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<dyn FnOnce() -> 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<dyn FnOnce()>) };
(*closure)();
}
pub fn wasm_instance_init(instance: &mut Instance) -> MizeResult<()> { pub fn wasm_instance_init(instance: &mut Instance) -> MizeResult<()> {
console_log!("Hello world from wasm_instance_init!!!!!!!!!!"); console_log!("Hello world from wasm_instance_init!!!!!!!!!!");