harn-vm 0.8.1

Async bytecode virtual machine for the Harn programming language
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
use std::rc::Rc;

use crate::stdlib::register_vm_stdlib;
use crate::values_equal;
use crate::{Chunk, VmError, VmValue};

use super::*;

/// Drive the VM forward from a `start()`ed chunk until it reaches
/// the first breakpoint (or exhausts the step budget). Returns the
/// VM positioned in whatever frame the breakpoint lives in. Used
/// by the `evaluate_in_frame` tests below so we can inspect a paused
/// scope without wiring a full DAP session.
fn run_until_paused(vm: &mut Vm, chunk: &Chunk) {
    vm.start(chunk);
    let rt = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap();
    rt.block_on(async {
        let local = tokio::task::LocalSet::new();
        local
            .run_until(async {
                for _ in 0..10_000 {
                    if vm.is_stopped() {
                        return;
                    }
                    match vm.step_execute().await {
                        Ok(Some((_, true))) => return,
                        Ok(_) => continue,
                        Err(e) => panic!("step_execute failed: {e}"),
                    }
                }
                panic!("run_until_paused: step budget exceeded");
            })
            .await
    })
}

/// Synchronously evaluate an expression on an already-paused VM.
/// Mirrors what harn-dap's `handle_evaluate` will do on the async
/// runtime it already owns.
fn eval(vm: &mut Vm, expr: &str) -> Result<VmValue, VmError> {
    let rt = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap();
    rt.block_on(async {
        let local = tokio::task::LocalSet::new();
        local.run_until(vm.evaluate_in_frame(expr, 0)).await
    })
}

#[test]
fn test_evaluate_in_frame_literal() {
    // Need a live frame for evaluate_in_frame, even for a pure
    // expression, because the scratch chunk inherits source info
    // from the top frame. Seed one by compiling & starting an empty
    // pipeline that just waits on a breakpoint.
    let mut vm = Vm::new();
    register_vm_stdlib(&mut vm);
    vm.set_breakpoints(vec![2]);
    let chunk = crate::compile_source("let __seed__: int = 0\nlog(__seed__)\n").unwrap();
    run_until_paused(&mut vm, &chunk);

    assert!(values_equal(
        &eval(&mut vm, "1 + 2").unwrap(),
        &VmValue::Int(3)
    ));
    assert!(values_equal(
        &eval(&mut vm, "\"hi\" + \" there\"").unwrap(),
        &VmValue::String(Rc::from("hi there"))
    ));
    assert!(values_equal(
        &eval(&mut vm, "5 > 3 && 2 < 4").unwrap(),
        &VmValue::Bool(true)
    ));
}

#[test]
fn test_evaluate_in_frame_sees_locals() {
    let mut vm = Vm::new();
    register_vm_stdlib(&mut vm);
    vm.set_breakpoints(vec![3]);
    let chunk =
        crate::compile_source("let user: string = \"alice\"\nlet count: int = 42\nlog(count)\n")
            .unwrap();
    run_until_paused(&mut vm, &chunk);

    assert!(values_equal(
        &eval(&mut vm, "user").unwrap(),
        &VmValue::String(Rc::from("alice"))
    ));
    assert!(values_equal(
        &eval(&mut vm, "count * 2").unwrap(),
        &VmValue::Int(84)
    ));
    assert!(values_equal(
        &eval(&mut vm, "user + \" has \" + to_string(count)").unwrap(),
        &VmValue::String(Rc::from("alice has 42"))
    ));
}

#[test]
fn test_evaluate_in_frame_does_not_leak_state() {
    // Evaluation must be transparent to the live session — no
    // scope leftovers, no stack residue, no step-mode drift.
    let mut vm = Vm::new();
    register_vm_stdlib(&mut vm);
    vm.set_breakpoints(vec![2]);
    let chunk = crate::compile_source("let x: int = 7\nlog(x)\n").unwrap();
    run_until_paused(&mut vm, &chunk);

    let pre_stack = vm.stack.len();
    let pre_frames = vm.frames.len();
    let pre_scope = vm.env.scope_depth();
    let _ = eval(&mut vm, "x + 100").unwrap();
    let _ = eval(&mut vm, "x * x").unwrap();
    assert_eq!(vm.stack.len(), pre_stack);
    assert_eq!(vm.frames.len(), pre_frames);
    assert_eq!(vm.env.scope_depth(), pre_scope);
    // The synthetic `__harn_eval_result__` binding must not linger
    // in the paused scope.
    assert!(vm.env.get("__harn_eval_result__").is_none());
}

