-
Notifications
You must be signed in to change notification settings - Fork 108
feat: add inheritance support for Rust classes #587
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
base: master
Are you sure you want to change the base?
Conversation
Closes DelSkayn#116 (partly), since extending JS classes is excluded from this MR as it requires significant refactoring of class methods.
|
Not a bad addition though the primary usecase is to extend rust from a js class to avoid the shenanigans we currently have to do to create classes like Buffer. Any solution we merge will need to support that. Also from a larger perspective I dont like the vtable design we currently have with a unique class ID for all rust classes. I would much rather we find a way to use the engine directly, we switched to this system as a patch to avoid clashes of class ID but nobody is really happy with the solution. |
|
Thanks for the feedback. To give some context on my motivation: I am currently working on a DOM-like library, so implementing Rust-to-Rust inheritance (e.g., Node -> Element -> HTMLElement) was my immediate priority. As for the "extending JS classes" requirement, I have a proposal: we could store the Rust struct (as a hidden object) inside the native JS object using a Symbol. This would incur a small extra lookup cost but would solve the binding issue without requiring a massive refactor right now. |
I don't think it has to be this complicated. We just have to call this function: With newTarget being the class we want to extend. I added some comments here: |
As I understand it, passing For QuickJS internal/built-in classes: The opaque field is already occupied by the engine (e.g., |
Closes #116 (partly), extending JS classes is excluded from this MR as it requires significant refactoring of class methods.
Description of changes
class.rsto exposeparent_vtablemethod for inheritance.inherits.rsto defineHasParenttrait.ffi.rsto include parent vtable in theVTablestruct.opaque.rsto handle constructors for inherited classes.extendsattribute.Checklist