Skip to content

Use isLikeNone to detect null/undefined for string enums #4186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions crates/cli-support/src/js/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,12 +712,11 @@ fn instruction(
} => {
let enum_val = js.pop();
let enum_val_expr = string_enum_to_wasm(name, *invalid, &enum_val);
js.cx.expose_is_like_none();

// e.g. someEnumVal == undefined ? 4 : (string_enum_to_wasm(someEnumVal))
// |
// double equals here in case it's null
// e.g. isLikeNone(someEnumVal) ? 4 : (string_enum_to_wasm(someEnumVal))
js.push(format!(
"{enum_val} == undefined ? {hole} : ({enum_val_expr})"
"isLikeNone({enum_val}) ? {hole} : ({enum_val_expr})"
))
}

Expand Down
2 changes: 1 addition & 1 deletion crates/cli/tests/reference/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function get_name(color) {
* @returns {any | undefined}
*/
export function option_string_enum_echo(color) {
const ret = wasm.option_string_enum_echo(color == undefined ? 4 : ((__wbindgen_enum_ColorName.indexOf(color) + 1 || 4) - 1));
const ret = wasm.option_string_enum_echo(isLikeNone(color) ? 4 : ((__wbindgen_enum_ColorName.indexOf(color) + 1 || 4) - 1));
return __wbindgen_enum_ColorName[ret];
}

Expand Down