Skip to main content

harn_vm/vm/
state.rs

1use std::collections::{BTreeMap, HashSet};
2use std::rc::Rc;
3use std::sync::Arc;
4use std::time::Instant;
5
6use crate::chunk::{Chunk, ChunkRef, Constant};
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
38/// Call frame for function execution.
39pub(crate) struct CallFrame {
40    pub(crate) chunk: ChunkRef,
41    pub(crate) ip: usize,
42    pub(crate) stack_base: usize,
43    pub(crate) saved_env: VmEnv,
44    /// Env snapshot captured at call-time, *after* argument binding. Used
45    /// by the debugger's `restartFrame` to rewind this frame to its
46    /// entry state (re-binding args from the original values) without
47    /// re-entering the call site. Cheap to clone because `VmEnv` is
48    /// already cloned into `saved_env` on every call. `None` for
49    /// scratch frames (evaluate, import init) where restart isn't
50    /// meaningful.
51    pub(crate) initial_env: Option<VmEnv>,
52    pub(crate) initial_local_slots: Option<Vec<LocalSlot>>,
53    /// Iterator stack depth to restore when this frame unwinds.
54    pub(crate) saved_iterator_depth: usize,
55    /// Function name for stack traces (empty for top-level pipeline).
56    pub(crate) fn_name: String,
57    /// Number of arguments actually passed by the caller (for default arg support).
58    pub(crate) argc: usize,
59    /// Saved VM_SOURCE_DIR to restore when this frame is popped.
60    /// Set when entering a closure that originated from an imported module.
61    pub(crate) saved_source_dir: Option<std::path::PathBuf>,
62    /// Module-local named functions available to symbolic calls within this frame.
63    pub(crate) module_functions: Option<ModuleFunctionRegistry>,
64    /// Shared module-level env for top-level `var` / `let` bindings of
65    /// this frame's originating module. Looked up after `self.env` and
66    /// before `self.globals` by `GetVar` / `SetVar`, giving each module
67    /// its own live static state that persists across calls. See the
68    /// `module_state` field on `VmClosure` for the full rationale.
69    pub(crate) module_state: Option<crate::value::ModuleState>,
70    /// Slot-indexed locals for compiler-resolved names in this frame.
71    pub(crate) local_slots: Vec<LocalSlot>,
72    /// Env scope index that corresponds to compiler local scope depth 0.
73    pub(crate) local_scope_base: usize,
74    /// Current compiler local scope depth, updated by PushScope/PopScope.
75    pub(crate) local_scope_depth: usize,
76}
77
78/// Exception handler for try/catch.
79pub(crate) struct ExceptionHandler {
80    pub(crate) catch_ip: usize,
81    pub(crate) stack_depth: usize,
82    pub(crate) frame_depth: usize,
83    pub(crate) env_scope_depth: usize,
84    /// If non-empty, this catch only handles errors whose enum_name matches.
85    pub(crate) error_type: String,
86}
87
88/// Iterator state for for-in loops.
89pub(crate) enum IterState {
90    Vec {
91        items: Rc<Vec<VmValue>>,
92        idx: usize,
93    },
94    Dict {
95        entries: Rc<BTreeMap<String, VmValue>>,
96        keys: Vec<String>,
97        idx: usize,
98    },
99    Channel {
100        receiver: std::sync::Arc<tokio::sync::Mutex<tokio::sync::mpsc::Receiver<VmValue>>>,
101        closed: std::sync::Arc<std::sync::atomic::AtomicBool>,
102    },
103    Generator {
104        gen: crate::value::VmGenerator,
105    },
106    Stream {
107        stream: crate::value::VmStream,
108    },
109    /// Step through a lazy range without materializing a Vec.
110    /// `next` holds the value to emit on the next IterNext; `stop` is
111    /// the first value that terminates the iteration (one past the end).
112    Range {
113        next: i64,
114        stop: i64,
115    },
116    VmIter {
117        handle: std::rc::Rc<std::cell::RefCell<crate::vm::iter::VmIter>>,
118    },
119}
120
121#[derive(Clone)]
122pub(crate) enum VmBuiltinDispatch {
123    Sync(VmBuiltinFn),
124    Async(VmAsyncBuiltinFn),
125}
126
127#[derive(Clone)]
128pub(crate) struct VmBuiltinEntry {
129    pub(crate) name: Rc<str>,
130    pub(crate) dispatch: VmBuiltinDispatch,
131}
132
133/// The Harn bytecode virtual machine.
134pub struct Vm {
135    pub(crate) stack: Vec<VmValue>,
136    pub(crate) env: VmEnv,
137    pub(crate) output: String,
138    pub(crate) builtins: Rc<BTreeMap<String, VmBuiltinFn>>,
139    pub(crate) async_builtins: Rc<BTreeMap<String, VmAsyncBuiltinFn>>,
140    pub(crate) builtin_metadata: Rc<BTreeMap<String, VmBuiltinMetadata>>,
141    /// Numeric side index for builtins. Name-keyed maps remain authoritative;
142    /// this index is the hot path for direct builtin bytecode and callback refs.
143    pub(crate) builtins_by_id: Rc<BTreeMap<BuiltinId, VmBuiltinEntry>>,
144    /// IDs with detected name collisions. Collided names safely fall back to
145    /// the authoritative name-keyed lookup path.
146    pub(crate) builtin_id_collisions: Rc<HashSet<BuiltinId>>,
147    /// Iterator state for for-in loops.
148    pub(crate) iterators: Vec<IterState>,
149    /// Call frame stack.
150    pub(crate) frames: Vec<CallFrame>,
151    /// Exception handler stack.
152    pub(crate) exception_handlers: Vec<ExceptionHandler>,
153    /// Spawned async task handles.
154    pub(crate) spawned_tasks: BTreeMap<String, VmTaskHandle>,
155    /// Shared process-local synchronization primitives inherited by child VMs.
156    pub(crate) sync_runtime: Arc<crate::synchronization::VmSyncRuntime>,
157    /// Shared process-local cells, maps, and mailboxes inherited by child VMs.
158    pub(crate) shared_state_runtime: Rc<crate::shared_state::VmSharedStateRuntime>,
159    /// Permits acquired by lexical synchronization blocks in this VM.
160    pub(crate) held_sync_guards: Vec<crate::synchronization::VmSyncHeldGuard>,
161    /// Counter for generating unique task IDs.
162    pub(crate) task_counter: u64,
163    /// Counter for logical runtime-context task groups.
164    pub(crate) runtime_context_counter: u64,
165    /// Logical runtime task context visible through `runtime_context()`.
166    pub(crate) runtime_context: crate::runtime_context::RuntimeContext,
167    /// Active deadline stack: (deadline_instant, frame_depth).
168    pub(crate) deadlines: Vec<(Instant, usize)>,
169    /// Breakpoints, keyed by source-file path so a breakpoint at line N
170    /// in `auto.harn` doesn't also fire when execution hits line N in an
171    /// imported lib. The empty-string key is a wildcard used by callers
172    /// that don't track source paths (legacy `set_breakpoints` API).
173    pub(crate) breakpoints: BTreeMap<String, std::collections::BTreeSet<usize>>,
174    /// Function-name breakpoints. Any closure call whose
175    /// `CompiledFunction.name` matches an entry here raises a stop on
176    /// entry, regardless of the call site's file or line. Lets the IDE
177    /// break on `llm_call` / `host_run_pipeline` / any user pipeline
178    /// function without pinning down a source location first.
179    pub(crate) function_breakpoints: std::collections::BTreeSet<String>,
180    /// Latched on `push_closure_frame` when the callee's name matches
181    /// `function_breakpoints`; consumed by the next step so the stop is
182    /// reported with reason="function breakpoint" and the breakpoint
183    /// name available for the DAP `stopped` event.
184    pub(crate) pending_function_bp: Option<String>,
185    /// Whether the VM is in step mode.
186    pub(crate) step_mode: bool,
187    /// The frame depth at which stepping started (for step-over).
188    pub(crate) step_frame_depth: usize,
189    /// Whether the VM is currently stopped at a debug point.
190    pub(crate) stopped: bool,
191    /// Last source line executed (to detect line changes).
192    pub(crate) last_line: usize,
193    /// Source directory for resolving imports.
194    pub(crate) source_dir: Option<std::path::PathBuf>,
195    /// Modules currently being imported (cycle prevention).
196    pub(crate) imported_paths: Vec<std::path::PathBuf>,
197    /// Loaded module cache keyed by canonical or synthetic module path.
198    pub(crate) module_cache: Rc<BTreeMap<std::path::PathBuf, LoadedModule>>,
199    /// Source text keyed by canonical or synthetic module path for debugger retrieval.
200    pub(crate) source_cache: Rc<BTreeMap<std::path::PathBuf, String>>,
201    /// Source file path for error reporting.
202    pub(crate) source_file: Option<String>,
203    /// Source text for error reporting.
204    pub(crate) source_text: Option<String>,
205    /// Optional bridge for delegating unknown builtins in bridge mode.
206    pub(crate) bridge: Option<Rc<crate::bridge::HostBridge>>,
207    /// Builtins denied by sandbox mode (`--deny` / `--allow` flags).
208    pub(crate) denied_builtins: Rc<HashSet<String>>,
209    /// Cancellation token for cooperative graceful shutdown (set by parent).
210    pub(crate) cancel_token: Option<std::sync::Arc<std::sync::atomic::AtomicBool>>,
211    /// Remaining instruction-boundary checks before a requested host
212    /// cancellation is forcefully raised. This gives `is_cancelled()` loops a
213    /// deterministic chance to return cleanly without letting non-cooperative
214    /// CPU-bound code run forever.
215    pub(crate) cancel_grace_instructions_remaining: Option<usize>,
216    /// Captured stack trace from the most recent error (fn_name, line, col).
217    pub(crate) error_stack_trace: Vec<(String, usize, usize, Option<String>)>,
218    /// Yield channel sender for generator execution. When set, `Op::Yield`
219    /// sends values through this channel instead of being a no-op.
220    pub(crate) yield_sender: Option<tokio::sync::mpsc::Sender<Result<VmValue, VmError>>>,
221    /// Project root directory (detected via harn.toml).
222    /// Used as base directory for metadata, store, and checkpoint operations.
223    pub(crate) project_root: Option<std::path::PathBuf>,
224    /// Global constants (e.g. `pi`, `e`). Checked as a fallback in `GetVar`
225    /// after the environment, so user-defined variables can shadow them.
226    pub(crate) globals: Rc<BTreeMap<String, VmValue>>,
227    /// Optional debugger hook invoked when execution advances to a new source line.
228    pub(crate) debug_hook: Option<Box<DebugHook>>,
229}
230
231impl Vm {
232    pub(crate) fn fresh_local_slots(chunk: &Chunk) -> Vec<LocalSlot> {
233        chunk
234            .local_slots
235            .iter()
236            .map(|_| LocalSlot {
237                value: VmValue::Nil,
238                initialized: false,
239                synced: false,
240            })
241            .collect()
242    }
243
244    pub(crate) fn bind_param_slots(
245        slots: &mut [LocalSlot],
246        func: &crate::chunk::CompiledFunction,
247        args: &[VmValue],
248        synced: bool,
249    ) {
250        let param_count = func.params.len();
251        for (i, _param) in func.params.iter().enumerate() {
252            if i >= slots.len() {
253                break;
254            }
255            if func.has_rest_param && i == param_count - 1 {
256                let rest_args = if i < args.len() {
257                    args[i..].to_vec()
258                } else {
259                    Vec::new()
260                };
261                slots[i].value = VmValue::List(Rc::new(rest_args));
262                slots[i].initialized = true;
263                slots[i].synced = synced;
264            } else if i < args.len() {
265                slots[i].value = args[i].clone();
266                slots[i].initialized = true;
267                slots[i].synced = synced;
268            }
269        }
270    }
271
272    pub(crate) fn visible_variables(&self) -> BTreeMap<String, VmValue> {
273        let mut vars = self.env.all_variables();
274        let Some(frame) = self.frames.last() else {
275            return vars;
276        };
277        for (slot, info) in frame.local_slots.iter().zip(frame.chunk.local_slots.iter()) {
278            if slot.initialized && info.scope_depth <= frame.local_scope_depth {
279                vars.insert(info.name.clone(), slot.value.clone());
280            }
281        }
282        vars
283    }
284
285    pub(crate) fn sync_current_frame_locals_to_env(&mut self) {
286        let frames = &mut self.frames;
287        let env = &mut self.env;
288        let Some(frame) = frames.last_mut() else {
289            return;
290        };
291        let local_scope_base = frame.local_scope_base;
292        let local_scope_depth = frame.local_scope_depth;
293        for (slot, info) in frame
294            .local_slots
295            .iter_mut()
296            .zip(frame.chunk.local_slots.iter())
297        {
298            if slot.initialized && !slot.synced && info.scope_depth <= local_scope_depth {
299                slot.synced = true;
300                let scope_idx = local_scope_base + info.scope_depth;
301                while env.scopes.len() <= scope_idx {
302                    env.push_scope();
303                }
304                env.scopes[scope_idx]
305                    .vars
306                    .insert(info.name.clone(), (slot.value.clone(), info.mutable));
307            }
308        }
309    }
310
311    pub(crate) fn closure_call_env_for_current_frame(
312        &self,
313        closure: &crate::value::VmClosure,
314    ) -> VmEnv {
315        if closure.module_state.is_some() {
316            return closure.env.clone();
317        }
318        let mut call_env = Self::closure_call_env(&self.env, closure);
319        let Some(frame) = self.frames.last() else {
320            return call_env;
321        };
322        for (slot, info) in frame
323            .local_slots
324            .iter()
325            .zip(frame.chunk.local_slots.iter())
326            .filter(|(slot, info)| slot.initialized && info.scope_depth <= frame.local_scope_depth)
327        {
328            if matches!(slot.value, VmValue::Closure(_)) && !call_env.contains(&info.name) {
329                let _ = call_env.define(&info.name, slot.value.clone(), info.mutable);
330            }
331        }
332        call_env
333    }
334
335    pub(crate) fn active_local_slot_value(&self, name: &str) -> Option<VmValue> {
336        let frame = self.frames.last()?;
337        for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
338            if info.name == name && info.scope_depth <= frame.local_scope_depth {
339                let slot = frame.local_slots.get(idx)?;
340                if slot.initialized {
341                    return Some(slot.value.clone());
342                }
343            }
344        }
345        None
346    }
347
348    pub(crate) fn assign_active_local_slot(
349        &mut self,
350        name: &str,
351        value: VmValue,
352        debug: bool,
353    ) -> Result<bool, VmError> {
354        let Some(frame) = self.frames.last_mut() else {
355            return Ok(false);
356        };
357        for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
358            if info.name == name && info.scope_depth <= frame.local_scope_depth {
359                if !debug && !info.mutable {
360                    return Err(VmError::ImmutableAssignment(name.to_string()));
361                }
362                if let Some(slot) = frame.local_slots.get_mut(idx) {
363                    slot.value = value;
364                    slot.initialized = true;
365                    slot.synced = false;
366                    return Ok(true);
367                }
368            }
369        }
370        Ok(false)
371    }
372
373    pub fn new() -> Self {
374        Self {
375            stack: Vec::with_capacity(256),
376            env: VmEnv::new(),
377            output: String::new(),
378            builtins: Rc::new(BTreeMap::new()),
379            async_builtins: Rc::new(BTreeMap::new()),
380            builtin_metadata: Rc::new(BTreeMap::new()),
381            builtins_by_id: Rc::new(BTreeMap::new()),
382            builtin_id_collisions: Rc::new(HashSet::new()),
383            iterators: Vec::new(),
384            frames: Vec::new(),
385            exception_handlers: Vec::new(),
386            spawned_tasks: BTreeMap::new(),
387            sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
388            shared_state_runtime: Rc::new(crate::shared_state::VmSharedStateRuntime::new()),
389            held_sync_guards: Vec::new(),
390            task_counter: 0,
391            runtime_context_counter: 0,
392            runtime_context: crate::runtime_context::RuntimeContext::root(),
393            deadlines: Vec::new(),
394            breakpoints: BTreeMap::new(),
395            function_breakpoints: std::collections::BTreeSet::new(),
396            pending_function_bp: None,
397            step_mode: false,
398            step_frame_depth: 0,
399            stopped: false,
400            last_line: 0,
401            source_dir: None,
402            imported_paths: Vec::new(),
403            module_cache: Rc::new(BTreeMap::new()),
404            source_cache: Rc::new(BTreeMap::new()),
405            source_file: None,
406            source_text: None,
407            bridge: None,
408            denied_builtins: Rc::new(HashSet::new()),
409            cancel_token: None,
410            cancel_grace_instructions_remaining: None,
411            error_stack_trace: Vec::new(),
412            yield_sender: None,
413            project_root: None,
414            globals: Rc::new(BTreeMap::new()),
415            debug_hook: None,
416        }
417    }
418
419    /// Set the bridge for delegating unknown builtins in bridge mode.
420    pub fn set_bridge(&mut self, bridge: Rc<crate::bridge::HostBridge>) {
421        self.bridge = Some(bridge);
422    }
423
424    /// Set builtins that are denied in sandbox mode.
425    /// When called, the given builtin names will produce a permission error.
426    pub fn set_denied_builtins(&mut self, denied: HashSet<String>) {
427        self.denied_builtins = Rc::new(denied);
428    }
429
430    /// Set source info for error reporting (file path and source text).
431    pub fn set_source_info(&mut self, file: &str, text: &str) {
432        self.source_file = Some(file.to_string());
433        self.source_text = Some(text.to_string());
434        Rc::make_mut(&mut self.source_cache)
435            .insert(std::path::PathBuf::from(file), text.to_string());
436    }
437
438    /// Initialize execution (push the initial frame).
439    pub fn start(&mut self, chunk: &Chunk) {
440        let initial_env = self.env.clone();
441        self.frames.push(CallFrame {
442            chunk: Rc::new(chunk.clone()),
443            ip: 0,
444            stack_base: self.stack.len(),
445            saved_env: self.env.clone(),
446            // The top-level pipeline frame captures env at start so
447            // restartFrame on the outermost frame rewinds to the
448            // pre-pipeline state — basically "restart session" in
449            // debugger terms.
450            initial_env: Some(initial_env),
451            initial_local_slots: Some(Self::fresh_local_slots(chunk)),
452            saved_iterator_depth: self.iterators.len(),
453            fn_name: String::new(),
454            argc: 0,
455            saved_source_dir: None,
456            module_functions: None,
457            module_state: None,
458            local_slots: Self::fresh_local_slots(chunk),
459            local_scope_base: self.env.scope_depth().saturating_sub(1),
460            local_scope_depth: 0,
461        });
462    }
463
464    /// Create a child VM that shares builtins and env but has fresh execution state.
465    /// Used for parallel/spawn to fork the VM for concurrent tasks.
466    pub(crate) fn child_vm(&self) -> Vm {
467        Vm {
468            stack: Vec::with_capacity(64),
469            env: self.env.clone(),
470            output: String::new(),
471            builtins: Rc::clone(&self.builtins),
472            async_builtins: Rc::clone(&self.async_builtins),
473            builtin_metadata: Rc::clone(&self.builtin_metadata),
474            builtins_by_id: Rc::clone(&self.builtins_by_id),
475            builtin_id_collisions: Rc::clone(&self.builtin_id_collisions),
476            iterators: Vec::new(),
477            frames: Vec::new(),
478            exception_handlers: Vec::new(),
479            spawned_tasks: BTreeMap::new(),
480            sync_runtime: self.sync_runtime.clone(),
481            shared_state_runtime: self.shared_state_runtime.clone(),
482            held_sync_guards: Vec::new(),
483            task_counter: 0,
484            runtime_context_counter: self.runtime_context_counter,
485            runtime_context: self.runtime_context.clone(),
486            deadlines: self.deadlines.clone(),
487            breakpoints: BTreeMap::new(),
488            function_breakpoints: std::collections::BTreeSet::new(),
489            pending_function_bp: None,
490            step_mode: false,
491            step_frame_depth: 0,
492            stopped: false,
493            last_line: 0,
494            source_dir: self.source_dir.clone(),
495            imported_paths: Vec::new(),
496            module_cache: Rc::clone(&self.module_cache),
497            source_cache: Rc::clone(&self.source_cache),
498            source_file: self.source_file.clone(),
499            source_text: self.source_text.clone(),
500            bridge: self.bridge.clone(),
501            denied_builtins: Rc::clone(&self.denied_builtins),
502            cancel_token: self.cancel_token.clone(),
503            cancel_grace_instructions_remaining: None,
504            error_stack_trace: Vec::new(),
505            yield_sender: None,
506            project_root: self.project_root.clone(),
507            globals: Rc::clone(&self.globals),
508            debug_hook: None,
509        }
510    }
511
512    /// Create a child VM for external adapters that need to invoke Harn
513    /// closures while sharing the parent's builtins, globals, and module state.
514    pub(crate) fn child_vm_for_host(&self) -> Vm {
515        self.child_vm()
516    }
517
518    /// Request cancellation for every outstanding child task owned by this VM
519    /// and then abort the join handles. This prevents un-awaited spawned tasks
520    /// from outliving their parent execution scope.
521    pub(crate) fn cancel_spawned_tasks(&mut self) {
522        for (_, task) in std::mem::take(&mut self.spawned_tasks) {
523            task.cancel_token
524                .store(true, std::sync::atomic::Ordering::SeqCst);
525            task.handle.abort();
526        }
527    }
528
529    /// Set the source directory for import resolution and introspection.
530    /// Also auto-detects the project root if not already set.
531    pub fn set_source_dir(&mut self, dir: &std::path::Path) {
532        let dir = crate::stdlib::process::normalize_context_path(dir);
533        self.source_dir = Some(dir.clone());
534        crate::stdlib::set_thread_source_dir(&dir);
535        // Auto-detect project root if not explicitly set.
536        if self.project_root.is_none() {
537            self.project_root = crate::stdlib::process::find_project_root(&dir);
538        }
539    }
540
541    /// Explicitly set the project root directory.
542    /// Used by ACP/CLI to override auto-detection.
543    pub fn set_project_root(&mut self, root: &std::path::Path) {
544        self.project_root = Some(root.to_path_buf());
545    }
546
547    /// Get the project root directory, falling back to source_dir.
548    pub fn project_root(&self) -> Option<&std::path::Path> {
549        self.project_root.as_deref().or(self.source_dir.as_deref())
550    }
551
552    /// Return all registered builtin names (sync + async).
553    pub fn builtin_names(&self) -> Vec<String> {
554        let mut names: Vec<String> = self.builtins.keys().cloned().collect();
555        names.extend(self.async_builtins.keys().cloned());
556        names
557    }
558
559    /// Return discoverable metadata for registered builtins.
560    pub fn builtin_metadata(&self) -> Vec<VmBuiltinMetadata> {
561        self.builtin_metadata.values().cloned().collect()
562    }
563
564    /// Return discoverable metadata for a registered builtin name.
565    pub fn builtin_metadata_for(&self, name: &str) -> Option<&VmBuiltinMetadata> {
566        self.builtin_metadata.get(name)
567    }
568
569    /// Set a global constant (e.g. `pi`, `e`).
570    /// Stored separately from the environment so user-defined variables can shadow them.
571    pub fn set_global(&mut self, name: &str, value: VmValue) {
572        Rc::make_mut(&mut self.globals).insert(name.to_string(), value);
573    }
574
575    /// Get the captured output.
576    pub fn output(&self) -> &str {
577        &self.output
578    }
579
580    /// Drain and return the captured output, leaving the buffer empty.
581    /// Used by the async-builtin dispatch path to forward closure output
582    /// from a child VM back to its parent.
583    pub fn take_output(&mut self) -> String {
584        std::mem::take(&mut self.output)
585    }
586
587    /// Append text to this VM's captured output. Used to forward output
588    /// from child VMs (e.g. closures invoked via `call_closure_pub`)
589    /// back into the parent stream.
590    pub fn append_output(&mut self, text: &str) {
591        self.output.push_str(text);
592    }
593
594    pub(crate) fn pop(&mut self) -> Result<VmValue, VmError> {
595        self.stack.pop().ok_or(VmError::StackUnderflow)
596    }
597
598    pub(crate) fn peek(&self) -> Result<&VmValue, VmError> {
599        self.stack.last().ok_or(VmError::StackUnderflow)
600    }
601
602    pub(crate) fn const_string(c: &Constant) -> Result<String, VmError> {
603        match c {
604            Constant::String(s) => Ok(s.clone()),
605            _ => Err(VmError::TypeError("expected string constant".into())),
606        }
607    }
608
609    pub(crate) fn const_str(c: &Constant) -> Result<&str, VmError> {
610        match c {
611            Constant::String(s) => Ok(s.as_str()),
612            _ => Err(VmError::TypeError("expected string constant".into())),
613        }
614    }
615
616    pub(crate) fn release_sync_guards_for_current_scope(&mut self) {
617        let depth = self.env.scope_depth();
618        self.held_sync_guards
619            .retain(|guard| guard.env_scope_depth < depth);
620    }
621
622    pub(crate) fn release_sync_guards_after_unwind(
623        &mut self,
624        frame_depth: usize,
625        env_scope_depth: usize,
626    ) {
627        self.held_sync_guards.retain(|guard| {
628            guard.frame_depth <= frame_depth && guard.env_scope_depth <= env_scope_depth
629        });
630    }
631
632    pub(crate) fn release_sync_guards_for_frame(&mut self, frame_depth: usize) {
633        self.held_sync_guards
634            .retain(|guard| guard.frame_depth != frame_depth);
635    }
636}
637
638impl Drop for Vm {
639    fn drop(&mut self) {
640        self.cancel_spawned_tasks();
641    }
642}
643
644impl Default for Vm {
645    fn default() -> Self {
646        Self::new()
647    }
648}