rustwasm / wasm-bindgen

Facilitating high-level interactions between Wasm modules and JavaScript
https://rustwasm.github.io/docs/wasm-bindgen/
Apache License 2.0
7.84k stars 1.08k forks source link

Fixed methods with `self: &Self` consuming the object #4178

Closed RunDevelopment closed 1 month ago

RunDevelopment commented 1 month ago

fixes #3958

Rust allows type annotations for the self parameter, so it can have many forms. E.g. self, &self, &mut self, self: &Self, self: Self, self: Box<Self>, etc. Wasm bindgen needs to know whether a value is consumed (pass by value) or referenced to generate the correct JS shim.

Previously, the parser only looked at whether the self parameter had an & before self. This works for self, &mut self, and &self, but not for self: &Self. So it previously interpreted self: &Self (a reference) as just self (pass by value), because it ignored any and all type annotations, which caused #3958.

This PR fixes the issue by looking at the type of self instead. Helpfully, syn always gives us a type even for &self. See their docs for more details. So instead of checking whether self has an &, it now checks whether the type of self is a reference type. This is the same approach wasm bindgen uses for all other arguments to figure out whether they are passed by referece or by value.