Lines
100 %
Functions
58.33 %
Branches
//! The inverse of `decode`: builds the guest value of an [`EvalValue`] for a host fn
//! declared to return `any`.
use std::future::Future;
use std::pin::Pin;
use wasmtime::{AnyRef, AsContextMut, Caller, I31, Rooted, Val};
use super::abi::{alloc_pair_chain, alloc_ratio_ref, alloc_string_ref};
use super::decode::EvalValue;
type Encoded = wasmtime::Result<Option<Rooted<AnyRef>>>;
/// The guest value of `value`: strings, numbers, `t`, `nil`, symbols and lists of them.
///
/// # Errors
/// A value kind with no host-built form (bytes, commodity), an index beyond 31 bits, or a
/// module without the exports the compiler always emits.
pub fn alloc_value<'a, T: Send + 'a>(
caller: &'a mut Caller<'_, T>,
value: &'a EvalValue,
) -> Pin<Box<dyn Future<Output = Encoded> + Send + 'a>> {
Box::pin(async move {
match value {
EvalValue::Nil => Ok(None),
EvalValue::T => t_singleton_of(caller).map(Some),
EvalValue::I32(n) => index_ref(caller, *n).map(Some),
EvalValue::Ratio { numer, denom } => {
Ok(Some(alloc_ratio_ref(caller, *numer, *denom)?.to_anyref()))
}
EvalValue::String(text) => {
Ok(Some(alloc_string_ref(caller, text.as_bytes())?.to_anyref()))
EvalValue::Symbol(name) => alloc_symbol(caller, name).await.map(Some),
EvalValue::List(items) => alloc_list(caller, items).await,
EvalValue::Bytes(_) | EvalValue::Commodity { .. } => Err(wasmtime::Error::msg(
"a host fn cannot return bytes or a commodity as an `any` value",
)),
EvalValue::Closure { .. }
| EvalValue::Condition
| EvalValue::Opaque
| EvalValue::Instance { .. }
| EvalValue::Struct(_) => Err(wasmtime::Error::msg(
"a host fn cannot return an opaque value as an `any` value",
})
fn t_singleton_of<T>(caller: &mut Caller<'_, T>) -> wasmtime::Result<Rooted<AnyRef>> {
let global = caller
.get_export("t")
.and_then(wasmtime::Extern::into_global)
.ok_or_else(|| wasmtime::Error::msg("module does not export the `t` global"))?;
match global.get(caller.as_context_mut()) {
Val::AnyRef(Some(t)) => Ok(t),
_ => Err(wasmtime::Error::msg(
"`t` global is not a non-null internal reference",
fn index_ref<T>(caller: &mut Caller<'_, T>, n: i32) -> wasmtime::Result<Rooted<AnyRef>> {
let i31 = I31::new_i32(n)
.ok_or_else(|| wasmtime::Error::msg(format!("index {n} exceeds 31 bits")))?;
Ok(AnyRef::from_i31(caller.as_context_mut(), i31))
async fn alloc_symbol<T: Send>(
caller: &mut Caller<'_, T>,
name: &str,
) -> wasmtime::Result<Rooted<AnyRef>> {
let symbol_new = caller
.get_export("symbol_new")
.and_then(wasmtime::Extern::into_func)
.ok_or_else(|| wasmtime::Error::msg("module does not export `symbol_new`"))?;
let text = alloc_string_ref(caller, name.as_bytes())?;
let mut results = [Val::AnyRef(None)];
symbol_new
.call_async(
caller.as_context_mut(),
&[Val::AnyRef(Some(text.to_anyref()))],
&mut results,
)
.await?;
match results {
[Val::AnyRef(Some(symbol))] => Ok(symbol),
_ => Err(wasmtime::Error::msg("symbol_new returned no symbol")),
async fn alloc_list<T: Send>(caller: &mut Caller<'_, T>, items: &[EvalValue]) -> Encoded {
let mut cars = Vec::with_capacity(items.len());
for item in items {
cars.push(alloc_value(caller, item).await?);
Ok(alloc_pair_chain(caller, cars)
.await?
.map(|head| head.to_anyref()))