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

JS Pointer gets nulled if rust method defined as self: &Self instead of &self #3958

Closed
froggydood opened this issue May 13, 2024 · 0 comments · Fixed by #4178
Closed

JS Pointer gets nulled if rust method defined as self: &Self instead of &self #3958

froggydood opened this issue May 13, 2024 · 0 comments · Fixed by #4178
Labels

Comments

@froggydood
Copy link

Describe the Bug

When defining methods on a rust struct that you want to be exported with wasm-bindgen, if you define the arguments as self: &Self then when called from JS, the JS pointer gets nulled out after calling that method instead of the expected behaviour of the pointer staying valid. If you intead define the arguments as &self then it works as expected.

Steps to Reproduce

Here is a minimal example of the code not working.

The rust code:

#[wasm_bindgen]
pub struct Test {

}

#[wasm_bindgen]
impl Test {
    #[wasm_bindgen(constructor)]
    pub fn new() -> Test {
        Test {}
    }
    pub fn method(self: &Self) -> String {
        "Test".to_string()
    }
    pub fn method_2(self: &Self) -> String {
        "Test_2".to_string()
    }
}

JS Code:

const test = new wasm.Test()
console.log(test)
test.method()
console.log(test)
test.method_2()
console.log(test)

When this is run, the JS code errors at test.method_2() "Error importing index.js: Error: null pointer passed to". We can see from the log that the internal wasm pointer goes from a valid number to 0 after the test.method() call.

If we change the rust code to:

#[wasm_bindgen]
pub struct Test {

}

#[wasm_bindgen]
impl Test {
    #[wasm_bindgen(constructor)]
    pub fn new() -> Test {
        Test {}
    }
    pub fn method(&self -> String {
        "Test".to_string()
    }
    pub fn method_2(&self -> String {
        "Test_2".to_string()
    }
}

(Just replacing self: &Self with &self)
Then both methods pass and no pointers are nulled out.

Expected Behavior

I would expect both pieces of rust code to behave the same, and the first definition shouldn't null pointers after calling each method.

Actual Behavior

The first method nulls the pointer after each method called on the struct.

Additional Context

This was run with wasm-bindgen version of "0.2.84" on Linux Ubuntu. I started with the hello wasm-pack template

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

Successfully merging a pull request may close this issue.

1 participant