#[host_object]
Expand description
A macro that makes it easy to define functions and properies on host objects.
struct ExampleHostObject;
#[host_object]
impl ExampleHostObject {
pub fn time(&self, _rt: &mut RuntimeHandle) -> anyhow::Result<i64> {
Ok(3200)
}
}
It is used on impl
blocks and on the functions within the impl
block.
-
Add
#[host_object(getter)]
to designate anfn
as a getter. This will allow JavaScript code to read the member like a property instead of having to call it like a function. Getters should befn(&self, &mut RuntimeHandle) -> Result<T>
whereT: IntoValue
.If the
fn
name in Rust starts withget_
, this prefix will be removed. -
Add
#[host_object(setter)]
to designate anfn
as a setter. This will allow JavaScript code to write the like a property instead of having to call it like a function. Setters should befn(&self, &mut RuntimeHandle, value: JsiValue) -> Result<()>
.If the
fn
name in Rust starts withset_
, this prefix will be removed. -
All other methods will appear as methods on the host object in JavaScript. The first two arguments should be
&self
and a&mut Runtime
.
By default, all member names in a host_object
block are converted from
snake_case
to camelCase
to give them idiomatic names in JavaScript. To
override this, you can set a name with #[host_object(method as custom_method_name)]
or #[host_object(getter as custom_prop_name)]
.