use crate::{new_error, Result};
pub mod call_ctx;
pub(crate) mod guest_dispatch;
pub(crate) mod guest_err;
pub mod host_functions;
pub(crate) mod param_type;
pub mod ret_type;
use std::sync::{Arc, Mutex};
pub use hyperlight_common::flatbuffer_wrappers::function_types::ParameterValue;
pub use hyperlight_common::flatbuffer_wrappers::function_types::ReturnType;
pub use hyperlight_common::flatbuffer_wrappers::function_types::ReturnValue;
pub use param_type::SupportedParameterType;
pub use ret_type::SupportedReturnType;
use tracing::{instrument, Span};
type HLFunc = Arc<Mutex<Box<dyn FnMut(Vec<ParameterValue>) -> Result<ReturnValue> + Send>>>;
#[derive(Clone)]
pub struct HyperlightFunction(HLFunc);
impl HyperlightFunction {
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
pub(crate) fn new<F>(f: F) -> Self
where
F: FnMut(Vec<ParameterValue>) -> Result<ReturnValue> + Send + 'static,
{
Self(Arc::new(Mutex::new(Box::new(f))))
}
#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
pub(crate) fn call(&self, args: Vec<ParameterValue>) -> Result<ReturnValue> {
let mut f = self
.0
.try_lock()
.map_err(|e| new_error!("Error locking at {}:{}: {}", file!(), line!(), e))?;
f(args)
}
}
pub use host_functions::HostFunction0;
pub use host_functions::HostFunction1;
pub use host_functions::HostFunction10;
pub use host_functions::HostFunction2;
pub use host_functions::HostFunction3;
pub use host_functions::HostFunction4;
pub use host_functions::HostFunction5;
pub use host_functions::HostFunction6;
pub use host_functions::HostFunction7;
pub use host_functions::HostFunction8;
pub use host_functions::HostFunction9;