fusevm 0.14.12

Language-agnostic bytecode VM with fused superinstructions and a 3-tier Cranelift JIT (linear/block/tracing)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#![allow(clippy::approx_constant)]
//! Control-flow + arithmetic-edge tests:
//!  - Op::Jump forward/backward
//!  - Op::JumpIfTrue/False (consuming) vs JumpIfTrue/FalseKeep (peeking)
//!  - Op::Call to undefined function → VMResult::Error
//!  - Op::Return / Op::ReturnValue from top-level (no frames) → Halt
//!  - Op::Div by zero → Undef (no panic)
//!  - Op::Mod by zero → 0 (no panic)
//!  - Op::Pow with various exponents
//!  - Op::Negate on Int (incl. i64::MIN wrapping) and Float
//!  - Op::Inc / Op::Dec on Int (incl. overflow wrap)

use fusevm::{ChunkBuilder, Op, VMResult, Value, VM};

fn exec(b: ChunkBuilder) -> VMResult {
    VM::new(b.build()).run()
}

fn run(b: ChunkBuilder) -> Value {
    match exec(b) {
        VMResult::Ok(v) => v,
        VMResult::Halted => Value::Undef,
        VMResult::Error(e) => panic!("unexpected VM error: {e}"),
    }
}

// ══════════════════════════════════════════════════════════════════════════
// Op::Jump
// ══════════════════════════════════════════════════════════════════════════

#[test]
fn unconditional_jump_skips_intermediate_ops() {
    // jump 0 → 3, skipping LoadInt(99). Final stack should have only 7.
    let mut b = ChunkBuilder::new();
    b.emit(Op::Jump(3), 1);
    b.emit(Op::LoadInt(99), 1);
    b.emit(Op::LoadInt(99), 1);
    b.emit(Op::LoadInt(7), 1);
    assert!(matches!(run(b), Value::Int(7)));
}

#[test]
fn unconditional_jump_past_end_halts_cleanly() {
    let mut b = ChunkBuilder::new();
    b.emit(Op::LoadInt(5), 1);
    b.emit(Op::Jump(99), 1);
    // jump past end → IP-out-of-bounds halts the run cleanly
    let _ = exec(b); // must not panic
}

#[test]
fn backward_jump_makes_a_finite_loop_via_counter() {
    // for i in 0..3 { sum += 1 }; sum = 3
    let mut b = ChunkBuilder::new();
    // slot 0 = i, slot 1 = sum
    b.emit(Op::LoadInt(0), 1);
    b.emit(Op::SetSlot(0), 1);
    b.emit(Op::LoadInt(0), 1);
    b.emit(Op::SetSlot(1), 1);
    let loop_start = 4;
    // if i >= 3, jump to end
    b.emit(Op::GetSlot(0), 1); // 4
    b.emit(Op::LoadInt(3), 1);
    b.emit(Op::NumLt, 1);
    let jf_ip = b.emit(Op::JumpIfFalse(0), 1);
    // body: sum += 1
    b.emit(Op::GetSlot(1), 1);
    b.emit(Op::LoadInt(1), 1);
    b.emit(Op::Add, 1);
    b.emit(Op::SetSlot(1), 1);
    // i += 1
    b.emit(Op::GetSlot(0), 1);
    b.emit(Op::LoadInt(1), 1);
    b.emit(Op::Add, 1);
    b.emit(Op::SetSlot(0), 1);
    b.emit(Op::Jump(loop_start), 1);
    let after = b.emit(Op::GetSlot(1), 1);
    b.patch_jump(jf_ip, after);
    let v = run(b);
    assert!(matches!(v, Value::Int(3)));
}

// ══════════════════════════════════════════════════════════════════════════
// Op::JumpIfTrue / JumpIfFalse — consuming
// ══════════════════════════════════════════════════════════════════════════

#[test]
fn jumpiftrue_pops_condition_and_jumps_when_truthy() {
    let mut b = ChunkBuilder::new();
    b.emit(Op::LoadInt(1), 1);
    b.emit(Op::JumpIfTrue(3), 1);
    b.emit(Op::LoadInt(11), 1);
    b.emit(Op::LoadInt(22), 1); // target
    let v = run(b);
    // Cond was consumed → only 22 is on stack.
    assert!(matches!(v, Value::Int(22)));
}

