use crate::function::{adapter::Token, IntoHostFunction};
use crate::invocation::{
IntoEffectFunction, IntoExternalFunction, IntoInputFunction, IntoSlotReadFunction,
IntoSlotWriteFunction,
};
use crate::{Error, Function, HostSchema, OpCapability, Result, Rustia, VmError, VmErrorKind};
use rustia_vm::HostFunctionOp;
use std::marker::PhantomData;
pub struct HostFunction<C: HostSchema = ()> {
build: Box<dyn FnOnce(String, u32) -> HostFunctionOp + Send + Sync>,
fuel_cost: u32,
schema: PhantomData<fn(C) -> C>,
}
impl<C: HostSchema> HostFunction<C> {
fn from_factory(
build: impl FnOnce(String, u32) -> HostFunctionOp + Send + Sync + 'static,
) -> Self {
Self {
build: Box::new(build),
fuel_cost: 1,
schema: PhantomData,
}
}
pub fn pure<F, A, R>(function: F) -> Self
where
F: IntoHostFunction<A, R>,
{
Self::with_capability(OpCapability::Pure, function)
}
pub fn with_capability<F, A, R>(capability: OpCapability, function: F) -> Self
where
F: IntoHostFunction<A, R>,
{
Self::from_factory(move |name, fuel| {
function.into_host_function(Token, name, fuel, capability.to_core())
})
}
pub fn input<F, A, R>(function: F) -> Self
where
F: IntoInputFunction<C, A, R>,
{
Self::from_factory(move |name, fuel| {
function.into_input_function(Token, name, fuel).into_op()
})
}
pub fn slot_read<F, A, R>(function: F) -> Self
where
F: IntoSlotReadFunction<C, A, R>,
{
Self::from_factory(move |name, fuel| {
function
.into_slot_read_function(Token, name, fuel)
.into_op()
})
}
pub fn effect<F, A, R>(function: F) -> Self
where
F: IntoEffectFunction<C, A, R>,
{
Self::from_factory(move |name, fuel| {
function.into_effect_function(Token, name, fuel).into_op()
})
}
pub fn slot_write<F, A, R>(function: F) -> Self
where
F: IntoSlotWriteFunction<C, A, R>,
{
Self::from_factory(move |name, fuel| {
function
.into_slot_write_function(Token, name, fuel)
.into_op()
})
}
pub fn external<F, A, R>(function: F) -> Self
where
F: IntoExternalFunction<C, A, R>,
{
Self::from_factory(move |name, fuel| {
function
.into_external_function(
Token,
name,
fuel,
OpCapability::ReadsExternalMutableState.to_core(),
)
.into_op()
})
}
pub fn nondeterministic<F, A, R>(function: F) -> Self
where
F: IntoExternalFunction<C, A, R>,
{
Self::from_factory(move |name, fuel| {
function
.into_external_function(
Token,
name,
fuel,
OpCapability::NondeterministicEffect.to_core(),
)
.into_op()
})
}
pub fn fuel_cost(mut self, fuel_cost: u32) -> Self {
self.fuel_cost = fuel_cost;
self
}
pub(crate) fn into_op(self, name: String) -> HostFunctionOp {
(self.build)(name, self.fuel_cost)
}
}
mod adapter {
use super::*;
pub(crate) trait Definition<C: HostSchema, Signature> {
fn into_definition(self, token: Token) -> HostFunction<C>;
}
}
#[allow(private_bounds)]
pub trait IntoHostFunctionDefinition<C: HostSchema, Signature>:
adapter::Definition<C, Signature>
{
}
impl<C: HostSchema, Signature, F> IntoHostFunctionDefinition<C, Signature> for F where
F: adapter::Definition<C, Signature>
{
}
impl<C: HostSchema> adapter::Definition<C, ()> for HostFunction<C> {
fn into_definition(self, _token: Token) -> HostFunction<C> {
self
}
}
impl<C, F, A, R> adapter::Definition<C, (A, R)> for F
where
C: HostSchema,
F: IntoHostFunction<A, R>,
{
fn into_definition(self, _token: Token) -> HostFunction<C> {
HostFunction::pure(self)
}
}
impl<C: HostSchema> Rustia<C> {
pub fn create_function<F, S>(&mut self, function: F) -> Result<Function>
where
F: IntoHostFunctionDefinition<C, S>,
{
self.create_named_function("<host>", function)
}
pub fn create_named_function<F, S>(
&mut self,
debug_name: impl Into<String>,
function: F,
) -> Result<Function>
where
F: IntoHostFunctionDefinition<C, S>,
{
self.ensure_healthy()?;
if self.vm.settings().guest_surface.is_hardened_leaf() {
return Err(Error::from(
VmError::new(VmErrorKind::HardenedLeafViolation {
feature: "live function construction",
})
.without_source_location(),
));
}
let function = function.into_definition(Token).into_op(debug_name.into());
let value = self
.vm
.create_retained_function(function)
.map_err(Error::from_engine)?;
Ok(Function::new(value, self.vm.decision_epoch()))
}
pub fn register_function<F, S>(&mut self, name: impl Into<String>, function: F) -> Result<()>
where
F: IntoHostFunctionDefinition<C, S>,
{
self.ensure_healthy()?;
let name = name.into();
if name.is_empty() {
return Err(Error::embed(
"host function name must not be empty".to_owned(),
));
}
self.vm
.install_host_function(function.into_definition(Token).into_op(name))
.map_err(Error::from_engine)
}
}