runmat-runtime 0.6.0

Core runtime for RunMat with builtins, BLAS/LAPACK integration, and execution APIs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
use once_cell::sync::OnceCell;
use runmat_builtins::Value;
use runmat_thread_local::runmat_thread_local;
use runmat_time::unix_timestamp_ms;
use std::cell::RefCell;
use std::io::Write;
use std::path::PathBuf;
use std::sync::{Arc, RwLock};

/// Identifies the console stream that received the text.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ConsoleStream {
    Stdout,
    Stderr,
    ClearScreen,
}

/// Single console write (line or chunk) captured during execution.
#[derive(Clone, Debug)]
pub struct ConsoleEntry {
    pub stream: ConsoleStream,
    pub text: String,
    pub timestamp_ms: u64,
}

type StreamForwarder = dyn Fn(&ConsoleEntry) + Send + Sync + 'static;

runmat_thread_local! {
    static THREAD_BUFFER: RefCell<Vec<ConsoleEntry>> = const { RefCell::new(Vec::new()) };
    static LAST_VALUE_OUTPUT: RefCell<Option<Value>> = const { RefCell::new(None) };
    static CAPTURE_STACK: RefCell<Vec<Vec<ConsoleEntry>>> = const { RefCell::new(Vec::new()) };
    static DIARY_STATE: RefCell<DiaryState> = RefCell::new(DiaryState::default());
}

static FORWARDER: OnceCell<RwLock<Option<Arc<StreamForwarder>>>> = OnceCell::new();

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DiaryStateSnapshot {
    pub enabled: bool,
    pub filename: PathBuf,
}

impl Default for DiaryStateSnapshot {
    fn default() -> Self {
        Self {
            enabled: false,
            filename: PathBuf::from("diary"),
        }
    }
}

#[derive(Debug)]
struct DiaryState {
    enabled: bool,
    filename: PathBuf,
    last_error: Option<String>,
}

impl Default for DiaryState {
    fn default() -> Self {
        Self {
            enabled: false,
            filename: PathBuf::from("diary"),
            last_error: None,
        }
    }
}

impl From<DiaryStateSnapshot> for DiaryState {
    fn from(snapshot: DiaryStateSnapshot) -> Self {
        Self {
            enabled: snapshot.enabled,
            filename: snapshot.filename,
            last_error: None,
        }
    }
}

/// Guard for a dynamic command-window capture scope such as `evalc`.
pub struct ConsoleCaptureGuard {
    active: bool,
}

fn now_ms() -> u64 {
    unix_timestamp_ms().min(u64::MAX as u128) as u64
}

/// Record console output for the current thread while also forwarding it to any
/// registered listener (used by wasm bindings for live streaming).
pub fn record_console_output(stream: ConsoleStream, text: impl Into<String>) {
    let entry = ConsoleEntry {
        stream,
        text: text.into(),
        timestamp_ms: now_ms(),
    };
    if CAPTURE_STACK.with(|captures| {
        let mut captures = captures.borrow_mut();
        if let Some(current) = captures.last_mut() {
            current.push(entry.clone());
            true
        } else {
            false
        }
    }) {
        return;
    }

    THREAD_BUFFER.with(|buf| buf.borrow_mut().push(entry.clone()));
    write_diary_entry(&entry);

    if let Some(forwarder) = FORWARDER
        .get()
        .and_then(|lock| lock.read().ok().map(|guard| guard.as_ref().cloned()))
        .flatten()
    {
        forwarder(&entry);
    }
}

/// Record a control event that asks the host to clear the visible console.
pub fn record_clear_screen() {
    record_console_output(ConsoleStream::ClearScreen, String::new());
}

/// Record a line-oriented console entry, ensuring the stream text ends with a newline.
pub fn record_console_line(stream: ConsoleStream, text: impl Into<String>) {
    let mut text = text.into();
    if !text.ends_with('\n') {
        text.push('\n');
    }
    record_console_output(stream, text);
}

/// Clears the per-thread console buffer. Call this before execution begins so
/// each run only returns fresh output.
pub fn reset_thread_buffer() {
    THREAD_BUFFER.with(|buf| buf.borrow_mut().clear());
    LAST_VALUE_OUTPUT.with(|value| value.borrow_mut().take());
}

/// Drain (and return) the buffered console entries for the current thread.
pub fn take_thread_buffer() -> Vec<ConsoleEntry> {
    THREAD_BUFFER.with(|buf| buf.borrow_mut().drain(..).collect())
}

