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            search_path: Arc::new(
52                runmat_runtime::builtins::common::path_state::SearchPath::new(
53                    runmat_runtime::builtins::common::path_state::current_path_string(),
54                ),
55            ),
56            dynamic_function_cache: Arc::new(Mutex::new(HashMap::new())),
57            source_pool: SourcePool::default(),
58            interrupt_flag: Arc::new(AtomicBool::new(false)),
59            is_executing: false,
60            async_input_handler: None,
61            callstack_limit: runmat_vm::DEFAULT_CALLSTACK_LIMIT,
62            error_namespace: runmat_vm::DEFAULT_ERROR_NAMESPACE.to_string(),
63            active_source_name: "<repl>".to_string(),
64            active_source_fullpath_name: None,
65            telemetry_consent: true,
66            telemetry_client_id: None,
67            telemetry_platform: TelemetryPlatformInfo::default(),
68            telemetry_sink: None,
69            workspace_preview_tokens: HashMap::new(),
70            workspace_version: 0,
71            emit_fusion_plan: false,
72            compat_mode: CompatMode::Matlab,
73            top_level_await_enabled: true,
74            dynamic_eval_enabled: true,
75            format_mode: runmat_builtins::FormatMode::default(),
76            diary_state: runmat_runtime::console::DiaryStateSnapshot::default(),
77            pending_companion_source_discovery: None,
78        };
79
80        runmat_vm::set_call_stack_limit(session.callstack_limit);
81
82        // Cache the shared plotting context (if a GPU provider is active) so the
83        // runtime can wire zero-copy render paths without instantiating another
84        // WebGPU device.
85        #[cfg(any(target_arch = "wasm32", not(target_arch = "wasm32")))]
86        {
87            if let Err(err) =
88                runmat_runtime::builtins::plotting::context::ensure_context_from_provider()
89            {
90                debug!("Plotting context unavailable during session init: {err}");
91            }
92        }
93
94        Ok(session)
95    }
96
97    pub(crate) fn current_source_name(&self) -> &str {
98        &self.active_source_name
99    }
100
101    pub(crate) fn current_source_fullpath_name(&self) -> Option<&str> {
102        self.active_source_fullpath_name.as_deref()
103    }
104}