(updated ) by

Error: null pointer passed to rust

Working with WASM and rust and finding this error.

Error: null pointer passed to rust

And a snippet of the code

#[wasm_bindgen]
impl Something {
    pub fn x(a: self) {

    }

    pub fn y(a: self) {

    }
}

With the JavaScript side:

const something = new Something();

something.x();
// null pointer here
something.y();

Well what’s happening is the first method is eating the value x(self) and then y(self) has no pointer.

You can move the method self to a borrowed pointer instead.

#[wasm_bindgen]
impl Something {
    pub fn x(a: &self) {

    }

    pub fn y(a: &self) {

    }
}

Not much direct info about what that was doing so wanted to share. Can be an issue that won’t pop up until you use that interface in JavaScript and use the object methods more than once (to dereference the pointer).