#![cfg(feature = "jit")]
use fusevm::{ChunkBuilder, JitCompiler, Op};
fn sum_loop(limit: i32) -> fusevm::Chunk {
let mut b = ChunkBuilder::new();
b.emit(Op::PushFrame, 1);
b.emit(Op::LoadInt(0), 1);
b.emit(Op::SetSlot(0), 1);
b.emit(Op::LoadInt(0), 1);
b.emit(Op::SetSlot(1), 1);
b.emit(Op::GetSlot(0), 1);
b.emit(Op::GetSlot(1), 1);
b.emit(Op::Add, 1);
b.emit(Op::SetSlot(0), 1);
b.emit(Op::PreIncSlotVoid(1), 1);
b.emit(Op::SlotLtIntJumpIfFalse(1, limit, 12), 1);
b.emit(Op::Jump(5), 1);
b.emit(Op::GetSlot(0), 1);
b.build()
}
#[test]
fn env_block_threshold_zero_compiles_on_first_call() {
std::env::set_var("FUSEVM_JIT_BLOCK_THRESHOLD", "0");
let handle = std::thread::spawn(|| {
let jit = JitCompiler::new();
let chunk = sum_loop(101); let mut slots = vec![0i64; 4];
jit.try_run_block(&chunk, &mut slots)
});
let first_call = handle.join().expect("worker thread panicked");
std::env::remove_var("FUSEVM_JIT_BLOCK_THRESHOLD");
assert_eq!(
first_call,
Some(5050),
"FUSEVM_JIT_BLOCK_THRESHOLD=0 should make the block JIT compile on the first invocation"
);
}