Skip to content

Commit 5e98a17

Browse files
Only generate JS values for string enums if used (#4193)
1 parent ec6b042 commit 5e98a17

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
* Fixed methods with `self: &Self` consuming the object.
1717
[#4178](https://github.com/rustwasm/wasm-bindgen/pull/4178)
1818

19+
* Fixed unused string enums generating JS values.
20+
[#4193](https://github.com/rustwasm/wasm-bindgen/pull/4193)
21+
1922
--------------------------------------------------------------------------------
2023

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

crates/cli-support/src/js/binding.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,7 @@ fn instruction(
701701

702702
Instruction::WasmToStringEnum { name } => {
703703
let index = js.pop();
704+
js.cx.expose_string_enum(name);
704705
js.push(wasm_to_string_enum(name, &index))
705706
}
706707

@@ -709,11 +710,13 @@ fn instruction(
709710
// ["a","b","c"][index], the lookup will implicitly return map
710711
// the hole to undefined, because OOB indexes will return undefined.
711712
let index = js.pop();
713+
js.cx.expose_string_enum(name);
712714
js.push(wasm_to_string_enum(name, &index))
713715
}
714716

715717
Instruction::StringEnumToWasm { name, invalid } => {
716718
let enum_val = js.pop();
719+
js.cx.expose_string_enum(name);
717720
js.push(string_enum_to_wasm(name, *invalid, &enum_val))
718721
}
719722

@@ -723,6 +726,7 @@ fn instruction(
723726
hole,
724727
} => {
725728
let enum_val = js.pop();
729+
js.cx.expose_string_enum(name);
726730
let enum_val_expr = string_enum_to_wasm(name, *invalid, &enum_val);
727731
js.cx.expose_is_like_none();
728732

crates/cli-support/src/js/mod.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ pub struct Context<'a> {
5252
/// function signatures, etc.
5353
typescript_refs: HashSet<TsReference>,
5454

55+
/// String enums that are used internally by the generated bindings.
56+
///
57+
/// This tracks which string enums are used independently from whether their
58+
/// type is used, because users may only use them in a way that doesn't
59+
/// require the type or requires only the type.
60+
used_string_enums: HashSet<String>,
61+
5562
exported_classes: Option<BTreeMap<String, ExportedClass>>,
5663

5764
/// A map of the name of npm dependencies we've loaded so far to the path
@@ -114,6 +121,7 @@ impl<'a> Context<'a> {
114121
defined_identifiers: Default::default(),
115122
wasm_import_definitions: Default::default(),
116123
typescript_refs: Default::default(),
124+
used_string_enums: Default::default(),
117125
exported_classes: Some(Default::default()),
118126
config,
119127
threads_enabled: config.threads.is_enabled(module),
@@ -3852,15 +3860,22 @@ __wbg_set_wasm(wasm);"
38523860
self.typescript.push_str(";\n");
38533861
}
38543862

3855-
self.global(&format!(
3856-
"const __wbindgen_enum_{name} = [{values}];\n",
3857-
name = string_enum.name,
3858-
values = variants.join(", ")
3859-
));
3863+
if self.used_string_enums.contains(&string_enum.name) {
3864+
// only generate the internal string enum array if it's actually used
3865+
self.global(&format!(
3866+
"const __wbindgen_enum_{name} = [{values}];\n",
3867+
name = string_enum.name,
3868+
values = variants.join(", ")
3869+
));
3870+
}
38603871

38613872
Ok(())
38623873
}
38633874

3875+
fn expose_string_enum(&mut self, string_enum_name: &str) {
3876+
self.used_string_enums.insert(string_enum_name.to_string());
3877+
}
3878+
38643879
fn generate_struct(&mut self, struct_: &AuxStruct) -> Result<(), Error> {
38653880
let class = require_class(&mut self.exported_classes, &struct_.name);
38663881
class.comments = format_doc_comments(&struct_.comments, None);

crates/cli/tests/reference/enums.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ export const ImplicitDiscriminant = Object.freeze({ A:0,"0":"A",B:1,"1":"B",C:42
8383

8484
const __wbindgen_enum_ColorName = ["green", "yellow", "red"];
8585

86-
const __wbindgen_enum_FooBar = ["foo", "bar"];
87-
88-
const __wbindgen_enum_PrivateStringEnum = ["foo", "bar"];
89-
9086
export function __wbindgen_throw(arg0, arg1) {
9187
throw new Error(getStringFromWasm0(arg0, arg1));
9288
};

crates/cli/tests/reference/web-sys.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,6 @@ function handleError(f, args) {
210210

211211
const __wbindgen_enum_MediaSourceEnum = ["camera", "screen", "application", "window", "browser", "microphone", "audioCapture", "other"];
212212

213-
const __wbindgen_enum_MediaSourceReadyState = ["closed", "open", "ended"];
214-
215213
export function __wbg_new_1cabf49927794f50() { return handleError(function (arg0, arg1) {
216214
const ret = new URL(getStringFromWasm0(arg0, arg1));
217215
return addHeapObject(ret);

0 commit comments

Comments
 (0)