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

Ability to make a field on exported sturct as enumerable #2322

Open
ranile opened this issue Oct 1, 2020 · 1 comment
Open

Ability to make a field on exported sturct as enumerable #2322

ranile opened this issue Oct 1, 2020 · 1 comment

Comments

@ranile
Copy link
Collaborator

ranile commented Oct 1, 2020

Motivation

I recently ran into this issue where I exported a struct like (original sturct that was used)

#[wasm_bindgen]
pub struct Foo {
    pub bar: Option<bool>,
    // ...
}

and returned it from a function. When the function was called from javascript, the returned value of the function was destructed like (snippet from here)

validity = {...validity, ...customValidity};

where customValidity is the said returned value, the values of struct's fields were assigned on prototype as getters and setters and were not enumerable. Since destructuring only reads the enumerable proprety keys, those values were not available on the final object, validity in this case.

Proposed Solution

Have a attribute that marks a property as enumerable like such

#[wasm_bindgen]
struct Foo {
  #[wasm_bindgen(enumerable)]
  bar: String,
}

This value will then be set as an enumerable property on the resulting javascript object

Alternatives

Use getters and setters to assign the properties on the object manually but obviously this adds a lot of boilerplate.

Additional Context

console.log of returned value from function:

image
Object.keys of returned value:

image
After destruction:

image`

I hope I made the issue clear. Let me know if any clarification is needed.

@codingstar
Copy link

I ran into the same issue

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

No branches or pull requests

2 participants