pub trait ClassDef {
    const CLASS_NAME: &'static str;
    const HAS_PROTO: bool = false;
    const HAS_STATIC: bool = false;
    const HAS_REFS: bool = false;

    unsafe fn class_id() -> &'static mut ClassId;

    fn init_proto<'js>(_ctx: Ctx<'js>, _proto: &Object<'js>) -> Result<()> { ... }
    fn init_static<'js>(_ctx: Ctx<'js>, _static: &Object<'js>) -> Result<()> { ... }
    fn mark_refs(&self, _marker: &RefsMarker) { ... }
    fn into_js_obj<'js>(self, ctx: Ctx<'js>) -> Result<Value<'js>>
    where
        Self: Sized
, { ... } fn from_js_ref<'js>(ctx: Ctx<'js>, value: Value<'js>) -> Result<&'js Self>
    where
        Self: Sized
, { ... } fn from_js_mut<'js>(
        ctx: Ctx<'js>,
        value: Value<'js>
    ) -> Result<&'js mut Self>
    where
        Self: Sized
, { ... } fn from_js_obj<'js>(ctx: Ctx<'js>, value: Value<'js>) -> Result<Self>
    where
        Self: Clone + Sized
, { ... } }
Available on crate feature classes only.
Expand description

The ES6 class definition trait

This trait helps export rust data types to QuickJS so JS code can interoperate with it as with usual ES6 classes. Usually implementing this trait only is not enough to introduce class. At least IntoJs trait should be implemented for transfering class instances to JS side. Get be able call methods or get access to class properties the FromJs trait should be implemented for class reference and for mutable reference when needed.

NOTE: Usually no need implements this trait manually. Instead you can use class_def macro or bind attribute to export classes to JS in easy way.

#[derive(Clone)]
struct MyClass;

impl ClassDef for MyClass {
    const CLASS_NAME: &'static str = "MyClass";

    unsafe fn class_id() -> &'static mut ClassId {
        static mut CLASS_ID: ClassId = ClassId::new();
        &mut CLASS_ID
    }

    // With prototype
    const HAS_PROTO: bool = true;
    fn init_proto<'js>(ctx: Ctx<'js>, proto: &Object<'js>) -> Result<()> {
        Ok(())
    }

    // With statics
    const HAS_STATIC: bool = true;
    fn init_static<'js>(ctx: Ctx<'js>, ctor: &Object<'js>) -> Result<()> {
        Ok(())
    }

    // With internal references
    const HAS_REFS: bool = true;
    fn mark_refs(&self, marker: &RefsMarker) {
        // marker.mark(&self.some_persistent_value);
    }
}

impl<'js> IntoJs<'js> for MyClass {
    fn into_js(self, ctx: Ctx<'js>) -> Result<Value<'js>> {
        self.into_js_obj(ctx)
    }
}

impl<'js> FromJs<'js> for &'js MyClass {
    fn from_js(ctx: Ctx<'js>, value: Value<'js>) -> Result<Self> {
        MyClass::from_js_ref(ctx, value)
    }
}

impl<'js> FromJs<'js> for &'js mut MyClass {
    fn from_js(ctx: Ctx<'js>, value: Value<'js>) -> Result<Self> {
        MyClass::from_js_mut(ctx, value)
    }
}

impl<'js> FromJs<'js> for MyClass {
    fn from_js(ctx: Ctx<'js>, value: Value<'js>) -> Result<Self> {
        MyClass::from_js_obj(ctx, value)
    }
}

Required Associated Constants

The name of a class

Provided Associated Constants

The class has prototype

The class has static data

The class has internal references to JS values

Needed for correct garbage collection

Required Methods

The reference to class identifier

Safety

This method should return reference to mutable static class id which should be initialized to zero.

Provided Methods

The prototype initializer method

The static initializer method

Mark internal references to JS values

Should be implemented to work with garbage collector

Convert an instance of class into JS object

This method helps implement IntoJs trait for classes

Get reference from JS object

This method helps implement FromJs trait for classes

Get mutable reference from JS object

This method helps implement FromJs trait for classes

Get an instance of class from JS object

Implementors