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
//! [`Runtime`], parsers ([`NodejsParser`], [`EsprimaParser`]) \[std-only!\]

mod esprima;
mod nodejs;
//mod qesprima;

use core::str::Utf8Error;
use std::io;

use crate::function::HostFn;
use crate::{
    error,
    Exception,
    Heap,
    JSString,
    JSValue,
    Program,
    JSON,
};
use crate::{
    prelude::*,
    CallContext,
    Interpreted,
    JSResult,
};

pub use self::esprima::EsprimaParser;
pub use self::nodejs::NodejsParser;

#[derive(Debug)]
pub enum EvalError {
    /// Javascript exception
    Exception(Exception),

    /// Serde error
    Serialization(serde_json::Error),

    /// I/O error
    Io(io::Error),
}

impl fmt::Display for EvalError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            EvalError::Exception(exc) => match exc {
                Exception::SyntaxTreeError(error::ParseError::InvalidJSON { err }) => {
                    writeln!(f, "Syntax error:{}", err.as_str())
                }
                _ => write!(f, "Error: {:?}", exc),
            },
            EvalError::Serialization(e) => writeln!(f, "Serialization error:\n{}", e),
            EvalError::Io(e) => writeln!(f, "{}", e),
        }
    }
}

impl From<Exception> for EvalError {
    fn from(exc: Exception) -> Self {
        EvalError::Exception(exc)
    }
}

impl From<serde_json::Error> for EvalError {
    fn from(err: serde_json::Error) -> Self {
        EvalError::Serialization(err)
    }
}

impl From<Utf8Error> for EvalError {
    fn from(err: Utf8Error) -> Self {
        let err = io::Error::new(io::ErrorKind::InvalidData, err);
        EvalError::Io(err)
    }
}

impl From<io::Error> for EvalError {
    fn from(err: io::Error) -> Self {
        EvalError::Io(err)
    }
}

impl From<std::string::FromUtf8Error> for EvalError {
    fn from(e: std::string::FromUtf8Error) -> EvalError {
        EvalError::Io(io::Error::new(io::ErrorKind::InvalidData, e))
    }
}

impl From<EvalError> for io::Error {
    fn from(err: EvalError) -> io::Error {
        match err {
            EvalError::Io(err) => err,
            _ => io::Error::new(io::ErrorKind::Other, err.to_string()),
        }
    }
}

impl From<EvalError> for Exception {
    fn from(err: EvalError) -> Exception {
        match err {
            EvalError::Exception(exc) => exc,
            EvalError::Serialization(e) => Exception::UserThrown(JSValue::from(e.to_string())),
            EvalError::Io(e) => Exception::UserThrown(JSValue::from(e.to_string())),
        }
    }
}

pub type EvalResult<T> = Result<T, EvalError>;

/// Describes anything that can parse JS code.
pub trait Parser: fmt::Debug {
    /// Called by [`Runtime::load`] to initialize the parser.
    ///
    /// If the parser does not need to touch [`Heap`], do nothing.
    #[allow(unused_variables)]
    fn load(&mut self, heap: &mut Heap) -> EvalResult<()> {
        Ok(())
    }

    /// Parses an input into a `Program` (potentially using the `heap`)
    fn parse(&self, input: &str, heap: &mut Heap) -> EvalResult<Program>;

    /// Get the native callback for `eval()` in JavaScript provided by this parser
    fn eval_func(&self) -> HostFn;
}

/// The sljs JavaScript runtime.
///
/// It should be given a [`Parser`] implementation, e.g. [`NodejsParser`] or
/// [`EsprimaParser`]:
///
/// ```
/// use sljs::JSON;
/// use sljs::runtime::{Runtime, NodejsParser};
///
/// let parser = NodejsParser::new();
/// let mut sljs = Runtime::load(parser)
///     .expect("Runtime::load");
/// ```
///
/// The [`Runtime`] evaluates JavaScript source code and creates/stores [`crate::JSObject`]s.
///
/// ```
/// # use sljs::JSON;
/// # use sljs::runtime::{Runtime, NodejsParser};
/// # let mut sljs = Runtime::load(NodejsParser::new()).expect("Runtime::load");
///
/// sljs.evaluate("var x = 12")
///     .expect("eval: var x");
/// let x = sljs.evaluate("x")
///     .expect("eval: x");
///
/// assert_eq!(sljs.json_from(x), JSON::from(12.0));
/// ```
///
pub struct Runtime {
    pub heap: Heap,
    parser: Box<dyn Parser>,
}

impl Runtime {
    /// Creates a sljs runtime.
    pub fn load(mut parser: Box<dyn Parser>) -> EvalResult<Self> {
        let mut heap = Heap::new();
        parser.load(&mut heap)?;

        let eval_ref = heap.alloc_func(parser.eval_func());
        heap.get_mut(Heap::GLOBAL)
            .set_hidden(JSString::from("eval"), eval_ref)?;

        Ok(Runtime { heap, parser })
    }

    /// Exposes the configured parser.
    pub fn parse(&mut self, input: &str) -> EvalResult<Program> {
        self.parser.parse(input, &mut self.heap)
    }

    /// Takes an `input` and evaluates it.
    pub fn evaluate(&mut self, input: &str) -> EvalResult<JSValue> {
        let program = self.parse(input)?;
        self.heap.evaluate(&program).map_err(EvalError::Exception)
    }

    /// Turn a [`JSValue`] into [`JSON`]
    pub fn json_from(&mut self, value: JSValue) -> JSON {
        value.to_json(&self.heap).expect("JSValue.to_json()")
    }

    /// Turn a [`JSValue`] into a human-readable string.
    pub fn string_from(&mut self, value: JSValue) -> JSString {
        value
            .to_string(&mut self.heap)
            .expect("JSValue.to_string()")
    }

    pub fn dbg(&mut self, refstr: &str) {
        if let Ok(refnum) = usize::from_str(refstr) {
            match self.heap.get_index(refnum) {
                Some(object) => {
                    dbg!(object);
                }
                None => {
                    eprintln!("No object at #{}", refnum);
                }
            };
        } else {
            // TODO: evaluate an expression to a reference
            eprintln!("Command error: expected a number");
        }
    }
}

/// Not-really-a-[`Parser`] implementation that just deserializes a JSON ESTree.
#[derive(Debug)]
pub struct JSONParser;

impl JSONParser {
    fn json_eval(_: CallContext, _: &mut Heap) -> JSResult<Interpreted> {
        unimplemented!("JSONParser does not support eval()")
    }
}

impl Parser for JSONParser {
    fn parse(&self, input: &str, _heap: &mut Heap) -> EvalResult<Program> {
        // It's not much, but it's honest work.
        let estree: JSON = serde_json::from_str(input)?;
        let program = Program::parse_from(&estree).map_err(Exception::from)?;
        Ok(program)
    }

    fn eval_func(&self) -> HostFn {
        Self::json_eval
    }
}