Skip to content
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

Fix handling of JS keyword for imported functions, types and statics, and when causing invalid code gen #4329

Merged
merged 19 commits into from
Dec 8, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add error messages for struct and enums
RunDevelopment committed Dec 6, 2024
commit a34c234f9b421aee492ad932187ee3c5c71bfee7
9 changes: 0 additions & 9 deletions crates/cli/tests/reference/keyword.d.ts
Original file line number Diff line number Diff line change
@@ -4,12 +4,3 @@ export function exported(): void;
export function _function(): void;
export function _var(): void;
export function weird_arguments(_new: number, _var: number, _switch: number, _default: number, _arguments: number): void;
export enum switch {
A = 0,
B = 1,
}
export class class {
private constructor();
free(): void;
static new(): class;
}
42 changes: 0 additions & 42 deletions crates/cli/tests/reference/keyword.js
Original file line number Diff line number Diff line change
@@ -47,48 +47,6 @@ export function weird_arguments(_new, _var, _switch, _default, _arguments) {
wasm.weird_arguments(_new, _var, _switch, _default, _arguments);
}

/**
* @enum {0 | 1}
*/
export const switch = Object.freeze({
A: 0, "0": "A",
B: 1, "1": "B",
});

const classFinalization = (typeof FinalizationRegistry === 'undefined')
? { register: () => {}, unregister: () => {} }
: new FinalizationRegistry(ptr => wasm.__wbg_class_free(ptr >>> 0, 1));

export class class {

static __wrap(ptr) {
ptr = ptr >>> 0;
const obj = Object.create(class.prototype);
obj.__wbg_ptr = ptr;
classFinalization.register(obj, obj.__wbg_ptr, obj);
return obj;
}

__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
classFinalization.unregister(this);
return ptr;
}

free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_class_free(ptr, 0);
}
/**
* @returns {class}
*/
static new() {
const ret = wasm.class_new();
return class.__wrap(ret);
}
}

export function __wbg_await_e0a0e75be8b6fef6() {
await();
};
15 changes: 0 additions & 15 deletions crates/cli/tests/reference/keyword.rs
Original file line number Diff line number Diff line change
@@ -55,18 +55,3 @@ pub fn sane_name() {}

#[wasm_bindgen]
pub fn weird_arguments(new: u32, var: u32, r#switch: u32, default: u32, arguments: u32) {}

#[wasm_bindgen]
pub struct class;
#[wasm_bindgen]
impl class {
pub fn new() -> class {
class
}
}

#[wasm_bindgen]
pub enum switch {
A,
B,
}
16 changes: 5 additions & 11 deletions crates/cli/tests/reference/keyword.wat
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
(module $reference_test.wasm
(type (;0;) (func))
(type (;1;) (func (result i32)))
(type (;2;) (func (param i32 i32)))
(type (;3;) (func (param i32 i32 i32 i32 i32)))
(type (;1;) (func (param i32 i32 i32 i32 i32)))
(import "./reference_test_bg.js" "__wbindgen_init_externref_table" (func (;0;) (type 0)))
(func $weird_arguments (;1;) (type 3) (param i32 i32 i32 i32 i32))
(func $__wbg_class_free (;2;) (type 2) (param i32 i32))
(func $class_new (;3;) (type 1) (result i32))
(func $exported (;4;) (type 0))
(func $_function (;5;) (type 0))
(func $_var (;6;) (type 0))
(func $weird_arguments (;1;) (type 1) (param i32 i32 i32 i32 i32))
(func $exported (;2;) (type 0))
(func $_function (;3;) (type 0))
(func $_var (;4;) (type 0))
(table (;0;) 128 externref)
(memory (;0;) 17)
(export "memory" (memory 0))
(export "exported" (func $exported))
(export "_function" (func $_function))
(export "_var" (func $_var))
(export "weird_arguments" (func $weird_arguments))
(export "__wbg_class_free" (func $__wbg_class_free))
(export "class_new" (func $class_new))
(export "__wbindgen_export_0" (table 0))
(export "__wbindgen_start" (func 0))
(@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext")
17 changes: 16 additions & 1 deletion crates/macro-support/src/parser.rs
Original file line number Diff line number Diff line change
@@ -488,6 +488,14 @@ impl ConvertToAst<(&ast::Program, BindgenAttrs)> for &mut syn::ItemStruct {
.js_name()
.map(|s| s.0.to_string())
.unwrap_or(self.ident.unraw().to_string());
if is_js_keyword(&js_name) {
bail_span!(
self.ident,
"struct cannot use the JS keyword `{}` as its name",
js_name
);
}

let is_inspectable = attrs.inspectable().is_some();
let getter_with_clone = attrs.getter_with_clone();
for (i, field) in self.fields.iter_mut().enumerate() {
@@ -1515,11 +1523,18 @@ impl<'a> MacroParse<(&'a mut TokenStream, BindgenAttrs)> for syn::ItemEnum {
}

let generate_typescript = opts.skip_typescript().is_none();
let comments = extract_doc_comments(&self.attrs);
let js_name = opts
.js_name()
.map(|s| s.0)
.map_or_else(|| self.ident.to_string(), |s| s.to_string());
let comments = extract_doc_comments(&self.attrs);
if is_js_keyword(&js_name) {
bail_span!(
self.ident,
"enum cannot use the JS keyword `{}` as its name",
js_name
);
}

opts.check_used();

10 changes: 10 additions & 0 deletions crates/macro/ui-tests/import-keyword.rs
Original file line number Diff line number Diff line change
@@ -53,4 +53,14 @@ extern "C" {
fn function();
}

// Classes and enums

#[wasm_bindgen]
pub struct class;
#[wasm_bindgen]
pub enum switch {
A,
B,
}

fn main() {}
12 changes: 12 additions & 0 deletions crates/macro/ui-tests/import-keyword.stderr
Original file line number Diff line number Diff line change
@@ -39,3 +39,15 @@ error: Empty namespace lists are not allowed.
|
52 | #[wasm_bindgen(js_namespace = [])]
| ^^

error: struct cannot use the JS keyword `class` as its name
--> ui-tests/import-keyword.rs:59:12
|
59 | pub struct class;
| ^^^^^

error: enum cannot use the JS keyword `switch` as its name
--> ui-tests/import-keyword.rs:61:10
|
61 | pub enum switch {
| ^^^^^^