Skip to content

Commit

Permalink
Disable Automatic Activation of externref
Browse files Browse the repository at this point in the history
Starting with Rust 1.82 `reference-types` is always present in the
`target_features` section. The problem is that JS bundlers like Webpack
can't currently handle the `reference-types` proposal. So for now there
should not be any automatic detection of the feature and instead it
should explicitly be enabled by the user.
  • Loading branch information
CryZe committed Oct 19, 2024
1 parent e0a5357 commit 37502b9
Show file tree
Hide file tree
Showing 21 changed files with 56 additions and 104 deletions.
16 changes: 11 additions & 5 deletions crates/cli-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,17 @@ impl Bindgen {
.context("failed getting Wasm module")?,
};

// Enable reference type transformations if the module is already using it.
if let Ok(true) = wasm_bindgen_wasm_conventions::target_feature(&module, "reference-types")
{
self.externref = true;
}
// FIXME: Starting with Rust 1.82 `reference-types` is always present
// in the `target_features` section. The problem is that JS bundlers
// like Webpack can't currently handle the `reference-types` proposal.
// So for now there should not be any automatic detection of the feature
// and instead it should explicitly be enabled by the user.

// // Enable reference type transformations if the module is already using it.
// if let Ok(true) = wasm_bindgen_wasm_conventions::target_feature(&module, "reference-types")
// {
// self.externref = true;
// }

// Enable multivalue transformations if the module is already using it.
if let Ok(true) = wasm_bindgen_wasm_conventions::target_feature(&module, "multivalue") {
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/tests/reference/add.wat
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
(export "memory" (memory 0))
(export "add_u32" (func $add_u32))
(export "add_i32" (func $add_i32))
(@custom "target_features" (after code) "\02+\0fmutable-globals+\08sign-ext")
(@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext")
)

2 changes: 1 addition & 1 deletion crates/cli/tests/reference/anyref-empty.wat
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
(export "memory" (memory 0))
(export "__wbindgen_export_0" (table 0))
(export "__wbindgen_start" (func 0))
(@custom "target_features" (after export) "\03+\0fmutable-globals+\08sign-ext+\0freference-types")
(@custom "target_features" (after export) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext")
)

22 changes: 3 additions & 19 deletions crates/cli/tests/reference/anyref-import-catch.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,16 @@ function getStringFromWasm0(ptr, len) {
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
}

let cachedDataViewMemory0 = null;

function getDataViewMemory0() {
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
}
return cachedDataViewMemory0;
}

function takeFromExternrefTable0(idx) {
const value = wasm.__wbindgen_export_2.get(idx);
wasm.__externref_table_dealloc(idx);
return value;
}

export function exported() {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.exported(retptr);
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
if (r1) {
throw takeFromExternrefTable0(r0);
}
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
const ret = wasm.exported();
if (ret[1]) {
throw takeFromExternrefTable0(ret[0]);
}
}

