Skip to main content

wcmu_rejection/
wcmu_rejection.rs

1//! Demonstrate WCMU rejection.
2//!
3//! Construct a Vm with a deliberately undersized arena. The
4//! verification rejects the module before any execution can begin.
5//!
6//! Run with: `cargo run --example wcmu_rejection`
7
8use keleusma::compiler::compile;
9use keleusma::lexer::tokenize;
10use keleusma::parser::parse;
11use keleusma::vm::{Vm, VmError};
12
13const SCRIPT: &str = "
14loop main(input: i64) -> i64 {
15    let doubled = input * 2;
16    let _ignored = yield doubled;
17    doubled
18}
19";
20
21fn main() {
22    let tokens = tokenize(SCRIPT).expect("lex error");
23    let program = parse(&tokens).expect("parse error");
24    let module = compile(&program).expect("compile error");
25
26    // Try to construct a Vm with an arena too small for the program.
27    // The verification fails at Vm construction, before any code runs.
28    let arena = keleusma::Arena::with_capacity(16);
29    match Vm::new(module, &arena) {
30        Ok(_) => unreachable!("expected verification to fail"),
31        Err(VmError::VerifyError(msg)) => {
32            println!("verification rejected the module:");
33            println!("  {}", msg);
34        }
35        Err(other) => panic!("expected VerifyError, got {:?}", other),
36    }
37
38    println!();
39    println!("the host can either pre-size the arena via keleusma::Arena::with_capacity");
40    println!("or compute the required capacity from the module via auto_arena_capacity_for");
41}