use chialisp::compiler::debug_metadata::{
DebugMetadataCollection, SerializedFrame, StackFrameStyle,
};
use clvmr::{
Allocator, NodePtr, diagnostics::run_program_with_diagnostics, error::EvalErr,
reduction::Reduction, serde::node_to_bytes,
};
use rue_lir::DebugDialect;
use crate::compact::compact_trace;
const MAX_FRAMES: usize = 64;
#[derive(Debug, Default, Clone)]
pub struct Sidecars {
collection: DebugMetadataCollection,
}
impl Sidecars {
pub fn insert(&mut self, sidecar: &[u8]) -> Result<(), String> {
self.collection.insert(sidecar)
}
}
#[derive(Debug)]
pub struct Failure {
pub error: EvalErr,
pub trace: Result<String, String>,
}
pub fn run_with_trace(
allocator: &mut Allocator,
dialect: &DebugDialect,
program: NodePtr,
env: NodePtr,
max_cost: u64,
sidecars: &Sidecars,
) -> Result<Reduction, Failure> {
let failure = match run_program_with_diagnostics(
allocator, dialect, program, env, max_cost, MAX_FRAMES,
) {
Ok(reduction) => return Ok(reduction),
Err(failure) => failure,
};
let trace = failure
.frames
.iter()
.map(|frame| {
Ok(SerializedFrame {
program: node_to_bytes(allocator, frame.program)?,
environment: node_to_bytes(allocator, frame.environment)?,
})
})
.collect::<Result<Vec<_>, EvalErr>>()
.map_err(|error| error.to_string())
.and_then(|frames| {
sidecars.collection.format_captured_stack(
&frames,
failure.truncated,
StackFrameStyle::Python,
)
})
.map(|trace| compact_trace(&trace));
Err(Failure {
error: failure.error,
trace,
})
}
#[cfg(test)]
mod tests {
use std::{fmt::Write, fs, path::Path};
use chialisp::compiler::debug_metadata::DebugMetadata;
use expect_test::expect_file;
use rue_clvm::{assemble, disassemble_value};
use rue_compiler::{Compiler, FileTree, normalize_path};
use rue_options::CompilerOptions;
use super::*;
#[test]
fn traces() {
let cases =
Path::new(env!("CARGO_MANIFEST_DIR")).join("../rue-compiler/tests/debug_metadata");
let mut paths = fs::read_dir(&cases)
.unwrap()
.map(|entry| entry.unwrap().path())
.filter(|path| path.extension().is_some_and(|extension| extension == "rue"))
.collect::<Vec<_>>();
paths.sort();
for path in paths {
let source = fs::read_to_string(&path).unwrap();
let solutions = source
.lines()
.map_while(|line| line.strip_prefix("// solution: "))
.collect::<Vec<_>>();
assert!(!solutions.is_empty(), "{} has no solution", path.display());
let mut snapshot = String::new();
for (build, options) in [
("debug", CompilerOptions::debug()),
("release", CompilerOptions::default()),
] {
let base_path = path.parent().unwrap().canonicalize().unwrap();
let kind = normalize_path(&path).unwrap();
let mut ctx = Compiler::new(options);
let tree = FileTree::compile_file(&mut ctx, &path).unwrap();
let mut allocator = Allocator::new();
let (program, metadata) = tree
.main_with_debug_metadata(&mut ctx, &mut allocator, &kind, base_path)
.unwrap()
.unwrap();
DebugMetadata::decode(&metadata)
.unwrap()
.verify_program(&node_to_bytes(&allocator, program).unwrap())
.unwrap();
let mut sidecars = Sidecars::default();
sidecars.insert(&metadata).unwrap();
for solution in &solutions {
let env = assemble(&mut allocator, solution).unwrap();
writeln!(snapshot, "===== {build}: {solution} =====").unwrap();
match run_with_trace(
&mut allocator,
&DebugDialect::new(rue_compiler::clvm_target(&options).flags(), false),
program,
env,
rue_lir::MAX_BLOCK_COST,
&sidecars,
) {
Ok(output) => {
writeln!(
snapshot,
"output: {}",
disassemble_value(&allocator, output.1)
)
.unwrap();
}
Err(failure) => {
writeln!(snapshot, "error: {}", failure.error).unwrap();
writeln!(snapshot, "{}", failure.trace.unwrap()).unwrap();
}
}
}
}
let name = path.file_stem().unwrap().to_string_lossy().to_string();
expect_file![
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/traces")
.join(format!("{name}.txt"))
]
.assert_eq(&snapshot);
}
}
}