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<Arc<str>>,
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) module_cache: Arc<BTreeMap<std::path::PathBuf, LoadedModule>>,
257 pub(crate) source_cache: Arc<BTreeMap<std::path::PathBuf, String>>,
259 pub(crate) source_file: Option<String>,
261 pub(crate) source_text: Option<String>,
263 pub(crate) bridge: Option<Arc<crate::bridge::HostBridge>>,
265 pub(crate) denied_builtins: Arc<HashSet<String>>,
267 pub(crate) cancel_token: Option<std::sync::Arc<std::sync::atomic::AtomicBool>>,
269 pub(crate) interrupt_signal_token: Option<std::sync::Arc<std::sync::Mutex<Option<String>>>>,
270 pub(crate) cancel_grace_instructions_remaining: Option<usize>,
275 pub(crate) interrupt_handlers: Vec<InterruptHandler>,
277 pub(crate) next_interrupt_handle: i64,
278 pub(crate) pending_interrupt_signal: Option<String>,
279 pub(crate) interrupted: bool,
280 pub(crate) dispatching_interrupt: bool,
281 pub(crate) interrupt_handler_deadline: Option<Instant>,
282 pub(crate) error_stack_trace: Vec<(String, usize, usize, Option<String>)>,
284 pub(crate) yield_sender: Option<tokio::sync::mpsc::Sender<Result<VmValue, VmError>>>,
287 pub(crate) project_root: Option<std::path::PathBuf>,
290 pub(crate) globals: Arc<crate::value::DictMap>,
293 pub(crate) debug_hook: Option<parking_lot::Mutex<Box<DebugHook>>>,
295 pub(crate) runtime_limits: RuntimeLimits,
297}
298
299#[derive(Clone)]
307pub struct VmBaseline {
308 builtins: Arc<BTreeMap<String, VmBuiltinFn>>,
309 async_builtins: Arc<BTreeMap<String, VmAsyncBuiltinFn>>,
310 builtin_metadata: Arc<BTreeMap<String, VmBuiltinMetadata>>,
311 builtins_by_id: Arc<HashMap<BuiltinId, VmBuiltinEntry>>,
312 builtin_id_collisions: Arc<HashSet<BuiltinId>>,
313 source_dir: Option<std::path::PathBuf>,
314 source_file: Option<String>,
315 source_text: Option<String>,
316 project_root: Option<std::path::PathBuf>,
317 globals: Arc<crate::value::DictMap>,
318 denied_builtins: Arc<HashSet<String>>,
319 runtime_limits: RuntimeLimits,
320}
321
322impl VmBaseline {
323 pub fn from_vm(vm: &Vm) -> Self {
324 Self {
325 builtins: Arc::clone(&vm.builtins),
326 async_builtins: Arc::clone(&vm.async_builtins),
327 builtin_metadata: Arc::clone(&vm.builtin_metadata),
328 builtins_by_id: Arc::clone(&vm.builtins_by_id),
329 builtin_id_collisions: Arc::clone(&vm.builtin_id_collisions),
330 source_dir: vm.source_dir.clone(),
331 source_file: vm.source_file.clone(),
332 source_text: vm.source_text.clone(),
333 project_root: vm.project_root.clone(),
334 globals: Arc::clone(&vm.globals),
335 denied_builtins: Arc::clone(&vm.denied_builtins),
336 runtime_limits: vm.runtime_limits,
337 }
338 }
339
340 pub fn instantiate(&self) -> Vm {
341 let mut source_cache = BTreeMap::new();
342 if let (Some(file), Some(text)) = (&self.source_file, &self.source_text) {
343 source_cache.insert(std::path::PathBuf::from(file), text.clone());
344 }
345 if let Some(dir) = &self.source_dir {
346 crate::stdlib::set_thread_source_dir(dir);
347 }
348
349 let mut vm = Vm {
350 stack: Vec::with_capacity(256),
351 env: VmEnv::new(),
352 output: String::new(),
353 builtins: Arc::clone(&self.builtins),
354 async_builtins: Arc::clone(&self.async_builtins),
355 builtin_metadata: Arc::clone(&self.builtin_metadata),
356 builtins_by_id: Arc::clone(&self.builtins_by_id),
357 builtin_id_collisions: Arc::clone(&self.builtin_id_collisions),
358 iterators: Vec::new(),
359 frames: Vec::new(),
360 exception_handlers: Vec::new(),
361 spawned_tasks: BTreeMap::new(),
362 sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
363 shared_state_runtime: Arc::new(crate::shared_state::VmSharedStateRuntime::new()),
364 inline_caches: HashMap::new(),
365 pool_registry: crate::stdlib::pool::new_pool_registry(),
366 wait_for_graph: Arc::new(crate::wait_for_graph::VmWaitForGraph::new()),
367 held_sync_guards: Vec::new(),
368 inherited_held_keys: Arc::new(Vec::new()),
369 task_scopes: Vec::new(),
370 task_counter: 0,
371 runtime_context_counter: 0,
372 runtime_context: crate::runtime_context::RuntimeContext::root(),
373 deadlines: Vec::new(),
374 breakpoints: BTreeMap::new(),
375 function_breakpoints: std::collections::BTreeSet::new(),
376 pending_function_bp: None,
377 step_mode: false,
378 step_frame_depth: 0,
379 stopped: false,
380 last_line: 0,
381 source_dir: self.source_dir.clone(),
382 imported_paths: Vec::new(),
383 module_cache: Arc::new(BTreeMap::new()),
384 source_cache: Arc::new(source_cache),
385 source_file: self.source_file.clone(),
386 source_text: self.source_text.clone(),
387 bridge: None,
388 denied_builtins: Arc::clone(&self.denied_builtins),
389 cancel_token: None,
390 interrupt_signal_token: None,
391 cancel_grace_instructions_remaining: None,
392 interrupt_handlers: Vec::new(),
393 next_interrupt_handle: 1,
394 pending_interrupt_signal: None,
395 interrupted: false,
396 dispatching_interrupt: false,
397 interrupt_handler_deadline: None,
398 error_stack_trace: Vec::new(),
399 yield_sender: None,
400 project_root: self.project_root.clone(),
401 globals: Arc::clone(&self.globals),
402 debug_hook: None,
403 runtime_limits: self.runtime_limits,
404 };
405
406 crate::stdlib::rebind_execution_state_builtins(&mut vm);
407 vm
408 }
409}
410
411impl Vm {
412 pub(crate) fn fresh_local_slots(chunk: &Chunk) -> Vec<LocalSlot> {
413 chunk
414 .local_slots
415 .iter()
416 .map(|_| LocalSlot {
417 value: VmValue::Nil,
418 initialized: false,
419 synced: false,
420 })
421 .collect()
422 }
423
424 pub(crate) fn bind_param_slots(
425 slots: &mut [LocalSlot],
426 func: &crate::chunk::CompiledFunction,
427 args: &[VmValue],
428 synced: bool,
429 ) {
430 Self::bind_param_slots_args(slots, func, &super::CallArgs::Slice(args), synced);
431 }
432
433 pub(crate) fn bind_param_slots_args(
434 slots: &mut [LocalSlot],
435 func: &crate::chunk::CompiledFunction,
436 args: &super::CallArgs<'_>,
437 synced: bool,
438 ) {
439 let param_count = func.params.len();
440 for (i, _param) in func.params.iter().enumerate() {
441 if i >= slots.len() {
442 break;
443 }
444 if func.has_rest_param && i == param_count - 1 {
445 let rest_args = args.to_vec_from(i);
446 slots[i].value = VmValue::List(std::sync::Arc::new(rest_args));
447 slots[i].initialized = true;
448 slots[i].synced = synced;
449 } else if let Some(arg) = args.get(i) {
450 slots[i].value = arg.clone();
451 slots[i].initialized = true;
452 slots[i].synced = synced;
453 }
454 }
455 }
456
457 pub(crate) fn visible_variables(&self) -> crate::value::DictMap {
458 let mut vars = self.env.all_variables();
459 let Some(frame) = self.frames.last() else {
460 return vars;
461 };
462 for (slot, info) in frame.local_slots.iter().zip(frame.chunk.local_slots.iter()) {
463 if slot.initialized && info.scope_depth <= frame.local_scope_depth {
464 vars.insert(info.name.clone(), slot.value.clone());
465 }
466 }
467 vars
468 }
469
470 pub(crate) fn sync_current_frame_locals_to_env(&mut self) {
471 let frames = &mut self.frames;
472 let env = &mut self.env;
473 let Some(frame) = frames.last_mut() else {
474 return;
475 };
476 let local_scope_base = frame.local_scope_base;
477 let local_scope_depth = frame.local_scope_depth;
478 for (slot, info) in frame
479 .local_slots
480 .iter_mut()
481 .zip(frame.chunk.local_slots.iter())
482 {
483 if slot.initialized && !slot.synced && info.scope_depth <= local_scope_depth {
484 slot.synced = true;
485 let scope_idx = local_scope_base + info.scope_depth;
486 while env.scopes.len() <= scope_idx {
487 env.push_scope();
488 }
489 Arc::make_mut(&mut env.scopes[scope_idx].vars)
490 .insert(info.name.clone(), (slot.value.clone(), info.mutable));
491 }
492 }
493 }
494
495 pub(crate) fn closure_call_env_for_current_frame(
496 &self,
497 closure: &crate::value::VmClosure,
498 ) -> VmEnv {
499 if closure.module_state().is_some() {
500 return closure.env.clone();
501 }
502 let call_env = Self::closure_call_env(&self.env, closure);
503 if !closure.func.chunk.references_outer_names {
508 return call_env;
509 }
510 let mut call_env = call_env;
511 let Some(frame) = self.frames.last() else {
512 return call_env;
513 };
514 for (slot, info) in frame
515 .local_slots
516 .iter()
517 .zip(frame.chunk.local_slots.iter())
518 .filter(|(slot, info)| slot.initialized && info.scope_depth <= frame.local_scope_depth)
519 {
520 if matches!(slot.value, VmValue::Closure(_)) && !call_env.contains(&info.name) {
521 let _ = call_env.define(&info.name, slot.value.clone(), info.mutable);
522 }
523 }
524 call_env
525 }
526
527 pub(crate) fn active_local_slot_value(&self, name: &str) -> Option<VmValue> {
528 let frame = self.frames.last()?;
529 let idx = self.active_local_slot_index(name)?;
530 frame.local_slots.get(idx).map(|slot| slot.value.clone())
531 }
532
533 pub(crate) fn active_local_slot_index(&self, name: &str) -> Option<usize> {
538 let frame = self.frames.last()?;
539 for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
540 if info.name == name && info.scope_depth <= frame.local_scope_depth {
541 if let Some(slot) = frame.local_slots.get(idx) {
542 if slot.initialized {
543 return Some(idx);
544 }
545 }
546 }
547 }
548 None
549 }
550
551 pub(crate) fn assign_active_local_slot(
552 &mut self,
553 name: &str,
554 value: VmValue,
555 debug: bool,
556 ) -> Result<bool, VmError> {
557 let Some(frame) = self.frames.last_mut() else {
558 return Ok(false);
559 };
560 for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
561 if info.name == name && info.scope_depth <= frame.local_scope_depth {
562 if !debug && !info.mutable {
563 return Err(VmError::ImmutableAssignment(name.to_string()));
564 }
565 if let Some(slot) = frame.local_slots.get_mut(idx) {
566 crate::value::recursion::dismantle(std::mem::replace(&mut slot.value, value));
567 slot.initialized = true;
568 slot.synced = false;
569 return Ok(true);
570 }
571 }
572 }
573 Ok(false)
574 }
575
576 pub fn new() -> Self {
577 Self {
578 stack: Vec::with_capacity(256),
579 env: VmEnv::new(),
580 output: String::new(),
581 builtins: Arc::new(BTreeMap::new()),
582 async_builtins: Arc::new(BTreeMap::new()),
583 builtin_metadata: Arc::new(BTreeMap::new()),
584 builtins_by_id: Arc::new(HashMap::new()),
585 builtin_id_collisions: Arc::new(HashSet::new()),
586 iterators: Vec::new(),
587 frames: Vec::new(),
588 exception_handlers: Vec::new(),
589 spawned_tasks: BTreeMap::new(),
590 sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
591 shared_state_runtime: Arc::new(crate::shared_state::VmSharedStateRuntime::new()),
592 inline_caches: HashMap::new(),
593 pool_registry: crate::stdlib::pool::new_pool_registry(),
594 wait_for_graph: Arc::new(crate::wait_for_graph::VmWaitForGraph::new()),
595 held_sync_guards: Vec::new(),
596 inherited_held_keys: Arc::new(Vec::new()),
597 task_scopes: Vec::new(),
598 task_counter: 0,
599 runtime_context_counter: 0,
600 runtime_context: crate::runtime_context::RuntimeContext::root(),
601 deadlines: Vec::new(),
602 breakpoints: BTreeMap::new(),
603 function_breakpoints: std::collections::BTreeSet::new(),
604 pending_function_bp: None,
605 step_mode: false,
606 step_frame_depth: 0,
607 stopped: false,
608 last_line: 0,
609 source_dir: None,
610 imported_paths: Vec::new(),
611 module_cache: Arc::new(BTreeMap::new()),
612 source_cache: Arc::new(BTreeMap::new()),
613 source_file: None,
614 source_text: None,
615 bridge: None,
616 denied_builtins: Arc::new(HashSet::new()),
617 cancel_token: None,
618 interrupt_signal_token: None,
619 cancel_grace_instructions_remaining: None,
620 interrupt_handlers: Vec::new(),
621 next_interrupt_handle: 1,
622 pending_interrupt_signal: None,
623 interrupted: false,
624 dispatching_interrupt: false,
625 interrupt_handler_deadline: None,
626 error_stack_trace: Vec::new(),
627 yield_sender: None,
628 project_root: None,
629 globals: Arc::new(crate::value::DictMap::new()),
630 debug_hook: None,
631 runtime_limits: RuntimeLimits::default(),
632 }
633 }
634
635 pub fn baseline(&self) -> VmBaseline {
636 VmBaseline::from_vm(self)
637 }
638
639 pub fn runtime_limits(&self) -> RuntimeLimits {
641 self.runtime_limits
642 }
643
644 pub fn runtime_limit_report(&self) -> crate::RuntimeLimitsReport {
646 self.runtime_limits.report()
647 }
648
649 #[inline]
663 pub(crate) fn debugger_attached(&self) -> bool {
664 self.debug_hook.is_some()
665 || !self.breakpoints.is_empty()
666 || !self.function_breakpoints.is_empty()
667 }
668
669 pub fn set_bridge(&mut self, bridge: Arc<crate::bridge::HostBridge>) {
671 self.bridge = Some(bridge);
672 }
673
674 pub fn set_denied_builtins(&mut self, denied: HashSet<String>) {
677 self.denied_builtins = Arc::new(denied);
678 }
679
680 pub fn set_source_info(&mut self, file: &str, text: &str) {
682 self.source_file = Some(file.to_string());
683 self.source_text = Some(text.to_string());
684 Arc::make_mut(&mut self.source_cache)
685 .insert(std::path::PathBuf::from(file), text.to_string());
686 }
687
688 pub fn start(&mut self, chunk: &Chunk) {
690 let debugger = self.debugger_attached();
697 let initial_env = if debugger {
698 Some(self.env.clone())
699 } else {
700 None
701 };
702 let initial_local_slots = if debugger {
703 Some(Self::fresh_local_slots(chunk))
704 } else {
705 None
706 };
707 self.frames.push(CallFrame {
708 chunk: Arc::new(chunk.clone()),
709 ip: 0,
710 stack_base: self.stack.len(),
711 saved_env: self.env.clone(),
712 initial_env,
713 initial_local_slots,
714 saved_iterator_depth: self.iterators.len(),
715 fn_name: String::new(),
716 argc: 0,
717 saved_source_dir: None,
718 module_functions: None,
719 module_state: None,
720 local_slots: Self::fresh_local_slots(chunk),
721 local_scope_base: self.env.scope_depth().saturating_sub(1),
722 local_scope_depth: 0,
723 });
724 }
725
726 pub(crate) fn child_vm(&self) -> Vm {
729 Vm {
730 stack: Vec::with_capacity(64),
731 env: self.env.clone(),
732 output: String::new(),
733 builtins: Arc::clone(&self.builtins),
734 async_builtins: Arc::clone(&self.async_builtins),
735 builtin_metadata: Arc::clone(&self.builtin_metadata),
736 builtins_by_id: Arc::clone(&self.builtins_by_id),
737 builtin_id_collisions: Arc::clone(&self.builtin_id_collisions),
738 iterators: Vec::new(),
739 frames: Vec::new(),
740 exception_handlers: Vec::new(),
741 spawned_tasks: BTreeMap::new(),
742 sync_runtime: self.sync_runtime.clone(),
743 shared_state_runtime: self.shared_state_runtime.clone(),
744 inline_caches: HashMap::new(),
745 pool_registry: self.pool_registry.clone(),
746 wait_for_graph: self.wait_for_graph.clone(),
747 held_sync_guards: Vec::new(),
748 inherited_held_keys: Arc::new(Vec::new()),
749 task_scopes: Vec::new(),
750 task_counter: 0,
751 runtime_context_counter: self.runtime_context_counter,
752 runtime_context: self.runtime_context.clone(),
753 deadlines: self.deadlines.clone(),
754 breakpoints: BTreeMap::new(),
755 function_breakpoints: std::collections::BTreeSet::new(),
756 pending_function_bp: None,
757 step_mode: false,
758 step_frame_depth: 0,
759 stopped: false,
760 last_line: 0,
761 source_dir: self.source_dir.clone(),
762 imported_paths: Vec::new(),
763 module_cache: Arc::clone(&self.module_cache),
764 source_cache: Arc::clone(&self.source_cache),
765 source_file: self.source_file.clone(),
766 source_text: self.source_text.clone(),
767 bridge: self.bridge.clone(),
768 denied_builtins: Arc::clone(&self.denied_builtins),
769 cancel_token: self.cancel_token.clone(),
770 interrupt_signal_token: self.interrupt_signal_token.clone(),
771 cancel_grace_instructions_remaining: None,
772 interrupt_handlers: Vec::new(),
773 next_interrupt_handle: 1,
774 pending_interrupt_signal: None,
775 interrupted: self.interrupted,
776 dispatching_interrupt: false,
777 interrupt_handler_deadline: None,
778 error_stack_trace: Vec::new(),
779 yield_sender: None,
780 project_root: self.project_root.clone(),
781 globals: Arc::clone(&self.globals),
782 debug_hook: None,
783 runtime_limits: self.runtime_limits,
784 }
785 }
786
787 pub(crate) fn child_vm_for_host(&self) -> Vm {
790 self.child_vm()
791 }
792
793 pub(crate) fn cancel_spawned_tasks(&mut self) {
797 for (_, task) in std::mem::take(&mut self.spawned_tasks) {
798 task.cancel_token
799 .store(true, std::sync::atomic::Ordering::SeqCst);
800 task.handle.abort();
801 }
802 }
803
804 pub fn set_source_dir(&mut self, dir: &std::path::Path) {
807 let dir = crate::stdlib::process::normalize_context_path(dir);
808 self.source_dir = Some(dir.clone());
809 crate::stdlib::set_thread_source_dir(&dir);
810 if self.project_root.is_none() {
812 self.project_root = crate::stdlib::process::find_project_root(&dir);
813 }
814 }
815
816 pub fn set_project_root(&mut self, root: &std::path::Path) {
819 self.project_root = Some(root.to_path_buf());
820 }
821
822 pub fn project_root(&self) -> Option<&std::path::Path> {
824 self.project_root.as_deref().or(self.source_dir.as_deref())
825 }
826
827 pub fn builtin_names(&self) -> Vec<String> {
829 let mut names: Vec<String> = self.builtins.keys().cloned().collect();
830 names.extend(self.async_builtins.keys().cloned());
831 names
832 }
833
834 pub fn builtin_metadata(&self) -> Vec<VmBuiltinMetadata> {
836 self.builtin_metadata.values().cloned().collect()
837 }
838
839 pub fn builtin_metadata_for(&self, name: &str) -> Option<&VmBuiltinMetadata> {
841 self.builtin_metadata.get(name)
842 }
843
844 pub fn set_global(&mut self, name: &str, value: VmValue) {
847 Arc::make_mut(&mut self.globals).insert(name.to_string(), value);
848 }
849
850 pub fn global(&self, name: &str) -> Option<&VmValue> {
856 self.globals.get(name)
857 }
858
859 pub fn set_harness(&mut self, harness: crate::harness::Harness) {
865 self.set_global("harness", harness.into_vm_value());
866 }
867
868 pub fn output(&self) -> &str {
870 &self.output
871 }
872
873 pub fn take_output(&mut self) -> String {
877 std::mem::take(&mut self.output)
878 }
879
880 pub fn append_output(&mut self, text: &str) {
884 self.output.push_str(text);
885 }
886
887 pub(crate) fn pop(&mut self) -> Result<VmValue, VmError> {
888 self.stack.pop().ok_or(VmError::StackUnderflow)
889 }
890
891 pub(crate) fn peek(&self) -> Result<&VmValue, VmError> {
892 self.stack.last().ok_or(VmError::StackUnderflow)
893 }
894
895 pub(crate) fn const_str(c: &Constant) -> Result<&str, VmError> {
896 match c {
897 Constant::String(s) => Ok(s.as_str()),
898 _ => Err(VmError::TypeError("expected string constant".into())),
899 }
900 }
901
902 pub(crate) fn release_sync_guards_for_current_scope(&mut self) {
903 let depth = self.env.scope_depth();
904 self.held_sync_guards
905 .retain(|guard| guard.env_scope_depth < depth);
906 self.cancel_task_scopes_where(|s| s.env_scope_depth >= depth);
909 }
910
911 pub(crate) fn release_sync_guards_after_unwind(
912 &mut self,
913 frame_depth: usize,
914 env_scope_depth: usize,
915 ) {
916 self.held_sync_guards.retain(|guard| {
917 guard.frame_depth <= frame_depth && guard.env_scope_depth <= env_scope_depth
918 });
919 self.cancel_task_scopes_where(|s| {
922 !(s.frame_depth <= frame_depth && s.env_scope_depth <= env_scope_depth)
923 });
924 }
925
926 pub(crate) fn release_sync_guards_for_frame(&mut self, frame_depth: usize) {
927 self.held_sync_guards
928 .retain(|guard| guard.frame_depth != frame_depth);
929 self.cancel_task_scopes_where(|s| s.frame_depth == frame_depth);
932 }
933
934 pub(crate) fn adopt_sync_permit_for_current_scope(
935 &mut self,
936 permit: crate::value::VmSyncPermitHandle,
937 ) {
938 if permit.is_released()
939 || self
940 .held_sync_guards
941 .iter()
942 .any(|guard| guard._permit.same_lease(&permit))
943 {
944 return;
945 }
946 self.held_sync_guards
947 .push(crate::synchronization::VmSyncHeldGuard {
948 _permit: permit,
949 frame_depth: self.frames.len(),
950 env_scope_depth: self.env.scope_depth(),
951 });
952 }
953
954 pub(crate) fn deregister_task_from_scopes(&mut self, id: &str) {
957 for scope in &mut self.task_scopes {
958 scope.task_ids.retain(|t| t != id);
959 }
960 }
961
962 fn cancel_task_scopes_where<F: Fn(&TaskScope) -> bool>(&mut self, doomed: F) {
965 let mut i = 0;
966 while i < self.task_scopes.len() {
967 if doomed(&self.task_scopes[i]) {
968 let scope = self.task_scopes.remove(i);
969 for id in &scope.task_ids {
970 if let Some(task) = self.spawned_tasks.remove(id) {
971 task.cancel_token
972 .store(true, std::sync::atomic::Ordering::SeqCst);
973 task.handle.abort();
974 }
975 }
976 } else {
977 i += 1;
978 }
979 }
980 }
981
982 pub(crate) fn held_permits_for(&self, kind: &str, key: &str) -> u32 {
986 let own: u32 = self
987 .held_sync_guards
988 .iter()
989 .filter(|guard| {
990 !guard._permit.is_released()
991 && guard._permit.kind() == kind
992 && guard._permit.key() == key
993 })
994 .map(|guard| guard._permit.permits())
995 .sum();
996 let inherited: u32 = self
997 .inherited_held_keys
998 .iter()
999 .filter(|held| held.kind == kind && held.key == key)
1000 .map(|held| held.permits)
1001 .sum();
1002 own + inherited
1003 }
1004
1005 pub(crate) fn combined_held_keys(&self) -> Vec<crate::synchronization::VmSyncHeldKey> {
1008 let mut keys: Vec<crate::synchronization::VmSyncHeldKey> = self
1009 .held_sync_guards
1010 .iter()
1011 .filter_map(|guard| crate::synchronization::VmSyncHeldKey::from_permit(&guard._permit))
1012 .collect();
1013 keys.extend(self.inherited_held_keys.iter().cloned());
1014 keys
1015 }
1016
1017 pub(crate) fn child_vm_inline(&self) -> Vm {
1023 let mut child = self.child_vm();
1024 child.inherited_held_keys = Arc::new(self.combined_held_keys());
1025 child
1026 }
1027}
1028
1029impl Drop for Vm {
1030 fn drop(&mut self) {
1031 self.cancel_spawned_tasks();
1032 }
1033}
1034
1035impl Default for Vm {
1036 fn default() -> Self {
1037 Self::new()
1038 }
1039}
1040
1041#[cfg(test)]
1042mod tests {
1043
1044 use super::*;
1045
1046 fn baseline_with_stdlib(source: &str) -> VmBaseline {
1047 let mut vm = Vm::new();
1048 crate::register_vm_stdlib(&mut vm);
1049 vm.set_source_info("baseline_test.harn", source);
1050 vm.set_global(
1051 "stable_global",
1052 VmValue::String(std::sync::Arc::from("baseline")),
1053 );
1054 vm.baseline()
1055 }
1056
1057 #[test]
1058 fn vm_baseline_instantiates_clean_mutable_execution_state() {
1059 let baseline = baseline_with_stdlib("pipeline main() { __io_println(stable_global) }");
1060
1061 let mut dirty = baseline.instantiate();
1062 dirty.stack.push(VmValue::Int(42));
1063 dirty.output.push_str("dirty");
1064 dirty.task_counter = 9;
1065 dirty.runtime_context_counter = 7;
1066 dirty
1067 .error_stack_trace
1068 .push(("main".to_string(), 1, 1, None));
1069
1070 let clean = baseline.instantiate();
1071 assert!(clean.stack.is_empty());
1072 assert!(clean.output.is_empty());
1073 assert!(clean.frames.is_empty());
1074 assert!(clean.exception_handlers.is_empty());
1075 assert!(clean.spawned_tasks.is_empty());
1076 assert!(clean.held_sync_guards.is_empty());
1077 assert_eq!(clean.task_counter, 0);
1078 assert_eq!(clean.runtime_context_counter, 0);
1079 assert!(clean.deadlines.is_empty());
1080 assert!(clean.cancel_token.is_none());
1081 assert!(clean.interrupt_handlers.is_empty());
1082 assert!(clean.error_stack_trace.is_empty());
1083 assert!(clean.bridge.is_none());
1084 assert!(clean
1085 .globals
1086 .get("stable_global")
1087 .is_some_and(|value| value.display() == "baseline"));
1088 }
1089
1090 #[tokio::test]
1091 async fn inline_child_inherits_held_lock_keys_but_concurrent_child_does_not() {
1092 let mut parent = Vm::new();
1093 let permit = parent
1094 .sync_runtime
1095 .acquire("mutex", "v:test", 1, 1, None, None)
1096 .await
1097 .unwrap()
1098 .unwrap();
1099 parent
1100 .held_sync_guards
1101 .push(crate::synchronization::VmSyncHeldGuard {
1102 _permit: permit,
1103 frame_depth: 0,
1104 env_scope_depth: 0,
1105 });
1106 assert_eq!(parent.held_permits_for("mutex", "v:test"), 1);
1107
1108 let inline = parent.child_vm_inline();
1113 assert_eq!(inline.held_permits_for("mutex", "v:test"), 1);
1114 assert_eq!(
1115 inline.child_vm_inline().held_permits_for("mutex", "v:test"),
1116 1
1117 );
1118
1119 let concurrent = parent.child_vm();
1123 assert_eq!(concurrent.held_permits_for("mutex", "v:test"), 0);
1124 }
1125
1126 #[test]
1127 fn vm_reports_effective_runtime_limits() {
1128 let vm = Vm::new();
1129
1130 assert_eq!(vm.runtime_limits(), RuntimeLimits::default());
1131 assert_eq!(
1132 vm.runtime_limit_report().entries.len(),
1133 crate::RUNTIME_LIMIT_DESCRIPTIONS.len()
1134 );
1135 assert_eq!(vm.child_vm().runtime_limits(), vm.runtime_limits());
1136 assert_eq!(
1137 vm.baseline().instantiate().runtime_limits(),
1138 vm.runtime_limits()
1139 );
1140 }
1141
1142 #[tokio::test(flavor = "current_thread")]
1143 async fn vm_baseline_rebinds_shared_state_builtins_per_instance() {
1144 let local = tokio::task::LocalSet::new();
1145 local
1146 .run_until(async {
1147 let source = r#"
1148pipeline main() {
1149 let cell = shared_cell({scope: "task_group", key: "turn", initial: 0})
1150 __io_println(shared_get(cell))
1151 shared_set(cell, shared_get(cell) + 1)
1152}"#;
1153 let chunk = crate::compile_source(source).expect("compile");
1154 let baseline = baseline_with_stdlib(source);
1155
1156 let mut first = baseline.instantiate();
1157 first.execute(&chunk).await.expect("first execute");
1158 assert_eq!(first.output(), "0\n");
1159
1160 let mut second = baseline.instantiate();
1161 second.execute(&chunk).await.expect("second execute");
1162 assert_eq!(
1163 second.output(),
1164 "0\n",
1165 "shared state created by the first VM must not leak into the next baseline instance"
1166 );
1167 })
1168 .await;
1169 }
1170}