Lines
100 %
Functions
Branches
//! The `$any_call_<shape>` dispatch helpers a call of an `any` value rides
//! (PERSIST Q46-2): one function per argument shape, reserved at the first
//! call of that shape and filled once the unit's last body has compiled.
use super::{ClosureSig, HelperSlot};
use crate::ast::WasmType;
use crate::compiler::context::CompileContext;
use crate::error::Result;
#[derive(Debug, Clone)]
pub(crate) struct AnyCallHelper {
pub shape: Vec<WasmType>,
pub func_idx: u32,
pub slot: HelperSlot,
}
impl CompileContext {
/// The function index of the helper for `shape`, registered with its code
/// slot reserved on first use.
pub(crate) fn any_call_helper(&mut self, shape: &[WasmType]) -> Result<u32> {
if let Some(helper) = self.closures.any_calls.iter().find(|h| h.shape == shape) {
return Ok(helper.func_idx);
let name = format!("__any_call_{}", self.closures.any_calls.len());
let anyref = self.anyref();
let mut params = vec![anyref];
for arg in shape {
params.push(self.wasm_val_type(*arg)?);
let func_idx = self.register_function(&name, ¶ms, &[anyref])?;
let slot = self.reserve_helper();
self.closures.any_calls.push(AnyCallHelper {
shape: shape.to_vec(),
func_idx,
slot,
});
Ok(func_idx)
/// The helpers still to be filled.
pub(crate) fn take_any_call_helpers(&mut self) -> Vec<AnyCallHelper> {
std::mem::take(&mut self.closures.any_calls)
/// Every closure signature of `arity` parameters, in interning order.
pub(crate) fn closure_sigs_of_arity(&self, arity: usize) -> Vec<ClosureSig> {
self.closures
.by_id
.iter()
.filter(|sig| sig.params.len() == arity)
.cloned()
.collect()