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 [`crate::abi::ExecutionOutcome`].
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    /// Configure garbage collector
93    pub fn configure_gc(&self, config: GcConfig) -> Result<()> {
94        gc_configure(config)
95            .map_err(|e| anyhow::anyhow!("Failed to configure garbage collector: {}", e))
96    }
97
98    /// Get GC statistics
99    pub fn gc_stats(&self) -> runmat_gc::GcStats {
100        gc_stats()
101    }
102
103    /// Show detailed system information
104    pub fn show_system_info(&self) {
105        let gc_stats = self.gc_stats();
106        info!(
107            jit = %if self.has_jit() { "available" } else { "disabled/failed" },
108            verbose = self.verbose,
109            total_executions = self.stats.total_executions,
110            jit_compiled = self.stats.jit_compiled,
111            interpreter_fallback = self.stats.interpreter_fallback,
112            avg_time_ms = self.stats.average_execution_time_ms,
113            total_allocations = gc_stats
114                .total_allocations
115                .load(std::sync::atomic::Ordering::Relaxed),
116            minor_collections = gc_stats
117                .minor_collections
118                .load(std::sync::atomic::Ordering::Relaxed),
119            major_collections = gc_stats
120                .major_collections
121                .load(std::sync::atomic::Ordering::Relaxed),
122            current_memory_mb = gc_stats
123                .current_memory_usage
124                .load(std::sync::atomic::Ordering::Relaxed) as f64
125                / 1024.0
126                / 1024.0,
127            workspace_vars = self.workspace_values.len(),
128            "RunMat Session Status"
129        );
130    }
131
132    #[cfg(feature = "jit")]
133    pub(crate) fn has_jit(&self) -> bool {
134        self.jit_engine.is_some()
135    }
136
137    #[cfg(not(feature = "jit"))]
138    pub(crate) fn has_jit(&self) -> bool {
139        false
140    }
141}