Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support as a vanilla polyfill of WebIDL bindings #1690

Merged
merged 1 commit into from
Aug 2, 2019

Conversation

alexcrichton
Copy link
Contributor

This commit adds support to wasm-bindgen to be a drop-in polyfill for
the WebIDL bindings proposal. Lots of internal refactoring has happened
previously to wasm-bindgen to make this possible, so this actually
ends up being a very small PR!

Most of wasm-bindgen is geared towards Rust-specific types and
Rust-specific support, but with the advent of WebIDL bindings this is a
standard way for a WebAssembly module to communicate its intended
interface in terms of higher level types. This PR allows wasm-bindgen
to be a polyfill for any WebAssembly module that has a valid WebIDL
bindings section, regardless of its producer. A standard WebIDL bindings
section is recognized in any input wasm module and that is slurped up
into wasm-bindgen's own internal data structures to get processed in the
same way that all Rust imports/exports are already processed.

The workflow for wasm-bindgen looks the same way that it does in Rust
today. You'd execute wasm-bindgen path/to/foo.wasm --out-dir . which
would output a new wasm file and a JS shim with the desired interface,
and the new wasm file would be suitable for loading in MVP
implementations of WebAssembly.

Note that this isn't super thoroughly tested, so there's likely still
some lingering assumptions that wasm-bindgen makes (such as
__wbindgen_malloc and others) which will need to be patched in the
future, but the intention of this commit is to start us down a road of
becoming a drop-in polyfill for WebIDL bindings, regardless of the
source. Also note that there's not actually any producer (AFAIK) of a
WebIDL bindings custom section, so it'd be that much harder to write
tests to do so!

This commit adds support to `wasm-bindgen` to be a drop-in polyfill for
the WebIDL bindings proposal. Lots of internal refactoring has happened
previously to `wasm-bindgen` to make this possible, so this actually
ends up being a very small PR!

Most of `wasm-bindgen` is geared towards Rust-specific types and
Rust-specific support, but with the advent of WebIDL bindings this is a
standard way for a WebAssembly module to communicate its intended
interface in terms of higher level types. This PR allows `wasm-bindgen`
to be a polyfill for any WebAssembly module that has a valid WebIDL
bindings section, regardless of its producer. A standard WebIDL bindings
section is recognized in any input wasm module and that is slurped up
into wasm-bindgen's own internal data structures to get processed in the
same way that all Rust imports/exports are already processed.

The workflow for `wasm-bindgen` looks the same way that it does in Rust
today. You'd execute `wasm-bindgen path/to/foo.wasm --out-dir .` which
would output a new wasm file and a JS shim with the desired interface,
and the new wasm file would be suitable for loading in MVP
implementations of WebAssembly.

Note that this isn't super thoroughly tested, so there's likely still
some lingering assumptions that `wasm-bindgen` makes (such as
`__wbindgen_malloc` and others) which will need to be patched in the
future, but the intention of this commit is to start us down a road of
becoming a drop-in polyfill for WebIDL bindings, regardless of the
source. Also note that there's not actually any producer (AFAIK) of a
WebIDL bindings custom section, so it'd be that much harder to write
tests to do so!
@alexcrichton
Copy link
Contributor Author

cc @jgravelle-google I'm curious to get your take on this as well, especially how this relates to https://github.com/jgravelle-google/wasm-webidl-polyfill. In theory wasm-bindgen itself could be compiled to wasm/JS (even using #[wasm_bindgen] for that bootstrapping goodness) and this might be a way to have a "for free" WebIDL bindings polyfill that's just a drop-in script on a page

Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All those refactorings are paying off :)

.exports
.iter()
.find(|e| match e.item {
walrus::ExportItem::Function(f) => f == bind.func,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait -- we don't have a better way than iterating over all exports? Filed rustwasm/walrus#113

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@alexcrichton alexcrichton merged commit 732b691 into rustwasm:master Aug 2, 2019
@alexcrichton alexcrichton deleted the webidl-polyfill branch August 2, 2019 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants