62 lines
1.5 KiB
Rust
Raw Normal View History

use std::any::TypeId;
use elements::FunctionType;
use interpreter::value::RuntimeValue;
2017-12-11 16:28:05 +01:00
use interpreter::Error;
2017-12-11 19:22:45 +01:00
/// Custom user error.
pub trait HostError: 'static + ::std::fmt::Display + ::std::fmt::Debug {
#[doc(hidden)]
fn __private_get_type_id__(&self) -> TypeId {
TypeId::of::<Self>()
}
}
impl HostError {
/// Attempt to downcast this `HostError` to a concrete type by reference.
pub fn downcast_ref<T: HostError>(&self) -> Option<&T> {
if self.__private_get_type_id__() == TypeId::of::<T>() {
unsafe { Some(&*(self as *const HostError as *const T)) }
} else {
None
}
}
/// Attempt to downcast this `HostError` to a concrete type by mutable
/// reference.
pub fn downcast_mut<T: HostError>(&mut self) -> Option<&mut T> {
if self.__private_get_type_id__() == TypeId::of::<T>() {
unsafe { Some(&mut *(self as *mut HostError as *mut T)) }
} else {
None
}
}
}
pub type HostFuncIndex = u32;
2017-12-15 18:23:54 +03:00
pub trait Externals {
fn invoke_index(
2017-12-18 16:37:48 +03:00
&mut self,
index: HostFuncIndex,
args: &[RuntimeValue],
) -> Result<Option<RuntimeValue>, Error>;
2017-12-12 15:18:35 +01:00
fn check_signature(&self, index: HostFuncIndex, signature: &FunctionType) -> bool;
2017-12-11 19:22:45 +01:00
}
2018-01-09 18:18:16 +03:00
pub struct NopExternals;
2017-12-14 15:33:40 +01:00
2018-01-09 18:18:16 +03:00
impl Externals for NopExternals {
fn invoke_index(
&mut self,
_index: HostFuncIndex,
_args: &[RuntimeValue],
) -> Result<Option<RuntimeValue>, Error> {
2018-01-09 18:18:16 +03:00
Err(Error::Trap("invoke index on no-op externals".into()))
2017-12-14 15:33:40 +01:00
}
fn check_signature(&self, _index: HostFuncIndex, _signature: &FunctionType) -> bool {
false
2017-12-14 15:33:40 +01:00
}
2017-12-11 16:28:05 +01:00
}