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
impl HostFunctions
Sourcepub const LOG: &'static str = "log"
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);
}
}Sourcepub const STATE_GET: &'static str = "state_get"
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)
}Sourcepub const STATE_SET: &'static str = "state_set"
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 bytesvalue_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
}Sourcepub const STATE_REMOVE: &'static str = "state_remove"
pub const STATE_REMOVE: &'static str = "state_remove"
Remove a value from plugin state.
§Parameters
key_ptr: Pointer to UTF-8 key byteskey_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§
impl Freeze for HostFunctions
impl RefUnwindSafe for HostFunctions
impl Send for HostFunctions
impl Sync for HostFunctions
impl Unpin for HostFunctions
impl UnwindSafe for HostFunctions
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more