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
38pub(crate) struct CallFrame {
40 pub(crate) chunk: ChunkRef,
41 pub(crate) ip: usize,
42 pub(crate) stack_base: usize,
43 pub(crate) saved_env: VmEnv,
44 pub(crate) initial_env: Option<VmEnv>,
52 pub(crate) initial_local_slots: Option<Vec<LocalSlot>>,
53 pub(crate) saved_iterator_depth: usize,
55 pub(crate) fn_name: String,
57 pub(crate) argc: usize,
59 pub(crate) saved_source_dir: Option<std::path::PathBuf>,
62 pub(crate) module_functions: Option<ModuleFunctionRegistry>,
64 pub(crate) module_state: Option<crate::value::ModuleState>,
70 pub(crate) local_slots: Vec<LocalSlot>,
72 pub(crate) local_scope_base: usize,
74 pub(crate) local_scope_depth: usize,
76}
77
78pub(crate) struct ExceptionHandler {
80 pub(crate) catch_ip: usize,
81 pub(crate) stack_depth: usize,
82 pub(crate) frame_depth: usize,
83 pub(crate) env_scope_depth: usize,
84 pub(crate) error_type: String,
86}
87
88pub(crate) enum IterState {
90 Vec {
91 items: Rc<Vec<VmValue>>,
92 idx: usize,
93 },
94 Dict {
95 entries: Rc<BTreeMap<String, VmValue>>,
96 keys: Vec<String>,
97 idx: usize,
98 },
99 Channel {
100 receiver: std::sync::Arc<tokio::sync::Mutex<tokio::sync::mpsc::Receiver<VmValue>>>,
101 closed: std::sync::Arc<std::sync::atomic::AtomicBool>,
102 },
103 Generator {
104 gen: crate::value::VmGenerator,
105 },
106 Stream {
107 stream: crate::value::VmStream,
108 },
109 Range {
113 next: i64,
114 stop: i64,
115 },
116 VmIter {
117 handle: std::rc::Rc<std::cell::RefCell<crate::vm::iter::VmIter>>,
118 },
119}
120
121#[derive(Clone)]
122pub(crate) enum VmBuiltinDispatch {
123 Sync(VmBuiltinFn),
124 Async(VmAsyncBuiltinFn),
125}
126
127#[derive(Clone)]
128pub(crate) struct VmBuiltinEntry {
129 pub(crate) name: Rc<str>,
130 pub(crate) dispatch: VmBuiltinDispatch,
131}
132
133pub struct Vm {
135 pub(crate) stack: Vec<VmValue>,
136 pub(crate) env: VmEnv,
137 pub(crate) output: String,
138 pub(crate) builtins: Rc<BTreeMap<String, VmBuiltinFn>>,
139 pub(crate) async_builtins: Rc<BTreeMap<String, VmAsyncBuiltinFn>>,
140 pub(crate) builtin_metadata: Rc<BTreeMap<String, VmBuiltinMetadata>>,
141 pub(crate) builtins_by_id: Rc<BTreeMap<BuiltinId, VmBuiltinEntry>>,
144 pub(crate) builtin_id_collisions: Rc<HashSet<BuiltinId>>,
147 pub(crate) iterators: Vec<IterState>,
149 pub(crate) frames: Vec<CallFrame>,
151 pub(crate) exception_handlers: Vec<ExceptionHandler>,
153 pub(crate) spawned_tasks: BTreeMap<String, VmTaskHandle>,
155 pub(crate) sync_runtime: Arc<crate::synchronization::VmSyncRuntime>,
157 pub(crate) shared_state_runtime: Rc<crate::shared_state::VmSharedStateRuntime>,
159 pub(crate) held_sync_guards: Vec<crate::synchronization::VmSyncHeldGuard>,
161 pub(crate) task_counter: u64,
163 pub(crate) runtime_context_counter: u64,
165 pub(crate) runtime_context: crate::runtime_context::RuntimeContext,
167 pub(crate) deadlines: Vec<(Instant, usize)>,
169 pub(crate) breakpoints: BTreeMap<String, std::collections::BTreeSet<usize>>,
174 pub(crate) function_breakpoints: std::collections::BTreeSet<String>,
180 pub(crate) pending_function_bp: Option<String>,
185 pub(crate) step_mode: bool,
187 pub(crate) step_frame_depth: usize,
189 pub(crate) stopped: bool,
191 pub(crate) last_line: usize,
193 pub(crate) source_dir: Option<std::path::PathBuf>,
195 pub(crate) imported_paths: Vec<std::path::PathBuf>,
197 pub(crate) module_cache: Rc<BTreeMap<std::path::PathBuf, LoadedModule>>,
199 pub(crate) source_cache: Rc<BTreeMap<std::path::PathBuf, String>>,
201 pub(crate) source_file: Option<String>,
203 pub(crate) source_text: Option<String>,
205 pub(crate) bridge: Option<Rc<crate::bridge::HostBridge>>,
207 pub(crate) denied_builtins: Rc<HashSet<String>>,
209 pub(crate) cancel_token: Option<std::sync::Arc<std::sync::atomic::AtomicBool>>,
211 pub(crate) cancel_grace_instructions_remaining: Option<usize>,
216 pub(crate) error_stack_trace: Vec<(String, usize, usize, Option<String>)>,
218 pub(crate) yield_sender: Option<tokio::sync::mpsc::Sender<Result<VmValue, VmError>>>,
221 pub(crate) project_root: Option<std::path::PathBuf>,
224 pub(crate) globals: Rc<BTreeMap<String, VmValue>>,
227 pub(crate) debug_hook: Option<Box<DebugHook>>,
229}
230
231impl Vm {
232 pub(crate) fn fresh_local_slots(chunk: &Chunk) -> Vec<LocalSlot> {
233 chunk
234 .local_slots
235 .iter()
236 .map(|_| LocalSlot {
237 value: VmValue::Nil,
238 initialized: false,
239 synced: false,
240 })
241 .collect()
242 }
243
244 pub(crate) fn bind_param_slots(
245 slots: &mut [LocalSlot],
246 func: &crate::chunk::CompiledFunction,
247 args: &[VmValue],
248 synced: bool,
249 ) {
250 let param_count = func.params.len();
251 for (i, _param) in func.params.iter().enumerate() {
252 if i >= slots.len() {
253 break;
254 }
255 if func.has_rest_param && i == param_count - 1 {
256 let rest_args = if i < args.len() {
257 args[i..].to_vec()
258 } else {
259 Vec::new()
260 };
261 slots[i].value = VmValue::List(Rc::new(rest_args));
262 slots[i].initialized = true;
263 slots[i].synced = synced;
264 } else if i < args.len() {
265 slots[i].value = args[i].clone();
266 slots[i].initialized = true;
267 slots[i].synced = synced;
268 }
269 }
270 }
271
272 pub(crate) fn visible_variables(&self) -> BTreeMap<String, VmValue> {
273 let mut vars = self.env.all_variables();
274 let Some(frame) = self.frames.last() else {
275 return vars;
276 };
277 for (slot, info) in frame.local_slots.iter().zip(frame.chunk.local_slots.iter()) {
278 if slot.initialized && info.scope_depth <= frame.local_scope_depth {
279 vars.insert(info.name.clone(), slot.value.clone());
280 }
281 }
282 vars
283 }
284
285 pub(crate) fn sync_current_frame_locals_to_env(&mut self) {
286 let frames = &mut self.frames;
287 let env = &mut self.env;
288 let Some(frame) = frames.last_mut() else {
289 return;
290 };
291 let local_scope_base = frame.local_scope_base;
292 let local_scope_depth = frame.local_scope_depth;
293 for (slot, info) in frame
294 .local_slots
295 .iter_mut()
296 .zip(frame.chunk.local_slots.iter())
297 {
298 if slot.initialized && !slot.synced && info.scope_depth <= local_scope_depth {
299 slot.synced = true;
300 let scope_idx = local_scope_base + info.scope_depth;
301 while env.scopes.len() <= scope_idx {
302 env.push_scope();
303 }
304 env.scopes[scope_idx]
305 .vars
306 .insert(info.name.clone(), (slot.value.clone(), info.mutable));
307 }
308 }
309 }
310
311 pub(crate) fn closure_call_env_for_current_frame(
312 &self,
313 closure: &crate::value::VmClosure,
314 ) -> VmEnv {
315 if closure.module_state.is_some() {
316 return closure.env.clone();
317 }
318 let mut call_env = Self::closure_call_env(&self.env, closure);
319 let Some(frame) = self.frames.last() else {
320 return call_env;
321 };
322 for (slot, info) in frame
323 .local_slots
324 .iter()
325 .zip(frame.chunk.local_slots.iter())
326 .filter(|(slot, info)| slot.initialized && info.scope_depth <= frame.local_scope_depth)
327 {
328 if matches!(slot.value, VmValue::Closure(_)) && !call_env.contains(&info.name) {
329 let _ = call_env.define(&info.name, slot.value.clone(), info.mutable);
330 }
331 }
332 call_env
333 }
334
335 pub(crate) fn active_local_slot_value(&self, name: &str) -> Option<VmValue> {
336 let frame = self.frames.last()?;
337 for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
338 if info.name == name && info.scope_depth <= frame.local_scope_depth {
339 let slot = frame.local_slots.get(idx)?;
340 if slot.initialized {
341 return Some(slot.value.clone());
342 }
343 }
344 }
345 None
346 }
347
348 pub(crate) fn assign_active_local_slot(
349 &mut self,
350 name: &str,
351 value: VmValue,
352 debug: bool,
353 ) -> Result<bool, VmError> {
354 let Some(frame) = self.frames.last_mut() else {
355 return Ok(false);
356 };
357 for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
358 if info.name == name && info.scope_depth <= frame.local_scope_depth {
359 if !debug && !info.mutable {
360 return Err(VmError::ImmutableAssignment(name.to_string()));
361 }
362 if let Some(slot) = frame.local_slots.get_mut(idx) {
363 slot.value = value;
364 slot.initialized = true;
365 slot.synced = false;
366 return Ok(true);
367 }
368 }
369 }
370 Ok(false)
371 }
372
373 pub fn new() -> Self {
374 Self {
375 stack: Vec::with_capacity(256),
376 env: VmEnv::new(),
377 output: String::new(),
378 builtins: Rc::new(BTreeMap::new()),
379 async_builtins: Rc::new(BTreeMap::new()),
380 builtin_metadata: Rc::new(BTreeMap::new()),
381 builtins_by_id: Rc::new(BTreeMap::new()),
382 builtin_id_collisions: Rc::new(HashSet::new()),
383 iterators: Vec::new(),
384 frames: Vec::new(),
385 exception_handlers: Vec::new(),
386 spawned_tasks: BTreeMap::new(),
387 sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
388 shared_state_runtime: Rc::new(crate::shared_state::VmSharedStateRuntime::new()),
389 held_sync_guards: Vec::new(),
390 task_counter: 0,
391 runtime_context_counter: 0,
392 runtime_context: crate::runtime_context::RuntimeContext::root(),
393 deadlines: Vec::new(),
394 breakpoints: BTreeMap::new(),
395 function_breakpoints: std::collections::BTreeSet::new(),
396 pending_function_bp: None,
397 step_mode: false,
398 step_frame_depth: 0,
399 stopped: false,
400 last_line: 0,
401 source_dir: None,
402 imported_paths: Vec::new(),
403 module_cache: Rc::new(BTreeMap::new()),
404 source_cache: Rc::new(BTreeMap::new()),
405 source_file: None,
406 source_text: None,
407 bridge: None,
408 denied_builtins: Rc::new(HashSet::new()),
409 cancel_token: None,
410 cancel_grace_instructions_remaining: None,
411 error_stack_trace: Vec::new(),
412 yield_sender: None,
413 project_root: None,
414 globals: Rc::new(BTreeMap::new()),
415 debug_hook: None,
416 }
417 }
418
419 pub fn set_bridge(&mut self, bridge: Rc<crate::bridge::HostBridge>) {
421 self.bridge = Some(bridge);
422 }
423
424 pub fn set_denied_builtins(&mut self, denied: HashSet<String>) {
427 self.denied_builtins = Rc::new(denied);
428 }
429
430 pub fn set_source_info(&mut self, file: &str, text: &str) {
432 self.source_file = Some(file.to_string());
433 self.source_text = Some(text.to_string());
434 Rc::make_mut(&mut self.source_cache)
435 .insert(std::path::PathBuf::from(file), text.to_string());
436 }
437
438 pub fn start(&mut self, chunk: &Chunk) {
440 let initial_env = self.env.clone();
441 self.frames.push(CallFrame {
442 chunk: Rc::new(chunk.clone()),
443 ip: 0,
444 stack_base: self.stack.len(),
445 saved_env: self.env.clone(),
446 initial_env: Some(initial_env),
451 initial_local_slots: Some(Self::fresh_local_slots(chunk)),
452 saved_iterator_depth: self.iterators.len(),
453 fn_name: String::new(),
454 argc: 0,
455 saved_source_dir: None,
456 module_functions: None,
457 module_state: None,
458 local_slots: Self::fresh_local_slots(chunk),
459 local_scope_base: self.env.scope_depth().saturating_sub(1),
460 local_scope_depth: 0,
461 });
462 }
463
464 pub(crate) fn child_vm(&self) -> Vm {
467 Vm {
468 stack: Vec::with_capacity(64),
469 env: self.env.clone(),
470 output: String::new(),
471 builtins: Rc::clone(&self.builtins),
472 async_builtins: Rc::clone(&self.async_builtins),
473 builtin_metadata: Rc::clone(&self.builtin_metadata),
474 builtins_by_id: Rc::clone(&self.builtins_by_id),
475 builtin_id_collisions: Rc::clone(&self.builtin_id_collisions),
476 iterators: Vec::new(),
477 frames: Vec::new(),
478 exception_handlers: Vec::new(),
479 spawned_tasks: BTreeMap::new(),
480 sync_runtime: self.sync_runtime.clone(),
481 shared_state_runtime: self.shared_state_runtime.clone(),
482 held_sync_guards: Vec::new(),
483 task_counter: 0,
484 runtime_context_counter: self.runtime_context_counter,
485 runtime_context: self.runtime_context.clone(),
486 deadlines: self.deadlines.clone(),
487 breakpoints: BTreeMap::new(),
488 function_breakpoints: std::collections::BTreeSet::new(),
489 pending_function_bp: None,
490 step_mode: false,
491 step_frame_depth: 0,
492 stopped: false,
493 last_line: 0,
494 source_dir: self.source_dir.clone(),
495 imported_paths: Vec::new(),
496 module_cache: Rc::clone(&self.module_cache),
497 source_cache: Rc::clone(&self.source_cache),
498 source_file: self.source_file.clone(),
499 source_text: self.source_text.clone(),
500 bridge: self.bridge.clone(),
501 denied_builtins: Rc::clone(&self.denied_builtins),
502 cancel_token: self.cancel_token.clone(),
503 cancel_grace_instructions_remaining: None,
504 error_stack_trace: Vec::new(),
505 yield_sender: None,
506 project_root: self.project_root.clone(),
507 globals: Rc::clone(&self.globals),
508 debug_hook: None,
509 }
510 }
511
512 pub(crate) fn child_vm_for_host(&self) -> Vm {
515 self.child_vm()
516 }
517
518 pub(crate) fn cancel_spawned_tasks(&mut self) {
522 for (_, task) in std::mem::take(&mut self.spawned_tasks) {
523 task.cancel_token
524 .store(true, std::sync::atomic::Ordering::SeqCst);
525 task.handle.abort();
526 }
527 }
528
529 pub fn set_source_dir(&mut self, dir: &std::path::Path) {
532 let dir = crate::stdlib::process::normalize_context_path(dir);
533 self.source_dir = Some(dir.clone());
534 crate::stdlib::set_thread_source_dir(&dir);
535 if self.project_root.is_none() {
537 self.project_root = crate::stdlib::process::find_project_root(&dir);
538 }
539 }
540
541 pub fn set_project_root(&mut self, root: &std::path::Path) {
544 self.project_root = Some(root.to_path_buf());
545 }
546
547 pub fn project_root(&self) -> Option<&std::path::Path> {
549 self.project_root.as_deref().or(self.source_dir.as_deref())
550 }
551
552 pub fn builtin_names(&self) -> Vec<String> {
554 let mut names: Vec<String> = self.builtins.keys().cloned().collect();
555 names.extend(self.async_builtins.keys().cloned());
556 names
557 }
558
559 pub fn builtin_metadata(&self) -> Vec<VmBuiltinMetadata> {
561 self.builtin_metadata.values().cloned().collect()
562 }
563
564 pub fn builtin_metadata_for(&self, name: &str) -> Option<&VmBuiltinMetadata> {
566 self.builtin_metadata.get(name)
567 }
568
569 pub fn set_global(&mut self, name: &str, value: VmValue) {
572 Rc::make_mut(&mut self.globals).insert(name.to_string(), value);
573 }
574
575 pub fn output(&self) -> &str {
577 &self.output
578 }
579
580 pub fn take_output(&mut self) -> String {
584 std::mem::take(&mut self.output)
585 }
586
587 pub fn append_output(&mut self, text: &str) {
591 self.output.push_str(text);
592 }
593
594 pub(crate) fn pop(&mut self) -> Result<VmValue, VmError> {
595 self.stack.pop().ok_or(VmError::StackUnderflow)
596 }
597
598 pub(crate) fn peek(&self) -> Result<&VmValue, VmError> {
599 self.stack.last().ok_or(VmError::StackUnderflow)
600 }
601
602 pub(crate) fn const_string(c: &Constant) -> Result<String, VmError> {
603 match c {
604 Constant::String(s) => Ok(s.clone()),
605 _ => Err(VmError::TypeError("expected string constant".into())),
606 }
607 }
608
609 pub(crate) fn const_str(c: &Constant) -> Result<&str, VmError> {
610 match c {
611 Constant::String(s) => Ok(s.as_str()),
612 _ => Err(VmError::TypeError("expected string constant".into())),
613 }
614 }
615
616 pub(crate) fn release_sync_guards_for_current_scope(&mut self) {
617 let depth = self.env.scope_depth();
618 self.held_sync_guards
619 .retain(|guard| guard.env_scope_depth < depth);
620 }
621
622 pub(crate) fn release_sync_guards_after_unwind(
623 &mut self,
624 frame_depth: usize,
625 env_scope_depth: usize,
626 ) {
627 self.held_sync_guards.retain(|guard| {
628 guard.frame_depth <= frame_depth && guard.env_scope_depth <= env_scope_depth
629 });
630 }
631
632 pub(crate) fn release_sync_guards_for_frame(&mut self, frame_depth: usize) {
633 self.held_sync_guards
634 .retain(|guard| guard.frame_depth != frame_depth);
635 }
636}
637
638impl Drop for Vm {
639 fn drop(&mut self) {
640 self.cancel_spawned_tasks();
641 }
642}
643
644impl Default for Vm {
645 fn default() -> Self {
646 Self::new()
647 }
648}