1
//! Shared helpers for the categorized `codegen::*` integration tests.
2
//!
3
//! Every codegen test follows the same pattern: compile a script via
4
//! the public `Compiler`, validate the emitted wasm with `wasmparser`,
5
//! and use real production patterns (host-fn calls inside a `let*`) to
6
//! introduce runtime-typed locals. The legacy approach of injecting
7
//! `Symbol::new(...).with_value(Expr::WasmRuntime(_))` into the symbol
8
//! table is gone — it bypassed the binder-promotion contract that
9
//! `compile_for_stack(WasmRuntime)` now refuses.
10

            
11
#![allow(dead_code)]
12

            
13
use nomiscript::compiler::wasm_validation_features;
14
use nomiscript::{CompileMode, Compiler, Reader, SymbolTable};
15
use wasmparser::Validator;
16

            
17
/// Validate emitted wasm under the same feature set
18
/// `nomiscript::compiler::wasm_validation_features` gives the compiler's own
19
/// `Compiler::compile*` validation — one source of truth, not a second copy
20
/// of the same GC + function-references + exceptions list to drift apart.
21
/// Catches missing-operand bugs, malformed type sections, and ref.cast
22
/// errors that the legacy "is the magic header correct" check missed.
23
503
pub fn validate_wasm(wasm: &[u8]) {
24
503
    Validator::new_with_features(wasm_validation_features())
25
503
        .validate_all(wasm)
26
503
        .expect("wasm validation failed");
27
503
}
28

            
29
/// Compile a script in the wasm-builtins symbol table and assert the
30
/// result validates. Returns the bytes for callers that want to inspect
31
/// them further.
32
497
pub fn compile_and_validate(src: &str) -> Vec<u8> {
33
497
    let program = Reader::parse(src).unwrap_or_else(|e| panic!("parse {src:?}: {e}"));
34
497
    let mut symbols = SymbolTable::with_builtins_for_wasm();
35
497
    let mut compiler = Compiler::new();
36
497
    let wasm = compiler
37
497
        .compile(&program, &mut symbols)
38
        .unwrap_or_else(|e| panic!("compile {src:?}: {e}"));
39
497
    validate_wasm(&wasm);
40
497
    wasm
41
497
}
42

            
43
/// `catch-each` imports `__nomi_catch_each`, resolvable only in eval-mode
44
/// modules, so this compiles via `CompileMode::Eval` rather than
45
/// `compile_and_validate`'s script mode.
46
5
pub fn compile_eval_and_validate(src: &str) -> Vec<u8> {
47
5
    let program = Reader::parse(src).unwrap_or_else(|e| panic!("parse {src:?}: {e}"));
48
5
    let mut symbols = SymbolTable::with_builtins_for_wasm();
49
5
    let mut compiler = Compiler::new();
50
5
    let wasm = compiler
51
5
        .compile_with_mode(&program, &mut symbols, CompileMode::Eval)
52
        .unwrap_or_else(|e| panic!("compile {src:?}: {e}"));
53
5
    validate_wasm(&wasm);
54
5
    wasm
55
5
}
56

            
57
/// Compile a script and expect a compile error. Returns the error
58
/// message for callers that want to assert on its contents.
59
112
pub fn compile_expect_error(src: &str) -> String {
60
112
    let program = Reader::parse(src).unwrap_or_else(|e| panic!("parse {src:?}: {e}"));
61
112
    let mut symbols = SymbolTable::with_builtins_for_wasm();
62
112
    let mut compiler = Compiler::new();
63
112
    match compiler.compile(&program, &mut symbols) {
64
        Ok(_) => panic!("expected compile error for {src:?}"),
65
112
        Err(e) => e.to_string(),
66
    }
67
112
}
68

            
69
/// Wraps a script body in a `let*` that introduces a runtime-typed
70
/// `X` of `WasmType::Ratio` via the entity accessor host fn. Use this
71
/// when a test wants to exercise the runtime-arithmetic / runtime-
72
/// control-flow codegen path: any `X` inside the body resolves to a
73
/// real `WasmLocal(idx, Ratio)` and the wasm has a producer for it.
74
80
pub fn wrap_with_runtime_ratio(body: &str) -> String {
75
80
    format!("(let* ((X (transaction-post-date 0))) {body})")
76
80
}
77

            
78
/// Same as [`wrap_with_runtime_ratio`] but binds `IDX` to a runtime
79
/// `WasmType::I32`. Uses `(entity-count)` as the producer. The name
80
/// `IDX` matches the entity-accessor host fn convention (`(entity-type
81
/// IDX)` etc.) so test bodies read naturally.
82
49
pub fn wrap_with_runtime_i32(body: &str) -> String {
83
49
    format!("(let* ((IDX (entity-count))) {body})")
84
49
}
85

            
86
/// Combines [`wrap_with_runtime_ratio`] for a value `X` and
87
/// [`wrap_with_runtime_i32`] for a value `IDX`. Useful when a test
88
/// needs both a runtime ratio and a runtime entity index in scope.
89
pub fn wrap_with_runtime_ratio_and_idx(body: &str) -> String {
90
    format!("(let* ((X (transaction-post-date 0)) (IDX (entity-count))) {body})")
91
}