rust-lang / rust-playground

The Rust Playground
https://play.rust-lang.org/
Apache License 2.0
1.27k stars 207 forks source link

Client-side interactive terminal for WASM #374

Open peterjoel opened 6 years ago

peterjoel commented 6 years ago

It would be cool to be able to write Rust code for a WASM target and then execute it immediately within the web browser. A simple terminal could be made which supports calling methods and viewing the output.

Some background from Lukas Kalbertodt: https://chat.stackoverflow.com/transcript/message/43471308#43471308

Regarding WASM. I think the way to go is certainly wasm32-unknown-unknown. And I think wasm-bindgen is also the way to go. As you already said, a super minimal terminal is already enough. You certainly wouldn't let the wasm thing access everything on the playground. So the only thing you (as the environment) need to provide is a print(&str) function. The user can (and is reminded to do so) add a #[wasm_bindgen] extern { fn print(&str); } to their code. Since we're talking about wasm32-unknown-unknown, the crate is compiled as library anyway. So the playground can decide how the main function would look like. So you could require that there is a global fn main(input: String) or something like that. You would call that function with something the user typed in a text box. Just as minimal example. So it would require some integration with wasm-bindgen, some good diagnostic for telling the user about how the main() should look and that they should add this extern { print } block. Some JS code to load the wasm file from the server. But apart from that, I think this should absolutely be possible.

shepmaster commented 6 years ago

require that there is a global fn main(input: String)

diagnostic for telling the user about how the main() should look

See also #323 which has the idea of "templates"

titaneric commented 3 years ago

Hi, I am interested to implement this feature and start to work on it. This feature could be used in mdBook to write the interactive tutorial for wasm-bindgen, yew and seed framework.

richardanaya commented 3 years ago

Even a very minimalistic js-fiddle like setup that let's us call functions of the wasm with javascript would be huge.

titaneric commented 3 years ago

Update the latest work so far. I already tested on mdBook and rust-playground will work on it too!

https://user-images.githubusercontent.com/12388235/113259166-67c1c500-92ff-11eb-81a3-7d2734fae7f1.mp4