Skip to main content

runmat_core/session/
config.rs

1use super::*;
2
3impl RunMatSession {
4    /// Install an async stdin handler (Phase 2). This is the preferred input path for
5    /// poll-driven execution (`ExecuteFuture`).
6    ///
7    /// The handler is invoked when `input()` / `pause()` needs a line or keypress, and the
8    /// returned future is awaited by the runtime.
9    pub fn install_async_input_handler<F, Fut>(&mut self, handler: F)
10    where
11        F: Fn(InputRequest) -> Fut + Send + Sync + 'static,
12        Fut: Future<Output = Result<InputResponse, String>> + 'static,
13    {
14        self.async_input_handler = Some(Arc::new(move |req: InputRequest| {
15            let fut = handler(req);
16            Box::pin(fut)
17        }));
18    }
19
20    pub fn clear_async_input_handler(&mut self) {
21        self.async_input_handler = None;
22    }
23
24    pub fn telemetry_consent(&self) -> bool {
25        self.telemetry_consent
26    }
27
28    pub fn set_telemetry_consent(&mut self, consent: bool) {
29        self.telemetry_consent = consent;
30    }
31
32    pub fn telemetry_client_id(&self) -> Option<&str> {
33        self.telemetry_client_id.as_deref()
34    }
35
36    pub fn set_telemetry_client_id(&mut self, cid: Option<String>) {
37        self.telemetry_client_id = cid;
38    }
39
40    /// Request cooperative cancellation for the currently running execution.
41    pub fn cancel_execution(&self) {
42        self.interrupt_flag.store(true, Ordering::Relaxed);
43    }
44
45    /// Shared interrupt flag used by the VM to implement cooperative cancellation.
46    pub fn interrupt_handle(&self) -> Arc<AtomicBool> {
47        Arc::clone(&self.interrupt_flag)
48    }
49
50    /// Get execution statistics
51    pub fn stats(&self) -> &ExecutionStats {
52        &self.stats
53    }
54
55    /// Reset execution statistics
56    pub fn reset_stats(&mut self) {
57        self.stats = ExecutionStats::default();
58    }
59
60    /// Control whether fusion plan snapshots are emitted in [`ExecutionResult`].
61    pub fn set_emit_fusion_plan(&mut self, enabled: bool) {
62        self.emit_fusion_plan = enabled;
63    }
64
65    /// Return the active language compatibility mode.
66    pub fn compat_mode(&self) -> CompatMode {
67        self.compat_mode
68    }
69
70    /// Set the language compatibility mode (`matlab` or `strict`).
71    pub fn set_compat_mode(&mut self, mode: CompatMode) {
72        self.compat_mode = mode;
73    }
74
75    pub fn set_callstack_limit(&mut self, limit: usize) {
76        self.callstack_limit = limit;
77        runmat_vm::set_call_stack_limit(limit);
78    }
79
80    pub fn set_error_namespace(&mut self, namespace: impl Into<String>) {
81        let namespace = namespace.into();
82        let namespace = if namespace.trim().is_empty() {
83            runmat_vm::DEFAULT_ERROR_NAMESPACE.to_string()
84        } else {
85            namespace
86        };
87        self.error_namespace = namespace.clone();
88        runmat_vm::set_error_namespace(&namespace);
89        runmat_hir::set_error_namespace(&namespace);
90    }
91
92    pub fn set_source_name_override(&mut self, name: Option<String>) {
93        self.source_name_override = name;
94    }
95
96    /// Configure garbage collector
97    pub fn configure_gc(&self, config: GcConfig) -> Result<()> {
98        gc_configure(config)
99            .map_err(|e| anyhow::anyhow!("Failed to configure garbage collector: {}", e))
100    }
101
102    /// Get GC statistics
103    pub fn gc_stats(&self) -> runmat_gc::GcStats {
104        gc_stats()
105    }
106
107    /// Show detailed system information
108    pub fn show_system_info(&self) {
109        let gc_stats = self.gc_stats();
110        info!(
111            jit = %if self.has_jit() { "available" } else { "disabled/failed" },
112            verbose = self.verbose,
113            total_executions = self.stats.total_executions,
114            jit_compiled = self.stats.jit_compiled,
115            interpreter_fallback = self.stats.interpreter_fallback,
116            avg_time_ms = self.stats.average_execution_time_ms,
117            total_allocations = gc_stats
118                .total_allocations
119                .load(std::sync::atomic::Ordering::Relaxed),
120            minor_collections = gc_stats
121                .minor_collections
122                .load(std::sync::atomic::Ordering::Relaxed),
123            major_collections = gc_stats
124                .major_collections
125                .load(std::sync::atomic::Ordering::Relaxed),
126            current_memory_mb = gc_stats
127                .current_memory_usage
128                .load(std::sync::atomic::Ordering::Relaxed) as f64
129                / 1024.0
130                / 1024.0,
131            workspace_vars = self.workspace_values.len(),
132            "RunMat Session Status"
133        );
134    }
135
136    #[cfg(feature = "jit")]
137    pub(crate) fn has_jit(&self) -> bool {
138        self.jit_engine.is_some()
139    }
140
141    #[cfg(not(feature = "jit"))]
142    pub(crate) fn has_jit(&self) -> bool {
143        false
144    }
145}