Trait rquickjs_core::ClassDef[][src]

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

    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
, { ... } }
This is supported on crate feature classes only.

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)
    }
}

Associated Constants

const CLASS_NAME: &'static str[src]

The name of a class

const HAS_PROTO: bool[src]

The class has prototype

const HAS_STATIC: bool[src]

The class has static data

const HAS_REFS: bool[src]

The class has internal references to JS values

Needed for correct garbage collection

Loading content...

Required methods

unsafe fn class_id() -> &'static mut ClassId[src]

The reference to class identifier

Safety

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

Loading content...

Provided methods

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

The prototype initializer method

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

The static initializer method

fn mark_refs(&self, _marker: &RefsMarker)[src]

Mark internal references to JS values

Should be implemented to work with garbage collector

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

Convert an instance of class into JS object

This method helps implement IntoJs trait for classes

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

Get reference from JS object

This method helps implement FromJs trait for classes

fn from_js_mut<'js>(ctx: Ctx<'js>, value: Value<'js>) -> Result<&'js mut Self> where
    Self: Sized
[src]

Get mutable reference from JS object

This method helps implement FromJs trait for classes

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

Get an instance of class from JS object

Loading content...

Implementors

Loading content...