Comments (6)
So far, I haven't enabled inheritance in #[js_class]
, mostly since I don't know what the syntax for that should be like. There's already enablement for that though via ClassDefinition::PARENT_PROTOTYPE_CHAIN_LENGTH
and ClassDefinition::parent_info
.
from spiderfire.
How about:
#[js_class]
pub mod class {
pub struct Derived {
#[ion(superclass)]
pub super_class: Super;
}
}
Since there is no inheritance in rust, the superclass object has to be added in via composition.
However, that does complicate the get_private
implementation, since it'll have to check superclasses as well; otherwise, you can't receieve subclasses as function arguments if I'm correct.
Also, you'll need to go from the derived object to the base object (e.g. for a function that receives Blob
, you also want to be able to give it a File
). However, you don't want to lose the original object, so we'd need something like:
// A trait types will implement (automatically) to let the world know their superclass
// I'm thinking each type implements SubClassOf for the entire prototype chain (not sure how to generate code for it), so Super can't be an associated type
trait SubClassOf<Super> {
fn super(&self) -> &Super;
}
// A struct that lets you receive any subclass of an object as an argument. It should implement FromValue.
struct SubClassInstance<Super>
{
sub: Box<dyn SubClassOf<Super>>, // Has to be a trait object since we don't know the subclass type beforehand
}
impl<Super> SubClassInstance<Super> {
fn super(&self) -> &Super {
self.sub.super()
}
}
from spiderfire.
I do not find relevant docs for servo but there is https://github.com/servo/servo/blob/master/components/script/dom/bindings/inheritance.rs which caries main impl. Inheritance (transmutes) safety is ensured via traits (at least for upcasts). Downcasts uses data present in reflector (from object creation) to know if you can safely transmute.
from spiderfire.
My original idea was to just store subclass and superclass privates in different reserved slots, and then you can individually pick out what you need. That should work fine with tracing, finalisation, etc..
I'm just realising that would be very weird with how &self
is treated in these functions, you'd be forced to manually use #[ion(this)]
and ClassDefinition::get_private
everywhere.
from spiderfire.
That would, indeed, make it difficult to go from a subclass instance to its superclass instance... unless #[js_class]
adds an extra field or two to store info about the superclass instance.
Also, creating instances of derived types would be difficult, since you'd need to create the superclass object separately and pass it through functions provided by ion
, as opposed to just creating a normal rust struct which is what happens right now.
from spiderfire.
Implemented as part of 091cef5.
#[js_class]
pub struct File {
blob: Blob, // Superclass
path: String
}
from spiderfire.
Related Issues (20)
- JavaScript API Documentation
- URLSearchParams in URL Module HOT 1
- TextDecoder and TextEncoder APIs HOT 1
- Improve Console Implementation HOT 1
- Dropping a context segfaults when native async functions have run HOT 10
- Looping over response headers with `for... of` results in a segfault HOT 2
- Error when Values are concurrently rooted HOT 37
- Adding Docker build step to Spiderfire.
- Macro parsing succeeds on invalid input HOT 4
- Race condition in the event loop? HOT 8
- Native inheritance/prototype chaining is not working correctly HOT 1
- Panic when printing a prototype HOT 1
- Using clearInterval from inside the setInterval callback does not clear the interval
- Converting an object with a bad toString implementation to a string crashes SpiderFire HOT 1
- Objects with getters that fail do not trigger an error HOT 1
- Strings with invalid UTF-16 codepoints cause SpiderFire to crash HOT 1
- Opt<T> does not respect passing undefined explicitly HOT 4
- get_private and get_mut_private don't work with subclasses HOT 2
- The event loop doesn't play nice with other async code
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spiderfire.