Skip to main content

harn_vm/vm/
state.rs

1use std::collections::{BTreeMap, HashMap, HashSet};
2use std::sync::Arc;
3use std::time::Instant;
4
5use crate::chunk::{Chunk, ChunkRef, Constant};
6use crate::runtime_limits::RuntimeLimits;
7use crate::value::{
8    ModuleFunctionRegistry, VmAsyncBuiltinFn, VmBuiltinFn, VmEnv, VmError, VmTaskHandle, VmValue,
9};
10use crate::BuiltinId;
11
12use super::debug::DebugHook;
13use super::modules::LoadedModule;
14use super::VmBuiltinMetadata;
15
16/// RAII guard that starts a tracing span on creation and ends it on drop.
17pub(crate) struct ScopeSpan(u64);
18
19impl ScopeSpan {
20    pub(crate) fn new(kind: crate::tracing::SpanKind, name: String) -> Self {
21        Self(crate::tracing::span_start(kind, name))
22    }
23}
24
25impl Drop for ScopeSpan {
26    fn drop(&mut self) {
27        crate::tracing::span_end(self.0);
28    }
29}
30
31#[derive(Clone)]
32pub(crate) struct LocalSlot {
33    pub(crate) value: VmValue,
34    pub(crate) initialized: bool,
35    pub(crate) synced: bool,
36}
37
38impl Drop for LocalSlot {
39    fn drop(&mut self) {
40        // Slot locals hold script values directly (e.g. a `var` bound to a
41        // deeply nested list). When a frame is torn down, the default
42        // recursive drop of such a value would overflow the native stack and
43        // abort the process. For the overwhelmingly common scalar slot this is
44        // a single `matches!` check and then the normal trivial drop; only a
45        // nested container is moved out and torn down iteratively, so hot
46        // frame teardown is unaffected.
47        if crate::value::recursion::is_recursive_container(&self.value) {
48            crate::value::recursion::dismantle(std::mem::replace(&mut self.value, VmValue::Nil));
49        }
50    }
51}
52
53#[derive(Clone)]
54pub(crate) struct InterruptHandler {
55    pub(crate) handle: i64,
56    pub(crate) signals: Vec<String>,
57    pub(crate) once: bool,
58    pub(crate) graceful_timeout_ms: Option<u64>,
59    pub(crate) handler: VmValue,
60}
61
62/// Call frame for function execution.
63pub(crate) struct CallFrame {
64    pub(crate) chunk: ChunkRef,
65    pub(crate) ip: usize,
66    pub(crate) stack_base: usize,
67    pub(crate) saved_env: VmEnv,
68    /// Env snapshot captured at call-time, *after* argument binding. Used
69    /// by the debugger's `restartFrame` to rewind this frame to its
70    /// entry state (re-binding args from the original values) without
71    /// re-entering the call site. Cheap to clone because `VmEnv` is
72    /// already cloned into `saved_env` on every call. `None` for
73    /// scratch frames (evaluate, import init) where restart isn't
74    /// meaningful.
75    pub(crate) initial_env: Option<VmEnv>,
76    pub(crate) initial_local_slots: Option<Vec<LocalSlot>>,
77    /// Iterator stack depth to restore when this frame unwinds.
78    pub(crate) saved_iterator_depth: usize,
79    /// Function name for stack traces (empty for top-level pipeline).
80    pub(crate) fn_name: String,
81    /// Number of arguments actually passed by the caller (for default arg support).
82    pub(crate) argc: usize,
83    /// Saved VM_SOURCE_DIR to restore when this frame is popped.
84    /// Set when entering a closure that originated from an imported module.
85    pub(crate) saved_source_dir: Option<std::path::PathBuf>,
86    /// Module-local named functions available to symbolic calls within this frame.
87    pub(crate) module_functions: Option<ModuleFunctionRegistry>,
88    /// Shared module-level env for top-level `var` / `let` bindings of
89    /// this frame's originating module. Looked up after `self.env` and
90    /// before `self.globals` by `GetVar` / `SetVar`, giving each module
91    /// its own live static state that persists across calls. See the
92    /// `module_state` field on `VmClosure` for the full rationale.
93    pub(crate) module_state: Option<crate::value::ModuleState>,
94    /// Slot-indexed locals for compiler-resolved names in this frame.
95    pub(crate) local_slots: Vec<LocalSlot>,
96    /// Env scope index that corresponds to compiler local scope depth 0.
97    pub(crate) local_scope_base: usize,
98    /// Current compiler local scope depth, updated by PushScope/PopScope.
99    pub(crate) local_scope_depth: usize,
100}
101
102/// Exception handler for try/catch.
103pub(crate) struct ExceptionHandler {
104    pub(crate) catch_ip: usize,
105    pub(crate) stack_depth: usize,
106    pub(crate) frame_depth: usize,
107    pub(crate) env_scope_depth: usize,
108    /// When present, this catch only handles errors whose enum_name matches.
109    pub(crate) error_type: Option<Arc<str>>,
110}
111
112/// A structured-concurrency nursery (`scope { }`). Tasks spawned while this
113/// scope is innermost record their id here; `TaskScopeExit` joins them.
114pub(crate) struct TaskScope {
115    /// Ids of tasks spawned in this scope that have not been explicitly
116    /// `await`ed away. Joined (normal exit) or cancelled (unwind) on close.
117    pub(crate) task_ids: Vec<String>,
118    /// Frame depth at which the scope was opened, for unwind pruning.
119    pub(crate) frame_depth: usize,
120    /// Env scope depth at open, for unwind pruning.
121    pub(crate) env_scope_depth: usize,
122}
123
124/// Iterator state for for-in loops.
125pub(crate) enum IterState {
126    Vec {
127        items: Arc<Vec<VmValue>>,
128        idx: usize,
129    },
130    Dict {
131        entries: Arc<crate::value::DictMap>,
132        keys: Vec<String>,
133        idx: usize,
134    },
135    Channel {
136        receiver: std::sync::Arc<tokio::sync::Mutex<tokio::sync::mpsc::Receiver<VmValue>>>,
137        close: std::sync::Arc<crate::value::VmChannelCloseState>,
138    },
139    Generator {
140        gen: Arc<crate::value::VmGenerator>,
141    },
142    Stream {
143        stream: Arc<crate::value::VmStream>,
144    },
145    /// Step through a lazy range without materializing a Vec.
146    /// Inclusive ranges keep `end` as an actual value so `i64::MAX to i64::MAX`
147    /// still yields one item instead of overflowing a one-past-end sentinel.
148    Range {
149        next: i64,
150        end: i64,
151        inclusive: bool,
152        done: bool,
153    },
154    VmIter {
155        handle: crate::vm::iter::VmIterHandle,
156    },
157}
158
159#[derive(Clone)]
160pub(crate) enum VmBuiltinDispatch {
161    Sync(VmBuiltinFn),
162    Async(VmAsyncBuiltinFn),
163}
164
165#[derive(Clone)]
166pub(crate) struct VmBuiltinEntry {
167    pub(crate) name: Arc<str>,
168    pub(crate) dispatch: VmBuiltinDispatch,
169}
170
171/// The Harn bytecode virtual machine.
172pub struct Vm {
173    pub(crate) stack: Vec<VmValue>,
174    pub(crate) env: VmEnv,
175    pub(crate) output: String,
176    pub(crate) builtins: Arc<BTreeMap<String, VmBuiltinFn>>,
177    pub(crate) async_builtins: Arc<BTreeMap<String, VmAsyncBuiltinFn>>,
178    pub(crate) builtin_metadata: Arc<BTreeMap<String, VmBuiltinMetadata>>,
179    /// Numeric side index for builtins. Name-keyed maps remain authoritative;
180    /// this index is the hot path for direct builtin bytecode and callback refs.
181    pub(crate) builtins_by_id: Arc<HashMap<BuiltinId, VmBuiltinEntry>>,
182    /// IDs with detected name collisions. Collided names safely fall back to
183    /// the authoritative name-keyed lookup path.
184    pub(crate) builtin_id_collisions: Arc<HashSet<BuiltinId>>,
185    /// Iterator state for for-in loops.
186    pub(crate) iterators: Vec<IterState>,
187    /// Call frame stack.
188    pub(crate) frames: Vec<CallFrame>,
189    /// Exception handler stack.
190    pub(crate) exception_handlers: Vec<ExceptionHandler>,
191    /// Spawned async task handles.
192    pub(crate) spawned_tasks: BTreeMap<String, VmTaskHandle>,
193    /// Shared process-local synchronization primitives inherited by child VMs.
194    pub(crate) sync_runtime: Arc<crate::synchronization::VmSyncRuntime>,
195    /// Shared process-local cells, maps, and mailboxes inherited by child VMs.
196    pub(crate) shared_state_runtime: Arc<crate::shared_state::VmSharedStateRuntime>,
197    /// Per-isolate inline cache entries keyed by compiled chunk identity.
198    pub(crate) inline_caches: HashMap<u64, Vec<crate::chunk::InlineCacheEntry>>,
199    /// VM-scoped pool registry inherited by child VMs and scoped into Tokio tasks.
200    pub(crate) pool_registry: Arc<crate::stdlib::pool::PoolRegistry>,
201    /// Shared task/channel wait graph for this VM execution tree.
202    pub(crate) wait_for_graph: Arc<crate::wait_for_graph::VmWaitForGraph>,
203    /// Permits acquired by lexical synchronization blocks in this VM.
204    pub(crate) held_sync_guards: Vec<crate::synchronization::VmSyncHeldGuard>,
205    /// Locks held by an ancestor VM that is *suspended on this VM's execution*:
206    /// an inline async-builtin child runs while its parent is parked
207    /// mid-instruction still holding these permits. Re-acquiring more permits
208    /// than the primitive can grant is a provably-unresolvable self-deadlock, so
209    /// HARN-ORC-011 fires across the child boundary. Empty for new concurrent
210    /// tasks (`spawn`/`parallel`/triggers), where the parent keeps running and
211    /// blocking can be legitimately resolvable.
212    pub(crate) inherited_held_keys: Arc<Vec<crate::synchronization::VmSyncHeldKey>>,
213    /// Structured-concurrency nursery stack. Each `scope { }` block pushes a
214    /// `TaskScope`; tasks spawned while it is innermost register their id here.
215    /// On normal exit (`TaskScopeExit`) the scope's tasks are joined and the
216    /// first error propagates; on unwind they are cancelled. Modeled on
217    /// `held_sync_guards` (push on enter, prune/cancel on frame/handler exit).
218    pub(crate) task_scopes: Vec<TaskScope>,
219    /// Counter for generating unique task IDs.
220    pub(crate) task_counter: u64,
221    /// Counter for logical runtime-context task groups.
222    pub(crate) runtime_context_counter: u64,
223    /// Logical runtime task context visible through `runtime_context()`.
224    pub(crate) runtime_context: crate::runtime_context::RuntimeContext,
225    /// Active deadline stack: (deadline_instant, frame_depth).
226    pub(crate) deadlines: Vec<(Instant, usize)>,
227    /// Breakpoints, keyed by source-file path so a breakpoint at line N
228    /// in `auto.harn` doesn't also fire when execution hits line N in an
229    /// imported lib. The empty-string key is a wildcard used by callers
230    /// that don't track source paths (legacy `set_breakpoints` API).
231    pub(crate) breakpoints: BTreeMap<String, std::collections::BTreeSet<usize>>,
232    /// Function-name breakpoints. Any closure call whose
233    /// `CompiledFunction.name` matches an entry here raises a stop on
234    /// entry, regardless of the call site's file or line. Lets the IDE
235    /// break on `llm_call` / `host_run_pipeline` / any user pipeline
236    /// function without pinning down a source location first.
237    pub(crate) function_breakpoints: std::collections::BTreeSet<String>,
238    /// Latched on `push_closure_frame` when the callee's name matches
239    /// `function_breakpoints`; consumed by the next step so the stop is
240    /// reported with reason="function breakpoint" and the breakpoint
241    /// name available for the DAP `stopped` event.
242    pub(crate) pending_function_bp: Option<String>,
243    /// Whether the VM is in step mode.
244    pub(crate) step_mode: bool,
245    /// The frame depth at which stepping started (for step-over).
246    pub(crate) step_frame_depth: usize,
247    /// Whether the VM is currently stopped at a debug point.
248    pub(crate) stopped: bool,
249    /// Last source line executed (to detect line changes).
250    pub(crate) last_line: usize,
251    /// Source directory for resolving imports.
252    pub(crate) source_dir: Option<std::path::PathBuf>,
253    /// Modules currently being imported (cycle prevention).
254    pub(crate) imported_paths: Vec<std::path::PathBuf>,
255    /// Loaded module cache keyed by canonical or synthetic module path.
256    pub(crate) module_cache: Arc<BTreeMap<std::path::PathBuf, LoadedModule>>,
257    /// Source text keyed by canonical or synthetic module path for debugger retrieval.
258    pub(crate) source_cache: Arc<BTreeMap<std::path::PathBuf, String>>,
259    /// Source file path for error reporting.
260    pub(crate) source_file: Option<String>,
261    /// Source text for error reporting.
262    pub(crate) source_text: Option<String>,
263    /// Optional bridge for delegating unknown builtins in bridge mode.
264    pub(crate) bridge: Option<Arc<crate::bridge::HostBridge>>,
265    /// Builtins denied by sandbox mode (`--deny` / `--allow` flags).
266    pub(crate) denied_builtins: Arc<HashSet<String>>,
267    /// Cancellation token for cooperative graceful shutdown (set by parent).
268    pub(crate) cancel_token: Option<std::sync::Arc<std::sync::atomic::AtomicBool>>,
269    pub(crate) interrupt_signal_token: Option<std::sync::Arc<std::sync::Mutex<Option<String>>>>,
270    /// Remaining instruction-boundary checks before a requested host
271    /// cancellation is forcefully raised. This gives `is_cancelled()` loops a
272    /// deterministic chance to return cleanly without letting non-cooperative
273    /// CPU-bound code run forever.
274    pub(crate) cancel_grace_instructions_remaining: Option<usize>,
275    /// User-visible interrupt handlers registered through `std/signal`.
276    pub(crate) interrupt_handlers: Vec<InterruptHandler>,
277    pub(crate) next_interrupt_handle: i64,
278    pub(crate) pending_interrupt_signal: Option<String>,
279    pub(crate) interrupted: bool,
280    pub(crate) dispatching_interrupt: bool,
281    pub(crate) interrupt_handler_deadline: Option<Instant>,
282    /// Captured stack trace from the most recent error (fn_name, line, col).
283    pub(crate) error_stack_trace: Vec<(String, usize, usize, Option<String>)>,
284    /// Yield channel sender for generator execution. When set, `Op::Yield`
285    /// sends values through this channel instead of being a no-op.
286    pub(crate) yield_sender: Option<tokio::sync::mpsc::Sender<Result<VmValue, VmError>>>,
287    /// Project root directory (detected via harn.toml).
288    /// Used as base directory for metadata, store, and checkpoint operations.
289    pub(crate) project_root: Option<std::path::PathBuf>,
290    /// Global constants (e.g. `pi`, `e`). Checked as a fallback in `GetVar`
291    /// after the environment, so user-defined variables can shadow them.
292    pub(crate) globals: Arc<crate::value::DictMap>,
293    /// Optional debugger hook invoked when execution advances to a new source line.
294    pub(crate) debug_hook: Option<parking_lot::Mutex<Box<DebugHook>>>,
295    /// Effective runtime ceilings for this VM execution.
296    pub(crate) runtime_limits: RuntimeLimits,
297}
298
299/// Reusable VM baseline for hosts that need many clean executions with the
300/// same stable builtin/source setup.
301///
302/// The baseline intentionally does not snapshot execution state. Each
303/// instantiation gets fresh stacks, frames, tasks, cancellation fields, sync
304/// primitives, shared cells/maps/mailboxes, and debug state. Builtin tables are
305/// shared through `Arc` until a per-execution rebind needs copy-on-write.
306#[derive(Clone)]
307pub struct VmBaseline {
308    builtins: Arc<BTreeMap<String, VmBuiltinFn>>,
309    async_builtins: Arc<BTreeMap<String, VmAsyncBuiltinFn>>,
310    builtin_metadata: Arc<BTreeMap<String, VmBuiltinMetadata>>,
311    builtins_by_id: Arc<HashMap<BuiltinId, VmBuiltinEntry>>,
312    builtin_id_collisions: Arc<HashSet<BuiltinId>>,
313    source_dir: Option<std::path::PathBuf>,
314    source_file: Option<String>,
315    source_text: Option<String>,
316    project_root: Option<std::path::PathBuf>,
317    globals: Arc<crate::value::DictMap>,
318    denied_builtins: Arc<HashSet<String>>,
319    runtime_limits: RuntimeLimits,
320}
321
322impl VmBaseline {
323    pub fn from_vm(vm: &Vm) -> Self {
324        Self {
325            builtins: Arc::clone(&vm.builtins),
326            async_builtins: Arc::clone(&vm.async_builtins),
327            builtin_metadata: Arc::clone(&vm.builtin_metadata),
328            builtins_by_id: Arc::clone(&vm.builtins_by_id),
329            builtin_id_collisions: Arc::clone(&vm.builtin_id_collisions),
330            source_dir: vm.source_dir.clone(),
331            source_file: vm.source_file.clone(),
332            source_text: vm.source_text.clone(),
333            project_root: vm.project_root.clone(),
334            globals: Arc::clone(&vm.globals),
335            denied_builtins: Arc::clone(&vm.denied_builtins),
336            runtime_limits: vm.runtime_limits,
337        }
338    }
339
340    pub fn instantiate(&self) -> Vm {
341        let mut source_cache = BTreeMap::new();
342        if let (Some(file), Some(text)) = (&self.source_file, &self.source_text) {
343            source_cache.insert(std::path::PathBuf::from(file), text.clone());
344        }
345        if let Some(dir) = &self.source_dir {
346            crate::stdlib::set_thread_source_dir(dir);
347        }
348
349        let mut vm = Vm {
350            stack: Vec::with_capacity(256),
351            env: VmEnv::new(),
352            output: String::new(),
353            builtins: Arc::clone(&self.builtins),
354            async_builtins: Arc::clone(&self.async_builtins),
355            builtin_metadata: Arc::clone(&self.builtin_metadata),
356            builtins_by_id: Arc::clone(&self.builtins_by_id),
357            builtin_id_collisions: Arc::clone(&self.builtin_id_collisions),
358            iterators: Vec::new(),
359            frames: Vec::new(),
360            exception_handlers: Vec::new(),
361            spawned_tasks: BTreeMap::new(),
362            sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
363            shared_state_runtime: Arc::new(crate::shared_state::VmSharedStateRuntime::new()),
364            inline_caches: HashMap::new(),
365            pool_registry: crate::stdlib::pool::new_pool_registry(),
366            wait_for_graph: Arc::new(crate::wait_for_graph::VmWaitForGraph::new()),
367            held_sync_guards: Vec::new(),
368            inherited_held_keys: Arc::new(Vec::new()),
369            task_scopes: Vec::new(),
370            task_counter: 0,
371            runtime_context_counter: 0,
372            runtime_context: crate::runtime_context::RuntimeContext::root(),
373            deadlines: Vec::new(),
374            breakpoints: BTreeMap::new(),
375            function_breakpoints: std::collections::BTreeSet::new(),
376            pending_function_bp: None,
377            step_mode: false,
378            step_frame_depth: 0,
379            stopped: false,
380            last_line: 0,
381            source_dir: self.source_dir.clone(),
382            imported_paths: Vec::new(),
383            module_cache: Arc::new(BTreeMap::new()),
384            source_cache: Arc::new(source_cache),
385            source_file: self.source_file.clone(),
386            source_text: self.source_text.clone(),
387            bridge: None,
388            denied_builtins: Arc::clone(&self.denied_builtins),
389            cancel_token: None,
390            interrupt_signal_token: None,
391            cancel_grace_instructions_remaining: None,
392            interrupt_handlers: Vec::new(),
393            next_interrupt_handle: 1,
394            pending_interrupt_signal: None,
395            interrupted: false,
396            dispatching_interrupt: false,
397            interrupt_handler_deadline: None,
398            error_stack_trace: Vec::new(),
399            yield_sender: None,
400            project_root: self.project_root.clone(),
401            globals: Arc::clone(&self.globals),
402            debug_hook: None,
403            runtime_limits: self.runtime_limits,
404        };
405
406        crate::stdlib::rebind_execution_state_builtins(&mut vm);
407        vm
408    }
409}
410
411impl Vm {
412    pub(crate) fn fresh_local_slots(chunk: &Chunk) -> Vec<LocalSlot> {
413        chunk
414            .local_slots
415            .iter()
416            .map(|_| LocalSlot {
417                value: VmValue::Nil,
418                initialized: false,
419                synced: false,
420            })
421            .collect()
422    }
423
424    pub(crate) fn bind_param_slots(
425        slots: &mut [LocalSlot],
426        func: &crate::chunk::CompiledFunction,
427        args: &[VmValue],
428        synced: bool,
429    ) {
430        Self::bind_param_slots_args(slots, func, &super::CallArgs::Slice(args), synced);
431    }
432
433    pub(crate) fn bind_param_slots_args(
434        slots: &mut [LocalSlot],
435        func: &crate::chunk::CompiledFunction,
436        args: &super::CallArgs<'_>,
437        synced: bool,
438    ) {
439        let param_count = func.params.len();
440        for (i, _param) in func.params.iter().enumerate() {
441            if i >= slots.len() {
442                break;
443            }
444            if func.has_rest_param && i == param_count - 1 {
445                let rest_args = args.to_vec_from(i);
446                slots[i].value = VmValue::List(std::sync::Arc::new(rest_args));
447                slots[i].initialized = true;
448                slots[i].synced = synced;
449            } else if let Some(arg) = args.get(i) {
450                slots[i].value = arg.clone();
451                slots[i].initialized = true;
452                slots[i].synced = synced;
453            }
454        }
455    }
456
457    pub(crate) fn visible_variables(&self) -> crate::value::DictMap {
458        let mut vars = self.env.all_variables();
459        let Some(frame) = self.frames.last() else {
460            return vars;
461        };
462        for (slot, info) in frame.local_slots.iter().zip(frame.chunk.local_slots.iter()) {
463            if slot.initialized && info.scope_depth <= frame.local_scope_depth {
464                vars.insert(info.name.clone(), slot.value.clone());
465            }
466        }
467        vars
468    }
469
470    pub(crate) fn sync_current_frame_locals_to_env(&mut self) {
471        let frames = &mut self.frames;
472        let env = &mut self.env;
473        let Some(frame) = frames.last_mut() else {
474            return;
475        };
476        let local_scope_base = frame.local_scope_base;
477        let local_scope_depth = frame.local_scope_depth;
478        for (slot, info) in frame
479            .local_slots
480            .iter_mut()
481            .zip(frame.chunk.local_slots.iter())
482        {
483            if slot.initialized && !slot.synced && info.scope_depth <= local_scope_depth {
484                slot.synced = true;
485                let scope_idx = local_scope_base + info.scope_depth;
486                while env.scopes.len() <= scope_idx {
487                    env.push_scope();
488                }
489                Arc::make_mut(&mut env.scopes[scope_idx].vars)
490                    .insert(info.name.clone(), (slot.value.clone(), info.mutable));
491            }
492        }
493    }
494
495    pub(crate) fn closure_call_env_for_current_frame(
496        &self,
497        closure: &crate::value::VmClosure,
498    ) -> VmEnv {
499        if closure.module_state().is_some() {
500            return closure.env.clone();
501        }
502        let call_env = Self::closure_call_env(&self.env, closure);
503        // Same compile-time short-circuit as the env walk in
504        // `closure_call_env`: when the callee body never resolves an
505        // outer name through the env, injecting closure-typed *slot*
506        // locals from the caller's frame is wasted work too.
507        if !closure.func.chunk.references_outer_names {
508            return call_env;
509        }
510        let mut call_env = call_env;
511        let Some(frame) = self.frames.last() else {
512            return call_env;
513        };
514        for (slot, info) in frame
515            .local_slots
516            .iter()
517            .zip(frame.chunk.local_slots.iter())
518            .filter(|(slot, info)| slot.initialized && info.scope_depth <= frame.local_scope_depth)
519        {
520            if matches!(slot.value, VmValue::Closure(_)) && !call_env.contains(&info.name) {
521                let _ = call_env.define(&info.name, slot.value.clone(), info.mutable);
522            }
523        }
524        call_env
525    }
526
527    pub(crate) fn active_local_slot_value(&self, name: &str) -> Option<VmValue> {
528        let frame = self.frames.last()?;
529        let idx = self.active_local_slot_index(name)?;
530        frame.local_slots.get(idx).map(|slot| slot.value.clone())
531    }
532
533    /// Returns the slot index of an initialized active local with the given
534    /// name, walking from innermost to outermost scope. Used by legacy by-name
535    /// hot paths that still want to mutate the slot value in place without
536    /// paying a defensive `VmValue::clone` first.
537    pub(crate) fn active_local_slot_index(&self, name: &str) -> Option<usize> {
538        let frame = self.frames.last()?;
539        for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
540            if info.name == name && info.scope_depth <= frame.local_scope_depth {
541                if let Some(slot) = frame.local_slots.get(idx) {
542                    if slot.initialized {
543                        return Some(idx);
544                    }
545                }
546            }
547        }
548        None
549    }
550
551    pub(crate) fn assign_active_local_slot(
552        &mut self,
553        name: &str,
554        value: VmValue,
555        debug: bool,
556    ) -> Result<bool, VmError> {
557        let Some(frame) = self.frames.last_mut() else {
558            return Ok(false);
559        };
560        for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
561            if info.name == name && info.scope_depth <= frame.local_scope_depth {
562                if !debug && !info.mutable {
563                    return Err(VmError::ImmutableAssignment(name.to_string()));
564                }
565                if let Some(slot) = frame.local_slots.get_mut(idx) {
566                    crate::value::recursion::dismantle(std::mem::replace(&mut slot.value, value));
567                    slot.initialized = true;
568                    slot.synced = false;
569                    return Ok(true);
570                }
571            }
572        }
573        Ok(false)
574    }
575
576    pub fn new() -> Self {
577        Self {
578            stack: Vec::with_capacity(256),
579            env: VmEnv::new(),
580            output: String::new(),
581            builtins: Arc::new(BTreeMap::new()),
582            async_builtins: Arc::new(BTreeMap::new()),
583            builtin_metadata: Arc::new(BTreeMap::new()),
584            builtins_by_id: Arc::new(HashMap::new()),
585            builtin_id_collisions: Arc::new(HashSet::new()),
586            iterators: Vec::new(),
587            frames: Vec::new(),
588            exception_handlers: Vec::new(),
589            spawned_tasks: BTreeMap::new(),
590            sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
591            shared_state_runtime: Arc::new(crate::shared_state::VmSharedStateRuntime::new()),
592            inline_caches: HashMap::new(),
593            pool_registry: crate::stdlib::pool::new_pool_registry(),
594            wait_for_graph: Arc::new(crate::wait_for_graph::VmWaitForGraph::new()),
595            held_sync_guards: Vec::new(),
596            inherited_held_keys: Arc::new(Vec::new()),
597            task_scopes: Vec::new(),
598            task_counter: 0,
599            runtime_context_counter: 0,
600            runtime_context: crate::runtime_context::RuntimeContext::root(),
601            deadlines: Vec::new(),
602            breakpoints: BTreeMap::new(),
603            function_breakpoints: std::collections::BTreeSet::new(),
604            pending_function_bp: None,
605            step_mode: false,
606            step_frame_depth: 0,
607            stopped: false,
608            last_line: 0,
609            source_dir: None,
610            imported_paths: Vec::new(),
611            module_cache: Arc::new(BTreeMap::new()),
612            source_cache: Arc::new(BTreeMap::new()),
613            source_file: None,
614            source_text: None,
615            bridge: None,
616            denied_builtins: Arc::new(HashSet::new()),
617            cancel_token: None,
618            interrupt_signal_token: None,
619            cancel_grace_instructions_remaining: None,
620            interrupt_handlers: Vec::new(),
621            next_interrupt_handle: 1,
622            pending_interrupt_signal: None,
623            interrupted: false,
624            dispatching_interrupt: false,
625            interrupt_handler_deadline: None,
626            error_stack_trace: Vec::new(),
627            yield_sender: None,
628            project_root: None,
629            globals: Arc::new(crate::value::DictMap::new()),
630            debug_hook: None,
631            runtime_limits: RuntimeLimits::default(),
632        }
633    }
634
635    pub fn baseline(&self) -> VmBaseline {
636        VmBaseline::from_vm(self)
637    }
638
639    /// Return the effective runtime limit profile for this VM.
640    pub fn runtime_limits(&self) -> RuntimeLimits {
641        self.runtime_limits
642    }
643
644    /// Return a host/debug report describing the VM's effective runtime limits.
645    pub fn runtime_limit_report(&self) -> crate::RuntimeLimitsReport {
646        self.runtime_limits.report()
647    }
648
649    /// Returns true if any debugging affordance is active — DAP hook,
650    /// line breakpoints, or function breakpoints. Call-site code uses
651    /// this to decide whether to capture per-frame restart snapshots
652    /// (`initial_env`, `initial_local_slots`); without a debugger those
653    /// snapshots are dead weight, so skipping them removes two
654    /// allocations from every function call hot path.
655    ///
656    /// All three signals are stable across a function call's lifetime
657    /// (they're set before pipeline execution starts), so the gate is
658    /// consistent between frame creation and any later `restart_frame`
659    /// invocation. The three `is_empty` checks compile to a handful of
660    /// branch-predicted memory probes — cheaper than a single
661    /// `BTreeMap` clone, which is what we're avoiding.
662    #[inline]
663    pub(crate) fn debugger_attached(&self) -> bool {
664        self.debug_hook.is_some()
665            || !self.breakpoints.is_empty()
666            || !self.function_breakpoints.is_empty()
667    }
668
669    /// Set the bridge for delegating unknown builtins in bridge mode.
670    pub fn set_bridge(&mut self, bridge: Arc<crate::bridge::HostBridge>) {
671        self.bridge = Some(bridge);
672    }
673
674    /// Set builtins that are denied in sandbox mode.
675    /// When called, the given builtin names will produce a permission error.
676    pub fn set_denied_builtins(&mut self, denied: HashSet<String>) {
677        self.denied_builtins = Arc::new(denied);
678    }
679
680    /// Set source info for error reporting (file path and source text).
681    pub fn set_source_info(&mut self, file: &str, text: &str) {
682        self.source_file = Some(file.to_string());
683        self.source_text = Some(text.to_string());
684        Arc::make_mut(&mut self.source_cache)
685            .insert(std::path::PathBuf::from(file), text.to_string());
686    }
687
688    /// Initialize execution (push the initial frame).
689    pub fn start(&mut self, chunk: &Chunk) {
690        // The top-level pipeline frame captures env at start so
691        // restartFrame on the outermost frame rewinds to the
692        // pre-pipeline state — basically "restart session" in
693        // debugger terms. Skipped when no debugger is attached:
694        // the snapshot is dead weight in that case and dominates
695        // call-overhead bench numbers (~5-10%).
696        let debugger = self.debugger_attached();
697        let initial_env = if debugger {
698            Some(self.env.clone())
699        } else {
700            None
701        };
702        let initial_local_slots = if debugger {
703            Some(Self::fresh_local_slots(chunk))
704        } else {
705            None
706        };
707        self.frames.push(CallFrame {
708            chunk: Arc::new(chunk.clone()),
709            ip: 0,
710            stack_base: self.stack.len(),
711            saved_env: self.env.clone(),
712            initial_env,
713            initial_local_slots,
714            saved_iterator_depth: self.iterators.len(),
715            fn_name: String::new(),
716            argc: 0,
717            saved_source_dir: None,
718            module_functions: None,
719            module_state: None,
720            local_slots: Self::fresh_local_slots(chunk),
721            local_scope_base: self.env.scope_depth().saturating_sub(1),
722            local_scope_depth: 0,
723        });
724    }
725
726    /// Create a child VM that shares builtins and env but has fresh execution state.
727    /// Used for parallel/spawn to fork the VM for concurrent tasks.
728    pub(crate) fn child_vm(&self) -> Vm {
729        Vm {
730            stack: Vec::with_capacity(64),
731            env: self.env.clone(),
732            output: String::new(),
733            builtins: Arc::clone(&self.builtins),
734            async_builtins: Arc::clone(&self.async_builtins),
735            builtin_metadata: Arc::clone(&self.builtin_metadata),
736            builtins_by_id: Arc::clone(&self.builtins_by_id),
737            builtin_id_collisions: Arc::clone(&self.builtin_id_collisions),
738            iterators: Vec::new(),
739            frames: Vec::new(),
740            exception_handlers: Vec::new(),
741            spawned_tasks: BTreeMap::new(),
742            sync_runtime: self.sync_runtime.clone(),
743            shared_state_runtime: self.shared_state_runtime.clone(),
744            inline_caches: HashMap::new(),
745            pool_registry: self.pool_registry.clone(),
746            wait_for_graph: self.wait_for_graph.clone(),
747            held_sync_guards: Vec::new(),
748            inherited_held_keys: Arc::new(Vec::new()),
749            task_scopes: Vec::new(),
750            task_counter: 0,
751            runtime_context_counter: self.runtime_context_counter,
752            runtime_context: self.runtime_context.clone(),
753            deadlines: self.deadlines.clone(),
754            breakpoints: BTreeMap::new(),
755            function_breakpoints: std::collections::BTreeSet::new(),
756            pending_function_bp: None,
757            step_mode: false,
758            step_frame_depth: 0,
759            stopped: false,
760            last_line: 0,
761            source_dir: self.source_dir.clone(),
762            imported_paths: Vec::new(),
763            module_cache: Arc::clone(&self.module_cache),
764            source_cache: Arc::clone(&self.source_cache),
765            source_file: self.source_file.clone(),
766            source_text: self.source_text.clone(),
767            bridge: self.bridge.clone(),
768            denied_builtins: Arc::clone(&self.denied_builtins),
769            cancel_token: self.cancel_token.clone(),
770            interrupt_signal_token: self.interrupt_signal_token.clone(),
771            cancel_grace_instructions_remaining: None,
772            interrupt_handlers: Vec::new(),
773            next_interrupt_handle: 1,
774            pending_interrupt_signal: None,
775            interrupted: self.interrupted,
776            dispatching_interrupt: false,
777            interrupt_handler_deadline: None,
778            error_stack_trace: Vec::new(),
779            yield_sender: None,
780            project_root: self.project_root.clone(),
781            globals: Arc::clone(&self.globals),
782            debug_hook: None,
783            runtime_limits: self.runtime_limits,
784        }
785    }
786
787    /// Create a child VM for external adapters that need to invoke Harn
788    /// closures while sharing the parent's builtins, globals, and module state.
789    pub(crate) fn child_vm_for_host(&self) -> Vm {
790        self.child_vm()
791    }
792
793    /// Request cancellation for every outstanding child task owned by this VM
794    /// and then abort the join handles. This prevents un-awaited spawned tasks
795    /// from outliving their parent execution scope.
796    pub(crate) fn cancel_spawned_tasks(&mut self) {
797        for (_, task) in std::mem::take(&mut self.spawned_tasks) {
798            task.cancel_token
799                .store(true, std::sync::atomic::Ordering::SeqCst);
800            task.handle.abort();
801        }
802    }
803
804    /// Set the source directory for import resolution and introspection.
805    /// Also auto-detects the project root if not already set.
806    pub fn set_source_dir(&mut self, dir: &std::path::Path) {
807        let dir = crate::stdlib::process::normalize_context_path(dir);
808        self.source_dir = Some(dir.clone());
809        crate::stdlib::set_thread_source_dir(&dir);
810        // Auto-detect project root if not explicitly set.
811        if self.project_root.is_none() {
812            self.project_root = crate::stdlib::process::find_project_root(&dir);
813        }
814    }
815
816    /// Explicitly set the project root directory.
817    /// Used by ACP/CLI to override auto-detection.
818    pub fn set_project_root(&mut self, root: &std::path::Path) {
819        self.project_root = Some(root.to_path_buf());
820    }
821
822    /// Get the project root directory, falling back to source_dir.
823    pub fn project_root(&self) -> Option<&std::path::Path> {
824        self.project_root.as_deref().or(self.source_dir.as_deref())
825    }
826
827    /// Return all registered builtin names (sync + async).
828    pub fn builtin_names(&self) -> Vec<String> {
829        let mut names: Vec<String> = self.builtins.keys().cloned().collect();
830        names.extend(self.async_builtins.keys().cloned());
831        names
832    }
833
834    /// Return discoverable metadata for registered builtins.
835    pub fn builtin_metadata(&self) -> Vec<VmBuiltinMetadata> {
836        self.builtin_metadata.values().cloned().collect()
837    }
838
839    /// Return discoverable metadata for a registered builtin name.
840    pub fn builtin_metadata_for(&self, name: &str) -> Option<&VmBuiltinMetadata> {
841        self.builtin_metadata.get(name)
842    }
843
844    /// Set a global constant (e.g. `pi`, `e`).
845    /// Stored separately from the environment so user-defined variables can shadow them.
846    pub fn set_global(&mut self, name: &str, value: VmValue) {
847        Arc::make_mut(&mut self.globals).insert(name.to_string(), value);
848    }
849
850    /// Read a previously-installed global (the value `set_global` /
851    /// `set_harness` recorded). Returns `None` for unknown names.
852    /// Hosts use this to look up runtime-installed capability handles
853    /// (e.g. the `harness` slot) without having to track them
854    /// separately.
855    pub fn global(&self, name: &str) -> Option<&VmValue> {
856        self.globals.get(name)
857    }
858
859    /// Install the script's `Harness` capability handle as the `harness`
860    /// global so the auto-call emitted by `Compiler::compile()` (for
861    /// `fn main(harness: Harness)` entrypoints) can read it. Hosts that
862    /// drive the VM directly (CLI, MCP server, composition runtime) call
863    /// this once before `execute()`.
864    pub fn set_harness(&mut self, harness: crate::harness::Harness) {
865        self.set_global("harness", harness.into_vm_value());
866    }
867
868    /// Get the captured output.
869    pub fn output(&self) -> &str {
870        &self.output
871    }
872
873    /// Drain and return the captured output, leaving the buffer empty.
874    /// Used by the async-builtin dispatch path to forward closure output
875    /// from a child VM back to its parent.
876    pub fn take_output(&mut self) -> String {
877        std::mem::take(&mut self.output)
878    }
879
880    /// Append text to this VM's captured output. Used to forward output
881    /// from child VMs (e.g. closures invoked via `call_closure_pub`)
882    /// back into the parent stream.
883    pub fn append_output(&mut self, text: &str) {
884        self.output.push_str(text);
885    }
886
887    pub(crate) fn pop(&mut self) -> Result<VmValue, VmError> {
888        self.stack.pop().ok_or(VmError::StackUnderflow)
889    }
890
891    pub(crate) fn peek(&self) -> Result<&VmValue, VmError> {
892        self.stack.last().ok_or(VmError::StackUnderflow)
893    }
894
895    pub(crate) fn const_str(c: &Constant) -> Result<&str, VmError> {
896        match c {
897            Constant::String(s) => Ok(s.as_str()),
898            _ => Err(VmError::TypeError("expected string constant".into())),
899        }
900    }
901
902    pub(crate) fn release_sync_guards_for_current_scope(&mut self) {
903        let depth = self.env.scope_depth();
904        self.held_sync_guards
905            .retain(|guard| guard.env_scope_depth < depth);
906        // A `scope { }` torn down without a normal `TaskScopeExit` (break /
907        // continue out of it) leaves a dangling nursery — cancel its tasks.
908        self.cancel_task_scopes_where(|s| s.env_scope_depth >= depth);
909    }
910
911    pub(crate) fn release_sync_guards_after_unwind(
912        &mut self,
913        frame_depth: usize,
914        env_scope_depth: usize,
915    ) {
916        self.held_sync_guards.retain(|guard| {
917            guard.frame_depth <= frame_depth && guard.env_scope_depth <= env_scope_depth
918        });
919        // Cancel nurseries opened above the catch handler (a `throw` unwound
920        // past their `TaskScopeExit`).
921        self.cancel_task_scopes_where(|s| {
922            !(s.frame_depth <= frame_depth && s.env_scope_depth <= env_scope_depth)
923        });
924    }
925
926    pub(crate) fn release_sync_guards_for_frame(&mut self, frame_depth: usize) {
927        self.held_sync_guards
928            .retain(|guard| guard.frame_depth != frame_depth);
929        // Cancel any nursery whose `scope {}` block belonged to the frame being
930        // torn down (e.g. a `return` jumped past its `TaskScopeExit`).
931        self.cancel_task_scopes_where(|s| s.frame_depth == frame_depth);
932    }
933
934    pub(crate) fn adopt_sync_permit_for_current_scope(
935        &mut self,
936        permit: crate::value::VmSyncPermitHandle,
937    ) {
938        if permit.is_released()
939            || self
940                .held_sync_guards
941                .iter()
942                .any(|guard| guard._permit.same_lease(&permit))
943        {
944            return;
945        }
946        self.held_sync_guards
947            .push(crate::synchronization::VmSyncHeldGuard {
948                _permit: permit,
949                frame_depth: self.frames.len(),
950                env_scope_depth: self.env.scope_depth(),
951            });
952    }
953
954    /// Deregister a task id from every open nursery (it was explicitly
955    /// `await`ed, so it must not be double-joined or cancelled at scope exit).
956    pub(crate) fn deregister_task_from_scopes(&mut self, id: &str) {
957        for scope in &mut self.task_scopes {
958            scope.task_ids.retain(|t| t != id);
959        }
960    }
961
962    /// Cancel and remove every task scope matching `doomed`, aborting its bound
963    /// tasks (used when a `scope {}` is torn down without a normal join).
964    fn cancel_task_scopes_where<F: Fn(&TaskScope) -> bool>(&mut self, doomed: F) {
965        let mut i = 0;
966        while i < self.task_scopes.len() {
967            if doomed(&self.task_scopes[i]) {
968                let scope = self.task_scopes.remove(i);
969                for id in &scope.task_ids {
970                    if let Some(task) = self.spawned_tasks.remove(id) {
971                        task.cancel_token
972                            .store(true, std::sync::atomic::Ordering::SeqCst);
973                        task.handle.abort();
974                    }
975                }
976            } else {
977                i += 1;
978            }
979        }
980    }
981
982    /// Total live permits this VM already holds for `kind:key`. The held-set is
983    /// tiny (bounded by lexical nesting and explicit sync acquisitions), so this
984    /// scan is cheap and only runs on the rare blocking-acquire path.
985    pub(crate) fn held_permits_for(&self, kind: &str, key: &str) -> u32 {
986        let own: u32 = self
987            .held_sync_guards
988            .iter()
989            .filter(|guard| {
990                !guard._permit.is_released()
991                    && guard._permit.kind() == kind
992                    && guard._permit.key() == key
993            })
994            .map(|guard| guard._permit.permits())
995            .sum();
996        let inherited: u32 = self
997            .inherited_held_keys
998            .iter()
999            .filter(|held| held.kind == kind && held.key == key)
1000            .map(|held| held.permits)
1001            .sum();
1002        own + inherited
1003    }
1004
1005    /// Every live sync permit held by this VM *and* its suspended ancestors: the
1006    /// transitive held-set seen by an inline child.
1007    pub(crate) fn combined_held_keys(&self) -> Vec<crate::synchronization::VmSyncHeldKey> {
1008        let mut keys: Vec<crate::synchronization::VmSyncHeldKey> = self
1009            .held_sync_guards
1010            .iter()
1011            .filter_map(|guard| crate::synchronization::VmSyncHeldKey::from_permit(&guard._permit))
1012            .collect();
1013        keys.extend(self.inherited_held_keys.iter().cloned());
1014        keys
1015    }
1016
1017    /// Clone a child VM for an **inline, same-task** execution (an async builtin
1018    /// awaited while this VM is parked, or a user closure that builtin runs and
1019    /// awaits). The child inherits this VM's transitive held-lock keys so a
1020    /// re-acquire of a parent-held lock is caught as a self-deadlock
1021    /// (HARN-ORC-011). Use plain `child_vm()` for new concurrent tasks.
1022    pub(crate) fn child_vm_inline(&self) -> Vm {
1023        let mut child = self.child_vm();
1024        child.inherited_held_keys = Arc::new(self.combined_held_keys());
1025        child
1026    }
1027}
1028
1029impl Drop for Vm {
1030    fn drop(&mut self) {
1031        self.cancel_spawned_tasks();
1032    }
1033}
1034
1035impl Default for Vm {
1036    fn default() -> Self {
1037        Self::new()
1038    }
1039}
1040
1041#[cfg(test)]
1042mod tests {
1043
1044    use super::*;
1045
1046    fn baseline_with_stdlib(source: &str) -> VmBaseline {
1047        let mut vm = Vm::new();
1048        crate::register_vm_stdlib(&mut vm);
1049        vm.set_source_info("baseline_test.harn", source);
1050        vm.set_global(
1051            "stable_global",
1052            VmValue::String(std::sync::Arc::from("baseline")),
1053        );
1054        vm.baseline()
1055    }
1056
1057    #[test]
1058    fn vm_baseline_instantiates_clean_mutable_execution_state() {
1059        let baseline = baseline_with_stdlib("pipeline main() { __io_println(stable_global) }");
1060
1061        let mut dirty = baseline.instantiate();
1062        dirty.stack.push(VmValue::Int(42));
1063        dirty.output.push_str("dirty");
1064        dirty.task_counter = 9;
1065        dirty.runtime_context_counter = 7;
1066        dirty
1067            .error_stack_trace
1068            .push(("main".to_string(), 1, 1, None));
1069
1070        let clean = baseline.instantiate();
1071        assert!(clean.stack.is_empty());
1072        assert!(clean.output.is_empty());
1073        assert!(clean.frames.is_empty());
1074        assert!(clean.exception_handlers.is_empty());
1075        assert!(clean.spawned_tasks.is_empty());
1076        assert!(clean.held_sync_guards.is_empty());
1077        assert_eq!(clean.task_counter, 0);
1078        assert_eq!(clean.runtime_context_counter, 0);
1079        assert!(clean.deadlines.is_empty());
1080        assert!(clean.cancel_token.is_none());
1081        assert!(clean.interrupt_handlers.is_empty());
1082        assert!(clean.error_stack_trace.is_empty());
1083        assert!(clean.bridge.is_none());
1084        assert!(clean
1085            .globals
1086            .get("stable_global")
1087            .is_some_and(|value| value.display() == "baseline"));
1088    }
1089
1090    #[tokio::test]
1091    async fn inline_child_inherits_held_lock_keys_but_concurrent_child_does_not() {
1092        let mut parent = Vm::new();
1093        let permit = parent
1094            .sync_runtime
1095            .acquire("mutex", "v:test", 1, 1, None, None)
1096            .await
1097            .unwrap()
1098            .unwrap();
1099        parent
1100            .held_sync_guards
1101            .push(crate::synchronization::VmSyncHeldGuard {
1102                _permit: permit,
1103                frame_depth: 0,
1104                env_scope_depth: 0,
1105            });
1106        assert_eq!(parent.held_permits_for("mutex", "v:test"), 1);
1107
1108        // An inline child (async builtin awaited while the parent is parked, or
1109        // a closure the builtin runs inline) inherits the held key, so a
1110        // re-acquire is caught as a cross-context self-deadlock (HARN-ORC-011)
1111        // — even transitively through a further inline child.
1112        let inline = parent.child_vm_inline();
1113        assert_eq!(inline.held_permits_for("mutex", "v:test"), 1);
1114        assert_eq!(
1115            inline.child_vm_inline().held_permits_for("mutex", "v:test"),
1116            1
1117        );
1118
1119        // A new concurrent task (spawn / parallel / trigger) does NOT inherit:
1120        // blocking on a parent-held lock there is legitimately resolvable, so
1121        // flagging it would be a false positive.
1122        let concurrent = parent.child_vm();
1123        assert_eq!(concurrent.held_permits_for("mutex", "v:test"), 0);
1124    }
1125
1126    #[test]
1127    fn vm_reports_effective_runtime_limits() {
1128        let vm = Vm::new();
1129
1130        assert_eq!(vm.runtime_limits(), RuntimeLimits::default());
1131        assert_eq!(
1132            vm.runtime_limit_report().entries.len(),
1133            crate::RUNTIME_LIMIT_DESCRIPTIONS.len()
1134        );
1135        assert_eq!(vm.child_vm().runtime_limits(), vm.runtime_limits());
1136        assert_eq!(
1137            vm.baseline().instantiate().runtime_limits(),
1138            vm.runtime_limits()
1139        );
1140    }
1141
1142    #[tokio::test(flavor = "current_thread")]
1143    async fn vm_baseline_rebinds_shared_state_builtins_per_instance() {
1144        let local = tokio::task::LocalSet::new();
1145        local
1146            .run_until(async {
1147                let source = r#"
1148pipeline main() {
1149  let cell = shared_cell({scope: "task_group", key: "turn", initial: 0})
1150  __io_println(shared_get(cell))
1151  shared_set(cell, shared_get(cell) + 1)
1152}"#;
1153                let chunk = crate::compile_source(source).expect("compile");
1154                let baseline = baseline_with_stdlib(source);
1155
1156                let mut first = baseline.instantiate();
1157                first.execute(&chunk).await.expect("first execute");
1158                assert_eq!(first.output(), "0\n");
1159
1160                let mut second = baseline.instantiate();
1161                second.execute(&chunk).await.expect("second execute");
1162                assert_eq!(
1163                    second.output(),
1164                    "0\n",
1165                    "shared state created by the first VM must not leak into the next baseline instance"
1166                );
1167            })
1168            .await;
1169    }
1170}