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 crate::orchestration::clear_pipeline_on_finish();
154 }
155 }
156}
157
158impl Drop for ScopeSpan {
159 fn drop(&mut self) {
160 crate::tracing::span_end(self.0);
161 }
162}
163
164#[derive(Clone)]
165pub(crate) struct LocalSlot {
166 pub(crate) value: VmValue,
167 pub(crate) initialized: bool,
168 pub(crate) synced: bool,
169}
170
171impl Drop for LocalSlot {
172 fn drop(&mut self) {
173 if crate::value::recursion::is_recursive_container(&self.value) {
181 crate::value::recursion::dismantle(std::mem::replace(&mut self.value, VmValue::Nil));
182 }
183 }
184}
185
186#[derive(Clone)]
187pub(crate) struct InterruptHandler {
188 pub(crate) handle: i64,
189 pub(crate) signals: Vec<String>,
190 pub(crate) once: bool,
191 pub(crate) graceful_timeout_ms: Option<u64>,
192 pub(crate) handler: VmValue,
193}
194
195pub(crate) struct CallFrame {
197 pub(crate) chunk: ChunkRef,
198 pub(crate) inline_cache_set: usize,
202 pub(crate) ip: usize,
203 pub(crate) stack_base: usize,
204 pub(crate) saved_env: VmEnv,
205 pub(crate) initial_env: Option<VmEnv>,
213 pub(crate) initial_local_slots: Option<Vec<LocalSlot>>,
214 pub(crate) saved_iterator_depth: usize,
216 pub(crate) fn_name: String,
218 pub(crate) argc: usize,
220 pub(crate) saved_source_dir: Option<std::path::PathBuf>,
223 pub(crate) module_functions: Option<ModuleFunctionRegistry>,
225 pub(crate) module_state: Option<crate::value::ModuleState>,
231 pub(crate) local_slots: Vec<LocalSlot>,
233 pub(crate) local_scope_base: usize,
235 pub(crate) local_scope_depth: usize,
237}
238
239pub(crate) struct InlineCacheSite {
240 pub(crate) cache_set: usize,
241 pub(crate) slot_count: usize,
242 pub(crate) slot: Option<usize>,
243}
244
245impl CallFrame {
246 #[inline]
247 pub(crate) fn inline_cache_site_for_previous_op(&self) -> InlineCacheSite {
248 let op_offset = self.ip.saturating_sub(1);
249 InlineCacheSite {
250 cache_set: self.inline_cache_set,
251 slot_count: self.chunk.inline_cache_slot_count(),
252 slot: self.chunk.inline_cache_slot(op_offset),
253 }
254 }
255}
256
257pub(crate) struct ExceptionHandler {
259 pub(crate) catch_ip: usize,
260 pub(crate) stack_depth: usize,
261 pub(crate) frame_depth: usize,
262 pub(crate) env_scope_depth: usize,
263 pub(crate) error_type: Option<crate::value::HarnStr>,
265}
266
267pub(crate) struct TaskScope {
270 pub(crate) task_ids: Vec<String>,
273 pub(crate) frame_depth: usize,
275 pub(crate) env_scope_depth: usize,
277}
278
279pub(crate) struct ProcessExitRequest {
283 code: Mutex<Option<i32>>,
284 requested: AtomicBool,
285}
286
287impl ProcessExitRequest {
288 fn new() -> Self {
289 Self {
290 code: Mutex::new(None),
291 requested: AtomicBool::new(false),
292 }
293 }
294
295 fn request(&self, code: i32) {
296 let mut recorded = self
297 .code
298 .lock()
299 .expect("process exit request lock poisoned");
300 if recorded.is_none() {
301 *recorded = Some(code);
302 self.requested.store(true, Ordering::Release);
303 }
304 }
305
306 fn code(&self) -> Option<i32> {
307 if !self.requested.load(Ordering::Acquire) {
308 return None;
309 }
310 *self
311 .code
312 .lock()
313 .expect("process exit request lock poisoned")
314 }
315}
316
317pub(crate) enum IterState {
319 Vec {
320 items: Arc<Vec<VmValue>>,
321 idx: usize,
322 },
323 Dict {
324 entries: Arc<crate::value::DictMap>,
325 keys: Vec<String>,
326 idx: usize,
327 },
328 Channel {
329 receiver: std::sync::Arc<tokio::sync::Mutex<tokio::sync::mpsc::Receiver<VmValue>>>,
330 close: std::sync::Arc<crate::value::VmChannelCloseState>,
331 },
332 Generator {
333 gen: Arc<crate::value::VmGenerator>,
334 },
335 Stream {
336 stream: Arc<crate::value::VmStream>,
337 },
338 Range {
342 next: i64,
343 end: i64,
344 inclusive: bool,
345 done: bool,
346 },
347 VmIter {
348 handle: crate::vm::iter::VmIterHandle,
349 },
350}
351
352#[derive(Clone)]
353pub(crate) enum VmBuiltinDispatch {
354 Sync(VmBuiltinFn),
355 Async(VmAsyncBuiltinFn),
356}
357
358#[derive(Clone)]
359pub(crate) struct VmBuiltinEntry {
360 pub(crate) name: Arc<str>,
361 pub(crate) dispatch: VmBuiltinDispatch,
362}
363
364pub struct Vm {
366 pub(crate) stack: Vec<VmValue>,
367 pub(crate) env: VmEnv,
368 pub(crate) output: String,
369 pub(crate) builtins: Arc<BTreeMap<String, VmBuiltinFn>>,
370 pub(crate) async_builtins: Arc<BTreeMap<String, VmAsyncBuiltinFn>>,
371 pub(crate) builtin_metadata: Arc<BTreeMap<String, VmBuiltinMetadata>>,
372 pub(crate) builtins_by_id: Arc<HashMap<BuiltinId, VmBuiltinEntry>>,
375 pub(crate) builtin_id_collisions: Arc<HashSet<BuiltinId>>,
378 pub(crate) iterators: Vec<IterState>,
380 pub(crate) frames: Vec<CallFrame>,
382 pub(crate) exception_handlers: Vec<ExceptionHandler>,
384 pub(crate) spawned_tasks: BTreeMap<String, VmTaskHandle>,
386 pub(crate) process_exit_request: Arc<ProcessExitRequest>,
388 pub(crate) sync_runtime: Arc<crate::synchronization::VmSyncRuntime>,
390 pub(crate) shared_state_runtime: Arc<crate::shared_state::VmSharedStateRuntime>,
392 pub(crate) inline_cache_sets: Vec<Vec<crate::chunk::InlineCacheEntry>>,
396 pub(crate) inline_cache_set_by_chunk: HashMap<u64, usize>,
397 pub(crate) pool_registry: Arc<crate::stdlib::pool::PoolRegistry>,
399 pub(crate) llm_mock_context: crate::llm::mock::LlmMockContext,
401 pub(crate) package_snapshot_registry: Arc<crate::stdlib::PackageSnapshotRegistry>,
405 pub(crate) wait_for_graph: Arc<crate::wait_for_graph::VmWaitForGraph>,
407 pub(crate) held_sync_guards: Vec<crate::synchronization::VmSyncHeldGuard>,
409 pub(crate) inherited_held_keys: Arc<Vec<crate::synchronization::VmSyncHeldKey>>,
417 pub(crate) task_scopes: Vec<TaskScope>,
423 pub(crate) task_counter: u64,
425 pub(crate) runtime_context_counter: u64,
427 pub(crate) runtime_context: crate::runtime_context::RuntimeContext,
429 pub(crate) deadlines: Vec<(Instant, usize)>,
431 pub(crate) execution_deadline: Arc<ExecutionDeadlineState>,
433 pub(crate) breakpoints: BTreeMap<String, std::collections::BTreeSet<usize>>,
438 pub(crate) function_breakpoints: std::collections::BTreeSet<String>,
444 pub(crate) pending_function_bp: Option<String>,
449 pub(crate) step_mode: bool,
451 pub(crate) step_frame_depth: usize,
453 pub(crate) stopped: bool,
455 pub(crate) last_line: usize,
457 pub(crate) source_dir: Option<std::path::PathBuf>,
459 pub(crate) package_execution_guard:
461 Option<Arc<harn_modules::package_execution::PackageExecutionGuard>>,
462 pub(crate) imported_paths: Vec<std::path::PathBuf>,
464 pub(crate) deferred_cyclic_imports: Vec<super::modules::DeferredCyclicImport>,
468 pub(crate) module_cache: ModuleCache,
470 pub(crate) prepared_module_cache: crate::PreparedModuleCache,
473 pub(crate) module_phase_recorder: Option<super::ModulePhaseRecorder>,
475 pub(crate) lazy_callable_modules: LazyCallableModuleCache,
479 pub(crate) source_cache: Arc<BTreeMap<std::path::PathBuf, String>>,
481 pub(crate) source_file: Option<String>,
483 pub(crate) source_text: Option<String>,
485 pub(crate) coverage: Option<crate::coverage::Coverage>,
488 pub(crate) bridge: Option<Arc<crate::bridge::HostBridge>>,
490 pub(crate) denied_builtins: Arc<HashSet<String>>,
492 pub(crate) cancel_token: Option<std::sync::Arc<std::sync::atomic::AtomicBool>>,
494 pub(crate) interrupt_signal_token: Option<std::sync::Arc<std::sync::Mutex<Option<String>>>>,
495 pub(crate) cancel_grace_instructions_remaining: Option<usize>,
500 pub(crate) interrupt_handlers: Vec<InterruptHandler>,
502 pub(crate) next_interrupt_handle: i64,
503 pub(crate) pending_interrupt_signal: Option<String>,
504 pub(crate) interrupted: bool,
505 pub(crate) dispatching_interrupt: bool,
506 pub(crate) interrupt_handler_deadline: Option<Instant>,
507 pub(crate) error_stack_trace: Vec<(String, usize, usize, Option<String>)>,
509 pub(crate) yield_sender: Option<tokio::sync::mpsc::Sender<Result<VmValue, VmError>>>,
512 pub(crate) project_root: Option<std::path::PathBuf>,
515 pub(crate) globals: Arc<crate::value::DictMap>,
518 pub(crate) debug_hook: Option<parking_lot::Mutex<Box<DebugHook>>>,
520 pub(crate) runtime_limits: RuntimeLimits,
522}
523
524#[derive(Clone)]
532pub struct VmBaseline {
533 builtins: Arc<BTreeMap<String, VmBuiltinFn>>,
534 async_builtins: Arc<BTreeMap<String, VmAsyncBuiltinFn>>,
535 builtin_metadata: Arc<BTreeMap<String, VmBuiltinMetadata>>,
536 builtins_by_id: Arc<HashMap<BuiltinId, VmBuiltinEntry>>,
537 builtin_id_collisions: Arc<HashSet<BuiltinId>>,
538 source_dir: Option<std::path::PathBuf>,
539 source_file: Option<String>,
540 source_text: Option<String>,
541 project_root: Option<std::path::PathBuf>,
542 globals: Arc<crate::value::DictMap>,
543 denied_builtins: Arc<HashSet<String>>,
544 prepared_module_cache: crate::PreparedModuleCache,
545 runtime_limits: RuntimeLimits,
546}
547
548impl VmBaseline {
549 pub fn from_vm(vm: &Vm) -> Self {
550 Self {
551 builtins: Arc::clone(&vm.builtins),
552 async_builtins: Arc::clone(&vm.async_builtins),
553 builtin_metadata: Arc::clone(&vm.builtin_metadata),
554 builtins_by_id: Arc::clone(&vm.builtins_by_id),
555 builtin_id_collisions: Arc::clone(&vm.builtin_id_collisions),
556 source_dir: vm.source_dir.clone(),
557 source_file: vm.source_file.clone(),
558 source_text: vm.source_text.clone(),
559 project_root: vm.project_root.clone(),
560 globals: Arc::clone(&vm.globals),
561 denied_builtins: Arc::clone(&vm.denied_builtins),
562 prepared_module_cache: vm.prepared_module_cache.clone(),
563 runtime_limits: vm.runtime_limits,
564 }
565 }
566
567 pub fn instantiate(&self) -> Vm {
568 crate::initialize_runtime_assets();
569 let mut source_cache = BTreeMap::new();
570 if let (Some(file), Some(text)) = (&self.source_file, &self.source_text) {
571 source_cache.insert(std::path::PathBuf::from(file), text.clone());
572 }
573 if let Some(dir) = &self.source_dir {
574 crate::stdlib::set_thread_source_dir(dir);
575 }
576
577 let mut vm = Vm {
578 stack: Vec::with_capacity(256),
579 env: VmEnv::new(),
580 output: String::new(),
581 builtins: Arc::clone(&self.builtins),
582 async_builtins: Arc::clone(&self.async_builtins),
583 builtin_metadata: Arc::clone(&self.builtin_metadata),
584 builtins_by_id: Arc::clone(&self.builtins_by_id),
585 builtin_id_collisions: Arc::clone(&self.builtin_id_collisions),
586 iterators: Vec::new(),
587 frames: Vec::new(),
588 exception_handlers: Vec::new(),
589 spawned_tasks: BTreeMap::new(),
590 process_exit_request: Arc::new(ProcessExitRequest::new()),
591 sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
592 shared_state_runtime: Arc::new(crate::shared_state::VmSharedStateRuntime::new()),
593 inline_cache_sets: Vec::new(),
594 inline_cache_set_by_chunk: HashMap::new(),
595 pool_registry: crate::stdlib::pool::new_pool_registry(),
596 llm_mock_context: crate::llm::mock::LlmMockContext::for_new_vm(),
597 package_snapshot_registry: Arc::new(Default::default()),
598 wait_for_graph: Arc::new(crate::wait_for_graph::VmWaitForGraph::new()),
599 held_sync_guards: Vec::new(),
600 inherited_held_keys: Arc::new(Vec::new()),
601 task_scopes: Vec::new(),
602 task_counter: 0,
603 runtime_context_counter: 0,
604 runtime_context: crate::runtime_context::RuntimeContext::root(),
605 deadlines: Vec::new(),
606 execution_deadline: super::execution::new_execution_deadline_state(None),
607 breakpoints: BTreeMap::new(),
608 function_breakpoints: std::collections::BTreeSet::new(),
609 pending_function_bp: None,
610 step_mode: false,
611 step_frame_depth: 0,
612 stopped: false,
613 last_line: 0,
614 source_dir: self.source_dir.clone(),
615 package_execution_guard: None,
616 imported_paths: Vec::new(),
617 deferred_cyclic_imports: Vec::new(),
618 module_cache: Arc::new(BTreeMap::new()),
619 prepared_module_cache: self.prepared_module_cache.clone(),
620 module_phase_recorder: None,
621 lazy_callable_modules: Arc::new(crate::value::VmMutex::new(BTreeMap::new())),
622 source_cache: Arc::new(source_cache),
623 source_file: self.source_file.clone(),
624 source_text: self.source_text.clone(),
625 coverage: crate::coverage::for_primary(self.source_file.as_deref()),
626 bridge: None,
627 denied_builtins: Arc::clone(&self.denied_builtins),
628 cancel_token: None,
629 interrupt_signal_token: None,
630 cancel_grace_instructions_remaining: None,
631 interrupt_handlers: Vec::new(),
632 next_interrupt_handle: 1,
633 pending_interrupt_signal: None,
634 interrupted: false,
635 dispatching_interrupt: false,
636 interrupt_handler_deadline: None,
637 error_stack_trace: Vec::new(),
638 yield_sender: None,
639 project_root: self.project_root.clone(),
640 globals: Arc::clone(&self.globals),
641 debug_hook: None,
642 runtime_limits: self.runtime_limits,
643 };
644
645 crate::stdlib::rebind_execution_state_builtins(&mut vm);
646 vm
647 }
648}
649
650impl Vm {
651 pub(crate) fn ensure_execution_available(&self) -> Result<(), VmError> {
652 if self.execution_deadline.is_abandoned() {
653 return Err(VmError::AbandonedExecution);
654 }
655 Ok(())
656 }
657
658 pub(crate) fn fresh_local_slots(chunk: &Chunk) -> Vec<LocalSlot> {
659 chunk
660 .local_slots
661 .iter()
662 .map(|_| LocalSlot {
663 value: VmValue::Nil,
664 initialized: false,
665 synced: false,
666 })
667 .collect()
668 }
669
670 pub(crate) fn bind_param_slots(
671 slots: &mut [LocalSlot],
672 func: &crate::chunk::CompiledFunction,
673 args: &[VmValue],
674 synced: bool,
675 ) {
676 Self::bind_param_slots_args(slots, func, &super::CallArgs::Slice(args), synced);
677 }
678
679 pub(crate) fn bind_param_slots_args(
680 slots: &mut [LocalSlot],
681 func: &crate::chunk::CompiledFunction,
682 args: &super::CallArgs<'_>,
683 synced: bool,
684 ) {
685 let param_count = func.params.len();
686 for (i, _param) in func.params.iter().enumerate() {
687 if i >= slots.len() {
688 break;
689 }
690 if func.has_rest_param && i == param_count - 1 {
691 let rest_args = args.to_vec_from(i);
692 slots[i].value = VmValue::List(std::sync::Arc::new(rest_args));
693 slots[i].initialized = true;
694 slots[i].synced = synced;
695 } else if let Some(arg) = args.get(i) {
696 slots[i].value = arg.clone();
697 slots[i].initialized = true;
698 slots[i].synced = synced;
699 }
700 }
701 }
702
703 pub(crate) fn visible_variables(&self) -> crate::value::DictMap {
704 let mut vars = self.env.all_variables();
705 let Some(frame) = self.frames.last() else {
706 return vars;
707 };
708 for (slot, info) in frame.local_slots.iter().zip(frame.chunk.local_slots.iter()) {
709 if slot.initialized && info.scope_depth <= frame.local_scope_depth {
710 vars.insert(crate::value::intern_key(&info.name), slot.value.clone());
711 }
712 }
713 vars
714 }
715
716 pub(crate) fn sync_current_frame_locals_to_env(&mut self) {
717 let frames = &mut self.frames;
718 let env = &mut self.env;
719 let Some(frame) = frames.last_mut() else {
720 return;
721 };
722 let local_scope_base = frame.local_scope_base;
723 let local_scope_depth = frame.local_scope_depth;
724 for (slot, info) in frame
725 .local_slots
726 .iter_mut()
727 .zip(frame.chunk.local_slots.iter())
728 {
729 if slot.initialized && !slot.synced && info.scope_depth <= local_scope_depth {
730 slot.synced = true;
731 let scope_idx = local_scope_base + info.scope_depth;
732 while env.scopes.len() <= scope_idx {
733 env.push_scope();
734 }
735 Arc::make_mut(&mut env.scopes[scope_idx].vars).insert(
739 info.name.clone(),
740 crate::value::Binding::Value {
741 value: slot.value.clone(),
742 mutable: info.mutable,
743 },
744 );
745 }
746 }
747 }
748
749 pub(crate) fn closure_call_env_for_current_frame(
750 &self,
751 closure: &crate::value::VmClosure,
752 ) -> VmEnv {
753 if closure.module_state().is_some() {
754 return closure.env.cloned_for_call();
755 }
756 let call_env = Self::closure_call_env(&self.env, closure);
757 if !closure.func.chunk.references_outer_names {
762 return call_env;
763 }
764 let mut call_env = call_env;
765 let Some(frame) = self.frames.last() else {
766 return call_env;
767 };
768 for (slot, info) in frame
769 .local_slots
770 .iter()
771 .zip(frame.chunk.local_slots.iter())
772 .filter(|(slot, info)| slot.initialized && info.scope_depth <= frame.local_scope_depth)
773 {
774 if matches!(slot.value, VmValue::Closure(_)) && !call_env.contains(&info.name) {
775 let _ = call_env.define(&info.name, slot.value.clone(), info.mutable);
776 }
777 }
778 call_env
779 }
780
781 pub(crate) fn active_local_slot_value(&self, name: &str) -> Option<VmValue> {
782 let frame = self.frames.last()?;
783 let idx = self.active_local_slot_index(name)?;
784 frame.local_slots.get(idx).map(|slot| slot.value.clone())
785 }
786
787 pub(crate) fn active_local_slot_index(&self, name: &str) -> Option<usize> {
792 let frame = self.frames.last()?;
793 for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
794 if info.name == name && info.scope_depth <= frame.local_scope_depth {
795 if let Some(slot) = frame.local_slots.get(idx) {
796 if slot.initialized {
797 return Some(idx);
798 }
799 }
800 }
801 }
802 None
803 }
804
805 pub(crate) fn assign_active_local_slot(
806 &mut self,
807 name: &str,
808 value: VmValue,
809 debug: bool,
810 ) -> Result<bool, VmError> {
811 let Some(frame) = self.frames.last_mut() else {
812 return Ok(false);
813 };
814 for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
815 if info.name == name && info.scope_depth <= frame.local_scope_depth {
816 if !debug && !info.mutable {
817 return Err(VmError::ImmutableAssignment(name.to_string()));
818 }
819 if let Some(slot) = frame.local_slots.get_mut(idx) {
820 crate::value::recursion::dismantle(std::mem::replace(&mut slot.value, value));
821 slot.initialized = true;
822 slot.synced = false;
823 return Ok(true);
824 }
825 }
826 }
827 Ok(false)
828 }
829
830 pub fn new() -> Self {
831 crate::initialize_runtime_assets();
832 Self {
833 stack: Vec::with_capacity(256),
834 env: VmEnv::new(),
835 output: String::new(),
836 builtins: Arc::new(BTreeMap::new()),
837 async_builtins: Arc::new(BTreeMap::new()),
838 builtin_metadata: Arc::new(BTreeMap::new()),
839 builtins_by_id: Arc::new(HashMap::new()),
840 builtin_id_collisions: Arc::new(HashSet::new()),
841 iterators: Vec::new(),
842 frames: Vec::new(),
843 exception_handlers: Vec::new(),
844 spawned_tasks: BTreeMap::new(),
845 process_exit_request: Arc::new(ProcessExitRequest::new()),
846 sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
847 shared_state_runtime: Arc::new(crate::shared_state::VmSharedStateRuntime::new()),
848 inline_cache_sets: Vec::new(),
849 inline_cache_set_by_chunk: HashMap::new(),
850 pool_registry: crate::stdlib::pool::new_pool_registry(),
851 llm_mock_context: crate::llm::mock::LlmMockContext::for_new_vm(),
852 package_snapshot_registry: Arc::new(Default::default()),
853 wait_for_graph: Arc::new(crate::wait_for_graph::VmWaitForGraph::new()),
854 held_sync_guards: Vec::new(),
855 inherited_held_keys: Arc::new(Vec::new()),
856 task_scopes: Vec::new(),
857 task_counter: 0,
858 runtime_context_counter: 0,
859 runtime_context: crate::runtime_context::RuntimeContext::root(),
860 deadlines: Vec::new(),
861 execution_deadline: super::execution::new_execution_deadline_state(None),
862 breakpoints: BTreeMap::new(),
863 function_breakpoints: std::collections::BTreeSet::new(),
864 pending_function_bp: None,
865 step_mode: false,
866 step_frame_depth: 0,
867 stopped: false,
868 last_line: 0,
869 source_dir: None,
870 package_execution_guard: None,
871 imported_paths: Vec::new(),
872 deferred_cyclic_imports: Vec::new(),
873 module_cache: Arc::new(BTreeMap::new()),
874 prepared_module_cache: crate::PreparedModuleCache::default(),
875 module_phase_recorder: None,
876 lazy_callable_modules: Arc::new(crate::value::VmMutex::new(BTreeMap::new())),
877 source_cache: Arc::new(BTreeMap::new()),
878 source_file: None,
879 source_text: None,
880 coverage: crate::coverage::for_primary(None),
881 bridge: None,
882 denied_builtins: Arc::new(HashSet::new()),
883 cancel_token: None,
884 interrupt_signal_token: None,
885 cancel_grace_instructions_remaining: None,
886 interrupt_handlers: Vec::new(),
887 next_interrupt_handle: 1,
888 pending_interrupt_signal: None,
889 interrupted: false,
890 dispatching_interrupt: false,
891 interrupt_handler_deadline: None,
892 error_stack_trace: Vec::new(),
893 yield_sender: None,
894 project_root: None,
895 globals: Arc::new(crate::value::DictMap::new()),
896 debug_hook: None,
897 runtime_limits: RuntimeLimits::default(),
898 }
899 }
900
901 pub fn baseline(&self) -> VmBaseline {
902 VmBaseline::from_vm(self)
903 }
904
905 pub fn set_prepared_module_cache(&mut self, cache: crate::PreparedModuleCache) {
908 self.prepared_module_cache = cache;
909 }
910
911 pub fn runtime_limits(&self) -> RuntimeLimits {
913 self.runtime_limits
914 }
915
916 pub fn runtime_limit_report(&self) -> crate::RuntimeLimitsReport {
918 self.runtime_limits.report()
919 }
920
921 #[inline]
935 pub(crate) fn debugger_attached(&self) -> bool {
936 self.debug_hook.is_some()
937 || !self.breakpoints.is_empty()
938 || !self.function_breakpoints.is_empty()
939 }
940
941 pub fn set_bridge(&mut self, bridge: Arc<crate::bridge::HostBridge>) {
943 self.bridge = Some(bridge);
944 }
945
946 pub fn set_denied_builtins(&mut self, denied: HashSet<String>) {
949 self.denied_builtins = Arc::new(denied);
950 }
951
952 pub fn set_source_info(&mut self, file: &str, text: &str) {
954 self.source_file = Some(file.to_string());
955 self.source_text = Some(text.to_string());
956 if let Some(cov) = self.coverage.as_mut() {
957 cov.set_primary_file(file);
958 }
959 Arc::make_mut(&mut self.source_cache)
960 .insert(std::path::PathBuf::from(file), text.to_string());
961 }
962
963 pub fn start(&mut self, chunk: &Chunk) -> Result<(), VmError> {
965 self.ensure_execution_available()?;
966 let debugger = self.debugger_attached();
973 let initial_env = if debugger {
974 Some(self.env.clone())
975 } else {
976 None
977 };
978 let initial_local_slots = if debugger {
979 Some(Self::fresh_local_slots(chunk))
980 } else {
981 None
982 };
983 let chunk = Arc::new(chunk.clone());
984 let local_slots = Self::fresh_local_slots(&chunk);
985 let inline_cache_set = self.inline_cache_set_index_for_chunk(&chunk);
986 self.frames.push(CallFrame {
987 chunk,
988 inline_cache_set,
989 ip: 0,
990 stack_base: self.stack.len(),
991 saved_env: self.env.clone(),
992 initial_env,
993 initial_local_slots,
994 saved_iterator_depth: self.iterators.len(),
995 fn_name: String::new(),
996 argc: 0,
997 saved_source_dir: None,
998 module_functions: None,
999 module_state: None,
1000 local_slots,
1001 local_scope_base: self.env.scope_depth().saturating_sub(1),
1002 local_scope_depth: 0,
1003 });
1004 Ok(())
1005 }
1006
1007 pub(crate) fn child_vm(&self) -> Vm {
1010 Vm {
1011 stack: Vec::with_capacity(64),
1012 env: self.env.clone(),
1013 output: String::new(),
1014 builtins: Arc::clone(&self.builtins),
1015 async_builtins: Arc::clone(&self.async_builtins),
1016 builtin_metadata: Arc::clone(&self.builtin_metadata),
1017 builtins_by_id: Arc::clone(&self.builtins_by_id),
1018 builtin_id_collisions: Arc::clone(&self.builtin_id_collisions),
1019 iterators: Vec::new(),
1020 frames: Vec::new(),
1021 exception_handlers: Vec::new(),
1022 spawned_tasks: BTreeMap::new(),
1023 process_exit_request: Arc::clone(&self.process_exit_request),
1024 sync_runtime: self.sync_runtime.clone(),
1025 shared_state_runtime: self.shared_state_runtime.clone(),
1026 inline_cache_sets: Vec::new(),
1027 inline_cache_set_by_chunk: HashMap::new(),
1028 pool_registry: self.pool_registry.clone(),
1029 llm_mock_context: self.llm_mock_context.clone(),
1030 package_snapshot_registry: self.package_snapshot_registry.clone(),
1031 wait_for_graph: self.wait_for_graph.clone(),
1032 held_sync_guards: Vec::new(),
1033 inherited_held_keys: Arc::new(Vec::new()),
1034 task_scopes: Vec::new(),
1035 task_counter: 0,
1036 runtime_context_counter: self.runtime_context_counter,
1037 runtime_context: self.runtime_context.clone(),
1038 deadlines: self.deadlines.clone(),
1039 execution_deadline: self.execution_deadline.fork(),
1040 breakpoints: BTreeMap::new(),
1041 function_breakpoints: std::collections::BTreeSet::new(),
1042 pending_function_bp: None,
1043 step_mode: false,
1044 step_frame_depth: 0,
1045 stopped: false,
1046 last_line: 0,
1047 source_dir: self.source_dir.clone(),
1048 package_execution_guard: self.package_execution_guard.clone(),
1049 imported_paths: Vec::new(),
1050 deferred_cyclic_imports: Vec::new(),
1051 module_cache: Arc::clone(&self.module_cache),
1052 prepared_module_cache: self.prepared_module_cache.clone(),
1053 module_phase_recorder: self.module_phase_recorder.clone(),
1054 lazy_callable_modules: Arc::clone(&self.lazy_callable_modules),
1055 source_cache: Arc::clone(&self.source_cache),
1056 source_file: self.source_file.clone(),
1057 source_text: self.source_text.clone(),
1058 coverage: crate::coverage::for_primary(self.source_file.as_deref()),
1059 bridge: self.bridge.clone(),
1060 denied_builtins: Arc::clone(&self.denied_builtins),
1061 cancel_token: self.cancel_token.clone(),
1062 interrupt_signal_token: self.interrupt_signal_token.clone(),
1063 cancel_grace_instructions_remaining: None,
1064 interrupt_handlers: Vec::new(),
1065 next_interrupt_handle: 1,
1066 pending_interrupt_signal: None,
1067 interrupted: self.interrupted,
1068 dispatching_interrupt: false,
1069 interrupt_handler_deadline: None,
1070 error_stack_trace: Vec::new(),
1071 yield_sender: None,
1072 project_root: self.project_root.clone(),
1073 globals: Arc::clone(&self.globals),
1074 debug_hook: None,
1075 runtime_limits: self.runtime_limits,
1076 }
1077 }
1078
1079 pub(crate) fn child_vm_for_host(&self) -> Vm {
1082 self.child_vm()
1083 }
1084
1085 pub(crate) fn request_process_exit(&self, code: i32) {
1086 self.process_exit_request.request(code);
1087 }
1088
1089 pub(crate) fn requested_process_exit(&self) -> Option<i32> {
1090 self.process_exit_request.code()
1091 }
1092
1093 pub(crate) fn cancel_spawned_tasks(&mut self) {
1097 for (_, task) in std::mem::take(&mut self.spawned_tasks) {
1098 task.cancel_token
1099 .store(true, std::sync::atomic::Ordering::SeqCst);
1100 task.handle.abort();
1101 }
1102 }
1103
1104 pub fn set_source_dir(&mut self, dir: &std::path::Path) {
1107 let dir = crate::stdlib::process::normalize_context_path(dir);
1108 self.source_dir = Some(dir.clone());
1109 crate::stdlib::set_thread_source_dir(&dir);
1110 if self.project_root.is_none() {
1112 self.project_root = crate::stdlib::process::find_project_root(&dir);
1113 }
1114 }
1115
1116 pub fn set_project_root(&mut self, root: &std::path::Path) {
1119 self.project_root = Some(root.to_path_buf());
1120 }
1121
1122 pub(crate) fn explicit_project_root(&self) -> Option<&std::path::Path> {
1125 self.project_root.as_deref()
1126 }
1127
1128 pub fn project_root(&self) -> Option<&std::path::Path> {
1130 self.project_root.as_deref().or(self.source_dir.as_deref())
1131 }
1132
1133 pub fn builtin_names(&self) -> Vec<String> {
1135 let mut names: Vec<String> = self.builtins.keys().cloned().collect();
1136 names.extend(self.async_builtins.keys().cloned());
1137 names
1138 }
1139
1140 pub fn builtin_metadata(&self) -> Vec<VmBuiltinMetadata> {
1142 self.builtin_metadata.values().cloned().collect()
1143 }
1144
1145 pub fn builtin_metadata_for(&self, name: &str) -> Option<&VmBuiltinMetadata> {
1147 self.builtin_metadata.get(name)
1148 }
1149
1150 pub fn set_global(&mut self, name: &str, value: VmValue) {
1153 Arc::make_mut(&mut self.globals).insert(crate::value::intern_key(name), value);
1154 }
1155
1156 pub fn global(&self, name: &str) -> Option<&VmValue> {
1162 self.globals.get(name)
1163 }
1164
1165 pub fn set_harness(&mut self, harness: crate::harness::Harness) {
1171 self.set_global("harness", harness.into_vm_value());
1172 }
1173
1174 pub fn output(&self) -> &str {
1176 &self.output
1177 }
1178
1179 pub fn take_output(&mut self) -> String {
1183 std::mem::take(&mut self.output)
1184 }
1185
1186 pub fn append_output(&mut self, text: &str) {
1190 self.output.push_str(text);
1191 }
1192
1193 pub(crate) fn pop(&mut self) -> Result<VmValue, VmError> {
1194 self.stack.pop().ok_or(VmError::StackUnderflow)
1195 }
1196
1197 pub(crate) fn peek(&self) -> Result<&VmValue, VmError> {
1198 self.stack.last().ok_or(VmError::StackUnderflow)
1199 }
1200
1201 pub(crate) fn const_str(c: &Constant) -> Result<&str, VmError> {
1202 match c {
1203 Constant::String(s) => Ok(s.as_str()),
1204 _ => Err(VmError::TypeError("expected string constant".into())),
1205 }
1206 }
1207
1208 pub(crate) fn release_sync_guards_for_current_scope(&mut self) {
1209 let depth = self.env.scope_depth();
1210 self.held_sync_guards
1211 .retain(|guard| guard.env_scope_depth < depth);
1212 self.cancel_task_scopes_where(|s| s.env_scope_depth >= depth);
1215 }
1216
1217 pub(crate) fn release_sync_guards_after_unwind(
1218 &mut self,
1219 frame_depth: usize,
1220 env_scope_depth: usize,
1221 ) {
1222 self.held_sync_guards.retain(|guard| {
1223 guard.frame_depth <= frame_depth && guard.env_scope_depth <= env_scope_depth
1224 });
1225 self.cancel_task_scopes_where(|s| {
1228 !(s.frame_depth <= frame_depth && s.env_scope_depth <= env_scope_depth)
1229 });
1230 }
1231
1232 pub(crate) fn release_sync_guards_for_frame(&mut self, frame_depth: usize) {
1233 self.held_sync_guards
1234 .retain(|guard| guard.frame_depth != frame_depth);
1235 self.cancel_task_scopes_where(|s| s.frame_depth == frame_depth);
1238 }
1239
1240 pub(crate) fn adopt_sync_permit_for_current_scope(
1241 &mut self,
1242 permit: crate::value::VmSyncPermitHandle,
1243 ) {
1244 if permit.is_released()
1245 || self
1246 .held_sync_guards
1247 .iter()
1248 .any(|guard| guard._permit.same_lease(&permit))
1249 {
1250 return;
1251 }
1252 self.held_sync_guards
1253 .push(crate::synchronization::VmSyncHeldGuard {
1254 _permit: permit,
1255 frame_depth: self.frames.len(),
1256 env_scope_depth: self.env.scope_depth(),
1257 });
1258 }
1259
1260 pub(crate) fn deregister_task_from_scopes(&mut self, id: &str) {
1263 for scope in &mut self.task_scopes {
1264 scope.task_ids.retain(|t| t != id);
1265 }
1266 }
1267
1268 fn cancel_task_scopes_where<F: Fn(&TaskScope) -> bool>(&mut self, doomed: F) {
1271 let mut i = 0;
1272 while i < self.task_scopes.len() {
1273 if doomed(&self.task_scopes[i]) {
1274 let scope = self.task_scopes.remove(i);
1275 for id in &scope.task_ids {
1276 if let Some(task) = self.spawned_tasks.remove(id) {
1277 task.cancel_token
1278 .store(true, std::sync::atomic::Ordering::SeqCst);
1279 task.handle.abort();
1280 }
1281 }
1282 } else {
1283 i += 1;
1284 }
1285 }
1286 }
1287
1288 pub(crate) fn held_permits_for(&self, kind: &str, key: &str) -> u32 {
1292 let own: u32 = self
1293 .held_sync_guards
1294 .iter()
1295 .filter(|guard| {
1296 !guard._permit.is_released()
1297 && guard._permit.kind() == kind
1298 && guard._permit.key() == key
1299 })
1300 .map(|guard| guard._permit.permits())
1301 .sum();
1302 let inherited: u32 = self
1303 .inherited_held_keys
1304 .iter()
1305 .filter(|held| held.kind == kind && held.key == key)
1306 .map(|held| held.permits)
1307 .sum();
1308 own + inherited
1309 }
1310
1311 pub(crate) fn combined_held_keys(&self) -> Vec<crate::synchronization::VmSyncHeldKey> {
1314 let mut keys: Vec<crate::synchronization::VmSyncHeldKey> = self
1315 .held_sync_guards
1316 .iter()
1317 .filter_map(|guard| crate::synchronization::VmSyncHeldKey::from_permit(&guard._permit))
1318 .collect();
1319 keys.extend(self.inherited_held_keys.iter().cloned());
1320 keys
1321 }
1322
1323 pub(crate) fn child_vm_inline(&self) -> Vm {
1329 let mut child = self.child_vm();
1330 child.inherited_held_keys = Arc::new(self.combined_held_keys());
1331 child
1332 }
1333}
1334
1335impl Drop for Vm {
1336 fn drop(&mut self) {
1337 if let Some(coverage) = self.coverage.take() {
1338 crate::coverage::merge_into_global(coverage);
1339 }
1340 self.cancel_spawned_tasks();
1341 }
1342}
1343
1344impl Default for Vm {
1345 fn default() -> Self {
1346 Self::new()
1347 }
1348}
1349
1350#[cfg(test)]
1351mod tests {
1352
1353 use super::*;
1354
1355 #[test]
1356 fn vm_construction_initializes_shared_secret_patterns() {
1357 let _vm = Vm::new();
1358 assert!(crate::secret_patterns::default_secret_patterns_initialized());
1359 }
1360
1361 fn baseline_with_stdlib(source: &str) -> VmBaseline {
1362 let mut vm = Vm::new();
1363 crate::register_vm_stdlib(&mut vm);
1364 vm.set_source_info("baseline_test.harn", source);
1365 vm.set_global(
1366 "stable_global",
1367 VmValue::String(arcstr::ArcStr::from("baseline")),
1368 );
1369 vm.baseline()
1370 }
1371
1372 #[test]
1373 fn vm_baseline_instantiates_clean_mutable_execution_state() {
1374 let baseline = baseline_with_stdlib("pipeline main() { __io_println(stable_global) }");
1375
1376 let mut dirty = baseline.instantiate();
1377 dirty.stack.push(VmValue::Int(42));
1378 dirty.output.push_str("dirty");
1379 dirty.task_counter = 9;
1380 dirty.runtime_context_counter = 7;
1381 dirty
1382 .error_stack_trace
1383 .push(("main".to_string(), 1, 1, None));
1384
1385 let clean = baseline.instantiate();
1386 assert!(clean.stack.is_empty());
1387 assert!(clean.output.is_empty());
1388 assert!(clean.frames.is_empty());
1389 assert!(clean.exception_handlers.is_empty());
1390 assert!(clean.spawned_tasks.is_empty());
1391 assert!(clean.held_sync_guards.is_empty());
1392 assert_eq!(clean.task_counter, 0);
1393 assert_eq!(clean.runtime_context_counter, 0);
1394 assert!(clean.deadlines.is_empty());
1395 assert!(clean.cancel_token.is_none());
1396 assert!(clean.interrupt_handlers.is_empty());
1397 assert!(clean.error_stack_trace.is_empty());
1398 assert!(clean.bridge.is_none());
1399 assert!(clean
1400 .globals
1401 .get("stable_global")
1402 .is_some_and(|value| value.display() == "baseline"));
1403 }
1404
1405 #[tokio::test]
1406 async fn inline_child_inherits_held_lock_keys_but_concurrent_child_does_not() {
1407 let mut parent = Vm::new();
1408 let permit = parent
1409 .sync_runtime
1410 .acquire("mutex", "v:test", 1, 1, None, None)
1411 .await
1412 .unwrap()
1413 .unwrap();
1414 parent
1415 .held_sync_guards
1416 .push(crate::synchronization::VmSyncHeldGuard {
1417 _permit: permit,
1418 frame_depth: 0,
1419 env_scope_depth: 0,
1420 });
1421 assert_eq!(parent.held_permits_for("mutex", "v:test"), 1);
1422
1423 let inline = parent.child_vm_inline();
1428 assert_eq!(inline.held_permits_for("mutex", "v:test"), 1);
1429 assert_eq!(
1430 inline.child_vm_inline().held_permits_for("mutex", "v:test"),
1431 1
1432 );
1433
1434 let concurrent = parent.child_vm();
1438 assert_eq!(concurrent.held_permits_for("mutex", "v:test"), 0);
1439 }
1440
1441 #[test]
1442 fn vm_reports_effective_runtime_limits() {
1443 let vm = Vm::new();
1444
1445 assert_eq!(vm.runtime_limits(), RuntimeLimits::default());
1446 assert_eq!(
1447 vm.runtime_limit_report().entries.len(),
1448 crate::RUNTIME_LIMIT_DESCRIPTIONS.len()
1449 );
1450 assert_eq!(vm.child_vm().runtime_limits(), vm.runtime_limits());
1451 assert_eq!(
1452 vm.baseline().instantiate().runtime_limits(),
1453 vm.runtime_limits()
1454 );
1455 }
1456
1457 #[tokio::test(flavor = "current_thread")]
1458 async fn vm_baseline_rebinds_shared_state_builtins_per_instance() {
1459 let local = tokio::task::LocalSet::new();
1460 local
1461 .run_until(async {
1462 let source = r#"
1463pipeline main() {
1464 const cell = shared_cell({scope: "task_group", key: "turn", initial: 0})
1465 __io_println(shared_get(cell))
1466 shared_set(cell, shared_get(cell) + 1)
1467}"#;
1468 let chunk = crate::compile_source(source).expect("compile");
1469 let baseline = baseline_with_stdlib(source);
1470
1471 let mut first = baseline.instantiate();
1472 first.execute(&chunk).await.expect("first execute");
1473 assert_eq!(first.output(), "0\n");
1474
1475 let mut second = baseline.instantiate();
1476 second.execute(&chunk).await.expect("second execute");
1477 assert_eq!(
1478 second.output(),
1479 "0\n",
1480 "shared state created by the first VM must not leak into the next baseline instance"
1481 );
1482 })
1483 .await;
1484 }
1485}