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
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
38impl Drop for LocalSlot {
39 fn drop(&mut self) {
40 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
62pub(crate) struct CallFrame {
64 pub(crate) chunk: ChunkRef,
65 pub(crate) inline_cache_set: usize,
69 pub(crate) ip: usize,
70 pub(crate) stack_base: usize,
71 pub(crate) saved_env: VmEnv,
72 pub(crate) initial_env: Option<VmEnv>,
80 pub(crate) initial_local_slots: Option<Vec<LocalSlot>>,
81 pub(crate) saved_iterator_depth: usize,
83 pub(crate) fn_name: String,
85 pub(crate) argc: usize,
87 pub(crate) saved_source_dir: Option<std::path::PathBuf>,
90 pub(crate) module_functions: Option<ModuleFunctionRegistry>,
92 pub(crate) module_state: Option<crate::value::ModuleState>,
98 pub(crate) local_slots: Vec<LocalSlot>,
100 pub(crate) local_scope_base: usize,
102 pub(crate) local_scope_depth: usize,
104}
105
106pub(crate) struct InlineCacheSite {
107 pub(crate) cache_set: usize,
108 pub(crate) slot_count: usize,
109 pub(crate) slot: Option<usize>,
110}
111
112impl CallFrame {
113 #[inline]
114 pub(crate) fn inline_cache_site_for_previous_op(&self) -> InlineCacheSite {
115 let op_offset = self.ip.saturating_sub(1);
116 InlineCacheSite {
117 cache_set: self.inline_cache_set,
118 slot_count: self.chunk.inline_cache_slot_count(),
119 slot: self.chunk.inline_cache_slot(op_offset),
120 }
121 }
122}
123
124pub(crate) struct ExceptionHandler {
126 pub(crate) catch_ip: usize,
127 pub(crate) stack_depth: usize,
128 pub(crate) frame_depth: usize,
129 pub(crate) env_scope_depth: usize,
130 pub(crate) error_type: Option<crate::value::HarnStr>,
132}
133
134pub(crate) struct TaskScope {
137 pub(crate) task_ids: Vec<String>,
140 pub(crate) frame_depth: usize,
142 pub(crate) env_scope_depth: usize,
144}
145
146pub(crate) enum IterState {
148 Vec {
149 items: Arc<Vec<VmValue>>,
150 idx: usize,
151 },
152 Dict {
153 entries: Arc<crate::value::DictMap>,
154 keys: Vec<String>,
155 idx: usize,
156 },
157 Channel {
158 receiver: std::sync::Arc<tokio::sync::Mutex<tokio::sync::mpsc::Receiver<VmValue>>>,
159 close: std::sync::Arc<crate::value::VmChannelCloseState>,
160 },
161 Generator {
162 gen: Arc<crate::value::VmGenerator>,
163 },
164 Stream {
165 stream: Arc<crate::value::VmStream>,
166 },
167 Range {
171 next: i64,
172 end: i64,
173 inclusive: bool,
174 done: bool,
175 },
176 VmIter {
177 handle: crate::vm::iter::VmIterHandle,
178 },
179}
180
181#[derive(Clone)]
182pub(crate) enum VmBuiltinDispatch {
183 Sync(VmBuiltinFn),
184 Async(VmAsyncBuiltinFn),
185}
186
187#[derive(Clone)]
188pub(crate) struct VmBuiltinEntry {
189 pub(crate) name: Arc<str>,
190 pub(crate) dispatch: VmBuiltinDispatch,
191}
192
193pub struct Vm {
195 pub(crate) stack: Vec<VmValue>,
196 pub(crate) env: VmEnv,
197 pub(crate) output: String,
198 pub(crate) builtins: Arc<BTreeMap<String, VmBuiltinFn>>,
199 pub(crate) async_builtins: Arc<BTreeMap<String, VmAsyncBuiltinFn>>,
200 pub(crate) builtin_metadata: Arc<BTreeMap<String, VmBuiltinMetadata>>,
201 pub(crate) builtins_by_id: Arc<HashMap<BuiltinId, VmBuiltinEntry>>,
204 pub(crate) builtin_id_collisions: Arc<HashSet<BuiltinId>>,
207 pub(crate) iterators: Vec<IterState>,
209 pub(crate) frames: Vec<CallFrame>,
211 pub(crate) exception_handlers: Vec<ExceptionHandler>,
213 pub(crate) spawned_tasks: BTreeMap<String, VmTaskHandle>,
215 pub(crate) sync_runtime: Arc<crate::synchronization::VmSyncRuntime>,
217 pub(crate) shared_state_runtime: Arc<crate::shared_state::VmSharedStateRuntime>,
219 pub(crate) inline_cache_sets: Vec<Vec<crate::chunk::InlineCacheEntry>>,
223 pub(crate) inline_cache_set_by_chunk: HashMap<u64, usize>,
224 pub(crate) pool_registry: Arc<crate::stdlib::pool::PoolRegistry>,
226 pub(crate) wait_for_graph: Arc<crate::wait_for_graph::VmWaitForGraph>,
228 pub(crate) held_sync_guards: Vec<crate::synchronization::VmSyncHeldGuard>,
230 pub(crate) inherited_held_keys: Arc<Vec<crate::synchronization::VmSyncHeldKey>>,
238 pub(crate) task_scopes: Vec<TaskScope>,
244 pub(crate) task_counter: u64,
246 pub(crate) runtime_context_counter: u64,
248 pub(crate) runtime_context: crate::runtime_context::RuntimeContext,
250 pub(crate) deadlines: Vec<(Instant, usize)>,
252 pub(crate) breakpoints: BTreeMap<String, std::collections::BTreeSet<usize>>,
257 pub(crate) function_breakpoints: std::collections::BTreeSet<String>,
263 pub(crate) pending_function_bp: Option<String>,
268 pub(crate) step_mode: bool,
270 pub(crate) step_frame_depth: usize,
272 pub(crate) stopped: bool,
274 pub(crate) last_line: usize,
276 pub(crate) source_dir: Option<std::path::PathBuf>,
278 pub(crate) imported_paths: Vec<std::path::PathBuf>,
280 pub(crate) deferred_cyclic_imports: Vec<super::modules::DeferredCyclicImport>,
284 pub(crate) module_cache: Arc<BTreeMap<std::path::PathBuf, LoadedModule>>,
286 pub(crate) source_cache: Arc<BTreeMap<std::path::PathBuf, String>>,
288 pub(crate) source_file: Option<String>,
290 pub(crate) source_text: Option<String>,
292 pub(crate) coverage: Option<crate::coverage::Coverage>,
295 pub(crate) bridge: Option<Arc<crate::bridge::HostBridge>>,
297 pub(crate) denied_builtins: Arc<HashSet<String>>,
299 pub(crate) cancel_token: Option<std::sync::Arc<std::sync::atomic::AtomicBool>>,
301 pub(crate) interrupt_signal_token: Option<std::sync::Arc<std::sync::Mutex<Option<String>>>>,
302 pub(crate) cancel_grace_instructions_remaining: Option<usize>,
307 pub(crate) interrupt_handlers: Vec<InterruptHandler>,
309 pub(crate) next_interrupt_handle: i64,
310 pub(crate) pending_interrupt_signal: Option<String>,
311 pub(crate) interrupted: bool,
312 pub(crate) dispatching_interrupt: bool,
313 pub(crate) interrupt_handler_deadline: Option<Instant>,
314 pub(crate) error_stack_trace: Vec<(String, usize, usize, Option<String>)>,
316 pub(crate) yield_sender: Option<tokio::sync::mpsc::Sender<Result<VmValue, VmError>>>,
319 pub(crate) project_root: Option<std::path::PathBuf>,
322 pub(crate) globals: Arc<crate::value::DictMap>,
325 pub(crate) debug_hook: Option<parking_lot::Mutex<Box<DebugHook>>>,
327 pub(crate) runtime_limits: RuntimeLimits,
329}
330
331#[derive(Clone)]
339pub struct VmBaseline {
340 builtins: Arc<BTreeMap<String, VmBuiltinFn>>,
341 async_builtins: Arc<BTreeMap<String, VmAsyncBuiltinFn>>,
342 builtin_metadata: Arc<BTreeMap<String, VmBuiltinMetadata>>,
343 builtins_by_id: Arc<HashMap<BuiltinId, VmBuiltinEntry>>,
344 builtin_id_collisions: Arc<HashSet<BuiltinId>>,
345 source_dir: Option<std::path::PathBuf>,
346 source_file: Option<String>,
347 source_text: Option<String>,
348 project_root: Option<std::path::PathBuf>,
349 globals: Arc<crate::value::DictMap>,
350 denied_builtins: Arc<HashSet<String>>,
351 runtime_limits: RuntimeLimits,
352}
353
354impl VmBaseline {
355 pub fn from_vm(vm: &Vm) -> Self {
356 Self {
357 builtins: Arc::clone(&vm.builtins),
358 async_builtins: Arc::clone(&vm.async_builtins),
359 builtin_metadata: Arc::clone(&vm.builtin_metadata),
360 builtins_by_id: Arc::clone(&vm.builtins_by_id),
361 builtin_id_collisions: Arc::clone(&vm.builtin_id_collisions),
362 source_dir: vm.source_dir.clone(),
363 source_file: vm.source_file.clone(),
364 source_text: vm.source_text.clone(),
365 project_root: vm.project_root.clone(),
366 globals: Arc::clone(&vm.globals),
367 denied_builtins: Arc::clone(&vm.denied_builtins),
368 runtime_limits: vm.runtime_limits,
369 }
370 }
371
372 pub fn instantiate(&self) -> Vm {
373 let mut source_cache = BTreeMap::new();
374 if let (Some(file), Some(text)) = (&self.source_file, &self.source_text) {
375 source_cache.insert(std::path::PathBuf::from(file), text.clone());
376 }
377 if let Some(dir) = &self.source_dir {
378 crate::stdlib::set_thread_source_dir(dir);
379 }
380
381 let mut vm = Vm {
382 stack: Vec::with_capacity(256),
383 env: VmEnv::new(),
384 output: String::new(),
385 builtins: Arc::clone(&self.builtins),
386 async_builtins: Arc::clone(&self.async_builtins),
387 builtin_metadata: Arc::clone(&self.builtin_metadata),
388 builtins_by_id: Arc::clone(&self.builtins_by_id),
389 builtin_id_collisions: Arc::clone(&self.builtin_id_collisions),
390 iterators: Vec::new(),
391 frames: Vec::new(),
392 exception_handlers: Vec::new(),
393 spawned_tasks: BTreeMap::new(),
394 sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
395 shared_state_runtime: Arc::new(crate::shared_state::VmSharedStateRuntime::new()),
396 inline_cache_sets: Vec::new(),
397 inline_cache_set_by_chunk: HashMap::new(),
398 pool_registry: crate::stdlib::pool::new_pool_registry(),
399 wait_for_graph: Arc::new(crate::wait_for_graph::VmWaitForGraph::new()),
400 held_sync_guards: Vec::new(),
401 inherited_held_keys: Arc::new(Vec::new()),
402 task_scopes: Vec::new(),
403 task_counter: 0,
404 runtime_context_counter: 0,
405 runtime_context: crate::runtime_context::RuntimeContext::root(),
406 deadlines: Vec::new(),
407 breakpoints: BTreeMap::new(),
408 function_breakpoints: std::collections::BTreeSet::new(),
409 pending_function_bp: None,
410 step_mode: false,
411 step_frame_depth: 0,
412 stopped: false,
413 last_line: 0,
414 source_dir: self.source_dir.clone(),
415 imported_paths: Vec::new(),
416 deferred_cyclic_imports: Vec::new(),
417 module_cache: Arc::new(BTreeMap::new()),
418 source_cache: Arc::new(source_cache),
419 source_file: self.source_file.clone(),
420 source_text: self.source_text.clone(),
421 coverage: crate::coverage::for_primary(self.source_file.as_deref()),
422 bridge: None,
423 denied_builtins: Arc::clone(&self.denied_builtins),
424 cancel_token: None,
425 interrupt_signal_token: None,
426 cancel_grace_instructions_remaining: None,
427 interrupt_handlers: Vec::new(),
428 next_interrupt_handle: 1,
429 pending_interrupt_signal: None,
430 interrupted: false,
431 dispatching_interrupt: false,
432 interrupt_handler_deadline: None,
433 error_stack_trace: Vec::new(),
434 yield_sender: None,
435 project_root: self.project_root.clone(),
436 globals: Arc::clone(&self.globals),
437 debug_hook: None,
438 runtime_limits: self.runtime_limits,
439 };
440
441 crate::stdlib::rebind_execution_state_builtins(&mut vm);
442 vm
443 }
444}
445
446impl Vm {
447 pub(crate) fn fresh_local_slots(chunk: &Chunk) -> Vec<LocalSlot> {
448 chunk
449 .local_slots
450 .iter()
451 .map(|_| LocalSlot {
452 value: VmValue::Nil,
453 initialized: false,
454 synced: false,
455 })
456 .collect()
457 }
458
459 pub(crate) fn bind_param_slots(
460 slots: &mut [LocalSlot],
461 func: &crate::chunk::CompiledFunction,
462 args: &[VmValue],
463 synced: bool,
464 ) {
465 Self::bind_param_slots_args(slots, func, &super::CallArgs::Slice(args), synced);
466 }
467
468 pub(crate) fn bind_param_slots_args(
469 slots: &mut [LocalSlot],
470 func: &crate::chunk::CompiledFunction,
471 args: &super::CallArgs<'_>,
472 synced: bool,
473 ) {
474 let param_count = func.params.len();
475 for (i, _param) in func.params.iter().enumerate() {
476 if i >= slots.len() {
477 break;
478 }
479 if func.has_rest_param && i == param_count - 1 {
480 let rest_args = args.to_vec_from(i);
481 slots[i].value = VmValue::List(std::sync::Arc::new(rest_args));
482 slots[i].initialized = true;
483 slots[i].synced = synced;
484 } else if let Some(arg) = args.get(i) {
485 slots[i].value = arg.clone();
486 slots[i].initialized = true;
487 slots[i].synced = synced;
488 }
489 }
490 }
491
492 pub(crate) fn visible_variables(&self) -> crate::value::DictMap {
493 let mut vars = self.env.all_variables();
494 let Some(frame) = self.frames.last() else {
495 return vars;
496 };
497 for (slot, info) in frame.local_slots.iter().zip(frame.chunk.local_slots.iter()) {
498 if slot.initialized && info.scope_depth <= frame.local_scope_depth {
499 vars.insert(crate::value::intern_key(&info.name), slot.value.clone());
500 }
501 }
502 vars
503 }
504
505 pub(crate) fn sync_current_frame_locals_to_env(&mut self) {
506 let frames = &mut self.frames;
507 let env = &mut self.env;
508 let Some(frame) = frames.last_mut() else {
509 return;
510 };
511 let local_scope_base = frame.local_scope_base;
512 let local_scope_depth = frame.local_scope_depth;
513 for (slot, info) in frame
514 .local_slots
515 .iter_mut()
516 .zip(frame.chunk.local_slots.iter())
517 {
518 if slot.initialized && !slot.synced && info.scope_depth <= local_scope_depth {
519 slot.synced = true;
520 let scope_idx = local_scope_base + info.scope_depth;
521 while env.scopes.len() <= scope_idx {
522 env.push_scope();
523 }
524 Arc::make_mut(&mut env.scopes[scope_idx].vars)
525 .insert(info.name.clone(), (slot.value.clone(), info.mutable));
526 }
527 }
528 }
529
530 pub(crate) fn closure_call_env_for_current_frame(
531 &self,
532 closure: &crate::value::VmClosure,
533 ) -> VmEnv {
534 if closure.module_state().is_some() {
535 return closure.env.cloned_for_call();
536 }
537 let call_env = Self::closure_call_env(&self.env, closure);
538 if !closure.func.chunk.references_outer_names {
543 return call_env;
544 }
545 let mut call_env = call_env;
546 let Some(frame) = self.frames.last() else {
547 return call_env;
548 };
549 for (slot, info) in frame
550 .local_slots
551 .iter()
552 .zip(frame.chunk.local_slots.iter())
553 .filter(|(slot, info)| slot.initialized && info.scope_depth <= frame.local_scope_depth)
554 {
555 if matches!(slot.value, VmValue::Closure(_)) && !call_env.contains(&info.name) {
556 let _ = call_env.define(&info.name, slot.value.clone(), info.mutable);
557 }
558 }
559 call_env
560 }
561
562 pub(crate) fn active_local_slot_value(&self, name: &str) -> Option<VmValue> {
563 let frame = self.frames.last()?;
564 let idx = self.active_local_slot_index(name)?;
565 frame.local_slots.get(idx).map(|slot| slot.value.clone())
566 }
567
568 pub(crate) fn active_local_slot_index(&self, name: &str) -> Option<usize> {
573 let frame = self.frames.last()?;
574 for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
575 if info.name == name && info.scope_depth <= frame.local_scope_depth {
576 if let Some(slot) = frame.local_slots.get(idx) {
577 if slot.initialized {
578 return Some(idx);
579 }
580 }
581 }
582 }
583 None
584 }
585
586 pub(crate) fn assign_active_local_slot(
587 &mut self,
588 name: &str,
589 value: VmValue,
590 debug: bool,
591 ) -> Result<bool, VmError> {
592 let Some(frame) = self.frames.last_mut() else {
593 return Ok(false);
594 };
595 for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
596 if info.name == name && info.scope_depth <= frame.local_scope_depth {
597 if !debug && !info.mutable {
598 return Err(VmError::ImmutableAssignment(name.to_string()));
599 }
600 if let Some(slot) = frame.local_slots.get_mut(idx) {
601 crate::value::recursion::dismantle(std::mem::replace(&mut slot.value, value));
602 slot.initialized = true;
603 slot.synced = false;
604 return Ok(true);
605 }
606 }
607 }
608 Ok(false)
609 }
610
611 pub fn new() -> Self {
612 Self {
613 stack: Vec::with_capacity(256),
614 env: VmEnv::new(),
615 output: String::new(),
616 builtins: Arc::new(BTreeMap::new()),
617 async_builtins: Arc::new(BTreeMap::new()),
618 builtin_metadata: Arc::new(BTreeMap::new()),
619 builtins_by_id: Arc::new(HashMap::new()),
620 builtin_id_collisions: Arc::new(HashSet::new()),
621 iterators: Vec::new(),
622 frames: Vec::new(),
623 exception_handlers: Vec::new(),
624 spawned_tasks: BTreeMap::new(),
625 sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
626 shared_state_runtime: Arc::new(crate::shared_state::VmSharedStateRuntime::new()),
627 inline_cache_sets: Vec::new(),
628 inline_cache_set_by_chunk: HashMap::new(),
629 pool_registry: crate::stdlib::pool::new_pool_registry(),
630 wait_for_graph: Arc::new(crate::wait_for_graph::VmWaitForGraph::new()),
631 held_sync_guards: Vec::new(),
632 inherited_held_keys: Arc::new(Vec::new()),
633 task_scopes: Vec::new(),
634 task_counter: 0,
635 runtime_context_counter: 0,
636 runtime_context: crate::runtime_context::RuntimeContext::root(),
637 deadlines: Vec::new(),
638 breakpoints: BTreeMap::new(),
639 function_breakpoints: std::collections::BTreeSet::new(),
640 pending_function_bp: None,
641 step_mode: false,
642 step_frame_depth: 0,
643 stopped: false,
644 last_line: 0,
645 source_dir: None,
646 imported_paths: Vec::new(),
647 deferred_cyclic_imports: Vec::new(),
648 module_cache: Arc::new(BTreeMap::new()),
649 source_cache: Arc::new(BTreeMap::new()),
650 source_file: None,
651 source_text: None,
652 coverage: crate::coverage::for_primary(None),
653 bridge: None,
654 denied_builtins: Arc::new(HashSet::new()),
655 cancel_token: None,
656 interrupt_signal_token: None,
657 cancel_grace_instructions_remaining: None,
658 interrupt_handlers: Vec::new(),
659 next_interrupt_handle: 1,
660 pending_interrupt_signal: None,
661 interrupted: false,
662 dispatching_interrupt: false,
663 interrupt_handler_deadline: None,
664 error_stack_trace: Vec::new(),
665 yield_sender: None,
666 project_root: None,
667 globals: Arc::new(crate::value::DictMap::new()),
668 debug_hook: None,
669 runtime_limits: RuntimeLimits::default(),
670 }
671 }
672
673 pub fn baseline(&self) -> VmBaseline {
674 VmBaseline::from_vm(self)
675 }
676
677 pub fn runtime_limits(&self) -> RuntimeLimits {
679 self.runtime_limits
680 }
681
682 pub fn runtime_limit_report(&self) -> crate::RuntimeLimitsReport {
684 self.runtime_limits.report()
685 }
686
687 #[inline]
701 pub(crate) fn debugger_attached(&self) -> bool {
702 self.debug_hook.is_some()
703 || !self.breakpoints.is_empty()
704 || !self.function_breakpoints.is_empty()
705 }
706
707 pub fn set_bridge(&mut self, bridge: Arc<crate::bridge::HostBridge>) {
709 self.bridge = Some(bridge);
710 }
711
712 pub fn set_denied_builtins(&mut self, denied: HashSet<String>) {
715 self.denied_builtins = Arc::new(denied);
716 }
717
718 pub fn set_source_info(&mut self, file: &str, text: &str) {
720 self.source_file = Some(file.to_string());
721 self.source_text = Some(text.to_string());
722 if let Some(cov) = self.coverage.as_mut() {
723 cov.set_primary_file(file);
724 }
725 Arc::make_mut(&mut self.source_cache)
726 .insert(std::path::PathBuf::from(file), text.to_string());
727 }
728
729 pub fn start(&mut self, chunk: &Chunk) {
731 let debugger = self.debugger_attached();
738 let initial_env = if debugger {
739 Some(self.env.clone())
740 } else {
741 None
742 };
743 let initial_local_slots = if debugger {
744 Some(Self::fresh_local_slots(chunk))
745 } else {
746 None
747 };
748 let chunk = Arc::new(chunk.clone());
749 let local_slots = Self::fresh_local_slots(&chunk);
750 let inline_cache_set = self.inline_cache_set_index_for_chunk(&chunk);
751 self.frames.push(CallFrame {
752 chunk,
753 inline_cache_set,
754 ip: 0,
755 stack_base: self.stack.len(),
756 saved_env: self.env.clone(),
757 initial_env,
758 initial_local_slots,
759 saved_iterator_depth: self.iterators.len(),
760 fn_name: String::new(),
761 argc: 0,
762 saved_source_dir: None,
763 module_functions: None,
764 module_state: None,
765 local_slots,
766 local_scope_base: self.env.scope_depth().saturating_sub(1),
767 local_scope_depth: 0,
768 });
769 }
770
771 pub(crate) fn child_vm(&self) -> Vm {
774 Vm {
775 stack: Vec::with_capacity(64),
776 env: self.env.clone(),
777 output: String::new(),
778 builtins: Arc::clone(&self.builtins),
779 async_builtins: Arc::clone(&self.async_builtins),
780 builtin_metadata: Arc::clone(&self.builtin_metadata),
781 builtins_by_id: Arc::clone(&self.builtins_by_id),
782 builtin_id_collisions: Arc::clone(&self.builtin_id_collisions),
783 iterators: Vec::new(),
784 frames: Vec::new(),
785 exception_handlers: Vec::new(),
786 spawned_tasks: BTreeMap::new(),
787 sync_runtime: self.sync_runtime.clone(),
788 shared_state_runtime: self.shared_state_runtime.clone(),
789 inline_cache_sets: Vec::new(),
790 inline_cache_set_by_chunk: HashMap::new(),
791 pool_registry: self.pool_registry.clone(),
792 wait_for_graph: self.wait_for_graph.clone(),
793 held_sync_guards: Vec::new(),
794 inherited_held_keys: Arc::new(Vec::new()),
795 task_scopes: Vec::new(),
796 task_counter: 0,
797 runtime_context_counter: self.runtime_context_counter,
798 runtime_context: self.runtime_context.clone(),
799 deadlines: self.deadlines.clone(),
800 breakpoints: BTreeMap::new(),
801 function_breakpoints: std::collections::BTreeSet::new(),
802 pending_function_bp: None,
803 step_mode: false,
804 step_frame_depth: 0,
805 stopped: false,
806 last_line: 0,
807 source_dir: self.source_dir.clone(),
808 imported_paths: Vec::new(),
809 deferred_cyclic_imports: Vec::new(),
810 module_cache: Arc::clone(&self.module_cache),
811 source_cache: Arc::clone(&self.source_cache),
812 source_file: self.source_file.clone(),
813 source_text: self.source_text.clone(),
814 coverage: crate::coverage::for_primary(self.source_file.as_deref()),
815 bridge: self.bridge.clone(),
816 denied_builtins: Arc::clone(&self.denied_builtins),
817 cancel_token: self.cancel_token.clone(),
818 interrupt_signal_token: self.interrupt_signal_token.clone(),
819 cancel_grace_instructions_remaining: None,
820 interrupt_handlers: Vec::new(),
821 next_interrupt_handle: 1,
822 pending_interrupt_signal: None,
823 interrupted: self.interrupted,
824 dispatching_interrupt: false,
825 interrupt_handler_deadline: None,
826 error_stack_trace: Vec::new(),
827 yield_sender: None,
828 project_root: self.project_root.clone(),
829 globals: Arc::clone(&self.globals),
830 debug_hook: None,
831 runtime_limits: self.runtime_limits,
832 }
833 }
834
835 pub(crate) fn child_vm_for_host(&self) -> Vm {
838 self.child_vm()
839 }
840
841 pub(crate) fn cancel_spawned_tasks(&mut self) {
845 for (_, task) in std::mem::take(&mut self.spawned_tasks) {
846 task.cancel_token
847 .store(true, std::sync::atomic::Ordering::SeqCst);
848 task.handle.abort();
849 }
850 }
851
852 pub fn set_source_dir(&mut self, dir: &std::path::Path) {
855 let dir = crate::stdlib::process::normalize_context_path(dir);
856 self.source_dir = Some(dir.clone());
857 crate::stdlib::set_thread_source_dir(&dir);
858 if self.project_root.is_none() {
860 self.project_root = crate::stdlib::process::find_project_root(&dir);
861 }
862 }
863
864 pub fn set_project_root(&mut self, root: &std::path::Path) {
867 self.project_root = Some(root.to_path_buf());
868 }
869
870 pub fn project_root(&self) -> Option<&std::path::Path> {
872 self.project_root.as_deref().or(self.source_dir.as_deref())
873 }
874
875 pub fn builtin_names(&self) -> Vec<String> {
877 let mut names: Vec<String> = self.builtins.keys().cloned().collect();
878 names.extend(self.async_builtins.keys().cloned());
879 names
880 }
881
882 pub fn builtin_metadata(&self) -> Vec<VmBuiltinMetadata> {
884 self.builtin_metadata.values().cloned().collect()
885 }
886
887 pub fn builtin_metadata_for(&self, name: &str) -> Option<&VmBuiltinMetadata> {
889 self.builtin_metadata.get(name)
890 }
891
892 pub fn set_global(&mut self, name: &str, value: VmValue) {
895 Arc::make_mut(&mut self.globals).insert(crate::value::intern_key(name), value);
896 }
897
898 pub fn global(&self, name: &str) -> Option<&VmValue> {
904 self.globals.get(name)
905 }
906
907 pub fn set_harness(&mut self, harness: crate::harness::Harness) {
913 self.set_global("harness", harness.into_vm_value());
914 }
915
916 pub fn output(&self) -> &str {
918 &self.output
919 }
920
921 pub fn take_output(&mut self) -> String {
925 std::mem::take(&mut self.output)
926 }
927
928 pub fn append_output(&mut self, text: &str) {
932 self.output.push_str(text);
933 }
934
935 pub(crate) fn pop(&mut self) -> Result<VmValue, VmError> {
936 self.stack.pop().ok_or(VmError::StackUnderflow)
937 }
938
939 pub(crate) fn peek(&self) -> Result<&VmValue, VmError> {
940 self.stack.last().ok_or(VmError::StackUnderflow)
941 }
942
943 pub(crate) fn const_str(c: &Constant) -> Result<&str, VmError> {
944 match c {
945 Constant::String(s) => Ok(s.as_str()),
946 _ => Err(VmError::TypeError("expected string constant".into())),
947 }
948 }
949
950 pub(crate) fn release_sync_guards_for_current_scope(&mut self) {
951 let depth = self.env.scope_depth();
952 self.held_sync_guards
953 .retain(|guard| guard.env_scope_depth < depth);
954 self.cancel_task_scopes_where(|s| s.env_scope_depth >= depth);
957 }
958
959 pub(crate) fn release_sync_guards_after_unwind(
960 &mut self,
961 frame_depth: usize,
962 env_scope_depth: usize,
963 ) {
964 self.held_sync_guards.retain(|guard| {
965 guard.frame_depth <= frame_depth && guard.env_scope_depth <= env_scope_depth
966 });
967 self.cancel_task_scopes_where(|s| {
970 !(s.frame_depth <= frame_depth && s.env_scope_depth <= env_scope_depth)
971 });
972 }
973
974 pub(crate) fn release_sync_guards_for_frame(&mut self, frame_depth: usize) {
975 self.held_sync_guards
976 .retain(|guard| guard.frame_depth != frame_depth);
977 self.cancel_task_scopes_where(|s| s.frame_depth == frame_depth);
980 }
981
982 pub(crate) fn adopt_sync_permit_for_current_scope(
983 &mut self,
984 permit: crate::value::VmSyncPermitHandle,
985 ) {
986 if permit.is_released()
987 || self
988 .held_sync_guards
989 .iter()
990 .any(|guard| guard._permit.same_lease(&permit))
991 {
992 return;
993 }
994 self.held_sync_guards
995 .push(crate::synchronization::VmSyncHeldGuard {
996 _permit: permit,
997 frame_depth: self.frames.len(),
998 env_scope_depth: self.env.scope_depth(),
999 });
1000 }
1001
1002 pub(crate) fn deregister_task_from_scopes(&mut self, id: &str) {
1005 for scope in &mut self.task_scopes {
1006 scope.task_ids.retain(|t| t != id);
1007 }
1008 }
1009
1010 fn cancel_task_scopes_where<F: Fn(&TaskScope) -> bool>(&mut self, doomed: F) {
1013 let mut i = 0;
1014 while i < self.task_scopes.len() {
1015 if doomed(&self.task_scopes[i]) {
1016 let scope = self.task_scopes.remove(i);
1017 for id in &scope.task_ids {
1018 if let Some(task) = self.spawned_tasks.remove(id) {
1019 task.cancel_token
1020 .store(true, std::sync::atomic::Ordering::SeqCst);
1021 task.handle.abort();
1022 }
1023 }
1024 } else {
1025 i += 1;
1026 }
1027 }
1028 }
1029
1030 pub(crate) fn held_permits_for(&self, kind: &str, key: &str) -> u32 {
1034 let own: u32 = self
1035 .held_sync_guards
1036 .iter()
1037 .filter(|guard| {
1038 !guard._permit.is_released()
1039 && guard._permit.kind() == kind
1040 && guard._permit.key() == key
1041 })
1042 .map(|guard| guard._permit.permits())
1043 .sum();
1044 let inherited: u32 = self
1045 .inherited_held_keys
1046 .iter()
1047 .filter(|held| held.kind == kind && held.key == key)
1048 .map(|held| held.permits)
1049 .sum();
1050 own + inherited
1051 }
1052
1053 pub(crate) fn combined_held_keys(&self) -> Vec<crate::synchronization::VmSyncHeldKey> {
1056 let mut keys: Vec<crate::synchronization::VmSyncHeldKey> = self
1057 .held_sync_guards
1058 .iter()
1059 .filter_map(|guard| crate::synchronization::VmSyncHeldKey::from_permit(&guard._permit))
1060 .collect();
1061 keys.extend(self.inherited_held_keys.iter().cloned());
1062 keys
1063 }
1064
1065 pub(crate) fn child_vm_inline(&self) -> Vm {
1071 let mut child = self.child_vm();
1072 child.inherited_held_keys = Arc::new(self.combined_held_keys());
1073 child
1074 }
1075}
1076
1077impl Drop for Vm {
1078 fn drop(&mut self) {
1079 if let Some(coverage) = self.coverage.take() {
1080 crate::coverage::merge_into_global(coverage);
1081 }
1082 self.cancel_spawned_tasks();
1083 }
1084}
1085
1086impl Default for Vm {
1087 fn default() -> Self {
1088 Self::new()
1089 }
1090}
1091
1092#[cfg(test)]
1093mod tests {
1094
1095 use super::*;
1096
1097 fn baseline_with_stdlib(source: &str) -> VmBaseline {
1098 let mut vm = Vm::new();
1099 crate::register_vm_stdlib(&mut vm);
1100 vm.set_source_info("baseline_test.harn", source);
1101 vm.set_global(
1102 "stable_global",
1103 VmValue::String(arcstr::ArcStr::from("baseline")),
1104 );
1105 vm.baseline()
1106 }
1107
1108 #[test]
1109 fn vm_baseline_instantiates_clean_mutable_execution_state() {
1110 let baseline = baseline_with_stdlib("pipeline main() { __io_println(stable_global) }");
1111
1112 let mut dirty = baseline.instantiate();
1113 dirty.stack.push(VmValue::Int(42));
1114 dirty.output.push_str("dirty");
1115 dirty.task_counter = 9;
1116 dirty.runtime_context_counter = 7;
1117 dirty
1118 .error_stack_trace
1119 .push(("main".to_string(), 1, 1, None));
1120
1121 let clean = baseline.instantiate();
1122 assert!(clean.stack.is_empty());
1123 assert!(clean.output.is_empty());
1124 assert!(clean.frames.is_empty());
1125 assert!(clean.exception_handlers.is_empty());
1126 assert!(clean.spawned_tasks.is_empty());
1127 assert!(clean.held_sync_guards.is_empty());
1128 assert_eq!(clean.task_counter, 0);
1129 assert_eq!(clean.runtime_context_counter, 0);
1130 assert!(clean.deadlines.is_empty());
1131 assert!(clean.cancel_token.is_none());
1132 assert!(clean.interrupt_handlers.is_empty());
1133 assert!(clean.error_stack_trace.is_empty());
1134 assert!(clean.bridge.is_none());
1135 assert!(clean
1136 .globals
1137 .get("stable_global")
1138 .is_some_and(|value| value.display() == "baseline"));
1139 }
1140
1141 #[tokio::test]
1142 async fn inline_child_inherits_held_lock_keys_but_concurrent_child_does_not() {
1143 let mut parent = Vm::new();
1144 let permit = parent
1145 .sync_runtime
1146 .acquire("mutex", "v:test", 1, 1, None, None)
1147 .await
1148 .unwrap()
1149 .unwrap();
1150 parent
1151 .held_sync_guards
1152 .push(crate::synchronization::VmSyncHeldGuard {
1153 _permit: permit,
1154 frame_depth: 0,
1155 env_scope_depth: 0,
1156 });
1157 assert_eq!(parent.held_permits_for("mutex", "v:test"), 1);
1158
1159 let inline = parent.child_vm_inline();
1164 assert_eq!(inline.held_permits_for("mutex", "v:test"), 1);
1165 assert_eq!(
1166 inline.child_vm_inline().held_permits_for("mutex", "v:test"),
1167 1
1168 );
1169
1170 let concurrent = parent.child_vm();
1174 assert_eq!(concurrent.held_permits_for("mutex", "v:test"), 0);
1175 }
1176
1177 #[test]
1178 fn vm_reports_effective_runtime_limits() {
1179 let vm = Vm::new();
1180
1181 assert_eq!(vm.runtime_limits(), RuntimeLimits::default());
1182 assert_eq!(
1183 vm.runtime_limit_report().entries.len(),
1184 crate::RUNTIME_LIMIT_DESCRIPTIONS.len()
1185 );
1186 assert_eq!(vm.child_vm().runtime_limits(), vm.runtime_limits());
1187 assert_eq!(
1188 vm.baseline().instantiate().runtime_limits(),
1189 vm.runtime_limits()
1190 );
1191 }
1192
1193 #[tokio::test(flavor = "current_thread")]
1194 async fn vm_baseline_rebinds_shared_state_builtins_per_instance() {
1195 let local = tokio::task::LocalSet::new();
1196 local
1197 .run_until(async {
1198 let source = r#"
1199pipeline main() {
1200 let cell = shared_cell({scope: "task_group", key: "turn", initial: 0})
1201 __io_println(shared_get(cell))
1202 shared_set(cell, shared_get(cell) + 1)
1203}"#;
1204 let chunk = crate::compile_source(source).expect("compile");
1205 let baseline = baseline_with_stdlib(source);
1206
1207 let mut first = baseline.instantiate();
1208 first.execute(&chunk).await.expect("first execute");
1209 assert_eq!(first.output(), "0\n");
1210
1211 let mut second = baseline.instantiate();
1212 second.execute(&chunk).await.expect("second execute");
1213 assert_eq!(
1214 second.output(),
1215 "0\n",
1216 "shared state created by the first VM must not leak into the next baseline instance"
1217 );
1218 })
1219 .await;
1220 }
1221}