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

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

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