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
use super::*;
use crate::{
    exec::Executable,
    syntax::ast::{
        node::{Call, Identifier, New},
        Const,
    },
};

impl Interpreter {
    /// Constructs a `RangeError` with the specified message.
    pub fn construct_range_error<M>(&mut self, message: M) -> Value
    where
        M: Into<String>,
    {
        // Runs a `new RangeError(message)`.
        New::from(Call::new(
            Identifier::from("RangeError"),
            vec![Const::from(message.into()).into()],
        ))
        .run(self)
        .expect_err("RangeError should always throw")
    }

    /// Throws a `RangeError` with the specified message.
    pub fn throw_range_error<M>(&mut self, message: M) -> ResultValue
    where
        M: Into<String>,
    {
        Err(self.construct_range_error(message))
    }

    /// Constructs a `TypeError` with the specified message.
    pub fn construct_type_error<M>(&mut self, message: M) -> Value
    where
        M: Into<String>,
    {
        // Runs a `new TypeError(message)`.
        New::from(Call::new(
            Identifier::from("TypeError"),
            vec![Const::from(message.into()).into()],
        ))
        .run(self)
        .expect_err("TypeError should always throw")
    }

    /// Throws a `TypeError` with the specified message.
    pub fn throw_type_error<M>(&mut self, message: M) -> ResultValue
    where
        M: Into<String>,
    {
        Err(self.construct_type_error(message))
    }

    /// Constructs a `ReferenceError` with the specified message.
    pub fn construct_reference_error<M>(&mut self, message: M) -> Value
    where
        M: Into<String>,
    {
        New::from(Call::new(
            Identifier::from("ReferenceError"),
            vec![Const::from(message.into() + " is not defined").into()],
        ))
        .run(self)
        .expect_err("ReferenceError should always throw")
    }

    /// Throws a `ReferenceError` with the specified message.
    pub fn throw_reference_error<M>(&mut self, message: M) -> ResultValue
    where
        M: Into<String>,
    {
        Err(self.construct_reference_error(message))
    }
}