Trait rquickjs::ClassDef [−][src]
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
pub const CLASS_NAME: &'static str
[src]
The name of a class
pub const HAS_PROTO: bool
[src]
The class has prototype
pub const HAS_STATIC: bool
[src]
The class has static data
pub const HAS_REFS: bool
[src]
The class has internal references to JS values
Needed for correct garbage collection
Required methods
pub 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.
Provided methods
pub fn init_proto(_ctx: Ctx<'js>, _proto: &Object<'js>) -> Result<(), Error>
[src]
The prototype initializer method
pub fn init_static(_ctx: Ctx<'js>, _static: &Object<'js>) -> Result<(), Error>
[src]
The static initializer method
pub fn mark_refs(&self, _marker: &RefsMarker)
[src]
Mark internal references to JS values
Should be implemented to work with garbage collector
pub fn into_js_obj(self, ctx: Ctx<'js>) -> Result<Value<'js>, Error>
[src]
Convert an instance of class into JS object
This method helps implement IntoJs
trait for classes
pub fn from_js_ref(ctx: Ctx<'js>, value: Value<'js>) -> Result<&'js Self, Error>
[src]
Get reference from JS object
This method helps implement FromJs
trait for classes
pub fn from_js_mut(
ctx: Ctx<'js>,
value: Value<'js>
) -> Result<&'js mut Self, Error>
[src]
ctx: Ctx<'js>,
value: Value<'js>
) -> Result<&'js mut Self, Error>
Get mutable reference from JS object
This method helps implement FromJs
trait for classes
pub fn from_js_obj(ctx: Ctx<'js>, value: Value<'js>) -> Result<Self, Error> where
Self: Clone,
[src]
Self: Clone,
Get an instance of class from JS object