From 727ae894f2f8b35f41cf663b31062ce6d2ccae3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8C=85=E5=B8=83=E4=B8=81?= Date: Fri, 4 Oct 2024 21:44:46 +0800 Subject: [PATCH 1/4] Add `TextDecoder.decode` to `IMMUTABLE_SLICE_WHITELIST` --- crates/webidl/src/constants.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/webidl/src/constants.rs b/crates/webidl/src/constants.rs index 779e0d40f96..ad1de70a0fd 100644 --- a/crates/webidl/src/constants.rs +++ b/crates/webidl/src/constants.rs @@ -102,6 +102,8 @@ pub(crate) static IMMUTABLE_SLICE_WHITELIST: Lazy> = Lazy "sign", "unwrapKey", "verify", + // TextDecoder + "decode", ]) }); From 6494fc33c1d29dadf438bab16b2f5e5c0ea3fd1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8C=85=E5=B8=83=E4=B8=81?= Date: Fri, 4 Oct 2024 21:59:51 +0800 Subject: [PATCH 2/4] Update gen_TextDecoder.rs to reflect `IMMUTABLE_SLICE_WHITELIST ` --- crates/web-sys/src/features/gen_TextDecoder.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/web-sys/src/features/gen_TextDecoder.rs b/crates/web-sys/src/features/gen_TextDecoder.rs index c1f55c93ad0..a8f4eb3f54e 100644 --- a/crates/web-sys/src/features/gen_TextDecoder.rs +++ b/crates/web-sys/src/features/gen_TextDecoder.rs @@ -74,7 +74,7 @@ extern "C" { #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder/decode)"] #[doc = ""] #[doc = "*This API requires the following crate features to be activated: `TextDecoder`*"] - pub fn decode_with_u8_array(this: &TextDecoder, input: &mut [u8]) -> Result; + pub fn decode_with_u8_array(this: &TextDecoder, input: &[u8]) -> Result; #[cfg(feature = "TextDecodeOptions")] # [wasm_bindgen (catch , method , structural , js_class = "TextDecoder" , js_name = decode)] #[doc = "The `decode()` method."] @@ -96,7 +96,7 @@ extern "C" { #[doc = "*This API requires the following crate features to be activated: `TextDecodeOptions`, `TextDecoder`*"] pub fn decode_with_u8_array_and_options( this: &TextDecoder, - input: &mut [u8], + input: &[u8], options: &TextDecodeOptions, ) -> Result; } From 108d0d8df2ac748f2541f5879bdb4c5e580c0231 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8C=85=E5=B8=83=E4=B8=81?= Date: Fri, 4 Oct 2024 22:10:55 +0800 Subject: [PATCH 3/4] Fix native test --- crates/cli/tests/reference/raw.js | 48 +++++++++++++++---------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/crates/cli/tests/reference/raw.js b/crates/cli/tests/reference/raw.js index 3432d150366..6b831debb89 100644 --- a/crates/cli/tests/reference/raw.js +++ b/crates/cli/tests/reference/raw.js @@ -6,26 +6,6 @@ export function __wbg_set_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)); -} - const heap = new Array(128).fill(undefined); heap.push(undefined, null, true, false); @@ -45,6 +25,26 @@ function takeObject(idx) { dropObject(idx); return ret; } + +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)); +} /** * @param {number} test * @returns {number} @@ -110,11 +110,11 @@ export function __wbg_test2_39fe629b9aa739cf() { return addHeapObject(ret); }; -export function __wbindgen_throw(arg0, arg1) { - throw new Error(getStringFromWasm0(arg0, arg1)); -}; - export function __wbindgen_object_drop_ref(arg0) { takeObject(arg0); }; +export function __wbindgen_throw(arg0, arg1) { + throw new Error(getStringFromWasm0(arg0, arg1)); +}; + From 73493be8bf24ca50ba7c8a22dcffdb79a3933632 Mon Sep 17 00:00:00 2001 From: daxpedda Date: Mon, 7 Oct 2024 11:02:49 +0200 Subject: [PATCH 4/4] Add changelog entry --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57639df10ed..7ee3a7d10fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,9 @@ * Removed `GamepadAxisMoveEvent`, `GamepadAxisMoveEventInit`, `GamepadButtonEvent`, `GamepadButtonEventInit` and `GamepadServiceTest`, which were seemingly never implemented by any JS environment. [#4134](https://github.com/rustwasm/wasm-bindgen/pull/4134) +* Changed `TextDecoder.decode()` `input` parameter type from `&mut [u8]` to `&[u8]`. + [#4141](https://github.com/rustwasm/wasm-bindgen/pull/4141) + ### Fixed * Fixed linked modules emitting snippet files when not using `--split-linked-modules`.