Attribute Macro host_object

Source
#[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 an fn 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 be fn(&self, &mut RuntimeHandle) -> Result<T> where T: IntoValue.

    If the fn name in Rust starts with get_, this prefix will be removed.

  • Add #[host_object(setter)] to designate an fn 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 be fn(&self, &mut RuntimeHandle, value: JsiValue) -> Result<()>.

    If the fn name in Rust starts with set_, 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)].