Lines
97.06 %
Functions
75 %
Branches
100 %
//! The text behind `env.log_value`: the argument list a PRINT / DISPLAY /
//! DEBUG call builds at run time, decoded with the eval decoder and printed
//! like the compile-time path prints constants.
use nomiscript::{PairElement, Value, format_value_plain};
use wasmtime::{AnyRef, AsContextMut, Caller, Rooted};
use super::decode::EvalValue;
use super::structure::{InstanceMode, decode_cars};
use super::type_handles::resolve_caller_type_handles;
/// The one log message for `args`, a `$pair` list of the printed values:
/// each printed `princ`-style, joined by a space. `None` when a value in it
/// cannot be read (compound money, a value of no known kind): the caller
/// reports that to the guest, never as text.
pub fn logged_values<T>(
caller: &mut Caller<'_, T>,
args: Option<Rooted<AnyRef>>,
) -> wasmtime::Result<Option<String>> {
let Some(head) = args else {
return Ok(Some(String::new()));
};
let handles = resolve_caller_type_handles(caller)?;
let Ok(items) = decode_cars(
caller.as_context_mut(),
head,
PairElement::AnyRef,
&handles,
InstanceMode::Contents,
0,
) else {
return Ok(None);
Ok(printed(items))
}
fn printed(items: Vec<EvalValue>) -> Option<String> {
if items.iter().any(has_opaque) {
return None;
let texts: Vec<_> = items
.into_iter()
.map(|item| format_value_plain(&Value::from(item)))
.collect();
Some(texts.join(" "))
fn has_opaque(value: &EvalValue) -> bool {
match value {
EvalValue::Opaque => true,
EvalValue::List(items) => items.iter().any(has_opaque),
EvalValue::Struct(view) => view.fields.iter().any(|(_, field)| has_opaque(field)),
_ => false,
#[cfg(test)]
#[path = "log_value_tests.rs"]
mod tests;