papyrus 0.17.2

A rust repl and script runner
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
437
438
439
440
441
442
443
444
445
446
use super::*;
use crate::{
    cmds::{self, CommandResult},
    code::{self, Input, SourceCode, StmtGrp},
    compile,
};
use std::borrow::{Borrow, BorrowMut};
use std::ops::{Deref, DerefMut};
use std::sync::{Arc, Mutex};

/// > **These methods are available when the REPL is in the [`Evaluate`] state.**
impl<D> Repl<Evaluate, D> {
    /// Evaluates the read input, compiling and executing the code and printing all line prints until
    /// a result is found. This result gets passed back as a print ready repl.
    pub fn eval(self, app_data: &mut D) -> EvalResult<D> {
        use std::cell::Cell;
        use std::rc::Rc;

        let ptr = Rc::into_raw(Rc::new(app_data));

        // as I am playing around with pointers here, I am going to do assertions in the rebuilding
        // if from_raw is called more than once, it is memory unsafe, so count the calls and assert it is only 1
        let rebuilds: Rc<Cell<u32>> = Rc::new(Cell::new(0));

        let func = || {
            let b = Rc::clone(&rebuilds);

            let n = b.get();

            assert_eq!(n, 0, "unsafe memory operation, can only rebuild Rc once.");

            b.set(n + 1);

            let c = unsafe { Rc::from_raw(ptr) };

            Rc::try_unwrap(c)
                .map_err(|_| "there should only be one strong reference")
                .unwrap()
        };

        map_variants(self, func, func)
    }

    /// Same as `eval` but will evaluate on another thread, not blocking this one.
    ///
    /// An `Arc::clone` will be taken of `app_data`.
    pub fn eval_async(self, app_data: &Arc<Mutex<D>>) -> Evaluating<D>
    where
        D: 'static + Send,
    {
        let (tx, rx) = crossbeam_channel::bounded(1);

        let clone = Arc::clone(app_data);

        std::thread::spawn(move || {
            let eval = map_variants(
                self,
                || clone.lock().expect("failed getting lock of data"),
                || clone.lock().expect("failed getting lock of data"),
            );

            tx.send(eval).unwrap();
        });

        Evaluating { jh: rx }
    }

    /// Begin listening to line change events on the output.
    pub fn output_listen(&mut self) -> output::Receiver {
        self.state.output.listen()
    }

    /// Close the sender side of the output channel.
    pub fn close_channel(&mut self) {
        self.state.output.close()
    }

    /// The current output.
    ///
    /// The output contains colouring ANSI escape codes, the prompt, and all input.
    pub fn output(&self) -> &str {
        self.state.output.buffer()
    }
}

impl<D> Evaluating<D> {
    /// Evaluating has finished.
    pub fn completed(&self) -> bool {
        !self.jh.is_empty()
    }

    /// Waits for the evaluating to finish before return the result.
    /// If evaluating is `completed` this will return immediately.
    pub fn wait(self) -> EvalResult<D> {
        self.jh
            .recv()
            .expect("receiving eval result from async thread failed")
    }
}

fn map_variants<D, Fmut, Fbrw, Rmut, Rbrw>(
    repl: Repl<Evaluate, D>,
    obtain_mut_data: Fmut,
    obtain_brw_data: Fbrw,
) -> EvalResult<D>
where
    Fmut: FnOnce() -> Rmut,
    Rmut: DerefMut<Target = D>,
    Fbrw: FnOnce() -> Rbrw,
    Rbrw: Deref<Target = D>,
{
    let Repl {
        state,
        mut data,
        more,
        data_mrker,
    } = repl;

    let Evaluate { mut output, result } = state;

    let mut keep_mutating = false; // default to stop mutating phase
                                   // can't cancel before as handle program requires it for decisions

    // map variants into Result<HandleInputResult, EvalSignal>
    let mapped = match result {
        InputResult::Command(cmds) => {
            let r = data.handle_command(&cmds, &mut output, obtain_mut_data);
            keep_mutating = data.linking.mutable; // a command can alter the mutating state, needs to persist
            r.map(EvalOutput::Print)
        }
        InputResult::Program(input) => {
            Ok(data.handle_program(input, &mut output, obtain_mut_data, obtain_brw_data))
        }
        InputResult::InputError(err) => Ok(EvalOutput::Print(Cow::Owned(err))),
        InputResult::Eof => Err(Signal::Exit),
        _ => Ok(EvalOutput::Print(Cow::Borrowed(""))),
    };

    let (eval_output, sig) = match mapped {
        Ok(hir) => (hir, Signal::None),
        Err(sig) => (EvalOutput::Print(Cow::Borrowed("")), sig),
    };

    data.linking.mutable = keep_mutating; // always cancel a mutating block on evaluation??
                                          // the alternative would be to keep alive on compilation failures, might not for now though.
                                          // this would have to be individually handled in each match arm and it, rather let the user
                                          // have to reinstate mutability if they fuck up input.

    EvalResult {
        signal: sig,
        repl: Repl {
            state: Print {
                output,
                data: eval_output,
            },
            data,
            more,
            data_mrker,
        },
    }
}

