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