Expand Down
18 changes: 8 additions & 10 deletions crates/cli/tests/reference/anyref-import-catch.wat
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
(module $reference_test.wasm
(type (;0;) (func))
(type (;1;) (func (result i32)))
(type (;2;) (func (param i32)))
(type (;3;) (func (param i32) (result i32)))
(type (;2;) (func (result i32 i32)))
(type (;3;) (func (param i32)))
(import "./reference_test_bg.js" "__wbindgen_init_externref_table" (func (;0;) (type 0)))
(func $exported (;1;) (type 2) (param i32))
(func $__externref_table_dealloc (;2;) (type 2) (param i32))
(func $__externref_table_alloc (;3;) (type 1) (result i32))
(func $__wbindgen_exn_store (;4;) (type 2) (param i32))
(func $__wbindgen_add_to_stack_pointer (;5;) (type 3) (param i32) (result i32))
(func $__externref_table_dealloc (;1;) (type 3) (param i32))
(func $__externref_table_alloc (;2;) (type 1) (result i32))
(func $__wbindgen_exn_store (;3;) (type 3) (param i32))
(func $"exported multivalue shim" (;4;) (type 2) (result i32 i32))
(table (;0;) 128 externref)
(memory (;0;) 17)
(export "memory" (memory 0))
(export "exported" (func $exported))
(export "exported" (func $"exported multivalue shim"))
(export "__wbindgen_exn_store" (func $__wbindgen_exn_store))
(export "__externref_table_alloc" (func $__externref_table_alloc))
(export "__wbindgen_export_2" (table 0))
(export "__wbindgen_add_to_stack_pointer" (func $__wbindgen_add_to_stack_pointer))
(export "__externref_table_dealloc" (func $__externref_table_dealloc))
(export "__wbindgen_start" (func 0))
(@custom "target_features" (after code) "\03+\0fmutable-globals+\08sign-ext+\0freference-types")
(@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext")
)

2 changes: 1 addition & 1 deletion crates/cli/tests/reference/anyref-nop.wat
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
(export "foo" (func $foo))
(export "__wbindgen_export_0" (table 0))
(export "__wbindgen_start" (func 0))
(@custom "target_features" (after code) "\03+\0fmutable-globals+\08sign-ext+\0freference-types")
(@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext")
)

2 changes: 1 addition & 1 deletion crates/cli/tests/reference/builder.wat
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
(export "memory" (memory 0))
(export "__wbg_classbuilder_free" (func $__wbg_classbuilder_free))
(export "classbuilder_builder" (func $classbuilder_builder))
(@custom "target_features" (after code) "\02+\0fmutable-globals+\08sign-ext")
(@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext")
)

2 changes: 1 addition & 1 deletion crates/cli/tests/reference/constructor.wat
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
(export "memory" (memory 0))
(export "__wbg_classconstructor_free" (func $__wbg_classconstructor_free))
(export "classconstructor_new" (func $classconstructor_new))
(@custom "target_features" (after code) "\02+\0fmutable-globals+\08sign-ext")
(@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext")
)

2 changes: 1 addition & 1 deletion crates/cli/tests/reference/empty.wat
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(module $reference_test.wasm
(memory (;0;) 16)
(export "memory" (memory 0))
(@custom "target_features" (after export) "\02+\0fmutable-globals+\08sign-ext")
(@custom "target_features" (after export) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext")
)

2 changes: 1 addition & 1 deletion crates/cli/tests/reference/enums.wat
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
(export "option_enum_echo" (func $option_enum_echo))
(export "get_name" (func $get_name))
(export "option_string_enum_echo" (func $option_string_enum_echo))
(@custom "target_features" (after code) "\02+\0fmutable-globals+\08sign-ext")
(@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext")
)

22 changes: 3 additions & 19 deletions crates/cli/tests/reference/import-catch.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ function handleError(f, args) {
}
}

let cachedDataViewMemory0 = null;

function getDataViewMemory0() {
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
}
return cachedDataViewMemory0;
}

function getObject(idx) { return heap[idx]; }