#[test]
fn test_set_variable_in_frame_updates_let_binding() {
    // Pipeline authors overwhelmingly use `let`; the debug
    // setVariable path must bypass immutability or it's useless.
    let mut vm = Vm::new();
    register_vm_stdlib(&mut vm);
    vm.set_breakpoints(vec![3]);
    let chunk =
        crate::compile_source("let count: int = 7\nlet label: string = \"before\"\nlog(count)\n")
            .unwrap();
    run_until_paused(&mut vm, &chunk);

    let rt = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap();
    let stored = rt.block_on(async {
        let local = tokio::task::LocalSet::new();
        local
            .run_until(vm.set_variable_in_frame("count", "42", 0))
            .await
    });
    assert!(values_equal(&stored.unwrap(), &VmValue::Int(42)));
    assert!(values_equal(
        &eval(&mut vm, "count").unwrap(),
        &VmValue::Int(42)
    ));

    // Expression RHS — not just literals.
    let rt = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap();
    rt.block_on(async {
        let local = tokio::task::LocalSet::new();
        local
            .run_until(vm.set_variable_in_frame("label", "\"x\" + to_string(count)", 0))
            .await
            .unwrap()
    });
    assert!(values_equal(
        &eval(&mut vm, "label").unwrap(),
        &VmValue::String(Rc::from("x42"))
    ));
}

#[test]
fn test_set_variable_in_frame_rejects_undefined() {
    let mut vm = Vm::new();
    register_vm_stdlib(&mut vm);
    vm.set_breakpoints(vec![2]);
    let chunk = crate::compile_source("let x: int = 1\nlog(x)\n").unwrap();
    run_until_paused(&mut vm, &chunk);

    let rt = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap();
    let err = rt
        .block_on(async {
            let local = tokio::task::LocalSet::new();
            local
                .run_until(vm.set_variable_in_frame("ghost", "0", 0))
                .await
        })
        .unwrap_err();
    let msg = err.to_string();
    assert!(
        msg.contains("ghost"),
        "expected 'ghost' in error, got {msg}"
    );
}

#[test]
fn test_restart_frame_rewinds_ip_and_rebinds_args() {
    // Pause inside a function, mutate a local, restart the frame
    // — the mutation must vanish and execution must resume from
    // the top of the function with the original args.
    let mut vm = Vm::new();
    register_vm_stdlib(&mut vm);
    vm.set_breakpoints(vec![3]);
    let chunk = crate::compile_source(
        "fn inner(n: int) -> int { \n  let doubled: int = n * 2\n  log(doubled)\n  return doubled\n}\nlog(inner(21))\n",
    )
    .unwrap();
    run_until_paused(&mut vm, &chunk);

    // We're paused at line 3 inside `inner`. Mutate the local so
    // we can assert the restart wiped it.
    let rt = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap();
    rt.block_on(async {
        let local = tokio::task::LocalSet::new();
        local
            .run_until(vm.set_variable_in_frame("doubled", "999", 0))
            .await
            .unwrap()
    });
    assert!(values_equal(
        &eval(&mut vm, "doubled").unwrap(),
        &VmValue::Int(999)
    ));

    // restart_frame(top_frame_index) rewinds `inner` to entry.
    let top = vm.frame_count() - 1;
    vm.restart_frame(top).unwrap();

    // `doubled` no longer exists because the function's scope was
    // blown away, but `n` should still be bound from the re-applied
    // arg.
    assert!(values_equal(
        &eval(&mut vm, "n").unwrap(),
        &VmValue::Int(21)
    ));
}

#[test]
fn test_restart_frame_rejects_scratch_frames() {
    let mut vm = Vm::new();
    register_vm_stdlib(&mut vm);
    vm.set_breakpoints(vec![2]);
    let chunk = crate::compile_source("let x: int = 1\nlog(x)\n").unwrap();
    run_until_paused(&mut vm, &chunk);
    // The top-level pipeline frame has `initial_env: Some(_)` so
    // restartFrame *is* valid there — our script has no inner
    // function yet. Push a synthetic scratch frame via
    // evaluate_in_frame (which leaves no live frame when done),
    // then attempt restart on an out-of-range id.
    let err = vm.restart_frame(99).unwrap_err();
    assert!(err.to_string().contains("out of range"));
}

#[test]
fn test_signal_cancel_unwinds_step_loop() {
    let mut vm = Vm::new();
    register_vm_stdlib(&mut vm);
    // A busy-looping pipeline that would never terminate under a
    // normal run; signal cancel before stepping so the first
    // instruction check throws VmError::Thrown with the
    // cancelled kind.
    let chunk =
        crate::compile_source("pipeline t(task) { var i = 0\n while i < 1000000 { i = i + 1 } }\n")
            .unwrap();
    vm.start(&chunk);
    vm.signal_cancel();
    let rt = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap();
    let result = rt.block_on(async {
        let local = tokio::task::LocalSet::new();
        local.run_until(vm.step_execute()).await
    });
    match result {
        Err(VmError::Thrown(VmValue::String(s))) => {
            assert!(
                s.contains("kind:cancelled:"),
                "cancellation must surface as a kind-tagged Thrown error"
            );
        }
        other => panic!("expected cancelled Thrown, got {other:?}"),
    }
}

