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_phase_recorder: Option<super::ModulePhaseRecorder>,
483 pub(crate) lazy_callable_modules: LazyCallableModuleCache,
487 pub(crate) source_cache: Arc<BTreeMap<std::path::PathBuf, Arc<str>>>,
491 pub(crate) graph_link_table: Option<Arc<crate::context_manifest::GraphLinkTable>>,
496 pub(crate) source_file: Option<String>,
498 pub(crate) source_text: Option<String>,
500 pub(crate) coverage: Option<crate::coverage::Coverage>,
503 pub(crate) bridge: Option<Arc<crate::bridge::HostBridge>>,
505 pub(crate) denied_builtins: Arc<HashSet<String>>,
507 pub(crate) cancel_token: Option<std::sync::Arc<std::sync::atomic::AtomicBool>>,
509 pub(crate) interrupt_signal_token: Option<std::sync::Arc<std::sync::Mutex<Option<String>>>>,
510 pub(crate) cancel_grace_instructions_remaining: Option<usize>,
515 pub(crate) interrupt_handlers: Vec<InterruptHandler>,
517 pub(crate) next_interrupt_handle: i64,
518 pub(crate) pending_interrupt_signal: Option<String>,
519 pub(crate) interrupted: bool,
520 pub(crate) dispatching_interrupt: bool,
521 pub(crate) interrupt_handler_deadline: Option<Instant>,
522 pub(crate) error_stack_trace: Vec<(String, usize, usize, Option<String>)>,
524 pub(crate) yield_sender: Option<tokio::sync::mpsc::Sender<Result<VmValue, VmError>>>,
527 pub(crate) project_root: Option<std::path::PathBuf>,
530 pub(crate) globals: Arc<crate::value::DictMap>,
533 pub(crate) root_harness: Option<VmValue>,
538 pub(crate) executed_effects:
542 Arc<Mutex<std::collections::BTreeSet<crate::orchestration::EffectRecord>>>,
543 pub(crate) debug_hook: Option<parking_lot::Mutex<Box<DebugHook>>>,
545 pub(crate) runtime_limits: RuntimeLimits,
547}
548
549#[derive(Clone)]
557pub struct VmBaseline {
558 builtins: Arc<BTreeMap<String, VmBuiltinFn>>,
559 async_builtins: Arc<BTreeMap<String, VmAsyncBuiltinFn>>,
560 capability_methods:
561 Arc<BTreeMap<harn_builtin_meta::CapabilityId, BTreeMap<String, VmBuiltinDispatch>>>,
562 builtin_metadata: Arc<BTreeMap<String, VmBuiltinMetadata>>,
563 builtins_by_id: Arc<HashMap<BuiltinId, VmBuiltinEntry>>,
564 builtin_id_collisions: Arc<HashSet<BuiltinId>>,
565 source_dir: Option<std::path::PathBuf>,
566 source_file: Option<String>,
567 source_text: Option<String>,
568 project_root: Option<std::path::PathBuf>,
569 globals: Arc<crate::value::DictMap>,
570 root_harness: Option<VmValue>,
571 denied_builtins: Arc<HashSet<String>>,
572 prepared_module_cache: crate::PreparedModuleCache,
573 graph_link_table: Option<Arc<crate::context_manifest::GraphLinkTable>>,
576 runtime_limits: RuntimeLimits,
577}
578
579impl VmBaseline {
580 pub fn from_vm(vm: &Vm) -> Self {
581 Self {
582 builtins: Arc::clone(&vm.builtins),
583 async_builtins: Arc::clone(&vm.async_builtins),
584 capability_methods: Arc::clone(&vm.capability_methods),
585 builtin_metadata: Arc::clone(&vm.builtin_metadata),
586 builtins_by_id: Arc::clone(&vm.builtins_by_id),
587 builtin_id_collisions: Arc::clone(&vm.builtin_id_collisions),
588 source_dir: vm.source_dir.clone(),
589 source_file: vm.source_file.clone(),
590 source_text: vm.source_text.clone(),
591 project_root: vm.project_root.clone(),
592 globals: Arc::clone(&vm.globals),
593 root_harness: vm.root_harness.clone(),
594 denied_builtins: Arc::clone(&vm.denied_builtins),
595 prepared_module_cache: vm.prepared_module_cache.clone(),
596 graph_link_table: vm.graph_link_table.clone(),
597 runtime_limits: vm.runtime_limits,
598 }
599 }
600
601 pub fn instantiate(&self) -> Vm {
602 crate::initialize_runtime_assets();
603 let mut source_cache = BTreeMap::new();
604 if let (Some(file), Some(text)) = (&self.source_file, &self.source_text) {
605 source_cache.insert(std::path::PathBuf::from(file), Arc::from(text.as_str()));
606 }
607 if let Some(dir) = &self.source_dir {
608 crate::stdlib::set_thread_source_dir(dir);
609 }
610
611 let mut vm = Vm {
612 stack: Vec::with_capacity(256),
613 env: VmEnv::new(),
614 output: String::new(),
615 builtins: Arc::clone(&self.builtins),
616 async_builtins: Arc::clone(&self.async_builtins),
617 capability_methods: Arc::clone(&self.capability_methods),
618 builtin_metadata: Arc::clone(&self.builtin_metadata),
619 builtins_by_id: Arc::clone(&self.builtins_by_id),
620 builtin_id_collisions: Arc::clone(&self.builtin_id_collisions),
621 iterators: Vec::new(),
622 frames: Vec::new(),
623 exception_handlers: Vec::new(),
624 spawned_tasks: BTreeMap::new(),
625 process_exit_request: Arc::new(ProcessExitRequest::new()),
626 sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
627 shared_state_runtime: Arc::new(crate::shared_state::VmSharedStateRuntime::new()),
628 inline_cache_sets: Vec::new(),
629 inline_cache_set_by_chunk: HashMap::new(),
630 pool_registry: crate::stdlib::pool::new_pool_registry(),
631 llm_mock_context: crate::llm::mock::LlmMockContext::for_new_vm(),
632 package_snapshot_registry: Arc::new(Default::default()),
633 wait_for_graph: Arc::new(crate::wait_for_graph::VmWaitForGraph::new()),
634 held_sync_guards: Vec::new(),
635 inherited_held_keys: Arc::new(Vec::new()),
636 task_scopes: Vec::new(),
637 task_counter: 0,
638 runtime_context_counter: 0,
639 runtime_context: crate::runtime_context::RuntimeContext::root(),
640 deadlines: Vec::new(),
641 execution_deadline: super::execution::new_execution_deadline_state(None),
642 breakpoints: BTreeMap::new(),
643 function_breakpoints: std::collections::BTreeSet::new(),
644 pending_function_bp: None,
645 step_mode: false,
646 step_frame_depth: 0,
647 stopped: false,
648 last_line: 0,
649 source_dir: self.source_dir.clone(),
650 package_execution_guard: None,
651 imported_paths: Vec::new(),
652 deferred_cyclic_imports: Vec::new(),
653 module_cache: Arc::new(BTreeMap::new()),
654 prepared_module_cache: self.prepared_module_cache.clone(),
655 module_phase_recorder: None,
656 lazy_callable_modules: Arc::new(crate::value::VmMutex::new(BTreeMap::new())),
657 source_cache: Arc::new(source_cache),
658 graph_link_table: self.graph_link_table.clone(),
659 source_file: self.source_file.clone(),
660 source_text: self.source_text.clone(),
661 coverage: crate::coverage::for_primary(self.source_file.as_deref()),
662 bridge: None,
663 denied_builtins: Arc::clone(&self.denied_builtins),
664 cancel_token: None,
665 interrupt_signal_token: None,
666 cancel_grace_instructions_remaining: None,
667 interrupt_handlers: Vec::new(),
668 next_interrupt_handle: 1,
669 pending_interrupt_signal: None,
670 interrupted: false,
671 dispatching_interrupt: false,
672 interrupt_handler_deadline: None,
673 error_stack_trace: Vec::new(),
674 yield_sender: None,
675 project_root: self.project_root.clone(),
676 globals: Arc::clone(&self.globals),
677 root_harness: self.root_harness.clone(),
678 executed_effects: Arc::new(Mutex::new(Default::default())),
679 debug_hook: None,
680 runtime_limits: self.runtime_limits,
681 };
682
683 crate::stdlib::rebind_execution_state_builtins(&mut vm);
684 vm
685 }
686}
687
688impl Vm {
689 pub(crate) fn ensure_execution_available(&self) -> Result<(), VmError> {
690 if self.execution_deadline.is_abandoned() {
691 return Err(VmError::AbandonedExecution);
692 }
693 Ok(())
694 }
695
696 pub(crate) fn fresh_local_slots(chunk: &Chunk) -> Vec<LocalSlot> {
697 chunk
698 .local_slots
699 .iter()
700 .map(|_| LocalSlot {
701 value: VmValue::Nil,
702 initialized: false,
703 synced: false,
704 })
705 .collect()
706 }
707
708 pub(crate) fn bind_param_slots(
709 slots: &mut [LocalSlot],
710 func: &crate::chunk::CompiledFunction,
711 args: &[VmValue],
712 synced: bool,
713 ) {
714 Self::bind_param_slots_args(slots, func, &super::CallArgs::Slice(args), synced);
715 }
716
717 pub(crate) fn bind_param_slots_args(
718 slots: &mut [LocalSlot],
719 func: &crate::chunk::CompiledFunction,
720 args: &super::CallArgs<'_>,
721 synced: bool,
722 ) {
723 let param_count = func.params.len();
724 for (i, _param) in func.params.iter().enumerate() {
725 if i >= slots.len() {
726 break;
727 }
728 if func.has_rest_param && i == param_count - 1 {
729 let rest_args = args.to_vec_from(i);
730 slots[i].value = VmValue::List(std::sync::Arc::new(rest_args));
731 slots[i].initialized = true;
732 slots[i].synced = synced;
733 } else if let Some(arg) = args.get(i) {
734 slots[i].value = arg.clone();
735 slots[i].initialized = true;
736 slots[i].synced = synced;
737 }
738 }
739 }
740
741 pub(crate) fn visible_variables(&self) -> crate::value::DictMap {
742 let mut vars = self.env.all_variables();
743 let Some(frame) = self.frames.last() else {
744 return vars;
745 };
746 for (slot, info) in frame.local_slots.iter().zip(frame.chunk.local_slots.iter()) {
747 if slot.initialized && info.scope_depth <= frame.local_scope_depth {
748 vars.insert(crate::value::intern_key(&info.name), slot.value.clone());
749 }
750 }
751 vars
752 }
753
754 pub(crate) fn sync_current_frame_locals_to_env(&mut self) {
755 let frames = &mut self.frames;
756 let env = &mut self.env;
757 let Some(frame) = frames.last_mut() else {
758 return;
759 };
760 let local_scope_base = frame.local_scope_base;
761 let local_scope_depth = frame.local_scope_depth;
762 for (slot, info) in frame
763 .local_slots
764 .iter_mut()
765 .zip(frame.chunk.local_slots.iter())
766 {
767 if slot.initialized && !slot.synced && info.scope_depth <= local_scope_depth {
768 slot.synced = true;
769 let scope_idx = local_scope_base + info.scope_depth;
770 while env.scopes.len() <= scope_idx {
771 env.push_scope();
772 }
773 Arc::make_mut(&mut env.scopes[scope_idx].vars).insert(
777 info.name.clone(),
778 crate::value::Binding::Value {
779 value: slot.value.clone(),
780 mutable: info.mutable,
781 },
782 );
783 }
784 }
785 }
786
787 pub(crate) fn closure_call_env_for_current_frame(
788 &self,
789 closure: &crate::value::VmClosure,
790 ) -> VmEnv {
791 if closure.module_state().is_some() {
792 return closure.env.cloned_for_call();
793 }
794 let call_env = Self::closure_call_env(&self.env, closure);
795 if !closure.func.chunk.references_outer_names {
800 return call_env;
801 }
802 let mut call_env = call_env;
803 let Some(frame) = self.frames.last() else {
804 return call_env;
805 };
806 for (slot, info) in frame
807 .local_slots
808 .iter()
809 .zip(frame.chunk.local_slots.iter())
810 .filter(|(slot, info)| slot.initialized && info.scope_depth <= frame.local_scope_depth)
811 {
812 if matches!(slot.value, VmValue::Closure(_)) && !call_env.contains(&info.name) {
813 let _ = call_env.define(&info.name, slot.value.clone(), info.mutable);
814 }
815 }
816 call_env
817 }
818
819 pub(crate) fn active_local_slot_value(&self, name: &str) -> Option<VmValue> {
820 let frame = self.frames.last()?;
821 let idx = self.active_local_slot_index(name)?;
822 frame.local_slots.get(idx).map(|slot| slot.value.clone())
823 }
824
825 pub(crate) fn active_local_slot_index(&self, name: &str) -> Option<usize> {
830 let frame = self.frames.last()?;
831 for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
832 if info.name == name && info.scope_depth <= frame.local_scope_depth {
833 if let Some(slot) = frame.local_slots.get(idx) {
834 if slot.initialized {
835 return Some(idx);
836 }
837 }
838 }
839 }
840 None
841 }
842
843 pub(crate) fn assign_active_local_slot(
844 &mut self,
845 name: &str,
846 value: VmValue,
847 debug: bool,
848 ) -> Result<bool, VmError> {
849 let Some(frame) = self.frames.last_mut() else {
850 return Ok(false);
851 };
852 for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
853 if info.name == name && info.scope_depth <= frame.local_scope_depth {
854 if !debug && !info.mutable {
855 return Err(VmError::ImmutableAssignment(name.to_string()));
856 }
857 if let Some(slot) = frame.local_slots.get_mut(idx) {
858 crate::value::recursion::dismantle(std::mem::replace(&mut slot.value, value));
859 slot.initialized = true;
860 slot.synced = false;
861 return Ok(true);
862 }
863 }
864 }
865 Ok(false)
866 }
867
868 pub fn new() -> Self {
869 crate::initialize_runtime_assets();
870 Self {
871 stack: Vec::with_capacity(256),
872 env: VmEnv::new(),
873 output: String::new(),
874 builtins: Arc::new(BTreeMap::new()),
875 async_builtins: Arc::new(BTreeMap::new()),
876 capability_methods: Arc::new(BTreeMap::new()),
877 builtin_metadata: Arc::new(BTreeMap::new()),
878 builtins_by_id: Arc::new(HashMap::new()),
879 builtin_id_collisions: Arc::new(HashSet::new()),
880 iterators: Vec::new(),
881 frames: Vec::new(),
882 exception_handlers: Vec::new(),
883 spawned_tasks: BTreeMap::new(),
884 process_exit_request: Arc::new(ProcessExitRequest::new()),
885 sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
886 shared_state_runtime: Arc::new(crate::shared_state::VmSharedStateRuntime::new()),
887 inline_cache_sets: Vec::new(),
888 inline_cache_set_by_chunk: HashMap::new(),
889 pool_registry: crate::stdlib::pool::new_pool_registry(),
890 llm_mock_context: crate::llm::mock::LlmMockContext::for_new_vm(),
891 package_snapshot_registry: Arc::new(Default::default()),
892 wait_for_graph: Arc::new(crate::wait_for_graph::VmWaitForGraph::new()),
893 held_sync_guards: Vec::new(),
894 inherited_held_keys: Arc::new(Vec::new()),
895 task_scopes: Vec::new(),
896 task_counter: 0,
897 runtime_context_counter: 0,
898 runtime_context: crate::runtime_context::RuntimeContext::root(),
899 deadlines: Vec::new(),
900 execution_deadline: super::execution::new_execution_deadline_state(None),
901 breakpoints: BTreeMap::new(),
902 function_breakpoints: std::collections::BTreeSet::new(),
903 pending_function_bp: None,
904 step_mode: false,
905 step_frame_depth: 0,
906 stopped: false,
907 last_line: 0,
908 source_dir: None,
909 package_execution_guard: None,
910 imported_paths: Vec::new(),
911 deferred_cyclic_imports: Vec::new(),
912 module_cache: Arc::new(BTreeMap::new()),
913 prepared_module_cache: crate::PreparedModuleCache::default(),
914 module_phase_recorder: None,
915 lazy_callable_modules: Arc::new(crate::value::VmMutex::new(BTreeMap::new())),
916 source_cache: Arc::new(BTreeMap::new()),
917 graph_link_table: None,
918 source_file: None,
919 source_text: None,
920 coverage: crate::coverage::for_primary(None),
921 bridge: None,
922 denied_builtins: Arc::new(HashSet::new()),
923 cancel_token: None,
924 interrupt_signal_token: None,
925 cancel_grace_instructions_remaining: None,
926 interrupt_handlers: Vec::new(),
927 next_interrupt_handle: 1,
928 pending_interrupt_signal: None,
929 interrupted: false,
930 dispatching_interrupt: false,
931 interrupt_handler_deadline: None,
932 error_stack_trace: Vec::new(),
933 yield_sender: None,
934 project_root: None,
935 globals: Arc::new(crate::value::DictMap::new()),
936 root_harness: None,
937 executed_effects: Arc::new(Mutex::new(Default::default())),
938 debug_hook: None,
939 runtime_limits: RuntimeLimits::default(),
940 }
941 }
942
943 pub fn baseline(&self) -> VmBaseline {
944 VmBaseline::from_vm(self)
945 }
946
947 pub fn executed_effects(&self) -> Vec<crate::orchestration::EffectRecord> {
949 self.executed_effects
950 .lock()
951 .expect("executed effect recorder poisoned")
952 .iter()
953 .cloned()
954 .collect()
955 }
956
957 pub fn clear_executed_effects(&self) {
959 self.executed_effects
960 .lock()
961 .expect("executed effect recorder poisoned")
962 .clear();
963 }
964
965 pub(crate) fn record_capability_effects(
966 &self,
967 capability: harn_builtin_meta::CapabilityId,
968 method: &str,
969 args: &[VmValue],
970 ) {
971 Self::record_capability_effects_into(&self.executed_effects, capability, method, args);
972 }
973
974 pub(crate) fn record_capability_effects_into(
975 recorder: &Arc<Mutex<std::collections::BTreeSet<crate::orchestration::EffectRecord>>>,
976 capability: harn_builtin_meta::CapabilityId,
977 method: &str,
978 args: &[VmValue],
979 ) {
980 let Some(entry) = crate::stdlib::capability_method_manifest_entry(capability, method)
981 else {
982 return;
983 };
984 let effects =
985 crate::orchestration::runtime_effects_from_contract(entry.contract.effects, args);
986 recorder
987 .lock()
988 .expect("executed effect recorder poisoned")
989 .extend(effects);
990 }
991
992 pub(crate) fn record_builtin_contract_effects(&self, name: &str, args: &[VmValue]) {
993 let Some(entry) = crate::stdlib::recorded_effect_builtin_manifest_entry(name) else {
994 return;
995 };
996 self.record_builtin_effect_specs(entry.contract.effects, args);
997 }
998
999 pub(crate) fn record_builtin_effect_specs(
1000 &self,
1001 specs: &'static [harn_builtin_meta::EffectSpec],
1002 args: &[VmValue],
1003 ) {
1004 let effects = crate::orchestration::runtime_effects_from_contract(specs, args);
1005 self.executed_effects
1006 .lock()
1007 .expect("executed effect recorder poisoned")
1008 .extend(effects);
1009 }
1010
1011 pub fn set_prepared_module_cache(&mut self, cache: crate::PreparedModuleCache) {
1014 self.prepared_module_cache = cache;
1015 }
1016
1017 pub fn set_graph_link_table(
1025 &mut self,
1026 link_table: Option<Arc<crate::context_manifest::GraphLinkTable>>,
1027 ) {
1028 self.graph_link_table = link_table;
1029 }
1030
1031 pub fn runtime_limits(&self) -> RuntimeLimits {
1033 self.runtime_limits
1034 }
1035
1036 pub fn runtime_limit_report(&self) -> crate::RuntimeLimitsReport {
1038 self.runtime_limits.report()
1039 }
1040
1041 #[inline]
1055 pub(crate) fn debugger_attached(&self) -> bool {
1056 self.debug_hook.is_some()
1057 || !self.breakpoints.is_empty()
1058 || !self.function_breakpoints.is_empty()
1059 }
1060
1061 pub fn set_bridge(&mut self, bridge: Arc<crate::bridge::HostBridge>) {
1063 self.bridge = Some(bridge);
1064 }
1065
1066 pub fn set_denied_builtins(&mut self, denied: HashSet<String>) {
1069 self.denied_builtins = Arc::new(denied);
1070 }
1071
1072 pub fn set_source_info(&mut self, file: &str, text: &str) {
1074 self.source_file = Some(file.to_string());
1075 self.source_text = Some(text.to_string());
1076 if let Some(cov) = self.coverage.as_mut() {
1077 cov.set_primary_file(file);
1078 }
1079 Arc::make_mut(&mut self.source_cache)
1080 .insert(std::path::PathBuf::from(file), Arc::from(text));
1081 }
1082
1083 pub fn start(&mut self, chunk: &Chunk) -> Result<(), VmError> {
1085 self.ensure_execution_available()?;
1086 let debugger = self.debugger_attached();
1093 let initial_env = if debugger {
1094 Some(self.env.clone())
1095 } else {
1096 None
1097 };
1098 let initial_local_slots = if debugger {
1099 Some(Self::fresh_local_slots(chunk))
1100 } else {
1101 None
1102 };
1103 let chunk = Arc::new(chunk.clone());
1104 let local_slots = Self::fresh_local_slots(&chunk);
1105 let inline_cache_set = self.inline_cache_set_index_for_chunk(&chunk);
1106 self.frames.push(CallFrame {
1107 chunk,
1108 inline_cache_set,
1109 ip: 0,
1110 stack_base: self.stack.len(),
1111 saved_env: self.env.clone(),
1112 initial_env,
1113 initial_local_slots,
1114 saved_iterator_depth: self.iterators.len(),
1115 fn_name: String::new(),
1116 argc: 0,
1117 saved_source_dir: None,
1118 module_functions: None,
1119 module_state: None,
1120 local_slots,
1121 local_scope_base: self.env.scope_depth().saturating_sub(1),
1122 local_scope_depth: 0,
1123 });
1124 Ok(())
1125 }
1126
1127 pub(crate) fn child_vm(&self) -> Vm {
1130 Vm {
1131 stack: Vec::with_capacity(64),
1132 env: self.env.clone(),
1133 output: String::new(),
1134 builtins: Arc::clone(&self.builtins),
1135 async_builtins: Arc::clone(&self.async_builtins),
1136 capability_methods: Arc::clone(&self.capability_methods),
1137 builtin_metadata: Arc::clone(&self.builtin_metadata),
1138 builtins_by_id: Arc::clone(&self.builtins_by_id),
1139 builtin_id_collisions: Arc::clone(&self.builtin_id_collisions),
1140 iterators: Vec::new(),
1141 frames: Vec::new(),
1142 exception_handlers: Vec::new(),
1143 spawned_tasks: BTreeMap::new(),
1144 process_exit_request: Arc::clone(&self.process_exit_request),
1145 sync_runtime: self.sync_runtime.clone(),
1146 shared_state_runtime: self.shared_state_runtime.clone(),
1147 inline_cache_sets: Vec::new(),
1148 inline_cache_set_by_chunk: HashMap::new(),
1149 pool_registry: self.pool_registry.clone(),
1150 llm_mock_context: self.llm_mock_context.clone(),
1151 package_snapshot_registry: self.package_snapshot_registry.clone(),
1152 wait_for_graph: self.wait_for_graph.clone(),
1153 held_sync_guards: Vec::new(),
1154 inherited_held_keys: Arc::new(Vec::new()),
1155 task_scopes: Vec::new(),
1156 task_counter: 0,
1157 runtime_context_counter: self.runtime_context_counter,
1158 runtime_context: self.runtime_context.clone(),
1159 deadlines: self.deadlines.clone(),
1160 execution_deadline: self.execution_deadline.fork(),
1161 breakpoints: BTreeMap::new(),
1162 function_breakpoints: std::collections::BTreeSet::new(),
1163 pending_function_bp: None,
1164 step_mode: false,
1165 step_frame_depth: 0,
1166 stopped: false,
1167 last_line: 0,
1168 source_dir: self.source_dir.clone(),
1169 package_execution_guard: self.package_execution_guard.clone(),
1170 imported_paths: Vec::new(),
1171 deferred_cyclic_imports: Vec::new(),
1172 module_cache: Arc::clone(&self.module_cache),
1173 prepared_module_cache: self.prepared_module_cache.clone(),
1174 module_phase_recorder: self.module_phase_recorder.clone(),
1175 lazy_callable_modules: Arc::clone(&self.lazy_callable_modules),
1176 source_cache: Arc::clone(&self.source_cache),
1177 graph_link_table: self.graph_link_table.clone(),
1178 source_file: self.source_file.clone(),
1179 source_text: self.source_text.clone(),
1180 coverage: crate::coverage::for_primary(self.source_file.as_deref()),
1181 bridge: self.bridge.clone(),
1182 denied_builtins: Arc::clone(&self.denied_builtins),
1183 cancel_token: self.cancel_token.clone(),
1184 interrupt_signal_token: self.interrupt_signal_token.clone(),
1185 cancel_grace_instructions_remaining: None,
1186 interrupt_handlers: Vec::new(),
1187 next_interrupt_handle: 1,
1188 pending_interrupt_signal: None,
1189 interrupted: self.interrupted,
1190 dispatching_interrupt: false,
1191 interrupt_handler_deadline: None,
1192 error_stack_trace: Vec::new(),
1193 yield_sender: None,
1194 project_root: self.project_root.clone(),
1195 globals: Arc::clone(&self.globals),
1196 root_harness: self.root_harness.clone(),
1197 executed_effects: Arc::clone(&self.executed_effects),
1198 debug_hook: None,
1199 runtime_limits: self.runtime_limits,
1200 }
1201 }
1202
1203 pub(crate) fn child_vm_for_host(&self) -> Vm {
1206 self.child_vm()
1207 }
1208
1209 pub(crate) fn request_process_exit(&self, code: i32) {
1210 self.process_exit_request.request(code);
1211 }
1212
1213 pub(crate) fn requested_process_exit(&self) -> Option<i32> {
1214 self.process_exit_request.code()
1215 }
1216
1217 pub(crate) fn cancel_spawned_tasks(&mut self) {
1221 for (_, task) in std::mem::take(&mut self.spawned_tasks) {
1222 task.cancel_token
1223 .store(true, std::sync::atomic::Ordering::SeqCst);
1224 task.handle.abort();
1225 }
1226 }
1227
1228 pub fn set_source_dir(&mut self, dir: &std::path::Path) {
1231 let dir = crate::stdlib::process::normalize_context_path(dir);
1232 self.source_dir = Some(dir.clone());
1233 crate::stdlib::set_thread_source_dir(&dir);
1234 if self.project_root.is_none() {
1236 self.project_root = crate::stdlib::process::find_project_root(&dir);
1237 }
1238 }
1239
1240 pub fn set_project_root(&mut self, root: &std::path::Path) {
1243 self.project_root = Some(root.to_path_buf());
1244 }
1245
1246 pub(crate) fn explicit_project_root(&self) -> Option<&std::path::Path> {
1249 self.project_root.as_deref()
1250 }
1251
1252 pub fn project_root(&self) -> Option<&std::path::Path> {
1254 self.project_root.as_deref().or(self.source_dir.as_deref())
1255 }
1256
1257 pub fn builtin_names(&self) -> Vec<String> {
1259 let mut names: Vec<String> = self.builtins.keys().cloned().collect();
1260 names.extend(self.async_builtins.keys().cloned());
1261 names
1262 }
1263
1264 pub fn builtin_metadata(&self) -> Vec<VmBuiltinMetadata> {
1266 self.builtin_metadata.values().cloned().collect()
1267 }
1268
1269 pub fn builtin_metadata_for(&self, name: &str) -> Option<&VmBuiltinMetadata> {
1271 self.builtin_metadata.get(name)
1272 }
1273
1274 pub fn set_global(&mut self, name: &str, value: VmValue) {
1277 Arc::make_mut(&mut self.globals).insert(crate::value::intern_key(name), value);
1278 }
1279
1280 pub fn global(&self, name: &str) -> Option<&VmValue> {
1282 self.globals.get(name)
1283 }
1284
1285 pub fn set_harness(&mut self, harness: crate::harness::Harness) {
1289 self.root_harness = Some(harness.into_vm_value());
1290 }
1291
1292 pub(crate) fn harness(&self) -> Option<&crate::harness::VmHarness> {
1293 match self.root_harness.as_ref() {
1294 Some(VmValue::Harness(handle)) => Some(handle),
1295 _ => None,
1296 }
1297 }
1298
1299 pub fn root_harness_value(&self) -> Option<VmValue> {
1302 self.root_harness.clone()
1303 }
1304
1305 pub fn output(&self) -> &str {
1307 &self.output
1308 }
1309
1310 pub fn take_output(&mut self) -> String {
1314 std::mem::take(&mut self.output)
1315 }
1316
1317 pub fn append_output(&mut self, text: &str) {
1321 self.output.push_str(text);
1322 }
1323
1324 pub(crate) fn pop(&mut self) -> Result<VmValue, VmError> {
1325 self.stack.pop().ok_or(VmError::StackUnderflow)
1326 }
1327
1328 pub(crate) fn peek(&self) -> Result<&VmValue, VmError> {
1329 self.stack.last().ok_or(VmError::StackUnderflow)
1330 }
1331
1332 pub(crate) fn const_str(c: &Constant) -> Result<&str, VmError> {
1333 match c {
1334 Constant::String(s) => Ok(s.as_str()),
1335 _ => Err(VmError::TypeError("expected string constant".into())),
1336 }
1337 }
1338
1339 pub(crate) fn release_sync_guards_for_current_scope(&mut self) {
1340 let depth = self.env.scope_depth();
1341 self.held_sync_guards
1342 .retain(|guard| guard.env_scope_depth < depth);
1343 self.cancel_task_scopes_where(|s| s.env_scope_depth >= depth);
1346 }
1347
1348 pub(crate) fn release_sync_guards_after_unwind(
1349 &mut self,
1350 frame_depth: usize,
1351 env_scope_depth: usize,
1352 ) {
1353 self.held_sync_guards.retain(|guard| {
1354 guard.frame_depth <= frame_depth && guard.env_scope_depth <= env_scope_depth
1355 });
1356 self.cancel_task_scopes_where(|s| {
1359 !(s.frame_depth <= frame_depth && s.env_scope_depth <= env_scope_depth)
1360 });
1361 }
1362
1363 pub(crate) fn release_sync_guards_for_frame(&mut self, frame_depth: usize) {
1364 self.held_sync_guards
1365 .retain(|guard| guard.frame_depth != frame_depth);
1366 self.cancel_task_scopes_where(|s| s.frame_depth == frame_depth);
1369 }
1370
1371 pub(crate) fn adopt_sync_permit_for_current_scope(
1372 &mut self,
1373 permit: crate::value::VmSyncPermitHandle,
1374 ) {
1375 if permit.is_released()
1376 || self
1377 .held_sync_guards
1378 .iter()
1379 .any(|guard| guard._permit.same_lease(&permit))
1380 {
1381 return;
1382 }
1383 self.held_sync_guards
1384 .push(crate::synchronization::VmSyncHeldGuard {
1385 _permit: permit,
1386 frame_depth: self.frames.len(),
1387 env_scope_depth: self.env.scope_depth(),
1388 });
1389 }
1390
1391 pub(crate) fn deregister_task_from_scopes(&mut self, id: &str) {
1394 for scope in &mut self.task_scopes {
1395 scope.task_ids.retain(|t| t != id);
1396 }
1397 }
1398
1399 fn cancel_task_scopes_where<F: Fn(&TaskScope) -> bool>(&mut self, doomed: F) {
1402 let mut i = 0;
1403 while i < self.task_scopes.len() {
1404 if doomed(&self.task_scopes[i]) {
1405 let scope = self.task_scopes.remove(i);
1406 for id in &scope.task_ids {
1407 if let Some(task) = self.spawned_tasks.remove(id) {
1408 task.cancel_token
1409 .store(true, std::sync::atomic::Ordering::SeqCst);
1410 task.handle.abort();
1411 }
1412 }
1413 } else {
1414 i += 1;
1415 }
1416 }
1417 }
1418
1419 pub(crate) fn held_permits_for(&self, kind: &str, key: &str) -> u32 {
1423 let own: u32 = self
1424 .held_sync_guards
1425 .iter()
1426 .filter(|guard| {
1427 !guard._permit.is_released()
1428 && guard._permit.kind() == kind
1429 && guard._permit.key() == key
1430 })
1431 .map(|guard| guard._permit.permits())
1432 .sum();
1433 let inherited: u32 = self
1434 .inherited_held_keys
1435 .iter()
1436 .filter(|held| held.kind == kind && held.key == key)
1437 .map(|held| held.permits)
1438 .sum();
1439 own + inherited
1440 }
1441
1442 pub(crate) fn combined_held_keys(&self) -> Vec<crate::synchronization::VmSyncHeldKey> {
1445 let mut keys: Vec<crate::synchronization::VmSyncHeldKey> = self
1446 .held_sync_guards
1447 .iter()
1448 .filter_map(|guard| crate::synchronization::VmSyncHeldKey::from_permit(&guard._permit))
1449 .collect();
1450 keys.extend(self.inherited_held_keys.iter().cloned());
1451 keys
1452 }
1453
1454 pub(crate) fn child_vm_inline(&self) -> Vm {
1460 let mut child = self.child_vm();
1461 child.inherited_held_keys = Arc::new(self.combined_held_keys());
1462 child
1463 }
1464}
1465
1466impl Drop for Vm {
1467 fn drop(&mut self) {
1468 if let Some(coverage) = self.coverage.take() {
1469 crate::coverage::merge_into_global(coverage);
1470 }
1471 self.cancel_spawned_tasks();
1472 }
1473}
1474
1475impl Default for Vm {
1476 fn default() -> Self {
1477 Self::new()
1478 }
1479}
1480
1481#[cfg(test)]
1482#[path = "state_tests.rs"]
1483mod tests;