Lines
88.44 %
Functions
70 %
Branches
100 %
//! Lambda body emit — production-side path that lifts a value-position
//! `(lambda ...)` / `(function ...)` from compile-time stringification
//! to a real wasm function plus a `$closure_<sig>` GC-struct value
//! pushed onto the stack.
//!
//! Scope of this slice (Tier 1.5 v1, ADR-0027): required-only params
//! typed by inference, no keyword/optional/rest/aux. Free-variable captures
//! are now lowered: each captured name with a stable local storage
//! location rides through a per-scope `$env_<id>` GC struct that the
//! helper-fn body unpacks in its prologue. Globally-resolvable names
//! (host-fn placeholders, special forms) need no env transport — they
//! resolve identically inside the helper through the cloned symbol
//! table.
//! The caller observes a `WasmType::Closure(sig)` on the stack — sig'd
//! through the per-context `ClosureRegistry` so distinct lambdas of the
//! same shape share `$fn_<sig>` and `$closure_<sig>`. Each capturing
//! lambda still gets its own `$env_<id>` so two closures with disjoint
//! captures don't collide.
use crate::ast::{ClosureSigId, Expr, LambdaParams, WasmType};
use crate::compiler::binder_key::function_owner;
use crate::compiler::context::CompileContext;
use crate::compiler::context::closure::{EnvField, EnvStructLayout, NO_SITE};
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::expr::{bind_local, compile_for_stack};
use crate::compiler::special::binding::shared::mentions;
use crate::compiler::special::needs_cell;
use crate::error::{Error, Result};
use crate::runtime::{Symbol, SymbolKind, SymbolTable};
use super::anyref::{emit_anyref_to_typed_downcast, promote_result_to_anyref};
use super::captures::{CaptureSet, compute_captures};
use super::guards::{reject_captured_stores, reject_foreign_locals};
use super::shape::{has_v1_param_shape, non_v1_lambda_error};
use super::source::closure_source;
/// Wire-level param types for the closure body fn. Catch-each (and other
/// future host-iteration callers) pass items as raw anyref values, so
/// the body fn must accept anyref params and downcast inside the
/// prologue to the declared user-visible param type. The lambda value
/// path (FUNCALL/APPLY/etc.) keeps the original typed signature so the
/// `call_ref` site doesn't need any boxing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(in crate::compiler) enum CallingConvention {
/// Wasm-side typed call_ref: param wasm types match user-visible types.
Typed,
/// Host-side anyref call: every user param is wired as anyref + downcast
/// in the prologue to the user-visible type.
HostAnyref,
}
/// Emits a real wasm function for `params`/`body` and pushes a
/// `(ref $closure_<sig>)` value onto the caller's stack. Returns the
/// signature id so the caller can record `WasmType::Closure(sig)` for
/// the stack arm.
pub(super) fn emit_lambda_value(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
symbols: &mut SymbolTable,
params: &LambdaParams,
body: &Expr,
) -> Result<ClosureSigId> {
let inferred = super::param_infer::infer_params(symbols, params, body);
let user_param_types: Vec<WasmType> = inferred
.types
.iter()
.map(|pin| pin.unwrap_or(super::param_infer::PLACEHOLDER))
.collect();
emit_lambda_value_typed(
ctx,
emit,
symbols,
params,
body,
&user_param_types,
CallingConvention::Typed,
)
.map_err(|err| name_unpinned_param(err, (params, body), &inferred.ambiguous))
/// A dimension error in a body whose used parameter nothing pinned, while
/// several dimensions fit it, keeps its message and gains the missing pin as
/// its likely cause.
fn name_unpinned_param(
err: Error,
(params, body): (&LambdaParams, &Expr),
ambiguous: &[bool],
) -> Error {
let unpinned = params
.required
.zip(ambiguous)
.find(|(name, ambiguous)| **ambiguous && mentions(body, name));
match (err, unpinned) {
(Error::Dimension(message), Some((name, _))) => Error::Dimension(format!(
"{message}; cannot infer the type of parameter {name}; pin it with THE"
)),
(err, _) => err,
/// Variant that lets the caller declare the user-visible param types
/// (the caller's choice). With `HostAnyref` calling convention every
/// param is wired as anyref on the wasm side and the prologue downcasts
/// to the declared user-visible type before the body runs.
pub(in crate::compiler) fn emit_lambda_value_typed(
user_param_types: &[WasmType],
cc: CallingConvention,
if !has_v1_param_shape(params) {
return Err(non_v1_lambda_error());
if user_param_types.len() != params.required.len() {
return Err(Error::Compile(format!(
"lambda emit: param-type count {} does not match required-param count {}",
user_param_types.len(),
params.required.len()
)));
let captures = compute_captures(symbols, params, body)?;
symbols.note_closure_deps(captures.followed_functions());
reject_captured_stores(ctx, symbols, &captures)?;
let env_fields = collect_env_fields(ctx, symbols, &captures)?;
for field in env_fields.iter().filter(|field| field.cell.is_none()) {
if let Some(symbol) = symbols.lookup_mut(&field.name) {
symbol.mark_captured();
let (result_ty, helper_func_idx, env_layout) = compile_body_into_helper(
¶ms.required,
user_param_types,
&env_fields,
cc,
)?;
let wire_param_types: Vec<WasmType> = match cc {
CallingConvention::Typed => user_param_types.to_vec(),
CallingConvention::HostAnyref => {
user_param_types.iter().map(|_| WasmType::AnyRef).collect()
};
let wire_result_ty = match cc {
CallingConvention::Typed => result_ty,
CallingConvention::HostAnyref => WasmType::AnyRef,
let sig = ctx.intern_closure_signature(&wire_param_types, wire_result_ty)?;
let source = closure_source(symbols, (params, body), &env_fields, cc)?;
let site = match source {
Some(source) => ctx.note_closure_source(source)?,
None => NO_SITE,
push_closure_value(
(sig, site),
helper_func_idx,
env_layout.as_ref(),
Ok(sig)
/// Walks `captures` against `symbols`, keeping only names that resolve
/// to a `WasmLocal(idx, ty)` — those have a stable outer-scope storage
/// location to copy from. `WasmRuntime` placeholders (e.g. registered
/// host-fn symbols) are globally resolvable and need no env transport;
/// the helper's cloned symbol table sees them unchanged.
fn collect_env_fields(
ctx: &CompileContext,
symbols: &SymbolTable,
captures: &CaptureSet,
) -> Result<Vec<EnvField>> {
let mut fields = Vec::new();
for name in captures.iter() {
let sym = symbols.lookup(name).ok_or_else(|| {
Error::Compile(format!(
"lambda body references undefined name {name:?} during capture analysis"
))
})?;
if let Some(Expr::WasmLocal(idx, ty)) = sym.value() {
fields.push(EnvField {
name: name.to_string(),
ty: *ty,
cell: ctx.cell_of(*idx),
});
Ok(fields)
/// Body emit pipeline:
/// 1. Snapshot caller's local-pool state.
/// 2. Build a fresh `FunctionEmitter`, bind params as `WasmLocal`s
/// (slot 0 = env anyref, 1..=N = user params).
/// 3. If the body captures anything: register an `$env_<id>` struct,
/// allocate one helper-side local per field, emit the prologue
/// that downcasts param-0 and unpacks the env into those locals,
/// rebind captured names in the helper's local symbol table.
/// 4. Walk body via `compile_for_stack` to produce the value the helper
/// returns.
/// 5. End the body, drop the locals declaration, register the helper
/// fn (reserves a stable wasm fn idx), queue the body into
/// `pending_helpers`.
/// 6. Restore caller's local-pool snapshot.
fn compile_body_into_helper(
param_names: &[String],
env_fields: &[EnvField],
) -> Result<(WasmType, u32, Option<EnvStructLayout>)> {
let env_anyref = ctx.anyref();
let mut helper_param_vts = Vec::with_capacity(user_param_types.len() + 1);
helper_param_vts.push(env_anyref);
for &ty in user_param_types {
let wire_ty = match cc {
CallingConvention::Typed => ty,
helper_param_vts.push(ctx.wasm_val_type(wire_ty)?);
let param_count = u32::try_from(helper_param_vts.len())
.map_err(|_| Error::Compile("lambda parameter count exceeds u32 range".to_string()))?;
let env_layout = if env_fields.is_empty() {
None
} else {
Some(ctx.intern_env_struct(env_fields)?)
let snapshot = ctx.take_local_pool(param_count);
let mut local_symbols = symbols.clone();
local_symbols.enter_function_body(function_owner(symbols, None, body));
local_symbols.drop_runtime_locals();
let mut helper_emit = FunctionEmitter::new();
if let Err(e) = bind_user_params(
&mut helper_emit,
&mut local_symbols,
param_names,
) {
ctx.restore_local_pool(snapshot);
return Err(e);
if let Some(layout) = env_layout.as_ref()
&& let Err(e) =
emit_env_unpack_prologue(ctx, &mut helper_emit, symbols, &mut local_symbols, layout)
{
// A `HostAnyref` callback is host-invoked (catch-each), so an
// uncaught `(error)` throw inside it must be bridged to `__nomi_raise`
// at this boundary — wrap the body in the Tier 3 boundary `try_table`
// (ADR-0026). The wire result is always anyref here, known up-front.
// `Typed` lambdas are in-module calls: NO wrapper, so an enclosing
// `(handler-case)` can still catch their throws.
let result_ty = match cc {
CallingConvention::Typed => {
match compile_for_stack(ctx, &mut helper_emit, &mut local_symbols, body) {
Ok(ty) => ty,
Err(e) => {
let wrap =
ctx.emit_boundary_wrapper(&mut helper_emit, Some(WasmType::AnyRef), |ctx, emit| {
let ty = compile_for_stack(ctx, emit, &mut local_symbols, body)?;
promote_result_to_anyref(ctx, emit, ty)?;
Ok(())
if let Err(e) = wrap {
WasmType::AnyRef
let helper_locals = match ctx.build_helper_locals(param_count) {
Ok(locals) => locals,
if let Err(e) = reject_foreign_locals(&helper_emit, param_count, &helper_locals) {
let helper_fn = match helper_emit.finish(&helper_locals) {
Ok(helper_fn) => helper_fn,
let result_vt = ctx.wasm_val_type(result_ty)?;
let helper_name = ctx.next_lambda_helper_name()?;
let func_idx = ctx.register_function(&helper_name, &helper_param_vts, &[result_vt])?;
ctx.declare_funcref(func_idx);
ctx.queue_helper(helper_fn);
Ok((result_ty, func_idx, env_layout))
/// Binds each user-facing param symbol so the body sees a `WasmLocal`
/// of the declared user type. With `Typed` the wasm param already holds
/// a typed value — bind directly. With `HostAnyref` the wasm param is
/// anyref; allocate a fresh helper-side typed local, downcast the
/// anyref into it, and bind the user name to that local.
fn bind_user_params(
helper: &mut FunctionEmitter,
local_symbols: &mut SymbolTable,
) -> Result<()> {
for (idx, (name, ty)) in param_names.iter().zip(user_param_types.iter()).enumerate() {
let wire_local_idx = u32::try_from(idx + 1)
let bound_idx = match cc {
CallingConvention::Typed => wire_local_idx,
let typed_local = ctx.alloc_local(*ty)?;
helper.local_get(wire_local_idx);
emit_anyref_to_typed_downcast(ctx, helper, *ty)?;
helper.local_set(typed_local);
typed_local
let bound_idx = if needs_cell(local_symbols, name, std::slice::from_ref(body))? {
helper.local_get(bound_idx);
bind_local(ctx, helper, *ty, true)?
bound_idx
local_symbols.define(
Symbol::new(name, SymbolKind::Variable).with_value(Expr::WasmLocal(bound_idx, *ty)),
);
/// Emits the helper-fn prologue that unpacks an env-struct into fresh
/// helper-side locals. For each captured field: downcast param-0
/// (`(ref null any)`) to the concrete env-struct type, read the field,
/// stash it in a freshly allocated helper-side local (a cell local when the
/// field carries a shared cell), then rebind the captured name in
/// `local_symbols` to that local so the body's `compile_for_stack` walks
/// resolve captures via `local.get`.
fn emit_env_unpack_prologue(
caller: &SymbolTable,
layout: &EnvStructLayout,
for (field_idx, field) in layout.fields.iter().enumerate() {
let local_idx = match field.cell {
Some(_) => ctx.alloc_cell_local(field.ty)?,
None => ctx.alloc_local(field.ty)?,
let field_idx_u32 = u32::try_from(field_idx)
.map_err(|_| Error::Compile("env-struct field count exceeds u32 range".to_string()))?;
helper.local_get(0);
helper.ref_cast(layout.type_idx);
helper.struct_get(layout.type_idx, field_idx_u32);
helper.local_set(local_idx);
let outer = caller.lookup(&field.name);
Symbol::new(&field.name, SymbolKind::Variable)
.with_value(Expr::WasmLocal(local_idx, field.ty))
.with_definition(outer.map_or(0, Symbol::value_definition))
.with_home(outer.and_then(Symbol::home).cloned()),
/// Emits the closure construction sequence at the caller's stack:
/// `ref.func $helper` + (env-struct construction or `ref.null any`) +
/// `struct.new $closure_<sig>`. Captures are loaded from the caller's
/// scope via `local.get` on each captured name's outer-scope local
/// index, then bundled into a `$env_<id>` struct whose ref slots into
/// the closure's nullable env field.
fn push_closure_value(
(sig, site): (ClosureSigId, i32),
func_idx: u32,
env_layout: Option<&EnvStructLayout>,
let closure_type_idx = ctx.closure_sig(sig)?.closure_type_idx;
let sig_id = i32::try_from(sig.0)
.map_err(|_| Error::Compile("closure signature id exceeds i32".to_string()))?;
emit.ref_func(func_idx);
match env_layout {
Some(layout) => emit_env_struct_construction(emit, symbols, layout)?,
None => emit.ref_null_any(),
emit.i32_const(site);
emit.i32_const(sig_id);
emit.struct_new(closure_type_idx);
fn emit_env_struct_construction(
for field in &layout.fields {
let sym = symbols.lookup(&field.name).ok_or_else(|| {
"captured name {:?} disappeared from symbol table before env-struct \
construction",
field.name
let Some(Expr::WasmLocal(idx, _)) = sym.value() else {
"captured name {:?} no longer resolves to a local at env-struct \
construction time",
emit.local_get(*idx);
emit.struct_new(layout.type_idx);
#[cfg(test)]
#[path = "emit_tests.rs"]
mod tests;