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 pub(crate) recorded_effects: Option<&'static [harn_builtin_meta::EffectSpec]>,
365}
366
367pub struct Vm {
369 pub(crate) stack: Vec<VmValue>,
370 pub(crate) env: VmEnv,
371 pub(crate) output: String,
372 pub(crate) builtins: Arc<BTreeMap<String, VmBuiltinFn>>,
373 pub(crate) async_builtins: Arc<BTreeMap<String, VmAsyncBuiltinFn>>,
374 pub(crate) capability_methods:
378 Arc<BTreeMap<harn_builtin_meta::CapabilityId, BTreeMap<String, VmBuiltinDispatch>>>,
379 pub(crate) builtin_metadata: Arc<BTreeMap<String, VmBuiltinMetadata>>,
380 pub(crate) builtins_by_id: Arc<HashMap<BuiltinId, VmBuiltinEntry>>,
383 pub(crate) builtin_id_collisions: Arc<HashSet<BuiltinId>>,
386 pub(crate) iterators: Vec<IterState>,
388 pub(crate) frames: Vec<CallFrame>,
390 pub(crate) exception_handlers: Vec<ExceptionHandler>,
392 pub(crate) spawned_tasks: BTreeMap<String, VmTaskHandle>,
394 pub(crate) process_exit_request: Arc<ProcessExitRequest>,
396 pub(crate) sync_runtime: Arc<crate::synchronization::VmSyncRuntime>,
398 pub(crate) shared_state_runtime: Arc<crate::shared_state::VmSharedStateRuntime>,
400 pub(crate) inline_cache_sets: Vec<Vec<crate::chunk::InlineCacheEntry>>,
404 pub(crate) inline_cache_set_by_chunk: HashMap<u64, usize>,
405 pub(crate) pool_registry: Arc<crate::stdlib::pool::PoolRegistry>,
407 pub(crate) llm_mock_context: crate::llm::mock::LlmMockContext,
409 pub(crate) package_snapshot_registry: Arc<crate::stdlib::PackageSnapshotRegistry>,
413 pub(crate) wait_for_graph: Arc<crate::wait_for_graph::VmWaitForGraph>,
415 pub(crate) held_sync_guards: Vec<crate::synchronization::VmSyncHeldGuard>,
417 pub(crate) inherited_held_keys: Arc<Vec<crate::synchronization::VmSyncHeldKey>>,
425 pub(crate) task_scopes: Vec<TaskScope>,
431 pub(crate) task_counter: u64,
433 pub(crate) runtime_context_counter: u64,
435 pub(crate) runtime_context: crate::runtime_context::RuntimeContext,
437 pub(crate) deadlines: Vec<(Instant, usize)>,
439 pub(crate) execution_deadline: Arc<ExecutionDeadlineState>,
441 pub(crate) breakpoints: BTreeMap<String, std::collections::BTreeSet<usize>>,
446 pub(crate) function_breakpoints: std::collections::BTreeSet<String>,
452 pub(crate) pending_function_bp: Option<String>,
457 pub(crate) step_mode: bool,
459 pub(crate) step_frame_depth: usize,
461 pub(crate) stopped: bool,
463 pub(crate) last_line: usize,
465 pub(crate) source_dir: Option<std::path::PathBuf>,
467 pub(crate) package_execution_guard:
469 Option<Arc<harn_modules::package_execution::PackageExecutionGuard>>,
470 pub(crate) imported_paths: Vec<std::path::PathBuf>,
472 pub(crate) deferred_cyclic_imports: Vec<super::modules::DeferredCyclicImport>,
476 pub(crate) module_cache: ModuleCache,
478 pub(crate) prepared_module_cache: crate::PreparedModuleCache,
481 pub(crate) module_provenance: crate::module_artifact::ModuleProvenance,
484 pub(crate) module_phase_recorder: Option<super::ModulePhaseRecorder>,
486 pub(crate) lazy_callable_modules: LazyCallableModuleCache,
490 pub(crate) source_cache: Arc<BTreeMap<std::path::PathBuf, Arc<str>>>,
494 pub(crate) graph_link_table: Option<Arc<crate::context_manifest::GraphLinkTable>>,
499 pub(crate) linked_program_repository:
503 Option<Arc<crate::linked_program::LinkedProgramRepository>>,
504 pub(crate) source_file: Option<String>,
506 pub(crate) source_text: Option<String>,
508 pub(crate) coverage: Option<crate::coverage::Coverage>,
511 pub(crate) bridge: Option<Arc<crate::bridge::HostBridge>>,
513 pub(crate) denied_builtins: Arc<HashSet<String>>,
515 pub(crate) cancel_token: Option<std::sync::Arc<std::sync::atomic::AtomicBool>>,
517 pub(crate) interrupt_signal_token: Option<std::sync::Arc<std::sync::Mutex<Option<String>>>>,
518 pub(crate) cancel_grace_instructions_remaining: Option<usize>,
523 pub(crate) interrupt_handlers: Vec<InterruptHandler>,
525 pub(crate) next_interrupt_handle: i64,
526 pub(crate) pending_interrupt_signal: Option<String>,
527 pub(crate) interrupted: bool,
528 pub(crate) dispatching_interrupt: bool,
529 pub(crate) interrupt_handler_deadline: Option<Instant>,
530 pub(crate) error_stack_trace: Vec<(String, usize, usize, Option<String>)>,
532 pub(crate) yield_sender: Option<tokio::sync::mpsc::Sender<Result<VmValue, VmError>>>,
535 pub(crate) project_root: Option<std::path::PathBuf>,
538 pub(crate) globals: Arc<crate::value::DictMap>,
541 pub(crate) root_harness: Option<VmValue>,
546 pub(crate) runtime_effects: crate::orchestration::RuntimeEffectState,
548 pub(crate) debug_hook: Option<parking_lot::Mutex<Box<DebugHook>>>,
550 pub(crate) runtime_limits: RuntimeLimits,
552}
553
554#[derive(Clone)]
562pub struct VmBaseline {
563 builtins: Arc<BTreeMap<String, VmBuiltinFn>>,
564 async_builtins: Arc<BTreeMap<String, VmAsyncBuiltinFn>>,
565 capability_methods:
566 Arc<BTreeMap<harn_builtin_meta::CapabilityId, BTreeMap<String, VmBuiltinDispatch>>>,
567 builtin_metadata: Arc<BTreeMap<String, VmBuiltinMetadata>>,
568 builtins_by_id: Arc<HashMap<BuiltinId, VmBuiltinEntry>>,
569 builtin_id_collisions: Arc<HashSet<BuiltinId>>,
570 source_dir: Option<std::path::PathBuf>,
571 source_file: Option<String>,
572 source_text: Option<String>,
573 project_root: Option<std::path::PathBuf>,
574 globals: Arc<crate::value::DictMap>,
575 root_harness: Option<VmValue>,
576 denied_builtins: Arc<HashSet<String>>,
577 prepared_module_cache: crate::PreparedModuleCache,
578 module_provenance: crate::module_artifact::ModuleProvenance,
579 graph_link_table: Option<Arc<crate::context_manifest::GraphLinkTable>>,
582 linked_program_repository: Option<Arc<crate::linked_program::LinkedProgramRepository>>,
583 runtime_limits: RuntimeLimits,
584}
585
586impl VmBaseline {
587 pub fn from_vm(vm: &Vm) -> Self {
588 Self {
589 builtins: Arc::clone(&vm.builtins),
590 async_builtins: Arc::clone(&vm.async_builtins),
591 capability_methods: Arc::clone(&vm.capability_methods),
592 builtin_metadata: Arc::clone(&vm.builtin_metadata),
593 builtins_by_id: Arc::clone(&vm.builtins_by_id),
594 builtin_id_collisions: Arc::clone(&vm.builtin_id_collisions),
595 source_dir: vm.source_dir.clone(),
596 source_file: vm.source_file.clone(),
597 source_text: vm.source_text.clone(),
598 project_root: vm.project_root.clone(),
599 globals: Arc::clone(&vm.globals),
600 root_harness: vm.root_harness.clone(),
601 denied_builtins: Arc::clone(&vm.denied_builtins),
602 prepared_module_cache: vm.prepared_module_cache.clone(),
603 module_provenance: vm.module_provenance,
604 graph_link_table: vm.graph_link_table.clone(),
605 linked_program_repository: vm.linked_program_repository.clone(),
606 runtime_limits: vm.runtime_limits,
607 }
608 }
609
610 pub fn instantiate(&self) -> Vm {
611 crate::initialize_runtime_assets();
612 let mut source_cache = BTreeMap::new();
613 if let (Some(file), Some(text)) = (&self.source_file, &self.source_text) {
614 source_cache.insert(std::path::PathBuf::from(file), Arc::from(text.as_str()));
615 }
616 if let Some(dir) = &self.source_dir {
617 crate::stdlib::set_thread_source_dir(dir);
618 }
619
620 let mut vm = Vm {
621 stack: Vec::with_capacity(256),
622 env: VmEnv::new(),
623 output: String::new(),
624 builtins: Arc::clone(&self.builtins),
625 async_builtins: Arc::clone(&self.async_builtins),
626 capability_methods: Arc::clone(&self.capability_methods),
627 builtin_metadata: Arc::clone(&self.builtin_metadata),
628 builtins_by_id: Arc::clone(&self.builtins_by_id),
629 builtin_id_collisions: Arc::clone(&self.builtin_id_collisions),
630 iterators: Vec::new(),
631 frames: Vec::new(),
632 exception_handlers: Vec::new(),
633 spawned_tasks: BTreeMap::new(),
634 process_exit_request: Arc::new(ProcessExitRequest::new()),
635 sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
636 shared_state_runtime: Arc::new(crate::shared_state::VmSharedStateRuntime::new()),
637 inline_cache_sets: Vec::new(),
638 inline_cache_set_by_chunk: HashMap::new(),
639 pool_registry: crate::stdlib::pool::new_pool_registry(),
640 llm_mock_context: crate::llm::mock::LlmMockContext::for_new_vm(),
641 package_snapshot_registry: Arc::new(Default::default()),
642 wait_for_graph: Arc::new(crate::wait_for_graph::VmWaitForGraph::new()),
643 held_sync_guards: Vec::new(),
644 inherited_held_keys: Arc::new(Vec::new()),
645 task_scopes: Vec::new(),
646 task_counter: 0,
647 runtime_context_counter: 0,
648 runtime_context: crate::runtime_context::RuntimeContext::root(),
649 deadlines: Vec::new(),
650 execution_deadline: super::execution::new_execution_deadline_state(None),
651 breakpoints: BTreeMap::new(),
652 function_breakpoints: std::collections::BTreeSet::new(),
653 pending_function_bp: None,
654 step_mode: false,
655 step_frame_depth: 0,
656 stopped: false,
657 last_line: 0,
658 source_dir: self.source_dir.clone(),
659 package_execution_guard: None,
660 imported_paths: Vec::new(),
661 deferred_cyclic_imports: Vec::new(),
662 module_cache: Arc::new(BTreeMap::new()),
663 prepared_module_cache: self.prepared_module_cache.clone(),
664 module_provenance: self.module_provenance,
665 module_phase_recorder: None,
666 lazy_callable_modules: Arc::new(crate::value::VmMutex::new(BTreeMap::new())),
667 source_cache: Arc::new(source_cache),
668 graph_link_table: self.graph_link_table.clone(),
669 linked_program_repository: self.linked_program_repository.clone(),
670 source_file: self.source_file.clone(),
671 source_text: self.source_text.clone(),
672 coverage: crate::coverage::for_primary(self.source_file.as_deref()),
673 bridge: None,
674 denied_builtins: Arc::clone(&self.denied_builtins),
675 cancel_token: None,
676 interrupt_signal_token: None,
677 cancel_grace_instructions_remaining: None,
678 interrupt_handlers: Vec::new(),
679 next_interrupt_handle: 1,
680 pending_interrupt_signal: None,
681 interrupted: false,
682 dispatching_interrupt: false,
683 interrupt_handler_deadline: None,
684 error_stack_trace: Vec::new(),
685 yield_sender: None,
686 project_root: self.project_root.clone(),
687 globals: Arc::clone(&self.globals),
688 root_harness: self.root_harness.clone(),
689 runtime_effects: crate::orchestration::RuntimeEffectState::fresh(),
690 debug_hook: None,
691 runtime_limits: self.runtime_limits,
692 };
693
694 crate::stdlib::rebind_execution_state_builtins(&mut vm);
695 vm
696 }
697}
698
699impl Vm {
700 pub(crate) fn ensure_execution_available(&self) -> Result<(), VmError> {
701 if self.execution_deadline.is_abandoned() {
702 return Err(VmError::AbandonedExecution);
703 }
704 Ok(())
705 }
706
707 pub(crate) fn fresh_local_slots(chunk: &Chunk) -> Vec<LocalSlot> {
708 chunk
709 .local_slots
710 .iter()
711 .map(|_| LocalSlot {
712 value: VmValue::Nil,
713 initialized: false,
714 synced: false,
715 })
716 .collect()
717 }
718
719 pub(crate) fn bind_param_slots(
720 slots: &mut [LocalSlot],
721 func: &crate::chunk::CompiledFunction,
722 args: &[VmValue],
723 synced: bool,
724 ) {
725 Self::bind_param_slots_args(slots, func, &super::CallArgs::Slice(args), synced);
726 }
727
728 pub(crate) fn bind_param_slots_args(
729 slots: &mut [LocalSlot],
730 func: &crate::chunk::CompiledFunction,
731 args: &super::CallArgs<'_>,
732 synced: bool,
733 ) {
734 let param_count = func.params.len();
735 for (i, _param) in func.params.iter().enumerate() {
736 if i >= slots.len() {
737 break;
738 }
739 if func.has_rest_param && i == param_count - 1 {
740 let rest_args = args.to_vec_from(i);
741 slots[i].value = VmValue::List(std::sync::Arc::new(rest_args));
742 slots[i].initialized = true;
743 slots[i].synced = synced;
744 } else if let Some(arg) = args.get(i) {
745 slots[i].value = arg.clone();
746 slots[i].initialized = true;
747 slots[i].synced = synced;
748 }
749 }
750 }
751
752 pub(crate) fn visible_variables(&self) -> crate::value::DictMap {
753 let mut vars = self.env.all_variables();
754 let Some(frame) = self.frames.last() else {
755 return vars;
756 };
757 for (slot, info) in frame.local_slots.iter().zip(frame.chunk.local_slots.iter()) {
758 if slot.initialized && info.scope_depth <= frame.local_scope_depth {
759 vars.insert(crate::value::intern_key(&info.name), slot.value.clone());
760 }
761 }
762 vars
763 }
764
765 pub(crate) fn sync_current_frame_locals_to_env(&mut self) {
766 let frames = &mut self.frames;
767 let env = &mut self.env;
768 let Some(frame) = frames.last_mut() else {
769 return;
770 };
771 let local_scope_base = frame.local_scope_base;
772 let local_scope_depth = frame.local_scope_depth;
773 for (slot, info) in frame
774 .local_slots
775 .iter_mut()
776 .zip(frame.chunk.local_slots.iter())
777 {
778 if slot.initialized && !slot.synced && info.scope_depth <= local_scope_depth {
779 slot.synced = true;
780 let scope_idx = local_scope_base + info.scope_depth;
781 while env.scopes.len() <= scope_idx {
782 env.push_scope();
783 }
784 Arc::make_mut(&mut env.scopes[scope_idx].vars).insert(
788 info.name.clone(),
789 crate::value::Binding::Value {
790 value: slot.value.clone(),
791 mutable: info.mutable,
792 },
793 );
794 }
795 }
796 }
797
798 pub(crate) fn closure_call_env_for_current_frame(
799 &self,
800 closure: &crate::value::VmClosure,
801 ) -> VmEnv {
802 if closure.module_state().is_some() {
803 return closure.env.cloned_for_call();
804 }
805 let call_env = Self::closure_call_env(&self.env, closure);
806 if !closure.func.chunk.references_outer_names {
811 return call_env;
812 }
813 let mut call_env = call_env;
814 let Some(frame) = self.frames.last() else {
815 return call_env;
816 };
817 for (slot, info) in frame
818 .local_slots
819 .iter()
820 .zip(frame.chunk.local_slots.iter())
821 .filter(|(slot, info)| slot.initialized && info.scope_depth <= frame.local_scope_depth)
822 {
823 if matches!(slot.value, VmValue::Closure(_)) && !call_env.contains(&info.name) {
824 let _ = call_env.define(&info.name, slot.value.clone(), info.mutable);
825 }
826 }
827 call_env
828 }
829
830 pub(crate) fn active_local_slot_value(&self, name: &str) -> Option<VmValue> {
831 let frame = self.frames.last()?;
832 let idx = self.active_local_slot_index(name)?;
833 frame.local_slots.get(idx).map(|slot| slot.value.clone())
834 }
835
836 pub(crate) fn active_local_slot_index(&self, name: &str) -> Option<usize> {
841 let frame = self.frames.last()?;
842 for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
843 if info.name == name && info.scope_depth <= frame.local_scope_depth {
844 if let Some(slot) = frame.local_slots.get(idx) {
845 if slot.initialized {
846 return Some(idx);
847 }
848 }
849 }
850 }
851 None
852 }
853
854 pub(crate) fn assign_active_local_slot(
855 &mut self,
856 name: &str,
857 value: VmValue,
858 debug: bool,
859 ) -> Result<bool, VmError> {
860 let Some(frame) = self.frames.last_mut() else {
861 return Ok(false);
862 };
863 for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
864 if info.name == name && info.scope_depth <= frame.local_scope_depth {
865 if !debug && !info.mutable {
866 return Err(VmError::ImmutableAssignment(name.to_string()));
867 }
868 if let Some(slot) = frame.local_slots.get_mut(idx) {
869 crate::value::recursion::dismantle(std::mem::replace(&mut slot.value, value));
870 slot.initialized = true;
871 slot.synced = false;
872 return Ok(true);
873 }
874 }
875 }
876 Ok(false)
877 }
878
879 pub fn new() -> Self {
880 crate::initialize_runtime_assets();
881 Self {
882 stack: Vec::with_capacity(256),
883 env: VmEnv::new(),
884 output: String::new(),
885 builtins: Arc::new(BTreeMap::new()),
886 async_builtins: Arc::new(BTreeMap::new()),
887 capability_methods: Arc::new(BTreeMap::new()),
888 builtin_metadata: Arc::new(BTreeMap::new()),
889 builtins_by_id: Arc::new(HashMap::new()),
890 builtin_id_collisions: Arc::new(HashSet::new()),
891 iterators: Vec::new(),
892 frames: Vec::new(),
893 exception_handlers: Vec::new(),
894 spawned_tasks: BTreeMap::new(),
895 process_exit_request: Arc::new(ProcessExitRequest::new()),
896 sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
897 shared_state_runtime: Arc::new(crate::shared_state::VmSharedStateRuntime::new()),
898 inline_cache_sets: Vec::new(),
899 inline_cache_set_by_chunk: HashMap::new(),
900 pool_registry: crate::stdlib::pool::new_pool_registry(),
901 llm_mock_context: crate::llm::mock::LlmMockContext::for_new_vm(),
902 package_snapshot_registry: Arc::new(Default::default()),
903 wait_for_graph: Arc::new(crate::wait_for_graph::VmWaitForGraph::new()),
904 held_sync_guards: Vec::new(),
905 inherited_held_keys: Arc::new(Vec::new()),
906 task_scopes: Vec::new(),
907 task_counter: 0,
908 runtime_context_counter: 0,
909 runtime_context: crate::runtime_context::RuntimeContext::root(),
910 deadlines: Vec::new(),
911 execution_deadline: super::execution::new_execution_deadline_state(None),
912 breakpoints: BTreeMap::new(),
913 function_breakpoints: std::collections::BTreeSet::new(),
914 pending_function_bp: None,
915 step_mode: false,
916 step_frame_depth: 0,
917 stopped: false,
918 last_line: 0,
919 source_dir: None,
920 package_execution_guard: None,
921 imported_paths: Vec::new(),
922 deferred_cyclic_imports: Vec::new(),
923 module_cache: Arc::new(BTreeMap::new()),
924 prepared_module_cache: crate::PreparedModuleCache::default(),
925 module_provenance: crate::module_artifact::ModuleProvenance::User,
926 module_phase_recorder: None,
927 lazy_callable_modules: Arc::new(crate::value::VmMutex::new(BTreeMap::new())),
928 source_cache: Arc::new(BTreeMap::new()),
929 graph_link_table: None,
930 linked_program_repository: None,
931 source_file: None,
932 source_text: None,
933 coverage: crate::coverage::for_primary(None),
934 bridge: None,
935 denied_builtins: Arc::new(HashSet::new()),
936 cancel_token: None,
937 interrupt_signal_token: None,
938 cancel_grace_instructions_remaining: None,
939 interrupt_handlers: Vec::new(),
940 next_interrupt_handle: 1,
941 pending_interrupt_signal: None,
942 interrupted: false,
943 dispatching_interrupt: false,
944 interrupt_handler_deadline: None,
945 error_stack_trace: Vec::new(),
946 yield_sender: None,
947 project_root: None,
948 globals: Arc::new(crate::value::DictMap::new()),
949 root_harness: None,
950 runtime_effects: crate::orchestration::RuntimeEffectState::fresh(),
951 debug_hook: None,
952 runtime_limits: RuntimeLimits::default(),
953 }
954 }
955
956 pub fn baseline(&self) -> VmBaseline {
957 VmBaseline::from_vm(self)
958 }
959
960 pub fn executed_effects(&self) -> Vec<crate::orchestration::EffectRecord> {
962 self.runtime_effects.snapshot()
963 }
964
965 pub fn clear_executed_effects(&mut self) {
967 self.runtime_effects.clear();
968 }
969
970 pub(crate) fn record_capability_effects(
971 &mut self,
972 capability: harn_builtin_meta::CapabilityId,
973 method: &str,
974 args: &[VmValue],
975 ) {
976 self.runtime_effects
977 .record_capability(capability, method, args);
978 }
979
980 pub(crate) fn record_builtin_contract_effects(&mut self, name: &str, args: &[VmValue]) {
981 let Some(entry) = crate::stdlib::recorded_effect_builtin_manifest_entry(name) else {
982 return;
983 };
984 self.record_builtin_effect_specs(entry.contract.effects, args);
985 }
986
987 pub(crate) fn record_builtin_effect_specs(
988 &mut self,
989 specs: &'static [harn_builtin_meta::EffectSpec],
990 args: &[VmValue],
991 ) {
992 self.runtime_effects.record_specs(specs, args);
993 }
994
995 pub fn set_prepared_module_cache(&mut self, cache: crate::PreparedModuleCache) {
998 self.prepared_module_cache = cache;
999 }
1000
1001 pub fn set_graph_link_table(
1009 &mut self,
1010 link_table: Option<Arc<crate::context_manifest::GraphLinkTable>>,
1011 ) {
1012 self.graph_link_table = link_table;
1013 }
1014
1015 pub fn set_linked_program_runtime(
1019 &mut self,
1020 runtime: &crate::linked_program::LinkedProgramRuntime,
1021 ) {
1022 self.linked_program_repository = Some(Arc::clone(&runtime.repository));
1023 self.graph_link_table = None;
1024 }
1025
1026 pub fn runtime_limits(&self) -> RuntimeLimits {
1028 self.runtime_limits
1029 }
1030
1031 pub fn runtime_limit_report(&self) -> crate::RuntimeLimitsReport {
1033 self.runtime_limits.report()
1034 }
1035
1036 #[inline]
1050 pub(crate) fn debugger_attached(&self) -> bool {
1051 self.debug_hook.is_some()
1052 || !self.breakpoints.is_empty()
1053 || !self.function_breakpoints.is_empty()
1054 }
1055
1056 pub fn set_bridge(&mut self, bridge: Arc<crate::bridge::HostBridge>) {
1058 self.bridge = Some(bridge);
1059 }
1060
1061 pub fn set_denied_builtins(&mut self, mut denied: HashSet<String>) {
1064 let denied_canonical_names = denied
1068 .iter()
1069 .filter_map(|name| crate::stdlib::builtin_manifest_entry(name))
1070 .map(|entry| entry.canonical_name)
1071 .collect::<HashSet<_>>();
1072 if !denied_canonical_names.is_empty() {
1073 denied.extend(
1074 crate::stdlib::all_builtin_manifest()
1075 .iter()
1076 .filter(|entry| denied_canonical_names.contains(entry.canonical_name))
1077 .map(|entry| entry.name.to_string()),
1078 );
1079 }
1080 self.denied_builtins = Arc::new(denied);
1081 }
1082
1083 pub fn set_source_info(&mut self, file: &str, text: &str) {
1085 self.source_file = Some(file.to_string());
1086 self.source_text = Some(text.to_string());
1087 if let Some(cov) = self.coverage.as_mut() {
1088 cov.set_primary_file(file);
1089 }
1090 Arc::make_mut(&mut self.source_cache)
1091 .insert(std::path::PathBuf::from(file), Arc::from(text));
1092 }
1093
1094 pub fn start(&mut self, chunk: &Chunk) -> Result<(), VmError> {
1096 self.ensure_execution_available()?;
1097 let debugger = self.debugger_attached();
1104 let initial_env = if debugger {
1105 Some(self.env.clone())
1106 } else {
1107 None
1108 };
1109 let initial_local_slots = if debugger {
1110 Some(Self::fresh_local_slots(chunk))
1111 } else {
1112 None
1113 };
1114 let chunk = Arc::new(chunk.clone());
1115 let local_slots = Self::fresh_local_slots(&chunk);
1116 let inline_cache_set = self.inline_cache_set_index_for_chunk(&chunk);
1117 self.frames.push(CallFrame {
1118 chunk,
1119 inline_cache_set,
1120 ip: 0,
1121 stack_base: self.stack.len(),
1122 saved_env: self.env.clone(),
1123 initial_env,
1124 initial_local_slots,
1125 saved_iterator_depth: self.iterators.len(),
1126 fn_name: String::new(),
1127 argc: 0,
1128 saved_source_dir: None,
1129 module_functions: None,
1130 module_state: None,
1131 local_slots,
1132 local_scope_base: self.env.scope_depth().saturating_sub(1),
1133 local_scope_depth: 0,
1134 });
1135 Ok(())
1136 }
1137
1138 pub(crate) fn child_vm(&self) -> Vm {
1141 Vm {
1142 stack: Vec::with_capacity(64),
1143 env: self.env.clone(),
1144 output: String::new(),
1145 builtins: Arc::clone(&self.builtins),
1146 async_builtins: Arc::clone(&self.async_builtins),
1147 capability_methods: Arc::clone(&self.capability_methods),
1148 builtin_metadata: Arc::clone(&self.builtin_metadata),
1149 builtins_by_id: Arc::clone(&self.builtins_by_id),
1150 builtin_id_collisions: Arc::clone(&self.builtin_id_collisions),
1151 iterators: Vec::new(),
1152 frames: Vec::new(),
1153 exception_handlers: Vec::new(),
1154 spawned_tasks: BTreeMap::new(),
1155 process_exit_request: Arc::clone(&self.process_exit_request),
1156 sync_runtime: self.sync_runtime.clone(),
1157 shared_state_runtime: self.shared_state_runtime.clone(),
1158 inline_cache_sets: Vec::new(),
1159 inline_cache_set_by_chunk: HashMap::new(),
1160 pool_registry: self.pool_registry.clone(),
1161 llm_mock_context: self.llm_mock_context.clone(),
1162 package_snapshot_registry: self.package_snapshot_registry.clone(),
1163 wait_for_graph: self.wait_for_graph.clone(),
1164 held_sync_guards: Vec::new(),
1165 inherited_held_keys: Arc::new(Vec::new()),
1166 task_scopes: Vec::new(),
1167 task_counter: 0,
1168 runtime_context_counter: self.runtime_context_counter,
1169 runtime_context: self.runtime_context.clone(),
1170 deadlines: self.deadlines.clone(),
1171 execution_deadline: self.execution_deadline.fork(),
1172 breakpoints: BTreeMap::new(),
1173 function_breakpoints: std::collections::BTreeSet::new(),
1174 pending_function_bp: None,
1175 step_mode: false,
1176 step_frame_depth: 0,
1177 stopped: false,
1178 last_line: 0,
1179 source_dir: self.source_dir.clone(),
1180 package_execution_guard: self.package_execution_guard.clone(),
1181 imported_paths: Vec::new(),
1182 deferred_cyclic_imports: Vec::new(),
1183 module_cache: Arc::clone(&self.module_cache),
1184 prepared_module_cache: self.prepared_module_cache.clone(),
1185 module_provenance: self.module_provenance,
1186 module_phase_recorder: self.module_phase_recorder.clone(),
1187 lazy_callable_modules: Arc::clone(&self.lazy_callable_modules),
1188 source_cache: Arc::clone(&self.source_cache),
1189 graph_link_table: self.graph_link_table.clone(),
1190 linked_program_repository: self.linked_program_repository.clone(),
1191 source_file: self.source_file.clone(),
1192 source_text: self.source_text.clone(),
1193 coverage: crate::coverage::for_primary(self.source_file.as_deref()),
1194 bridge: self.bridge.clone(),
1195 denied_builtins: Arc::clone(&self.denied_builtins),
1196 cancel_token: self.cancel_token.clone(),
1197 interrupt_signal_token: self.interrupt_signal_token.clone(),
1198 cancel_grace_instructions_remaining: None,
1199 interrupt_handlers: Vec::new(),
1200 next_interrupt_handle: 1,
1201 pending_interrupt_signal: None,
1202 interrupted: self.interrupted,
1203 dispatching_interrupt: false,
1204 interrupt_handler_deadline: None,
1205 error_stack_trace: Vec::new(),
1206 yield_sender: None,
1207 project_root: self.project_root.clone(),
1208 globals: Arc::clone(&self.globals),
1209 root_harness: self.root_harness.clone(),
1210 runtime_effects: crate::orchestration::RuntimeEffectState::with_shared_recorder(
1211 Arc::clone(&self.runtime_effects.recorder),
1212 ),
1213 debug_hook: None,
1214 runtime_limits: self.runtime_limits,
1215 }
1216 }
1217
1218 pub(crate) fn child_vm_for_host(&self) -> Vm {
1221 self.child_vm()
1222 }
1223
1224 pub(crate) fn interrupt_sources(
1229 &self,
1230 ) -> (
1231 Option<std::sync::Arc<std::sync::atomic::AtomicBool>>,
1232 Option<std::time::Instant>,
1233 ) {
1234 let scope_deadline = self.deadlines.last().map(|(deadline, _)| *deadline);
1235 let deadline = match (scope_deadline, self.interrupt_handler_deadline) {
1236 (Some(scope), Some(interrupt)) => Some(scope.min(interrupt)),
1237 (scope, interrupt) => scope.or(interrupt),
1238 };
1239 (self.cancel_token.clone(), deadline)
1240 }
1241
1242 pub(crate) fn request_process_exit(&self, code: i32) {
1243 self.process_exit_request.request(code);
1244 }
1245
1246 pub(crate) fn requested_process_exit(&self) -> Option<i32> {
1247 self.process_exit_request.code()
1248 }
1249
1250 pub(crate) fn cancel_spawned_tasks(&mut self) {
1254 for (_, task) in std::mem::take(&mut self.spawned_tasks) {
1255 task.cancel_token
1256 .store(true, std::sync::atomic::Ordering::SeqCst);
1257 task.handle.abort();
1258 }
1259 }
1260
1261 pub fn set_source_dir(&mut self, dir: &std::path::Path) {
1264 let dir = crate::stdlib::process::normalize_context_path(dir);
1265 self.source_dir = Some(dir.clone());
1266 crate::stdlib::set_thread_source_dir(&dir);
1267 if self.project_root.is_none() {
1269 self.project_root = crate::stdlib::process::find_project_root(&dir);
1270 }
1271 }
1272
1273 pub fn set_project_root(&mut self, root: &std::path::Path) {
1276 self.project_root = Some(root.to_path_buf());
1277 }
1278
1279 pub(crate) fn explicit_project_root(&self) -> Option<&std::path::Path> {
1282 self.project_root.as_deref()
1283 }
1284
1285 pub fn project_root(&self) -> Option<&std::path::Path> {
1287 self.project_root.as_deref().or(self.source_dir.as_deref())
1288 }
1289
1290 pub fn set_global(&mut self, name: &str, value: VmValue) {
1293 Arc::make_mut(&mut self.globals).insert(crate::value::intern_key(name), value);
1294 }
1295
1296 pub fn global(&self, name: &str) -> Option<&VmValue> {
1298 self.globals.get(name)
1299 }
1300
1301 pub fn set_harness(&mut self, harness: crate::harness::Harness) {
1305 self.root_harness = Some(harness.into_vm_value());
1306 }
1307
1308 pub(crate) fn harness(&self) -> Option<&crate::harness::VmHarness> {
1309 match self.root_harness.as_ref() {
1310 Some(VmValue::Harness(handle)) => Some(handle),
1311 _ => None,
1312 }
1313 }
1314
1315 pub fn root_harness_value(&self) -> Option<VmValue> {
1318 self.root_harness.clone()
1319 }
1320
1321 pub fn output(&self) -> &str {
1323 &self.output
1324 }
1325
1326 pub fn take_output(&mut self) -> String {
1330 std::mem::take(&mut self.output)
1331 }
1332
1333 pub fn append_output(&mut self, text: &str) {
1337 self.output.push_str(text);
1338 }
1339
1340 pub(crate) fn pop(&mut self) -> Result<VmValue, VmError> {
1341 self.stack.pop().ok_or(VmError::StackUnderflow)
1342 }
1343
1344 pub(crate) fn peek(&self) -> Result<&VmValue, VmError> {
1345 self.stack.last().ok_or(VmError::StackUnderflow)
1346 }
1347
1348 pub(crate) fn const_str(c: &Constant) -> Result<&str, VmError> {
1349 match c {
1350 Constant::String(s) => Ok(s.as_str()),
1351 _ => Err(VmError::TypeError("expected string constant".into())),
1352 }
1353 }
1354
1355 pub(crate) fn release_sync_guards_for_current_scope(&mut self) {
1356 let depth = self.env.scope_depth();
1357 self.held_sync_guards
1358 .retain(|guard| guard.env_scope_depth < depth);
1359 self.cancel_task_scopes_where(|s| s.env_scope_depth >= depth);
1362 }
1363
1364 pub(crate) fn release_sync_guards_after_unwind(
1365 &mut self,
1366 frame_depth: usize,
1367 env_scope_depth: usize,
1368 ) {
1369 self.held_sync_guards.retain(|guard| {
1370 guard.frame_depth <= frame_depth && guard.env_scope_depth <= env_scope_depth
1371 });
1372 self.cancel_task_scopes_where(|s| {
1375 !(s.frame_depth <= frame_depth && s.env_scope_depth <= env_scope_depth)
1376 });
1377 }
1378
1379 pub(crate) fn release_sync_guards_for_frame(&mut self, frame_depth: usize) {
1380 self.held_sync_guards
1381 .retain(|guard| guard.frame_depth != frame_depth);
1382 self.cancel_task_scopes_where(|s| s.frame_depth == frame_depth);
1385 }
1386
1387 pub(crate) fn adopt_sync_permit_for_current_scope(
1388 &mut self,
1389 permit: crate::value::VmSyncPermitHandle,
1390 ) {
1391 if permit.is_released()
1392 || self
1393 .held_sync_guards
1394 .iter()
1395 .any(|guard| guard._permit.same_lease(&permit))
1396 {
1397 return;
1398 }
1399 self.held_sync_guards
1400 .push(crate::synchronization::VmSyncHeldGuard {
1401 _permit: permit,
1402 frame_depth: self.frames.len(),
1403 env_scope_depth: self.env.scope_depth(),
1404 });
1405 }
1406
1407 pub(crate) fn deregister_task_from_scopes(&mut self, id: &str) {
1410 for scope in &mut self.task_scopes {
1411 scope.task_ids.retain(|t| t != id);
1412 }
1413 }
1414
1415 fn cancel_task_scopes_where<F: Fn(&TaskScope) -> bool>(&mut self, doomed: F) {
1418 let mut i = 0;
1419 while i < self.task_scopes.len() {
1420 if doomed(&self.task_scopes[i]) {
1421 let scope = self.task_scopes.remove(i);
1422 for id in &scope.task_ids {
1423 if let Some(task) = self.spawned_tasks.remove(id) {
1424 task.cancel_token
1425 .store(true, std::sync::atomic::Ordering::SeqCst);
1426 task.handle.abort();
1427 }
1428 }
1429 } else {
1430 i += 1;
1431 }
1432 }
1433 }
1434
1435 pub(crate) fn held_permits_for(&self, kind: &str, key: &str) -> u32 {
1439 let own: u32 = self
1440 .held_sync_guards
1441 .iter()
1442 .filter(|guard| {
1443 !guard._permit.is_released()
1444 && guard._permit.kind() == kind
1445 && guard._permit.key() == key
1446 })
1447 .map(|guard| guard._permit.permits())
1448 .sum();
1449 let inherited: u32 = self
1450 .inherited_held_keys
1451 .iter()
1452 .filter(|held| held.kind == kind && held.key == key)
1453 .map(|held| held.permits)
1454 .sum();
1455 own + inherited
1456 }
1457
1458 pub(crate) fn combined_held_keys(&self) -> Vec<crate::synchronization::VmSyncHeldKey> {
1461 let mut keys: Vec<crate::synchronization::VmSyncHeldKey> = self
1462 .held_sync_guards
1463 .iter()
1464 .filter_map(|guard| crate::synchronization::VmSyncHeldKey::from_permit(&guard._permit))
1465 .collect();
1466 keys.extend(self.inherited_held_keys.iter().cloned());
1467 keys
1468 }
1469
1470 pub(crate) fn child_vm_inline(&self) -> Vm {
1476 let mut child = self.child_vm();
1477 child.inherited_held_keys = Arc::new(self.combined_held_keys());
1478 child
1479 }
1480}
1481
1482impl Drop for Vm {
1483 fn drop(&mut self) {
1484 if let Some(coverage) = self.coverage.take() {
1485 crate::coverage::merge_into_global(coverage);
1486 }
1487 self.cancel_spawned_tasks();
1488 }
1489}
1490
1491impl Default for Vm {
1492 fn default() -> Self {
1493 Self::new()
1494 }
1495}
1496
1497#[cfg(test)]
1498#[path = "state_tests.rs"]
1499mod tests;