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: StringThe 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
impl ClassDef
Sourcepub fn new(name: impl Into<String>) -> ClassDef
pub fn new(name: impl Into<String>) -> ClassDef
A class with no behaviour beyond being its own class id.
Sourcepub fn with_finalizer(
self,
finalizer: impl Fn(ClassId, Opaque) + 'static,
) -> ClassDef
pub fn with_finalizer( self, finalizer: impl Fn(ClassId, Opaque) + 'static, ) -> ClassDef
Attach the finalizer JSClassDef::finalizer would carry.
Sourcepub fn with_gc_mark(
self,
mark: impl Fn(Opaque, &mut Tracer<'_>) + 'static,
) -> ClassDef
pub fn with_gc_mark( self, mark: impl Fn(Opaque, &mut Tracer<'_>) + 'static, ) -> ClassDef
Attach the gc_mark callback.
Sourcepub fn with_exotic(self, exotic: Rc<dyn Exotic>) -> ClassDef
pub fn with_exotic(self, exotic: Rc<dyn Exotic>) -> ClassDef
Attach the exotic methods.