rune/modules/
capture_io.rs

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
//! I/O module capable of capturing what's been written to a buffer.
//!
//! # Examples
//!
//! ```no_run
//! use rune::Context;
//! use rune::modules::capture_io::{self, CaptureIo};
//!
//! let io = CaptureIo::new();
//!
//! let mut context = rune::Context::with_config(false)?;
//! context.install(capture_io::module(&io)?)?;
//! # Ok::<_, rune::ContextError>(())
//! ```

use core::mem::take;

use ::rust_alloc::sync::Arc;

use parking_lot::Mutex;

use crate as rune;
use crate::alloc::fmt::TryWrite;
use crate::alloc::string::FromUtf8Error;
use crate::alloc::{String, Vec};
use crate::runtime::{InstAddress, Memory, Output, VmError, VmResult};
use crate::{ContextError, Module, Value};

/// I/O module capable of capturing what's been written to a buffer.
#[rune::module(::std::io)]
pub fn module(io: &CaptureIo) -> Result<Module, ContextError> {
    let mut module = Module::from_meta(self::module_meta)?;

    let o = io.clone();

    module
        .function("print", move |m: &str| {
            match write!(o.inner.lock(), "{}", m) {
                Ok(()) => VmResult::Ok(()),
                Err(error) => VmResult::panic(error),
            }
        })
        .build()?;

    let o = io.clone();

    module
        .function("println", move |m: &str| {
            match writeln!(o.inner.lock(), "{}", m) {
                Ok(()) => VmResult::Ok(()),
                Err(error) => VmResult::panic(error),
            }
        })
        .build()?;

    let o = io.clone();

    module
        .raw_function("dbg", move |stack, addr, args, output| {
            let mut o = o.inner.lock();
            dbg_impl(&mut o, stack, addr, args, output)
        })
        .build()?;

    Ok(module)
}

/// Type which captures output from rune scripts.
#[derive(Default, Clone)]
pub struct CaptureIo {
    inner: Arc<Mutex<Vec<u8>>>,
}

impl CaptureIo {
    /// Construct a new capture.
    pub fn new() -> Self {
        Self::default()
    }

    /// Test if capture is empty.
    pub fn is_empty(&self) -> bool {
        self.inner.lock().is_empty()
    }

    /// Drain all captured I/O that has been written to output functions.
    pub fn drain(&self) -> Vec<u8> {
        let mut o = self.inner.lock();
        take(&mut *o)
    }

    cfg_std! {
        /// Drain all captured I/O that has been written to output functions into
        /// the given [Write].
        ///
        /// [Write]: std::io::Write
        pub fn drain_into<O>(&self, mut out: O) -> std::io::Result<()>
        where
            O: std::io::Write,
        {
            let mut o = self.inner.lock();
            out.write_all(o.as_slice())?;
            o.clear();
            Ok(())
        }
    }

    /// Drain all captured I/O that has been written to output functions and try
    /// to decode as UTF-8.
    pub fn drain_utf8(&self) -> Result<String, FromUtf8Error> {
        String::from_utf8(self.drain())
    }
}

fn dbg_impl(
    o: &mut Vec<u8>,
    stack: &mut dyn Memory,
    addr: InstAddress,
    args: usize,
    out: Output,
) -> VmResult<()> {
    for value in vm_try!(stack.slice_at(addr, args)) {
        vm_try!(writeln!(o, "{:?}", value).map_err(VmError::panic));
    }

    vm_try!(out.store(stack, Value::unit));
    VmResult::Ok(())
}