1
//! `COVERAGE-DUMP` — snapshot of per-native-fn compile-time
2
//! reference counts populated by the compiler's host-fn emit path.
3
//! Returns a string with one `name count` line per referenced native,
4
//! sorted alphabetically so test runners can diff the output for
5
//! stable assertions. Empty output means no native fn calls were
6
//! emitted in the session (typical for the language sandbox).
7

            
8
use crate::ast::{Expr, WasmType};
9
use crate::compiler::context::CompileContext;
10
use crate::compiler::emit::FunctionEmitter;
11
use crate::compiler::expr::compile_expr;
12
use crate::error::{Error, Result};
13
use crate::runtime::SymbolTable;
14

            
15
use super::compile_static_result_for_stack;
16

            
17
832
pub(super) fn coverage_dump(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
18
832
    if !args.is_empty() {
19
        return Err(Error::Arity {
20
            name: "COVERAGE-DUMP".to_string(),
21
            expected: 0,
22
            actual: args.len(),
23
        });
24
832
    }
25
832
    let mut entries: Vec<(&String, &u32)> = symbols.native_coverage().iter().collect();
26
832
    entries.sort_by(|a, b| a.0.cmp(b.0));
27
832
    let body = entries
28
832
        .into_iter()
29
208
        .map(|(name, count)| format!("{name} {count}"))
30
        .collect::<Vec<_>>()
31
832
        .join("\n");
32
832
    Ok(Expr::String(body))
33
832
}
34

            
35
208
pub(super) fn compile_coverage_dump(
36
208
    ctx: &mut CompileContext,
37
208
    emit: &mut FunctionEmitter,
38
208
    symbols: &mut SymbolTable,
39
208
    args: &[Expr],
40
208
) -> Result<()> {
41
208
    let result = coverage_dump(symbols, args)?;
42
208
    compile_expr(ctx, emit, symbols, &result)
43
208
}
44

            
45
624
pub(super) fn compile_coverage_dump_for_stack(
46
624
    ctx: &mut CompileContext,
47
624
    emit: &mut FunctionEmitter,
48
624
    symbols: &mut SymbolTable,
49
624
    args: &[Expr],
50
624
) -> Result<WasmType> {
51
624
    let result = coverage_dump(symbols, args)?;
52
624
    compile_static_result_for_stack(ctx, emit, symbols, &result)
53
624
}