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
use std::{fmt, ops::Deref};

use crate::{convert::Coerced, qjs, Ctx, Error, FromJs, IntoJs, Object, Result, Value};

/// A javascript instance of Error
///
/// Will turn into a error when converted to javascript but won't autmatically be thrown.
#[repr(transparent)]
pub struct Exception<'js>(Object<'js>);

impl fmt::Debug for Exception<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Exception")
            .field("object", &self.0)
            .field("message", &self.message())
            .field("file", &self.file())
            .field("line", &self.line())
            .field("stack", &self.stack())
            .finish()
    }
}

impl<'js> Exception<'js> {
    /// Turns the exception into the underlying object.
    pub fn into_object(self) -> Object<'js> {
        self.0
    }

    /// Returns a reference to the underlying object.
    pub fn as_object(&self) -> &Object<'js> {
        &self.0
    }

    /// Turns the exception into a generic javascript value.
    pub fn into_value(self) -> Value<'js> {
        self.0.into_value()
    }

    /// Creates an exception from an object if it is an instance of error.
    pub fn from_object(obj: Object<'js>) -> Option<Self> {
        if obj.is_error() {
            Some(Self(obj))
        } else {
            None
        }
    }

    /// Creates a new exception with a give message.
    pub fn from_message(ctx: Ctx<'js>, message: &str) -> Result<Self> {
        let obj = unsafe {
            let value = ctx.handle_exception(qjs::JS_NewError(ctx.as_ptr()))?;
            Value::from_js_value(ctx, value)
                .into_object()
                .expect("`JS_NewError` did not return an object")
        };
        obj.set("message", message)?;
        Ok(Exception(obj))
    }

    /// Creates a new exception with a give message, file name and line number.
    pub fn from_message_location(
        ctx: Ctx<'js>,
        message: &str,
        file: &str,
        line: i32,
    ) -> Result<Self> {
        let obj = unsafe {
            let value = ctx.handle_exception(qjs::JS_NewError(ctx.as_ptr()))?;
            Value::from_js_value(ctx, value)
                .into_object()
                .expect("`JS_NewError` did not return an object")
        };
        obj.set("message", message)?;
        obj.set("fileName", file)?;
        obj.set("lineNumber", line)?;
        Ok(Exception(obj))
    }

    /// Returns the message of the error.
    ///
    /// Same as retrieving `error.message` in javascript.
    pub fn message(&self) -> Option<String> {
        self.get::<_, Option<Coerced<String>>>("message")
            .ok()
            .and_then(|x| x)
            .map(|x| x.0)
    }

    /// Returns the file name from with the error originated..
    ///
    /// Same as retrieving `error.fileName` in javascript.
    pub fn file(&self) -> Option<String> {
        self.get::<_, Option<Coerced<String>>>("fileName")
            .ok()
            .and_then(|x| x)
            .map(|x| x.0)
    }

    /// Returns the file line from with the error originated..
    ///
    /// Same as retrieving `error.lineNumber` in javascript.
    pub fn line(&self) -> Option<i32> {
        self.get::<_, Option<Coerced<i32>>>("lineNumber")
            .ok()
            .and_then(|x| x)
            .map(|x| x.0)
    }

    /// Returns the error stack.
    ///
    /// Same as retrieving `error.stack` in javascript.
    pub fn stack(&self) -> Option<String> {
        self.get::<_, Option<Coerced<String>>>("stack")
            .ok()
            .and_then(|x| x)
            .map(|x| x.0)
    }

    /// Throws a new generic error.
    ///
    /// Equivalent to:
    /// ```rust
    /// # use rquickjs::{Runtime,Context,Exception};
    /// # let rt = Runtime::new().unwrap();
    /// # let ctx = Context::full(&rt).unwrap();
    /// # ctx.with(|ctx|{
    /// # let _ = {
    /// # let message = "";
    /// let (Ok(e) | Err(e)) = Exception::from_message(ctx, message).map(|x| x.throw());
    /// e
    /// # };
    /// # })
    /// ```
    pub fn throw_message(ctx: Ctx<'js>, message: &str) -> Error {
        let (Ok(e) | Err(e)) = Self::from_message(ctx, message).map(|x| x.throw());
        e
    }
    /// Throws a new generic error with a file name and line number.
    pub fn throw_message_location(ctx: Ctx<'js>, message: &str, file: &str, line: i32) -> Error {
        let (Ok(e) | Err(e)) =
            Self::from_message_location(ctx, message, file, line).map(|x| x.throw());
        e
    }

    /// Throws a new syntax error.
    pub fn throw_syntax(ctx: Ctx<'js>, message: &str) -> Error {
        // generate C string inline.
        let mut buffer = std::mem::MaybeUninit::<[u8; 256]>::uninit();
        let str_len = message.as_bytes().len().min(255);
        unsafe {
            std::ptr::copy_nonoverlapping(message.as_ptr(), buffer.as_mut_ptr().cast(), str_len);
            buffer.as_mut_ptr().cast::<u8>().add(str_len).write(b'\0');
            let res = qjs::JS_ThrowSyntaxError(ctx.as_ptr(), buffer.as_ptr().cast());
            debug_assert_eq!(qjs::JS_VALUE_GET_NORM_TAG(res), qjs::JS_TAG_EXCEPTION);
        }
        Error::Exception
    }

    /// Throws a new type error.
    pub fn throw_type(ctx: Ctx<'js>, message: &str) -> Error {
        // generate C string inline.
        let mut buffer = std::mem::MaybeUninit::<[u8; 256]>::uninit();
        let str_len = message.as_bytes().len().min(255);
        unsafe {
            std::ptr::copy_nonoverlapping(
                message.as_ptr(),
                buffer.as_mut_ptr().cast::<u8>(),
                str_len,
            );
            buffer.as_mut_ptr().cast::<u8>().add(str_len).write(b'\0');
            let res = qjs::JS_ThrowTypeError(ctx.as_ptr(), buffer.as_ptr().cast());
            debug_assert_eq!(qjs::JS_VALUE_GET_NORM_TAG(res), qjs::JS_TAG_EXCEPTION);
        }
        Error::Exception
    }

    /// Throws a new reference error.
    pub fn throw_reference(ctx: Ctx<'js>, message: &str) -> Error {
        // generate C string inline.
        let mut buffer = std::mem::MaybeUninit::<[u8; 256]>::uninit();
        let str_len = message.as_bytes().len().min(255);
        unsafe {
            std::ptr::copy_nonoverlapping(
                message.as_ptr(),
                buffer.as_mut_ptr().cast::<u8>(),
                str_len,
            );
            buffer.as_mut_ptr().cast::<u8>().add(str_len).write(b'\0');
            let res = qjs::JS_ThrowReferenceError(ctx.as_ptr(), buffer.as_ptr().cast());
            debug_assert_eq!(qjs::JS_VALUE_GET_NORM_TAG(res), qjs::JS_TAG_EXCEPTION);
        }
        Error::Exception
    }

    /// Throws a new range error.
    pub fn throw_range(ctx: Ctx<'js>, message: &str) -> Error {
        // generate C string inline.
        let mut buffer = std::mem::MaybeUninit::<[u8; 256]>::uninit();
        let str_len = message.as_bytes().len().min(255);
        unsafe {
            std::ptr::copy_nonoverlapping(
                message.as_ptr(),
                buffer.as_mut_ptr().cast::<u8>(),
                str_len,
            );
            buffer.as_mut_ptr().cast::<u8>().add(str_len).write(b'\0');
            let res = qjs::JS_ThrowRangeError(ctx.as_ptr(), buffer.as_ptr().cast());
            debug_assert_eq!(qjs::JS_VALUE_GET_NORM_TAG(res), qjs::JS_TAG_EXCEPTION);
        }
        Error::Exception
    }

    /// Throws a new internal error.
    pub fn throw_internal(ctx: Ctx<'js>, message: &str) -> Error {
        // generate C string inline.
        let mut buffer = std::mem::MaybeUninit::<[u8; 256]>::uninit();
        let str_len = message.as_bytes().len().min(255);
        unsafe {
            std::ptr::copy_nonoverlapping(
                message.as_ptr(),
                buffer.as_mut_ptr().cast::<u8>(),
                str_len,
            );
            buffer.as_mut_ptr().cast::<u8>().add(str_len).write(b'\0');
            let res = qjs::JS_ThrowInternalError(ctx.as_ptr(), buffer.as_ptr().cast());
            debug_assert_eq!(qjs::JS_VALUE_GET_NORM_TAG(res), qjs::JS_TAG_EXCEPTION);
        }
        Error::Exception
    }

    /// Sets the exception as the current error an returns `Error::Exception`
    pub fn throw(self) -> Error {
        self.0.ctx.throw(self.0.into_value())
    }
}