/// Append console entries captured on another execution thread without
/// forwarding them again to live stream listeners.
pub fn append_thread_buffer(entries: impl IntoIterator<Item = ConsoleEntry>) {
    THREAD_BUFFER.with(|buf| {
        let mut buf = buf.borrow_mut();
        buf.extend(entries);
        buf.sort_by_key(|entry| entry.timestamp_ms);
    });
}

/// Install (or remove) a global forwarder for console output. Passing `None`
/// removes the current listener.
pub fn install_forwarder(forwarder: Option<Arc<StreamForwarder>>) {
    let lock = FORWARDER.get_or_init(|| RwLock::new(None));
    if let Ok(mut guard) = lock.write() {
        *guard = forwarder;
    }
}

/// Convenience helper to record formatted value output (matching MATLAB's `name = value` layout).
pub fn record_value_output(label: Option<&str>, value: &Value) {
    LAST_VALUE_OUTPUT.with(|last| {
        *last.borrow_mut() = Some(value.clone());
    });
    let value_text = match value {
        Value::Object(obj) if obj.is_class("datetime") => {
            crate::builtins::datetime::datetime_display_text(value)
                .ok()
                .flatten()
                .unwrap_or_else(|| value.to_string())
        }
        Value::Object(obj) if obj.is_class("duration") => {
            crate::builtins::duration::duration_display_text(value)
                .ok()
                .flatten()
                .unwrap_or_else(|| value.to_string())
        }
        _ => value.to_string(),
    };
    let text = if let Some(name) = label {
        if is_unlabeled_nd_page_display(&value_text) {
            inject_label_into_nd_page_headers(name, &value_text)
        } else if value_text.contains('\n') {
            format!("{name} =\n{value_text}")
        } else {
            format!("{name} = {value_text}")
        }
    } else {
        value_text
    };
    record_console_line(ConsoleStream::Stdout, text);
}

pub fn take_last_value_output() -> Option<Value> {
    LAST_VALUE_OUTPUT.with(|value| value.borrow_mut().take())
}

/// Begin capturing command-window text for the current execution context.
///
/// While a capture is active, stdout/stderr entries are diverted into the
/// capture buffer instead of the normal execution stream, live forwarder, or
/// diary log. This matches MATLAB's `evalc` behavior, where `diary` is disabled
/// inside the captured evaluation.
pub fn begin_capture() -> ConsoleCaptureGuard {
    CAPTURE_STACK.with(|captures| captures.borrow_mut().push(Vec::new()));
    ConsoleCaptureGuard { active: true }
}

impl ConsoleCaptureGuard {
    pub fn finish(mut self) -> String {
        self.active = false;
        let entries =
            CAPTURE_STACK.with(|captures| captures.borrow_mut().pop().unwrap_or_default());
        captured_text(entries)
    }
}

impl Drop for ConsoleCaptureGuard {
    fn drop(&mut self) {
        if self.active {
            CAPTURE_STACK.with(|captures| {
                captures.borrow_mut().pop();
            });
        }
    }
}

fn captured_text(entries: Vec<ConsoleEntry>) -> String {
    let mut out = String::new();
    for entry in entries {
        if matches!(entry.stream, ConsoleStream::Stdout | ConsoleStream::Stderr) {
            out.push_str(&entry.text);
        }
    }
    out
}

pub fn diary_enabled() -> bool {
    DIARY_STATE.with(|state| state.borrow().enabled)
}

pub fn diary_filename() -> PathBuf {
    DIARY_STATE.with(|state| state.borrow().filename.clone())
}

pub fn diary_state_snapshot() -> DiaryStateSnapshot {
    DIARY_STATE.with(|state| {
        let state = state.borrow();
        DiaryStateSnapshot {
            enabled: state.enabled,
            filename: state.filename.clone(),
        }
    })
}

pub fn replace_diary_state(snapshot: DiaryStateSnapshot) -> DiaryStateSnapshot {
    DIARY_STATE.with(|state| {
        let previous = {
            let state = state.borrow();
            DiaryStateSnapshot {
                enabled: state.enabled,
                filename: state.filename.clone(),
            }
        };
        *state.borrow_mut() = DiaryState::from(snapshot);
        previous
    })
}

pub fn take_diary_error() -> Option<String> {
    DIARY_STATE.with(|state| state.borrow_mut().last_error.take())
}

