|
| 1 | +--- |
| 2 | +title: Our Vision for wasm-bindgen |
| 3 | +--- |
| 4 | + |
| 5 | +[The last blog post laid out the Rust and WebAssembly domain working group's |
| 6 | +overall vision for Rust and WebAssembly.][vision-rust-wasm] In this blog post, |
| 7 | +we will dive into the details of `wasm-bindgen`, the future we envision for it, |
| 8 | +and *how you can help us build that future*. |
| 9 | + |
| 10 | +[`wasm-bindgen` facilitates communication between JavaScript and Rust compiled |
| 11 | +to WebAssembly.][wasm-bindgen] It allows you to speak in terms of Rust structs, |
| 12 | +JavaScript classes, strings, etc... instead of only the integers and floats |
| 13 | +supported by WebAssembly's raw calling convention. You only pay for what you |
| 14 | +use, so using `alert` won't pull in unused bindings for `requestAnimationFrame`. |
| 15 | +Additionally, it is designed to support the upcoming ["Host Bindings" |
| 16 | +proposal][host-bindings], which will eliminate the need for any kind of |
| 17 | +JavaScript shim functions between WebAssembly functions and native DOM |
| 18 | +functions. This promises to unlock even-faster-than-JavaScript DOM access, since |
| 19 | +DOM API calls can be validated at the time that the WebAssembly is compiled, and |
| 20 | +won't need to be dynamically checked on every invocation. |
| 21 | + |
| 22 | +## wasm-bindgen is a Shared Foundation |
| 23 | + |
| 24 | +We are building a shared foundation for an ecosystem of Rust crates that target |
| 25 | +JavaScript environments with wasm-bindgen. Sharing a foundation means sharing |
| 26 | +raw `extern` imports. Every library that uses the Web's |
| 27 | +`window.requestAnimationFrame` function or ECMAScript's `Object.freeze` function |
| 28 | +shouldn't need to write the `extern` imports themselves. |
| 29 | + |
| 30 | +Having bindings to all these APIs already in one place should make it easy for |
| 31 | +people to write really neat libraries like [MPSC channels built on top of the |
| 32 | +`postMessage` API][mpsc] and other utility crates for the Web. |
| 33 | + |
| 34 | +[mpsc]: https://github.com/rustwasm/team/issues/163 |
| 35 | + |
| 36 | +### Sharing ECMAScript Global APIs |
| 37 | + |
| 38 | +The global APIs that are available in every JavaScript environment, as defined |
| 39 | +by the ECMAScript standard, are being made available through the |
| 40 | +`wasm_bindgen::js` module. For example, the `Object.freeze` function is |
| 41 | +available as `wasm_bindgen::js::Object::freeze`. |
| 42 | + |
| 43 | +*But we aren't done yet, and we need more help!* Adding more of these global API |
| 44 | +bindings is a great way to start with `wasm-bindgen` and there is lots of work |
| 45 | +that can be done in concurrent pull requests from various contributors! |
| 46 | + |
| 47 | +**Check out the [ECMAScript global APIs meta issue][js-globals] to help out!** |
| 48 | + |
| 49 | +### Sharing Web APIs |
| 50 | + |
| 51 | +ECMAScript's global APIs aren't the end of the story -- we also need shared |
| 52 | +bindings for the Web's DOM APIs, such as `window.requestAnimationFrame`, |
| 53 | +`Node.prototype.appendChild`, and `WebSocket`. |
| 54 | + |
| 55 | +All of the Web's API types, functions, and methods are specified with |
| 56 | +[WebIDL][webidl], so we are working on a new WebIDL frontend to |
| 57 | +`wasm-bindgen`. When the WebIDL frontend is ready, we plan on taking the |
| 58 | +interface definitions for all the Web APIs from all their standards and |
| 59 | +automatically generating a big `-sys` style crate from them. |
| 60 | + |
| 61 | +What do we mean by a new "frontend"? Recall the simplified description of |
| 62 | +`wasm-bindgen`'s architecture today from the last blog post. It is a procedural |
| 63 | +macro that parses `#[wasm_bindgen]` annotations that describe `extern` |
| 64 | +JavaScript imports and Rust things to export to JavaScript into an abstract |
| 65 | +syntax tree (AST). Then, it walks the AST and produces two results: Rust glue to |
| 66 | +use imported JavaScript things, JavaScript glue to use exported Rust things. We |
| 67 | +call the code responsible for parsing `#[wasm_bindgen]` declarations into the |
| 68 | +AST the "procedural macro frontend". |
| 69 | + |
| 70 | +<style> |
| 71 | +img { |
| 72 | + max-height: 300px; |
| 73 | + max-width: 300px; |
| 74 | +} |
| 75 | +</style> |
| 76 | + |
| 77 | +[](/images/wasm-bindgen-architecture-current.png) |
| 78 | + |
| 79 | +With the WebIDL frontend, we are introducing a new way to create the AST: |
| 80 | +parsing WebIDL and turning its definitions into `wasm-bindgen`'s AST. When using |
| 81 | +the WebIDL frontend, there are no `#[wasm_bindgen]` annotations or procedural |
| 82 | +macros involved anymore, but the rest of the pipeline after the AST is |
| 83 | +constructed is the same. |
| 84 | + |
| 85 | +[](/images/new-wasm-bindgen-architecture.png) |
| 86 | + |
| 87 | +Does this sound cool to you? **Check out [issues labeled "frontend:webidl" in |
| 88 | +the `wasm-bindgen` repository][webidl-issues] to help us build the WebIDL |
| 89 | +frontend!** The WebIDL frontend's code lives in `wasm-bindgen/crates/webidl`, |
| 90 | +and if you have any questions, don't hesitate to ping `fitzgen` in `#rust-wasm` |
| 91 | +on irc.mozilla.org or drop a comment in the relevant issue. |
| 92 | + |
| 93 | +Special shout out to [`@ohanar`][ohanar], who has been doing a wonderful job |
| 94 | +adding support for converting various WebIDL constructs into `wasm-bindgen`'s |
| 95 | +AST! We need more folks like `@ohanar` willing to step up in the same way. 🙏 |
| 96 | + |
| 97 | +[ohanar]: https://github.com/ohanar |
| 98 | +[vision-rust-wasm]: https://rustwasm.github.io/2018/06/25/vision-for-rust-and-wasm.html |
| 99 | +[host-bindings]: https://github.com/WebAssembly/host-bindings/blob/master/proposals/host-bindings/Overview.md |
| 100 | +[wasm-bindgen]: https://github.com/rustwasm/wasm-bindgen |
| 101 | +[readme]: https://github.com/rustwasm/wasm-bindgen/blob/master/README.md |
| 102 | +[design]: https://github.com/rustwasm/wasm-bindgen/blob/master/DESIGN.md |
| 103 | +[contributing]: https://github.com/rustwasm/wasm-bindgen/blob/master/CONTRIBUTING.md |
| 104 | +[webidl-issues]: https://github.com/rustwasm/wasm-bindgen/issues?q=is%3Aissue+is%3Aopen+label%3Afrontend%3Awebidl |
| 105 | +[more-types]: https://github.com/rustwasm/wasm-bindgen/issues?q=is%3Aissue+is%3Aopen+label%3Amore-types |
| 106 | +[typescript-issues]: https://github.com/rustwasm/wasm-bindgen/issues?q=is%3Aissue+is%3Aopen+label%3Afrontend%3Atypescript |
| 107 | +[js-globals]: https://github.com/rustwasm/wasm-bindgen/issues/275 |
| 108 | +[webidl]: https://heycam.github.io/webidl/ |
0 commit comments