Skip to content

Commit 74353cc

Browse files
authored
Deprecate --reference-types (#4237)
1 parent 37b9aa5 commit 74353cc

File tree

7 files changed

+35
-18
lines changed

7 files changed

+35
-18
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
* Reference type proposal transformations are not applied by default when detecting it in the Wasm module for the bundler target because currently `webpack` doesn't support it.
3232
[#4235](https://github.com/rustwasm/wasm-bindgen/pull/4235)
3333

34+
* Deprecate `--reference-types` in favor of automatic target feature detection.
35+
[#4237](https://github.com/rustwasm/wasm-bindgen/pull/4237)
36+
3437
### Fixed
3538

3639
* Fixed methods with `self: &Self` consuming the object.
@@ -57,6 +60,9 @@
5760
* Fixed invalid TypeScript return types for multivalue signatures.
5861
[#4210](https://github.com/rustwasm/wasm-bindgen/pull/4210)
5962

63+
* Only emit `table.fill` instructions if the bulk-memory proposal is enabled.
64+
[#4237](https://github.com/rustwasm/wasm-bindgen/pull/4237)
65+
6066
--------------------------------------------------------------------------------
6167

6268
## [0.2.95](https://github.com/rustwasm/wasm-bindgen/compare/0.2.94...0.2.95)

crates/cli/src/bin/wasm-bindgen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Options:
4040
--web Deprecated, use `--target web`
4141
--no-modules Deprecated, use `--target no-modules`
4242
--weak-refs Deprecated, is runtime-detected
43-
--reference-types Enable usage of WebAssembly reference types
43+
--reference-types Deprecated, use `-Ctarget-feature=+reference-types`
4444
-V --version Print the version number of wasm-bindgen
4545
4646
Additional documentation: https://rustwasm.github.io/wasm-bindgen/reference/cli.html

crates/externref-xform/src/lib.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ pub struct Context {
5252

5353
// The externref table we'll be using, injected after construction
5454
table: Option<TableId>,
55+
56+
// If the bulk memory proposal is enabled.
57+
bulk_memory: bool,
5558
}
5659

5760
pub struct Meta {
@@ -105,6 +108,11 @@ impl Context {
105108
wasm_bindgen_wasm_conventions::insert_target_feature(module, "reference-types")
106109
.context("failed to parse `target_features` custom section")?;
107110

111+
self.bulk_memory = matches!(
112+
wasm_bindgen_wasm_conventions::target_feature(module, "bulk-memory"),
113+
Ok(true)
114+
);
115+
108116
// Figure out what the maximum index of functions pointers are. We'll
109117
// be adding new entries to the function table later (maybe) so
110118
// precalculate this ahead of time.
@@ -671,11 +679,23 @@ impl Transform<'_> {
671679
// that the table doesn't accidentally hold a strong reference to items
672680
// no longer in use by our Wasm instance.
673681
if externref_stack > 0 {
682+
if self.cx.bulk_memory {
683+
body.local_get(fp)
684+
.ref_null(RefType::Externref)
685+
.i32_const(externref_stack)
686+
.table_fill(self.table);
687+
} else {
688+
for i in 0..externref_stack {
689+
body.local_get(fp);
690+
if i > 0 {
691+
body.i32_const(i).binop(BinaryOp::I32Add);
692+
}
693+
body.ref_null(RefType::Externref);
694+
body.table_set(self.table);
695+
}
696+
}
697+
674698
body.local_get(fp)
675-
.ref_null(RefType::Externref)
676-
.i32_const(externref_stack)
677-
.table_fill(self.table)
678-
.local_get(fp)
679699
.i32_const(externref_stack)
680700
.binop(BinaryOp::I32Add)
681701
.global_set(self.stack_pointer);

crates/externref-xform/tests/anyref-param.wat

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
call $foo
2626
local.get 1
2727
ref.null extern
28-
i32.const 1
29-
table.fill 0
28+
table.set 0
3029
local.get 1
3130
i32.const 1
3231
i32.add

crates/externref-xform/tests/mixed-export.wat

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434
call $a
3535
local.get 5
3636
ref.null extern
37-
i32.const 1
38-
table.fill 0
37+
table.set 0
3938
local.get 5
4039
i32.const 1
4140
i32.add

guide/src/reference/cli.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,6 @@ When generating bundler-compatible code (see the section on [deployment]) this
8484
indicates that the bundled code is always intended to go into a browser so a few
8585
checks for Node.js can be elided.
8686

87-
### `--reference-types`
88-
89-
Enables usage of the [WebAssembly References Types
90-
proposal](https://github.com/webassembly/reference-types) proposal, meaning that
91-
the WebAssembly binary will use `externref` when importing and exporting
92-
functions that work with `JsValue`. For more information see the [documentation
93-
about reference types](./reference-types.md).
94-
9587
### `--omit-default-module-path`
9688

9789
Don't add WebAssembly fallback imports in generated JavaScript.

guide/src/reference/reference-types.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ export function takes_js_value(a) {
4242

4343
We can see here how under the hood the JS is managing a table of JS values which
4444
are passed to the Wasm binary, so Wasm actually only works in indices. If we
45-
pass the `--reference-types` flag to the CLI, however, the generated JS looks like:
45+
compile with `-Ctarget-feature=+reference-types` (by default since Rust v1.82),
46+
however, the generated JS looks like:
4647

4748
```js
4849
export function takes_js_value(a) {

0 commit comments

Comments
 (0)