impl<D> ReplData<D> {
    fn handle_command<F, R, W>(
        &mut self,
        cmds: &str,
        writer: &mut W,
        obtain_mut_app_data: F,
    ) -> Result<Cow<'static, str>, Signal>
    where
        F: FnOnce() -> R,
        R: DerefMut<Target = D>,
        W: io::Write,
    {
        use cmdtree::LineResult as lr;

        let tuple = match self.cmdtree.parse_line(cmds, true, writer) {
            lr::Exit => return Err(Signal::Exit),
            lr::Cancel => {
                self.linking.mutable = false; // reset the mutating on cancel
                self.editing = None; // reset the editing on cancel
                Cow::Borrowed("cancelled input and returned to root")
            }
            lr::Action(res) => match res {
                CommandResult::BeginMutBlock => {
                    self.linking.mutable = true;
                    Cow::Borrowed("beginning mut block")
                }
                CommandResult::EditAlter(ei) => Cow::Borrowed(cmds::edit_alter(self, ei)),
                CommandResult::EditReplace(ei, val) => {
                    let r = Cow::Borrowed(cmds::edit_alter(self, ei));

                    if r.is_empty() {
                        return Err(Signal::ReEvaluate(val));
                    } else {
                        r
                    }
                }
                CommandResult::SwitchModule(path) => {
                    Cow::Borrowed(crate::cmds::switch_module(self, &path))
                }

                CommandResult::ActionOnReplData(action) => Cow::Owned(action(self, writer)),
                CommandResult::ActionOnAppData(action) => {
                    let mut r = obtain_mut_app_data();
                    let app_data: &mut D = r.borrow_mut();
                    let s = action(app_data, self, writer);
                    Cow::Owned(s)
                }
                CommandResult::Empty => Cow::Borrowed(""),
            },
            _ => Cow::Borrowed(""),
        };

        Ok(tuple)
    }

