Skip to main content

ClassDef

Struct ClassDef 

Source
pub struct ClassDef {
    pub name: String,
    pub finalizer: Option<HostFinalizer>,
    pub gc_mark: Option<HostGcMark>,
    pub exotic: Option<Rc<dyn Exotic>>,
}
Expand description

What every instance of one host class shares: the engine’s half of JSClassDef.

Shared by Rc with each instance rather than looked up in a table when needed, because the finalizer runs from Drop — at which point there is no interpreter, and often no Context, left to look anything up in.

Fields§

§name: String

The class name (JSClassDef::class_name). Used in diagnostics.

It is deliberately not the Object.prototype.toString tag: the reference answers [object Object] for a host object named Point, and only names its own built-in class ids. Making this the tag would be a visible difference.

§finalizer: Option<HostFinalizer>

Run once when an instance is released (JSClassDef::finalizer).

Called from Drop, so it must not touch the object being destroyed and must not allocate JavaScript values — it is not given an interpreter, which is what makes both mistakes impossible rather than merely discouraged.

§gc_mark: Option<HostGcMark>

Report the JavaScript values an instance’s opaque state points at (JSClassDef::gc_mark).

An instance whose Rust state holds a Gc has to say so here. The collector cannot see inside an opaque pointer, so a hidden handle counts as a reference from outside the heap — which makes the object it holds look like a root, and every garbage cycle closed through it immortal. It is a leak rather than a use-after-free (an invisible reference is still a reference), but it is a leak a host class creates without noticing, which is why the C API makes the callback part of the class definition.

The callback must only report edges: it runs from Trace::trace, which the collector calls while it holds a snapshot of the heap.

§exotic: Option<Rc<dyn Exotic>>

Property behaviour (JSClassDef::exotic).

Implementations§

Source§

impl ClassDef

Source

pub fn new(name: impl Into<String>) -> ClassDef

A class with no behaviour beyond being its own class id.

Source

pub fn with_finalizer( self, finalizer: impl Fn(ClassId, Opaque) + 'static, ) -> ClassDef

Attach the finalizer JSClassDef::finalizer would carry.

Source

pub fn with_gc_mark( self, mark: impl Fn(Opaque, &mut Tracer<'_>) + 'static, ) -> ClassDef

Attach the gc_mark callback.

Source

pub fn with_exotic(self, exotic: Rc<dyn Exotic>) -> ClassDef

Attach the exotic methods.

Trait Implementations§

Source§

impl Debug for ClassDef

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for ClassDef

§

impl !Send for ClassDef

§

impl !Sync for ClassDef

§

impl !UnwindSafe for ClassDef

§

impl Forget for ClassDef

§

impl Freeze for ClassDef
where *const u8 is !null: Freeze, PhantomData<u8>: Freeze, *const dyn Fn(ClassId, Opaque) is !null: Freeze, PhantomData<dyn Fn(ClassId, Opaque)>: Freeze, *const dyn Fn(Opaque, &mut Tracer<'_>) is !null: Freeze, PhantomData<dyn Fn(Opaque, &mut Tracer<'_>)>: Freeze, *const RcInner<dyn Exotic> is !null: Freeze, PhantomData<RcInner<dyn Exotic>>: Freeze,

§

impl Move for ClassDef

§

impl Unpin for ClassDef
where *const u8 is !null: Unpin, Box<dyn Fn(ClassId, Opaque)>: Unpin, Box<dyn Fn(Opaque, &mut Tracer<'_>)>: Unpin, Rc<dyn Exotic>: Unpin,

§

impl UnsafeUnpin for ClassDef
where *const u8 is !null: UnsafeUnpin, PhantomData<u8>: UnsafeUnpin, *const dyn Fn(ClassId, Opaque) is !null: UnsafeUnpin, PhantomData<dyn Fn(ClassId, Opaque)>: UnsafeUnpin, *const dyn Fn(Opaque, &mut Tracer<'_>) is !null: UnsafeUnpin, PhantomData<dyn Fn(Opaque, &mut Tracer<'_>)>: UnsafeUnpin, *const RcInner<dyn Exotic> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Exotic>>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.