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::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: String,
95}
96
97pub(crate) enum IterState {
99 Vec {
100 items: Rc<Vec<VmValue>>,
101 idx: usize,
102 },
103 Dict {
104 entries: Rc<BTreeMap<String, VmValue>>,
105 keys: Vec<String>,
106 idx: usize,
107 },
108 Channel {
109 receiver: std::sync::Arc<tokio::sync::Mutex<tokio::sync::mpsc::Receiver<VmValue>>>,
110 closed: std::sync::Arc<std::sync::atomic::AtomicBool>,
111 },
112 Generator {
113 gen: crate::value::VmGenerator,
114 },
115 Stream {
116 stream: crate::value::VmStream,
117 },
118 Range {
122 next: i64,
123 stop: i64,
124 },
125 VmIter {
126 handle: std::rc::Rc<std::cell::RefCell<crate::vm::iter::VmIter>>,
127 },
128}
129
130#[derive(Clone)]
131pub(crate) enum VmBuiltinDispatch {
132 Sync(VmBuiltinFn),
133 Async(VmAsyncBuiltinFn),
134}
135
136#[derive(Clone)]
137pub(crate) struct VmBuiltinEntry {
138 pub(crate) name: Rc<str>,
139 pub(crate) dispatch: VmBuiltinDispatch,
140}
141
142pub struct Vm {
144 pub(crate) stack: Vec<VmValue>,
145 pub(crate) env: VmEnv,
146 pub(crate) output: String,
147 pub(crate) builtins: Rc<BTreeMap<String, VmBuiltinFn>>,
148 pub(crate) async_builtins: Rc<BTreeMap<String, VmAsyncBuiltinFn>>,
149 pub(crate) builtin_metadata: Rc<BTreeMap<String, VmBuiltinMetadata>>,
150 pub(crate) builtins_by_id: Rc<BTreeMap<BuiltinId, VmBuiltinEntry>>,
153 pub(crate) builtin_id_collisions: Rc<HashSet<BuiltinId>>,
156 pub(crate) iterators: Vec<IterState>,
158 pub(crate) frames: Vec<CallFrame>,
160 pub(crate) exception_handlers: Vec<ExceptionHandler>,
162 pub(crate) spawned_tasks: BTreeMap<String, VmTaskHandle>,
164 pub(crate) sync_runtime: Arc<crate::synchronization::VmSyncRuntime>,
166 pub(crate) shared_state_runtime: Rc<crate::shared_state::VmSharedStateRuntime>,
168 pub(crate) held_sync_guards: Vec<crate::synchronization::VmSyncHeldGuard>,
170 pub(crate) task_counter: u64,
172 pub(crate) runtime_context_counter: u64,
174 pub(crate) runtime_context: crate::runtime_context::RuntimeContext,
176 pub(crate) deadlines: Vec<(Instant, usize)>,
178 pub(crate) breakpoints: BTreeMap<String, std::collections::BTreeSet<usize>>,
183 pub(crate) function_breakpoints: std::collections::BTreeSet<String>,
189 pub(crate) pending_function_bp: Option<String>,
194 pub(crate) step_mode: bool,
196 pub(crate) step_frame_depth: usize,
198 pub(crate) stopped: bool,
200 pub(crate) last_line: usize,
202 pub(crate) source_dir: Option<std::path::PathBuf>,
204 pub(crate) imported_paths: Vec<std::path::PathBuf>,
206 pub(crate) module_cache: Rc<BTreeMap<std::path::PathBuf, LoadedModule>>,
208 pub(crate) source_cache: Rc<BTreeMap<std::path::PathBuf, String>>,
210 pub(crate) source_file: Option<String>,
212 pub(crate) source_text: Option<String>,
214 pub(crate) bridge: Option<Rc<crate::bridge::HostBridge>>,
216 pub(crate) denied_builtins: Rc<HashSet<String>>,
218 pub(crate) cancel_token: Option<std::sync::Arc<std::sync::atomic::AtomicBool>>,
220 pub(crate) interrupt_signal_token: Option<std::sync::Arc<std::sync::Mutex<Option<String>>>>,
221 pub(crate) cancel_grace_instructions_remaining: Option<usize>,
226 pub(crate) interrupt_handlers: Vec<InterruptHandler>,
228 pub(crate) next_interrupt_handle: i64,
229 pub(crate) pending_interrupt_signal: Option<String>,
230 pub(crate) interrupted: bool,
231 pub(crate) dispatching_interrupt: bool,
232 pub(crate) interrupt_handler_deadline: Option<Instant>,
233 pub(crate) error_stack_trace: Vec<(String, usize, usize, Option<String>)>,
235 pub(crate) yield_sender: Option<tokio::sync::mpsc::Sender<Result<VmValue, VmError>>>,
238 pub(crate) project_root: Option<std::path::PathBuf>,
241 pub(crate) globals: Rc<BTreeMap<String, VmValue>>,
244 pub(crate) debug_hook: Option<Box<DebugHook>>,
246}
247
248impl Vm {
249 pub(crate) fn fresh_local_slots(chunk: &Chunk) -> Vec<LocalSlot> {
250 chunk
251 .local_slots
252 .iter()
253 .map(|_| LocalSlot {
254 value: VmValue::Nil,
255 initialized: false,
256 synced: false,
257 })
258 .collect()
259 }
260
261 pub(crate) fn bind_param_slots(
262 slots: &mut [LocalSlot],
263 func: &crate::chunk::CompiledFunction,
264 args: &[VmValue],
265 synced: bool,
266 ) {
267 let param_count = func.params.len();
268 for (i, _param) in func.params.iter().enumerate() {
269 if i >= slots.len() {
270 break;
271 }
272 if func.has_rest_param && i == param_count - 1 {
273 let rest_args = if i < args.len() {
274 args[i..].to_vec()
275 } else {
276 Vec::new()
277 };
278 slots[i].value = VmValue::List(Rc::new(rest_args));
279 slots[i].initialized = true;
280 slots[i].synced = synced;
281 } else if i < args.len() {
282 slots[i].value = args[i].clone();
283 slots[i].initialized = true;
284 slots[i].synced = synced;
285 }
286 }
287 }
288
289 pub(crate) fn visible_variables(&self) -> BTreeMap<String, VmValue> {
290 let mut vars = self.env.all_variables();
291 let Some(frame) = self.frames.last() else {
292 return vars;
293 };
294 for (slot, info) in frame.local_slots.iter().zip(frame.chunk.local_slots.iter()) {
295 if slot.initialized && info.scope_depth <= frame.local_scope_depth {
296 vars.insert(info.name.clone(), slot.value.clone());
297 }
298 }
299 vars
300 }
301
302 pub(crate) fn sync_current_frame_locals_to_env(&mut self) {
303 let frames = &mut self.frames;
304 let env = &mut self.env;
305 let Some(frame) = frames.last_mut() else {
306 return;
307 };
308 let local_scope_base = frame.local_scope_base;
309 let local_scope_depth = frame.local_scope_depth;
310 for (slot, info) in frame
311 .local_slots
312 .iter_mut()
313 .zip(frame.chunk.local_slots.iter())
314 {
315 if slot.initialized && !slot.synced && info.scope_depth <= local_scope_depth {
316 slot.synced = true;
317 let scope_idx = local_scope_base + info.scope_depth;
318 while env.scopes.len() <= scope_idx {
319 env.push_scope();
320 }
321 env.scopes[scope_idx]
322 .vars
323 .insert(info.name.clone(), (slot.value.clone(), info.mutable));
324 }
325 }
326 }
327
328 pub(crate) fn closure_call_env_for_current_frame(
329 &self,
330 closure: &crate::value::VmClosure,
331 ) -> VmEnv {
332 if closure.module_state.is_some() {
333 return closure.env.clone();
334 }
335 let mut call_env = Self::closure_call_env(&self.env, closure);
336 let Some(frame) = self.frames.last() else {
337 return call_env;
338 };
339 for (slot, info) in frame
340 .local_slots
341 .iter()
342 .zip(frame.chunk.local_slots.iter())
343 .filter(|(slot, info)| slot.initialized && info.scope_depth <= frame.local_scope_depth)
344 {
345 if matches!(slot.value, VmValue::Closure(_)) && !call_env.contains(&info.name) {
346 let _ = call_env.define(&info.name, slot.value.clone(), info.mutable);
347 }
348 }
349 call_env
350 }
351
352 pub(crate) fn active_local_slot_value(&self, name: &str) -> Option<VmValue> {
353 let frame = self.frames.last()?;
354 let idx = self.active_local_slot_index(name)?;
355 frame.local_slots.get(idx).map(|slot| slot.value.clone())
356 }
357
358 pub(crate) fn active_local_slot_index(&self, name: &str) -> Option<usize> {
363 let frame = self.frames.last()?;
364 for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
365 if info.name == name && info.scope_depth <= frame.local_scope_depth {
366 if let Some(slot) = frame.local_slots.get(idx) {
367 if slot.initialized {
368 return Some(idx);
369 }
370 }
371 }
372 }
373 None
374 }
375
376 pub(crate) fn assign_active_local_slot(
377 &mut self,
378 name: &str,
379 value: VmValue,
380 debug: bool,
381 ) -> Result<bool, VmError> {
382 let Some(frame) = self.frames.last_mut() else {
383 return Ok(false);
384 };
385 for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
386 if info.name == name && info.scope_depth <= frame.local_scope_depth {
387 if !debug && !info.mutable {
388 return Err(VmError::ImmutableAssignment(name.to_string()));
389 }
390 if let Some(slot) = frame.local_slots.get_mut(idx) {
391 slot.value = value;
392 slot.initialized = true;
393 slot.synced = false;
394 return Ok(true);
395 }
396 }
397 }
398 Ok(false)
399 }
400
401 pub fn new() -> Self {
402 Self {
403 stack: Vec::with_capacity(256),
404 env: VmEnv::new(),
405 output: String::new(),
406 builtins: Rc::new(BTreeMap::new()),
407 async_builtins: Rc::new(BTreeMap::new()),
408 builtin_metadata: Rc::new(BTreeMap::new()),
409 builtins_by_id: Rc::new(BTreeMap::new()),
410 builtin_id_collisions: Rc::new(HashSet::new()),
411 iterators: Vec::new(),
412 frames: Vec::new(),
413 exception_handlers: Vec::new(),
414 spawned_tasks: BTreeMap::new(),
415 sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
416 shared_state_runtime: Rc::new(crate::shared_state::VmSharedStateRuntime::new()),
417 held_sync_guards: Vec::new(),
418 task_counter: 0,
419 runtime_context_counter: 0,
420 runtime_context: crate::runtime_context::RuntimeContext::root(),
421 deadlines: Vec::new(),
422 breakpoints: BTreeMap::new(),
423 function_breakpoints: std::collections::BTreeSet::new(),
424 pending_function_bp: None,
425 step_mode: false,
426 step_frame_depth: 0,
427 stopped: false,
428 last_line: 0,
429 source_dir: None,
430 imported_paths: Vec::new(),
431 module_cache: Rc::new(BTreeMap::new()),
432 source_cache: Rc::new(BTreeMap::new()),
433 source_file: None,
434 source_text: None,
435 bridge: None,
436 denied_builtins: Rc::new(HashSet::new()),
437 cancel_token: None,
438 interrupt_signal_token: None,
439 cancel_grace_instructions_remaining: None,
440 interrupt_handlers: Vec::new(),
441 next_interrupt_handle: 1,
442 pending_interrupt_signal: None,
443 interrupted: false,
444 dispatching_interrupt: false,
445 interrupt_handler_deadline: None,
446 error_stack_trace: Vec::new(),
447 yield_sender: None,
448 project_root: None,
449 globals: Rc::new(BTreeMap::new()),
450 debug_hook: None,
451 }
452 }
453
454 pub fn set_bridge(&mut self, bridge: Rc<crate::bridge::HostBridge>) {
456 self.bridge = Some(bridge);
457 }
458
459 pub fn set_denied_builtins(&mut self, denied: HashSet<String>) {
462 self.denied_builtins = Rc::new(denied);
463 }
464
465 pub fn set_source_info(&mut self, file: &str, text: &str) {
467 self.source_file = Some(file.to_string());
468 self.source_text = Some(text.to_string());
469 Rc::make_mut(&mut self.source_cache)
470 .insert(std::path::PathBuf::from(file), text.to_string());
471 }
472
473 pub fn start(&mut self, chunk: &Chunk) {
475 let initial_env = self.env.clone();
476 self.frames.push(CallFrame {
477 chunk: Rc::new(chunk.clone()),
478 ip: 0,
479 stack_base: self.stack.len(),
480 saved_env: self.env.clone(),
481 initial_env: Some(initial_env),
486 initial_local_slots: Some(Self::fresh_local_slots(chunk)),
487 saved_iterator_depth: self.iterators.len(),
488 fn_name: String::new(),
489 argc: 0,
490 saved_source_dir: None,
491 module_functions: None,
492 module_state: None,
493 local_slots: Self::fresh_local_slots(chunk),
494 local_scope_base: self.env.scope_depth().saturating_sub(1),
495 local_scope_depth: 0,
496 });
497 }
498
499 pub(crate) fn child_vm(&self) -> Vm {
502 Vm {
503 stack: Vec::with_capacity(64),
504 env: self.env.clone(),
505 output: String::new(),
506 builtins: Rc::clone(&self.builtins),
507 async_builtins: Rc::clone(&self.async_builtins),
508 builtin_metadata: Rc::clone(&self.builtin_metadata),
509 builtins_by_id: Rc::clone(&self.builtins_by_id),
510 builtin_id_collisions: Rc::clone(&self.builtin_id_collisions),
511 iterators: Vec::new(),
512 frames: Vec::new(),
513 exception_handlers: Vec::new(),
514 spawned_tasks: BTreeMap::new(),
515 sync_runtime: self.sync_runtime.clone(),
516 shared_state_runtime: self.shared_state_runtime.clone(),
517 held_sync_guards: Vec::new(),
518 task_counter: 0,
519 runtime_context_counter: self.runtime_context_counter,
520 runtime_context: self.runtime_context.clone(),
521 deadlines: self.deadlines.clone(),
522 breakpoints: BTreeMap::new(),
523 function_breakpoints: std::collections::BTreeSet::new(),
524 pending_function_bp: None,
525 step_mode: false,
526 step_frame_depth: 0,
527 stopped: false,
528 last_line: 0,
529 source_dir: self.source_dir.clone(),
530 imported_paths: Vec::new(),
531 module_cache: Rc::clone(&self.module_cache),
532 source_cache: Rc::clone(&self.source_cache),
533 source_file: self.source_file.clone(),
534 source_text: self.source_text.clone(),
535 bridge: self.bridge.clone(),
536 denied_builtins: Rc::clone(&self.denied_builtins),
537 cancel_token: self.cancel_token.clone(),
538 interrupt_signal_token: self.interrupt_signal_token.clone(),
539 cancel_grace_instructions_remaining: None,
540 interrupt_handlers: Vec::new(),
541 next_interrupt_handle: 1,
542 pending_interrupt_signal: None,
543 interrupted: self.interrupted,
544 dispatching_interrupt: false,
545 interrupt_handler_deadline: None,
546 error_stack_trace: Vec::new(),
547 yield_sender: None,
548 project_root: self.project_root.clone(),
549 globals: Rc::clone(&self.globals),
550 debug_hook: None,
551 }
552 }
553
554 pub(crate) fn child_vm_for_host(&self) -> Vm {
557 self.child_vm()
558 }
559
560 pub(crate) fn cancel_spawned_tasks(&mut self) {
564 for (_, task) in std::mem::take(&mut self.spawned_tasks) {
565 task.cancel_token
566 .store(true, std::sync::atomic::Ordering::SeqCst);
567 task.handle.abort();
568 }
569 }
570
571 pub fn set_source_dir(&mut self, dir: &std::path::Path) {
574 let dir = crate::stdlib::process::normalize_context_path(dir);
575 self.source_dir = Some(dir.clone());
576 crate::stdlib::set_thread_source_dir(&dir);
577 if self.project_root.is_none() {
579 self.project_root = crate::stdlib::process::find_project_root(&dir);
580 }
581 }
582
583 pub fn set_project_root(&mut self, root: &std::path::Path) {
586 self.project_root = Some(root.to_path_buf());
587 }
588
589 pub fn project_root(&self) -> Option<&std::path::Path> {
591 self.project_root.as_deref().or(self.source_dir.as_deref())
592 }
593
594 pub fn builtin_names(&self) -> Vec<String> {
596 let mut names: Vec<String> = self.builtins.keys().cloned().collect();
597 names.extend(self.async_builtins.keys().cloned());
598 names
599 }
600
601 pub fn builtin_metadata(&self) -> Vec<VmBuiltinMetadata> {
603 self.builtin_metadata.values().cloned().collect()
604 }
605
606 pub fn builtin_metadata_for(&self, name: &str) -> Option<&VmBuiltinMetadata> {
608 self.builtin_metadata.get(name)
609 }
610
611 pub fn set_global(&mut self, name: &str, value: VmValue) {
614 Rc::make_mut(&mut self.globals).insert(name.to_string(), value);
615 }
616
617 pub fn output(&self) -> &str {
619 &self.output
620 }
621
622 pub fn take_output(&mut self) -> String {
626 std::mem::take(&mut self.output)
627 }
628
629 pub fn append_output(&mut self, text: &str) {
633 self.output.push_str(text);
634 }
635
636 pub(crate) fn pop(&mut self) -> Result<VmValue, VmError> {
637 self.stack.pop().ok_or(VmError::StackUnderflow)
638 }
639
640 pub(crate) fn peek(&self) -> Result<&VmValue, VmError> {
641 self.stack.last().ok_or(VmError::StackUnderflow)
642 }
643
644 pub(crate) fn const_string(c: &Constant) -> Result<String, VmError> {
645 match c {
646 Constant::String(s) => Ok(s.clone()),
647 _ => Err(VmError::TypeError("expected string constant".into())),
648 }
649 }
650
651 pub(crate) fn const_str(c: &Constant) -> Result<&str, VmError> {
652 match c {
653 Constant::String(s) => Ok(s.as_str()),
654 _ => Err(VmError::TypeError("expected string constant".into())),
655 }
656 }
657
658 pub(crate) fn release_sync_guards_for_current_scope(&mut self) {
659 let depth = self.env.scope_depth();
660 self.held_sync_guards
661 .retain(|guard| guard.env_scope_depth < depth);
662 }
663
664 pub(crate) fn release_sync_guards_after_unwind(
665 &mut self,
666 frame_depth: usize,
667 env_scope_depth: usize,
668 ) {
669 self.held_sync_guards.retain(|guard| {
670 guard.frame_depth <= frame_depth && guard.env_scope_depth <= env_scope_depth
671 });
672 }
673
674 pub(crate) fn release_sync_guards_for_frame(&mut self, frame_depth: usize) {
675 self.held_sync_guards
676 .retain(|guard| guard.frame_depth != frame_depth);
677 }
678}
679
680impl Drop for Vm {
681 fn drop(&mut self) {
682 self.cancel_spawned_tasks();
683 }
684}
685
686impl Default for Vm {
687 fn default() -> Self {
688 Self::new()
689 }
690}