pub fn set_diary_filename(filename: impl Into<PathBuf>) {
    DIARY_STATE.with(|state| {
        let mut state = state.borrow_mut();
        state.filename = filename.into();
        state.enabled = true;
        state.last_error = None;
    });
}

pub fn set_diary_filename_checked(filename: impl Into<PathBuf>) -> std::io::Result<()> {
    let filename = filename.into();
    open_diary_append(&filename).map(|_| ())?;
    set_diary_filename(filename);
    Ok(())
}

pub fn set_diary_enabled(enabled: bool) {
    DIARY_STATE.with(|state| {
        let mut state = state.borrow_mut();
        state.enabled = enabled;
        if enabled {
            state.last_error = None;
        }
    });
}

pub fn set_diary_enabled_checked(enabled: bool) -> std::io::Result<()> {
    if enabled {
        ensure_diary_writable()?;
    }
    set_diary_enabled(enabled);
    Ok(())
}

pub fn toggle_diary() -> std::io::Result<()> {
    if diary_enabled() {
        set_diary_enabled(false);
    } else {
        set_diary_enabled_checked(true)?;
    }
    Ok(())
}

pub fn ensure_diary_writable() -> std::io::Result<()> {
    let filename = diary_filename();
    open_diary_append(&filename).map(|_| ())
}

/// Append the top-level source text to the diary without echoing it to stdout.
pub fn record_diary_command(text: &str) {
    if !diary_enabled() {
        return;
    }
    let mut owned = text.to_string();
    if !owned.ends_with('\n') {
        owned.push('\n');
    }
    if let Err(err) = write_diary_text(&owned) {
        record_diary_failure(err);
    }
}

fn write_diary_entry(entry: &ConsoleEntry) {
    if !matches!(entry.stream, ConsoleStream::Stdout | ConsoleStream::Stderr) {
        return;
    }
    if diary_enabled() {
        if let Err(err) = write_diary_text(&entry.text) {
            record_diary_failure(err);
        }
    }
}

fn record_diary_failure(err: std::io::Error) {
    DIARY_STATE.with(|state| {
        let mut state = state.borrow_mut();
        state.enabled = false;
        state.last_error = Some(format!("diary: failed to write diary file ({err})"));
    });
}

fn write_diary_text(text: &str) -> std::io::Result<()> {
    let filename = diary_filename();
    let mut file = open_diary_append(&filename)?;
    file.write_all(text.as_bytes())?;
    file.flush()
}

fn open_diary_append(path: &PathBuf) -> std::io::Result<runmat_filesystem::File> {
    let mut options = runmat_filesystem::OpenOptions::new();
    options.create(true).append(true);
    options.open(path)
}

fn is_unlabeled_nd_page_display(text: &str) -> bool {
    text.lines()
        .any(|line| line.trim_start().starts_with("(:, :") && line.trim_end().ends_with('='))
}

fn inject_label_into_nd_page_headers(label: &str, text: &str) -> String {
    let mut out = String::new();
    for (idx, line) in text.lines().enumerate() {
        if idx > 0 {
            out.push('\n');
        }
        let trimmed = line.trim_start();
        if trimmed.starts_with("(:, :") && trimmed.trim_end().ends_with('=') {
            out.push_str(label);
            out.push_str(trimmed);
        } else {
            out.push_str(line);
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    fn entry(text: &str, timestamp_ms: u64) -> ConsoleEntry {
        ConsoleEntry {
            stream: ConsoleStream::Stdout,
            text: text.to_string(),
            timestamp_ms,
        }
    }

    #[test]
    fn append_thread_buffer_orders_entries_by_timestamp_stably() {
        reset_thread_buffer();
        append_thread_buffer(vec![entry("late", 20)]);
        append_thread_buffer(vec![entry("early", 10), entry("same-time", 20)]);

        let entries = take_thread_buffer();
        let texts = entries
            .into_iter()
            .map(|entry| entry.text)
            .collect::<Vec<_>>();
        assert_eq!(texts, vec!["early", "late", "same-time"]);
    }

    #[test]
    fn capture_diverts_console_text_from_thread_buffer() {
        let _lock = runmat_filesystem::provider_override_lock();
        set_diary_enabled(false);
        reset_thread_buffer();
        let capture = begin_capture();
        record_console_line(ConsoleStream::Stdout, "inside");
        let text = capture.finish();
        record_console_line(ConsoleStream::Stdout, "outside");

        assert_eq!(text, "inside\n");
        let entries = take_thread_buffer();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].text, "outside\n");
    }
}