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

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

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