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

Method of struct missing with js_name #4154

Open
b-zee opened this issue Oct 9, 2024 · 2 comments
Open

Method of struct missing with js_name #4154

b-zee opened this issue Oct 9, 2024 · 2 comments
Labels

Comments

@b-zee
Copy link

b-zee commented Oct 9, 2024

Describe the Bug

When using the js_name on an enum, a method does not get exported.

Steps to Reproduce

Consider this snippet:

#[wasm_bindgen(js_name = Data)]
pub struct JsData(u8);

#[wasm_bindgen]
impl JsData {
    #[wasm_bindgen(js_name = toString)]
    pub fn to_string(&self) -> String {
        self.0.to_string()
    }
}

Compiled with wasm-pack build --dev --target=web.

Expected Behavior

The Data class to include the toString method.

// In the `.d.ts` file
export class Data {
  free(): void;
/**
* @returns {string}
*/
  toString(): string;
}

Actual Behavior

// In the `.d.ts` file
export class Data {
  free(): void;
}

Additional Context

When the js_name attribute is not used, e.g.:

-#[wasm_bindgen(js_name = Data)]
+#[wasm_bindgen()]
pub struct JsData(u8);

The .d.ts file will include the method:

export class JsData {
  free(): void;
/**
* @returns {string}
*/
  toString(): string;
}
@b-zee b-zee added the bug label Oct 9, 2024
@RunDevelopment
Copy link
Contributor

I think you can use js_class to fix this.

That said, wasm bindgen should still be better here IMO.

@b-zee
Copy link
Author

b-zee commented Oct 9, 2024

I tried adding it both to the method and struct:

warning: unused variable: `js_class`

But I forgot trying to add it to the impl, that works!

So, in short, this works:

#[wasm_bindgen(js_name = Data)]
pub struct JsData(u8);

#[wasm_bindgen(js_class = Data)]
impl JsData {
    #[wasm_bindgen(js_name = toString)]
    pub fn to_string(&self) -> String {
        self.0.to_string()
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants