Skip to main content

Host

Struct Host 

Source
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>

Source

pub fn kind(&self, value: GenericValue) -> ValueKind

The ValueKind of a value.

Source

pub fn decode(&self, value: GenericValue) -> ArgValue<'_>

Decode a value into a borrowed view.

Source

pub fn as_bool(&self, value: GenericValue) -> Option<bool>

None if the value is not a bool.

Source

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).

Source

pub fn as_float(&self, value: GenericValue) -> Option<f64>

None if the value is not a float.

Source

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.

Source

pub fn list_len(&self, value: GenericValue) -> Option<usize>

None if the value is not a list.

Source

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.

Source

pub fn tuple_len(&self, value: GenericValue) -> Option<usize>

None if the value is not a tuple.

Source

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.

Source

pub fn dict_len(&self, value: GenericValue) -> Option<usize>

None if the value is not a dict.

Source

pub fn set_len(&self, value: GenericValue) -> Option<usize>

None if the value is not a set.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn attr_set( &self, receiver: GenericValue, name: &str, value: GenericValue, ) -> Result<(), PluginError>

Set a field on an instance.

§Errors

TypeError if the receiver is not an instance.

Source

pub fn attr_has( &self, receiver: GenericValue, name: &str, ) -> Result<bool, PluginError>

Whether an instance has a field.

§Errors

TypeError if the receiver is not an instance.

Source

pub fn make_nil(&self) -> GenericValue

A new nil value.

Source

pub fn make_bool(&self, value: bool) -> GenericValue

A new boolean value.

Source

pub fn make_int(&self, value: i64) -> GenericValue

A new integer value.

Source

pub fn make_float(&self, value: f64) -> GenericValue

A new float value.

Source

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).

Source

pub fn make_list(&self) -> GenericValue

A new, empty list.

Source

pub fn list_push( &self, list: GenericValue, item: GenericValue, ) -> Result<(), PluginError>

Append to a list value.

§Errors

TypeError if the target is not a list.

Source

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.

Source

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.

Source

pub fn exception(&self, message: &str) -> PluginError

A PluginError carrying a fresh base Exception instance.

Source

pub fn type_error(&self, message: &str) -> PluginError

A PluginError carrying a fresh TypeError instance.

Source

pub fn value_error(&self, message: &str) -> PluginError

A PluginError carrying a fresh ValueError instance.

Source

pub fn name_error(&self, message: &str) -> PluginError

A PluginError carrying a fresh NameError instance.

Source

pub fn const_reassignment_error(&self, message: &str) -> PluginError

A PluginError carrying a fresh ConstReassignmentError instance.

Source

pub fn attribute_error(&self, message: &str) -> PluginError

A PluginError carrying a fresh AttributeError instance.

Source

pub fn import_error(&self, message: &str) -> PluginError

A PluginError carrying a fresh ImportError instance.

Source

pub fn assertion_error(&self, message: &str) -> PluginError

A PluginError carrying a fresh AssertionError instance.

Source

pub fn io_error(&self, message: &str) -> PluginError

A PluginError carrying a fresh IoError instance.

Source

pub fn key_error(&self, message: &str) -> PluginError

A PluginError carrying a fresh KeyError instance.

Source

pub fn index_error(&self, message: &str) -> PluginError

A PluginError carrying a fresh IndexError instance.

Source

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.

Source

pub fn display_string(&self, value: GenericValue) -> String

Host::display, copied out as an owned Rust String.

Source

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.

Source

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.

Source

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.

Source

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__.

Source

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__.

Source

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__.

Source

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__.

Source

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__.

Source

pub fn truthy(&mut self, value: GenericValue) -> Result<bool, PluginError>

Truthiness honoring __bool__.

§Errors

Returns the generic exception raised by __bool__, if any.

Source

pub fn equals( &mut self, a: GenericValue, b: GenericValue, ) -> Result<bool, PluginError>

Equality honoring __eq__.

§Errors

Returns the generic exception raised by __eq__, if any.

Source

pub fn hash(&mut self, value: GenericValue) -> Result<i64, PluginError>

Hash honoring __hash__.

§Errors

Returns the generic exception raised by __hash__, if any.

Source

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.

Source

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.

Source

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).

Source

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.

Source

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.

Source

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).

Auto Trait Implementations§

§

impl<'a> !Send for Host<'a>

§

impl<'a> !Sync for Host<'a>

§

impl<'a> Freeze for Host<'a>

§

impl<'a> RefUnwindSafe for Host<'a>

§

impl<'a> Unpin for Host<'a>

§

impl<'a> UnsafeUnpin for Host<'a>

§

impl<'a> UnwindSafe for Host<'a>

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.