impl fmt::Display for Exception<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        "Exception generated by quickjs: ".fmt(f)?;
        if let Some(file) = self.file() {
            '['.fmt(f)?;
            file.fmt(f)?;
            ']'.fmt(f)?;
        }
        if let Some(line) = self.line() {
            ':'.fmt(f)?;
            line.fmt(f)?;
        }
        if let Some(message) = self.message() {
            ' '.fmt(f)?;
            message.fmt(f)?;
        }
        if let Some(stack) = self.stack() {
            '\n'.fmt(f)?;
            stack.fmt(f)?;
        }
        Ok(())
    }
}

impl<'js> Deref for Exception<'js> {
    type Target = Object<'js>;

    fn deref(&self) -> &Self::Target {
        self.as_object()
    }
}

impl<'js> FromJs<'js> for Exception<'js> {
    fn from_js(_ctx: crate::Ctx<'js>, value: Value<'js>) -> Result<Self> {
        if let Some(obj) = value.as_object() {
            if obj.is_error() {
                return Ok(Exception(obj.clone()));
            } else {
                return Err(Error::FromJs {
                    from: value.type_name(),
                    to: "Exception",
                    message: Some("object was not an instance of error".to_string()),
                });
            }
        }
        return Err(Error::FromJs {
            from: value.type_name(),
            to: "Exception",
            message: Some("value was not a type".to_string()),
        });
    }
}

impl<'js> IntoJs<'js> for Exception<'js> {
    fn into_js(self, _ctx: crate::Ctx<'js>) -> Result<Value<'js>> {
        Ok(self.0.into_value())
    }
}