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
16pub(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#[derive(Clone)]
39pub(crate) struct InterruptHandler {
40 pub(crate) handle: i64,
41 pub(crate) signals: Vec<String>,
42 pub(crate) once: bool,
43 pub(crate) graceful_timeout_ms: Option<u64>,
44 pub(crate) handler: VmValue,
45}
46
47pub(crate) struct CallFrame {
49 pub(crate) chunk: ChunkRef,
50 pub(crate) ip: usize,
51 pub(crate) stack_base: usize,
52 pub(crate) saved_env: VmEnv,
53 pub(crate) initial_env: Option<VmEnv>,
61 pub(crate) initial_local_slots: Option<Vec<LocalSlot>>,
62 pub(crate) saved_iterator_depth: usize,
64 pub(crate) fn_name: String,
66 pub(crate) argc: usize,
68 pub(crate) saved_source_dir: Option<std::path::PathBuf>,
71 pub(crate) module_functions: Option<ModuleFunctionRegistry>,
73 pub(crate) module_state: Option<crate::value::ModuleState>,
79 pub(crate) local_slots: Vec<LocalSlot>,
81 pub(crate) local_scope_base: usize,
83 pub(crate) local_scope_depth: usize,
85}
86
87pub(crate) struct ExceptionHandler {
89 pub(crate) catch_ip: usize,
90 pub(crate) stack_depth: usize,
91 pub(crate) frame_depth: usize,
92 pub(crate) env_scope_depth: usize,
93 pub(crate) error_type: String,
95}
96
97pub(crate) enum IterState {
99 Vec {
100 items: Rc<Vec<VmValue>>,
101 idx: usize,
102 },
103 Dict {
104 entries: Rc<BTreeMap<String, VmValue>>,
105 keys: Vec<String>,
106 idx: usize,
107 },
108 Channel {
109 receiver: std::sync::Arc<tokio::sync::Mutex<tokio::sync::mpsc::Receiver<VmValue>>>,
110 closed: std::sync::Arc<std::sync::atomic::AtomicBool>,
111 },
112 Generator {
113 gen: crate::value::VmGenerator,
114 },
115 Stream {
116 stream: crate::value::VmStream,
117 },
118 Range {
122 next: i64,
123 end: i64,
124 inclusive: bool,
125 done: bool,
126 },
127 VmIter {
128 handle: std::rc::Rc<std::cell::RefCell<crate::vm::iter::VmIter>>,
129 },
130}
131
132#[derive(Clone)]
133pub(crate) enum VmBuiltinDispatch {
134 Sync(VmBuiltinFn),
135 Async(VmAsyncBuiltinFn),
136}
137
138#[derive(Clone)]
139pub(crate) struct VmBuiltinEntry {
140 pub(crate) name: Rc<str>,
141 pub(crate) dispatch: VmBuiltinDispatch,
142}
143
144pub struct Vm {
146 pub(crate) stack: Vec<VmValue>,
147 pub(crate) env: VmEnv,
148 pub(crate) output: String,
149 pub(crate) builtins: Rc<BTreeMap<String, VmBuiltinFn>>,
150 pub(crate) async_builtins: Rc<BTreeMap<String, VmAsyncBuiltinFn>>,
151 pub(crate) builtin_metadata: Rc<BTreeMap<String, VmBuiltinMetadata>>,
152 pub(crate) builtins_by_id: Rc<BTreeMap<BuiltinId, VmBuiltinEntry>>,
155 pub(crate) builtin_id_collisions: Rc<HashSet<BuiltinId>>,
158 pub(crate) iterators: Vec<IterState>,
160 pub(crate) frames: Vec<CallFrame>,
162 pub(crate) exception_handlers: Vec<ExceptionHandler>,
164 pub(crate) spawned_tasks: BTreeMap<String, VmTaskHandle>,
166 pub(crate) sync_runtime: Arc<crate::synchronization::VmSyncRuntime>,
168 pub(crate) shared_state_runtime: Rc<crate::shared_state::VmSharedStateRuntime>,
170 pub(crate) held_sync_guards: Vec<crate::synchronization::VmSyncHeldGuard>,
172 pub(crate) task_counter: u64,
174 pub(crate) runtime_context_counter: u64,
176 pub(crate) runtime_context: crate::runtime_context::RuntimeContext,
178 pub(crate) deadlines: Vec<(Instant, usize)>,
180 pub(crate) breakpoints: BTreeMap<String, std::collections::BTreeSet<usize>>,
185 pub(crate) function_breakpoints: std::collections::BTreeSet<String>,
191 pub(crate) pending_function_bp: Option<String>,
196 pub(crate) step_mode: bool,
198 pub(crate) step_frame_depth: usize,
200 pub(crate) stopped: bool,
202 pub(crate) last_line: usize,
204 pub(crate) source_dir: Option<std::path::PathBuf>,
206 pub(crate) imported_paths: Vec<std::path::PathBuf>,
208 pub(crate) module_cache: Rc<BTreeMap<std::path::PathBuf, LoadedModule>>,
210 pub(crate) source_cache: Rc<BTreeMap<std::path::PathBuf, String>>,
212 pub(crate) source_file: Option<String>,
214 pub(crate) source_text: Option<String>,
216 pub(crate) bridge: Option<Rc<crate::bridge::HostBridge>>,
218 pub(crate) denied_builtins: Rc<HashSet<String>>,
220 pub(crate) cancel_token: Option<std::sync::Arc<std::sync::atomic::AtomicBool>>,
222 pub(crate) interrupt_signal_token: Option<std::sync::Arc<std::sync::Mutex<Option<String>>>>,
223 pub(crate) cancel_grace_instructions_remaining: Option<usize>,
228 pub(crate) interrupt_handlers: Vec<InterruptHandler>,
230 pub(crate) next_interrupt_handle: i64,
231 pub(crate) pending_interrupt_signal: Option<String>,
232 pub(crate) interrupted: bool,
233 pub(crate) dispatching_interrupt: bool,
234 pub(crate) interrupt_handler_deadline: Option<Instant>,
235 pub(crate) error_stack_trace: Vec<(String, usize, usize, Option<String>)>,
237 pub(crate) yield_sender: Option<tokio::sync::mpsc::Sender<Result<VmValue, VmError>>>,
240 pub(crate) project_root: Option<std::path::PathBuf>,
243 pub(crate) globals: Rc<BTreeMap<String, VmValue>>,
246 pub(crate) debug_hook: Option<Box<DebugHook>>,
248}
249
250#[derive(Clone)]
258pub struct VmBaseline {
259 builtins: Rc<BTreeMap<String, VmBuiltinFn>>,
260 async_builtins: Rc<BTreeMap<String, VmAsyncBuiltinFn>>,
261 builtin_metadata: Rc<BTreeMap<String, VmBuiltinMetadata>>,
262 builtins_by_id: Rc<BTreeMap<BuiltinId, VmBuiltinEntry>>,
263 builtin_id_collisions: Rc<HashSet<BuiltinId>>,
264 source_dir: Option<std::path::PathBuf>,
265 source_file: Option<String>,
266 source_text: Option<String>,
267 project_root: Option<std::path::PathBuf>,
268 globals: Rc<BTreeMap<String, VmValue>>,
269 denied_builtins: Rc<HashSet<String>>,
270}
271
272impl VmBaseline {
273 pub fn from_vm(vm: &Vm) -> Self {
274 Self {
275 builtins: Rc::clone(&vm.builtins),
276 async_builtins: Rc::clone(&vm.async_builtins),
277 builtin_metadata: Rc::clone(&vm.builtin_metadata),
278 builtins_by_id: Rc::clone(&vm.builtins_by_id),
279 builtin_id_collisions: Rc::clone(&vm.builtin_id_collisions),
280 source_dir: vm.source_dir.clone(),
281 source_file: vm.source_file.clone(),
282 source_text: vm.source_text.clone(),
283 project_root: vm.project_root.clone(),
284 globals: Rc::clone(&vm.globals),
285 denied_builtins: Rc::clone(&vm.denied_builtins),
286 }
287 }
288
289 pub fn instantiate(&self) -> Vm {
290 let mut source_cache = BTreeMap::new();
291 if let (Some(file), Some(text)) = (&self.source_file, &self.source_text) {
292 source_cache.insert(std::path::PathBuf::from(file), text.clone());
293 }
294 if let Some(dir) = &self.source_dir {
295 crate::stdlib::set_thread_source_dir(dir);
296 }
297
298 let mut vm = Vm {
299 stack: Vec::with_capacity(256),
300 env: VmEnv::new(),
301 output: String::new(),
302 builtins: Rc::clone(&self.builtins),
303 async_builtins: Rc::clone(&self.async_builtins),
304 builtin_metadata: Rc::clone(&self.builtin_metadata),
305 builtins_by_id: Rc::clone(&self.builtins_by_id),
306 builtin_id_collisions: Rc::clone(&self.builtin_id_collisions),
307 iterators: Vec::new(),
308 frames: Vec::new(),
309 exception_handlers: Vec::new(),
310 spawned_tasks: BTreeMap::new(),
311 sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
312 shared_state_runtime: Rc::new(crate::shared_state::VmSharedStateRuntime::new()),
313 held_sync_guards: Vec::new(),
314 task_counter: 0,
315 runtime_context_counter: 0,
316 runtime_context: crate::runtime_context::RuntimeContext::root(),
317 deadlines: Vec::new(),
318 breakpoints: BTreeMap::new(),
319 function_breakpoints: std::collections::BTreeSet::new(),
320 pending_function_bp: None,
321 step_mode: false,
322 step_frame_depth: 0,
323 stopped: false,
324 last_line: 0,
325 source_dir: self.source_dir.clone(),
326 imported_paths: Vec::new(),
327 module_cache: Rc::new(BTreeMap::new()),
328 source_cache: Rc::new(source_cache),
329 source_file: self.source_file.clone(),
330 source_text: self.source_text.clone(),
331 bridge: None,
332 denied_builtins: Rc::clone(&self.denied_builtins),
333 cancel_token: None,
334 interrupt_signal_token: None,
335 cancel_grace_instructions_remaining: None,
336 interrupt_handlers: Vec::new(),
337 next_interrupt_handle: 1,
338 pending_interrupt_signal: None,
339 interrupted: false,
340 dispatching_interrupt: false,
341 interrupt_handler_deadline: None,
342 error_stack_trace: Vec::new(),
343 yield_sender: None,
344 project_root: self.project_root.clone(),
345 globals: Rc::clone(&self.globals),
346 debug_hook: None,
347 };
348
349 crate::stdlib::rebind_execution_state_builtins(&mut vm);
350 vm
351 }
352}
353
354impl Vm {
355 pub(crate) fn fresh_local_slots(chunk: &Chunk) -> Vec<LocalSlot> {
356 chunk
357 .local_slots
358 .iter()
359 .map(|_| LocalSlot {
360 value: VmValue::Nil,
361 initialized: false,
362 synced: false,
363 })
364 .collect()
365 }
366
367 pub(crate) fn bind_param_slots(
368 slots: &mut [LocalSlot],
369 func: &crate::chunk::CompiledFunction,
370 args: &[VmValue],
371 synced: bool,
372 ) {
373 let param_count = func.params.len();
374 for (i, _param) in func.params.iter().enumerate() {
375 if i >= slots.len() {
376 break;
377 }
378 if func.has_rest_param && i == param_count - 1 {
379 let rest_args = if i < args.len() {
380 args[i..].to_vec()
381 } else {
382 Vec::new()
383 };
384 slots[i].value = VmValue::List(Rc::new(rest_args));
385 slots[i].initialized = true;
386 slots[i].synced = synced;
387 } else if i < args.len() {
388 slots[i].value = args[i].clone();
389 slots[i].initialized = true;
390 slots[i].synced = synced;
391 }
392 }
393 }
394
395 pub(crate) fn visible_variables(&self) -> BTreeMap<String, VmValue> {
396 let mut vars = self.env.all_variables();
397 let Some(frame) = self.frames.last() else {
398 return vars;
399 };
400 for (slot, info) in frame.local_slots.iter().zip(frame.chunk.local_slots.iter()) {
401 if slot.initialized && info.scope_depth <= frame.local_scope_depth {
402 vars.insert(info.name.clone(), slot.value.clone());
403 }
404 }
405 vars
406 }
407
408 pub(crate) fn sync_current_frame_locals_to_env(&mut self) {
409 let frames = &mut self.frames;
410 let env = &mut self.env;
411 let Some(frame) = frames.last_mut() else {
412 return;
413 };
414 let local_scope_base = frame.local_scope_base;
415 let local_scope_depth = frame.local_scope_depth;
416 for (slot, info) in frame
417 .local_slots
418 .iter_mut()
419 .zip(frame.chunk.local_slots.iter())
420 {
421 if slot.initialized && !slot.synced && info.scope_depth <= local_scope_depth {
422 slot.synced = true;
423 let scope_idx = local_scope_base + info.scope_depth;
424 while env.scopes.len() <= scope_idx {
425 env.push_scope();
426 }
427 env.scopes[scope_idx]
428 .vars
429 .insert(info.name.clone(), (slot.value.clone(), info.mutable));
430 }
431 }
432 }
433
434 pub(crate) fn closure_call_env_for_current_frame(
435 &self,
436 closure: &crate::value::VmClosure,
437 ) -> VmEnv {
438 if closure.module_state.is_some() {
439 return closure.env.clone();
440 }
441 let mut call_env = Self::closure_call_env(&self.env, closure);
442 let Some(frame) = self.frames.last() else {
443 return call_env;
444 };
445 for (slot, info) in frame
446 .local_slots
447 .iter()
448 .zip(frame.chunk.local_slots.iter())
449 .filter(|(slot, info)| slot.initialized && info.scope_depth <= frame.local_scope_depth)
450 {
451 if matches!(slot.value, VmValue::Closure(_)) && !call_env.contains(&info.name) {
452 let _ = call_env.define(&info.name, slot.value.clone(), info.mutable);
453 }
454 }
455 call_env
456 }
457
458 pub(crate) fn active_local_slot_value(&self, name: &str) -> Option<VmValue> {
459 let frame = self.frames.last()?;
460 let idx = self.active_local_slot_index(name)?;
461 frame.local_slots.get(idx).map(|slot| slot.value.clone())
462 }
463
464 pub(crate) fn active_local_slot_index(&self, name: &str) -> Option<usize> {
469 let frame = self.frames.last()?;
470 for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
471 if info.name == name && info.scope_depth <= frame.local_scope_depth {
472 if let Some(slot) = frame.local_slots.get(idx) {
473 if slot.initialized {
474 return Some(idx);
475 }
476 }
477 }
478 }
479 None
480 }
481
482 pub(crate) fn assign_active_local_slot(
483 &mut self,
484 name: &str,
485 value: VmValue,
486 debug: bool,
487 ) -> Result<bool, VmError> {
488 let Some(frame) = self.frames.last_mut() else {
489 return Ok(false);
490 };
491 for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
492 if info.name == name && info.scope_depth <= frame.local_scope_depth {
493 if !debug && !info.mutable {
494 return Err(VmError::ImmutableAssignment(name.to_string()));
495 }
496 if let Some(slot) = frame.local_slots.get_mut(idx) {
497 slot.value = value;
498 slot.initialized = true;
499 slot.synced = false;
500 return Ok(true);
501 }
502 }
503 }
504 Ok(false)
505 }
506
507 pub fn new() -> Self {
508 Self {
509 stack: Vec::with_capacity(256),
510 env: VmEnv::new(),
511 output: String::new(),
512 builtins: Rc::new(BTreeMap::new()),
513 async_builtins: Rc::new(BTreeMap::new()),
514 builtin_metadata: Rc::new(BTreeMap::new()),
515 builtins_by_id: Rc::new(BTreeMap::new()),
516 builtin_id_collisions: Rc::new(HashSet::new()),
517 iterators: Vec::new(),
518 frames: Vec::new(),
519 exception_handlers: Vec::new(),
520 spawned_tasks: BTreeMap::new(),
521 sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
522 shared_state_runtime: Rc::new(crate::shared_state::VmSharedStateRuntime::new()),
523 held_sync_guards: Vec::new(),
524 task_counter: 0,
525 runtime_context_counter: 0,
526 runtime_context: crate::runtime_context::RuntimeContext::root(),
527 deadlines: Vec::new(),
528 breakpoints: BTreeMap::new(),
529 function_breakpoints: std::collections::BTreeSet::new(),
530 pending_function_bp: None,
531 step_mode: false,
532 step_frame_depth: 0,
533 stopped: false,
534 last_line: 0,
535 source_dir: None,
536 imported_paths: Vec::new(),
537 module_cache: Rc::new(BTreeMap::new()),
538 source_cache: Rc::new(BTreeMap::new()),
539 source_file: None,
540 source_text: None,
541 bridge: None,
542 denied_builtins: Rc::new(HashSet::new()),
543 cancel_token: None,
544 interrupt_signal_token: None,
545 cancel_grace_instructions_remaining: None,
546 interrupt_handlers: Vec::new(),
547 next_interrupt_handle: 1,
548 pending_interrupt_signal: None,
549 interrupted: false,
550 dispatching_interrupt: false,
551 interrupt_handler_deadline: None,
552 error_stack_trace: Vec::new(),
553 yield_sender: None,
554 project_root: None,
555 globals: Rc::new(BTreeMap::new()),
556 debug_hook: None,
557 }
558 }
559
560 pub fn baseline(&self) -> VmBaseline {
561 VmBaseline::from_vm(self)
562 }
563
564 pub fn set_bridge(&mut self, bridge: Rc<crate::bridge::HostBridge>) {
566 self.bridge = Some(bridge);
567 }
568
569 pub fn set_denied_builtins(&mut self, denied: HashSet<String>) {
572 self.denied_builtins = Rc::new(denied);
573 }
574
575 pub fn set_source_info(&mut self, file: &str, text: &str) {
577 self.source_file = Some(file.to_string());
578 self.source_text = Some(text.to_string());
579 Rc::make_mut(&mut self.source_cache)
580 .insert(std::path::PathBuf::from(file), text.to_string());
581 }
582
583 pub fn start(&mut self, chunk: &Chunk) {
585 let initial_env = self.env.clone();
586 self.frames.push(CallFrame {
587 chunk: Rc::new(chunk.clone()),
588 ip: 0,
589 stack_base: self.stack.len(),
590 saved_env: self.env.clone(),
591 initial_env: Some(initial_env),
596 initial_local_slots: Some(Self::fresh_local_slots(chunk)),
597 saved_iterator_depth: self.iterators.len(),
598 fn_name: String::new(),
599 argc: 0,
600 saved_source_dir: None,
601 module_functions: None,
602 module_state: None,
603 local_slots: Self::fresh_local_slots(chunk),
604 local_scope_base: self.env.scope_depth().saturating_sub(1),
605 local_scope_depth: 0,
606 });
607 }
608
609 pub(crate) fn child_vm(&self) -> Vm {
612 Vm {
613 stack: Vec::with_capacity(64),
614 env: self.env.clone(),
615 output: String::new(),
616 builtins: Rc::clone(&self.builtins),
617 async_builtins: Rc::clone(&self.async_builtins),
618 builtin_metadata: Rc::clone(&self.builtin_metadata),
619 builtins_by_id: Rc::clone(&self.builtins_by_id),
620 builtin_id_collisions: Rc::clone(&self.builtin_id_collisions),
621 iterators: Vec::new(),
622 frames: Vec::new(),
623 exception_handlers: Vec::new(),
624 spawned_tasks: BTreeMap::new(),
625 sync_runtime: self.sync_runtime.clone(),
626 shared_state_runtime: self.shared_state_runtime.clone(),
627 held_sync_guards: Vec::new(),
628 task_counter: 0,
629 runtime_context_counter: self.runtime_context_counter,
630 runtime_context: self.runtime_context.clone(),
631 deadlines: self.deadlines.clone(),
632 breakpoints: BTreeMap::new(),
633 function_breakpoints: std::collections::BTreeSet::new(),
634 pending_function_bp: None,
635 step_mode: false,
636 step_frame_depth: 0,
637 stopped: false,
638 last_line: 0,
639 source_dir: self.source_dir.clone(),
640 imported_paths: Vec::new(),
641 module_cache: Rc::clone(&self.module_cache),
642 source_cache: Rc::clone(&self.source_cache),
643 source_file: self.source_file.clone(),
644 source_text: self.source_text.clone(),
645 bridge: self.bridge.clone(),
646 denied_builtins: Rc::clone(&self.denied_builtins),
647 cancel_token: self.cancel_token.clone(),
648 interrupt_signal_token: self.interrupt_signal_token.clone(),
649 cancel_grace_instructions_remaining: None,
650 interrupt_handlers: Vec::new(),
651 next_interrupt_handle: 1,
652 pending_interrupt_signal: None,
653 interrupted: self.interrupted,
654 dispatching_interrupt: false,
655 interrupt_handler_deadline: None,
656 error_stack_trace: Vec::new(),
657 yield_sender: None,
658 project_root: self.project_root.clone(),
659 globals: Rc::clone(&self.globals),
660 debug_hook: None,
661 }
662 }
663
664 pub(crate) fn child_vm_for_host(&self) -> Vm {
667 self.child_vm()
668 }
669
670 pub(crate) fn cancel_spawned_tasks(&mut self) {
674 for (_, task) in std::mem::take(&mut self.spawned_tasks) {
675 task.cancel_token
676 .store(true, std::sync::atomic::Ordering::SeqCst);
677 task.handle.abort();
678 }
679 }
680
681 pub fn set_source_dir(&mut self, dir: &std::path::Path) {
684 let dir = crate::stdlib::process::normalize_context_path(dir);
685 self.source_dir = Some(dir.clone());
686 crate::stdlib::set_thread_source_dir(&dir);
687 if self.project_root.is_none() {
689 self.project_root = crate::stdlib::process::find_project_root(&dir);
690 }
691 }
692
693 pub fn set_project_root(&mut self, root: &std::path::Path) {
696 self.project_root = Some(root.to_path_buf());
697 }
698
699 pub fn project_root(&self) -> Option<&std::path::Path> {
701 self.project_root.as_deref().or(self.source_dir.as_deref())
702 }
703
704 pub fn builtin_names(&self) -> Vec<String> {
706 let mut names: Vec<String> = self.builtins.keys().cloned().collect();
707 names.extend(self.async_builtins.keys().cloned());
708 names
709 }
710
711 pub fn builtin_metadata(&self) -> Vec<VmBuiltinMetadata> {
713 self.builtin_metadata.values().cloned().collect()
714 }
715
716 pub fn builtin_metadata_for(&self, name: &str) -> Option<&VmBuiltinMetadata> {
718 self.builtin_metadata.get(name)
719 }
720
721 pub fn set_global(&mut self, name: &str, value: VmValue) {
724 Rc::make_mut(&mut self.globals).insert(name.to_string(), value);
725 }
726
727 pub fn output(&self) -> &str {
729 &self.output
730 }
731
732 pub fn take_output(&mut self) -> String {
736 std::mem::take(&mut self.output)
737 }
738
739 pub fn append_output(&mut self, text: &str) {
743 self.output.push_str(text);
744 }
745
746 pub(crate) fn pop(&mut self) -> Result<VmValue, VmError> {
747 self.stack.pop().ok_or(VmError::StackUnderflow)
748 }
749
750 pub(crate) fn peek(&self) -> Result<&VmValue, VmError> {
751 self.stack.last().ok_or(VmError::StackUnderflow)
752 }
753
754 pub(crate) fn const_string(c: &Constant) -> Result<String, VmError> {
755 match c {
756 Constant::String(s) => Ok(s.clone()),
757 _ => Err(VmError::TypeError("expected string constant".into())),
758 }
759 }
760
761 pub(crate) fn const_str(c: &Constant) -> Result<&str, VmError> {
762 match c {
763 Constant::String(s) => Ok(s.as_str()),
764 _ => Err(VmError::TypeError("expected string constant".into())),
765 }
766 }
767
768 pub(crate) fn release_sync_guards_for_current_scope(&mut self) {
769 let depth = self.env.scope_depth();
770 self.held_sync_guards
771 .retain(|guard| guard.env_scope_depth < depth);
772 }
773
774 pub(crate) fn release_sync_guards_after_unwind(
775 &mut self,
776 frame_depth: usize,
777 env_scope_depth: usize,
778 ) {
779 self.held_sync_guards.retain(|guard| {
780 guard.frame_depth <= frame_depth && guard.env_scope_depth <= env_scope_depth
781 });
782 }
783
784 pub(crate) fn release_sync_guards_for_frame(&mut self, frame_depth: usize) {
785 self.held_sync_guards
786 .retain(|guard| guard.frame_depth != frame_depth);
787 }
788}
789
790impl Drop for Vm {
791 fn drop(&mut self) {
792 self.cancel_spawned_tasks();
793 }
794}
795
796impl Default for Vm {
797 fn default() -> Self {
798 Self::new()
799 }
800}
801
802#[cfg(test)]
803mod tests {
804 use std::rc::Rc;
805
806 use super::*;
807
808 fn baseline_with_stdlib(source: &str) -> VmBaseline {
809 let mut vm = Vm::new();
810 crate::register_vm_stdlib(&mut vm);
811 vm.set_source_info("baseline_test.harn", source);
812 vm.set_global("stable_global", VmValue::String(Rc::from("baseline")));
813 vm.baseline()
814 }
815
816 #[test]
817 fn vm_baseline_instantiates_clean_mutable_execution_state() {
818 let baseline = baseline_with_stdlib("pipeline main() { println(stable_global) }");
819
820 let mut dirty = baseline.instantiate();
821 dirty.stack.push(VmValue::Int(42));
822 dirty.output.push_str("dirty");
823 dirty.task_counter = 9;
824 dirty.runtime_context_counter = 7;
825 dirty
826 .error_stack_trace
827 .push(("main".to_string(), 1, 1, None));
828
829 let clean = baseline.instantiate();
830 assert!(clean.stack.is_empty());
831 assert!(clean.output.is_empty());
832 assert!(clean.frames.is_empty());
833 assert!(clean.exception_handlers.is_empty());
834 assert!(clean.spawned_tasks.is_empty());
835 assert!(clean.held_sync_guards.is_empty());
836 assert_eq!(clean.task_counter, 0);
837 assert_eq!(clean.runtime_context_counter, 0);
838 assert!(clean.deadlines.is_empty());
839 assert!(clean.cancel_token.is_none());
840 assert!(clean.interrupt_handlers.is_empty());
841 assert!(clean.error_stack_trace.is_empty());
842 assert!(clean.bridge.is_none());
843 assert!(clean
844 .globals
845 .get("stable_global")
846 .is_some_and(|value| value.display() == "baseline"));
847 }
848
849 #[tokio::test(flavor = "current_thread")]
850 async fn vm_baseline_rebinds_shared_state_builtins_per_instance() {
851 let local = tokio::task::LocalSet::new();
852 local
853 .run_until(async {
854 let source = r#"
855pipeline main() {
856 let cell = shared_cell({scope: "task_group", key: "turn", initial: 0})
857 println(shared_get(cell))
858 shared_set(cell, shared_get(cell) + 1)
859}"#;
860 let chunk = crate::compile_source(source).expect("compile");
861 let baseline = baseline_with_stdlib(source);
862
863 let mut first = baseline.instantiate();
864 first.execute(&chunk).await.expect("first execute");
865 assert_eq!(first.output(), "0\n");
866
867 let mut second = baseline.instantiate();
868 second.execute(&chunk).await.expect("second execute");
869 assert_eq!(
870 second.output(),
871 "0\n",
872 "shared state created by the first VM must not leak into the next baseline instance"
873 );
874 })
875 .await;
876 }
877}