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
38#[derive(Clone)]
39pub(crate) struct InterruptHandler {
40 pub(crate) handle: i64,
41 pub(crate) signals: Vec<String>,
42 pub(crate) once: bool,
43 pub(crate) graceful_timeout_ms: Option<u64>,
44 pub(crate) handler: VmValue,
45}
46
47pub(crate) struct CallFrame {
49 pub(crate) chunk: ChunkRef,
50 pub(crate) ip: usize,
51 pub(crate) stack_base: usize,
52 pub(crate) saved_env: VmEnv,
53 pub(crate) initial_env: Option<VmEnv>,
61 pub(crate) initial_local_slots: Option<Vec<LocalSlot>>,
62 pub(crate) saved_iterator_depth: usize,
64 pub(crate) fn_name: String,
66 pub(crate) argc: usize,
68 pub(crate) saved_source_dir: Option<std::path::PathBuf>,
71 pub(crate) module_functions: Option<ModuleFunctionRegistry>,
73 pub(crate) module_state: Option<crate::value::ModuleState>,
79 pub(crate) local_slots: Vec<LocalSlot>,
81 pub(crate) local_scope_base: usize,
83 pub(crate) local_scope_depth: usize,
85}
86
87pub(crate) struct ExceptionHandler {
89 pub(crate) catch_ip: usize,
90 pub(crate) stack_depth: usize,
91 pub(crate) frame_depth: usize,
92 pub(crate) env_scope_depth: usize,
93 pub(crate) error_type: Option<Arc<str>>,
95}
96
97pub(crate) struct TaskScope {
100 pub(crate) task_ids: Vec<String>,
103 pub(crate) frame_depth: usize,
105 pub(crate) env_scope_depth: usize,
107}
108
109pub(crate) enum IterState {
111 Vec {
112 items: Arc<Vec<VmValue>>,
113 idx: usize,
114 },
115 Dict {
116 entries: Arc<BTreeMap<String, VmValue>>,
117 keys: Vec<String>,
118 idx: usize,
119 },
120 Channel {
121 receiver: std::sync::Arc<tokio::sync::Mutex<tokio::sync::mpsc::Receiver<VmValue>>>,
122 close: std::sync::Arc<crate::value::VmChannelCloseState>,
123 },
124 Generator {
125 gen: Arc<crate::value::VmGenerator>,
126 },
127 Stream {
128 stream: Arc<crate::value::VmStream>,
129 },
130 Range {
134 next: i64,
135 end: i64,
136 inclusive: bool,
137 done: bool,
138 },
139 VmIter {
140 handle: crate::vm::iter::VmIterHandle,
141 },
142}
143
144#[derive(Clone)]
145pub(crate) enum VmBuiltinDispatch {
146 Sync(VmBuiltinFn),
147 Async(VmAsyncBuiltinFn),
148}
149
150#[derive(Clone)]
151pub(crate) struct VmBuiltinEntry {
152 pub(crate) name: Arc<str>,
153 pub(crate) dispatch: VmBuiltinDispatch,
154}
155
156pub struct Vm {
158 pub(crate) stack: Vec<VmValue>,
159 pub(crate) env: VmEnv,
160 pub(crate) output: String,
161 pub(crate) builtins: Arc<BTreeMap<String, VmBuiltinFn>>,
162 pub(crate) async_builtins: Arc<BTreeMap<String, VmAsyncBuiltinFn>>,
163 pub(crate) builtin_metadata: Arc<BTreeMap<String, VmBuiltinMetadata>>,
164 pub(crate) builtins_by_id: Arc<BTreeMap<BuiltinId, VmBuiltinEntry>>,
167 pub(crate) builtin_id_collisions: Arc<HashSet<BuiltinId>>,
170 pub(crate) iterators: Vec<IterState>,
172 pub(crate) frames: Vec<CallFrame>,
174 pub(crate) exception_handlers: Vec<ExceptionHandler>,
176 pub(crate) spawned_tasks: BTreeMap<String, VmTaskHandle>,
178 pub(crate) sync_runtime: Arc<crate::synchronization::VmSyncRuntime>,
180 pub(crate) shared_state_runtime: Arc<crate::shared_state::VmSharedStateRuntime>,
182 pub(crate) inline_caches: HashMap<u64, Vec<crate::chunk::InlineCacheEntry>>,
184 pub(crate) pool_registry: Arc<crate::stdlib::pool::PoolRegistry>,
186 pub(crate) wait_for_graph: Arc<crate::wait_for_graph::VmWaitForGraph>,
188 pub(crate) held_sync_guards: Vec<crate::synchronization::VmSyncHeldGuard>,
190 pub(crate) inherited_held_keys: Arc<Vec<crate::synchronization::VmSyncHeldKey>>,
198 pub(crate) task_scopes: Vec<TaskScope>,
204 pub(crate) task_counter: u64,
206 pub(crate) runtime_context_counter: u64,
208 pub(crate) runtime_context: crate::runtime_context::RuntimeContext,
210 pub(crate) deadlines: Vec<(Instant, usize)>,
212 pub(crate) breakpoints: BTreeMap<String, std::collections::BTreeSet<usize>>,
217 pub(crate) function_breakpoints: std::collections::BTreeSet<String>,
223 pub(crate) pending_function_bp: Option<String>,
228 pub(crate) step_mode: bool,
230 pub(crate) step_frame_depth: usize,
232 pub(crate) stopped: bool,
234 pub(crate) last_line: usize,
236 pub(crate) source_dir: Option<std::path::PathBuf>,
238 pub(crate) imported_paths: Vec<std::path::PathBuf>,
240 pub(crate) module_cache: Arc<BTreeMap<std::path::PathBuf, LoadedModule>>,
242 pub(crate) source_cache: Arc<BTreeMap<std::path::PathBuf, String>>,
244 pub(crate) source_file: Option<String>,
246 pub(crate) source_text: Option<String>,
248 pub(crate) bridge: Option<Arc<crate::bridge::HostBridge>>,
250 pub(crate) denied_builtins: Arc<HashSet<String>>,
252 pub(crate) cancel_token: Option<std::sync::Arc<std::sync::atomic::AtomicBool>>,
254 pub(crate) interrupt_signal_token: Option<std::sync::Arc<std::sync::Mutex<Option<String>>>>,
255 pub(crate) cancel_grace_instructions_remaining: Option<usize>,
260 pub(crate) interrupt_handlers: Vec<InterruptHandler>,
262 pub(crate) next_interrupt_handle: i64,
263 pub(crate) pending_interrupt_signal: Option<String>,
264 pub(crate) interrupted: bool,
265 pub(crate) dispatching_interrupt: bool,
266 pub(crate) interrupt_handler_deadline: Option<Instant>,
267 pub(crate) error_stack_trace: Vec<(String, usize, usize, Option<String>)>,
269 pub(crate) yield_sender: Option<tokio::sync::mpsc::Sender<Result<VmValue, VmError>>>,
272 pub(crate) project_root: Option<std::path::PathBuf>,
275 pub(crate) globals: Arc<BTreeMap<String, VmValue>>,
278 pub(crate) debug_hook: Option<parking_lot::Mutex<Box<DebugHook>>>,
280 pub(crate) runtime_limits: RuntimeLimits,
282}
283
284#[derive(Clone)]
292pub struct VmBaseline {
293 builtins: Arc<BTreeMap<String, VmBuiltinFn>>,
294 async_builtins: Arc<BTreeMap<String, VmAsyncBuiltinFn>>,
295 builtin_metadata: Arc<BTreeMap<String, VmBuiltinMetadata>>,
296 builtins_by_id: Arc<BTreeMap<BuiltinId, VmBuiltinEntry>>,
297 builtin_id_collisions: Arc<HashSet<BuiltinId>>,
298 source_dir: Option<std::path::PathBuf>,
299 source_file: Option<String>,
300 source_text: Option<String>,
301 project_root: Option<std::path::PathBuf>,
302 globals: Arc<BTreeMap<String, VmValue>>,
303 denied_builtins: Arc<HashSet<String>>,
304 runtime_limits: RuntimeLimits,
305}
306
307impl VmBaseline {
308 pub fn from_vm(vm: &Vm) -> Self {
309 Self {
310 builtins: Arc::clone(&vm.builtins),
311 async_builtins: Arc::clone(&vm.async_builtins),
312 builtin_metadata: Arc::clone(&vm.builtin_metadata),
313 builtins_by_id: Arc::clone(&vm.builtins_by_id),
314 builtin_id_collisions: Arc::clone(&vm.builtin_id_collisions),
315 source_dir: vm.source_dir.clone(),
316 source_file: vm.source_file.clone(),
317 source_text: vm.source_text.clone(),
318 project_root: vm.project_root.clone(),
319 globals: Arc::clone(&vm.globals),
320 denied_builtins: Arc::clone(&vm.denied_builtins),
321 runtime_limits: vm.runtime_limits,
322 }
323 }
324
325 pub fn instantiate(&self) -> Vm {
326 let mut source_cache = BTreeMap::new();
327 if let (Some(file), Some(text)) = (&self.source_file, &self.source_text) {
328 source_cache.insert(std::path::PathBuf::from(file), text.clone());
329 }
330 if let Some(dir) = &self.source_dir {
331 crate::stdlib::set_thread_source_dir(dir);
332 }
333
334 let mut vm = Vm {
335 stack: Vec::with_capacity(256),
336 env: VmEnv::new(),
337 output: String::new(),
338 builtins: Arc::clone(&self.builtins),
339 async_builtins: Arc::clone(&self.async_builtins),
340 builtin_metadata: Arc::clone(&self.builtin_metadata),
341 builtins_by_id: Arc::clone(&self.builtins_by_id),
342 builtin_id_collisions: Arc::clone(&self.builtin_id_collisions),
343 iterators: Vec::new(),
344 frames: Vec::new(),
345 exception_handlers: Vec::new(),
346 spawned_tasks: BTreeMap::new(),
347 sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
348 shared_state_runtime: Arc::new(crate::shared_state::VmSharedStateRuntime::new()),
349 inline_caches: HashMap::new(),
350 pool_registry: crate::stdlib::pool::new_pool_registry(),
351 wait_for_graph: Arc::new(crate::wait_for_graph::VmWaitForGraph::new()),
352 held_sync_guards: Vec::new(),
353 inherited_held_keys: Arc::new(Vec::new()),
354 task_scopes: Vec::new(),
355 task_counter: 0,
356 runtime_context_counter: 0,
357 runtime_context: crate::runtime_context::RuntimeContext::root(),
358 deadlines: Vec::new(),
359 breakpoints: BTreeMap::new(),
360 function_breakpoints: std::collections::BTreeSet::new(),
361 pending_function_bp: None,
362 step_mode: false,
363 step_frame_depth: 0,
364 stopped: false,
365 last_line: 0,
366 source_dir: self.source_dir.clone(),
367 imported_paths: Vec::new(),
368 module_cache: Arc::new(BTreeMap::new()),
369 source_cache: Arc::new(source_cache),
370 source_file: self.source_file.clone(),
371 source_text: self.source_text.clone(),
372 bridge: None,
373 denied_builtins: Arc::clone(&self.denied_builtins),
374 cancel_token: None,
375 interrupt_signal_token: None,
376 cancel_grace_instructions_remaining: None,
377 interrupt_handlers: Vec::new(),
378 next_interrupt_handle: 1,
379 pending_interrupt_signal: None,
380 interrupted: false,
381 dispatching_interrupt: false,
382 interrupt_handler_deadline: None,
383 error_stack_trace: Vec::new(),
384 yield_sender: None,
385 project_root: self.project_root.clone(),
386 globals: Arc::clone(&self.globals),
387 debug_hook: None,
388 runtime_limits: self.runtime_limits,
389 };
390
391 crate::stdlib::rebind_execution_state_builtins(&mut vm);
392 vm
393 }
394}
395
396impl Vm {
397 pub(crate) fn fresh_local_slots(chunk: &Chunk) -> Vec<LocalSlot> {
398 chunk
399 .local_slots
400 .iter()
401 .map(|_| LocalSlot {
402 value: VmValue::Nil,
403 initialized: false,
404 synced: false,
405 })
406 .collect()
407 }
408
409 pub(crate) fn bind_param_slots(
410 slots: &mut [LocalSlot],
411 func: &crate::chunk::CompiledFunction,
412 args: &[VmValue],
413 synced: bool,
414 ) {
415 Self::bind_param_slots_args(slots, func, &super::CallArgs::Slice(args), synced);
416 }
417
418 pub(crate) fn bind_param_slots_args(
419 slots: &mut [LocalSlot],
420 func: &crate::chunk::CompiledFunction,
421 args: &super::CallArgs<'_>,
422 synced: bool,
423 ) {
424 let param_count = func.params.len();
425 for (i, _param) in func.params.iter().enumerate() {
426 if i >= slots.len() {
427 break;
428 }
429 if func.has_rest_param && i == param_count - 1 {
430 let rest_args = args.to_vec_from(i);
431 slots[i].value = VmValue::List(std::sync::Arc::new(rest_args));
432 slots[i].initialized = true;
433 slots[i].synced = synced;
434 } else if let Some(arg) = args.get(i) {
435 slots[i].value = arg.clone();
436 slots[i].initialized = true;
437 slots[i].synced = synced;
438 }
439 }
440 }
441
442 pub(crate) fn visible_variables(&self) -> BTreeMap<String, VmValue> {
443 let mut vars = self.env.all_variables();
444 let Some(frame) = self.frames.last() else {
445 return vars;
446 };
447 for (slot, info) in frame.local_slots.iter().zip(frame.chunk.local_slots.iter()) {
448 if slot.initialized && info.scope_depth <= frame.local_scope_depth {
449 vars.insert(info.name.clone(), slot.value.clone());
450 }
451 }
452 vars
453 }
454
455 pub(crate) fn sync_current_frame_locals_to_env(&mut self) {
456 let frames = &mut self.frames;
457 let env = &mut self.env;
458 let Some(frame) = frames.last_mut() else {
459 return;
460 };
461 let local_scope_base = frame.local_scope_base;
462 let local_scope_depth = frame.local_scope_depth;
463 for (slot, info) in frame
464 .local_slots
465 .iter_mut()
466 .zip(frame.chunk.local_slots.iter())
467 {
468 if slot.initialized && !slot.synced && info.scope_depth <= local_scope_depth {
469 slot.synced = true;
470 let scope_idx = local_scope_base + info.scope_depth;
471 while env.scopes.len() <= scope_idx {
472 env.push_scope();
473 }
474 Arc::make_mut(&mut env.scopes[scope_idx].vars)
475 .insert(info.name.clone(), (slot.value.clone(), info.mutable));
476 }
477 }
478 }
479
480 pub(crate) fn closure_call_env_for_current_frame(
481 &self,
482 closure: &crate::value::VmClosure,
483 ) -> VmEnv {
484 if closure.module_state().is_some() {
485 return closure.env.clone();
486 }
487 let call_env = Self::closure_call_env(&self.env, closure);
488 if !closure.func.chunk.references_outer_names {
493 return call_env;
494 }
495 let mut call_env = call_env;
496 let Some(frame) = self.frames.last() else {
497 return call_env;
498 };
499 for (slot, info) in frame
500 .local_slots
501 .iter()
502 .zip(frame.chunk.local_slots.iter())
503 .filter(|(slot, info)| slot.initialized && info.scope_depth <= frame.local_scope_depth)
504 {
505 if matches!(slot.value, VmValue::Closure(_)) && !call_env.contains(&info.name) {
506 let _ = call_env.define(&info.name, slot.value.clone(), info.mutable);
507 }
508 }
509 call_env
510 }
511
512 pub(crate) fn active_local_slot_value(&self, name: &str) -> Option<VmValue> {
513 let frame = self.frames.last()?;
514 let idx = self.active_local_slot_index(name)?;
515 frame.local_slots.get(idx).map(|slot| slot.value.clone())
516 }
517
518 pub(crate) fn active_local_slot_index(&self, name: &str) -> Option<usize> {
523 let frame = self.frames.last()?;
524 for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
525 if info.name == name && info.scope_depth <= frame.local_scope_depth {
526 if let Some(slot) = frame.local_slots.get(idx) {
527 if slot.initialized {
528 return Some(idx);
529 }
530 }
531 }
532 }
533 None
534 }
535
536 pub(crate) fn assign_active_local_slot(
537 &mut self,
538 name: &str,
539 value: VmValue,
540 debug: bool,
541 ) -> Result<bool, VmError> {
542 let Some(frame) = self.frames.last_mut() else {
543 return Ok(false);
544 };
545 for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
546 if info.name == name && info.scope_depth <= frame.local_scope_depth {
547 if !debug && !info.mutable {
548 return Err(VmError::ImmutableAssignment(name.to_string()));
549 }
550 if let Some(slot) = frame.local_slots.get_mut(idx) {
551 slot.value = value;
552 slot.initialized = true;
553 slot.synced = false;
554 return Ok(true);
555 }
556 }
557 }
558 Ok(false)
559 }
560
561 pub fn new() -> Self {
562 Self {
563 stack: Vec::with_capacity(256),
564 env: VmEnv::new(),
565 output: String::new(),
566 builtins: Arc::new(BTreeMap::new()),
567 async_builtins: Arc::new(BTreeMap::new()),
568 builtin_metadata: Arc::new(BTreeMap::new()),
569 builtins_by_id: Arc::new(BTreeMap::new()),
570 builtin_id_collisions: Arc::new(HashSet::new()),
571 iterators: Vec::new(),
572 frames: Vec::new(),
573 exception_handlers: Vec::new(),
574 spawned_tasks: BTreeMap::new(),
575 sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
576 shared_state_runtime: Arc::new(crate::shared_state::VmSharedStateRuntime::new()),
577 inline_caches: HashMap::new(),
578 pool_registry: crate::stdlib::pool::new_pool_registry(),
579 wait_for_graph: Arc::new(crate::wait_for_graph::VmWaitForGraph::new()),
580 held_sync_guards: Vec::new(),
581 inherited_held_keys: Arc::new(Vec::new()),
582 task_scopes: Vec::new(),
583 task_counter: 0,
584 runtime_context_counter: 0,
585 runtime_context: crate::runtime_context::RuntimeContext::root(),
586 deadlines: Vec::new(),
587 breakpoints: BTreeMap::new(),
588 function_breakpoints: std::collections::BTreeSet::new(),
589 pending_function_bp: None,
590 step_mode: false,
591 step_frame_depth: 0,
592 stopped: false,
593 last_line: 0,
594 source_dir: None,
595 imported_paths: Vec::new(),
596 module_cache: Arc::new(BTreeMap::new()),
597 source_cache: Arc::new(BTreeMap::new()),
598 source_file: None,
599 source_text: None,
600 bridge: None,
601 denied_builtins: Arc::new(HashSet::new()),
602 cancel_token: None,
603 interrupt_signal_token: None,
604 cancel_grace_instructions_remaining: None,
605 interrupt_handlers: Vec::new(),
606 next_interrupt_handle: 1,
607 pending_interrupt_signal: None,
608 interrupted: false,
609 dispatching_interrupt: false,
610 interrupt_handler_deadline: None,
611 error_stack_trace: Vec::new(),
612 yield_sender: None,
613 project_root: None,
614 globals: Arc::new(BTreeMap::new()),
615 debug_hook: None,
616 runtime_limits: RuntimeLimits::default(),
617 }
618 }
619
620 pub fn baseline(&self) -> VmBaseline {
621 VmBaseline::from_vm(self)
622 }
623
624 pub fn runtime_limits(&self) -> RuntimeLimits {
626 self.runtime_limits
627 }
628
629 pub fn runtime_limit_report(&self) -> crate::RuntimeLimitsReport {
631 self.runtime_limits.report()
632 }
633
634 #[inline]
648 pub(crate) fn debugger_attached(&self) -> bool {
649 self.debug_hook.is_some()
650 || !self.breakpoints.is_empty()
651 || !self.function_breakpoints.is_empty()
652 }
653
654 pub fn set_bridge(&mut self, bridge: Arc<crate::bridge::HostBridge>) {
656 self.bridge = Some(bridge);
657 }
658
659 pub fn set_denied_builtins(&mut self, denied: HashSet<String>) {
662 self.denied_builtins = Arc::new(denied);
663 }
664
665 pub fn set_source_info(&mut self, file: &str, text: &str) {
667 self.source_file = Some(file.to_string());
668 self.source_text = Some(text.to_string());
669 Arc::make_mut(&mut self.source_cache)
670 .insert(std::path::PathBuf::from(file), text.to_string());
671 }
672
673 pub fn start(&mut self, chunk: &Chunk) {
675 let debugger = self.debugger_attached();
682 let initial_env = if debugger {
683 Some(self.env.clone())
684 } else {
685 None
686 };
687 let initial_local_slots = if debugger {
688 Some(Self::fresh_local_slots(chunk))
689 } else {
690 None
691 };
692 self.frames.push(CallFrame {
693 chunk: Arc::new(chunk.clone()),
694 ip: 0,
695 stack_base: self.stack.len(),
696 saved_env: self.env.clone(),
697 initial_env,
698 initial_local_slots,
699 saved_iterator_depth: self.iterators.len(),
700 fn_name: String::new(),
701 argc: 0,
702 saved_source_dir: None,
703 module_functions: None,
704 module_state: None,
705 local_slots: Self::fresh_local_slots(chunk),
706 local_scope_base: self.env.scope_depth().saturating_sub(1),
707 local_scope_depth: 0,
708 });
709 }
710
711 pub(crate) fn child_vm(&self) -> Vm {
714 Vm {
715 stack: Vec::with_capacity(64),
716 env: self.env.clone(),
717 output: String::new(),
718 builtins: Arc::clone(&self.builtins),
719 async_builtins: Arc::clone(&self.async_builtins),
720 builtin_metadata: Arc::clone(&self.builtin_metadata),
721 builtins_by_id: Arc::clone(&self.builtins_by_id),
722 builtin_id_collisions: Arc::clone(&self.builtin_id_collisions),
723 iterators: Vec::new(),
724 frames: Vec::new(),
725 exception_handlers: Vec::new(),
726 spawned_tasks: BTreeMap::new(),
727 sync_runtime: self.sync_runtime.clone(),
728 shared_state_runtime: self.shared_state_runtime.clone(),
729 inline_caches: HashMap::new(),
730 pool_registry: self.pool_registry.clone(),
731 wait_for_graph: self.wait_for_graph.clone(),
732 held_sync_guards: Vec::new(),
733 inherited_held_keys: Arc::new(Vec::new()),
734 task_scopes: Vec::new(),
735 task_counter: 0,
736 runtime_context_counter: self.runtime_context_counter,
737 runtime_context: self.runtime_context.clone(),
738 deadlines: self.deadlines.clone(),
739 breakpoints: BTreeMap::new(),
740 function_breakpoints: std::collections::BTreeSet::new(),
741 pending_function_bp: None,
742 step_mode: false,
743 step_frame_depth: 0,
744 stopped: false,
745 last_line: 0,
746 source_dir: self.source_dir.clone(),
747 imported_paths: Vec::new(),
748 module_cache: Arc::clone(&self.module_cache),
749 source_cache: Arc::clone(&self.source_cache),
750 source_file: self.source_file.clone(),
751 source_text: self.source_text.clone(),
752 bridge: self.bridge.clone(),
753 denied_builtins: Arc::clone(&self.denied_builtins),
754 cancel_token: self.cancel_token.clone(),
755 interrupt_signal_token: self.interrupt_signal_token.clone(),
756 cancel_grace_instructions_remaining: None,
757 interrupt_handlers: Vec::new(),
758 next_interrupt_handle: 1,
759 pending_interrupt_signal: None,
760 interrupted: self.interrupted,
761 dispatching_interrupt: false,
762 interrupt_handler_deadline: None,
763 error_stack_trace: Vec::new(),
764 yield_sender: None,
765 project_root: self.project_root.clone(),
766 globals: Arc::clone(&self.globals),
767 debug_hook: None,
768 runtime_limits: self.runtime_limits,
769 }
770 }
771
772 pub(crate) fn child_vm_for_host(&self) -> Vm {
775 self.child_vm()
776 }
777
778 pub(crate) fn cancel_spawned_tasks(&mut self) {
782 for (_, task) in std::mem::take(&mut self.spawned_tasks) {
783 task.cancel_token
784 .store(true, std::sync::atomic::Ordering::SeqCst);
785 task.handle.abort();
786 }
787 }
788
789 pub fn set_source_dir(&mut self, dir: &std::path::Path) {
792 let dir = crate::stdlib::process::normalize_context_path(dir);
793 self.source_dir = Some(dir.clone());
794 crate::stdlib::set_thread_source_dir(&dir);
795 if self.project_root.is_none() {
797 self.project_root = crate::stdlib::process::find_project_root(&dir);
798 }
799 }
800
801 pub fn set_project_root(&mut self, root: &std::path::Path) {
804 self.project_root = Some(root.to_path_buf());
805 }
806
807 pub fn project_root(&self) -> Option<&std::path::Path> {
809 self.project_root.as_deref().or(self.source_dir.as_deref())
810 }
811
812 pub fn builtin_names(&self) -> Vec<String> {
814 let mut names: Vec<String> = self.builtins.keys().cloned().collect();
815 names.extend(self.async_builtins.keys().cloned());
816 names
817 }
818
819 pub fn builtin_metadata(&self) -> Vec<VmBuiltinMetadata> {
821 self.builtin_metadata.values().cloned().collect()
822 }
823
824 pub fn builtin_metadata_for(&self, name: &str) -> Option<&VmBuiltinMetadata> {
826 self.builtin_metadata.get(name)
827 }
828
829 pub fn set_global(&mut self, name: &str, value: VmValue) {
832 Arc::make_mut(&mut self.globals).insert(name.to_string(), value);
833 }
834
835 pub fn global(&self, name: &str) -> Option<&VmValue> {
841 self.globals.get(name)
842 }
843
844 pub fn set_harness(&mut self, harness: crate::harness::Harness) {
850 self.set_global("harness", harness.into_vm_value());
851 }
852
853 pub fn output(&self) -> &str {
855 &self.output
856 }
857
858 pub fn take_output(&mut self) -> String {
862 std::mem::take(&mut self.output)
863 }
864
865 pub fn append_output(&mut self, text: &str) {
869 self.output.push_str(text);
870 }
871
872 pub(crate) fn pop(&mut self) -> Result<VmValue, VmError> {
873 self.stack.pop().ok_or(VmError::StackUnderflow)
874 }
875
876 pub(crate) fn peek(&self) -> Result<&VmValue, VmError> {
877 self.stack.last().ok_or(VmError::StackUnderflow)
878 }
879
880 pub(crate) fn const_str(c: &Constant) -> Result<&str, VmError> {
881 match c {
882 Constant::String(s) => Ok(s.as_str()),
883 _ => Err(VmError::TypeError("expected string constant".into())),
884 }
885 }
886
887 pub(crate) fn release_sync_guards_for_current_scope(&mut self) {
888 let depth = self.env.scope_depth();
889 self.held_sync_guards
890 .retain(|guard| guard.env_scope_depth < depth);
891 self.cancel_task_scopes_where(|s| s.env_scope_depth >= depth);
894 }
895
896 pub(crate) fn release_sync_guards_after_unwind(
897 &mut self,
898 frame_depth: usize,
899 env_scope_depth: usize,
900 ) {
901 self.held_sync_guards.retain(|guard| {
902 guard.frame_depth <= frame_depth && guard.env_scope_depth <= env_scope_depth
903 });
904 self.cancel_task_scopes_where(|s| {
907 !(s.frame_depth <= frame_depth && s.env_scope_depth <= env_scope_depth)
908 });
909 }
910
911 pub(crate) fn release_sync_guards_for_frame(&mut self, frame_depth: usize) {
912 self.held_sync_guards
913 .retain(|guard| guard.frame_depth != frame_depth);
914 self.cancel_task_scopes_where(|s| s.frame_depth == frame_depth);
917 }
918
919 pub(crate) fn adopt_sync_permit_for_current_scope(
920 &mut self,
921 permit: crate::value::VmSyncPermitHandle,
922 ) {
923 if permit.is_released()
924 || self
925 .held_sync_guards
926 .iter()
927 .any(|guard| guard._permit.same_lease(&permit))
928 {
929 return;
930 }
931 self.held_sync_guards
932 .push(crate::synchronization::VmSyncHeldGuard {
933 _permit: permit,
934 frame_depth: self.frames.len(),
935 env_scope_depth: self.env.scope_depth(),
936 });
937 }
938
939 pub(crate) fn deregister_task_from_scopes(&mut self, id: &str) {
942 for scope in &mut self.task_scopes {
943 scope.task_ids.retain(|t| t != id);
944 }
945 }
946
947 fn cancel_task_scopes_where<F: Fn(&TaskScope) -> bool>(&mut self, doomed: F) {
950 let mut i = 0;
951 while i < self.task_scopes.len() {
952 if doomed(&self.task_scopes[i]) {
953 let scope = self.task_scopes.remove(i);
954 for id in &scope.task_ids {
955 if let Some(task) = self.spawned_tasks.remove(id) {
956 task.cancel_token
957 .store(true, std::sync::atomic::Ordering::SeqCst);
958 task.handle.abort();
959 }
960 }
961 } else {
962 i += 1;
963 }
964 }
965 }
966
967 pub(crate) fn held_permits_for(&self, kind: &str, key: &str) -> u32 {
971 let own: u32 = self
972 .held_sync_guards
973 .iter()
974 .filter(|guard| {
975 !guard._permit.is_released()
976 && guard._permit.kind() == kind
977 && guard._permit.key() == key
978 })
979 .map(|guard| guard._permit.permits())
980 .sum();
981 let inherited: u32 = self
982 .inherited_held_keys
983 .iter()
984 .filter(|held| held.kind == kind && held.key == key)
985 .map(|held| held.permits)
986 .sum();
987 own + inherited
988 }
989
990 pub(crate) fn combined_held_keys(&self) -> Vec<crate::synchronization::VmSyncHeldKey> {
993 let mut keys: Vec<crate::synchronization::VmSyncHeldKey> = self
994 .held_sync_guards
995 .iter()
996 .filter_map(|guard| crate::synchronization::VmSyncHeldKey::from_permit(&guard._permit))
997 .collect();
998 keys.extend(self.inherited_held_keys.iter().cloned());
999 keys
1000 }
1001
1002 pub(crate) fn child_vm_inline(&self) -> Vm {
1008 let mut child = self.child_vm();
1009 child.inherited_held_keys = Arc::new(self.combined_held_keys());
1010 child
1011 }
1012}
1013
1014impl Drop for Vm {
1015 fn drop(&mut self) {
1016 self.cancel_spawned_tasks();
1017 }
1018}
1019
1020impl Default for Vm {
1021 fn default() -> Self {
1022 Self::new()
1023 }
1024}
1025
1026#[cfg(test)]
1027mod tests {
1028
1029 use super::*;
1030
1031 fn baseline_with_stdlib(source: &str) -> VmBaseline {
1032 let mut vm = Vm::new();
1033 crate::register_vm_stdlib(&mut vm);
1034 vm.set_source_info("baseline_test.harn", source);
1035 vm.set_global(
1036 "stable_global",
1037 VmValue::String(std::sync::Arc::from("baseline")),
1038 );
1039 vm.baseline()
1040 }
1041
1042 #[test]
1043 fn vm_baseline_instantiates_clean_mutable_execution_state() {
1044 let baseline = baseline_with_stdlib("pipeline main() { __io_println(stable_global) }");
1045
1046 let mut dirty = baseline.instantiate();
1047 dirty.stack.push(VmValue::Int(42));
1048 dirty.output.push_str("dirty");
1049 dirty.task_counter = 9;
1050 dirty.runtime_context_counter = 7;
1051 dirty
1052 .error_stack_trace
1053 .push(("main".to_string(), 1, 1, None));
1054
1055 let clean = baseline.instantiate();
1056 assert!(clean.stack.is_empty());
1057 assert!(clean.output.is_empty());
1058 assert!(clean.frames.is_empty());
1059 assert!(clean.exception_handlers.is_empty());
1060 assert!(clean.spawned_tasks.is_empty());
1061 assert!(clean.held_sync_guards.is_empty());
1062 assert_eq!(clean.task_counter, 0);
1063 assert_eq!(clean.runtime_context_counter, 0);
1064 assert!(clean.deadlines.is_empty());
1065 assert!(clean.cancel_token.is_none());
1066 assert!(clean.interrupt_handlers.is_empty());
1067 assert!(clean.error_stack_trace.is_empty());
1068 assert!(clean.bridge.is_none());
1069 assert!(clean
1070 .globals
1071 .get("stable_global")
1072 .is_some_and(|value| value.display() == "baseline"));
1073 }
1074
1075 #[tokio::test]
1076 async fn inline_child_inherits_held_lock_keys_but_concurrent_child_does_not() {
1077 let mut parent = Vm::new();
1078 let permit = parent
1079 .sync_runtime
1080 .acquire("mutex", "v:test", 1, 1, None, None)
1081 .await
1082 .unwrap()
1083 .unwrap();
1084 parent
1085 .held_sync_guards
1086 .push(crate::synchronization::VmSyncHeldGuard {
1087 _permit: permit,
1088 frame_depth: 0,
1089 env_scope_depth: 0,
1090 });
1091 assert_eq!(parent.held_permits_for("mutex", "v:test"), 1);
1092
1093 let inline = parent.child_vm_inline();
1098 assert_eq!(inline.held_permits_for("mutex", "v:test"), 1);
1099 assert_eq!(
1100 inline.child_vm_inline().held_permits_for("mutex", "v:test"),
1101 1
1102 );
1103
1104 let concurrent = parent.child_vm();
1108 assert_eq!(concurrent.held_permits_for("mutex", "v:test"), 0);
1109 }
1110
1111 #[test]
1112 fn vm_reports_effective_runtime_limits() {
1113 let vm = Vm::new();
1114
1115 assert_eq!(vm.runtime_limits(), RuntimeLimits::default());
1116 assert_eq!(
1117 vm.runtime_limit_report().entries.len(),
1118 crate::RUNTIME_LIMIT_DESCRIPTIONS.len()
1119 );
1120 assert_eq!(vm.child_vm().runtime_limits(), vm.runtime_limits());
1121 assert_eq!(
1122 vm.baseline().instantiate().runtime_limits(),
1123 vm.runtime_limits()
1124 );
1125 }
1126
1127 #[tokio::test(flavor = "current_thread")]
1128 async fn vm_baseline_rebinds_shared_state_builtins_per_instance() {
1129 let local = tokio::task::LocalSet::new();
1130 local
1131 .run_until(async {
1132 let source = r#"
1133pipeline main() {
1134 let cell = shared_cell({scope: "task_group", key: "turn", initial: 0})
1135 __io_println(shared_get(cell))
1136 shared_set(cell, shared_get(cell) + 1)
1137}"#;
1138 let chunk = crate::compile_source(source).expect("compile");
1139 let baseline = baseline_with_stdlib(source);
1140
1141 let mut first = baseline.instantiate();
1142 first.execute(&chunk).await.expect("first execute");
1143 assert_eq!(first.output(), "0\n");
1144
1145 let mut second = baseline.instantiate();
1146 second.execute(&chunk).await.expect("second execute");
1147 assert_eq!(
1148 second.output(),
1149 "0\n",
1150 "shared state created by the first VM must not leak into the next baseline instance"
1151 );
1152 })
1153 .await;
1154 }
1155}