    fn handle_program<Fmut, Fbrw, Rmut, Rbrw>(
        &mut self,
        mut input: Input,
        writer: &mut Output<output::Write>,
        obtain_mut_data: Fmut,
        obtain_brw_data: Fbrw,
    ) -> EvalOutput
    where
        Fmut: FnOnce() -> Rmut,
        Rmut: DerefMut<Target = D>,
        Fbrw: FnOnce() -> Rbrw,
        Rbrw: Deref<Target = D>,
    {
        let (nitems, ncrates) = (input.items.len(), input.crates.len());

        let has_stmts = !input.stmts.is_empty();

        let (lstmts, litem, lcrates) = {
            let src = self.current_src();
            (src.stmts.len(), src.items.len(), src.crates.len())
        };

        let mut undo = true;

        let (stmt_idx, item_idx, crate_idx) = if let Some(ei) = self.editing.take() {
            let src = self.get_current_file_mut(); // remove at the index
                                                   // then insert, so
                                                   // acts like replace

            undo = false;

            match ei.editing {
                // we clear the edits if the indices fall outside the bounds
                Editing::Stmt => {
                    if ei.index >= lstmts {
                        input.stmts.clear();
                    } else {
                        src.stmts.remove(ei.index);
                    }

                    (ei.index, litem, lcrates)
                }
                Editing::Item => {
                    if ei.index >= litem {
                        input.items.clear();
                    } else {
                        src.items.remove(ei.index);
                    }

                    (lstmts, ei.index, lcrates)
                }
                Editing::Crate => {
                    if ei.index >= lcrates {
                        input.crates.clear();
                    } else {
                        src.crates.remove(ei.index);
                    }

                    (lstmts, litem, ei.index)
                }
            }
        } else {
            (lstmts, litem, lcrates)
        };

        self.insert_input(input, stmt_idx, item_idx, crate_idx);

        let maybe_pop_input = |repl_data: &mut ReplData<D>| {
            if undo {
                let src = repl_data.get_current_file_mut();

                if has_stmts {
                    src.stmts.remove(stmt_idx);
                }

                for _ in 0..nitems {
                    src.items.remove(item_idx);
                }

                for _ in 0..ncrates {
                    src.crates.remove(crate_idx);
                }
            }
        };

        // build directory
        let res = compile::build_compile_dir(
            &self.compilation_dir,
            &self.mods_map,
            &self.linking,
            &self.static_files,
        );
        if let Err(e) = res {
            maybe_pop_input(self); // failed so don't save
            return EvalOutput::Print(Cow::Owned(format!(
                "failed to build compile directory: {}",
                e
            )));
        }

        // compile
        let lib_file = compile::compile(&self.compilation_dir, &self.linking, |line| {
            writer.erase_last_line();
            writer.write_str(line);
        });

        writer.erase_last_line();

        let lib_file = match lib_file {
            Ok(f) => f,
            Err(e) => {
                maybe_pop_input(self); // failed so don't save
                return EvalOutput::Print(Cow::Owned(format!("{}", e)));
            }
        };

        if has_stmts {
            // execute
            let exec_res = {
                // once compilation succeeds and we are going to evaluate it (which libloads) we
                // first rename the files to avoid locking for the next compilation that might
                // happen
                let lib_file = compile::unshackle_library_file(lib_file);

                let mut fn_name = String::new();
                code::eval_fn_name(&code::into_mod_path_vec(self.current_mod()), &mut fn_name);

                if self.linking.mutable {
                    let mut r = obtain_mut_data();
                    let app_data: &mut D = r.borrow_mut();
                    compile::exec(&lib_file, &fn_name, app_data)
                } else {
                    let r = obtain_brw_data();
                    let app_data: &D = r.borrow();
                    compile::exec(&lib_file, &fn_name, app_data)
                }
            };
            match exec_res {
                Ok((kserd, lib)) => {
                    // store vec, maybe
                    add_to_limit_vec(
                        &mut self.loadedlibs,
                        Box::new(lib),
                        self.loaded_libs_size_limit,
                    );

                    if self.linking.mutable {
                        maybe_pop_input(self); // don't save mutating inputs
                        EvalOutput::Print(Cow::Owned(format!("finished mutating block: {}", kserd)))
                    // don't print as `out#`
                    } else {
                        EvalOutput::Data(kserd)
                    }
                }
                Err(e) => {
                    maybe_pop_input(self); // failed so don't save
                    EvalOutput::Print(Cow::Borrowed(e))
                }
            }
        } else {
            // this will keep inputs, might not be preferrable to do so in mutating state?
            EvalOutput::Print(Cow::Borrowed("")) // do not execute if no extra statements have been added
        }
    }

    fn insert_input(&mut self, input: Input, stmt_idx: usize, item_idx: usize, crate_idx: usize) {
        let Input {
            items,
            crates,
            stmts,
        } = input;

        let src = self.get_current_file_mut();

        if !stmts.is_empty() {
            src.stmts.insert(stmt_idx, StmtGrp(stmts));
        }

        for item in items.into_iter().rev() {
            src.items.insert(item_idx, item);
        }

        for cr in crates.into_iter().rev() {
            src.crates.insert(crate_idx, cr);
        }
    }

    fn get_current_file_mut(&mut self) -> &mut SourceCode {
        let cmod = &self.current_mod;
        self.mods_map
            .get_mut(cmod)
            .unwrap_or_else(|| panic!("file map does not have key: {}", cmod.display()))
    }
}

fn add_to_limit_vec<T>(store: &mut VecDeque<T>, item: T, limit: usize) {
    match (limit, store.len()) {
        (0, 0) => (),             // do nothing, lib will drop after this
        (0, _x) => store.clear(), // zero limit and store has something, clear them
        (limit, _) => {
            let limit = limit - 1; // limit will be gt zero
            store.truncate(limit); // truncate to limit - 1 length, as we will add new lib in
            store.push_front(item); // we keep the newest versions at front of queue
        }
    }
}

#[test]
fn vec_limited_testing() {
    let mut vec: VecDeque<i32> = VecDeque::new();
    vec.push_front(-3);
    vec.push_front(-2);

    add_to_limit_vec(&mut vec, 0, 3);
    assert_eq!(&vec, &[0, -2, -3]);

    add_to_limit_vec(&mut vec, 3, 3);
    assert_eq!(&vec, &[3, 0, -2]);

    add_to_limit_vec(&mut vec, -1, 0);
    assert!(vec.is_empty());
    add_to_limit_vec(&mut vec, -1, 0);
    assert!(vec.is_empty());

    add_to_limit_vec(&mut vec, 0, 1);
    add_to_limit_vec(&mut vec, 1, 1);
    add_to_limit_vec(&mut vec, 2, 1);
    assert_eq!(&vec, &[2]);
}