#[test]
fn jumpiftrue_falls_through_when_falsy() {
    let mut b = ChunkBuilder::new();
    b.emit(Op::LoadInt(0), 1);
    b.emit(Op::JumpIfTrue(3), 1);
    b.emit(Op::LoadInt(11), 1); // executed
    b.emit(Op::LoadInt(22), 1); // also executed
    let v = run(b);
    assert!(matches!(v, Value::Int(22)));
}

#[test]
fn jumpiffalse_pops_condition_and_jumps_when_falsy() {
    let mut b = ChunkBuilder::new();
    b.emit(Op::LoadInt(0), 1);
    b.emit(Op::JumpIfFalse(3), 1);
    b.emit(Op::LoadInt(11), 1);
    b.emit(Op::LoadInt(22), 1);
    assert!(matches!(run(b), Value::Int(22)));
}

#[test]
fn jumpiffalse_falls_through_when_truthy() {
    let mut b = ChunkBuilder::new();
    b.emit(Op::LoadInt(1), 1);
    b.emit(Op::JumpIfFalse(3), 1);
    b.emit(Op::LoadInt(11), 1);
    b.emit(Op::LoadInt(22), 1);
    assert!(matches!(run(b), Value::Int(22)));
}

// ══════════════════════════════════════════════════════════════════════════
// Op::JumpIfTrueKeep / JumpIfFalseKeep — peek (preserve stack)
// ══════════════════════════════════════════════════════════════════════════

#[test]
fn jumpiftruekeep_preserves_top_of_stack_on_jump() {
    let mut b = ChunkBuilder::new();
    b.emit(Op::LoadInt(7), 1);
    b.emit(Op::JumpIfTrueKeep(3), 1);
    b.emit(Op::LoadInt(99), 1);
    // landing point — 7 should still be on top
    let v = run(b);
    assert!(matches!(v, Value::Int(7)));
}

#[test]
fn jumpiftruekeep_does_not_jump_when_falsy_and_keeps_value() {
    let mut b = ChunkBuilder::new();
    b.emit(Op::LoadInt(0), 1);
    b.emit(Op::JumpIfTrueKeep(4), 1);
    b.emit(Op::Pop, 1);
    b.emit(Op::LoadInt(123), 1);
    let v = run(b);
    assert!(matches!(v, Value::Int(123)));
}

#[test]
fn jumpiffalsekeep_preserves_top_of_stack_on_jump() {
    let mut b = ChunkBuilder::new();
    b.emit(Op::LoadInt(0), 1);
    b.emit(Op::JumpIfFalseKeep(3), 1);
    b.emit(Op::LoadInt(99), 1);
    let v = run(b);
    // 0 was kept on stack; we jumped over the LoadInt(99).
    assert!(matches!(v, Value::Int(0)));
}

#[test]
fn jumpiffalsekeep_does_not_jump_when_truthy_and_keeps_value() {
    let mut b = ChunkBuilder::new();
    b.emit(Op::LoadInt(5), 1);
    b.emit(Op::JumpIfFalseKeep(4), 1);
    b.emit(Op::Pop, 1);
    b.emit(Op::LoadInt(123), 1);
    let v = run(b);
    assert!(matches!(v, Value::Int(123)));
}

// ══════════════════════════════════════════════════════════════════════════
// Short-circuit `&&` / `||` patterns expressible with JumpIf*Keep
// ══════════════════════════════════════════════════════════════════════════

#[test]
fn short_circuit_and_pattern_a_false_skips_b() {
    // emulate `0 && pop+99` → result 0
    let mut b = ChunkBuilder::new();
    b.emit(Op::LoadInt(0), 1);
    let j = b.emit(Op::JumpIfFalseKeep(0), 1);
    b.emit(Op::Pop, 1);
    b.emit(Op::LoadInt(99), 1);
    let end = b.emit(Op::Nop, 1);
    b.patch_jump(j, end);
    assert!(matches!(run(b), Value::Int(0)));
}

#[test]
fn short_circuit_or_pattern_a_true_skips_b() {
    // emulate `5 || pop+99` → result 5
    let mut b = ChunkBuilder::new();
    b.emit(Op::LoadInt(5), 1);
    let j = b.emit(Op::JumpIfTrueKeep(0), 1);
    b.emit(Op::Pop, 1);
    b.emit(Op::LoadInt(99), 1);
    let end = b.emit(Op::Nop, 1);
    b.patch_jump(j, end);
    assert!(matches!(run(b), Value::Int(5)));
}

