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) package_snapshot_registry: Arc<crate::stdlib::PackageSnapshotRegistry>,
403 pub(crate) wait_for_graph: Arc<crate::wait_for_graph::VmWaitForGraph>,
405 pub(crate) held_sync_guards: Vec<crate::synchronization::VmSyncHeldGuard>,
407 pub(crate) inherited_held_keys: Arc<Vec<crate::synchronization::VmSyncHeldKey>>,
415 pub(crate) task_scopes: Vec<TaskScope>,
421 pub(crate) task_counter: u64,
423 pub(crate) runtime_context_counter: u64,
425 pub(crate) runtime_context: crate::runtime_context::RuntimeContext,
427 pub(crate) deadlines: Vec<(Instant, usize)>,
429 pub(crate) execution_deadline: Arc<ExecutionDeadlineState>,
431 pub(crate) breakpoints: BTreeMap<String, std::collections::BTreeSet<usize>>,
436 pub(crate) function_breakpoints: std::collections::BTreeSet<String>,
442 pub(crate) pending_function_bp: Option<String>,
447 pub(crate) step_mode: bool,
449 pub(crate) step_frame_depth: usize,
451 pub(crate) stopped: bool,
453 pub(crate) last_line: usize,
455 pub(crate) source_dir: Option<std::path::PathBuf>,
457 pub(crate) package_execution_guard:
459 Option<Arc<harn_modules::package_execution::PackageExecutionGuard>>,
460 pub(crate) imported_paths: Vec<std::path::PathBuf>,
462 pub(crate) deferred_cyclic_imports: Vec<super::modules::DeferredCyclicImport>,
466 pub(crate) module_cache: ModuleCache,
468 pub(crate) prepared_module_cache: crate::PreparedModuleCache,
471 pub(crate) module_phase_recorder: Option<super::ModulePhaseRecorder>,
473 pub(crate) lazy_callable_modules: LazyCallableModuleCache,
477 pub(crate) source_cache: Arc<BTreeMap<std::path::PathBuf, String>>,
479 pub(crate) source_file: Option<String>,
481 pub(crate) source_text: Option<String>,
483 pub(crate) coverage: Option<crate::coverage::Coverage>,
486 pub(crate) bridge: Option<Arc<crate::bridge::HostBridge>>,
488 pub(crate) denied_builtins: Arc<HashSet<String>>,
490 pub(crate) cancel_token: Option<std::sync::Arc<std::sync::atomic::AtomicBool>>,
492 pub(crate) interrupt_signal_token: Option<std::sync::Arc<std::sync::Mutex<Option<String>>>>,
493 pub(crate) cancel_grace_instructions_remaining: Option<usize>,
498 pub(crate) interrupt_handlers: Vec<InterruptHandler>,
500 pub(crate) next_interrupt_handle: i64,
501 pub(crate) pending_interrupt_signal: Option<String>,
502 pub(crate) interrupted: bool,
503 pub(crate) dispatching_interrupt: bool,
504 pub(crate) interrupt_handler_deadline: Option<Instant>,
505 pub(crate) error_stack_trace: Vec<(String, usize, usize, Option<String>)>,
507 pub(crate) yield_sender: Option<tokio::sync::mpsc::Sender<Result<VmValue, VmError>>>,
510 pub(crate) project_root: Option<std::path::PathBuf>,
513 pub(crate) globals: Arc<crate::value::DictMap>,
516 pub(crate) debug_hook: Option<parking_lot::Mutex<Box<DebugHook>>>,
518 pub(crate) runtime_limits: RuntimeLimits,
520}
521
522#[derive(Clone)]
530pub struct VmBaseline {
531 builtins: Arc<BTreeMap<String, VmBuiltinFn>>,
532 async_builtins: Arc<BTreeMap<String, VmAsyncBuiltinFn>>,
533 builtin_metadata: Arc<BTreeMap<String, VmBuiltinMetadata>>,
534 builtins_by_id: Arc<HashMap<BuiltinId, VmBuiltinEntry>>,
535 builtin_id_collisions: Arc<HashSet<BuiltinId>>,
536 source_dir: Option<std::path::PathBuf>,
537 source_file: Option<String>,
538 source_text: Option<String>,
539 project_root: Option<std::path::PathBuf>,
540 globals: Arc<crate::value::DictMap>,
541 denied_builtins: Arc<HashSet<String>>,
542 prepared_module_cache: crate::PreparedModuleCache,
543 runtime_limits: RuntimeLimits,
544}
545
546impl VmBaseline {
547 pub fn from_vm(vm: &Vm) -> Self {
548 Self {
549 builtins: Arc::clone(&vm.builtins),
550 async_builtins: Arc::clone(&vm.async_builtins),
551 builtin_metadata: Arc::clone(&vm.builtin_metadata),
552 builtins_by_id: Arc::clone(&vm.builtins_by_id),
553 builtin_id_collisions: Arc::clone(&vm.builtin_id_collisions),
554 source_dir: vm.source_dir.clone(),
555 source_file: vm.source_file.clone(),
556 source_text: vm.source_text.clone(),
557 project_root: vm.project_root.clone(),
558 globals: Arc::clone(&vm.globals),
559 denied_builtins: Arc::clone(&vm.denied_builtins),
560 prepared_module_cache: vm.prepared_module_cache.clone(),
561 runtime_limits: vm.runtime_limits,
562 }
563 }
564
565 pub fn instantiate(&self) -> Vm {
566 crate::initialize_runtime_assets();
567 let mut source_cache = BTreeMap::new();
568 if let (Some(file), Some(text)) = (&self.source_file, &self.source_text) {
569 source_cache.insert(std::path::PathBuf::from(file), text.clone());
570 }
571 if let Some(dir) = &self.source_dir {
572 crate::stdlib::set_thread_source_dir(dir);
573 }
574
575 let mut vm = Vm {
576 stack: Vec::with_capacity(256),
577 env: VmEnv::new(),
578 output: String::new(),
579 builtins: Arc::clone(&self.builtins),
580 async_builtins: Arc::clone(&self.async_builtins),
581 builtin_metadata: Arc::clone(&self.builtin_metadata),
582 builtins_by_id: Arc::clone(&self.builtins_by_id),
583 builtin_id_collisions: Arc::clone(&self.builtin_id_collisions),
584 iterators: Vec::new(),
585 frames: Vec::new(),
586 exception_handlers: Vec::new(),
587 spawned_tasks: BTreeMap::new(),
588 process_exit_request: Arc::new(ProcessExitRequest::new()),
589 sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
590 shared_state_runtime: Arc::new(crate::shared_state::VmSharedStateRuntime::new()),
591 inline_cache_sets: Vec::new(),
592 inline_cache_set_by_chunk: HashMap::new(),
593 pool_registry: crate::stdlib::pool::new_pool_registry(),
594 package_snapshot_registry: Arc::new(Default::default()),
595 wait_for_graph: Arc::new(crate::wait_for_graph::VmWaitForGraph::new()),
596 held_sync_guards: Vec::new(),
597 inherited_held_keys: Arc::new(Vec::new()),
598 task_scopes: Vec::new(),
599 task_counter: 0,
600 runtime_context_counter: 0,
601 runtime_context: crate::runtime_context::RuntimeContext::root(),
602 deadlines: Vec::new(),
603 execution_deadline: super::execution::new_execution_deadline_state(None),
604 breakpoints: BTreeMap::new(),
605 function_breakpoints: std::collections::BTreeSet::new(),
606 pending_function_bp: None,
607 step_mode: false,
608 step_frame_depth: 0,
609 stopped: false,
610 last_line: 0,
611 source_dir: self.source_dir.clone(),
612 package_execution_guard: None,
613 imported_paths: Vec::new(),
614 deferred_cyclic_imports: Vec::new(),
615 module_cache: Arc::new(BTreeMap::new()),
616 prepared_module_cache: self.prepared_module_cache.clone(),
617 module_phase_recorder: None,
618 lazy_callable_modules: Arc::new(crate::value::VmMutex::new(BTreeMap::new())),
619 source_cache: Arc::new(source_cache),
620 source_file: self.source_file.clone(),
621 source_text: self.source_text.clone(),
622 coverage: crate::coverage::for_primary(self.source_file.as_deref()),
623 bridge: None,
624 denied_builtins: Arc::clone(&self.denied_builtins),
625 cancel_token: None,
626 interrupt_signal_token: None,
627 cancel_grace_instructions_remaining: None,
628 interrupt_handlers: Vec::new(),
629 next_interrupt_handle: 1,
630 pending_interrupt_signal: None,
631 interrupted: false,
632 dispatching_interrupt: false,
633 interrupt_handler_deadline: None,
634 error_stack_trace: Vec::new(),
635 yield_sender: None,
636 project_root: self.project_root.clone(),
637 globals: Arc::clone(&self.globals),
638 debug_hook: None,
639 runtime_limits: self.runtime_limits,
640 };
641
642 crate::stdlib::rebind_execution_state_builtins(&mut vm);
643 vm
644 }
645}
646
647impl Vm {
648 pub(crate) fn ensure_execution_available(&self) -> Result<(), VmError> {
649 if self.execution_deadline.is_abandoned() {
650 return Err(VmError::AbandonedExecution);
651 }
652 Ok(())
653 }
654
655 pub(crate) fn fresh_local_slots(chunk: &Chunk) -> Vec<LocalSlot> {
656 chunk
657 .local_slots
658 .iter()
659 .map(|_| LocalSlot {
660 value: VmValue::Nil,
661 initialized: false,
662 synced: false,
663 })
664 .collect()
665 }
666
667 pub(crate) fn bind_param_slots(
668 slots: &mut [LocalSlot],
669 func: &crate::chunk::CompiledFunction,
670 args: &[VmValue],
671 synced: bool,
672 ) {
673 Self::bind_param_slots_args(slots, func, &super::CallArgs::Slice(args), synced);
674 }
675
676 pub(crate) fn bind_param_slots_args(
677 slots: &mut [LocalSlot],
678 func: &crate::chunk::CompiledFunction,
679 args: &super::CallArgs<'_>,
680 synced: bool,
681 ) {
682 let param_count = func.params.len();
683 for (i, _param) in func.params.iter().enumerate() {
684 if i >= slots.len() {
685 break;
686 }
687 if func.has_rest_param && i == param_count - 1 {
688 let rest_args = args.to_vec_from(i);
689 slots[i].value = VmValue::List(std::sync::Arc::new(rest_args));
690 slots[i].initialized = true;
691 slots[i].synced = synced;
692 } else if let Some(arg) = args.get(i) {
693 slots[i].value = arg.clone();
694 slots[i].initialized = true;
695 slots[i].synced = synced;
696 }
697 }
698 }
699
700 pub(crate) fn visible_variables(&self) -> crate::value::DictMap {
701 let mut vars = self.env.all_variables();
702 let Some(frame) = self.frames.last() else {
703 return vars;
704 };
705 for (slot, info) in frame.local_slots.iter().zip(frame.chunk.local_slots.iter()) {
706 if slot.initialized && info.scope_depth <= frame.local_scope_depth {
707 vars.insert(crate::value::intern_key(&info.name), slot.value.clone());
708 }
709 }
710 vars
711 }
712
713 pub(crate) fn sync_current_frame_locals_to_env(&mut self) {
714 let frames = &mut self.frames;
715 let env = &mut self.env;
716 let Some(frame) = frames.last_mut() else {
717 return;
718 };
719 let local_scope_base = frame.local_scope_base;
720 let local_scope_depth = frame.local_scope_depth;
721 for (slot, info) in frame
722 .local_slots
723 .iter_mut()
724 .zip(frame.chunk.local_slots.iter())
725 {
726 if slot.initialized && !slot.synced && info.scope_depth <= local_scope_depth {
727 slot.synced = true;
728 let scope_idx = local_scope_base + info.scope_depth;
729 while env.scopes.len() <= scope_idx {
730 env.push_scope();
731 }
732 Arc::make_mut(&mut env.scopes[scope_idx].vars).insert(
736 info.name.clone(),
737 crate::value::Binding::Value {
738 value: slot.value.clone(),
739 mutable: info.mutable,
740 },
741 );
742 }
743 }
744 }
745
746 pub(crate) fn closure_call_env_for_current_frame(
747 &self,
748 closure: &crate::value::VmClosure,
749 ) -> VmEnv {
750 if closure.module_state().is_some() {
751 return closure.env.cloned_for_call();
752 }
753 let call_env = Self::closure_call_env(&self.env, closure);
754 if !closure.func.chunk.references_outer_names {
759 return call_env;
760 }
761 let mut call_env = call_env;
762 let Some(frame) = self.frames.last() else {
763 return call_env;
764 };
765 for (slot, info) in frame
766 .local_slots
767 .iter()
768 .zip(frame.chunk.local_slots.iter())
769 .filter(|(slot, info)| slot.initialized && info.scope_depth <= frame.local_scope_depth)
770 {
771 if matches!(slot.value, VmValue::Closure(_)) && !call_env.contains(&info.name) {
772 let _ = call_env.define(&info.name, slot.value.clone(), info.mutable);
773 }
774 }
775 call_env
776 }
777
778 pub(crate) fn active_local_slot_value(&self, name: &str) -> Option<VmValue> {
779 let frame = self.frames.last()?;
780 let idx = self.active_local_slot_index(name)?;
781 frame.local_slots.get(idx).map(|slot| slot.value.clone())
782 }
783
784 pub(crate) fn active_local_slot_index(&self, name: &str) -> Option<usize> {
789 let frame = self.frames.last()?;
790 for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
791 if info.name == name && info.scope_depth <= frame.local_scope_depth {
792 if let Some(slot) = frame.local_slots.get(idx) {
793 if slot.initialized {
794 return Some(idx);
795 }
796 }
797 }
798 }
799 None
800 }
801
802 pub(crate) fn assign_active_local_slot(
803 &mut self,
804 name: &str,
805 value: VmValue,
806 debug: bool,
807 ) -> Result<bool, VmError> {
808 let Some(frame) = self.frames.last_mut() else {
809 return Ok(false);
810 };
811 for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
812 if info.name == name && info.scope_depth <= frame.local_scope_depth {
813 if !debug && !info.mutable {
814 return Err(VmError::ImmutableAssignment(name.to_string()));
815 }
816 if let Some(slot) = frame.local_slots.get_mut(idx) {
817 crate::value::recursion::dismantle(std::mem::replace(&mut slot.value, value));
818 slot.initialized = true;
819 slot.synced = false;
820 return Ok(true);
821 }
822 }
823 }
824 Ok(false)
825 }
826
827 pub fn new() -> Self {
828 crate::initialize_runtime_assets();
829 Self {
830 stack: Vec::with_capacity(256),
831 env: VmEnv::new(),
832 output: String::new(),
833 builtins: Arc::new(BTreeMap::new()),
834 async_builtins: Arc::new(BTreeMap::new()),
835 builtin_metadata: Arc::new(BTreeMap::new()),
836 builtins_by_id: Arc::new(HashMap::new()),
837 builtin_id_collisions: Arc::new(HashSet::new()),
838 iterators: Vec::new(),
839 frames: Vec::new(),
840 exception_handlers: Vec::new(),
841 spawned_tasks: BTreeMap::new(),
842 process_exit_request: Arc::new(ProcessExitRequest::new()),
843 sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
844 shared_state_runtime: Arc::new(crate::shared_state::VmSharedStateRuntime::new()),
845 inline_cache_sets: Vec::new(),
846 inline_cache_set_by_chunk: HashMap::new(),
847 pool_registry: crate::stdlib::pool::new_pool_registry(),
848 package_snapshot_registry: Arc::new(Default::default()),
849 wait_for_graph: Arc::new(crate::wait_for_graph::VmWaitForGraph::new()),
850 held_sync_guards: Vec::new(),
851 inherited_held_keys: Arc::new(Vec::new()),
852 task_scopes: Vec::new(),
853 task_counter: 0,
854 runtime_context_counter: 0,
855 runtime_context: crate::runtime_context::RuntimeContext::root(),
856 deadlines: Vec::new(),
857 execution_deadline: super::execution::new_execution_deadline_state(None),
858 breakpoints: BTreeMap::new(),
859 function_breakpoints: std::collections::BTreeSet::new(),
860 pending_function_bp: None,
861 step_mode: false,
862 step_frame_depth: 0,
863 stopped: false,
864 last_line: 0,
865 source_dir: None,
866 package_execution_guard: None,
867 imported_paths: Vec::new(),
868 deferred_cyclic_imports: Vec::new(),
869 module_cache: Arc::new(BTreeMap::new()),
870 prepared_module_cache: crate::PreparedModuleCache::default(),
871 module_phase_recorder: None,
872 lazy_callable_modules: Arc::new(crate::value::VmMutex::new(BTreeMap::new())),
873 source_cache: Arc::new(BTreeMap::new()),
874 source_file: None,
875 source_text: None,
876 coverage: crate::coverage::for_primary(None),
877 bridge: None,
878 denied_builtins: Arc::new(HashSet::new()),
879 cancel_token: None,
880 interrupt_signal_token: None,
881 cancel_grace_instructions_remaining: None,
882 interrupt_handlers: Vec::new(),
883 next_interrupt_handle: 1,
884 pending_interrupt_signal: None,
885 interrupted: false,
886 dispatching_interrupt: false,
887 interrupt_handler_deadline: None,
888 error_stack_trace: Vec::new(),
889 yield_sender: None,
890 project_root: None,
891 globals: Arc::new(crate::value::DictMap::new()),
892 debug_hook: None,
893 runtime_limits: RuntimeLimits::default(),
894 }
895 }
896
897 pub fn baseline(&self) -> VmBaseline {
898 VmBaseline::from_vm(self)
899 }
900
901 pub fn set_prepared_module_cache(&mut self, cache: crate::PreparedModuleCache) {
904 self.prepared_module_cache = cache;
905 }
906
907 pub fn runtime_limits(&self) -> RuntimeLimits {
909 self.runtime_limits
910 }
911
912 pub fn runtime_limit_report(&self) -> crate::RuntimeLimitsReport {
914 self.runtime_limits.report()
915 }
916
917 #[inline]
931 pub(crate) fn debugger_attached(&self) -> bool {
932 self.debug_hook.is_some()
933 || !self.breakpoints.is_empty()
934 || !self.function_breakpoints.is_empty()
935 }
936
937 pub fn set_bridge(&mut self, bridge: Arc<crate::bridge::HostBridge>) {
939 self.bridge = Some(bridge);
940 }
941
942 pub fn set_denied_builtins(&mut self, denied: HashSet<String>) {
945 self.denied_builtins = Arc::new(denied);
946 }
947
948 pub fn set_source_info(&mut self, file: &str, text: &str) {
950 self.source_file = Some(file.to_string());
951 self.source_text = Some(text.to_string());
952 if let Some(cov) = self.coverage.as_mut() {
953 cov.set_primary_file(file);
954 }
955 Arc::make_mut(&mut self.source_cache)
956 .insert(std::path::PathBuf::from(file), text.to_string());
957 }
958
959 pub fn start(&mut self, chunk: &Chunk) -> Result<(), VmError> {
961 self.ensure_execution_available()?;
962 let debugger = self.debugger_attached();
969 let initial_env = if debugger {
970 Some(self.env.clone())
971 } else {
972 None
973 };
974 let initial_local_slots = if debugger {
975 Some(Self::fresh_local_slots(chunk))
976 } else {
977 None
978 };
979 let chunk = Arc::new(chunk.clone());
980 let local_slots = Self::fresh_local_slots(&chunk);
981 let inline_cache_set = self.inline_cache_set_index_for_chunk(&chunk);
982 self.frames.push(CallFrame {
983 chunk,
984 inline_cache_set,
985 ip: 0,
986 stack_base: self.stack.len(),
987 saved_env: self.env.clone(),
988 initial_env,
989 initial_local_slots,
990 saved_iterator_depth: self.iterators.len(),
991 fn_name: String::new(),
992 argc: 0,
993 saved_source_dir: None,
994 module_functions: None,
995 module_state: None,
996 local_slots,
997 local_scope_base: self.env.scope_depth().saturating_sub(1),
998 local_scope_depth: 0,
999 });
1000 Ok(())
1001 }
1002
1003 pub(crate) fn child_vm(&self) -> Vm {
1006 Vm {
1007 stack: Vec::with_capacity(64),
1008 env: self.env.clone(),
1009 output: String::new(),
1010 builtins: Arc::clone(&self.builtins),
1011 async_builtins: Arc::clone(&self.async_builtins),
1012 builtin_metadata: Arc::clone(&self.builtin_metadata),
1013 builtins_by_id: Arc::clone(&self.builtins_by_id),
1014 builtin_id_collisions: Arc::clone(&self.builtin_id_collisions),
1015 iterators: Vec::new(),
1016 frames: Vec::new(),
1017 exception_handlers: Vec::new(),
1018 spawned_tasks: BTreeMap::new(),
1019 process_exit_request: Arc::clone(&self.process_exit_request),
1020 sync_runtime: self.sync_runtime.clone(),
1021 shared_state_runtime: self.shared_state_runtime.clone(),
1022 inline_cache_sets: Vec::new(),
1023 inline_cache_set_by_chunk: HashMap::new(),
1024 pool_registry: self.pool_registry.clone(),
1025 package_snapshot_registry: self.package_snapshot_registry.clone(),
1026 wait_for_graph: self.wait_for_graph.clone(),
1027 held_sync_guards: Vec::new(),
1028 inherited_held_keys: Arc::new(Vec::new()),
1029 task_scopes: Vec::new(),
1030 task_counter: 0,
1031 runtime_context_counter: self.runtime_context_counter,
1032 runtime_context: self.runtime_context.clone(),
1033 deadlines: self.deadlines.clone(),
1034 execution_deadline: self.execution_deadline.fork(),
1035 breakpoints: BTreeMap::new(),
1036 function_breakpoints: std::collections::BTreeSet::new(),
1037 pending_function_bp: None,
1038 step_mode: false,
1039 step_frame_depth: 0,
1040 stopped: false,
1041 last_line: 0,
1042 source_dir: self.source_dir.clone(),
1043 package_execution_guard: self.package_execution_guard.clone(),
1044 imported_paths: Vec::new(),
1045 deferred_cyclic_imports: Vec::new(),
1046 module_cache: Arc::clone(&self.module_cache),
1047 prepared_module_cache: self.prepared_module_cache.clone(),
1048 module_phase_recorder: self.module_phase_recorder.clone(),
1049 lazy_callable_modules: Arc::clone(&self.lazy_callable_modules),
1050 source_cache: Arc::clone(&self.source_cache),
1051 source_file: self.source_file.clone(),
1052 source_text: self.source_text.clone(),
1053 coverage: crate::coverage::for_primary(self.source_file.as_deref()),
1054 bridge: self.bridge.clone(),
1055 denied_builtins: Arc::clone(&self.denied_builtins),
1056 cancel_token: self.cancel_token.clone(),
1057 interrupt_signal_token: self.interrupt_signal_token.clone(),
1058 cancel_grace_instructions_remaining: None,
1059 interrupt_handlers: Vec::new(),
1060 next_interrupt_handle: 1,
1061 pending_interrupt_signal: None,
1062 interrupted: self.interrupted,
1063 dispatching_interrupt: false,
1064 interrupt_handler_deadline: None,
1065 error_stack_trace: Vec::new(),
1066 yield_sender: None,
1067 project_root: self.project_root.clone(),
1068 globals: Arc::clone(&self.globals),
1069 debug_hook: None,
1070 runtime_limits: self.runtime_limits,
1071 }
1072 }
1073
1074 pub(crate) fn child_vm_for_host(&self) -> Vm {
1077 self.child_vm()
1078 }
1079
1080 pub(crate) fn request_process_exit(&self, code: i32) {
1081 self.process_exit_request.request(code);
1082 }
1083
1084 pub(crate) fn requested_process_exit(&self) -> Option<i32> {
1085 self.process_exit_request.code()
1086 }
1087
1088 pub(crate) fn cancel_spawned_tasks(&mut self) {
1092 for (_, task) in std::mem::take(&mut self.spawned_tasks) {
1093 task.cancel_token
1094 .store(true, std::sync::atomic::Ordering::SeqCst);
1095 task.handle.abort();
1096 }
1097 }
1098
1099 pub fn set_source_dir(&mut self, dir: &std::path::Path) {
1102 let dir = crate::stdlib::process::normalize_context_path(dir);
1103 self.source_dir = Some(dir.clone());
1104 crate::stdlib::set_thread_source_dir(&dir);
1105 if self.project_root.is_none() {
1107 self.project_root = crate::stdlib::process::find_project_root(&dir);
1108 }
1109 }
1110
1111 pub fn set_project_root(&mut self, root: &std::path::Path) {
1114 self.project_root = Some(root.to_path_buf());
1115 }
1116
1117 pub(crate) fn explicit_project_root(&self) -> Option<&std::path::Path> {
1120 self.project_root.as_deref()
1121 }
1122
1123 pub fn project_root(&self) -> Option<&std::path::Path> {
1125 self.project_root.as_deref().or(self.source_dir.as_deref())
1126 }
1127
1128 pub fn builtin_names(&self) -> Vec<String> {
1130 let mut names: Vec<String> = self.builtins.keys().cloned().collect();
1131 names.extend(self.async_builtins.keys().cloned());
1132 names
1133 }
1134
1135 pub fn builtin_metadata(&self) -> Vec<VmBuiltinMetadata> {
1137 self.builtin_metadata.values().cloned().collect()
1138 }
1139
1140 pub fn builtin_metadata_for(&self, name: &str) -> Option<&VmBuiltinMetadata> {
1142 self.builtin_metadata.get(name)
1143 }
1144
1145 pub fn set_global(&mut self, name: &str, value: VmValue) {
1148 Arc::make_mut(&mut self.globals).insert(crate::value::intern_key(name), value);
1149 }
1150
1151 pub fn global(&self, name: &str) -> Option<&VmValue> {
1157 self.globals.get(name)
1158 }
1159
1160 pub fn set_harness(&mut self, harness: crate::harness::Harness) {
1166 self.set_global("harness", harness.into_vm_value());
1167 }
1168
1169 pub fn output(&self) -> &str {
1171 &self.output
1172 }
1173
1174 pub fn take_output(&mut self) -> String {
1178 std::mem::take(&mut self.output)
1179 }
1180
1181 pub fn append_output(&mut self, text: &str) {
1185 self.output.push_str(text);
1186 }
1187
1188 pub(crate) fn pop(&mut self) -> Result<VmValue, VmError> {
1189 self.stack.pop().ok_or(VmError::StackUnderflow)
1190 }
1191
1192 pub(crate) fn peek(&self) -> Result<&VmValue, VmError> {
1193 self.stack.last().ok_or(VmError::StackUnderflow)
1194 }
1195
1196 pub(crate) fn const_str(c: &Constant) -> Result<&str, VmError> {
1197 match c {
1198 Constant::String(s) => Ok(s.as_str()),
1199 _ => Err(VmError::TypeError("expected string constant".into())),
1200 }
1201 }
1202
1203 pub(crate) fn release_sync_guards_for_current_scope(&mut self) {
1204 let depth = self.env.scope_depth();
1205 self.held_sync_guards
1206 .retain(|guard| guard.env_scope_depth < depth);
1207 self.cancel_task_scopes_where(|s| s.env_scope_depth >= depth);
1210 }
1211
1212 pub(crate) fn release_sync_guards_after_unwind(
1213 &mut self,
1214 frame_depth: usize,
1215 env_scope_depth: usize,
1216 ) {
1217 self.held_sync_guards.retain(|guard| {
1218 guard.frame_depth <= frame_depth && guard.env_scope_depth <= env_scope_depth
1219 });
1220 self.cancel_task_scopes_where(|s| {
1223 !(s.frame_depth <= frame_depth && s.env_scope_depth <= env_scope_depth)
1224 });
1225 }
1226
1227 pub(crate) fn release_sync_guards_for_frame(&mut self, frame_depth: usize) {
1228 self.held_sync_guards
1229 .retain(|guard| guard.frame_depth != frame_depth);
1230 self.cancel_task_scopes_where(|s| s.frame_depth == frame_depth);
1233 }
1234
1235 pub(crate) fn adopt_sync_permit_for_current_scope(
1236 &mut self,
1237 permit: crate::value::VmSyncPermitHandle,
1238 ) {
1239 if permit.is_released()
1240 || self
1241 .held_sync_guards
1242 .iter()
1243 .any(|guard| guard._permit.same_lease(&permit))
1244 {
1245 return;
1246 }
1247 self.held_sync_guards
1248 .push(crate::synchronization::VmSyncHeldGuard {
1249 _permit: permit,
1250 frame_depth: self.frames.len(),
1251 env_scope_depth: self.env.scope_depth(),
1252 });
1253 }
1254
1255 pub(crate) fn deregister_task_from_scopes(&mut self, id: &str) {
1258 for scope in &mut self.task_scopes {
1259 scope.task_ids.retain(|t| t != id);
1260 }
1261 }
1262
1263 fn cancel_task_scopes_where<F: Fn(&TaskScope) -> bool>(&mut self, doomed: F) {
1266 let mut i = 0;
1267 while i < self.task_scopes.len() {
1268 if doomed(&self.task_scopes[i]) {
1269 let scope = self.task_scopes.remove(i);
1270 for id in &scope.task_ids {
1271 if let Some(task) = self.spawned_tasks.remove(id) {
1272 task.cancel_token
1273 .store(true, std::sync::atomic::Ordering::SeqCst);
1274 task.handle.abort();
1275 }
1276 }
1277 } else {
1278 i += 1;
1279 }
1280 }
1281 }
1282
1283 pub(crate) fn held_permits_for(&self, kind: &str, key: &str) -> u32 {
1287 let own: u32 = self
1288 .held_sync_guards
1289 .iter()
1290 .filter(|guard| {
1291 !guard._permit.is_released()
1292 && guard._permit.kind() == kind
1293 && guard._permit.key() == key
1294 })
1295 .map(|guard| guard._permit.permits())
1296 .sum();
1297 let inherited: u32 = self
1298 .inherited_held_keys
1299 .iter()
1300 .filter(|held| held.kind == kind && held.key == key)
1301 .map(|held| held.permits)
1302 .sum();
1303 own + inherited
1304 }
1305
1306 pub(crate) fn combined_held_keys(&self) -> Vec<crate::synchronization::VmSyncHeldKey> {
1309 let mut keys: Vec<crate::synchronization::VmSyncHeldKey> = self
1310 .held_sync_guards
1311 .iter()
1312 .filter_map(|guard| crate::synchronization::VmSyncHeldKey::from_permit(&guard._permit))
1313 .collect();
1314 keys.extend(self.inherited_held_keys.iter().cloned());
1315 keys
1316 }
1317
1318 pub(crate) fn child_vm_inline(&self) -> Vm {
1324 let mut child = self.child_vm();
1325 child.inherited_held_keys = Arc::new(self.combined_held_keys());
1326 child
1327 }
1328}
1329
1330impl Drop for Vm {
1331 fn drop(&mut self) {
1332 if let Some(coverage) = self.coverage.take() {
1333 crate::coverage::merge_into_global(coverage);
1334 }
1335 self.cancel_spawned_tasks();
1336 }
1337}
1338
1339impl Default for Vm {
1340 fn default() -> Self {
1341 Self::new()
1342 }
1343}
1344
1345#[cfg(test)]
1346mod tests {
1347
1348 use super::*;
1349
1350 #[test]
1351 fn vm_construction_initializes_shared_secret_patterns() {
1352 let _vm = Vm::new();
1353 assert!(crate::secret_patterns::default_secret_patterns_initialized());
1354 }
1355
1356 fn baseline_with_stdlib(source: &str) -> VmBaseline {
1357 let mut vm = Vm::new();
1358 crate::register_vm_stdlib(&mut vm);
1359 vm.set_source_info("baseline_test.harn", source);
1360 vm.set_global(
1361 "stable_global",
1362 VmValue::String(arcstr::ArcStr::from("baseline")),
1363 );
1364 vm.baseline()
1365 }
1366
1367 #[test]
1368 fn vm_baseline_instantiates_clean_mutable_execution_state() {
1369 let baseline = baseline_with_stdlib("pipeline main() { __io_println(stable_global) }");
1370
1371 let mut dirty = baseline.instantiate();
1372 dirty.stack.push(VmValue::Int(42));
1373 dirty.output.push_str("dirty");
1374 dirty.task_counter = 9;
1375 dirty.runtime_context_counter = 7;
1376 dirty
1377 .error_stack_trace
1378 .push(("main".to_string(), 1, 1, None));
1379
1380 let clean = baseline.instantiate();
1381 assert!(clean.stack.is_empty());
1382 assert!(clean.output.is_empty());
1383 assert!(clean.frames.is_empty());
1384 assert!(clean.exception_handlers.is_empty());
1385 assert!(clean.spawned_tasks.is_empty());
1386 assert!(clean.held_sync_guards.is_empty());
1387 assert_eq!(clean.task_counter, 0);
1388 assert_eq!(clean.runtime_context_counter, 0);
1389 assert!(clean.deadlines.is_empty());
1390 assert!(clean.cancel_token.is_none());
1391 assert!(clean.interrupt_handlers.is_empty());
1392 assert!(clean.error_stack_trace.is_empty());
1393 assert!(clean.bridge.is_none());
1394 assert!(clean
1395 .globals
1396 .get("stable_global")
1397 .is_some_and(|value| value.display() == "baseline"));
1398 }
1399
1400 #[tokio::test]
1401 async fn inline_child_inherits_held_lock_keys_but_concurrent_child_does_not() {
1402 let mut parent = Vm::new();
1403 let permit = parent
1404 .sync_runtime
1405 .acquire("mutex", "v:test", 1, 1, None, None)
1406 .await
1407 .unwrap()
1408 .unwrap();
1409 parent
1410 .held_sync_guards
1411 .push(crate::synchronization::VmSyncHeldGuard {
1412 _permit: permit,
1413 frame_depth: 0,
1414 env_scope_depth: 0,
1415 });
1416 assert_eq!(parent.held_permits_for("mutex", "v:test"), 1);
1417
1418 let inline = parent.child_vm_inline();
1423 assert_eq!(inline.held_permits_for("mutex", "v:test"), 1);
1424 assert_eq!(
1425 inline.child_vm_inline().held_permits_for("mutex", "v:test"),
1426 1
1427 );
1428
1429 let concurrent = parent.child_vm();
1433 assert_eq!(concurrent.held_permits_for("mutex", "v:test"), 0);
1434 }
1435
1436 #[test]
1437 fn vm_reports_effective_runtime_limits() {
1438 let vm = Vm::new();
1439
1440 assert_eq!(vm.runtime_limits(), RuntimeLimits::default());
1441 assert_eq!(
1442 vm.runtime_limit_report().entries.len(),
1443 crate::RUNTIME_LIMIT_DESCRIPTIONS.len()
1444 );
1445 assert_eq!(vm.child_vm().runtime_limits(), vm.runtime_limits());
1446 assert_eq!(
1447 vm.baseline().instantiate().runtime_limits(),
1448 vm.runtime_limits()
1449 );
1450 }
1451
1452 #[tokio::test(flavor = "current_thread")]
1453 async fn vm_baseline_rebinds_shared_state_builtins_per_instance() {
1454 let local = tokio::task::LocalSet::new();
1455 local
1456 .run_until(async {
1457 let source = r#"
1458pipeline main() {
1459 const cell = shared_cell({scope: "task_group", key: "turn", initial: 0})
1460 __io_println(shared_get(cell))
1461 shared_set(cell, shared_get(cell) + 1)
1462}"#;
1463 let chunk = crate::compile_source(source).expect("compile");
1464 let baseline = baseline_with_stdlib(source);
1465
1466 let mut first = baseline.instantiate();
1467 first.execute(&chunk).await.expect("first execute");
1468 assert_eq!(first.output(), "0\n");
1469
1470 let mut second = baseline.instantiate();
1471 second.execute(&chunk).await.expect("second execute");
1472 assert_eq!(
1473 second.output(),
1474 "0\n",
1475 "shared state created by the first VM must not leak into the next baseline instance"
1476 );
1477 })
1478 .await;
1479 }
1480}