Skip to content

How to access data descriptor properties of a JS class instance? #2588

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

Closed
Jules-Bertholet opened this issue Jun 14, 2021 · 5 comments
Closed
Labels

Comments

@Jules-Bertholet
Copy link
Contributor

Jules-Bertholet commented Jun 14, 2021

Summary

With wasm-bindgen it is easy to get properties of classes accessed by getters. For example,

#[wasm_bindgen]
extern "C" {
    pub type Foo;

    #[wasm_bindgen(method, getter)]
    fn bar(this: &Foo) -> String;
}

allows using the getter for property bar of Foo from Rust.

But what if the object property is a simple data descriptor, as opposed to having getter or setter methods? How can one access the property from Rust then (other than with Reflect)? I've searched the docs but haven't found anything.

@alexcrichton
Copy link
Contributor

I believe both this syntax and Reflect will work, both translate to a field access of foo.bar in JS

@Jules-Bertholet
Copy link
Contributor Author

@alexcrichton The first syntax only works (at least in my testing) if my_foo.bar is a property assessed with getters and setters, because the generated JS uses getOwnPropertyDescriptor to retrieve the getter/setter. But if bar doesn't have getters and setters, and is just a normal data descriptor (with value and a writable), this doesn't work. And Reflect is unwieldy, and loses type information.

@alexcrichton
Copy link
Contributor

You may need to add structural to the attribute, but I thought that was the default nowadays...

@Jules-Bertholet
Copy link
Contributor Author

You may need to add structural to the attribute, but I thought that was the default nowadays...

Ah, thanks! I had added final—the object I was using was frozen, so I thought final would suit my usage... Maybe the documentation in this area should be improved?

@alexcrichton
Copy link
Contributor

Seems reasonable to me!

alexcrichton pushed a commit that referenced this issue Jun 17, 2021
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