Giter Site home page Giter Site logo

Comments (6)

Redfire75369 avatar Redfire75369 commented on May 25, 2024

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.

Arshia001 avatar Arshia001 commented on May 25, 2024

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.

sagudev avatar sagudev commented on May 25, 2024

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.

Redfire75369 avatar Redfire75369 commented on May 25, 2024

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.

Arshia001 avatar Arshia001 commented on May 25, 2024

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.

Redfire75369 avatar Redfire75369 commented on May 25, 2024

Implemented as part of 091cef5.

#[js_class]
pub struct File {
  blob: Blob, // Superclass
  path: String
}

from spiderfire.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.