Skip to main content

runmat_core/session/
init.rs

1use super::*;
2
3impl RunMatSession {
4    /// Create a new session
5    pub fn new() -> Result<Self> {
6        Self::with_options(true, false) // JIT enabled, verbose disabled
7    }
8
9    /// Create a new session with specific options
10    pub fn with_options(enable_jit: bool, verbose: bool) -> Result<Self> {
11        Self::initialize(enable_jit, verbose)
12    }
13
14    fn initialize(enable_jit: bool, verbose: bool) -> Result<Self> {
15        #[cfg(feature = "jit")]
16        let jit_engine = if enable_jit {
17            match TurbineEngine::new() {
18                Ok(engine) => {
19                    info!("JIT compiler initialized successfully");
20                    Some(engine)
21                }
22                Err(e) => {
23                    warn!("JIT compiler initialization failed: {e}, falling back to interpreter");
24                    None
25                }
26            }
27        } else {
28            info!("JIT compiler disabled, using interpreter only");
29            None
30        };
31
32        #[cfg(not(feature = "jit"))]
33        if enable_jit {
34            info!(
35                "JIT support was requested but the 'jit' feature is disabled; running interpreter-only."
36            );
37        }
38
39        let session = Self {
40            #[cfg(feature = "jit")]
41            jit_engine,
42            verbose,
43            stats: ExecutionStats::default(),
44            variable_array: Vec::new(),
45            workspace_bindings: HashMap::new(),
46            workspace_values: HashMap::new(),
47            abi_workspace_handle: crate::abi::WorkspaceHandle(Uuid::new_v4()),
48            active_source_identity: None,
49            function_registry: runmat_vm::FunctionRegistry::default(),
50            next_semantic_function_id: 0,
51            source_pool: SourcePool::default(),
52            interrupt_flag: Arc::new(AtomicBool::new(false)),
53            is_executing: false,
54            async_input_handler: None,
55            callstack_limit: runmat_vm::DEFAULT_CALLSTACK_LIMIT,
56            error_namespace: runmat_vm::DEFAULT_ERROR_NAMESPACE.to_string(),
57            active_source_name: "<repl>".to_string(),
58            active_source_fullpath_name: None,
59            telemetry_consent: true,
60            telemetry_client_id: None,
61            telemetry_platform: TelemetryPlatformInfo::default(),
62            telemetry_sink: None,
63            workspace_preview_tokens: HashMap::new(),
64            workspace_version: 0,
65            emit_fusion_plan: false,
66            compat_mode: CompatMode::Matlab,
67            top_level_await_enabled: true,
68            dynamic_eval_enabled: true,
69            format_mode: runmat_builtins::FormatMode::default(),
70            diary_state: runmat_runtime::console::DiaryStateSnapshot::default(),
71            pending_companion_source_discovery: None,
72        };
73
74        runmat_vm::set_call_stack_limit(session.callstack_limit);
75
76        // Cache the shared plotting context (if a GPU provider is active) so the
77        // runtime can wire zero-copy render paths without instantiating another
78        // WebGPU device.
79        #[cfg(any(target_arch = "wasm32", not(target_arch = "wasm32")))]
80        {
81            if let Err(err) =
82                runmat_runtime::builtins::plotting::context::ensure_context_from_provider()
83            {
84                debug!("Plotting context unavailable during session init: {err}");
85            }
86        }
87
88        Ok(session)
89    }
90
91    pub(crate) fn current_source_name(&self) -> &str {
92        &self.active_source_name
93    }
94
95    pub(crate) fn current_source_fullpath_name(&self) -> Option<&str> {
96        self.active_source_fullpath_name.as_deref()
97    }
98}