aboutsummaryrefslogtreecommitdiff
path: root/bundle/src-tauri/src/runner.rs
blob: eef35d21cf2c9ef0f89985fe369ddb1c7e2249e1 (plain)
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

use serde::Serialize;
use std::{
    path::PathBuf,
    process::Stdio,
    sync::{Arc, Mutex},
};
use tokio::{
    io::{AsyncRead, AsyncReadExt, AsyncWriteExt},
    process::Command,
    sync::watch,
};

#[derive(Clone, Default)]
pub struct Runner(Arc<Mutex<Option<Control>>>);

struct Control {
    stop: watch::Sender<bool>,
    done: watch::Receiver<bool>,
}

#[derive(Serialize)]
#[serde(tag = "event", rename_all = "camelCase")]
pub enum OutputEvent {
    Data { stream: String, bytes: Vec<u8> },
    Finished { code: Option<i32>, stopped: bool },
}

pub struct RunResult {
    pub code: Option<i32>,
    pub stopped: bool,
}

impl Runner {
    pub async fn run(
        &self,
        executable: PathBuf,
        source: PathBuf,
        input: String,
        output: impl Fn(&str, Vec<u8>) + Send + Sync,
    ) -> Result<RunResult, String> {
        let (stop, stop_rx) = watch::channel(false);
        let (done_tx, done) = watch::channel(false);
        {
            let mut active = self.0.lock().unwrap();
            if active.is_some() {
                return Err("A program is already running.".into());
            }
            *active = Some(Control { stop, done });
        }
        let result = run_process(executable, source, input, stop_rx, output).await;
        self.0.lock().unwrap().take();
        let _ = done_tx.send(true);
        result
    }

    pub async fn stop(&self) {
        let control = self
            .0
            .lock()
            .unwrap()
            .as_ref()
            .map(|run| (run.stop.clone(), run.done.clone()));
        if let Some((stop, mut done)) = control {
            let _ = stop.send(true);
            while !*done.borrow_and_update() {
                if done.changed().await.is_err() {
                    break;
                }
            }
        }
    }
}

async fn read_output(
    mut pipe: impl AsyncRead + Unpin,
    stream: &str,
    output: &(impl Fn(&str, Vec<u8>) + Send + Sync),
) -> Result<(), String> {
    let mut buffer = [0; 8192];
    loop {
        let count = pipe
            .read(&mut buffer)
            .await
            .map_err(|error| error.to_string())?;
        if count == 0 {
            return Ok(());
        }
        output(stream, buffer[..count].to_vec());
    }
}

