From 99ade4b35756de5b23377b44cd3dfbdaefb6a35c Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Sun, 13 Oct 2024 14:24:02 +0200 Subject: [PATCH 1/3] Only generate Js values for string enums if used --- crates/cli-support/src/js/binding.rs | 4 ++++ crates/cli-support/src/js/mod.rs | 25 ++++++++++++++++++++----- crates/cli/tests/reference/enums.js | 4 ---- crates/cli/tests/reference/web-sys.js | 2 -- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/crates/cli-support/src/js/binding.rs b/crates/cli-support/src/js/binding.rs index 50d993c3f9f..ccbfea09141 100644 --- a/crates/cli-support/src/js/binding.rs +++ b/crates/cli-support/src/js/binding.rs @@ -701,6 +701,7 @@ fn instruction( Instruction::WasmToStringEnum { name } => { let index = js.pop(); + js.cx.expose_string_enum(name); js.push(wasm_to_string_enum(name, &index)) } @@ -709,11 +710,13 @@ fn instruction( // ["a","b","c"][index], the lookup will implicitly return map // the hole to undefined, because OOB indexes will return undefined. let index = js.pop(); + js.cx.expose_string_enum(name); js.push(wasm_to_string_enum(name, &index)) } Instruction::StringEnumToWasm { name, invalid } => { let enum_val = js.pop(); + js.cx.expose_string_enum(name); js.push(string_enum_to_wasm(name, *invalid, &enum_val)) } @@ -723,6 +726,7 @@ fn instruction( hole, } => { let enum_val = js.pop(); + js.cx.expose_string_enum(name); let enum_val_expr = string_enum_to_wasm(name, *invalid, &enum_val); js.cx.expose_is_like_none(); diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 22bd9246e6a..406326d13a6 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -52,6 +52,13 @@ pub struct Context<'a> { /// function signatures, etc. typescript_refs: HashSet, + /// String enums that are used internally by the generated code. + /// + /// This tracks which string enums are used independently from whether their + /// type is used, because users may only use them in a way that doesn't + /// require the type or requires only the type. + used_string_enums: HashSet, + exported_classes: Option>, /// A map of the name of npm dependencies we've loaded so far to the path @@ -114,6 +121,7 @@ impl<'a> Context<'a> { defined_identifiers: Default::default(), wasm_import_definitions: Default::default(), typescript_refs: Default::default(), + used_string_enums: Default::default(), exported_classes: Some(Default::default()), config, threads_enabled: config.threads.is_enabled(module), @@ -3852,15 +3860,22 @@ __wbg_set_wasm(wasm);" self.typescript.push_str(";\n"); } - self.global(&format!( - "const __wbindgen_enum_{name} = [{values}];\n", - name = string_enum.name, - values = variants.join(", ") - )); + if self.used_string_enums.contains(&string_enum.name) { + // only generate the internal string enum array if it's actually used + self.global(&format!( + "const __wbindgen_enum_{name} = [{values}];\n", + name = string_enum.name, + values = variants.join(", ") + )); + } Ok(()) } + fn expose_string_enum(&mut self, string_enum_name: &str) { + self.used_string_enums.insert(string_enum_name.to_string()); + } + fn generate_struct(&mut self, struct_: &AuxStruct) -> Result<(), Error> { let class = require_class(&mut self.exported_classes, &struct_.name); class.comments = format_doc_comments(&struct_.comments, None); diff --git a/crates/cli/tests/reference/enums.js b/crates/cli/tests/reference/enums.js index 0af1ffd6404..dd9d482ce6b 100644 --- a/crates/cli/tests/reference/enums.js +++ b/crates/cli/tests/reference/enums.js @@ -83,10 +83,6 @@ export const ImplicitDiscriminant = Object.freeze({ A:0,"0":"A",B:1,"1":"B",C:42 const __wbindgen_enum_ColorName = ["green", "yellow", "red"]; -const __wbindgen_enum_FooBar = ["foo", "bar"]; - -const __wbindgen_enum_PrivateStringEnum = ["foo", "bar"]; - export function __wbindgen_throw(arg0, arg1) { throw new Error(getStringFromWasm0(arg0, arg1)); }; diff --git a/crates/cli/tests/reference/web-sys.js b/crates/cli/tests/reference/web-sys.js index 7e4cc173aa7..12bc6777d2e 100644 --- a/crates/cli/tests/reference/web-sys.js +++ b/crates/cli/tests/reference/web-sys.js @@ -210,8 +210,6 @@ function handleError(f, args) { const __wbindgen_enum_MediaSourceEnum = ["camera", "screen", "application", "window", "browser", "microphone", "audioCapture", "other"]; -const __wbindgen_enum_MediaSourceReadyState = ["closed", "open", "ended"]; - export function __wbg_new_1cabf49927794f50() { return handleError(function (arg0, arg1) { const ret = new URL(getStringFromWasm0(arg0, arg1)); return addHeapObject(ret); From 86622195b757789cb3339c6fb63d9a61aed17418 Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Sun, 13 Oct 2024 14:30:25 +0200 Subject: [PATCH 2/3] changed comment --- crates/cli-support/src/js/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 406326d13a6..1055cf43905 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -52,7 +52,7 @@ pub struct Context<'a> { /// function signatures, etc. typescript_refs: HashSet, - /// String enums that are used internally by the generated code. + /// String enums that are used internally by the generated bindings. /// /// This tracks which string enums are used independently from whether their /// type is used, because users may only use them in a way that doesn't From 7e2b029ae1fb7bea3cbd0e1072d1cd4285ba4b7a Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Sun, 13 Oct 2024 19:18:17 +0200 Subject: [PATCH 3/3] Updated changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d28bef07beb..1d0fb027cb7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,9 @@ * Fixed methods with `self: &Self` consuming the object. [#4178](https://github.com/rustwasm/wasm-bindgen/pull/4178) +* Fixed unused string enums generating JS values. + [#4193](https://github.com/rustwasm/wasm-bindgen/pull/4193) + -------------------------------------------------------------------------------- ## [0.2.95](https://github.com/rustwasm/wasm-bindgen/compare/0.2.94...0.2.95)