#[test]
fn test_function_breakpoint_stops_on_entry() {
    let mut vm = Vm::new();
    register_vm_stdlib(&mut vm);
    vm.set_function_breakpoints(vec!["do_work".to_string()]);
    let chunk = crate::compile_source(
        "fn do_work(n: int) -> int { return n + 1 }\npipeline t(task) { let x = do_work(41)\nlog(x) }\n",
    )
    .unwrap();
    run_until_paused(&mut vm, &chunk);
    // The latch must identify the matching function and get
    // drained exactly once.
    let hit = vm.take_pending_function_bp().expect("must latch a hit");
    assert_eq!(hit, "do_work");
    assert!(vm.take_pending_function_bp().is_none(), "one-shot");

    // The top frame should be `do_work` at entry.
    let frames = vm.debug_stack_frames();
    let top = frames.last().expect("callee frame on stack");
    assert_eq!(top.0, "do_work");
}

#[test]
fn test_generated_module_source_is_cached_and_tagged_for_debugger() {
    let source = "pub fn generated_answer() {\n  return 42\n}\n";
    let mut vm = Vm::new();
    let rt = tokio::runtime::Builder::new_current_thread()
        .build()
        .unwrap();
    let exports = rt
        .block_on(vm.load_module_exports_from_source("<generated>/wrapper.harn", source))
        .expect("generated module loads");

    assert_eq!(
        vm.debug_source_for_path("<generated>/wrapper.harn")
            .as_deref(),
        Some(source)
    );
    let closure = exports
        .get("generated_answer")
        .expect("exported generated function");
    assert_eq!(
        closure.func.chunk.source_file.as_deref(),
        Some("<generated>/wrapper.harn")
    );
}

#[test]
fn test_function_breakpoint_unknown_name_does_not_fire() {
    let mut vm = Vm::new();
    register_vm_stdlib(&mut vm);
    vm.set_function_breakpoints(vec!["nonexistent".to_string()]);
    let chunk = crate::compile_source("pipeline t(task) { let x = 1\nlog(x) }\n").unwrap();
    // With no matching callee, the program runs to completion
    // without latching a hit; run_until_paused would have panicked
    // with "step budget exceeded" if the VM idled, so wrap with a
    // finite run of step_execute until a natural terminate.
    vm.start(&chunk);
    let rt = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap();
    rt.block_on(async {
        let local = tokio::task::LocalSet::new();
        local
            .run_until(async {
                for _ in 0..10_000 {
                    match vm.step_execute().await {
                        Ok(Some((_, false))) => return,
                        Ok(_) => continue,
                        Err(e) => panic!("step_execute failed: {e}"),
                    }
                }
                panic!("step budget exceeded");
            })
            .await
    });
    assert!(vm.take_pending_function_bp().is_none());
}

#[test]
fn test_evaluate_in_frame_parse_error_is_surfaced_standalone() {
    let mut vm = Vm::new();
    register_vm_stdlib(&mut vm);
    vm.set_breakpoints(vec![1]);
    let chunk = crate::compile_source("log(0)\n").unwrap();
    run_until_paused(&mut vm, &chunk);

    let err = eval(&mut vm, "(\"unterminated").unwrap_err();
    let msg = err.to_string();
    assert!(
        msg.contains("evaluate:"),
        "expected evaluate error prefix, got: {msg}"
    );
}

#[test]
fn test_breakpoints_wildcard_matches_any_file() {
    let mut vm = Vm::new();
    vm.set_breakpoints(vec![3, 7]);
    assert!(vm.breakpoint_matches(3));
    assert!(vm.breakpoint_matches(7));
    assert!(!vm.breakpoint_matches(4));
}

#[test]
fn test_breakpoints_per_file_does_not_leak_to_wildcard() {
    let mut vm = Vm::new();
    vm.set_breakpoints_for_file("auto.harn", vec![10]);
    // Without an active frame, only the empty-string key matches; a
    // file-scoped breakpoint must NOT fire when no frame is active.
    assert!(!vm.breakpoint_matches(10));
}

#[test]
fn test_breakpoints_per_file_clear_on_empty() {
    let mut vm = Vm::new();
    vm.set_breakpoints_for_file("a.harn", vec![1, 2]);
    vm.set_breakpoints_for_file("a.harn", vec![]);
    assert!(!vm.breakpoints.contains_key("a.harn"));
}