1use std::collections::{BTreeMap, HashMap, HashSet};
2use std::path::PathBuf;
3use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
4use std::sync::{Arc, Mutex};
5use std::time::Instant;
6
7use crate::chunk::{Chunk, ChunkRef, Constant};
8use crate::runtime_limits::RuntimeLimits;
9use crate::value::{
10 ModuleFunctionRegistry, VmAsyncBuiltinFn, VmBuiltinFn, VmClosure, VmEnv, VmError, VmMutex,
11 VmTaskHandle, VmValue,
12};
13use crate::BuiltinId;
14
15use super::debug::DebugHook;
16use super::modules::ModuleCache;
17use super::VmBuiltinMetadata;
18
19pub(crate) struct ResolvedLazyCallable {
33 pub(crate) exports: BTreeMap<String, Arc<VmClosure>>,
34 #[allow(dead_code)]
39 pub(crate) retained_module_graph: ModuleCache,
40}
41
42pub(crate) type LazyCallableResolution = Arc<ResolvedLazyCallable>;
43pub(crate) struct LazyCallableCacheSlot {
44 pub(crate) execution_guard: Option<Arc<harn_modules::package_execution::PackageExecutionGuard>>,
45 pub(crate) resolution: Arc<tokio::sync::OnceCell<LazyCallableResolution>>,
46}
47pub(crate) type LazyCallableModuleCache =
48 Arc<VmMutex<BTreeMap<PathBuf, Vec<LazyCallableCacheSlot>>>>;
49
50pub(crate) struct ScopeSpan(u64);
52
53impl ScopeSpan {
54 pub(crate) fn new(kind: crate::tracing::SpanKind, name: String) -> Self {
55 Self(crate::tracing::span_start(kind, name))
56 }
57}
58
59pub(crate) struct ExecutionDeadlineState {
64 origin: Instant,
65 deadline_offset: AtomicU64,
67 abandoned: AtomicBool,
71}
72
73impl ExecutionDeadlineState {
74 pub(crate) fn new(origin: Instant, deadline: Option<Instant>) -> Arc<Self> {
75 Arc::new(Self {
76 origin,
77 deadline_offset: AtomicU64::new(Self::encode(origin, deadline)),
78 abandoned: AtomicBool::new(false),
79 })
80 }
81
82 #[inline]
83 pub(crate) fn is_active(&self) -> bool {
84 self.deadline_offset.load(Ordering::Acquire) != 0
85 }
86
87 #[inline]
88 pub(crate) fn is_abandoned(&self) -> bool {
89 self.abandoned.load(Ordering::Acquire)
90 }
91
92 pub(crate) fn fork(&self) -> Arc<Self> {
93 let state = Self::new(self.origin, self.current());
94 state
95 .abandoned
96 .store(self.is_abandoned(), Ordering::Release);
97 state
98 }
99
100 pub(crate) fn current(&self) -> Option<Instant> {
101 let encoded = self.deadline_offset.load(Ordering::Acquire);
102 (encoded != 0)
103 .then(|| self.origin + std::time::Duration::from_nanos(encoded.saturating_sub(1)))
104 }
105
106 pub(crate) fn install(self: &Arc<Self>, deadline: Instant) -> ExecutionDeadlineGuard {
107 let previous = self.deadline_offset.load(Ordering::Acquire);
108 let requested = Self::encode(self.origin, Some(deadline));
109 let active = if previous == 0 {
110 requested
111 } else {
112 previous.min(requested)
113 };
114 self.deadline_offset.store(active, Ordering::Release);
115 ExecutionDeadlineGuard {
116 state: Arc::clone(self),
117 previous,
118 completed: false,
119 }
120 }
121
122 fn encode(origin: Instant, deadline: Option<Instant>) -> u64 {
123 deadline.map_or(0, |deadline| {
124 let nanos = deadline.saturating_duration_since(origin).as_nanos();
125 u64::try_from(nanos)
126 .unwrap_or(u64::MAX - 1)
127 .saturating_add(1)
128 })
129 }
130}
131
132pub(crate) struct ExecutionDeadlineGuard {
133 state: Arc<ExecutionDeadlineState>,
134 previous: u64,
135 completed: bool,
136}
137
138impl ExecutionDeadlineGuard {
139 pub(crate) fn complete(mut self) {
142 self.completed = true;
143 }
144}
145
146impl Drop for ExecutionDeadlineGuard {
147 fn drop(&mut self) {
148 self.state
149 .deadline_offset
150 .store(self.previous, Ordering::Release);
151 if !self.completed {
152 self.state.abandoned.store(true, Ordering::Release);
153 }
154 }
155}
156
157impl Drop for ScopeSpan {
158 fn drop(&mut self) {
159 crate::tracing::span_end(self.0);
160 }
161}
162
163#[derive(Clone)]
164pub(crate) struct LocalSlot {
165 pub(crate) value: VmValue,
166 pub(crate) initialized: bool,
167 pub(crate) synced: bool,
168}
169
170impl Drop for LocalSlot {
171 fn drop(&mut self) {
172 if crate::value::recursion::is_recursive_container(&self.value) {
180 crate::value::recursion::dismantle(std::mem::replace(&mut self.value, VmValue::Nil));
181 }
182 }
183}
184
185#[derive(Clone)]
186pub(crate) struct InterruptHandler {
187 pub(crate) handle: i64,
188 pub(crate) signals: Vec<String>,
189 pub(crate) once: bool,
190 pub(crate) graceful_timeout_ms: Option<u64>,
191 pub(crate) handler: VmValue,
192}
193
194pub(crate) struct CallFrame {
196 pub(crate) chunk: ChunkRef,
197 pub(crate) inline_cache_set: usize,
201 pub(crate) ip: usize,
202 pub(crate) stack_base: usize,
203 pub(crate) saved_env: VmEnv,
204 pub(crate) initial_env: Option<VmEnv>,
212 pub(crate) initial_local_slots: Option<Vec<LocalSlot>>,
213 pub(crate) saved_iterator_depth: usize,
215 pub(crate) fn_name: String,
217 pub(crate) argc: usize,
219 pub(crate) saved_source_dir: Option<std::path::PathBuf>,
222 pub(crate) module_functions: Option<ModuleFunctionRegistry>,
224 pub(crate) module_state: Option<crate::value::ModuleState>,
230 pub(crate) local_slots: Vec<LocalSlot>,
232 pub(crate) local_scope_base: usize,
234 pub(crate) local_scope_depth: usize,
236}
237
238pub(crate) struct InlineCacheSite {
239 pub(crate) cache_set: usize,
240 pub(crate) slot_count: usize,
241 pub(crate) slot: Option<usize>,
242}
243
244impl CallFrame {
245 #[inline]
246 pub(crate) fn inline_cache_site_for_previous_op(&self) -> InlineCacheSite {
247 let op_offset = self.ip.saturating_sub(1);
248 InlineCacheSite {
249 cache_set: self.inline_cache_set,
250 slot_count: self.chunk.inline_cache_slot_count(),
251 slot: self.chunk.inline_cache_slot(op_offset),
252 }
253 }
254}
255
256pub(crate) struct ExceptionHandler {
258 pub(crate) catch_ip: usize,
259 pub(crate) stack_depth: usize,
260 pub(crate) frame_depth: usize,
261 pub(crate) env_scope_depth: usize,
262 pub(crate) error_type: Option<crate::value::HarnStr>,
264}
265
266pub(crate) struct TaskScope {
269 pub(crate) task_ids: Vec<String>,
272 pub(crate) frame_depth: usize,
274 pub(crate) env_scope_depth: usize,
276}
277
278pub(crate) struct ProcessExitRequest {
282 code: Mutex<Option<i32>>,
283 requested: AtomicBool,
284}
285
286impl ProcessExitRequest {
287 fn new() -> Self {
288 Self {
289 code: Mutex::new(None),
290 requested: AtomicBool::new(false),
291 }
292 }
293
294 fn request(&self, code: i32) {
295 let mut recorded = self
296 .code
297 .lock()
298 .expect("process exit request lock poisoned");
299 if recorded.is_none() {
300 *recorded = Some(code);
301 self.requested.store(true, Ordering::Release);
302 }
303 }
304
305 fn code(&self) -> Option<i32> {
306 if !self.requested.load(Ordering::Acquire) {
307 return None;
308 }
309 *self
310 .code
311 .lock()
312 .expect("process exit request lock poisoned")
313 }
314}
315
316pub(crate) enum IterState {
318 Vec {
319 items: Arc<Vec<VmValue>>,
320 idx: usize,
321 },
322 Dict {
323 entries: Arc<crate::value::DictMap>,
324 keys: Vec<String>,
325 idx: usize,
326 },
327 Channel {
328 receiver: std::sync::Arc<tokio::sync::Mutex<tokio::sync::mpsc::Receiver<VmValue>>>,
329 close: std::sync::Arc<crate::value::VmChannelCloseState>,
330 },
331 Generator {
332 gen: Arc<crate::value::VmGenerator>,
333 },
334 Stream {
335 stream: Arc<crate::value::VmStream>,
336 },
337 Range {
341 next: i64,
342 end: i64,
343 inclusive: bool,
344 done: bool,
345 },
346 VmIter {
347 handle: crate::vm::iter::VmIterHandle,
348 },
349}
350
351#[derive(Clone)]
352pub(crate) enum VmBuiltinDispatch {
353 Sync(VmBuiltinFn),
354 Async(VmAsyncBuiltinFn),
355}
356
357#[derive(Clone)]
358pub(crate) struct VmBuiltinEntry {
359 pub(crate) name: Arc<str>,
360 pub(crate) dispatch: VmBuiltinDispatch,
361}
362
363pub struct Vm {
365 pub(crate) stack: Vec<VmValue>,
366 pub(crate) env: VmEnv,
367 pub(crate) output: String,
368 pub(crate) builtins: Arc<BTreeMap<String, VmBuiltinFn>>,
369 pub(crate) async_builtins: Arc<BTreeMap<String, VmAsyncBuiltinFn>>,
370 pub(crate) builtin_metadata: Arc<BTreeMap<String, VmBuiltinMetadata>>,
371 pub(crate) builtins_by_id: Arc<HashMap<BuiltinId, VmBuiltinEntry>>,
374 pub(crate) builtin_id_collisions: Arc<HashSet<BuiltinId>>,
377 pub(crate) iterators: Vec<IterState>,
379 pub(crate) frames: Vec<CallFrame>,
381 pub(crate) exception_handlers: Vec<ExceptionHandler>,
383 pub(crate) spawned_tasks: BTreeMap<String, VmTaskHandle>,
385 pub(crate) process_exit_request: Arc<ProcessExitRequest>,
387 pub(crate) sync_runtime: Arc<crate::synchronization::VmSyncRuntime>,
389 pub(crate) shared_state_runtime: Arc<crate::shared_state::VmSharedStateRuntime>,
391 pub(crate) inline_cache_sets: Vec<Vec<crate::chunk::InlineCacheEntry>>,
395 pub(crate) inline_cache_set_by_chunk: HashMap<u64, usize>,
396 pub(crate) pool_registry: Arc<crate::stdlib::pool::PoolRegistry>,
398 pub(crate) llm_mock_context: crate::llm::mock::LlmMockContext,
400 pub(crate) package_snapshot_registry: Arc<crate::stdlib::PackageSnapshotRegistry>,
404 pub(crate) wait_for_graph: Arc<crate::wait_for_graph::VmWaitForGraph>,
406 pub(crate) held_sync_guards: Vec<crate::synchronization::VmSyncHeldGuard>,
408 pub(crate) inherited_held_keys: Arc<Vec<crate::synchronization::VmSyncHeldKey>>,
416 pub(crate) task_scopes: Vec<TaskScope>,
422 pub(crate) task_counter: u64,
424 pub(crate) runtime_context_counter: u64,
426 pub(crate) runtime_context: crate::runtime_context::RuntimeContext,
428 pub(crate) deadlines: Vec<(Instant, usize)>,
430 pub(crate) execution_deadline: Arc<ExecutionDeadlineState>,
432 pub(crate) breakpoints: BTreeMap<String, std::collections::BTreeSet<usize>>,
437 pub(crate) function_breakpoints: std::collections::BTreeSet<String>,
443 pub(crate) pending_function_bp: Option<String>,
448 pub(crate) step_mode: bool,
450 pub(crate) step_frame_depth: usize,
452 pub(crate) stopped: bool,
454 pub(crate) last_line: usize,
456 pub(crate) source_dir: Option<std::path::PathBuf>,
458 pub(crate) package_execution_guard:
460 Option<Arc<harn_modules::package_execution::PackageExecutionGuard>>,
461 pub(crate) imported_paths: Vec<std::path::PathBuf>,
463 pub(crate) deferred_cyclic_imports: Vec<super::modules::DeferredCyclicImport>,
467 pub(crate) module_cache: ModuleCache,
469 pub(crate) prepared_module_cache: crate::PreparedModuleCache,
472 pub(crate) module_phase_recorder: Option<super::ModulePhaseRecorder>,
474 pub(crate) lazy_callable_modules: LazyCallableModuleCache,
478 pub(crate) source_cache: Arc<BTreeMap<std::path::PathBuf, Arc<str>>>,
482 pub(crate) source_file: Option<String>,
484 pub(crate) source_text: Option<String>,
486 pub(crate) coverage: Option<crate::coverage::Coverage>,
489 pub(crate) bridge: Option<Arc<crate::bridge::HostBridge>>,
491 pub(crate) denied_builtins: Arc<HashSet<String>>,
493 pub(crate) cancel_token: Option<std::sync::Arc<std::sync::atomic::AtomicBool>>,
495 pub(crate) interrupt_signal_token: Option<std::sync::Arc<std::sync::Mutex<Option<String>>>>,
496 pub(crate) cancel_grace_instructions_remaining: Option<usize>,
501 pub(crate) interrupt_handlers: Vec<InterruptHandler>,
503 pub(crate) next_interrupt_handle: i64,
504 pub(crate) pending_interrupt_signal: Option<String>,
505 pub(crate) interrupted: bool,
506 pub(crate) dispatching_interrupt: bool,
507 pub(crate) interrupt_handler_deadline: Option<Instant>,
508 pub(crate) error_stack_trace: Vec<(String, usize, usize, Option<String>)>,
510 pub(crate) yield_sender: Option<tokio::sync::mpsc::Sender<Result<VmValue, VmError>>>,
513 pub(crate) project_root: Option<std::path::PathBuf>,
516 pub(crate) globals: Arc<crate::value::DictMap>,
519 pub(crate) debug_hook: Option<parking_lot::Mutex<Box<DebugHook>>>,
521 pub(crate) runtime_limits: RuntimeLimits,
523}
524
525#[derive(Clone)]
533pub struct VmBaseline {
534 builtins: Arc<BTreeMap<String, VmBuiltinFn>>,
535 async_builtins: Arc<BTreeMap<String, VmAsyncBuiltinFn>>,
536 builtin_metadata: Arc<BTreeMap<String, VmBuiltinMetadata>>,
537 builtins_by_id: Arc<HashMap<BuiltinId, VmBuiltinEntry>>,
538 builtin_id_collisions: Arc<HashSet<BuiltinId>>,
539 source_dir: Option<std::path::PathBuf>,
540 source_file: Option<String>,
541 source_text: Option<String>,
542 project_root: Option<std::path::PathBuf>,
543 globals: Arc<crate::value::DictMap>,
544 denied_builtins: Arc<HashSet<String>>,
545 prepared_module_cache: crate::PreparedModuleCache,
546 runtime_limits: RuntimeLimits,
547}
548
549impl VmBaseline {
550 pub fn from_vm(vm: &Vm) -> Self {
551 Self {
552 builtins: Arc::clone(&vm.builtins),
553 async_builtins: Arc::clone(&vm.async_builtins),
554 builtin_metadata: Arc::clone(&vm.builtin_metadata),
555 builtins_by_id: Arc::clone(&vm.builtins_by_id),
556 builtin_id_collisions: Arc::clone(&vm.builtin_id_collisions),
557 source_dir: vm.source_dir.clone(),
558 source_file: vm.source_file.clone(),
559 source_text: vm.source_text.clone(),
560 project_root: vm.project_root.clone(),
561 globals: Arc::clone(&vm.globals),
562 denied_builtins: Arc::clone(&vm.denied_builtins),
563 prepared_module_cache: vm.prepared_module_cache.clone(),
564 runtime_limits: vm.runtime_limits,
565 }
566 }
567
568 pub fn instantiate(&self) -> Vm {
569 crate::initialize_runtime_assets();
570 let mut source_cache = BTreeMap::new();
571 if let (Some(file), Some(text)) = (&self.source_file, &self.source_text) {
572 source_cache.insert(std::path::PathBuf::from(file), Arc::from(text.as_str()));
573 }
574 if let Some(dir) = &self.source_dir {
575 crate::stdlib::set_thread_source_dir(dir);
576 }
577
578 let mut vm = Vm {
579 stack: Vec::with_capacity(256),
580 env: VmEnv::new(),
581 output: String::new(),
582 builtins: Arc::clone(&self.builtins),
583 async_builtins: Arc::clone(&self.async_builtins),
584 builtin_metadata: Arc::clone(&self.builtin_metadata),
585 builtins_by_id: Arc::clone(&self.builtins_by_id),
586 builtin_id_collisions: Arc::clone(&self.builtin_id_collisions),
587 iterators: Vec::new(),
588 frames: Vec::new(),
589 exception_handlers: Vec::new(),
590 spawned_tasks: BTreeMap::new(),
591 process_exit_request: Arc::new(ProcessExitRequest::new()),
592 sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
593 shared_state_runtime: Arc::new(crate::shared_state::VmSharedStateRuntime::new()),
594 inline_cache_sets: Vec::new(),
595 inline_cache_set_by_chunk: HashMap::new(),
596 pool_registry: crate::stdlib::pool::new_pool_registry(),
597 llm_mock_context: crate::llm::mock::LlmMockContext::for_new_vm(),
598 package_snapshot_registry: Arc::new(Default::default()),
599 wait_for_graph: Arc::new(crate::wait_for_graph::VmWaitForGraph::new()),
600 held_sync_guards: Vec::new(),
601 inherited_held_keys: Arc::new(Vec::new()),
602 task_scopes: Vec::new(),
603 task_counter: 0,
604 runtime_context_counter: 0,
605 runtime_context: crate::runtime_context::RuntimeContext::root(),
606 deadlines: Vec::new(),
607 execution_deadline: super::execution::new_execution_deadline_state(None),
608 breakpoints: BTreeMap::new(),
609 function_breakpoints: std::collections::BTreeSet::new(),
610 pending_function_bp: None,
611 step_mode: false,
612 step_frame_depth: 0,
613 stopped: false,
614 last_line: 0,
615 source_dir: self.source_dir.clone(),
616 package_execution_guard: None,
617 imported_paths: Vec::new(),
618 deferred_cyclic_imports: Vec::new(),
619 module_cache: Arc::new(BTreeMap::new()),
620 prepared_module_cache: self.prepared_module_cache.clone(),
621 module_phase_recorder: None,
622 lazy_callable_modules: Arc::new(crate::value::VmMutex::new(BTreeMap::new())),
623 source_cache: Arc::new(source_cache),
624 source_file: self.source_file.clone(),
625 source_text: self.source_text.clone(),
626 coverage: crate::coverage::for_primary(self.source_file.as_deref()),
627 bridge: None,
628 denied_builtins: Arc::clone(&self.denied_builtins),
629 cancel_token: None,
630 interrupt_signal_token: None,
631 cancel_grace_instructions_remaining: None,
632 interrupt_handlers: Vec::new(),
633 next_interrupt_handle: 1,
634 pending_interrupt_signal: None,
635 interrupted: false,
636 dispatching_interrupt: false,
637 interrupt_handler_deadline: None,
638 error_stack_trace: Vec::new(),
639 yield_sender: None,
640 project_root: self.project_root.clone(),
641 globals: Arc::clone(&self.globals),
642 debug_hook: None,
643 runtime_limits: self.runtime_limits,
644 };
645
646 crate::stdlib::rebind_execution_state_builtins(&mut vm);
647 vm
648 }
649}
650
651impl Vm {
652 pub(crate) fn ensure_execution_available(&self) -> Result<(), VmError> {
653 if self.execution_deadline.is_abandoned() {
654 return Err(VmError::AbandonedExecution);
655 }
656 Ok(())
657 }
658
659 pub(crate) fn fresh_local_slots(chunk: &Chunk) -> Vec<LocalSlot> {
660 chunk
661 .local_slots
662 .iter()
663 .map(|_| LocalSlot {
664 value: VmValue::Nil,
665 initialized: false,
666 synced: false,
667 })
668 .collect()
669 }
670
671 pub(crate) fn bind_param_slots(
672 slots: &mut [LocalSlot],
673 func: &crate::chunk::CompiledFunction,
674 args: &[VmValue],
675 synced: bool,
676 ) {
677 Self::bind_param_slots_args(slots, func, &super::CallArgs::Slice(args), synced);
678 }
679
680 pub(crate) fn bind_param_slots_args(
681 slots: &mut [LocalSlot],
682 func: &crate::chunk::CompiledFunction,
683 args: &super::CallArgs<'_>,
684 synced: bool,
685 ) {
686 let param_count = func.params.len();
687 for (i, _param) in func.params.iter().enumerate() {
688 if i >= slots.len() {
689 break;
690 }
691 if func.has_rest_param && i == param_count - 1 {
692 let rest_args = args.to_vec_from(i);
693 slots[i].value = VmValue::List(std::sync::Arc::new(rest_args));
694 slots[i].initialized = true;
695 slots[i].synced = synced;
696 } else if let Some(arg) = args.get(i) {
697 slots[i].value = arg.clone();
698 slots[i].initialized = true;
699 slots[i].synced = synced;
700 }
701 }
702 }
703
704 pub(crate) fn visible_variables(&self) -> crate::value::DictMap {
705 let mut vars = self.env.all_variables();
706 let Some(frame) = self.frames.last() else {
707 return vars;
708 };
709 for (slot, info) in frame.local_slots.iter().zip(frame.chunk.local_slots.iter()) {
710 if slot.initialized && info.scope_depth <= frame.local_scope_depth {
711 vars.insert(crate::value::intern_key(&info.name), slot.value.clone());
712 }
713 }
714 vars
715 }
716
717 pub(crate) fn sync_current_frame_locals_to_env(&mut self) {
718 let frames = &mut self.frames;
719 let env = &mut self.env;
720 let Some(frame) = frames.last_mut() else {
721 return;
722 };
723 let local_scope_base = frame.local_scope_base;
724 let local_scope_depth = frame.local_scope_depth;
725 for (slot, info) in frame
726 .local_slots
727 .iter_mut()
728 .zip(frame.chunk.local_slots.iter())
729 {
730 if slot.initialized && !slot.synced && info.scope_depth <= local_scope_depth {
731 slot.synced = true;
732 let scope_idx = local_scope_base + info.scope_depth;
733 while env.scopes.len() <= scope_idx {
734 env.push_scope();
735 }
736 Arc::make_mut(&mut env.scopes[scope_idx].vars).insert(
740 info.name.clone(),
741 crate::value::Binding::Value {
742 value: slot.value.clone(),
743 mutable: info.mutable,
744 },
745 );
746 }
747 }
748 }
749
750 pub(crate) fn closure_call_env_for_current_frame(
751 &self,
752 closure: &crate::value::VmClosure,
753 ) -> VmEnv {
754 if closure.module_state().is_some() {
755 return closure.env.cloned_for_call();
756 }
757 let call_env = Self::closure_call_env(&self.env, closure);
758 if !closure.func.chunk.references_outer_names {
763 return call_env;
764 }
765 let mut call_env = call_env;
766 let Some(frame) = self.frames.last() else {
767 return call_env;
768 };
769 for (slot, info) in frame
770 .local_slots
771 .iter()
772 .zip(frame.chunk.local_slots.iter())
773 .filter(|(slot, info)| slot.initialized && info.scope_depth <= frame.local_scope_depth)
774 {
775 if matches!(slot.value, VmValue::Closure(_)) && !call_env.contains(&info.name) {
776 let _ = call_env.define(&info.name, slot.value.clone(), info.mutable);
777 }
778 }
779 call_env
780 }
781
782 pub(crate) fn active_local_slot_value(&self, name: &str) -> Option<VmValue> {
783 let frame = self.frames.last()?;
784 let idx = self.active_local_slot_index(name)?;
785 frame.local_slots.get(idx).map(|slot| slot.value.clone())
786 }
787
788 pub(crate) fn active_local_slot_index(&self, name: &str) -> Option<usize> {
793 let frame = self.frames.last()?;
794 for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
795 if info.name == name && info.scope_depth <= frame.local_scope_depth {
796 if let Some(slot) = frame.local_slots.get(idx) {
797 if slot.initialized {
798 return Some(idx);
799 }
800 }
801 }
802 }
803 None
804 }
805
806 pub(crate) fn assign_active_local_slot(
807 &mut self,
808 name: &str,
809 value: VmValue,
810 debug: bool,
811 ) -> Result<bool, VmError> {
812 let Some(frame) = self.frames.last_mut() else {
813 return Ok(false);
814 };
815 for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
816 if info.name == name && info.scope_depth <= frame.local_scope_depth {
817 if !debug && !info.mutable {
818 return Err(VmError::ImmutableAssignment(name.to_string()));
819 }
820 if let Some(slot) = frame.local_slots.get_mut(idx) {
821 crate::value::recursion::dismantle(std::mem::replace(&mut slot.value, value));
822 slot.initialized = true;
823 slot.synced = false;
824 return Ok(true);
825 }
826 }
827 }
828 Ok(false)
829 }
830
831 pub fn new() -> Self {
832 crate::initialize_runtime_assets();
833 Self {
834 stack: Vec::with_capacity(256),
835 env: VmEnv::new(),
836 output: String::new(),
837 builtins: Arc::new(BTreeMap::new()),
838 async_builtins: Arc::new(BTreeMap::new()),
839 builtin_metadata: Arc::new(BTreeMap::new()),
840 builtins_by_id: Arc::new(HashMap::new()),
841 builtin_id_collisions: Arc::new(HashSet::new()),
842 iterators: Vec::new(),
843 frames: Vec::new(),
844 exception_handlers: Vec::new(),
845 spawned_tasks: BTreeMap::new(),
846 process_exit_request: Arc::new(ProcessExitRequest::new()),
847 sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
848 shared_state_runtime: Arc::new(crate::shared_state::VmSharedStateRuntime::new()),
849 inline_cache_sets: Vec::new(),
850 inline_cache_set_by_chunk: HashMap::new(),
851 pool_registry: crate::stdlib::pool::new_pool_registry(),
852 llm_mock_context: crate::llm::mock::LlmMockContext::for_new_vm(),
853 package_snapshot_registry: Arc::new(Default::default()),
854 wait_for_graph: Arc::new(crate::wait_for_graph::VmWaitForGraph::new()),
855 held_sync_guards: Vec::new(),
856 inherited_held_keys: Arc::new(Vec::new()),
857 task_scopes: Vec::new(),
858 task_counter: 0,
859 runtime_context_counter: 0,
860 runtime_context: crate::runtime_context::RuntimeContext::root(),
861 deadlines: Vec::new(),
862 execution_deadline: super::execution::new_execution_deadline_state(None),
863 breakpoints: BTreeMap::new(),
864 function_breakpoints: std::collections::BTreeSet::new(),
865 pending_function_bp: None,
866 step_mode: false,
867 step_frame_depth: 0,
868 stopped: false,
869 last_line: 0,
870 source_dir: None,
871 package_execution_guard: None,
872 imported_paths: Vec::new(),
873 deferred_cyclic_imports: Vec::new(),
874 module_cache: Arc::new(BTreeMap::new()),
875 prepared_module_cache: crate::PreparedModuleCache::default(),
876 module_phase_recorder: None,
877 lazy_callable_modules: Arc::new(crate::value::VmMutex::new(BTreeMap::new())),
878 source_cache: Arc::new(BTreeMap::new()),
879 source_file: None,
880 source_text: None,
881 coverage: crate::coverage::for_primary(None),
882 bridge: None,
883 denied_builtins: Arc::new(HashSet::new()),
884 cancel_token: None,
885 interrupt_signal_token: None,
886 cancel_grace_instructions_remaining: None,
887 interrupt_handlers: Vec::new(),
888 next_interrupt_handle: 1,
889 pending_interrupt_signal: None,
890 interrupted: false,
891 dispatching_interrupt: false,
892 interrupt_handler_deadline: None,
893 error_stack_trace: Vec::new(),
894 yield_sender: None,
895 project_root: None,
896 globals: Arc::new(crate::value::DictMap::new()),
897 debug_hook: None,
898 runtime_limits: RuntimeLimits::default(),
899 }
900 }
901
902 pub fn baseline(&self) -> VmBaseline {
903 VmBaseline::from_vm(self)
904 }
905
906 pub fn set_prepared_module_cache(&mut self, cache: crate::PreparedModuleCache) {
909 self.prepared_module_cache = cache;
910 }
911
912 pub fn runtime_limits(&self) -> RuntimeLimits {
914 self.runtime_limits
915 }
916
917 pub fn runtime_limit_report(&self) -> crate::RuntimeLimitsReport {
919 self.runtime_limits.report()
920 }
921
922 #[inline]
936 pub(crate) fn debugger_attached(&self) -> bool {
937 self.debug_hook.is_some()
938 || !self.breakpoints.is_empty()
939 || !self.function_breakpoints.is_empty()
940 }
941
942 pub fn set_bridge(&mut self, bridge: Arc<crate::bridge::HostBridge>) {
944 self.bridge = Some(bridge);
945 }
946
947 pub fn set_denied_builtins(&mut self, denied: HashSet<String>) {
950 self.denied_builtins = Arc::new(denied);
951 }
952
953 pub fn set_source_info(&mut self, file: &str, text: &str) {
955 self.source_file = Some(file.to_string());
956 self.source_text = Some(text.to_string());
957 if let Some(cov) = self.coverage.as_mut() {
958 cov.set_primary_file(file);
959 }
960 Arc::make_mut(&mut self.source_cache)
961 .insert(std::path::PathBuf::from(file), Arc::from(text));
962 }
963
964 pub fn start(&mut self, chunk: &Chunk) -> Result<(), VmError> {
966 self.ensure_execution_available()?;
967 let debugger = self.debugger_attached();
974 let initial_env = if debugger {
975 Some(self.env.clone())
976 } else {
977 None
978 };
979 let initial_local_slots = if debugger {
980 Some(Self::fresh_local_slots(chunk))
981 } else {
982 None
983 };
984 let chunk = Arc::new(chunk.clone());
985 let local_slots = Self::fresh_local_slots(&chunk);
986 let inline_cache_set = self.inline_cache_set_index_for_chunk(&chunk);
987 self.frames.push(CallFrame {
988 chunk,
989 inline_cache_set,
990 ip: 0,
991 stack_base: self.stack.len(),
992 saved_env: self.env.clone(),
993 initial_env,
994 initial_local_slots,
995 saved_iterator_depth: self.iterators.len(),
996 fn_name: String::new(),
997 argc: 0,
998 saved_source_dir: None,
999 module_functions: None,
1000 module_state: None,
1001 local_slots,
1002 local_scope_base: self.env.scope_depth().saturating_sub(1),
1003 local_scope_depth: 0,
1004 });
1005 Ok(())
1006 }
1007
1008 pub(crate) fn child_vm(&self) -> Vm {
1011 Vm {
1012 stack: Vec::with_capacity(64),
1013 env: self.env.clone(),
1014 output: String::new(),
1015 builtins: Arc::clone(&self.builtins),
1016 async_builtins: Arc::clone(&self.async_builtins),
1017 builtin_metadata: Arc::clone(&self.builtin_metadata),
1018 builtins_by_id: Arc::clone(&self.builtins_by_id),
1019 builtin_id_collisions: Arc::clone(&self.builtin_id_collisions),
1020 iterators: Vec::new(),
1021 frames: Vec::new(),
1022 exception_handlers: Vec::new(),
1023 spawned_tasks: BTreeMap::new(),
1024 process_exit_request: Arc::clone(&self.process_exit_request),
1025 sync_runtime: self.sync_runtime.clone(),
1026 shared_state_runtime: self.shared_state_runtime.clone(),
1027 inline_cache_sets: Vec::new(),
1028 inline_cache_set_by_chunk: HashMap::new(),
1029 pool_registry: self.pool_registry.clone(),
1030 llm_mock_context: self.llm_mock_context.clone(),
1031 package_snapshot_registry: self.package_snapshot_registry.clone(),
1032 wait_for_graph: self.wait_for_graph.clone(),
1033 held_sync_guards: Vec::new(),
1034 inherited_held_keys: Arc::new(Vec::new()),
1035 task_scopes: Vec::new(),
1036 task_counter: 0,
1037 runtime_context_counter: self.runtime_context_counter,
1038 runtime_context: self.runtime_context.clone(),
1039 deadlines: self.deadlines.clone(),
1040 execution_deadline: self.execution_deadline.fork(),
1041 breakpoints: BTreeMap::new(),
1042 function_breakpoints: std::collections::BTreeSet::new(),
1043 pending_function_bp: None,
1044 step_mode: false,
1045 step_frame_depth: 0,
1046 stopped: false,
1047 last_line: 0,
1048 source_dir: self.source_dir.clone(),
1049 package_execution_guard: self.package_execution_guard.clone(),
1050 imported_paths: Vec::new(),
1051 deferred_cyclic_imports: Vec::new(),
1052 module_cache: Arc::clone(&self.module_cache),
1053 prepared_module_cache: self.prepared_module_cache.clone(),
1054 module_phase_recorder: self.module_phase_recorder.clone(),
1055 lazy_callable_modules: Arc::clone(&self.lazy_callable_modules),
1056 source_cache: Arc::clone(&self.source_cache),
1057 source_file: self.source_file.clone(),
1058 source_text: self.source_text.clone(),
1059 coverage: crate::coverage::for_primary(self.source_file.as_deref()),
1060 bridge: self.bridge.clone(),
1061 denied_builtins: Arc::clone(&self.denied_builtins),
1062 cancel_token: self.cancel_token.clone(),
1063 interrupt_signal_token: self.interrupt_signal_token.clone(),
1064 cancel_grace_instructions_remaining: None,
1065 interrupt_handlers: Vec::new(),
1066 next_interrupt_handle: 1,
1067 pending_interrupt_signal: None,
1068 interrupted: self.interrupted,
1069 dispatching_interrupt: false,
1070 interrupt_handler_deadline: None,
1071 error_stack_trace: Vec::new(),
1072 yield_sender: None,
1073 project_root: self.project_root.clone(),
1074 globals: Arc::clone(&self.globals),
1075 debug_hook: None,
1076 runtime_limits: self.runtime_limits,
1077 }
1078 }
1079
1080 pub(crate) fn child_vm_for_host(&self) -> Vm {
1083 self.child_vm()
1084 }
1085
1086 pub(crate) fn request_process_exit(&self, code: i32) {
1087 self.process_exit_request.request(code);
1088 }
1089
1090 pub(crate) fn requested_process_exit(&self) -> Option<i32> {
1091 self.process_exit_request.code()
1092 }
1093
1094 pub(crate) fn cancel_spawned_tasks(&mut self) {
1098 for (_, task) in std::mem::take(&mut self.spawned_tasks) {
1099 task.cancel_token
1100 .store(true, std::sync::atomic::Ordering::SeqCst);
1101 task.handle.abort();
1102 }
1103 }
1104
1105 pub fn set_source_dir(&mut self, dir: &std::path::Path) {
1108 let dir = crate::stdlib::process::normalize_context_path(dir);
1109 self.source_dir = Some(dir.clone());
1110 crate::stdlib::set_thread_source_dir(&dir);
1111 if self.project_root.is_none() {
1113 self.project_root = crate::stdlib::process::find_project_root(&dir);
1114 }
1115 }
1116
1117 pub fn set_project_root(&mut self, root: &std::path::Path) {
1120 self.project_root = Some(root.to_path_buf());
1121 }
1122
1123 pub(crate) fn explicit_project_root(&self) -> Option<&std::path::Path> {
1126 self.project_root.as_deref()
1127 }
1128
1129 pub fn project_root(&self) -> Option<&std::path::Path> {
1131 self.project_root.as_deref().or(self.source_dir.as_deref())
1132 }
1133
1134 pub fn builtin_names(&self) -> Vec<String> {
1136 let mut names: Vec<String> = self.builtins.keys().cloned().collect();
1137 names.extend(self.async_builtins.keys().cloned());
1138 names
1139 }
1140
1141 pub fn builtin_metadata(&self) -> Vec<VmBuiltinMetadata> {
1143 self.builtin_metadata.values().cloned().collect()
1144 }
1145
1146 pub fn builtin_metadata_for(&self, name: &str) -> Option<&VmBuiltinMetadata> {
1148 self.builtin_metadata.get(name)
1149 }
1150
1151 pub fn set_global(&mut self, name: &str, value: VmValue) {
1154 Arc::make_mut(&mut self.globals).insert(crate::value::intern_key(name), value);
1155 }
1156
1157 pub fn global(&self, name: &str) -> Option<&VmValue> {
1163 self.globals.get(name)
1164 }
1165
1166 pub fn set_harness(&mut self, harness: crate::harness::Harness) {
1172 self.set_global("harness", harness.into_vm_value());
1173 }
1174
1175 pub fn output(&self) -> &str {
1177 &self.output
1178 }
1179
1180 pub fn take_output(&mut self) -> String {
1184 std::mem::take(&mut self.output)
1185 }
1186
1187 pub fn append_output(&mut self, text: &str) {
1191 self.output.push_str(text);
1192 }
1193
1194 pub(crate) fn pop(&mut self) -> Result<VmValue, VmError> {
1195 self.stack.pop().ok_or(VmError::StackUnderflow)
1196 }
1197
1198 pub(crate) fn peek(&self) -> Result<&VmValue, VmError> {
1199 self.stack.last().ok_or(VmError::StackUnderflow)
1200 }
1201
1202 pub(crate) fn const_str(c: &Constant) -> Result<&str, VmError> {
1203 match c {
1204 Constant::String(s) => Ok(s.as_str()),
1205 _ => Err(VmError::TypeError("expected string constant".into())),
1206 }
1207 }
1208
1209 pub(crate) fn release_sync_guards_for_current_scope(&mut self) {
1210 let depth = self.env.scope_depth();
1211 self.held_sync_guards
1212 .retain(|guard| guard.env_scope_depth < depth);
1213 self.cancel_task_scopes_where(|s| s.env_scope_depth >= depth);
1216 }
1217
1218 pub(crate) fn release_sync_guards_after_unwind(
1219 &mut self,
1220 frame_depth: usize,
1221 env_scope_depth: usize,
1222 ) {
1223 self.held_sync_guards.retain(|guard| {
1224 guard.frame_depth <= frame_depth && guard.env_scope_depth <= env_scope_depth
1225 });
1226 self.cancel_task_scopes_where(|s| {
1229 !(s.frame_depth <= frame_depth && s.env_scope_depth <= env_scope_depth)
1230 });
1231 }
1232
1233 pub(crate) fn release_sync_guards_for_frame(&mut self, frame_depth: usize) {
1234 self.held_sync_guards
1235 .retain(|guard| guard.frame_depth != frame_depth);
1236 self.cancel_task_scopes_where(|s| s.frame_depth == frame_depth);
1239 }
1240
1241 pub(crate) fn adopt_sync_permit_for_current_scope(
1242 &mut self,
1243 permit: crate::value::VmSyncPermitHandle,
1244 ) {
1245 if permit.is_released()
1246 || self
1247 .held_sync_guards
1248 .iter()
1249 .any(|guard| guard._permit.same_lease(&permit))
1250 {
1251 return;
1252 }
1253 self.held_sync_guards
1254 .push(crate::synchronization::VmSyncHeldGuard {
1255 _permit: permit,
1256 frame_depth: self.frames.len(),
1257 env_scope_depth: self.env.scope_depth(),
1258 });
1259 }
1260
1261 pub(crate) fn deregister_task_from_scopes(&mut self, id: &str) {
1264 for scope in &mut self.task_scopes {
1265 scope.task_ids.retain(|t| t != id);
1266 }
1267 }
1268
1269 fn cancel_task_scopes_where<F: Fn(&TaskScope) -> bool>(&mut self, doomed: F) {
1272 let mut i = 0;
1273 while i < self.task_scopes.len() {
1274 if doomed(&self.task_scopes[i]) {
1275 let scope = self.task_scopes.remove(i);
1276 for id in &scope.task_ids {
1277 if let Some(task) = self.spawned_tasks.remove(id) {
1278 task.cancel_token
1279 .store(true, std::sync::atomic::Ordering::SeqCst);
1280 task.handle.abort();
1281 }
1282 }
1283 } else {
1284 i += 1;
1285 }
1286 }
1287 }
1288
1289 pub(crate) fn held_permits_for(&self, kind: &str, key: &str) -> u32 {
1293 let own: u32 = self
1294 .held_sync_guards
1295 .iter()
1296 .filter(|guard| {
1297 !guard._permit.is_released()
1298 && guard._permit.kind() == kind
1299 && guard._permit.key() == key
1300 })
1301 .map(|guard| guard._permit.permits())
1302 .sum();
1303 let inherited: u32 = self
1304 .inherited_held_keys
1305 .iter()
1306 .filter(|held| held.kind == kind && held.key == key)
1307 .map(|held| held.permits)
1308 .sum();
1309 own + inherited
1310 }
1311
1312 pub(crate) fn combined_held_keys(&self) -> Vec<crate::synchronization::VmSyncHeldKey> {
1315 let mut keys: Vec<crate::synchronization::VmSyncHeldKey> = self
1316 .held_sync_guards
1317 .iter()
1318 .filter_map(|guard| crate::synchronization::VmSyncHeldKey::from_permit(&guard._permit))
1319 .collect();
1320 keys.extend(self.inherited_held_keys.iter().cloned());
1321 keys
1322 }
1323
1324 pub(crate) fn child_vm_inline(&self) -> Vm {
1330 let mut child = self.child_vm();
1331 child.inherited_held_keys = Arc::new(self.combined_held_keys());
1332 child
1333 }
1334}
1335
1336impl Drop for Vm {
1337 fn drop(&mut self) {
1338 if let Some(coverage) = self.coverage.take() {
1339 crate::coverage::merge_into_global(coverage);
1340 }
1341 self.cancel_spawned_tasks();
1342 }
1343}
1344
1345impl Default for Vm {
1346 fn default() -> Self {
1347 Self::new()
1348 }
1349}
1350
1351#[cfg(test)]
1352mod tests {
1353
1354 use super::*;
1355
1356 #[test]
1357 fn vm_construction_initializes_shared_secret_patterns() {
1358 let _vm = Vm::new();
1359 assert!(crate::secret_patterns::default_secret_patterns_initialized());
1360 }
1361
1362 fn baseline_with_stdlib(source: &str) -> VmBaseline {
1363 let mut vm = Vm::new();
1364 crate::register_vm_stdlib(&mut vm);
1365 vm.set_source_info("baseline_test.harn", source);
1366 vm.set_global(
1367 "stable_global",
1368 VmValue::String(arcstr::ArcStr::from("baseline")),
1369 );
1370 vm.baseline()
1371 }
1372
1373 #[test]
1374 fn vm_baseline_instantiates_clean_mutable_execution_state() {
1375 let baseline = baseline_with_stdlib("pipeline main() { __io_println(stable_global) }");
1376
1377 let mut dirty = baseline.instantiate();
1378 dirty.stack.push(VmValue::Int(42));
1379 dirty.output.push_str("dirty");
1380 dirty.task_counter = 9;
1381 dirty.runtime_context_counter = 7;
1382 dirty
1383 .error_stack_trace
1384 .push(("main".to_string(), 1, 1, None));
1385
1386 let clean = baseline.instantiate();
1387 assert!(clean.stack.is_empty());
1388 assert!(clean.output.is_empty());
1389 assert!(clean.frames.is_empty());
1390 assert!(clean.exception_handlers.is_empty());
1391 assert!(clean.spawned_tasks.is_empty());
1392 assert!(clean.held_sync_guards.is_empty());
1393 assert_eq!(clean.task_counter, 0);
1394 assert_eq!(clean.runtime_context_counter, 0);
1395 assert!(clean.deadlines.is_empty());
1396 assert!(clean.cancel_token.is_none());
1397 assert!(clean.interrupt_handlers.is_empty());
1398 assert!(clean.error_stack_trace.is_empty());
1399 assert!(clean.bridge.is_none());
1400 assert!(clean
1401 .globals
1402 .get("stable_global")
1403 .is_some_and(|value| value.display() == "baseline"));
1404 }
1405
1406 #[tokio::test]
1407 async fn inline_child_inherits_held_lock_keys_but_concurrent_child_does_not() {
1408 let mut parent = Vm::new();
1409 let permit = parent
1410 .sync_runtime
1411 .acquire("mutex", "v:test", 1, 1, None, None)
1412 .await
1413 .unwrap()
1414 .unwrap();
1415 parent
1416 .held_sync_guards
1417 .push(crate::synchronization::VmSyncHeldGuard {
1418 _permit: permit,
1419 frame_depth: 0,
1420 env_scope_depth: 0,
1421 });
1422 assert_eq!(parent.held_permits_for("mutex", "v:test"), 1);
1423
1424 let inline = parent.child_vm_inline();
1429 assert_eq!(inline.held_permits_for("mutex", "v:test"), 1);
1430 assert_eq!(
1431 inline.child_vm_inline().held_permits_for("mutex", "v:test"),
1432 1
1433 );
1434
1435 let concurrent = parent.child_vm();
1439 assert_eq!(concurrent.held_permits_for("mutex", "v:test"), 0);
1440 }
1441
1442 #[test]
1443 fn vm_reports_effective_runtime_limits() {
1444 let vm = Vm::new();
1445
1446 assert_eq!(vm.runtime_limits(), RuntimeLimits::default());
1447 assert_eq!(
1448 vm.runtime_limit_report().entries.len(),
1449 crate::RUNTIME_LIMIT_DESCRIPTIONS.len()
1450 );
1451 assert_eq!(vm.child_vm().runtime_limits(), vm.runtime_limits());
1452 assert_eq!(
1453 vm.baseline().instantiate().runtime_limits(),
1454 vm.runtime_limits()
1455 );
1456 }
1457
1458 #[tokio::test(flavor = "current_thread")]
1459 async fn vm_baseline_rebinds_shared_state_builtins_per_instance() {
1460 let local = tokio::task::LocalSet::new();
1461 local
1462 .run_until(async {
1463 let source = r#"
1464pipeline main() {
1465 const cell = shared_cell({scope: "task_group", key: "turn", initial: 0})
1466 __io_println(shared_get(cell))
1467 shared_set(cell, shared_get(cell) + 1)
1468}"#;
1469 let chunk = crate::compile_source(source).expect("compile");
1470 let baseline = baseline_with_stdlib(source);
1471
1472 let mut first = baseline.instantiate();
1473 first.execute(&chunk).await.expect("first execute");
1474 assert_eq!(first.output(), "0\n");
1475
1476 let mut second = baseline.instantiate();
1477 second.execute(&chunk).await.expect("second execute");
1478 assert_eq!(
1479 second.output(),
1480 "0\n",
1481 "shared state created by the first VM must not leak into the next baseline instance"
1482 );
1483 })
1484 .await;
1485 }
1486}