shape_vm/executor/vm_impl/
init.rs1use super::super::*;
2
3impl VirtualMachine {
4 pub fn new(config: VMConfig) -> Self {
5 let debugger = if config.debug_mode {
6 Some(VMDebugger::new())
7 } else {
8 None
9 };
10
11 let gc = GarbageCollector::new(config.gc_config.clone());
12
13 let (registry, builtin_schemas) =
16 shape_runtime::type_schema::TypeSchemaRegistry::with_stdlib_types_and_builtin_ids();
17
18 let mut program = BytecodeProgram::new();
19 program.type_schema_registry = registry;
20
21 let mut vm = Self {
22 config,
23 program,
24 ip: 0,
25 stack: (0..crate::constants::DEFAULT_STACK_CAPACITY)
26 .map(|_| ValueWord::none())
27 .collect(),
28 sp: 0,
29 module_bindings: Vec::new(),
30 call_stack: Vec::with_capacity(crate::constants::DEFAULT_CALL_STACK_CAPACITY),
31 loop_stack: Vec::new(),
32 timeframe_stack: Vec::new(),
33 debugger,
34 gc,
35 instruction_count: 0,
36 exception_handlers: Vec::new(),
37 builtin_schemas,
38 last_error_line: None,
39 last_error_file: None,
40 last_uncaught_exception: None,
41 module_init_done: false,
42 output_buffer: None,
43 module_registry: shape_runtime::module_exports::ModuleExportRegistry::new(),
44 module_fn_table: Vec::new(),
45 function_name_index: HashMap::new(),
46 extension_methods: HashMap::new(),
47 merged_schema_cache: HashMap::new(),
48 interrupt: Arc::new(AtomicU8::new(0)),
49 future_id_counter: 0,
50 async_scope_stack: Vec::new(),
51 task_scheduler: task_scheduler::TaskScheduler::new(),
52 foreign_fn_handles: Vec::new(),
53 function_hashes: Vec::new(),
54 function_hash_raw: Vec::new(),
55 function_id_by_hash: HashMap::new(),
56 function_entry_points: Vec::new(),
57 program_entry_ip: 0,
58 resource_usage: None,
59 time_travel: None,
60 #[cfg(feature = "gc")]
61 gc_heap: None,
62 #[cfg(feature = "jit")]
63 jit_compiled: false,
64 #[cfg(feature = "jit")]
65 jit_dispatch_table: std::collections::HashMap::new(),
66 tier_manager: None,
67 pending_resume: None,
68 pending_frame_resume: None,
69 metrics: None,
70 feedback_vectors: Vec::new(),
71 megamorphic_cache: crate::megamorphic_cache::MegamorphicCache::new(),
72 };
73
74 vm.register_stdlib_module(state_builtins::create_state_module());
78 vm.register_stdlib_module(create_transport_module_exports());
79 vm.register_stdlib_module(create_remote_module_exports());
80 for module in shape_runtime::stdlib::all_stdlib_modules() {
82 vm.register_stdlib_module(module);
83 }
84
85 if vm.config.metrics_enabled {
87 vm.metrics = Some(crate::metrics::VmMetrics::new());
88 }
89
90 #[cfg(feature = "gc")]
92 if vm.config.use_tracing_gc {
93 vm.init_gc_heap();
94 }
95
96 vm
97 }
98
99 pub fn with_resource_limits(mut self, limits: crate::resource_limits::ResourceLimits) -> Self {
101 let mut usage = crate::resource_limits::ResourceUsage::new(limits);
102 usage.start();
103 self.resource_usage = Some(usage);
104 self
105 }
106
107 #[cfg(feature = "gc")]
113 pub fn init_gc_heap(&mut self) {
114 let heap = shape_gc::GcHeap::new();
115 self.gc_heap = Some(heap);
116 if let Some(ref mut heap) = self.gc_heap {
119 unsafe { shape_gc::set_thread_gc_heap(heap as *mut _) };
120 }
121 }
122
123 pub fn set_interrupt(&mut self, flag: Arc<AtomicU8>) {
125 self.interrupt = flag;
126 }
127
128 pub fn enable_time_travel(&mut self, mode: time_travel::CaptureMode, max_entries: usize) {
130 self.time_travel = Some(time_travel::TimeTravel::new(mode, max_entries));
131 }
132
133 pub fn disable_time_travel(&mut self) {
135 self.time_travel = None;
136 }
137
138 #[cfg(feature = "jit")]
153 pub fn set_jit_compiled(&mut self) {
154 self.jit_compiled = true;
155 }
156
157 #[cfg(feature = "jit")]
159 pub fn is_jit_compiled(&self) -> bool {
160 self.jit_compiled
161 }
162
163 #[cfg(feature = "jit")]
168 pub fn register_jit_function(&mut self, function_id: u16, ptr: JitFnPtr) {
169 self.jit_dispatch_table.insert(function_id, ptr);
170 self.jit_compiled = true;
171 }
172
173 #[cfg(feature = "jit")]
175 pub fn jit_dispatch_table(&self) -> &std::collections::HashMap<u16, JitFnPtr> {
176 &self.jit_dispatch_table
177 }
178
179 pub fn enable_tiered_compilation(
187 &mut self,
188 ) -> (
189 std::sync::mpsc::Receiver<crate::tier::CompilationRequest>,
190 std::sync::mpsc::Sender<crate::tier::CompilationResult>,
191 ) {
192 let function_count = self.program.functions.len();
193 let mut mgr = crate::tier::TierManager::new(function_count, true);
194
195 let (req_tx, req_rx) = std::sync::mpsc::channel();
196 let (res_tx, res_rx) = std::sync::mpsc::channel();
197 mgr.set_channels(req_tx, res_rx);
198
199 self.tier_manager = Some(mgr);
200 (req_rx, res_tx)
201 }
202
203 pub fn tier_manager(&self) -> Option<&crate::tier::TierManager> {
205 self.tier_manager.as_ref()
206 }
207
208 pub(crate) fn poll_tier_completions(&mut self) {
217 if let Some(ref mut tier_mgr) = self.tier_manager {
218 let completions = tier_mgr.poll_completions();
221
222 if let Some(ref mut metrics) = self.metrics {
224 for result in &completions {
225 if result.native_code.is_some() {
226 let from_tier = match result.compiled_tier {
227 crate::tier::Tier::BaselineJit => 0, crate::tier::Tier::OptimizingJit => 1, crate::tier::Tier::Interpreted => continue,
230 };
231 let to_tier = match result.compiled_tier {
232 crate::tier::Tier::BaselineJit => 1,
233 crate::tier::Tier::OptimizingJit => 2,
234 crate::tier::Tier::Interpreted => continue,
235 };
236 metrics.record_tier_event(crate::metrics::TierEvent {
237 function_id: result.function_id,
238 from_tier,
239 to_tier,
240 call_count: tier_mgr.get_call_count(result.function_id),
241 timestamp_us: metrics.elapsed_us(),
242 });
243 }
244 }
245 }
246 }
247 }
248
249 #[inline]
252 pub(crate) fn current_feedback_vector(
253 &mut self,
254 ) -> Option<&mut crate::feedback::FeedbackVector> {
255 let func_id = self.call_stack.last()?.function_id? as usize;
256 if func_id >= self.feedback_vectors.len() {
257 return None;
258 }
259 if self.feedback_vectors[func_id].is_none() {
260 if self.tier_manager.is_none() {
261 return None;
262 }
263 self.feedback_vectors[func_id] =
264 Some(crate::feedback::FeedbackVector::new(func_id as u16));
265 }
266 self.feedback_vectors[func_id].as_mut()
267 }
268
269 pub fn feedback_vectors(&self) -> &[Option<crate::feedback::FeedbackVector>] {
271 &self.feedback_vectors
272 }
273
274 pub fn program(&self) -> &BytecodeProgram {
276 &self.program
277 }
278
279 pub fn time_travel(&self) -> Option<&time_travel::TimeTravel> {
281 self.time_travel.as_ref()
282 }
283
284 pub fn time_travel_mut(&mut self) -> Option<&mut time_travel::TimeTravel> {
286 self.time_travel.as_mut()
287 }
288
289 pub fn module_registry(&self) -> &shape_runtime::module_exports::ModuleExportRegistry {
291 &self.module_registry
292 }
293
294 pub(crate) fn next_future_id(&mut self) -> u64 {
296 self.future_id_counter += 1;
297 self.future_id_counter
298 }
299
300 pub fn get_function_id(&self, name: &str) -> Option<u16> {
302 self.program
303 .functions
304 .iter()
305 .position(|f| f.name == name)
306 .map(|id| id as u16)
307 }
308}