-
Notifications
You must be signed in to change notification settings - Fork 10
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 #11
Conversation
Thanks for writing this up! This is definitely something I think we need to support in one form or another, and it's great to see progress on this! One of my main questions would be how we're going to implement this all in terms of generated code. It seems to me that when you specify a superclass you typically want to, in methods, be able to access the Would you be able to expand the detailed design with snippets and examples of what the generated code would look like? (both on the Rust and JS side of things) |
Heh yeah these are some of the things I was thinking of! To be clear I haven't thought a huge amount about this feature, so I don't really have all the answers. A good driving use case will help design this, and I figured web components was one which would need access to the actual JS object to do things like modify the DOM, but I could be wrong. I'm not entirely sure how we're going to draw all these connections between parent/child classes. IDs and byte concatenation is one (although I don't know how to do that) but we could possibly just use strings as well. In any case this is where I think it'd be helpful to prototype something that's workable and then we can have that help influence the design of the RFC and such. |
I think it's probably fine to just stick with something that works for now , this is a relatively minor portion of the feature set in this change :) |
@eggyal This was brought to my attention at the Rust/Wasm WG meeting, and just wanted to give a few comments, hope I'm not driving the current discussion away:
// superclass imported from JS
#[wasm_bindgen]
extern "C" {
pub type ImportedParent;
}
#[wasm_bindgen(superclass=ImportedParent)]
pub struct ChildOfImportedParent { ... } If Hope that helps, thanks! 😄 👍 |
Personally, I think the attribute should be called But that's all bikeshedding stuff which should be easy to change. |
@torch2424 @Pauan I agree that I've also fleshed out the "Detailed Explanation" section quite a bit more to reflect the work I've undertaken in rustwasm/wasm-bindgen#1737 (together with further changes not yet pushed). Would very much welcome critique of this design! |
I've moved my earlier comments (to the extent they're still relevant) into the draft RFC where they now have more context and will hopefully make more sense; this also tidies up the conversation on this PR to (hopefully) facilitate wider discussion. |
Are you referring to the JS |
I believe that @alexcrichton was referring to the For the JS object, you are correct that the |
@Pauan, on reflection, it's actually a lot more difficult in an inheritance scenario. The I therefore don't think that post-instantiation wrapping is straightforward for derived objects. Far better, I think, to require that shims for derived objects be instantiated upon construction and live for the duration of the object's lifetime. Of course, this does mean that the current implementation of that I originally thought that a smart pointer holding the One idea could be to inject into exported derived objects a field that holds their prototype instance (which would be the typed Evidently this is a much much much more involved idea than I had thought at the outset! Still working on it... |
@alexcrichton @Pauan Following further learnings, I've completely rewritten the RFC but feel it's now extremely close to a workable solution. I have a little more to do to fully implement this, but very grateful for any & all thoughts at this stage. |
Sorry for taking awhile to get back to this @eggyal, but after reading this over I'm wondering if we can perhaps try to expose some of the lower level building blocks here? There's a lot of stylistic and library-style decisions that |
That's definitely a good idea, @alexcrichton: this proposal turned out to be significantly more complex than I (naively) anticipated at first—and whilst I have got a working implementation, I made a number of choices along the way that with more considered thought would likely have been decided differently. Nevertheless, as you suggested early on, undertaking this implementation greatly improved my understanding of the issues... I shall try to pull out some building blocks as you suggest. |
Any progress on this? |
@Nachasic I've not looked at this for a while, but wasm-bindgen's (then?) current approach:
This proposal addressed these problems by making some changes to the JS/WASM bridge; in particular, the JS shim object becomes instantiated (by calling JS constructors) upon the Rust struct's creation and is maintained until the Rust struct is dropped; furthermore the Rust struct is only accessible through an However this resulted in significant changes to the API, making it somewhat less ergonomic—in particular, you can no longer take ownership of exported types, and must instantiate them using a macro; furthermore, absent the WeakReferences TC39 proposal, shims must be manually freed or memory leaks could ensue. Ultimately, given that this whole proposal is just a stop-gap until WASM interface-types lands and there wasn't a huge amount of interest, I wasn't convinced it was worth pursuing? I did try breaking it down in "building blocks" as @alexcrichton suggested, but I struggle to view this as much more than the one "big" change to shim construction (with everything else being housekeeping required to make that work). |
Rendered
Fixes rustwasm/wasm-bindgen#210 and rustwasm/wasm-bindgen#1721.
This is an early draft, but I'd be really grateful for feedback at this stage—not sure whether I'm detailing it correctly, nor where exactly to go with some of the blank sections?