HostFunctions

Struct HostFunctions 

Source
pub struct HostFunctions;
Expand description

Host functions that plugins can import and call.

These functions are implemented by the Orbis runtime and available to all plugins. Plugins should declare these in an unsafe extern "C" block:

unsafe extern "C" {
    fn log(level: i32, ptr: i32, len: i32);
    fn state_get(key_ptr: i32, key_len: i32) -> i32;
    fn state_set(key_ptr: i32, key_len: i32, value_ptr: i32, value_len: i32) -> i32;
    fn state_remove(key_ptr: i32, key_len: i32) -> i32;
}

Implementations§

Source§

impl HostFunctions

Source

pub const LOG: &'static str = "log"

Log a message from the plugin.

§Parameters
  • level: Log level (0=ERROR, 1=WARN, 2=INFO, 3=DEBUG, 4=TRACE)
  • ptr: Pointer to UTF-8 message bytes (as i32 in WASM)
  • len: Length of message in bytes
§Example
unsafe extern "C" {
    fn log(level: i32, ptr: i32, len: i32);
}

fn log_info(msg: &str) {
    unsafe {
        log(2, msg.as_ptr() as i32, msg.len() as i32);
    }
}
Source

pub const STATE_GET: &'static str = "state_get"

Get a value from plugin state.

§Parameters
  • key_ptr: Pointer to UTF-8 key bytes (as i32 in WASM)
  • key_len: Length of key in bytes
§Returns

Pointer to JSON-serialized value (with 4-byte length prefix), or 0 if key not found.

§Example
unsafe extern "C" {
    fn state_get(key_ptr: i32, key_len: i32) -> i32;
}

fn get_counter() -> Option<i64> {
    let key = "counter";
    let ptr = unsafe {
        state_get(key.as_ptr() as i32, key.len() as i32)
    };
     
    if ptr == 0 {
        return None;
    }
     
    // Read length and data, deserialize JSON
    // ...
    Some(0)
}
Source

pub const STATE_SET: &'static str = "state_set"

Set a value in plugin state.

§Parameters
  • key_ptr: Pointer to UTF-8 key bytes (as i32 in WASM)
  • key_len: Length of key in bytes
  • value_ptr: Pointer to JSON-serialized value bytes (as i32 in WASM)
  • value_len: Length of value in bytes
§Returns

1 on success, 0 on failure.

§Example
unsafe extern "C" {
    fn state_set(key_ptr: i32, key_len: i32, value_ptr: i32, value_len: i32) -> i32;
}

fn set_counter(value: i64) -> bool {
    let key = "counter";
    let value_json = serde_json::to_string(&value).unwrap();
     
    let result = unsafe {
        state_set(
            key.as_ptr() as i32,
            key.len() as i32,
            value_json.as_ptr() as i32,
            value_json.len() as i32,
        )
    };
     
    result == 1
}
Source

pub const STATE_REMOVE: &'static str = "state_remove"

Remove a value from plugin state.

§Parameters
  • key_ptr: Pointer to UTF-8 key bytes
  • key_len: Length of key in bytes
§Returns

1 on success, 0 on failure.

§Example
unsafe extern "C" {
    fn state_remove(key_ptr: i32, key_len: i32) -> i32;
}

fn clear_counter() -> bool {
    let key = "counter";
    let result = unsafe {
        state_remove(key.as_ptr() as i32, key.len() as i32)
    };
     
    result == 1
}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.