1use std::collections::{BTreeMap, HashSet};
2use std::rc::Rc;
3use std::sync::Arc;
4use std::time::Instant;
5
6use crate::chunk::{Chunk, ChunkRef, Constant};
7use crate::runtime_limits::RuntimeLimits;
8use crate::value::{
9 ModuleFunctionRegistry, VmAsyncBuiltinFn, VmBuiltinFn, VmEnv, VmError, VmTaskHandle, VmValue,
10};
11use crate::BuiltinId;
12
13use super::debug::DebugHook;
14use super::modules::LoadedModule;
15use super::VmBuiltinMetadata;
16
17pub(crate) struct ScopeSpan(u64);
19
20impl ScopeSpan {
21 pub(crate) fn new(kind: crate::tracing::SpanKind, name: String) -> Self {
22 Self(crate::tracing::span_start(kind, name))
23 }
24}
25
26impl Drop for ScopeSpan {
27 fn drop(&mut self) {
28 crate::tracing::span_end(self.0);
29 }
30}
31
32#[derive(Clone)]
33pub(crate) struct LocalSlot {
34 pub(crate) value: VmValue,
35 pub(crate) initialized: bool,
36 pub(crate) synced: bool,
37}
38
39#[derive(Clone)]
40pub(crate) struct InterruptHandler {
41 pub(crate) handle: i64,
42 pub(crate) signals: Vec<String>,
43 pub(crate) once: bool,
44 pub(crate) graceful_timeout_ms: Option<u64>,
45 pub(crate) handler: VmValue,
46}
47
48pub(crate) struct CallFrame {
50 pub(crate) chunk: ChunkRef,
51 pub(crate) ip: usize,
52 pub(crate) stack_base: usize,
53 pub(crate) saved_env: VmEnv,
54 pub(crate) initial_env: Option<VmEnv>,
62 pub(crate) initial_local_slots: Option<Vec<LocalSlot>>,
63 pub(crate) saved_iterator_depth: usize,
65 pub(crate) fn_name: String,
67 pub(crate) argc: usize,
69 pub(crate) saved_source_dir: Option<std::path::PathBuf>,
72 pub(crate) module_functions: Option<ModuleFunctionRegistry>,
74 pub(crate) module_state: Option<crate::value::ModuleState>,
80 pub(crate) local_slots: Vec<LocalSlot>,
82 pub(crate) local_scope_base: usize,
84 pub(crate) local_scope_depth: usize,
86}
87
88pub(crate) struct ExceptionHandler {
90 pub(crate) catch_ip: usize,
91 pub(crate) stack_depth: usize,
92 pub(crate) frame_depth: usize,
93 pub(crate) env_scope_depth: usize,
94 pub(crate) error_type: Option<Rc<str>>,
96}
97
98pub(crate) enum IterState {
100 Vec {
101 items: Rc<Vec<VmValue>>,
102 idx: usize,
103 },
104 Dict {
105 entries: Rc<BTreeMap<String, VmValue>>,
106 keys: Vec<String>,
107 idx: usize,
108 },
109 Channel {
110 receiver: std::sync::Arc<tokio::sync::Mutex<tokio::sync::mpsc::Receiver<VmValue>>>,
111 closed: std::sync::Arc<std::sync::atomic::AtomicBool>,
112 },
113 Generator {
114 gen: std::rc::Rc<crate::value::VmGenerator>,
115 },
116 Stream {
117 stream: std::rc::Rc<crate::value::VmStream>,
118 },
119 Range {
123 next: i64,
124 end: i64,
125 inclusive: bool,
126 done: bool,
127 },
128 VmIter {
129 handle: std::rc::Rc<std::cell::RefCell<crate::vm::iter::VmIter>>,
130 },
131}
132
133#[derive(Clone)]
134pub(crate) enum VmBuiltinDispatch {
135 Sync(VmBuiltinFn),
136 Async(VmAsyncBuiltinFn),
137}
138
139#[derive(Clone)]
140pub(crate) struct VmBuiltinEntry {
141 pub(crate) name: Rc<str>,
142 pub(crate) dispatch: VmBuiltinDispatch,
143}
144
145pub(crate) type DeferredBuiltinRegistrar = fn(&mut Vm);
146
147pub struct Vm {
149 pub(crate) stack: Vec<VmValue>,
150 pub(crate) env: VmEnv,
151 pub(crate) output: String,
152 pub(crate) builtins: Rc<BTreeMap<String, VmBuiltinFn>>,
153 pub(crate) async_builtins: Rc<BTreeMap<String, VmAsyncBuiltinFn>>,
154 pub(crate) builtin_metadata: Rc<BTreeMap<String, VmBuiltinMetadata>>,
155 pub(crate) builtins_by_id: Rc<BTreeMap<BuiltinId, VmBuiltinEntry>>,
158 pub(crate) builtin_id_collisions: Rc<HashSet<BuiltinId>>,
161 pub(crate) deferred_builtin_registrars: Rc<BTreeMap<String, DeferredBuiltinRegistrar>>,
163 pub(crate) iterators: Vec<IterState>,
165 pub(crate) frames: Vec<CallFrame>,
167 pub(crate) exception_handlers: Vec<ExceptionHandler>,
169 pub(crate) spawned_tasks: BTreeMap<String, VmTaskHandle>,
171 pub(crate) sync_runtime: Arc<crate::synchronization::VmSyncRuntime>,
173 pub(crate) shared_state_runtime: Rc<crate::shared_state::VmSharedStateRuntime>,
175 pub(crate) held_sync_guards: Vec<crate::synchronization::VmSyncHeldGuard>,
177 pub(crate) task_counter: u64,
179 pub(crate) runtime_context_counter: u64,
181 pub(crate) runtime_context: crate::runtime_context::RuntimeContext,
183 pub(crate) deadlines: Vec<(Instant, usize)>,
185 pub(crate) breakpoints: BTreeMap<String, std::collections::BTreeSet<usize>>,
190 pub(crate) function_breakpoints: std::collections::BTreeSet<String>,
196 pub(crate) pending_function_bp: Option<String>,
201 pub(crate) step_mode: bool,
203 pub(crate) step_frame_depth: usize,
205 pub(crate) stopped: bool,
207 pub(crate) last_line: usize,
209 pub(crate) source_dir: Option<std::path::PathBuf>,
211 pub(crate) imported_paths: Vec<std::path::PathBuf>,
213 pub(crate) module_cache: Rc<BTreeMap<std::path::PathBuf, LoadedModule>>,
215 pub(crate) source_cache: Rc<BTreeMap<std::path::PathBuf, String>>,
217 pub(crate) source_file: Option<String>,
219 pub(crate) source_text: Option<String>,
221 pub(crate) bridge: Option<Rc<crate::bridge::HostBridge>>,
223 pub(crate) denied_builtins: Rc<HashSet<String>>,
225 pub(crate) cancel_token: Option<std::sync::Arc<std::sync::atomic::AtomicBool>>,
227 pub(crate) interrupt_signal_token: Option<std::sync::Arc<std::sync::Mutex<Option<String>>>>,
228 pub(crate) cancel_grace_instructions_remaining: Option<usize>,
233 pub(crate) interrupt_handlers: Vec<InterruptHandler>,
235 pub(crate) next_interrupt_handle: i64,
236 pub(crate) pending_interrupt_signal: Option<String>,
237 pub(crate) interrupted: bool,
238 pub(crate) dispatching_interrupt: bool,
239 pub(crate) interrupt_handler_deadline: Option<Instant>,
240 pub(crate) error_stack_trace: Vec<(String, usize, usize, Option<String>)>,
242 pub(crate) yield_sender: Option<tokio::sync::mpsc::Sender<Result<VmValue, VmError>>>,
245 pub(crate) project_root: Option<std::path::PathBuf>,
248 pub(crate) globals: Rc<BTreeMap<String, VmValue>>,
251 pub(crate) debug_hook: Option<Box<DebugHook>>,
253 pub(crate) runtime_limits: RuntimeLimits,
255}
256
257#[derive(Clone)]
265pub struct VmBaseline {
266 builtins: Rc<BTreeMap<String, VmBuiltinFn>>,
267 async_builtins: Rc<BTreeMap<String, VmAsyncBuiltinFn>>,
268 builtin_metadata: Rc<BTreeMap<String, VmBuiltinMetadata>>,
269 builtins_by_id: Rc<BTreeMap<BuiltinId, VmBuiltinEntry>>,
270 builtin_id_collisions: Rc<HashSet<BuiltinId>>,
271 deferred_builtin_registrars: Rc<BTreeMap<String, DeferredBuiltinRegistrar>>,
272 source_dir: Option<std::path::PathBuf>,
273 source_file: Option<String>,
274 source_text: Option<String>,
275 project_root: Option<std::path::PathBuf>,
276 globals: Rc<BTreeMap<String, VmValue>>,
277 denied_builtins: Rc<HashSet<String>>,
278 runtime_limits: RuntimeLimits,
279}
280
281impl VmBaseline {
282 pub fn from_vm(vm: &Vm) -> Self {
283 Self {
284 builtins: Rc::clone(&vm.builtins),
285 async_builtins: Rc::clone(&vm.async_builtins),
286 builtin_metadata: Rc::clone(&vm.builtin_metadata),
287 builtins_by_id: Rc::clone(&vm.builtins_by_id),
288 builtin_id_collisions: Rc::clone(&vm.builtin_id_collisions),
289 deferred_builtin_registrars: Rc::clone(&vm.deferred_builtin_registrars),
290 source_dir: vm.source_dir.clone(),
291 source_file: vm.source_file.clone(),
292 source_text: vm.source_text.clone(),
293 project_root: vm.project_root.clone(),
294 globals: Rc::clone(&vm.globals),
295 denied_builtins: Rc::clone(&vm.denied_builtins),
296 runtime_limits: vm.runtime_limits,
297 }
298 }
299
300 pub fn instantiate(&self) -> Vm {
301 let mut source_cache = BTreeMap::new();
302 if let (Some(file), Some(text)) = (&self.source_file, &self.source_text) {
303 source_cache.insert(std::path::PathBuf::from(file), text.clone());
304 }
305 if let Some(dir) = &self.source_dir {
306 crate::stdlib::set_thread_source_dir(dir);
307 }
308
309 let mut vm = Vm {
310 stack: Vec::with_capacity(256),
311 env: VmEnv::new(),
312 output: String::new(),
313 builtins: Rc::clone(&self.builtins),
314 async_builtins: Rc::clone(&self.async_builtins),
315 builtin_metadata: Rc::clone(&self.builtin_metadata),
316 builtins_by_id: Rc::clone(&self.builtins_by_id),
317 builtin_id_collisions: Rc::clone(&self.builtin_id_collisions),
318 deferred_builtin_registrars: Rc::clone(&self.deferred_builtin_registrars),
319 iterators: Vec::new(),
320 frames: Vec::new(),
321 exception_handlers: Vec::new(),
322 spawned_tasks: BTreeMap::new(),
323 sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
324 shared_state_runtime: Rc::new(crate::shared_state::VmSharedStateRuntime::new()),
325 held_sync_guards: Vec::new(),
326 task_counter: 0,
327 runtime_context_counter: 0,
328 runtime_context: crate::runtime_context::RuntimeContext::root(),
329 deadlines: Vec::new(),
330 breakpoints: BTreeMap::new(),
331 function_breakpoints: std::collections::BTreeSet::new(),
332 pending_function_bp: None,
333 step_mode: false,
334 step_frame_depth: 0,
335 stopped: false,
336 last_line: 0,
337 source_dir: self.source_dir.clone(),
338 imported_paths: Vec::new(),
339 module_cache: Rc::new(BTreeMap::new()),
340 source_cache: Rc::new(source_cache),
341 source_file: self.source_file.clone(),
342 source_text: self.source_text.clone(),
343 bridge: None,
344 denied_builtins: Rc::clone(&self.denied_builtins),
345 cancel_token: None,
346 interrupt_signal_token: None,
347 cancel_grace_instructions_remaining: None,
348 interrupt_handlers: Vec::new(),
349 next_interrupt_handle: 1,
350 pending_interrupt_signal: None,
351 interrupted: false,
352 dispatching_interrupt: false,
353 interrupt_handler_deadline: None,
354 error_stack_trace: Vec::new(),
355 yield_sender: None,
356 project_root: self.project_root.clone(),
357 globals: Rc::clone(&self.globals),
358 debug_hook: None,
359 runtime_limits: self.runtime_limits,
360 };
361
362 crate::stdlib::rebind_execution_state_builtins(&mut vm);
363 vm
364 }
365}
366
367impl Vm {
368 pub(crate) fn fresh_local_slots(chunk: &Chunk) -> Vec<LocalSlot> {
369 chunk
370 .local_slots
371 .iter()
372 .map(|_| LocalSlot {
373 value: VmValue::Nil,
374 initialized: false,
375 synced: false,
376 })
377 .collect()
378 }
379
380 pub(crate) fn bind_param_slots(
381 slots: &mut [LocalSlot],
382 func: &crate::chunk::CompiledFunction,
383 args: &[VmValue],
384 synced: bool,
385 ) {
386 Self::bind_param_slots_args(slots, func, &super::CallArgs::Slice(args), synced);
387 }
388
389 pub(crate) fn bind_param_slots_args(
390 slots: &mut [LocalSlot],
391 func: &crate::chunk::CompiledFunction,
392 args: &super::CallArgs<'_>,
393 synced: bool,
394 ) {
395 let param_count = func.params.len();
396 for (i, _param) in func.params.iter().enumerate() {
397 if i >= slots.len() {
398 break;
399 }
400 if func.has_rest_param && i == param_count - 1 {
401 let rest_args = args.to_vec_from(i);
402 slots[i].value = VmValue::List(Rc::new(rest_args));
403 slots[i].initialized = true;
404 slots[i].synced = synced;
405 } else if let Some(arg) = args.get(i) {
406 slots[i].value = arg.clone();
407 slots[i].initialized = true;
408 slots[i].synced = synced;
409 }
410 }
411 }
412
413 pub(crate) fn visible_variables(&self) -> BTreeMap<String, VmValue> {
414 let mut vars = self.env.all_variables();
415 let Some(frame) = self.frames.last() else {
416 return vars;
417 };
418 for (slot, info) in frame.local_slots.iter().zip(frame.chunk.local_slots.iter()) {
419 if slot.initialized && info.scope_depth <= frame.local_scope_depth {
420 vars.insert(info.name.clone(), slot.value.clone());
421 }
422 }
423 vars
424 }
425
426 pub(crate) fn sync_current_frame_locals_to_env(&mut self) {
427 let frames = &mut self.frames;
428 let env = &mut self.env;
429 let Some(frame) = frames.last_mut() else {
430 return;
431 };
432 let local_scope_base = frame.local_scope_base;
433 let local_scope_depth = frame.local_scope_depth;
434 for (slot, info) in frame
435 .local_slots
436 .iter_mut()
437 .zip(frame.chunk.local_slots.iter())
438 {
439 if slot.initialized && !slot.synced && info.scope_depth <= local_scope_depth {
440 slot.synced = true;
441 let scope_idx = local_scope_base + info.scope_depth;
442 while env.scopes.len() <= scope_idx {
443 env.push_scope();
444 }
445 Rc::make_mut(&mut env.scopes[scope_idx].vars)
446 .insert(info.name.clone(), (slot.value.clone(), info.mutable));
447 }
448 }
449 }
450
451 pub(crate) fn closure_call_env_for_current_frame(
452 &self,
453 closure: &crate::value::VmClosure,
454 ) -> VmEnv {
455 if closure.module_state.is_some() {
456 return closure.env.clone();
457 }
458 let mut call_env = Self::closure_call_env(&self.env, closure);
459 let Some(frame) = self.frames.last() else {
460 return call_env;
461 };
462 for (slot, info) in frame
463 .local_slots
464 .iter()
465 .zip(frame.chunk.local_slots.iter())
466 .filter(|(slot, info)| slot.initialized && info.scope_depth <= frame.local_scope_depth)
467 {
468 if matches!(slot.value, VmValue::Closure(_)) && !call_env.contains(&info.name) {
469 let _ = call_env.define(&info.name, slot.value.clone(), info.mutable);
470 }
471 }
472 call_env
473 }
474
475 pub(crate) fn active_local_slot_value(&self, name: &str) -> Option<VmValue> {
476 let frame = self.frames.last()?;
477 let idx = self.active_local_slot_index(name)?;
478 frame.local_slots.get(idx).map(|slot| slot.value.clone())
479 }
480
481 pub(crate) fn active_local_slot_index(&self, name: &str) -> Option<usize> {
486 let frame = self.frames.last()?;
487 for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
488 if info.name == name && info.scope_depth <= frame.local_scope_depth {
489 if let Some(slot) = frame.local_slots.get(idx) {
490 if slot.initialized {
491 return Some(idx);
492 }
493 }
494 }
495 }
496 None
497 }
498
499 pub(crate) fn assign_active_local_slot(
500 &mut self,
501 name: &str,
502 value: VmValue,
503 debug: bool,
504 ) -> Result<bool, VmError> {
505 let Some(frame) = self.frames.last_mut() else {
506 return Ok(false);
507 };
508 for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
509 if info.name == name && info.scope_depth <= frame.local_scope_depth {
510 if !debug && !info.mutable {
511 return Err(VmError::ImmutableAssignment(name.to_string()));
512 }
513 if let Some(slot) = frame.local_slots.get_mut(idx) {
514 slot.value = value;
515 slot.initialized = true;
516 slot.synced = false;
517 return Ok(true);
518 }
519 }
520 }
521 Ok(false)
522 }
523
524 pub fn new() -> Self {
525 Self {
526 stack: Vec::with_capacity(256),
527 env: VmEnv::new(),
528 output: String::new(),
529 builtins: Rc::new(BTreeMap::new()),
530 async_builtins: Rc::new(BTreeMap::new()),
531 builtin_metadata: Rc::new(BTreeMap::new()),
532 builtins_by_id: Rc::new(BTreeMap::new()),
533 builtin_id_collisions: Rc::new(HashSet::new()),
534 deferred_builtin_registrars: Rc::new(BTreeMap::new()),
535 iterators: Vec::new(),
536 frames: Vec::new(),
537 exception_handlers: Vec::new(),
538 spawned_tasks: BTreeMap::new(),
539 sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
540 shared_state_runtime: Rc::new(crate::shared_state::VmSharedStateRuntime::new()),
541 held_sync_guards: Vec::new(),
542 task_counter: 0,
543 runtime_context_counter: 0,
544 runtime_context: crate::runtime_context::RuntimeContext::root(),
545 deadlines: Vec::new(),
546 breakpoints: BTreeMap::new(),
547 function_breakpoints: std::collections::BTreeSet::new(),
548 pending_function_bp: None,
549 step_mode: false,
550 step_frame_depth: 0,
551 stopped: false,
552 last_line: 0,
553 source_dir: None,
554 imported_paths: Vec::new(),
555 module_cache: Rc::new(BTreeMap::new()),
556 source_cache: Rc::new(BTreeMap::new()),
557 source_file: None,
558 source_text: None,
559 bridge: None,
560 denied_builtins: Rc::new(HashSet::new()),
561 cancel_token: None,
562 interrupt_signal_token: None,
563 cancel_grace_instructions_remaining: None,
564 interrupt_handlers: Vec::new(),
565 next_interrupt_handle: 1,
566 pending_interrupt_signal: None,
567 interrupted: false,
568 dispatching_interrupt: false,
569 interrupt_handler_deadline: None,
570 error_stack_trace: Vec::new(),
571 yield_sender: None,
572 project_root: None,
573 globals: Rc::new(BTreeMap::new()),
574 debug_hook: None,
575 runtime_limits: RuntimeLimits::default(),
576 }
577 }
578
579 pub fn baseline(&self) -> VmBaseline {
580 VmBaseline::from_vm(self)
581 }
582
583 pub fn runtime_limits(&self) -> RuntimeLimits {
585 self.runtime_limits
586 }
587
588 pub fn runtime_limit_report(&self) -> crate::RuntimeLimitsReport {
590 self.runtime_limits.report()
591 }
592
593 #[inline]
607 pub(crate) fn debugger_attached(&self) -> bool {
608 self.debug_hook.is_some()
609 || !self.breakpoints.is_empty()
610 || !self.function_breakpoints.is_empty()
611 }
612
613 pub fn set_bridge(&mut self, bridge: Rc<crate::bridge::HostBridge>) {
615 self.bridge = Some(bridge);
616 }
617
618 pub fn set_denied_builtins(&mut self, denied: HashSet<String>) {
621 self.denied_builtins = Rc::new(denied);
622 }
623
624 pub fn set_source_info(&mut self, file: &str, text: &str) {
626 self.source_file = Some(file.to_string());
627 self.source_text = Some(text.to_string());
628 Rc::make_mut(&mut self.source_cache)
629 .insert(std::path::PathBuf::from(file), text.to_string());
630 }
631
632 pub fn start(&mut self, chunk: &Chunk) {
634 let debugger = self.debugger_attached();
641 let initial_env = if debugger {
642 Some(self.env.clone())
643 } else {
644 None
645 };
646 let initial_local_slots = if debugger {
647 Some(Self::fresh_local_slots(chunk))
648 } else {
649 None
650 };
651 self.frames.push(CallFrame {
652 chunk: Rc::new(chunk.clone()),
653 ip: 0,
654 stack_base: self.stack.len(),
655 saved_env: self.env.clone(),
656 initial_env,
657 initial_local_slots,
658 saved_iterator_depth: self.iterators.len(),
659 fn_name: String::new(),
660 argc: 0,
661 saved_source_dir: None,
662 module_functions: None,
663 module_state: None,
664 local_slots: Self::fresh_local_slots(chunk),
665 local_scope_base: self.env.scope_depth().saturating_sub(1),
666 local_scope_depth: 0,
667 });
668 }
669
670 pub(crate) fn child_vm(&self) -> Vm {
673 Vm {
674 stack: Vec::with_capacity(64),
675 env: self.env.clone(),
676 output: String::new(),
677 builtins: Rc::clone(&self.builtins),
678 async_builtins: Rc::clone(&self.async_builtins),
679 builtin_metadata: Rc::clone(&self.builtin_metadata),
680 builtins_by_id: Rc::clone(&self.builtins_by_id),
681 builtin_id_collisions: Rc::clone(&self.builtin_id_collisions),
682 deferred_builtin_registrars: Rc::clone(&self.deferred_builtin_registrars),
683 iterators: Vec::new(),
684 frames: Vec::new(),
685 exception_handlers: Vec::new(),
686 spawned_tasks: BTreeMap::new(),
687 sync_runtime: self.sync_runtime.clone(),
688 shared_state_runtime: self.shared_state_runtime.clone(),
689 held_sync_guards: Vec::new(),
690 task_counter: 0,
691 runtime_context_counter: self.runtime_context_counter,
692 runtime_context: self.runtime_context.clone(),
693 deadlines: self.deadlines.clone(),
694 breakpoints: BTreeMap::new(),
695 function_breakpoints: std::collections::BTreeSet::new(),
696 pending_function_bp: None,
697 step_mode: false,
698 step_frame_depth: 0,
699 stopped: false,
700 last_line: 0,
701 source_dir: self.source_dir.clone(),
702 imported_paths: Vec::new(),
703 module_cache: Rc::clone(&self.module_cache),
704 source_cache: Rc::clone(&self.source_cache),
705 source_file: self.source_file.clone(),
706 source_text: self.source_text.clone(),
707 bridge: self.bridge.clone(),
708 denied_builtins: Rc::clone(&self.denied_builtins),
709 cancel_token: self.cancel_token.clone(),
710 interrupt_signal_token: self.interrupt_signal_token.clone(),
711 cancel_grace_instructions_remaining: None,
712 interrupt_handlers: Vec::new(),
713 next_interrupt_handle: 1,
714 pending_interrupt_signal: None,
715 interrupted: self.interrupted,
716 dispatching_interrupt: false,
717 interrupt_handler_deadline: None,
718 error_stack_trace: Vec::new(),
719 yield_sender: None,
720 project_root: self.project_root.clone(),
721 globals: Rc::clone(&self.globals),
722 debug_hook: None,
723 runtime_limits: self.runtime_limits,
724 }
725 }
726
727 pub(crate) fn child_vm_for_host(&self) -> Vm {
730 self.child_vm()
731 }
732
733 pub(crate) fn cancel_spawned_tasks(&mut self) {
737 for (_, task) in std::mem::take(&mut self.spawned_tasks) {
738 task.cancel_token
739 .store(true, std::sync::atomic::Ordering::SeqCst);
740 task.handle.abort();
741 }
742 }
743
744 pub fn set_source_dir(&mut self, dir: &std::path::Path) {
747 let dir = crate::stdlib::process::normalize_context_path(dir);
748 self.source_dir = Some(dir.clone());
749 crate::stdlib::set_thread_source_dir(&dir);
750 if self.project_root.is_none() {
752 self.project_root = crate::stdlib::process::find_project_root(&dir);
753 }
754 }
755
756 pub fn set_project_root(&mut self, root: &std::path::Path) {
759 self.project_root = Some(root.to_path_buf());
760 }
761
762 pub fn project_root(&self) -> Option<&std::path::Path> {
764 self.project_root.as_deref().or(self.source_dir.as_deref())
765 }
766
767 pub fn builtin_names(&self) -> Vec<String> {
769 let mut names: Vec<String> = self.builtins.keys().cloned().collect();
770 names.extend(self.async_builtins.keys().cloned());
771 names
772 }
773
774 pub fn builtin_metadata(&self) -> Vec<VmBuiltinMetadata> {
776 self.builtin_metadata.values().cloned().collect()
777 }
778
779 pub fn builtin_metadata_for(&self, name: &str) -> Option<&VmBuiltinMetadata> {
781 self.builtin_metadata.get(name)
782 }
783
784 pub fn set_global(&mut self, name: &str, value: VmValue) {
787 Rc::make_mut(&mut self.globals).insert(name.to_string(), value);
788 }
789
790 pub fn set_harness(&mut self, harness: crate::harness::Harness) {
796 self.set_global("harness", harness.into_vm_value());
797 }
798
799 pub fn output(&self) -> &str {
801 &self.output
802 }
803
804 pub fn take_output(&mut self) -> String {
808 std::mem::take(&mut self.output)
809 }
810
811 pub fn append_output(&mut self, text: &str) {
815 self.output.push_str(text);
816 }
817
818 pub(crate) fn pop(&mut self) -> Result<VmValue, VmError> {
819 self.stack.pop().ok_or(VmError::StackUnderflow)
820 }
821
822 pub(crate) fn peek(&self) -> Result<&VmValue, VmError> {
823 self.stack.last().ok_or(VmError::StackUnderflow)
824 }
825
826 pub(crate) fn const_str(c: &Constant) -> Result<&str, VmError> {
827 match c {
828 Constant::String(s) => Ok(s.as_str()),
829 _ => Err(VmError::TypeError("expected string constant".into())),
830 }
831 }
832
833 pub(crate) fn release_sync_guards_for_current_scope(&mut self) {
834 let depth = self.env.scope_depth();
835 self.held_sync_guards
836 .retain(|guard| guard.env_scope_depth < depth);
837 }
838
839 pub(crate) fn release_sync_guards_after_unwind(
840 &mut self,
841 frame_depth: usize,
842 env_scope_depth: usize,
843 ) {
844 self.held_sync_guards.retain(|guard| {
845 guard.frame_depth <= frame_depth && guard.env_scope_depth <= env_scope_depth
846 });
847 }
848
849 pub(crate) fn release_sync_guards_for_frame(&mut self, frame_depth: usize) {
850 self.held_sync_guards
851 .retain(|guard| guard.frame_depth != frame_depth);
852 }
853}
854
855impl Drop for Vm {
856 fn drop(&mut self) {
857 self.cancel_spawned_tasks();
858 }
859}
860
861impl Default for Vm {
862 fn default() -> Self {
863 Self::new()
864 }
865}
866
867#[cfg(test)]
868mod tests {
869 use std::rc::Rc;
870
871 use super::*;
872
873 fn baseline_with_stdlib(source: &str) -> VmBaseline {
874 let mut vm = Vm::new();
875 crate::register_vm_stdlib(&mut vm);
876 vm.set_source_info("baseline_test.harn", source);
877 vm.set_global("stable_global", VmValue::String(Rc::from("baseline")));
878 vm.baseline()
879 }
880
881 #[test]
882 fn vm_baseline_instantiates_clean_mutable_execution_state() {
883 let baseline = baseline_with_stdlib("pipeline main() { __io_println(stable_global) }");
884
885 let mut dirty = baseline.instantiate();
886 dirty.stack.push(VmValue::Int(42));
887 dirty.output.push_str("dirty");
888 dirty.task_counter = 9;
889 dirty.runtime_context_counter = 7;
890 dirty
891 .error_stack_trace
892 .push(("main".to_string(), 1, 1, None));
893
894 let clean = baseline.instantiate();
895 assert!(clean.stack.is_empty());
896 assert!(clean.output.is_empty());
897 assert!(clean.frames.is_empty());
898 assert!(clean.exception_handlers.is_empty());
899 assert!(clean.spawned_tasks.is_empty());
900 assert!(clean.held_sync_guards.is_empty());
901 assert_eq!(clean.task_counter, 0);
902 assert_eq!(clean.runtime_context_counter, 0);
903 assert!(clean.deadlines.is_empty());
904 assert!(clean.cancel_token.is_none());
905 assert!(clean.interrupt_handlers.is_empty());
906 assert!(clean.error_stack_trace.is_empty());
907 assert!(clean.bridge.is_none());
908 assert!(clean
909 .globals
910 .get("stable_global")
911 .is_some_and(|value| value.display() == "baseline"));
912 }
913
914 #[test]
915 fn vm_reports_effective_runtime_limits() {
916 let vm = Vm::new();
917
918 assert_eq!(vm.runtime_limits(), RuntimeLimits::default());
919 assert_eq!(
920 vm.runtime_limit_report().entries.len(),
921 crate::RUNTIME_LIMIT_DESCRIPTIONS.len()
922 );
923 assert_eq!(vm.child_vm().runtime_limits(), vm.runtime_limits());
924 assert_eq!(
925 vm.baseline().instantiate().runtime_limits(),
926 vm.runtime_limits()
927 );
928 }
929
930 #[tokio::test(flavor = "current_thread")]
931 async fn vm_baseline_rebinds_shared_state_builtins_per_instance() {
932 let local = tokio::task::LocalSet::new();
933 local
934 .run_until(async {
935 let source = r#"
936pipeline main() {
937 let cell = shared_cell({scope: "task_group", key: "turn", initial: 0})
938 __io_println(shared_get(cell))
939 shared_set(cell, shared_get(cell) + 1)
940}"#;
941 let chunk = crate::compile_source(source).expect("compile");
942 let baseline = baseline_with_stdlib(source);
943
944 let mut first = baseline.instantiate();
945 first.execute(&chunk).await.expect("first execute");
946 assert_eq!(first.output(), "0\n");
947
948 let mut second = baseline.instantiate();
949 second.execute(&chunk).await.expect("second execute");
950 assert_eq!(
951 second.output(),
952 "0\n",
953 "shared state created by the first VM must not leak into the next baseline instance"
954 );
955 })
956 .await;
957 }
958}