rust-playground icon indicating copy to clipboard operation
rust-playground copied to clipboard

Client-side interactive terminal for WASM

Open peterjoel opened this issue 5 years ago • 4 comments

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.

peterjoel avatar Aug 03 '18 13:08 peterjoel

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"

shepmaster avatar Aug 03 '18 13:08 shepmaster

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.

titaneric avatar Mar 25 '21 06:03 titaneric

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

richardanaya avatar Mar 30 '21 05:03 richardanaya

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

titaneric avatar Apr 01 '21 07:04 titaneric