1use std::collections::{BTreeMap, HashMap, HashSet};
2use std::sync::Arc;
3use std::time::Instant;
4
5use crate::chunk::{Chunk, ChunkRef, Constant};
6use crate::runtime_limits::RuntimeLimits;
7use crate::value::{
8 ModuleFunctionRegistry, VmAsyncBuiltinFn, VmBuiltinFn, VmEnv, VmError, VmTaskHandle, VmValue,
9};
10use crate::BuiltinId;
11
12use super::debug::DebugHook;
13use super::modules::LoadedModule;
14use super::VmBuiltinMetadata;
15
16pub(crate) struct ScopeSpan(u64);
18
19impl ScopeSpan {
20 pub(crate) fn new(kind: crate::tracing::SpanKind, name: String) -> Self {
21 Self(crate::tracing::span_start(kind, name))
22 }
23}
24
25impl Drop for ScopeSpan {
26 fn drop(&mut self) {
27 crate::tracing::span_end(self.0);
28 }
29}
30
31#[derive(Clone)]
32pub(crate) struct LocalSlot {
33 pub(crate) value: VmValue,
34 pub(crate) initialized: bool,
35 pub(crate) synced: bool,
36}
37
38impl Drop for LocalSlot {
39 fn drop(&mut self) {
40 if crate::value::recursion::is_recursive_container(&self.value) {
48 crate::value::recursion::dismantle(std::mem::replace(&mut self.value, VmValue::Nil));
49 }
50 }
51}
52
53#[derive(Clone)]
54pub(crate) struct InterruptHandler {
55 pub(crate) handle: i64,
56 pub(crate) signals: Vec<String>,
57 pub(crate) once: bool,
58 pub(crate) graceful_timeout_ms: Option<u64>,
59 pub(crate) handler: VmValue,
60}
61
62pub(crate) struct CallFrame {
64 pub(crate) chunk: ChunkRef,
65 pub(crate) ip: usize,
66 pub(crate) stack_base: usize,
67 pub(crate) saved_env: VmEnv,
68 pub(crate) initial_env: Option<VmEnv>,
76 pub(crate) initial_local_slots: Option<Vec<LocalSlot>>,
77 pub(crate) saved_iterator_depth: usize,
79 pub(crate) fn_name: String,
81 pub(crate) argc: usize,
83 pub(crate) saved_source_dir: Option<std::path::PathBuf>,
86 pub(crate) module_functions: Option<ModuleFunctionRegistry>,
88 pub(crate) module_state: Option<crate::value::ModuleState>,
94 pub(crate) local_slots: Vec<LocalSlot>,
96 pub(crate) local_scope_base: usize,
98 pub(crate) local_scope_depth: usize,
100}
101
102pub(crate) struct ExceptionHandler {
104 pub(crate) catch_ip: usize,
105 pub(crate) stack_depth: usize,
106 pub(crate) frame_depth: usize,
107 pub(crate) env_scope_depth: usize,
108 pub(crate) error_type: Option<crate::value::HarnStr>,
110}
111
112pub(crate) struct TaskScope {
115 pub(crate) task_ids: Vec<String>,
118 pub(crate) frame_depth: usize,
120 pub(crate) env_scope_depth: usize,
122}
123
124pub(crate) enum IterState {
126 Vec {
127 items: Arc<Vec<VmValue>>,
128 idx: usize,
129 },
130 Dict {
131 entries: Arc<crate::value::DictMap>,
132 keys: Vec<String>,
133 idx: usize,
134 },
135 Channel {
136 receiver: std::sync::Arc<tokio::sync::Mutex<tokio::sync::mpsc::Receiver<VmValue>>>,
137 close: std::sync::Arc<crate::value::VmChannelCloseState>,
138 },
139 Generator {
140 gen: Arc<crate::value::VmGenerator>,
141 },
142 Stream {
143 stream: Arc<crate::value::VmStream>,
144 },
145 Range {
149 next: i64,
150 end: i64,
151 inclusive: bool,
152 done: bool,
153 },
154 VmIter {
155 handle: crate::vm::iter::VmIterHandle,
156 },
157}
158
159#[derive(Clone)]
160pub(crate) enum VmBuiltinDispatch {
161 Sync(VmBuiltinFn),
162 Async(VmAsyncBuiltinFn),
163}
164
165#[derive(Clone)]
166pub(crate) struct VmBuiltinEntry {
167 pub(crate) name: Arc<str>,
168 pub(crate) dispatch: VmBuiltinDispatch,
169}
170
171pub struct Vm {
173 pub(crate) stack: Vec<VmValue>,
174 pub(crate) env: VmEnv,
175 pub(crate) output: String,
176 pub(crate) builtins: Arc<BTreeMap<String, VmBuiltinFn>>,
177 pub(crate) async_builtins: Arc<BTreeMap<String, VmAsyncBuiltinFn>>,
178 pub(crate) builtin_metadata: Arc<BTreeMap<String, VmBuiltinMetadata>>,
179 pub(crate) builtins_by_id: Arc<HashMap<BuiltinId, VmBuiltinEntry>>,
182 pub(crate) builtin_id_collisions: Arc<HashSet<BuiltinId>>,
185 pub(crate) iterators: Vec<IterState>,
187 pub(crate) frames: Vec<CallFrame>,
189 pub(crate) exception_handlers: Vec<ExceptionHandler>,
191 pub(crate) spawned_tasks: BTreeMap<String, VmTaskHandle>,
193 pub(crate) sync_runtime: Arc<crate::synchronization::VmSyncRuntime>,
195 pub(crate) shared_state_runtime: Arc<crate::shared_state::VmSharedStateRuntime>,
197 pub(crate) inline_caches: HashMap<u64, Vec<crate::chunk::InlineCacheEntry>>,
199 pub(crate) pool_registry: Arc<crate::stdlib::pool::PoolRegistry>,
201 pub(crate) wait_for_graph: Arc<crate::wait_for_graph::VmWaitForGraph>,
203 pub(crate) held_sync_guards: Vec<crate::synchronization::VmSyncHeldGuard>,
205 pub(crate) inherited_held_keys: Arc<Vec<crate::synchronization::VmSyncHeldKey>>,
213 pub(crate) task_scopes: Vec<TaskScope>,
219 pub(crate) task_counter: u64,
221 pub(crate) runtime_context_counter: u64,
223 pub(crate) runtime_context: crate::runtime_context::RuntimeContext,
225 pub(crate) deadlines: Vec<(Instant, usize)>,
227 pub(crate) breakpoints: BTreeMap<String, std::collections::BTreeSet<usize>>,
232 pub(crate) function_breakpoints: std::collections::BTreeSet<String>,
238 pub(crate) pending_function_bp: Option<String>,
243 pub(crate) step_mode: bool,
245 pub(crate) step_frame_depth: usize,
247 pub(crate) stopped: bool,
249 pub(crate) last_line: usize,
251 pub(crate) source_dir: Option<std::path::PathBuf>,
253 pub(crate) imported_paths: Vec<std::path::PathBuf>,
255 pub(crate) deferred_cyclic_imports: Vec<super::modules::DeferredCyclicImport>,
259 pub(crate) module_cache: Arc<BTreeMap<std::path::PathBuf, LoadedModule>>,
261 pub(crate) source_cache: Arc<BTreeMap<std::path::PathBuf, String>>,
263 pub(crate) source_file: Option<String>,
265 pub(crate) source_text: Option<String>,
267 pub(crate) bridge: Option<Arc<crate::bridge::HostBridge>>,
269 pub(crate) denied_builtins: Arc<HashSet<String>>,
271 pub(crate) cancel_token: Option<std::sync::Arc<std::sync::atomic::AtomicBool>>,
273 pub(crate) interrupt_signal_token: Option<std::sync::Arc<std::sync::Mutex<Option<String>>>>,
274 pub(crate) cancel_grace_instructions_remaining: Option<usize>,
279 pub(crate) interrupt_handlers: Vec<InterruptHandler>,
281 pub(crate) next_interrupt_handle: i64,
282 pub(crate) pending_interrupt_signal: Option<String>,
283 pub(crate) interrupted: bool,
284 pub(crate) dispatching_interrupt: bool,
285 pub(crate) interrupt_handler_deadline: Option<Instant>,
286 pub(crate) error_stack_trace: Vec<(String, usize, usize, Option<String>)>,
288 pub(crate) yield_sender: Option<tokio::sync::mpsc::Sender<Result<VmValue, VmError>>>,
291 pub(crate) project_root: Option<std::path::PathBuf>,
294 pub(crate) globals: Arc<crate::value::DictMap>,
297 pub(crate) debug_hook: Option<parking_lot::Mutex<Box<DebugHook>>>,
299 pub(crate) runtime_limits: RuntimeLimits,
301}
302
303#[derive(Clone)]
311pub struct VmBaseline {
312 builtins: Arc<BTreeMap<String, VmBuiltinFn>>,
313 async_builtins: Arc<BTreeMap<String, VmAsyncBuiltinFn>>,
314 builtin_metadata: Arc<BTreeMap<String, VmBuiltinMetadata>>,
315 builtins_by_id: Arc<HashMap<BuiltinId, VmBuiltinEntry>>,
316 builtin_id_collisions: Arc<HashSet<BuiltinId>>,
317 source_dir: Option<std::path::PathBuf>,
318 source_file: Option<String>,
319 source_text: Option<String>,
320 project_root: Option<std::path::PathBuf>,
321 globals: Arc<crate::value::DictMap>,
322 denied_builtins: Arc<HashSet<String>>,
323 runtime_limits: RuntimeLimits,
324}
325
326impl VmBaseline {
327 pub fn from_vm(vm: &Vm) -> Self {
328 Self {
329 builtins: Arc::clone(&vm.builtins),
330 async_builtins: Arc::clone(&vm.async_builtins),
331 builtin_metadata: Arc::clone(&vm.builtin_metadata),
332 builtins_by_id: Arc::clone(&vm.builtins_by_id),
333 builtin_id_collisions: Arc::clone(&vm.builtin_id_collisions),
334 source_dir: vm.source_dir.clone(),
335 source_file: vm.source_file.clone(),
336 source_text: vm.source_text.clone(),
337 project_root: vm.project_root.clone(),
338 globals: Arc::clone(&vm.globals),
339 denied_builtins: Arc::clone(&vm.denied_builtins),
340 runtime_limits: vm.runtime_limits,
341 }
342 }
343
344 pub fn instantiate(&self) -> Vm {
345 let mut source_cache = BTreeMap::new();
346 if let (Some(file), Some(text)) = (&self.source_file, &self.source_text) {
347 source_cache.insert(std::path::PathBuf::from(file), text.clone());
348 }
349 if let Some(dir) = &self.source_dir {
350 crate::stdlib::set_thread_source_dir(dir);
351 }
352
353 let mut vm = Vm {
354 stack: Vec::with_capacity(256),
355 env: VmEnv::new(),
356 output: String::new(),
357 builtins: Arc::clone(&self.builtins),
358 async_builtins: Arc::clone(&self.async_builtins),
359 builtin_metadata: Arc::clone(&self.builtin_metadata),
360 builtins_by_id: Arc::clone(&self.builtins_by_id),
361 builtin_id_collisions: Arc::clone(&self.builtin_id_collisions),
362 iterators: Vec::new(),
363 frames: Vec::new(),
364 exception_handlers: Vec::new(),
365 spawned_tasks: BTreeMap::new(),
366 sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
367 shared_state_runtime: Arc::new(crate::shared_state::VmSharedStateRuntime::new()),
368 inline_caches: HashMap::new(),
369 pool_registry: crate::stdlib::pool::new_pool_registry(),
370 wait_for_graph: Arc::new(crate::wait_for_graph::VmWaitForGraph::new()),
371 held_sync_guards: Vec::new(),
372 inherited_held_keys: Arc::new(Vec::new()),
373 task_scopes: Vec::new(),
374 task_counter: 0,
375 runtime_context_counter: 0,
376 runtime_context: crate::runtime_context::RuntimeContext::root(),
377 deadlines: Vec::new(),
378 breakpoints: BTreeMap::new(),
379 function_breakpoints: std::collections::BTreeSet::new(),
380 pending_function_bp: None,
381 step_mode: false,
382 step_frame_depth: 0,
383 stopped: false,
384 last_line: 0,
385 source_dir: self.source_dir.clone(),
386 imported_paths: Vec::new(),
387 deferred_cyclic_imports: Vec::new(),
388 module_cache: Arc::new(BTreeMap::new()),
389 source_cache: Arc::new(source_cache),
390 source_file: self.source_file.clone(),
391 source_text: self.source_text.clone(),
392 bridge: None,
393 denied_builtins: Arc::clone(&self.denied_builtins),
394 cancel_token: None,
395 interrupt_signal_token: None,
396 cancel_grace_instructions_remaining: None,
397 interrupt_handlers: Vec::new(),
398 next_interrupt_handle: 1,
399 pending_interrupt_signal: None,
400 interrupted: false,
401 dispatching_interrupt: false,
402 interrupt_handler_deadline: None,
403 error_stack_trace: Vec::new(),
404 yield_sender: None,
405 project_root: self.project_root.clone(),
406 globals: Arc::clone(&self.globals),
407 debug_hook: None,
408 runtime_limits: self.runtime_limits,
409 };
410
411 crate::stdlib::rebind_execution_state_builtins(&mut vm);
412 vm
413 }
414}
415
416impl Vm {
417 pub(crate) fn fresh_local_slots(chunk: &Chunk) -> Vec<LocalSlot> {
418 chunk
419 .local_slots
420 .iter()
421 .map(|_| LocalSlot {
422 value: VmValue::Nil,
423 initialized: false,
424 synced: false,
425 })
426 .collect()
427 }
428
429 pub(crate) fn bind_param_slots(
430 slots: &mut [LocalSlot],
431 func: &crate::chunk::CompiledFunction,
432 args: &[VmValue],
433 synced: bool,
434 ) {
435 Self::bind_param_slots_args(slots, func, &super::CallArgs::Slice(args), synced);
436 }
437
438 pub(crate) fn bind_param_slots_args(
439 slots: &mut [LocalSlot],
440 func: &crate::chunk::CompiledFunction,
441 args: &super::CallArgs<'_>,
442 synced: bool,
443 ) {
444 let param_count = func.params.len();
445 for (i, _param) in func.params.iter().enumerate() {
446 if i >= slots.len() {
447 break;
448 }
449 if func.has_rest_param && i == param_count - 1 {
450 let rest_args = args.to_vec_from(i);
451 slots[i].value = VmValue::List(std::sync::Arc::new(rest_args));
452 slots[i].initialized = true;
453 slots[i].synced = synced;
454 } else if let Some(arg) = args.get(i) {
455 slots[i].value = arg.clone();
456 slots[i].initialized = true;
457 slots[i].synced = synced;
458 }
459 }
460 }
461
462 pub(crate) fn visible_variables(&self) -> crate::value::DictMap {
463 let mut vars = self.env.all_variables();
464 let Some(frame) = self.frames.last() else {
465 return vars;
466 };
467 for (slot, info) in frame.local_slots.iter().zip(frame.chunk.local_slots.iter()) {
468 if slot.initialized && info.scope_depth <= frame.local_scope_depth {
469 vars.insert(crate::value::intern_key(&info.name), slot.value.clone());
470 }
471 }
472 vars
473 }
474
475 pub(crate) fn sync_current_frame_locals_to_env(&mut self) {
476 let frames = &mut self.frames;
477 let env = &mut self.env;
478 let Some(frame) = frames.last_mut() else {
479 return;
480 };
481 let local_scope_base = frame.local_scope_base;
482 let local_scope_depth = frame.local_scope_depth;
483 for (slot, info) in frame
484 .local_slots
485 .iter_mut()
486 .zip(frame.chunk.local_slots.iter())
487 {
488 if slot.initialized && !slot.synced && info.scope_depth <= local_scope_depth {
489 slot.synced = true;
490 let scope_idx = local_scope_base + info.scope_depth;
491 while env.scopes.len() <= scope_idx {
492 env.push_scope();
493 }
494 Arc::make_mut(&mut env.scopes[scope_idx].vars)
495 .insert(info.name.clone(), (slot.value.clone(), info.mutable));
496 }
497 }
498 }
499
500 pub(crate) fn closure_call_env_for_current_frame(
501 &self,
502 closure: &crate::value::VmClosure,
503 ) -> VmEnv {
504 if closure.module_state().is_some() {
505 return closure.env.cloned_for_call();
506 }
507 let call_env = Self::closure_call_env(&self.env, closure);
508 if !closure.func.chunk.references_outer_names {
513 return call_env;
514 }
515 let mut call_env = call_env;
516 let Some(frame) = self.frames.last() else {
517 return call_env;
518 };
519 for (slot, info) in frame
520 .local_slots
521 .iter()
522 .zip(frame.chunk.local_slots.iter())
523 .filter(|(slot, info)| slot.initialized && info.scope_depth <= frame.local_scope_depth)
524 {
525 if matches!(slot.value, VmValue::Closure(_)) && !call_env.contains(&info.name) {
526 let _ = call_env.define(&info.name, slot.value.clone(), info.mutable);
527 }
528 }
529 call_env
530 }
531
532 pub(crate) fn active_local_slot_value(&self, name: &str) -> Option<VmValue> {
533 let frame = self.frames.last()?;
534 let idx = self.active_local_slot_index(name)?;
535 frame.local_slots.get(idx).map(|slot| slot.value.clone())
536 }
537
538 pub(crate) fn active_local_slot_index(&self, name: &str) -> Option<usize> {
543 let frame = self.frames.last()?;
544 for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
545 if info.name == name && info.scope_depth <= frame.local_scope_depth {
546 if let Some(slot) = frame.local_slots.get(idx) {
547 if slot.initialized {
548 return Some(idx);
549 }
550 }
551 }
552 }
553 None
554 }
555
556 pub(crate) fn assign_active_local_slot(
557 &mut self,
558 name: &str,
559 value: VmValue,
560 debug: bool,
561 ) -> Result<bool, VmError> {
562 let Some(frame) = self.frames.last_mut() else {
563 return Ok(false);
564 };
565 for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
566 if info.name == name && info.scope_depth <= frame.local_scope_depth {
567 if !debug && !info.mutable {
568 return Err(VmError::ImmutableAssignment(name.to_string()));
569 }
570 if let Some(slot) = frame.local_slots.get_mut(idx) {
571 crate::value::recursion::dismantle(std::mem::replace(&mut slot.value, value));
572 slot.initialized = true;
573 slot.synced = false;
574 return Ok(true);
575 }
576 }
577 }
578 Ok(false)
579 }
580
581 pub fn new() -> Self {
582 Self {
583 stack: Vec::with_capacity(256),
584 env: VmEnv::new(),
585 output: String::new(),
586 builtins: Arc::new(BTreeMap::new()),
587 async_builtins: Arc::new(BTreeMap::new()),
588 builtin_metadata: Arc::new(BTreeMap::new()),
589 builtins_by_id: Arc::new(HashMap::new()),
590 builtin_id_collisions: Arc::new(HashSet::new()),
591 iterators: Vec::new(),
592 frames: Vec::new(),
593 exception_handlers: Vec::new(),
594 spawned_tasks: BTreeMap::new(),
595 sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
596 shared_state_runtime: Arc::new(crate::shared_state::VmSharedStateRuntime::new()),
597 inline_caches: HashMap::new(),
598 pool_registry: crate::stdlib::pool::new_pool_registry(),
599 wait_for_graph: Arc::new(crate::wait_for_graph::VmWaitForGraph::new()),
600 held_sync_guards: Vec::new(),
601 inherited_held_keys: Arc::new(Vec::new()),
602 task_scopes: Vec::new(),
603 task_counter: 0,
604 runtime_context_counter: 0,
605 runtime_context: crate::runtime_context::RuntimeContext::root(),
606 deadlines: Vec::new(),
607 breakpoints: BTreeMap::new(),
608 function_breakpoints: std::collections::BTreeSet::new(),
609 pending_function_bp: None,
610 step_mode: false,
611 step_frame_depth: 0,
612 stopped: false,
613 last_line: 0,
614 source_dir: None,
615 imported_paths: Vec::new(),
616 deferred_cyclic_imports: Vec::new(),
617 module_cache: Arc::new(BTreeMap::new()),
618 source_cache: Arc::new(BTreeMap::new()),
619 source_file: None,
620 source_text: None,
621 bridge: None,
622 denied_builtins: Arc::new(HashSet::new()),
623 cancel_token: None,
624 interrupt_signal_token: None,
625 cancel_grace_instructions_remaining: None,
626 interrupt_handlers: Vec::new(),
627 next_interrupt_handle: 1,
628 pending_interrupt_signal: None,
629 interrupted: false,
630 dispatching_interrupt: false,
631 interrupt_handler_deadline: None,
632 error_stack_trace: Vec::new(),
633 yield_sender: None,
634 project_root: None,
635 globals: Arc::new(crate::value::DictMap::new()),
636 debug_hook: None,
637 runtime_limits: RuntimeLimits::default(),
638 }
639 }
640
641 pub fn baseline(&self) -> VmBaseline {
642 VmBaseline::from_vm(self)
643 }
644
645 pub fn runtime_limits(&self) -> RuntimeLimits {
647 self.runtime_limits
648 }
649
650 pub fn runtime_limit_report(&self) -> crate::RuntimeLimitsReport {
652 self.runtime_limits.report()
653 }
654
655 #[inline]
669 pub(crate) fn debugger_attached(&self) -> bool {
670 self.debug_hook.is_some()
671 || !self.breakpoints.is_empty()
672 || !self.function_breakpoints.is_empty()
673 }
674
675 pub fn set_bridge(&mut self, bridge: Arc<crate::bridge::HostBridge>) {
677 self.bridge = Some(bridge);
678 }
679
680 pub fn set_denied_builtins(&mut self, denied: HashSet<String>) {
683 self.denied_builtins = Arc::new(denied);
684 }
685
686 pub fn set_source_info(&mut self, file: &str, text: &str) {
688 self.source_file = Some(file.to_string());
689 self.source_text = Some(text.to_string());
690 Arc::make_mut(&mut self.source_cache)
691 .insert(std::path::PathBuf::from(file), text.to_string());
692 }
693
694 pub fn start(&mut self, chunk: &Chunk) {
696 let debugger = self.debugger_attached();
703 let initial_env = if debugger {
704 Some(self.env.clone())
705 } else {
706 None
707 };
708 let initial_local_slots = if debugger {
709 Some(Self::fresh_local_slots(chunk))
710 } else {
711 None
712 };
713 self.frames.push(CallFrame {
714 chunk: Arc::new(chunk.clone()),
715 ip: 0,
716 stack_base: self.stack.len(),
717 saved_env: self.env.clone(),
718 initial_env,
719 initial_local_slots,
720 saved_iterator_depth: self.iterators.len(),
721 fn_name: String::new(),
722 argc: 0,
723 saved_source_dir: None,
724 module_functions: None,
725 module_state: None,
726 local_slots: Self::fresh_local_slots(chunk),
727 local_scope_base: self.env.scope_depth().saturating_sub(1),
728 local_scope_depth: 0,
729 });
730 }
731
732 pub(crate) fn child_vm(&self) -> Vm {
735 Vm {
736 stack: Vec::with_capacity(64),
737 env: self.env.clone(),
738 output: String::new(),
739 builtins: Arc::clone(&self.builtins),
740 async_builtins: Arc::clone(&self.async_builtins),
741 builtin_metadata: Arc::clone(&self.builtin_metadata),
742 builtins_by_id: Arc::clone(&self.builtins_by_id),
743 builtin_id_collisions: Arc::clone(&self.builtin_id_collisions),
744 iterators: Vec::new(),
745 frames: Vec::new(),
746 exception_handlers: Vec::new(),
747 spawned_tasks: BTreeMap::new(),
748 sync_runtime: self.sync_runtime.clone(),
749 shared_state_runtime: self.shared_state_runtime.clone(),
750 inline_caches: HashMap::new(),
751 pool_registry: self.pool_registry.clone(),
752 wait_for_graph: self.wait_for_graph.clone(),
753 held_sync_guards: Vec::new(),
754 inherited_held_keys: Arc::new(Vec::new()),
755 task_scopes: Vec::new(),
756 task_counter: 0,
757 runtime_context_counter: self.runtime_context_counter,
758 runtime_context: self.runtime_context.clone(),
759 deadlines: self.deadlines.clone(),
760 breakpoints: BTreeMap::new(),
761 function_breakpoints: std::collections::BTreeSet::new(),
762 pending_function_bp: None,
763 step_mode: false,
764 step_frame_depth: 0,
765 stopped: false,
766 last_line: 0,
767 source_dir: self.source_dir.clone(),
768 imported_paths: Vec::new(),
769 deferred_cyclic_imports: Vec::new(),
770 module_cache: Arc::clone(&self.module_cache),
771 source_cache: Arc::clone(&self.source_cache),
772 source_file: self.source_file.clone(),
773 source_text: self.source_text.clone(),
774 bridge: self.bridge.clone(),
775 denied_builtins: Arc::clone(&self.denied_builtins),
776 cancel_token: self.cancel_token.clone(),
777 interrupt_signal_token: self.interrupt_signal_token.clone(),
778 cancel_grace_instructions_remaining: None,
779 interrupt_handlers: Vec::new(),
780 next_interrupt_handle: 1,
781 pending_interrupt_signal: None,
782 interrupted: self.interrupted,
783 dispatching_interrupt: false,
784 interrupt_handler_deadline: None,
785 error_stack_trace: Vec::new(),
786 yield_sender: None,
787 project_root: self.project_root.clone(),
788 globals: Arc::clone(&self.globals),
789 debug_hook: None,
790 runtime_limits: self.runtime_limits,
791 }
792 }
793
794 pub(crate) fn child_vm_for_host(&self) -> Vm {
797 self.child_vm()
798 }
799
800 pub(crate) fn cancel_spawned_tasks(&mut self) {
804 for (_, task) in std::mem::take(&mut self.spawned_tasks) {
805 task.cancel_token
806 .store(true, std::sync::atomic::Ordering::SeqCst);
807 task.handle.abort();
808 }
809 }
810
811 pub fn set_source_dir(&mut self, dir: &std::path::Path) {
814 let dir = crate::stdlib::process::normalize_context_path(dir);
815 self.source_dir = Some(dir.clone());
816 crate::stdlib::set_thread_source_dir(&dir);
817 if self.project_root.is_none() {
819 self.project_root = crate::stdlib::process::find_project_root(&dir);
820 }
821 }
822
823 pub fn set_project_root(&mut self, root: &std::path::Path) {
826 self.project_root = Some(root.to_path_buf());
827 }
828
829 pub fn project_root(&self) -> Option<&std::path::Path> {
831 self.project_root.as_deref().or(self.source_dir.as_deref())
832 }
833
834 pub fn builtin_names(&self) -> Vec<String> {
836 let mut names: Vec<String> = self.builtins.keys().cloned().collect();
837 names.extend(self.async_builtins.keys().cloned());
838 names
839 }
840
841 pub fn builtin_metadata(&self) -> Vec<VmBuiltinMetadata> {
843 self.builtin_metadata.values().cloned().collect()
844 }
845
846 pub fn builtin_metadata_for(&self, name: &str) -> Option<&VmBuiltinMetadata> {
848 self.builtin_metadata.get(name)
849 }
850
851 pub fn set_global(&mut self, name: &str, value: VmValue) {
854 Arc::make_mut(&mut self.globals).insert(crate::value::intern_key(name), value);
855 }
856
857 pub fn global(&self, name: &str) -> Option<&VmValue> {
863 self.globals.get(name)
864 }
865
866 pub fn set_harness(&mut self, harness: crate::harness::Harness) {
872 self.set_global("harness", harness.into_vm_value());
873 }
874
875 pub fn output(&self) -> &str {
877 &self.output
878 }
879
880 pub fn take_output(&mut self) -> String {
884 std::mem::take(&mut self.output)
885 }
886
887 pub fn append_output(&mut self, text: &str) {
891 self.output.push_str(text);
892 }
893
894 pub(crate) fn pop(&mut self) -> Result<VmValue, VmError> {
895 self.stack.pop().ok_or(VmError::StackUnderflow)
896 }
897
898 pub(crate) fn peek(&self) -> Result<&VmValue, VmError> {
899 self.stack.last().ok_or(VmError::StackUnderflow)
900 }
901
902 pub(crate) fn const_str(c: &Constant) -> Result<&str, VmError> {
903 match c {
904 Constant::String(s) => Ok(s.as_str()),
905 _ => Err(VmError::TypeError("expected string constant".into())),
906 }
907 }
908
909 pub(crate) fn release_sync_guards_for_current_scope(&mut self) {
910 let depth = self.env.scope_depth();
911 self.held_sync_guards
912 .retain(|guard| guard.env_scope_depth < depth);
913 self.cancel_task_scopes_where(|s| s.env_scope_depth >= depth);
916 }
917
918 pub(crate) fn release_sync_guards_after_unwind(
919 &mut self,
920 frame_depth: usize,
921 env_scope_depth: usize,
922 ) {
923 self.held_sync_guards.retain(|guard| {
924 guard.frame_depth <= frame_depth && guard.env_scope_depth <= env_scope_depth
925 });
926 self.cancel_task_scopes_where(|s| {
929 !(s.frame_depth <= frame_depth && s.env_scope_depth <= env_scope_depth)
930 });
931 }
932
933 pub(crate) fn release_sync_guards_for_frame(&mut self, frame_depth: usize) {
934 self.held_sync_guards
935 .retain(|guard| guard.frame_depth != frame_depth);
936 self.cancel_task_scopes_where(|s| s.frame_depth == frame_depth);
939 }
940
941 pub(crate) fn adopt_sync_permit_for_current_scope(
942 &mut self,
943 permit: crate::value::VmSyncPermitHandle,
944 ) {
945 if permit.is_released()
946 || self
947 .held_sync_guards
948 .iter()
949 .any(|guard| guard._permit.same_lease(&permit))
950 {
951 return;
952 }
953 self.held_sync_guards
954 .push(crate::synchronization::VmSyncHeldGuard {
955 _permit: permit,
956 frame_depth: self.frames.len(),
957 env_scope_depth: self.env.scope_depth(),
958 });
959 }
960
961 pub(crate) fn deregister_task_from_scopes(&mut self, id: &str) {
964 for scope in &mut self.task_scopes {
965 scope.task_ids.retain(|t| t != id);
966 }
967 }
968
969 fn cancel_task_scopes_where<F: Fn(&TaskScope) -> bool>(&mut self, doomed: F) {
972 let mut i = 0;
973 while i < self.task_scopes.len() {
974 if doomed(&self.task_scopes[i]) {
975 let scope = self.task_scopes.remove(i);
976 for id in &scope.task_ids {
977 if let Some(task) = self.spawned_tasks.remove(id) {
978 task.cancel_token
979 .store(true, std::sync::atomic::Ordering::SeqCst);
980 task.handle.abort();
981 }
982 }
983 } else {
984 i += 1;
985 }
986 }
987 }
988
989 pub(crate) fn held_permits_for(&self, kind: &str, key: &str) -> u32 {
993 let own: u32 = self
994 .held_sync_guards
995 .iter()
996 .filter(|guard| {
997 !guard._permit.is_released()
998 && guard._permit.kind() == kind
999 && guard._permit.key() == key
1000 })
1001 .map(|guard| guard._permit.permits())
1002 .sum();
1003 let inherited: u32 = self
1004 .inherited_held_keys
1005 .iter()
1006 .filter(|held| held.kind == kind && held.key == key)
1007 .map(|held| held.permits)
1008 .sum();
1009 own + inherited
1010 }
1011
1012 pub(crate) fn combined_held_keys(&self) -> Vec<crate::synchronization::VmSyncHeldKey> {
1015 let mut keys: Vec<crate::synchronization::VmSyncHeldKey> = self
1016 .held_sync_guards
1017 .iter()
1018 .filter_map(|guard| crate::synchronization::VmSyncHeldKey::from_permit(&guard._permit))
1019 .collect();
1020 keys.extend(self.inherited_held_keys.iter().cloned());
1021 keys
1022 }
1023
1024 pub(crate) fn child_vm_inline(&self) -> Vm {
1030 let mut child = self.child_vm();
1031 child.inherited_held_keys = Arc::new(self.combined_held_keys());
1032 child
1033 }
1034}
1035
1036impl Drop for Vm {
1037 fn drop(&mut self) {
1038 self.cancel_spawned_tasks();
1039 }
1040}
1041
1042impl Default for Vm {
1043 fn default() -> Self {
1044 Self::new()
1045 }
1046}
1047
1048#[cfg(test)]
1049mod tests {
1050
1051 use super::*;
1052
1053 fn baseline_with_stdlib(source: &str) -> VmBaseline {
1054 let mut vm = Vm::new();
1055 crate::register_vm_stdlib(&mut vm);
1056 vm.set_source_info("baseline_test.harn", source);
1057 vm.set_global(
1058 "stable_global",
1059 VmValue::String(arcstr::ArcStr::from("baseline")),
1060 );
1061 vm.baseline()
1062 }
1063
1064 #[test]
1065 fn vm_baseline_instantiates_clean_mutable_execution_state() {
1066 let baseline = baseline_with_stdlib("pipeline main() { __io_println(stable_global) }");
1067
1068 let mut dirty = baseline.instantiate();
1069 dirty.stack.push(VmValue::Int(42));
1070 dirty.output.push_str("dirty");
1071 dirty.task_counter = 9;
1072 dirty.runtime_context_counter = 7;
1073 dirty
1074 .error_stack_trace
1075 .push(("main".to_string(), 1, 1, None));
1076
1077 let clean = baseline.instantiate();
1078 assert!(clean.stack.is_empty());
1079 assert!(clean.output.is_empty());
1080 assert!(clean.frames.is_empty());
1081 assert!(clean.exception_handlers.is_empty());
1082 assert!(clean.spawned_tasks.is_empty());
1083 assert!(clean.held_sync_guards.is_empty());
1084 assert_eq!(clean.task_counter, 0);
1085 assert_eq!(clean.runtime_context_counter, 0);
1086 assert!(clean.deadlines.is_empty());
1087 assert!(clean.cancel_token.is_none());
1088 assert!(clean.interrupt_handlers.is_empty());
1089 assert!(clean.error_stack_trace.is_empty());
1090 assert!(clean.bridge.is_none());
1091 assert!(clean
1092 .globals
1093 .get("stable_global")
1094 .is_some_and(|value| value.display() == "baseline"));
1095 }
1096
1097 #[tokio::test]
1098 async fn inline_child_inherits_held_lock_keys_but_concurrent_child_does_not() {
1099 let mut parent = Vm::new();
1100 let permit = parent
1101 .sync_runtime
1102 .acquire("mutex", "v:test", 1, 1, None, None)
1103 .await
1104 .unwrap()
1105 .unwrap();
1106 parent
1107 .held_sync_guards
1108 .push(crate::synchronization::VmSyncHeldGuard {
1109 _permit: permit,
1110 frame_depth: 0,
1111 env_scope_depth: 0,
1112 });
1113 assert_eq!(parent.held_permits_for("mutex", "v:test"), 1);
1114
1115 let inline = parent.child_vm_inline();
1120 assert_eq!(inline.held_permits_for("mutex", "v:test"), 1);
1121 assert_eq!(
1122 inline.child_vm_inline().held_permits_for("mutex", "v:test"),
1123 1
1124 );
1125
1126 let concurrent = parent.child_vm();
1130 assert_eq!(concurrent.held_permits_for("mutex", "v:test"), 0);
1131 }
1132
1133 #[test]
1134 fn vm_reports_effective_runtime_limits() {
1135 let vm = Vm::new();
1136
1137 assert_eq!(vm.runtime_limits(), RuntimeLimits::default());
1138 assert_eq!(
1139 vm.runtime_limit_report().entries.len(),
1140 crate::RUNTIME_LIMIT_DESCRIPTIONS.len()
1141 );
1142 assert_eq!(vm.child_vm().runtime_limits(), vm.runtime_limits());
1143 assert_eq!(
1144 vm.baseline().instantiate().runtime_limits(),
1145 vm.runtime_limits()
1146 );
1147 }
1148
1149 #[tokio::test(flavor = "current_thread")]
1150 async fn vm_baseline_rebinds_shared_state_builtins_per_instance() {
1151 let local = tokio::task::LocalSet::new();
1152 local
1153 .run_until(async {
1154 let source = r#"
1155pipeline main() {
1156 let cell = shared_cell({scope: "task_group", key: "turn", initial: 0})
1157 __io_println(shared_get(cell))
1158 shared_set(cell, shared_get(cell) + 1)
1159}"#;
1160 let chunk = crate::compile_source(source).expect("compile");
1161 let baseline = baseline_with_stdlib(source);
1162
1163 let mut first = baseline.instantiate();
1164 first.execute(&chunk).await.expect("first execute");
1165 assert_eq!(first.output(), "0\n");
1166
1167 let mut second = baseline.instantiate();
1168 second.execute(&chunk).await.expect("second execute");
1169 assert_eq!(
1170 second.output(),
1171 "0\n",
1172 "shared state created by the first VM must not leak into the next baseline instance"
1173 );
1174 })
1175 .await;
1176 }
1177}