Skip to content

Support symbols for fields and functions of exported structs #4149

Open
@RunDevelopment

Description

@RunDevelopment

Motivation

Certain JS protocols require symbols to identify a certain method or field. E.g. for #4142, we need to use Symbol.iterator to define an iterable object.

Proposed Solution

Support the syntax #[wasm_bindgen(js_name = Symbol.name)] to define use symbols instead of regular (string) identifiers for struct fields and functions. A different syntax to identify symbols may also be used, e.g. @@name.

Whatever syntax is used, I think the syntax should be made exclusive. So current code using js_name = Symbol.name (or whatever syntax is chosen) should fail to compile for non-class member bindings. Example:

#[wasm_bindgen(js_name = Symbol.iterator)] // error
fn foo() { }

#[wasm_bindgen]
struct Foo;

#[wasm_bindgen]
impl Foo {
    #[wasm_bindgen(js_name = Symbol.iterator)] // ok
    pub fn iterator(&self) -> js_sys::Iterator { todo!() }
}

Alternatives

  1. Don't support symbols via #[wasm_bindgen] and rely on reflection. This is quite painful.
  2. Add a new option, e.g. js_symbol, separate from js_name for symbols. I don't think this is a good idea. A symbol is intended to be a unique name, so using js_name makes sense.

Additional Context

I'm willing to make a PR, but want to know whether my proposed solution is acceptable.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions