Wasm32, multithreading (co-op is fine)?

Here is the problem:

  1. I am building a web app with Rust/wasm32.

  2. I have a small VM / scripting language implemented in Rust (compiled to wasm32).

  3. I allow users to upload scripts that the VM runs.

  4. Problem: Right now, when this happens, the VM runs the script to completion -- freezing up the UI while the VM is running.

  5. Because I implemented the VM, I can force the VM to "yield" every 1000 steps or so.

  6. I want the UI to remain responsive as the script is running.


Question: What are my options for solving this?

What is the best approach if I want true multi threading in Rust/wasm32 ?

What is the best solution if I am okay with co-op "threading" ?

What other potential solutions should I look into ?

The only way to get true multithreading on the web is to use web workers. The downside to this is that you can't share memory (only copy or pass ownership, similar to Rust's memory management) and all communication has to happen via a built-in messaging system.
However, I think that that would work for your use case.

If you want to go for cooperative multitasking, you have to do something similar to async functions. Take a look at JavaScript generators.