pub struct Host<'a> { /* private fields */ }Expand description
Safe access to the host VM for the duration of one plugin call.
Methods that run generic bytecode (and may therefore trigger garbage
collection) take &mut self: the borrow checker then guarantees the
rooting contract’s string rule - any &str obtained from
the host borrows self and cannot be held across a re-entering call.
Values held across a re-entering call must be rooted, e.g.
via Host::rooted.
Implementations§
Source§impl<'a> Host<'a>
impl<'a> Host<'a>
Sourcepub fn kind(&self, value: GenericValue) -> ValueKind
pub fn kind(&self, value: GenericValue) -> ValueKind
The ValueKind of a value.
Sourcepub fn decode(&self, value: GenericValue) -> ArgValue<'_>
pub fn decode(&self, value: GenericValue) -> ArgValue<'_>
Decode a value into a borrowed view.
Sourcepub fn as_bool(&self, value: GenericValue) -> Option<bool>
pub fn as_bool(&self, value: GenericValue) -> Option<bool>
None if the value is not a bool.
Sourcepub fn as_int(&self, value: GenericValue) -> Option<i64>
pub fn as_int(&self, value: GenericValue) -> Option<i64>
The value as an i64; None if it is not an integer or does not
fit in an i64 (big integers - fall back to display).
Sourcepub fn as_float(&self, value: GenericValue) -> Option<f64>
pub fn as_float(&self, value: GenericValue) -> Option<f64>
None if the value is not a float.
Sourcepub fn as_str(&self, value: GenericValue) -> Option<&str>
pub fn as_str(&self, value: GenericValue) -> Option<&str>
The contents of a string value; None if the value is not a string
(or the host answered with malformed string data - a null pointer or
invalid UTF-8, both protocol violations).
The returned string borrows the host and therefore cannot be held
across a re-entering call (&mut self methods); the compiler
rejects it:
use generic_lang_api::{GenericValue, Host, PluginError};
fn plugin_fn(host: &mut Host, args: &[GenericValue]) -> Result<GenericValue, PluginError> {
let name = host.as_str(args[0]).unwrap(); // borrows `host`
host.call(args[1], &[])?; // re-enters: needs `&mut host`
Ok(host.make_str(name)) // ERROR: `name` still borrowed
}Copy the string out (.to_owned()) before re-entering if it is
needed afterwards.
Sourcepub fn list_len(&self, value: GenericValue) -> Option<usize>
pub fn list_len(&self, value: GenericValue) -> Option<usize>
None if the value is not a list.
Sourcepub fn list_get(
&self,
value: GenericValue,
index: usize,
) -> Result<GenericValue, PluginError>
pub fn list_get( &self, value: GenericValue, index: usize, ) -> Result<GenericValue, PluginError>
The list element at index.
§Errors
TypeError if the value is not a list; IndexError if the index
is out of bounds.
Sourcepub fn tuple_len(&self, value: GenericValue) -> Option<usize>
pub fn tuple_len(&self, value: GenericValue) -> Option<usize>
None if the value is not a tuple.
Sourcepub fn tuple_get(
&self,
value: GenericValue,
index: usize,
) -> Result<GenericValue, PluginError>
pub fn tuple_get( &self, value: GenericValue, index: usize, ) -> Result<GenericValue, PluginError>
The tuple element at index.
§Errors
TypeError if the value is not a tuple; IndexError if the index
is out of bounds.
Sourcepub fn dict_len(&self, value: GenericValue) -> Option<usize>
pub fn dict_len(&self, value: GenericValue) -> Option<usize>
None if the value is not a dict.
Sourcepub fn set_len(&self, value: GenericValue) -> Option<usize>
pub fn set_len(&self, value: GenericValue) -> Option<usize>
None if the value is not a set.
Sourcepub fn builtin(&self, name: &str) -> Result<GenericValue, PluginError>
pub fn builtin(&self, name: &str) -> Result<GenericValue, PluginError>
Look up a builtin global by name - exception classes like
"TypeError", native classes, builtin functions.
§Errors
NameError if absent.
Sourcepub fn is_instance(
&self,
value: GenericValue,
class: GenericValue,
) -> Result<bool, PluginError>
pub fn is_instance( &self, value: GenericValue, class: GenericValue, ) -> Result<bool, PluginError>
Whether value is an instance of class or of a subclass of it -
the exact semantics of the isinstance builtin, value-type proxy
classes included.
§Errors
TypeError if class is not a class.
Sourcepub fn class_of(&self, value: GenericValue) -> Result<GenericValue, PluginError>
pub fn class_of(&self, value: GenericValue) -> Result<GenericValue, PluginError>
The class of an instance, as a class value (the analogue of
type(self)). Call it to construct another instance of the same class,
or pass it to Host::is_instance to type-check another argument
before reading its opaque state.
§Errors
TypeError if value is not an instance.
Sourcepub fn attr_get(
&self,
receiver: GenericValue,
name: &str,
) -> Result<GenericValue, PluginError>
pub fn attr_get( &self, receiver: GenericValue, name: &str, ) -> Result<GenericValue, PluginError>
A field of an instance.
§Errors
AttributeError if the field is absent, TypeError if the receiver
is not an instance.
Sourcepub fn attr_set(
&self,
receiver: GenericValue,
name: &str,
value: GenericValue,
) -> Result<(), PluginError>
pub fn attr_set( &self, receiver: GenericValue, name: &str, value: GenericValue, ) -> Result<(), PluginError>
Sourcepub fn attr_has(
&self,
receiver: GenericValue,
name: &str,
) -> Result<bool, PluginError>
pub fn attr_has( &self, receiver: GenericValue, name: &str, ) -> Result<bool, PluginError>
Sourcepub fn make_nil(&self) -> GenericValue
pub fn make_nil(&self) -> GenericValue
A new nil value.
Sourcepub fn make_bool(&self, value: bool) -> GenericValue
pub fn make_bool(&self, value: bool) -> GenericValue
A new boolean value.
Sourcepub fn make_int(&self, value: i64) -> GenericValue
pub fn make_int(&self, value: i64) -> GenericValue
A new integer value.
Sourcepub fn make_float(&self, value: f64) -> GenericValue
pub fn make_float(&self, value: f64) -> GenericValue
A new float value.
Sourcepub fn make_str(&self, value: &str) -> GenericValue
pub fn make_str(&self, value: &str) -> GenericValue
Intern a string value.
§Panics
Panics if the host rejects the string, which cannot happen for Rust strings (they are always valid UTF-8).
Sourcepub fn make_list(&self) -> GenericValue
pub fn make_list(&self) -> GenericValue
A new, empty list.
Sourcepub fn list_push(
&self,
list: GenericValue,
item: GenericValue,
) -> Result<(), PluginError>
pub fn list_push( &self, list: GenericValue, item: GenericValue, ) -> Result<(), PluginError>
Sourcepub fn list_set(
&self,
list: GenericValue,
index: usize,
value: GenericValue,
) -> Result<(), PluginError>
pub fn list_set( &self, list: GenericValue, index: usize, value: GenericValue, ) -> Result<(), PluginError>
Replace the element at an index.
§Errors
TypeError if the target is not a list; IndexError if the index
is out of bounds.
Sourcepub fn make_exception(
&self,
class: GenericValue,
message: &str,
) -> Result<GenericValue, PluginError>
pub fn make_exception( &self, class: GenericValue, message: &str, ) -> Result<GenericValue, PluginError>
A new exception instance of class (any class deriving from
Exception - builtin or user-defined), ready to be thrown
(returned inside PluginError::Exception) or passed to generic
code. Sets the message directly, bypassing __init__. Prefer the
typed constructors below for the common builtin-class case.
§Errors
TypeError if class is not a class deriving from Exception.
Sourcepub fn exception(&self, message: &str) -> PluginError
pub fn exception(&self, message: &str) -> PluginError
A PluginError carrying a fresh base Exception instance.
Sourcepub fn type_error(&self, message: &str) -> PluginError
pub fn type_error(&self, message: &str) -> PluginError
A PluginError carrying a fresh TypeError instance.
Sourcepub fn value_error(&self, message: &str) -> PluginError
pub fn value_error(&self, message: &str) -> PluginError
A PluginError carrying a fresh ValueError instance.
Sourcepub fn name_error(&self, message: &str) -> PluginError
pub fn name_error(&self, message: &str) -> PluginError
A PluginError carrying a fresh NameError instance.
Sourcepub fn const_reassignment_error(&self, message: &str) -> PluginError
pub fn const_reassignment_error(&self, message: &str) -> PluginError
A PluginError carrying a fresh ConstReassignmentError instance.
Sourcepub fn attribute_error(&self, message: &str) -> PluginError
pub fn attribute_error(&self, message: &str) -> PluginError
A PluginError carrying a fresh AttributeError instance.
Sourcepub fn import_error(&self, message: &str) -> PluginError
pub fn import_error(&self, message: &str) -> PluginError
A PluginError carrying a fresh ImportError instance.
Sourcepub fn assertion_error(&self, message: &str) -> PluginError
pub fn assertion_error(&self, message: &str) -> PluginError
A PluginError carrying a fresh AssertionError instance.
Sourcepub fn io_error(&self, message: &str) -> PluginError
pub fn io_error(&self, message: &str) -> PluginError
A PluginError carrying a fresh IoError instance.
Sourcepub fn key_error(&self, message: &str) -> PluginError
pub fn key_error(&self, message: &str) -> PluginError
A PluginError carrying a fresh KeyError instance.
Sourcepub fn index_error(&self, message: &str) -> PluginError
pub fn index_error(&self, message: &str) -> PluginError
A PluginError carrying a fresh IndexError instance.
Sourcepub fn display(&self, value: GenericValue) -> GenericValue
pub fn display(&self, value: GenericValue) -> GenericValue
The raw string representation of any value, as a string value.
Does NOT honor a user class’s __str__ - see Host::to_str.
Sourcepub fn display_string(&self, value: GenericValue) -> String
pub fn display_string(&self, value: GenericValue) -> String
Host::display, copied out as an owned Rust String.
Sourcepub fn call(
&mut self,
callee: GenericValue,
args: &[GenericValue],
) -> Result<GenericValue, PluginError>
pub fn call( &mut self, callee: GenericValue, args: &[GenericValue], ) -> Result<GenericValue, PluginError>
Call a callable value with the given arguments.
§Errors
Returns the generic exception raised by the callee, if any.
Sourcepub fn invoke(
&mut self,
receiver: GenericValue,
name: &str,
args: &[GenericValue],
) -> Result<GenericValue, PluginError>
pub fn invoke( &mut self, receiver: GenericValue, name: &str, args: &[GenericValue], ) -> Result<GenericValue, PluginError>
Invoke a named method on a receiver.
§Errors
Returns the generic exception raised by the method, if any.
Sourcepub fn to_str(
&mut self,
value: GenericValue,
) -> Result<GenericValue, PluginError>
pub fn to_str( &mut self, value: GenericValue, ) -> Result<GenericValue, PluginError>
String conversion honoring a user class’s __str__.
§Errors
Returns the generic exception raised by __str__, if any.
Sourcepub fn dict_get(
&mut self,
dict: GenericValue,
key: GenericValue,
) -> Result<GenericValue, PluginError>
pub fn dict_get( &mut self, dict: GenericValue, key: GenericValue, ) -> Result<GenericValue, PluginError>
Look up a key in a dict.
§Errors
KeyError if absent, TypeError for unusable targets/keys, or any
exception raised by __hash__/__eq__.
Sourcepub fn dict_set(
&mut self,
dict: GenericValue,
key: GenericValue,
value: GenericValue,
) -> Result<(), PluginError>
pub fn dict_set( &mut self, dict: GenericValue, key: GenericValue, value: GenericValue, ) -> Result<(), PluginError>
Insert or replace a key in a dict.
§Errors
TypeError for unusable targets/keys, or any exception raised by
__hash__/__eq__.
Sourcepub fn dict_contains(
&mut self,
dict: GenericValue,
key: GenericValue,
) -> Result<bool, PluginError>
pub fn dict_contains( &mut self, dict: GenericValue, key: GenericValue, ) -> Result<bool, PluginError>
Whether a dict contains a key.
§Errors
TypeError for unusable targets/keys, or any exception raised by
__hash__/__eq__.
Sourcepub fn set_add(
&mut self,
set: GenericValue,
item: GenericValue,
) -> Result<(), PluginError>
pub fn set_add( &mut self, set: GenericValue, item: GenericValue, ) -> Result<(), PluginError>
Add an item to a set.
§Errors
TypeError for unusable targets/items, or any exception raised by
__hash__/__eq__.
Sourcepub fn set_contains(
&mut self,
set: GenericValue,
item: GenericValue,
) -> Result<bool, PluginError>
pub fn set_contains( &mut self, set: GenericValue, item: GenericValue, ) -> Result<bool, PluginError>
Whether a set contains an item.
§Errors
TypeError for unusable targets/items, or any exception raised by
__hash__/__eq__.
Sourcepub fn truthy(&mut self, value: GenericValue) -> Result<bool, PluginError>
pub fn truthy(&mut self, value: GenericValue) -> Result<bool, PluginError>
Sourcepub fn equals(
&mut self,
a: GenericValue,
b: GenericValue,
) -> Result<bool, PluginError>
pub fn equals( &mut self, a: GenericValue, b: GenericValue, ) -> Result<bool, PluginError>
Sourcepub fn hash(&mut self, value: GenericValue) -> Result<i64, PluginError>
pub fn hash(&mut self, value: GenericValue) -> Result<i64, PluginError>
Sourcepub fn root(&self, value: GenericValue)
pub fn root(&self, value: GenericValue)
Keep a value alive across re-entering calls for the rest of this
plugin call (the host releases all roots automatically on return).
Prefer the RAII form, Host::rooted.
Sourcepub fn unroot(&self, n: usize)
pub fn unroot(&self, n: usize)
Release the n most recent roots early. Releasing more roots than
were pushed corrupts interpreter state; prefer the RAII form,
Host::rooted.
Sourcepub fn rooted(&self, value: GenericValue) -> Rooted<'a>
pub fn rooted(&self, value: GenericValue) -> Rooted<'a>
Root a value for the lifetime of the returned guard.
Guards release in LIFO order - drop them in reverse order of creation (scopes do this naturally).
Sourcepub fn set_opaque(
&self,
receiver: GenericValue,
ptr: *mut c_void,
) -> Result<(), PluginError>
pub fn set_opaque( &self, receiver: GenericValue, ptr: *mut c_void, ) -> Result<(), PluginError>
Install the plugin’s opaque pointer on a plugin-backed instance.
Typically called from __init__ with args[0] (the receiver) and a
Box::into_raw(state) pointer. The class’s drop callback is called
with this pointer when the instance is garbage-collected.
Overwriting an already-installed pointer leaks the previous one: the
host does not run drop on it, since it cannot know whether the plugin
still holds a copy elsewhere. If a plugin means to replace state, it must
Host::get_opaque and free the old pointer itself first.
§Errors
TypeError if receiver is not a plugin-backed instance.
Sourcepub fn get_opaque(&self, receiver: GenericValue) -> *mut c_void
pub fn get_opaque(&self, receiver: GenericValue) -> *mut c_void
Recover the pointer installed by Host::set_opaque, or null if none
was installed (e.g. before __init__ ran) or receiver is not a
plugin-backed instance. Never raises.
Sourcepub unsafe fn opaque_ref<T>(&self, receiver: GenericValue) -> Option<&mut T>
pub unsafe fn opaque_ref<T>(&self, receiver: GenericValue) -> Option<&mut T>
Typed mutable view of the opaque pointer, or None if it is null or
receiver is not a plugin-backed instance.
§Safety
The caller must ensure T is the correct type for this instance’s
opaque state. The reference is valid while the instance is alive (the
GC will not collect it while the plugin holds the instance value).