Stream: wit-bindgen

Topic: Rust to JS


view this post on Zulip Jack Gilcrest (Jul 05 2023 at 22:22):

Hello, very much a newbie with wasi32 and wasm in general - used to using wasm-pack. Struggling to export rust compiled to wasm32-wasi into javascript. Wondering if I am doing the right thing using wit-bindgen for this purpose?

view this post on Zulip Dan Gohman (Jul 05 2023 at 22:27):

wit-bindgen is part of the picture here, however there are now also higher-level tools that do more of the work for you. cargo-component can produce wasm components written in Rust, and jco can help them run in a JS environment so you can use them from JS.

A Cargo subcommand for creating WebAssembly components based on the component model proposal. - GitHub - bytecodealliance/cargo-component: A Cargo subcommand for creating WebAssembly components bas...
JavaScript tooling for working with WebAssembly Components - GitHub - bytecodealliance/jco: JavaScript tooling for working with WebAssembly Components

view this post on Zulip Dan Gohman (Jul 05 2023 at 22:29):

This video includes a walkthrough of using these tools together.

view this post on Zulip Jack Gilcrest (Jul 05 2023 at 22:34):

awesome! digging in now, tysm

view this post on Zulip Karel Hrkal (kajacx) (Jul 06 2023 at 07:15):

Hello @Jack Gilcrest, to run rust-compiled-to-wasm with wit bindgen, you can check out this example repo at the wit-bindgen branch.

In folder plguin-rust, you have the Rust code that will run in the browser, and in runtime-web-jco you have the JS implementation. The code is very messy becauseI was testing something, but you should be able to read how to put it together. See also the build-and-run-web.sh script.

One note is that this is using wasm32-unknown-unkown target and not wasi, so this might not work for you if you need wasi capabilities.

Various projects related to WASM, each in it's own branch, so that I don't have 50 repositories. - GitHub - kajacx/wasm-playground at wit-bindgen

Last updated: Dec 23 2024 at 13:07 UTC