-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add a --reference-types
CLI flag
#2257
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
Merged
alexcrichton
merged 1 commit into
rustwasm:master
from
alexcrichton:reference-types-flag
Jul 28, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Support for Reference Types | ||
|
||
WebAssembly recently has gained support for a new value type called `externref`. | ||
Proposed in the [WebAssembly reference types | ||
repo](https://github.com/webassembly/reference-types) this feature of | ||
WebAssembly is hoped to enable more efficient communication between the host | ||
(JS) and the wasm module. This feature removes the need for much of the JS glue | ||
generated by `wasm-bindgen` because it can natively call APIs with JS values. | ||
|
||
For example, this Rust function: | ||
|
||
```rust | ||
#[wasm_bindgen] | ||
pub fn takes_js_value(a: &JsValue) { | ||
// ... | ||
} | ||
``` | ||
|
||
generates this JS glue *without* reference types support: | ||
|
||
```js | ||
const heap = new Array(32).fill(undefined); | ||
|
||
heap.push(undefined, null, true, false); | ||
|
||
let stack_pointer = 32; | ||
|
||
function addBorrowedObject(obj) { | ||
if (stack_pointer == 1) throw new Error('out of js stack'); | ||
heap[--stack_pointer] = obj; | ||
return stack_pointer; | ||
} | ||
|
||
export function takes_js_value(a) { | ||
try { | ||
wasm.takes_js_value(addBorrowedObject(a)); | ||
} finally { | ||
heap[stack_pointer++] = undefined; | ||
} | ||
} | ||
``` | ||
|
||
We can see here how under the hood the JS is managing a table of JS values which | ||
are passed to the wasm binary, so wasm actually only works in indices. If we | ||
pass the `--reference-types` flag to the CLI, however, the generated JS looks like: | ||
|
||
```js | ||
export function takes_js_value(a) { | ||
wasm.takes_js_value(a); | ||
} | ||
``` | ||
|
||
And that's it! The WebAssembly binary takes the JS value directly and manages it | ||
internally. | ||
|
||
Currently this feature is supported in Firefox 79+ and Chrome. Support in other | ||
browsers is likely coming soon! In Node.js this feature is behind the | ||
`--experimental-wasm-anyref` flag, although the support does not currently align | ||
with the upstream specification as of 14.6.0. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey quick question.
How come this isn't
if (stack_pointer == 0)
?What's the issue with assigning an object to
heap[0]
.Just for my own understanding.
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think at some point we were reserving the first slot (index 0) as a sort of null pointer or something like that. I don't believe those plans ever panned out though so this is likely a bug that we can fix nowadays.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sweet thanks for explaining!