Trait boa_engine::class::Class

source ·
pub trait Class: NativeObject + Sized {
    const NAME: &'static str;
    const LENGTH: usize = 0usize;
    const ATTRIBUTES: Attribute = _;

    // Required methods
    fn init(class: &mut ClassBuilder<'_>) -> JsResult<()>;
    fn data_constructor(
        new_target: &JsValue,
        args: &[JsValue],
        context: &mut Context
    ) -> JsResult<Self>;

    // Provided methods
    fn object_constructor(
        instance: &JsObject,
        args: &[JsValue],
        context: &mut Context
    ) -> JsResult<()> { ... }
    fn construct(
        new_target: &JsValue,
        args: &[JsValue],
        context: &mut Context
    ) -> JsResult<JsObject> { ... }
    fn from_data(data: Self, context: &mut Context) -> JsResult<JsObject> { ... }
}
Expand description

Native class.

See the module-level documentation for more details.

Required Associated Constants§

source

const NAME: &'static str

The binding name of this class.

Provided Associated Constants§

source

const LENGTH: usize = 0usize

The amount of arguments this class’ constructor takes. Default is 0.

source

const ATTRIBUTES: Attribute = _

The property attributes of this class’ constructor in the global object. Default is writable, enumerable, configurable.

Required Methods§

source

fn init(class: &mut ClassBuilder<'_>) -> JsResult<()>

Initializes the properties and methods of this class.

source

fn data_constructor( new_target: &JsValue, args: &[JsValue], context: &mut Context ) -> JsResult<Self>

Creates the internal data for an instance of this class.

Provided Methods§

source

fn object_constructor( instance: &JsObject, args: &[JsValue], context: &mut Context ) -> JsResult<()>

Initializes the properties of the constructed object for an instance of this class.

Useful to initialize additional properties for the constructed object that aren’t stored inside the native data.

source

fn construct( new_target: &JsValue, args: &[JsValue], context: &mut Context ) -> JsResult<JsObject>

Creates a new JsObject with its internal data set to the result of calling Class::data_constructor and Class::object_constructor.

§Errors
  • Throws an error if new_target is undefined.
  • Throws an error if this class is not registered in new_target’s realm. See Context::register_global_class.
Overriding this method could be useful for certain usages, but incorrectly implementing this could lead to weird errors like missing inherited methods or incorrect internal data.
source

fn from_data(data: Self, context: &mut Context) -> JsResult<JsObject>

Constructs an instance of this class from its inner native data.

Note that the default implementation won’t call Class::data_constructor, but it will call Class::object_constructor with no arguments.

§Errors
Overriding this method could be useful for certain usages, but incorrectly implementing this could lead to weird errors like missing inherited methods or incorrect internal data.

Object Safety§

This trait is not object safe.

Implementors§