boa_class

Attribute Macro boa_class 

Source
#[boa_class]
Expand description

boa_class proc macro attribute that applies to an impl XYZ block and add a [boa_engine::JsClass] implementation for it.

It will transform functions in the impl ... block as follow (by default, see below):

  1. fn some_method(&self, ...) -> ... {} will be added as class methods with the name some_method, borrowing the object for the ref. This is dangerous if the function execute/eval JavaScript back (potentially leading to a BorrowError).
  2. fn some_method(&mut self, ...) -> ... {} will be added as class methods, similar to the above but borrowing as mutable at runtime.
  3. fn some_method(...) -> ... {} (no self mention) will be added as a static method.
  4. #[boa(constructor)] fn ...(...) -> Self {} (or returning JsResult<Self>) will be used as the constructor of the class. If no constructor is declared, Default::default() will be used instead. If the Default trait is not defined for the type, an error will happen.
  5. #[boa(getter)]

To change this behaviour, you can use the following attributes on the function declarations:

  1. #[boa(rename = "...")] renames the function in JavaScript with the string.
  2. #[boa(getter)] will declare a getter accessor.
  3. #[boa(setter)] will declare a setter accessor.
  4. #[boa(static)] will declare a static method.
  5. #[boa(method)] will declare a method.
  6. #[boa(constructor)] will declare a constructor.
  7. #[boa(length = 123)] sets the length of the function in JavaScript (ie. its number of arguments accepted).

Multiple of those attributes can be added to a single method.

The top level boa_class supports the following:

  1. #[boa_class(rename = "...")] sets the name of the class in JavaScript.
  2. #[boa(rename_all = "camelCase")] will change the naming scheme of verbatim to using “camelCase” or “none”.

§Warning

This should not be used directly as is, and instead should be used through the embed_module! macro in boa_engine for convenience.