async fn run_process(
    executable: PathBuf,
    source: PathBuf,
    input: String,
    mut stop: watch::Receiver<bool>,
    output: impl Fn(&str, Vec<u8>) + Send + Sync,
) -> Result<RunResult, String> {
    let mut command = Command::new(executable);
    command
        .arg(&source)
        .arg("-r")
        // Output renders stderr colors; auto would only see a pipe.
        .arg("-fdiagnostics-color=always")
        .current_dir(source.parent().ok_or("Invalid source path.")?)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .kill_on_drop(true);
    #[cfg(windows)]
    command.creation_flags(0x08000000); // CREATE_NO_WINDOW
    let mut child = command
        .spawn()
        .map_err(|error| format!("Cannot start compiler: {error}"))?;
    let mut stdin = child.stdin.take().unwrap();
    let stdout = child.stdout.take().unwrap();
    let stderr = child.stderr.take().unwrap();
    let write_input = async move {
        let result = stdin.write_all(input.as_bytes()).await;
        drop(stdin); // EOF must reach programs even when Input is empty.
        match result {
            Err(error) if error.kind() != std::io::ErrorKind::BrokenPipe => Err(error.to_string()),
            _ => Ok(()), // Programs may finish without consuming all input.
        }
    };
    let wait = async {
        tokio::select! {
            status = child.wait() => status.map(|status| RunResult { code: status.code(), stopped: false }),
            _ = stop.changed() => {
                child.kill().await.map_err(|error| error.to_string())?;
                child.wait().await.map(|status| RunResult { code: status.code(), stopped: true })
            }
        }.map_err(|error| error.to_string())
    };
    let (result, input_result, stdout_result, stderr_result) = tokio::join!(
        wait,
        write_input,
        read_output(stdout, "stdout", &output),
        read_output(stderr, "stderr", &output)
    );
    let result = result?;
    input_result?;
    stdout_result?;
    stderr_result?;
    Ok(result)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use tokio::time::{timeout, Duration};

    struct Source(PathBuf);
    impl Source {
        fn new(text: &str) -> Self {
            static NEXT: AtomicUsize = AtomicUsize::new(0);
            let directory = std::env::temp_dir().join(format!(
                "cpsc bundle test {} {}",
                std::process::id(),
                NEXT.fetch_add(1, Ordering::Relaxed)
            ));
            std::fs::create_dir_all(&directory).unwrap();
            let source = directory.join("program.cps");
            std::fs::write(&source, text).unwrap();
            Self(source)
        }
    }
    impl Drop for Source {
        fn drop(&mut self) {
            let _ = std::fs::remove_dir_all(self.0.parent().unwrap());
        }
    }
    fn compiler() -> PathBuf {
        PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(if cfg!(windows) {
            "../../build/cpsc.exe"
        } else {
            "../../build/cpsc"
        })
    }

    #[tokio::test]
    async fn prefilled_input_empty_eof_and_diagnostics() {
        let runner = Runner::default();
        for (program, input, expected, code) in [
            (
                "DECLARE N : INTEGER\nINPUT N\nOUTPUT N\nOUTPUT \"\\U0001F600\"\n",
                "42\n",
                "42\n😀\n",
                0,
            ),
            (
                "DECLARE S : STRING\nINPUT S\nOUTPUT \"done\"\n",
                "",
                "done\n",
                0,
            ),
            (
                "OUTPUT Missing\n",
                "",
                "\u{1b}[01;31m\u{1b}[Kerror: \u{1b}[m\u{1b}[K",
                1,
            ),
            (
                "OUTPUT 1 DIV 0\n",
                "",
                "Unhandled exception. Program terminated.",
                1,
            ),
        ] {
            let source = Source::new(program);
            let bytes = Mutex::new(Vec::new());
            let result = timeout(
                Duration::from_secs(30),
                runner.run(compiler(), source.0.clone(), input.into(), |_, chunk| {
                    bytes.lock().unwrap().extend(chunk)
                }),
            )
            .await
            .unwrap()
            .unwrap();
            assert_eq!(result.code, Some(code));
            let text = String::from_utf8(bytes.into_inner().unwrap())
                .unwrap()
                .replace("\r\n", "\n");
            if code == 0 {
                assert_eq!(text, expected);
            } else {
                assert!(text.contains(expected), "{text}");
            }
        }
    }

    #[tokio::test]
    async fn output_arrives_before_exit_and_stop_allows_restart() {
        let runner = Runner::default();
        let source = Source::new("OUTPUT \"ready\"\nWHILE TRUE\nENDWHILE\n");
        let (send, mut receive) = tokio::sync::mpsc::unbounded_channel();
        let running = runner.clone();
        let path = source.0.clone();
        let task = tokio::spawn(async move {
            running
                .run(compiler(), path, String::new(), |_, bytes| {
                    let _ = send.send(bytes);
                })
                .await
        });
        let mut output = Vec::new();
        timeout(Duration::from_secs(30), async {
            while !output.ends_with(b"ready\n") && !output.ends_with(b"ready\r\n") {
                output.extend(receive.recv().await.expect("output closed before ready"));
            }
        })
        .await
        .expect("stdout was buffered until exit");
        assert!(runner
            .run(compiler(), source.0.clone(), String::new(), |_, _| {})
            .await
            .is_err());
        timeout(Duration::from_secs(5), runner.stop())
            .await
            .expect("stop hung");
        assert!(task.await.unwrap().unwrap().stopped);
        let source = Source::new("OUTPUT \"again\"\n");
        assert_eq!(
            runner
                .run(compiler(), source.0.clone(), String::new(), |_, _| {})
                .await
                .unwrap()
                .code,
            Some(0)
        );
    }
}