Lines
88.41 %
Functions
100 %
Branches
//! An eval result as its whole GC value graph, flattened in pre-order: every
//! struct field and array element, down to the scalars. It keeps what the
//! host's rendering (`decode_eval_result`) loses — a list of 1 and 2 against
//! the string "(1 2)", an i31 index against a ratio, an entity's pair fields.
//! The walk is iterative and bounded, so a deep or huge value neither
//! overflows the host stack nor runs away.
use scripting::host::ExecutionState;
use wasmtime::{AnyRef, Rooted, Store, Val};
/// Far beyond any snippet's value.
const MAX_NODES: usize = 1 << 16;
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) enum Node {
Null,
/// The module's `$t` singleton, told apart by identity.
T,
I31(i32),
I32(i32),
I64(i64),
F32(u32),
F64(u64),
/// A funcref, by whether it is null: a function's identity is its index.
Func(bool),
/// A struct with this many fields, which follow.
Struct(usize),
/// An array with this many elements, which follow.
Array(u32),
Opaque(&'static str),
}
/// `root`'s graph, telling `t` apart by identity.
pub(super) fn observe(
store: &mut Store<ExecutionState>,
t: &Rooted<AnyRef>,
root: Option<Rooted<AnyRef>>,
) -> Result<Vec<Node>, String> {
let mut nodes = Vec::new();
let mut pending = vec![Val::AnyRef(root)];
while let Some(val) = pending.pop() {
let room = MAX_NODES.saturating_sub(nodes.len() + pending.len() + 1);
nodes.push(node(store, t, val, room, &mut pending).map_err(|err| err.to_string())?);
Ok(nodes)
/// `val`'s node; a struct's fields or an array's elements go on `pending`,
/// last first, so they pop in order, and only if at most `room` of them.
fn node(
val: Val,
room: usize,
pending: &mut Vec<Val>,
) -> wasmtime::Result<Node> {
Ok(match val {
Val::I32(n) => Node::I32(n),
Val::I64(n) => Node::I64(n),
Val::F32(bits) => Node::F32(bits),
Val::F64(bits) => Node::F64(bits),
Val::FuncRef(func) => Node::Func(func.is_some()),
Val::AnyRef(None) => Node::Null,
Val::AnyRef(Some(any)) => reference(store, t, &any, room, pending)?,
Val::V128(_) => Node::Opaque("v128"),
Val::ExternRef(_) => Node::Opaque("externref"),
Val::ExnRef(_) => Node::Opaque("exnref"),
Val::ContRef(_) => Node::Opaque("contref"),
})
fn reference(
any: &Rooted<AnyRef>,
if Rooted::<AnyRef>::ref_eq(&*store, any, t)? {
return Ok(Node::T);
if let Some(i31) = any.as_i31(&*store)? {
return Ok(Node::I31(i31.get_i32()));
if let Some(record) = any.as_struct(&*store)? {
let count = record.ty(&*store)?.fields().count();
fits("a struct", count, room)?;
let fields: Vec<Val> = record.fields(&mut *store)?.collect();
pending.extend(fields.into_iter().rev());
return Ok(Node::Struct(count));
if let Some(array) = any.as_array(&*store)? {
let len = array.len(&*store)?;
fits("an array", usize::try_from(len)?, room)?;
let elems: Vec<Val> = array.elems(&mut *store)?.collect();
pending.extend(elems.into_iter().rev());
return Ok(Node::Array(len));
Ok(Node::Opaque("anyref"))
/// Refuses children that would take the value past [`MAX_NODES`].
fn fits(what: &str, children: usize, room: usize) -> wasmtime::Result<()> {
if children > room {
return Err(wasmtime::Error::msg(format!(
"{what} of {children} children exceeds the {MAX_NODES}-node budget"
)));
Ok(())