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

Support defining getters in Rust #222

Closed
alexcrichton opened this issue Jun 1, 2018 · 5 comments · Fixed by #1440
Closed

Support defining getters in Rust #222

alexcrichton opened this issue Jun 1, 2018 · 5 comments · Fixed by #1440

Comments

@alexcrichton
Copy link
Contributor

Currently when you export a struct in Rust to JS it only exports public methods, but we should enable the ability to define getters and setters for properties too, for example:

#[wasm_bindgen]
struct Foo { /* ... */ }

#[wasm_bindgen]
impl Foo {
    pub fn new() -> Foo { ... }

    #[wasm_bindgen(getter)]
    pub fn field1(&self) -> u32 { ... }
    #[wasm_bindgen(setter)]
    pub fn set_field1(&mut self, f: u32) { ... }

    #[wasm_bindgen(getter = anotherName)]
    pub fn another_name(&self) { ... }
    #[wasm_bindgen(setter = anotherName)]
    pub fn set_another_name(&mut self, param: u32) { ... }
}
@dtysky
Copy link

dtysky commented Dec 24, 2018

Any updating?

@alexcrichton
Copy link
Contributor Author

None yet, but would still be great to implement!

@c410-f3r
Copy link
Contributor

I will work on it. Any tips for this issue, @alexcrichton ?

@alexcrichton
Copy link
Contributor Author

Sure! I think a rough set of steps for implementing this would be:

  1. Update the shared definition of Export between the macro and the CLI. We probably want to delete the is_constructor field and instead include kind: MethodKind<'a>.
  2. Next we'll need to update the macro codegen to produce this. An Export is produced here.
  3. That'll require updating ast::Export defined here. How that's represented internally I'm not sure, but adding some more booleans is fine.
  4. Instances of ast::Export are parsed and constructed here which is where you'd add the attribute parsing code. You'd modify this macro to parse the new attributes.
  5. After all that's working you'd need to update the CLI. You'll naturally get compile errors for generating information since the structure of shared::Export changed. I think for this you'd basically follow the rabbit hole of compiler errors and once they're all ironed out in theory it's a working implementation

If you've got any questions though let me know!

@c410-f3r
Copy link
Contributor

@alexcrichton Oh, this is plenty. Thanks!

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

Successfully merging a pull request may close this issue.

3 participants