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

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

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