function dropObject(idx) {
Expand All @@ -51,16 +42,9 @@ function takeObject(idx) {
}

export function exported() {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.exported(retptr);
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
if (r1) {
throw takeObject(r0);
}
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
const ret = wasm.exported();
if (ret[1]) {
throw takeObject(ret[0]);
}
}

Expand Down
14 changes: 6 additions & 8 deletions crates/cli/tests/reference/import-catch.wat
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
(module $reference_test.wasm
(type (;0;) (func (param i32)))
(type (;1;) (func (param i32) (result i32)))
(func $exported (;0;) (type 0) (param i32))
(func $__wbindgen_exn_store (;1;) (type 0) (param i32))
(func $__wbindgen_add_to_stack_pointer (;2;) (type 1) (param i32) (result i32))
(type (;0;) (func (result i32 i32)))
(type (;1;) (func (param i32)))
(func $__wbindgen_exn_store (;0;) (type 1) (param i32))
(func $"exported multivalue shim" (;1;) (type 0) (result i32 i32))
(memory (;0;) 17)
(export "memory" (memory 0))
(export "exported" (func $exported))
(export "exported" (func $"exported multivalue shim"))
(export "__wbindgen_exn_store" (func $__wbindgen_exn_store))
(export "__wbindgen_add_to_stack_pointer" (func $__wbindgen_add_to_stack_pointer))
(@custom "target_features" (after code) "\02+\0fmutable-globals+\08sign-ext")
(@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext")
)

2 changes: 1 addition & 1 deletion crates/cli/tests/reference/nop.wat
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
(memory (;0;) 17)
(export "memory" (memory 0))
(export "nop" (func $nop))
(@custom "target_features" (after code) "\02+\0fmutable-globals+\08sign-ext")
(@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext")
)

2 changes: 1 addition & 1 deletion crates/cli/tests/reference/pointers.wat
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
(export "memory" (memory 0))
(export "const_pointer" (func $const_pointer))
(export "mut_pointer" (func $mut_pointer))
(@custom "target_features" (after code) "\02+\0fmutable-globals+\08sign-ext")
(@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext")
)

2 changes: 1 addition & 1 deletion crates/cli/tests/reference/raw.wat
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
(export "__wbg_test_free" (func $__wbg_test_free))
(export "test_test1" (func $test_test1))
(export "test_test2" (func $test_test2))
(@custom "target_features" (after code) "\02+\0fmutable-globals+\08sign-ext")
(@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext")
)

25 changes: 5 additions & 20 deletions crates/cli/tests/reference/result-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ function addHeapObject(obj) {
return idx;
}

let cachedDataViewMemory0 = null;

function getDataViewMemory0() {
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
}
return cachedDataViewMemory0;
}

function getObject(idx) { return heap[idx]; }

function dropObject(idx) {
Expand Down Expand Up @@ -68,23 +59,17 @@ export function exported() {
let deferred2_0;
let deferred2_1;
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.exported(retptr);
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
var ptr1 = r0;
var len1 = r1;
if (r3) {
const ret = wasm.exported();
var ptr1 = ret[0];
var len1 = ret[1];
if (ret[3]) {
ptr1 = 0; len1 = 0;
throw takeObject(r2);
throw takeObject(ret[2]);
}
deferred2_0 = ptr1;
deferred2_1 = len1;
return getStringFromWasm0(ptr1, len1);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
}
}
Expand Down
15 changes: 6 additions & 9 deletions crates/cli/tests/reference/result-string.wat
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
(module $reference_test.wasm
(type (;0;) (func (param i32)))
(type (;1;) (func (param i32) (result i32)))
(type (;2;) (func (param i32 i32 i32)))
(func $exported (;0;) (type 0) (param i32))
(func $__wbindgen_free (;1;) (type 2) (param i32 i32 i32))
(func $__wbindgen_add_to_stack_pointer (;2;) (type 1) (param i32) (result i32))
(type (;0;) (func (result i32 i32 i32 i32)))
(type (;1;) (func (param i32 i32 i32)))
(func $__wbindgen_free (;0;) (type 1) (param i32 i32 i32))
(func $"exported multivalue shim" (;1;) (type 0) (result i32 i32 i32 i32))
(memory (;0;) 17)
(export "memory" (memory 0))
(export "exported" (func $exported))
(export "__wbindgen_add_to_stack_pointer" (func $__wbindgen_add_to_stack_pointer))
(export "exported" (func $"exported multivalue shim"))
(export "__wbindgen_free" (func $__wbindgen_free))
(@custom "target_features" (after code) "\02+\0fmutable-globals+\08sign-ext")
(@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext")
)

2 changes: 1 addition & 1 deletion crates/cli/tests/reference/self-type.wat
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
(export "test_self_Self" (func $test_self_Self))
(export "test_self_ref_Self" (func $test_self_ref_Self))
(export "test_self_ref_mut_Self" (func $test_self_ref_mut_Self))
(@custom "target_features" (after code) "\02+\0fmutable-globals+\08sign-ext")
(@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext")
)

2 changes: 1 addition & 1 deletion crates/cli/tests/reference/skip-jsdoc.wat
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
(export "memory" (memory 0))
(export "docme" (func $docme))
(export "i_has_docs" (func $i_has_docs))
(@custom "target_features" (after code) "\02+\0fmutable-globals+\08sign-ext")
(@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext")
)

2 changes: 1 addition & 1 deletion crates/cli/tests/reference/string-arg.wat
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
(export "foo" (func $foo))
(export "__wbindgen_malloc" (func $__wbindgen_malloc))
(export "__wbindgen_realloc" (func $__wbindgen_realloc))
(@custom "target_features" (after code) "\02+\0fmutable-globals+\08sign-ext")
(@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext")
)

2 changes: 1 addition & 1 deletion crates/cli/tests/reference/web-sys.wat
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
(export "__wbindgen_exn_store" (func $__wbindgen_exn_store))
(export "__wbindgen_malloc" (func $__wbindgen_malloc))
(export "__wbindgen_realloc" (func $__wbindgen_realloc))
(@custom "target_features" (after code) "\02+\0fmutable-globals+\08sign-ext")
(@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext")
)

0 comments on commit 37502b9

Please sign in to comment.