Lines
89.09 %
Functions
75 %
Branches
100 %
//! Compile-time emit for caller-supplied host fn calls.
//!
//! When `(rpc-protocol-version)` (or any other registered `HostFnSpec`)
//! shows up in nomiscript source, the dispatcher in `super::mod`
//! routes here. We push each argument onto the wasm stack via
//! `compile_for_stack`, validate the type matches the spec, and emit
//! `(call $idx)` against the import index recorded by
//! [`CompileContext::register_host_fn`].
use super::CompileContext;
use super::nullable::{nullref_trap, require_non_null};
use crate::ast::{Expr, WasmType};
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::expr::compile_for_stack_as;
use crate::error::{Error, Result};
use crate::runtime::SymbolTable;
pub(in crate::compiler) fn compile_host_fn_for_stack(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
symbols: &mut SymbolTable,
name: &str,
args: &[Expr],
) -> Result<WasmType> {
let entry = expect_entry(ctx, name)?;
let result = entry.result.ok_or_else(|| {
Error::Compile(format!(
"host fn '{name}' has no return type and cannot produce a stack value"
))
})?;
let func_idx = entry.func_idx;
let params = entry.params;
push_args(ctx, emit, symbols, name, ¶ms, args)?;
symbols.mark_native_referenced(name);
emit.call(func_idx);
// Host imports declare GC-ref results with the abstract heap type
// (`(ref null struct)` / `(ref null array)`) because that's what
// `Rooted<StructRef>` / `Rooted<ArrayRef>` report through
// `WasmTy::valtype()`. Cast the result back to the concrete type
// the rest of the pipeline expects.
emit_concrete_cast(ctx, emit, result)?;
Ok(result)
}
pub(in crate::compiler) fn compile_host_fn_for_effect(
) -> Result<()> {
let result = entry.result;
if let Some(ty) = result {
emit_concrete_cast(ctx, emit, ty)?;
emit.drop_value();
Ok(())
/// Casts the abstract `(ref null struct)` / `(ref null array)` left on
/// the stack by a host import to the concrete type the result's
/// `WasmType` declares. Primitive returns (I32) pass through unchanged.
/// Nullable variant: wasmtime surfaces `Option<Rooted<…>>` returns as
/// `(ref null …)`, so an empty `list-accounts` / not-found
/// `get-account` returns null and the cast must preserve it. The
/// non-null `ref_cast` would trap on every nil-shaped result.
pub(in crate::compiler) fn emit_concrete_cast(
ctx: &CompileContext,
ty: WasmType,
match ty {
WasmType::I32 | WasmType::Bool => {}
WasmType::Ratio => emit.ref_cast_nullable(ctx.ids.ty_ratio),
WasmType::Commodity => emit.ref_cast_nullable(ctx.ids.ty_commodity),
WasmType::StringRef => emit.ref_cast_nullable(ctx.ids.ty_i8_array),
WasmType::SymbolRef => emit.ref_cast_nullable(ctx.ids.ty_symbol),
WasmType::PairRef(_) => emit.ref_cast_nullable(ctx.ids.ty_pair),
WasmType::EntityRef(kind) => emit.ref_cast_nullable(ctx.ids.entity_type(kind)),
WasmType::StructRef(_) => emit.ref_cast_nullable(ctx.ids.ty_struct),
WasmType::Closure(sig) => emit.ref_cast_nullable(ctx.closure_sig(sig)?.closure_type_idx),
WasmType::AnyRef => {}
fn expect_entry(ctx: &CompileContext, name: &str) -> Result<HostFnSnapshot> {
let entry = ctx.lookup_host_fn(name).ok_or_else(|| {
"host fn dispatch reached for unregistered name '{name}'"
Ok(HostFnSnapshot {
func_idx: entry.func_idx,
params: entry.params.clone(),
result: entry.result,
})
struct HostFnSnapshot {
func_idx: u32,
params: Vec<WasmType>,
result: Option<WasmType>,
fn push_args(
params: &[WasmType],
if args.len() != params.len() {
return Err(Error::Arity {
name: name.to_string(),
expected: params.len(),
actual: args.len(),
});
// Pass 1: evaluate every argument, left to right, into a scratch local —
// BEFORE any validation runs. A validation throw (the null check below,
// or the commodity atomicity guard) must never pre-empt a later
// argument's own side effects/errors: `(f (if p x) (progn (error ...) y))`
// must raise from evaluating the second argument even when the first
// would fail its own check, exactly as evaluating both arguments would
// with no host-fn call at all.
let mut locals = Vec::with_capacity(args.len());
for (idx, (arg, expected_ty)) in args.iter().zip(params.iter()).enumerate() {
// Coerce each argument to the host fn's declared parameter type; an
// integer/fractional literal crosses the sanctioned Index↔Scalar
// boundary so e.g. a `Ratio` arg accepts a bare literal.
compile_for_stack_as(ctx, emit, symbols, arg, *expected_ty).map_err(|_| Error::Type {
expected: format!("{expected_ty} for argument {idx} of '{name}'"),
actual: "an incompatible value".to_string(),
let local = ctx.alloc_local(*expected_ty)?;
emit.local_set(local);
locals.push((local, *expected_ty));
// Pass 2: validate every already-evaluated argument.
for &(local, expected_ty) in &locals {
// A nullable `StringRef` / `EntityRef` reaching a host import as `nil`
// would surface as an uncatchable `Option::None`-vs-value mismatch on
// the wasmtime boundary rather than a compiler-level condition; catch
// it here as a catchable `TYPE-ERROR` before the call (ADR-0033 Phase
// 5). `rpc::natives::*` keeps its own `Option` handling as a
// defensive fallback, not the primary guard.
if let Some(trap) = nullref_trap(ctx, expected_ty) {
emit.local_get(local);
require_non_null(ctx, emit, expected_ty, trap)?;
// ADR-0028 E2: the wasm↔host border is unit-erased — host fns only see
// ATOMIC single-currency money (fields 0-3). Guard every Commodity arg:
// `commodity_assert_atomic` returns it unchanged when its unit term is
// null, else throws a catchable `NON-ATOMIC-COMMODITY`, so a compound
// value can never be misread as `id = 0` money.
if expected_ty == WasmType::Commodity {
emit.call(ctx.ids.commodity_assert_atomic);
// Pass 3: reload the validated arguments onto the stack, in call order.
for &(local, _) in &locals {