From 1e3cc7565bf98a6d6d12e7a55fec7723c85d0950 Mon Sep 17 00:00:00 2001 From: daxpedda Date: Thu, 19 Dec 2024 10:21:16 +0100 Subject: [PATCH] Apply transformations even if the shim is empty --- CHANGELOG.md | 3 + .../cli/tests/reference/intrinsic-only.d.ts | 3 + crates/cli/tests/reference/intrinsic-only.js | 57 +++++++++++++++++++ crates/cli/tests/reference/intrinsic-only.rs | 6 ++ crates/cli/tests/reference/intrinsic-only.wat | 17 ++++++ crates/externref-xform/src/lib.rs | 6 -- 6 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 crates/cli/tests/reference/intrinsic-only.d.ts create mode 100644 crates/cli/tests/reference/intrinsic-only.js create mode 100644 crates/cli/tests/reference/intrinsic-only.rs create mode 100644 crates/cli/tests/reference/intrinsic-only.wat diff --git a/CHANGELOG.md b/CHANGELOG.md index 72bc905f84f..f00eaaa3cb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,6 +64,9 @@ * Error if URL in `_REMOTE` can't be parsed. [#4362](https://github.com/rustwasm/wasm-bindgen/pull/4362) +* Internal functions are now removed instead of invalidly imported if they are unused. + [#4366](https://github.com/rustwasm/wasm-bindgen/pull/4366) + -------------------------------------------------------------------------------- ## [0.2.99](https://github.com/rustwasm/wasm-bindgen/compare/0.2.98...0.2.99) diff --git a/crates/cli/tests/reference/intrinsic-only.d.ts b/crates/cli/tests/reference/intrinsic-only.d.ts new file mode 100644 index 00000000000..bb95c9509da --- /dev/null +++ b/crates/cli/tests/reference/intrinsic-only.d.ts @@ -0,0 +1,3 @@ +/* tslint:disable */ +/* eslint-disable */ +export function causes_error(): number; diff --git a/crates/cli/tests/reference/intrinsic-only.js b/crates/cli/tests/reference/intrinsic-only.js new file mode 100644 index 00000000000..26525348f80 --- /dev/null +++ b/crates/cli/tests/reference/intrinsic-only.js @@ -0,0 +1,57 @@ +let wasm; +export function __wbg_set_wasm(val) { + wasm = val; +} + + +const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder; + +let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true }); + +cachedTextDecoder.decode(); + +let cachedUint8ArrayMemory0 = null; + +function getUint8ArrayMemory0() { + if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) { + cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer); + } + return cachedUint8ArrayMemory0; +} + +function getStringFromWasm0(ptr, len) { + ptr = ptr >>> 0; + return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len)); +} + +function takeFromExternrefTable0(idx) { + const value = wasm.__wbindgen_export_0.get(idx); + wasm.__externref_table_dealloc(idx); + return value; +} +/** + * @returns {number} + */ +export function causes_error() { + const ret = wasm.causes_error(); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return ret[0]; +} + +export function __wbindgen_init_externref_table() { + const table = wasm.__wbindgen_export_0; + const offset = table.grow(4); + table.set(0, undefined); + table.set(offset + 0, undefined); + table.set(offset + 1, null); + table.set(offset + 2, true); + table.set(offset + 3, false); + ; +}; + +export function __wbindgen_throw(arg0, arg1) { + throw new Error(getStringFromWasm0(arg0, arg1)); +}; + diff --git a/crates/cli/tests/reference/intrinsic-only.rs b/crates/cli/tests/reference/intrinsic-only.rs new file mode 100644 index 00000000000..2dc264ef2b4 --- /dev/null +++ b/crates/cli/tests/reference/intrinsic-only.rs @@ -0,0 +1,6 @@ +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub fn causes_error() -> Result { + Ok(1.0) +} diff --git a/crates/cli/tests/reference/intrinsic-only.wat b/crates/cli/tests/reference/intrinsic-only.wat new file mode 100644 index 00000000000..8f0b95f09dc --- /dev/null +++ b/crates/cli/tests/reference/intrinsic-only.wat @@ -0,0 +1,17 @@ +(module $reference_test.wasm + (type (;0;) (func)) + (type (;1;) (func (result f64 i32 i32))) + (type (;2;) (func (param i32))) + (import "./reference_test_bg.js" "__wbindgen_init_externref_table" (func (;0;) (type 0))) + (func $__externref_table_dealloc (;1;) (type 2) (param i32)) + (func $"causes_error multivalue shim" (;2;) (type 1) (result f64 i32 i32)) + (table (;0;) 128 externref) + (memory (;0;) 17) + (export "memory" (memory 0)) + (export "causes_error" (func $"causes_error multivalue shim")) + (export "__wbindgen_export_0" (table 0)) + (export "__externref_table_dealloc" (func $__externref_table_dealloc)) + (export "__wbindgen_start" (func 0)) + (@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext") +) + diff --git a/crates/externref-xform/src/lib.rs b/crates/externref-xform/src/lib.rs index 018f2f96716..6d4688bf2b8 100644 --- a/crates/externref-xform/src/lib.rs +++ b/crates/externref-xform/src/lib.rs @@ -302,12 +302,6 @@ impl Transform<'_> { self.process_elements(module)?; assert!(self.cx.new_elements.is_empty()); - // If we didn't actually transform anything, no need to inject or - // rewrite anything from below. - if self.shims.is_empty() { - return Ok(()); - } - // Perform all instruction transformations to rewrite calls between // functions and make sure everything is still hooked up right. self.rewrite_calls(module)?;