use crate::live_value::Value as LiveValue;
use crate::serde_value::from_live;
use crate::value::{
embed_to_vm_error, load_owned_arguments, load_owned_value, snapshot_value, FromRustia,
FromRustiaMulti, IntoRustia, IntoRustiaMulti, OwnedValue,
};
use crate::{Error, HostSchema, Invocation, Result, Rustia};
use rustia_vm::{HostFunctionOp, OpCapability, RetainedValue, Value as RawValue, ValueKind, Vm};
use std::any::type_name;
use std::sync::Arc;
#[derive(Clone, Debug)]
pub struct Function {
value: LiveValue,
decision_epoch: u64,
}
impl Function {
pub(crate) fn new(value: RetainedValue, decision_epoch: u64) -> Self {
Self {
value: LiveValue::new(value, decision_epoch),
decision_epoch,
}
}
pub(crate) fn from_value(value: LiveValue) -> Self {
Self {
decision_epoch: value.decision_epoch(),
value,
}
}
pub fn as_value(&self) -> LiveValue {
self.value.clone()
}
pub fn decision_epoch(&self) -> u64 {
self.decision_epoch
}
pub fn call<Args, Return>(&self, rustia: &mut Rustia, args: Args) -> Result<Return>
where
Args: IntoRustiaMulti,
Return: FromRustiaMulti,
{
let function = self.value.raw(rustia.vm(), "function handle")?;
let args = args.into_rustia_multi()?;
let (raw_args, _argument_roots) = load_owned_arguments(rustia.vm_mut(), &args)?;
let raw_results = rustia
.vm_mut()
.call_retained_function(function, &raw_args)
.map_err(Error::from_engine)?;
let results = raw_results
.into_iter()
.map(|value| snapshot_value(rustia.vm_mut(), value))
.collect::<Result<Vec<_>>>()?;
Return::from_rustia_multi(results)
}
pub fn call_values(&self, rustia: &mut Rustia, args: &[LiveValue]) -> Result<Vec<LiveValue>> {
let function = self.value.raw(rustia.vm(), "function handle")?;
let raw_args = args
.iter()
.map(|value| value.raw(rustia.vm(), "function argument handle"))
.collect::<Result<Vec<_>>>()?;
let results = rustia
.vm_mut()
.call_retained_function(function, &raw_args)
.map_err(Error::from_engine)?;
LiveValue::retain_many(rustia.vm_mut(), &results)
}
pub fn call_in<C, Args, Return>(
&self,
invocation: &mut Invocation<'_, C>,
args: Args,
) -> Result<Return>
where
C: HostSchema,
Args: IntoRustiaMulti,
Return: FromRustiaMulti,
{
let function = self.value.raw(invocation.vm, "function handle")?;
invocation.convert_call(function, args)
}
pub fn call_values_in<C>(
&self,
invocation: &mut Invocation<'_, C>,
args: &[LiveValue],
) -> Result<Vec<LiveValue>>
where
C: HostSchema,
{
invocation.scoped(|invocation| {
let function = self.value.raw(invocation.vm, "function handle")?;
let raw_args = args
.iter()
.map(|value| value.raw(invocation.vm, "function argument handle"))
.collect::<Result<Vec<_>>>()?;
let results = invocation.call_retained_raw(function, &raw_args)?;
LiveValue::retain_many(invocation.vm, &results)
})
}
}
#[derive(Clone, Debug)]
pub struct Exports {
value: LiveValue,
decision_epoch: u64,
}
impl Exports {
pub(crate) fn new(value: RetainedValue, decision_epoch: u64) -> Self {
Self {
value: LiveValue::new(value, decision_epoch),
decision_epoch,
}
}
pub fn as_value(&self) -> LiveValue {
self.value.clone()
}
pub fn as_table<C>(&self, rustia: &Rustia<C>) -> Result<Option<crate::Table>>
where
C: HostSchema,
{
self.value.as_table(rustia)
}
pub fn decision_epoch(&self) -> u64 {
self.decision_epoch
}
pub fn value(&self, rustia: &mut Rustia) -> Result<OwnedValue> {
let value = self.value.raw(rustia.vm(), "exports handle")?;
snapshot_value(rustia.vm_mut(), value)
}
pub fn value_in<C>(&self, invocation: &mut Invocation<'_, C>) -> Result<OwnedValue>
where
C: HostSchema,
{
invocation.scoped(|invocation| {
let value = self.value.raw(invocation.vm, "exports handle")?;
snapshot_value(invocation.vm, value)
})
}
pub fn deserialize<T>(&self, rustia: &mut Rustia<impl HostSchema>) -> Result<T>
where
T: serde::de::DeserializeOwned,
{
let value = self.value.raw(rustia.vm(), "exports handle")?;
from_live(rustia.vm_mut(), value)
}
pub fn get<T>(&self, rustia: &mut Rustia, name: &str) -> Result<T>
where
T: FromRustia,
{
let value = self.raw_get(rustia, name)?;
let value = snapshot_value(rustia.vm_mut(), value)?;
T::from_rustia(value)
}
pub fn get_live(&self, rustia: &mut Rustia, name: &str) -> Result<LiveValue> {
let value = self.raw_get(rustia, name)?;
LiveValue::retain(rustia.vm_mut(), value)
}
pub fn get_in<C, T>(&self, invocation: &mut Invocation<'_, C>, name: &str) -> Result<T>
where
C: HostSchema,
T: FromRustia,
{
invocation.scoped(|invocation| {
let value = self.raw_get_in(invocation, name)?;
let value = snapshot_value(invocation.vm, value)?;
T::from_rustia(value)
})
}
pub fn get_live_in<C>(
&self,
invocation: &mut Invocation<'_, C>,
name: &str,
) -> Result<LiveValue>
where
C: HostSchema,
{
invocation.scoped(|invocation| {
let value = self.raw_get_in(invocation, name)?;
LiveValue::retain(invocation.vm, value)
})
}
pub fn function(&self, rustia: &mut Rustia, name: &str) -> Result<Function> {
let value = self.raw_get(rustia, name)?;
if value.kind() != ValueKind::Function {
return Err(Error::embed(format!("export `{name}` is not a function")));
}
let value = rustia
.vm_mut()
.retain_value(value)
.map_err(Error::from_engine)?;
Ok(Function::new(value, self.decision_epoch))
}
pub fn function_in<C>(&self, invocation: &mut Invocation<'_, C>, name: &str) -> Result<Function>
where
C: HostSchema,
{
invocation.scoped(|invocation| {
let value = self.raw_get_in(invocation, name)?;
if value.kind() != ValueKind::Function {
return Err(Error::embed(format!("export `{name}` is not a function")));
}
let value = invocation
.vm
.retain_value(value)
.map_err(Error::from_engine)?;
Ok(Function::new(value, self.decision_epoch))
})
}
fn raw_get(&self, rustia: &mut Rustia, name: &str) -> Result<RawValue> {
let table = self.value.raw(rustia.vm(), "exports handle")?;
let key = rustia.vm_mut().string(name);
rustia
.vm_mut()
.raw_table_get_value(table, key)
.map_err(Error::from_engine)
}
fn raw_get_in<C>(&self, invocation: &mut Invocation<'_, C>, name: &str) -> Result<RawValue>
where
C: HostSchema,
{
let table = self.value.raw(invocation.vm, "exports handle")?;
let key = invocation.vm.string(name);
invocation
.vm
.raw_table_get_value(table, key)
.map_err(Error::from_engine)
}
}
pub(crate) mod adapter {
use super::*;
pub(crate) struct Token;
pub(crate) trait Function<Args, Return>: Send + Sync + 'static {
fn into_host_function(
self,
token: Token,
name: String,
fuel_cost: u32,
capability: OpCapability,
) -> HostFunctionOp;
}
pub(crate) trait Value {
fn into_host_value(self, token: Token) -> Result<HostReturnValue>;
}
pub(crate) trait Argument: Sized {
fn from_host_argument(token: Token, vm: &mut Vm, value: RawValue) -> Result<Self>;
}
pub(crate) trait Return {
fn into_host_return(self, token: Token) -> Result<Vec<HostReturnValue>>;
fn rustia_type_names(token: Token) -> Vec<&'static str>
where
Self: Sized;
fn is_fallible(_token: Token) -> bool
where
Self: Sized,
{
false
}
}
}
use adapter::Value as _;
#[allow(private_bounds)]
pub trait IntoHostFunction<Args, Return>:
adapter::Function<Args, Return> + Send + Sync + 'static
{
}
impl<T, Args, Return> IntoHostFunction<Args, Return> for T where T: adapter::Function<Args, Return> {}
pub(crate) enum HostReturnValue {
Owned(OwnedValue),
Live(LiveValue),
NewObject {
rust_type_id: std::any::TypeId,
rust_type_name: &'static str,
value: Box<rustia_vm::HostObjectPayload>,
},
}
#[allow(private_bounds)]
pub trait IntoHostValue: adapter::Value {}
impl<T: adapter::Value> IntoHostValue for T {}
impl<T: IntoRustia> adapter::Value for T {
fn into_host_value(self, _token: adapter::Token) -> Result<HostReturnValue> {
self.into_rustia().map(HostReturnValue::Owned)
}
}
impl adapter::Value for LiveValue {
fn into_host_value(self, _token: adapter::Token) -> Result<HostReturnValue> {
Ok(HostReturnValue::Live(self))
}
}
impl adapter::Value for crate::Table {
fn into_host_value(self, token: adapter::Token) -> Result<HostReturnValue> {
self.as_value().into_host_value(token)
}
}
impl adapter::Value for Function {
fn into_host_value(self, token: adapter::Token) -> Result<HostReturnValue> {
self.as_value().into_host_value(token)
}
}
#[allow(private_bounds)]
pub trait FromHostArgument: adapter::Argument + Sized {}
impl<T: adapter::Argument> FromHostArgument for T {}
impl<T: FromRustia> adapter::Argument for T {
fn from_host_argument(_token: adapter::Token, vm: &mut Vm, value: RawValue) -> Result<Self> {
T::from_rustia(snapshot_value(vm, value)?)
}
}
impl adapter::Argument for LiveValue {
fn from_host_argument(_token: adapter::Token, vm: &mut Vm, value: RawValue) -> Result<Self> {
LiveValue::retain(vm, value)
}
}
impl adapter::Argument for crate::Table {
fn from_host_argument(_token: adapter::Token, vm: &mut Vm, value: RawValue) -> Result<Self> {
if value.kind() != ValueKind::Table {
return Err(Error::embed("expected table".to_owned()));
}
Ok(Self::new(LiveValue::retain(vm, value)?))
}
}
impl adapter::Argument for Function {
fn from_host_argument(_token: adapter::Token, vm: &mut Vm, value: RawValue) -> Result<Self> {
if value.kind() != ValueKind::Function {
return Err(Error::embed("expected function".to_owned()));
}
Ok(Self::from_value(LiveValue::retain(vm, value)?))
}
}
#[allow(private_bounds)]
pub trait IntoHostReturn: adapter::Return {}
impl<T: adapter::Return> IntoHostReturn for T {}
macro_rules! impl_single_host_return {
($($ty:ty),+ $(,)?) => {$(
impl adapter::Return for $ty {
fn into_host_return(self, _token: adapter::Token) -> Result<Vec<HostReturnValue>> {
Ok(vec![self.into_host_value(adapter::Token)?])
}
fn rustia_type_names(_token: adapter::Token) -> Vec<&'static str> {
vec![type_name::<Self>()]
}
}
)+};
}
impl_single_host_return!(
OwnedValue,
crate::OwnedTable,
bool,
f64,
f32,
i8,
i16,
i32,
i64,
isize,
u8,
u16,
u32,
u64,
usize,
String,
&str,
LiveValue,
crate::Table,
Function
);
macro_rules! impl_owned_container_host_return {
($ty:ty) => {
impl<T: IntoRustia> adapter::Return for $ty {
fn into_host_return(self, _token: adapter::Token) -> Result<Vec<HostReturnValue>> {
Ok(vec![self.into_host_value(adapter::Token)?])
}
fn rustia_type_names(_token: adapter::Token) -> Vec<&'static str> {
vec![type_name::<Self>()]
}
}
};
}
impl_owned_container_host_return!(Option<T>);
impl_owned_container_host_return!(Vec<T>);
impl_owned_container_host_return!(std::collections::BTreeMap<String, T>);
impl adapter::Return for () {
fn into_host_return(self, _token: adapter::Token) -> Result<Vec<HostReturnValue>> {
Ok(Vec::new())
}
fn rustia_type_names(_token: adapter::Token) -> Vec<&'static str> {
Vec::new()
}
}
macro_rules! impl_tuple_host_return {
($($name:ident),+ $(,)?) => {
impl<$($name: IntoHostValue),+> adapter::Return for ($($name,)+) {
#[allow(non_snake_case)]
fn into_host_return(self, _token: adapter::Token) -> Result<Vec<HostReturnValue>> {
let ($($name,)+) = self;
Ok(vec![$($name.into_host_value(adapter::Token)?,)+])
}
fn rustia_type_names(_token: adapter::Token) -> Vec<&'static str> {
vec![$(type_name::<$name>()),+]
}
}
};
}
impl_tuple_host_return!(A);
impl_tuple_host_return!(A, B);
impl_tuple_host_return!(A, B, C);
impl_tuple_host_return!(A, B, C, D);
impl_tuple_host_return!(A, B, C, D, E);
impl_tuple_host_return!(A, B, C, D, E, F);
impl_tuple_host_return!(A, B, C, D, E, F, G);
impl_tuple_host_return!(A, B, C, D, E, F, G, H);
impl<T: IntoHostReturn> adapter::Return for Result<T> {
fn into_host_return(self, _token: adapter::Token) -> Result<Vec<HostReturnValue>> {
self.and_then(|value| value.into_host_return(adapter::Token))
}
fn rustia_type_names(_token: adapter::Token) -> Vec<&'static str> {
T::rustia_type_names(adapter::Token)
}
fn is_fallible(_token: adapter::Token) -> bool {
true
}
}
impl<F, Return> adapter::Function<(), Return> for F
where
F: Fn() -> Return + Send + Sync + 'static,
Return: IntoHostReturn,
{
fn into_host_function(
self,
_token: adapter::Token,
name: String,
fuel_cost: u32,
capability: OpCapability,
) -> HostFunctionOp {
let diagnostic_name = name.clone();
HostFunctionOp::new(
name,
fuel_cost,
capability,
Arc::new(move |vm, _args| host_return_to_raw(vm, &diagnostic_name, self())),
)
.with_signature(
0,
Vec::new(),
Return::rustia_type_names(adapter::Token),
Return::is_fallible(adapter::Token),
)
}
}
macro_rules! impl_host_function {
($parameter_count:expr; $($name:ident),+ $(,)?) => {
impl<Func, Return, $($name),+> adapter::Function<($($name,)+), Return> for Func
where
Func: Fn($($name),+) -> Return + Send + Sync + 'static,
Return: IntoHostReturn,
$($name: FromHostArgument,)+
{
#[allow(non_snake_case)]
fn into_host_function(
self,
_token: adapter::Token,
name: String,
fuel_cost: u32,
capability: OpCapability,
) -> HostFunctionOp {
let diagnostic_name = name.clone();
HostFunctionOp::new(
name,
fuel_cost,
capability,
Arc::new(move |vm, args| {
let mut index = 0usize;
$(
let $name = host_arg_from_raw(vm, args, index, &diagnostic_name)?;
index += 1;
)+
let _ = index;
host_return_to_raw(vm, &diagnostic_name, self($($name),+))
}),
)
.with_signature(
$parameter_count,
vec![$(type_name::<$name>()),+],
Return::rustia_type_names(adapter::Token),
Return::is_fallible(adapter::Token),
)
}
}
};
}
impl_host_function!(1; A);
impl_host_function!(2; A, B);
impl_host_function!(3; A, B, C);
impl_host_function!(4; A, B, C, D);
impl_host_function!(5; A, B, C, D, E);
impl_host_function!(6; A, B, C, D, E, F);
impl_host_function!(7; A, B, C, D, E, F, G);
impl_host_function!(8; A, B, C, D, E, F, G, H);
pub(crate) fn host_arg_from_raw<T>(
vm: &mut Vm,
args: &[RawValue],
index: usize,
function_name: &str,
) -> rustia_vm::Result<T>
where
T: FromHostArgument,
{
let value = args.get(index).copied().unwrap_or_else(RawValue::nil);
T::from_host_argument(adapter::Token, vm, value)
.map_err(|error| host_embed_error(function_name, format!("argument {}", index + 1), error))
}
pub(crate) fn host_return_to_raw<Return>(
vm: &mut Vm,
function_name: &str,
value: Return,
) -> rustia_vm::Result<Vec<RawValue>>
where
Return: IntoHostReturn,
{
let values = value
.into_host_return(adapter::Token)
.map_err(|error| host_embed_error(function_name, "return value", error))?;
let mut live_roots = Vec::new();
for value in &values {
match value {
HostReturnValue::Live(value) => {
value
.raw(vm, "host function result")
.map_err(embed_to_vm_error)?;
live_roots.push(value.clone());
}
HostReturnValue::NewObject { rust_type_id, .. } => {
vm.validate_host_object_creation(*rust_type_id)?;
}
HostReturnValue::Owned(_) => {}
}
}
let mut roots = Vec::new();
let mut results = Vec::with_capacity(values.len());
let count = values.len();
for (index, value) in values.into_iter().enumerate() {
let raw = match value {
HostReturnValue::Owned(value) => {
let raw = load_owned_value(vm, &value).map_err(|error| {
host_embed_error(function_name, format!("return value {}", index + 1), error)
})?;
if index + 1 < count
&& !matches!(
raw.kind(),
ValueKind::Nil | ValueKind::Boolean | ValueKind::Number
)
{
roots.push(vm.retain_value(raw)?);
}
raw
}
HostReturnValue::Live(value) => value
.raw(vm, "host function result")
.map_err(embed_to_vm_error)?,
HostReturnValue::NewObject {
rust_type_id,
rust_type_name,
value,
} => {
let raw = vm.create_host_object(rust_type_id, rust_type_name, value)?;
if index + 1 < count {
roots.push(vm.retain_value(raw)?);
}
raw
}
};
results.push(raw);
}
Ok(results)
}
pub(crate) fn host_embed_error(
function_name: &str,
slot: impl AsRef<str>,
error: Error,
) -> rustia_vm::Error {
match error {
Error {
kind: crate::ErrorKind::Vm(error),
..
} => (*error).into_core(),
error => embed_to_vm_error(
error.with_context(format!("host function `{function_name}` {}", slot.as_ref())),
),
}
}