Lines
100 %
Functions
Branches
//! Compile-time guards a closure emit runs before it trusts its captures.
use wasm_encoder::ValType;
use crate::ast::Expr;
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::error::{Error, Result};
use crate::runtime::SymbolTable;
use super::super::cell_store::captured_store_error;
use super::captures::CaptureSet;
/// The closure env holds a copy of each captured variable that is not a
/// shared cell, so a store to one would be lost (ISS-001).
pub(super) fn reject_captured_stores(
ctx: &CompileContext,
symbols: &SymbolTable,
captures: &CaptureSet,
) -> Result<()> {
let stored = captures.stored_captures().find(|name| {
matches!(
symbols.lookup(name).and_then(|sym| sym.value()),
Some(Expr::WasmLocal(idx, _)) if ctx.cell_of(*idx).is_none()
)
});
match stored {
Some(name) => Err(captured_store_error(name)),
None => Ok(()),
}
/// A helper body naming a local its own pool does not own would be an invalid
/// module: an outer variable reached without being captured.
pub(super) fn reject_foreign_locals(
helper: &FunctionEmitter,
param_count: u32,
locals: &[(u32, ValType)],
let owned = u32::try_from(locals.len())
.unwrap_or(u32::MAX)
.saturating_add(param_count);
if helper.locals_addressed()? > owned {
return Err(Error::Compile(
"a closure body reaches a variable it does not capture; not supported yet".to_string(),
));
Ok(())
#[cfg(test)]
#[path = "guards_tests.rs"]
mod tests;