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

    // Required method
    fn class_id() -> &'static ClassId;

    // Provided methods
    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_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";

    fn class_id() -> &'static ClassId {
        static CLASS_ID: ClassId = ClassId::new();
        &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 MyClass {
    fn from_js(ctx: Ctx<'js>, value: Value<'js>) -> Result<Self> {
        MyClass::from_js_obj(ctx, value)
    }
}

Required Associated Constants§

source

const CLASS_NAME: &'static str

The name of a class

Provided Associated Constants§

source

const HAS_PROTO: bool = false

The class has prototype

source

const HAS_STATIC: bool = false

The class has static data

source

const HAS_REFS: bool = false

The class has internal references to JS values

Needed for correct garbage collection

Required Methods§

source

fn class_id() -> &'static ClassId

The reference to class identifier

Safety

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

Provided Methods§

source

fn init_proto<'js>(_ctx: Ctx<'js>, _proto: &Object<'js>) -> Result<()>

The prototype initializer method

source

fn init_static<'js>(_ctx: Ctx<'js>, _static: &Object<'js>) -> Result<()>

The static initializer method

source

fn mark_refs(&self, _marker: &RefsMarker)

Mark internal references to JS values

Should be implemented to work with garbage collector

source

fn into_js_obj<'js>(self, ctx: Ctx<'js>) -> Result<Value<'js>>where Self: Sized,

Convert an instance of class into JS object

This method helps implement IntoJs trait for classes

source

fn from_js_ref<'js>(ctx: Ctx<'js>, value: Value<'js>) -> Result<&'js Self>where Self: Sized,

Get reference from JS object

This method helps implement FromJs trait for classes

source

fn from_js_obj<'js>(ctx: Ctx<'js>, value: Value<'js>) -> Result<Self>where Self: Clone + Sized,

Get an instance of class from JS object

Implementors§