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

Enable exported Rust structs to specify prototypal inheritance #1721

Open
eggyal opened this issue Aug 14, 2019 · 1 comment
Open

Enable exported Rust structs to specify prototypal inheritance #1721

eggyal opened this issue Aug 14, 2019 · 1 comment

Comments

@eggyal
Copy link

eggyal commented Aug 14, 2019

Motivation

Because the JS classes generated for exported Rust structs do not currently specify a parent class, they are implicit subclasses of Object—that is, their prototype objects are Object.prototype. However, there are many situations where the parent class (prototype object) of one's exported structs should be something other than this default.

Proposed Solution

I propose a new super = Type attribute for the wasm_bindgen macro, as follows:

#[wasm_bindgen]
pub struct Parent { ... }
#[wasm_bindgen(super=Parent)]
pub struct Child { ... }

// the superclass might be imported from JS
#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(extends=Grandparent)]
    pub type ImportedParent;
}
#[wasm_bindgen(super=ImportedParent)]
pub struct ChildOfImportedParent { ... }

// or might be a built-in published by the `js_sys` crate
#[wasm_bindgen(super=js_sys::Date)]
pub struct CustomDate { ... }

// or might be a Web IDL published by the `web_sys` crate
#[wasm_bindgen(super=web_sys::XmlHttpRequest)]
pub struct MyAjaxRequest { ... }

In each case, the generated JavaScript class would include the relevant extends clause, and Rust implementations for the DeRef, AsRef, AsMut and From traits would all be automatically generated.

@alexcrichton
Copy link
Contributor

This is similar to #210 I think as well.

AFAIK no one really knows how best to progress on this at this point in terms of what the design for this would look like, but proposals for such a design (fleshed out as an RFC most likely) would be most welcome!

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

Successfully merging a pull request may close this issue.

2 participants