// ══════════════════════════════════════════════════════════════════════════
// Op::Call to undefined function
// ══════════════════════════════════════════════════════════════════════════

#[test]
fn call_undefined_function_returns_error() {
    let mut b = ChunkBuilder::new();
    let n = b.add_name("does_not_exist");
    b.emit(Op::Call(n, 0), 1);
    let result = VM::new(b.build()).run();
    match result {
        VMResult::Error(e) => {
            assert!(
                e.contains("does_not_exist") || e.contains("undefined"),
                "error message should mention the function: {e}"
            );
        }
        other => panic!("expected Error, got {:?}", other),
    }
}

// ══════════════════════════════════════════════════════════════════════════
// Op::Return / Op::ReturnValue from top-level (no frames)
// ══════════════════════════════════════════════════════════════════════════

#[test]
fn return_at_top_level_halts() {
    let mut b = ChunkBuilder::new();
    b.emit(Op::Return, 1);
    b.emit(Op::LoadInt(99), 1); // unreachable
    let r = exec(b);
    assert!(matches!(r, VMResult::Halted | VMResult::Ok(_)));
}

#[test]
fn returnvalue_at_top_level_returns_value() {
    let mut b = ChunkBuilder::new();
    b.emit(Op::LoadInt(7), 1);
    b.emit(Op::ReturnValue, 1);
    b.emit(Op::LoadInt(99), 1); // unreachable
    match exec(b) {
        VMResult::Ok(Value::Int(7)) => {}
        VMResult::Halted => {}
        other => panic!("expected Int(7) or Halted, got {:?}", other),
    }
}

// ══════════════════════════════════════════════════════════════════════════
// Op::Div by zero / Op::Mod by zero — must not panic
// ══════════════════════════════════════════════════════════════════════════

#[test]
fn div_int_by_zero_yields_undef_no_panic() {
    let mut b = ChunkBuilder::new();
    b.emit(Op::LoadInt(10), 1);
    b.emit(Op::LoadInt(0), 1);
    b.emit(Op::Div, 1);
    let v = run(b);
    assert!(matches!(v, Value::Undef));
}

#[test]
fn div_float_by_zero_yields_undef_no_panic() {
    let mut b = ChunkBuilder::new();
    let a = b.add_constant(Value::Float(3.14));
    let z = b.add_constant(Value::Float(0.0));
    b.emit(Op::LoadConst(a), 1);
    b.emit(Op::LoadConst(z), 1);
    b.emit(Op::Div, 1);
    let v = run(b);
    assert!(matches!(v, Value::Undef));
}

#[test]
fn div_normal_returns_float_quotient() {
    let mut b = ChunkBuilder::new();
    b.emit(Op::LoadInt(7), 1);
    b.emit(Op::LoadInt(2), 1);
    b.emit(Op::Div, 1);
    match run(b) {
        Value::Float(f) => assert!((f - 3.5).abs() < 1e-9),
        other => panic!("expected Float(3.5), got {:?}", other),
    }
}

#[test]
fn mod_by_zero_yields_zero_no_panic() {
    let mut b = ChunkBuilder::new();
    b.emit(Op::LoadInt(10), 1);
    b.emit(Op::LoadInt(0), 1);
    b.emit(Op::Mod, 1);
    // Behavior: arith_int_fast returns 0 in int path.
    let v = run(b);
    assert!(matches!(v, Value::Int(0) | Value::Float(_) | Value::Undef));
}

#[test]
fn mod_basic_int_result() {
    let mut b = ChunkBuilder::new();
    b.emit(Op::LoadInt(17), 1);
    b.emit(Op::LoadInt(5), 1);
    b.emit(Op::Mod, 1);
    assert!(matches!(run(b), Value::Int(2)));
}

// ══════════════════════════════════════════════════════════════════════════
// Op::Pow
// ══════════════════════════════════════════════════════════════════════════

#[test]
fn pow_int_base_int_exp_returns_float() {
    let mut b = ChunkBuilder::new();
    b.emit(Op::LoadInt(2), 1);
    b.emit(Op::LoadInt(10), 1);
    b.emit(Op::Pow, 1);
    match run(b) {
        Value::Float(f) => assert_eq!(f, 1024.0),
        other => panic!("expected Float(1024.0), got {:?}", other),
    }
}

