turtle icon indicating copy to clipboard operation
turtle copied to clipboard

Run in Rust Playground: Compile to WASM

Open sunjay opened this issue 8 years ago • 5 comments

To further our goal of making this library as easy to use as possible, we should consider a way to make the library compile to WASM/JS and then run in the Rust Playground. WASM support in Rust is very infantile right now so this probably won't happen for a while, but it would be really interesting to experiment with this and see what we can get going.

The idea: compile a program using the turtle crate into WASM/JS using target wasm32-unknown-unknown. The playground already supports compiling to WASM. This will require us to conditionally compile and remove a lot of parts that won't be web compatible like any of the threading/process stuff and the window/rendering stuff. All of that should be replaced with some WASM/JS (still written in Rust) that sends commands to something external to the script (i.e. calls some external functions to run the drawing commands and get the state). The WASM/JS can be driven by another JS script that runs in a browser. With this setup, someone who uses extern crate turtle in the playground can have their "▶️ Run" button change to a "🎨 Draw" button. We can generate the WASM/JS (which the playground already supports doing) and run it in their browser with the driver script executing the commands.

Alternative Methods

Do most of what was said above except also execute the entire script on the server (no animation delays obviously). Then send a list of JSON (or something) commands back and run them in the browser. This is probably how this would have worked anyway had this been planned without WASM.

Resources

To Do

  • [ ] Add CI for testing wasm compatibility (will need to determine which tests can be run)

sunjay avatar Dec 02 '17 16:12 sunjay

This is "postponed" because I am working on releasing 1.0.0 and writing the guide. However if someone wants to start thinking about it and experimenting, please do! I will help you out as much as possible! Feel free to leave your ideas or code in the comments on this issue! :)

sunjay avatar Dec 07 '17 18:12 sunjay

I'll take a look at it this weekend. I've already played a little bit with rust and wasm, so I can hopefully make at least some progress on it.

marshallpierce avatar Dec 09 '17 15:12 marshallpierce

Great! Thanks! Let me know if you need any assistance!

sunjay avatar Dec 09 '17 16:12 sunjay

The initial WASM support that was added in #93 has now been removed in #173. We never had a full WASM backend working, and it just wasn't worth investing in trying to make it work now that we are fully async. Given that tokio does not support WASM, there is quite a bit more work to be done now in order to get WASM support working again.

Before we do that work, we should take some time to think about what we actually want WASM support to look like in the turtle crate. The full answer to that will greatly effect how we proceed forward with this in the future. Now that we have several backends, including a multithreaded backend, there is at least some hope that we may be able to do this without having to emulate the two-process architecture we had before.

Getting turtle working in something like the Rust Playground would be a huge step forward for the library and it is definitely something I am excited about pursuing in the future! :grin:

sunjay avatar May 25 '20 05:05 sunjay

Wouldn't this require having a duplicate of the basic functionalities but in a single-threaded context?

PaulDance avatar Oct 09 '20 19:10 PaulDance