#[test]
fn pow_zero_exponent_is_one() {
    let mut b = ChunkBuilder::new();
    b.emit(Op::LoadInt(7), 1);
    b.emit(Op::LoadInt(0), 1);
    b.emit(Op::Pow, 1);
    match run(b) {
        Value::Float(f) => assert_eq!(f, 1.0),
        other => panic!("expected Float(1.0), got {:?}", other),
    }
}

#[test]
fn pow_negative_exponent_is_reciprocal_form() {
    let mut b = ChunkBuilder::new();
    b.emit(Op::LoadInt(2), 1);
    b.emit(Op::LoadInt(-2), 1);
    b.emit(Op::Pow, 1);
    match run(b) {
        Value::Float(f) => assert!((f - 0.25).abs() < 1e-9),
        other => panic!("expected Float(0.25), got {:?}", other),
    }
}

#[test]
fn pow_with_float_exponent() {
    let mut b = ChunkBuilder::new();
    b.emit(Op::LoadInt(4), 1);
    let half = b.add_constant(Value::Float(0.5));
    b.emit(Op::LoadConst(half), 1);
    b.emit(Op::Pow, 1);
    match run(b) {
        Value::Float(f) => assert!((f - 2.0).abs() < 1e-9),
        other => panic!("expected Float(2.0), got {:?}", other),
    }
}

// ══════════════════════════════════════════════════════════════════════════
// Op::Negate
// ══════════════════════════════════════════════════════════════════════════

#[test]
fn negate_int_flips_sign() {
    let mut b = ChunkBuilder::new();
    b.emit(Op::LoadInt(42), 1);
    b.emit(Op::Negate, 1);
    assert!(matches!(run(b), Value::Int(-42)));
}

#[test]
fn negate_zero_is_zero() {
    let mut b = ChunkBuilder::new();
    b.emit(Op::LoadInt(0), 1);
    b.emit(Op::Negate, 1);
    assert!(matches!(run(b), Value::Int(0)));
}

#[test]
fn negate_i64_min_wraps_via_wrapping_neg() {
    let mut b = ChunkBuilder::new();
    let c = b.add_constant(Value::Int(i64::MIN));
    b.emit(Op::LoadConst(c), 1);
    b.emit(Op::Negate, 1);
    // wrapping_neg(i64::MIN) == i64::MIN
    assert!(matches!(run(b), Value::Int(x) if x == i64::MIN));
}

#[test]
fn negate_float_flips_sign() {
    let mut b = ChunkBuilder::new();
    let c = b.add_constant(Value::Float(3.14));
    b.emit(Op::LoadConst(c), 1);
    b.emit(Op::Negate, 1);
    match run(b) {
        Value::Float(f) => assert!((f + 3.14).abs() < 1e-9),
        other => panic!("expected Float(-3.14), got {:?}", other),
    }
}

// ══════════════════════════════════════════════════════════════════════════
// Op::Inc / Op::Dec on Int (wrapping)
// ══════════════════════════════════════════════════════════════════════════

#[test]
fn inc_i64_max_wraps_to_i64_min() {
    let mut b = ChunkBuilder::new();
    let c = b.add_constant(Value::Int(i64::MAX));
    b.emit(Op::LoadConst(c), 1);
    b.emit(Op::Inc, 1);
    assert!(matches!(run(b), Value::Int(x) if x == i64::MIN));
}

#[test]
fn dec_i64_min_wraps_to_i64_max() {
    let mut b = ChunkBuilder::new();
    let c = b.add_constant(Value::Int(i64::MIN));
    b.emit(Op::LoadConst(c), 1);
    b.emit(Op::Dec, 1);
    assert!(matches!(run(b), Value::Int(x) if x == i64::MAX));
}

#[test]
fn inc_on_string_numeric_coerces_then_increments() {
    let mut b = ChunkBuilder::new();
    let c = b.add_constant(Value::str("99"));
    b.emit(Op::LoadConst(c), 1);
    b.emit(Op::Inc, 1);
    assert!(matches!(run(b), Value::Int(100)));
}

#[test]
fn dec_on_undef_coerces_to_zero_minus_one() {
    let mut b = ChunkBuilder::new();
    let c = b.add_constant(Value::Undef);
    b.emit(Op::LoadConst(c), 1);
    b.emit(Op::Dec, 1);
    assert!(matches!(run(b), Value::Int(-1)));
}