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
//! utils for getting and reporting exceptions

use crate::jsutils::JsError;
use crate::quickjs_utils::{objects, primitives};
use crate::quickjsrealmadapter::QuickJsRealmAdapter;
use crate::quickjsvalueadapter::{QuickJsValueAdapter, TAG_EXCEPTION};
use libquickjs_sys as q;

/// Get the last exception from the runtime, and if present, convert it to an JsError.
/// # Safety
/// When passing a context pointer please make sure the corresponding QuickJsContext is still valid
pub unsafe fn get_exception(context: *mut q::JSContext) -> Option<JsError> {
    log::trace!("get_exception");
    let exception_val = q::JS_GetException(context);
    log::trace!("get_exception / 2");
    let exception_ref =
        QuickJsValueAdapter::new(context, exception_val, false, true, "errors::get_exception");

    if exception_ref.is_null() {
        None
    } else {
        let err = if exception_ref.is_exception() {
            JsError::new_str("Could not get exception from runtime")
        } else if exception_ref.is_object() {
            error_to_js_error(context, &exception_ref)
        } else {
            JsError::new_str("no clue what happened")
        };
        Some(err)
    }
}

/// convert an instance of Error to JsError
/// # Safety
/// When passing a context pointer please make sure the corresponding QuickJsContext is still valid
pub unsafe fn error_to_js_error(
    context: *mut q::JSContext,
    exception_ref: &QuickJsValueAdapter,
) -> JsError {
    log::trace!("error_to_js_error");
    let name_ref = objects::get_property(context, exception_ref, "name")
        .ok()
        .unwrap();
    let name_string = primitives::to_string(context, &name_ref).ok().unwrap();
    let message_ref = objects::get_property(context, exception_ref, "message")
        .ok()
        .unwrap();
    let message_string = primitives::to_string(context, &message_ref).ok().unwrap();
    let stack_ref = objects::get_property(context, exception_ref, "stack")
        .ok()
        .unwrap();
    let mut stack_string = "".to_string();

    let stack2_ref = objects::get_property(context, exception_ref, "stack2")
        .ok()
        .unwrap();
    if stack2_ref.is_string() {
        stack_string.push_str(
            primitives::to_string(context, &stack2_ref)
                .ok()
                .unwrap()
                .as_str(),
        );
    }

    if stack_ref.is_string() {
        let stack_str = primitives::to_string(context, &stack_ref).ok().unwrap();
        #[cfg(feature = "typescript")]
        let stack_str = crate::typescript::unmap_stack_trace(stack_str.as_str());

        stack_string.push_str(stack_str.as_str());
    }

    JsError::new(name_string, message_string, stack_string)
}

/// Create a new Error object
/// # Safety
/// When passing a context pointer please make sure the corresponding QuickJsContext is still valid
pub unsafe fn new_error(
    context: *mut q::JSContext,
    name: &str,
    message: &str,
    stack: &str,
) -> Result<QuickJsValueAdapter, JsError> {
    let obj = q::JS_NewError(context);
    let obj_ref = QuickJsValueAdapter::new(
        context,
        obj,
        false,
        true,
        format!("new_error {name}").as_str(),
    );
    objects::set_property(
        context,
        &obj_ref,
        "message",
        &primitives::from_string(context, message)?,
    )?;
    objects::set_property(
        context,
        &obj_ref,
        "name",
        &primitives::from_string(context, name)?,
    )?;
    objects::set_property(
        context,
        &obj_ref,
        "stack2",
        &primitives::from_string(context, stack)?,
    )?;
    Ok(obj_ref)
}

/// See if a JSValueRef is an Error object
pub fn is_error_q(q_ctx: &QuickJsRealmAdapter, obj_ref: &QuickJsValueAdapter) -> bool {
    unsafe { is_error(q_ctx.context, obj_ref) }
}

/// See if a JSValueRef is an Error object
/// # Safety
/// When passing a context pointer please make sure the corresponding QuickJsContext is still valid
pub unsafe fn is_error(context: *mut q::JSContext, obj_ref: &QuickJsValueAdapter) -> bool {
    if obj_ref.is_object() {
        let res = q::JS_IsError(context, *obj_ref.borrow_value());
        res != 0
    } else {
        false
    }
}

pub fn get_stack(realm: &QuickJsRealmAdapter) -> Result<QuickJsValueAdapter, JsError> {
    let e = realm.invoke_function_by_name(&[], "Error", &[])?;
    realm.get_object_property(&e, "stack")
}

/// Throw an error and get an Exception JSValue to return from native methods
/// # Safety
/// When passing a context pointer please make sure the corresponding QuickJsContext is still valid
pub unsafe fn throw(context: *mut q::JSContext, error: QuickJsValueAdapter) -> q::JSValue {
    assert!(is_error(context, &error));
    q::JS_Throw(context, error.clone_value_incr_rc());
    q::JSValue {
        u: q::JSValueUnion { int32: 0 },
        tag: TAG_EXCEPTION,
    }
}

#[cfg(test)]
pub mod tests {
    use crate::facades::tests::init_test_rt;
    use crate::jsutils::{JsError, Script};
    use crate::quickjs_utils::functions;
    use crate::values::{JsValueConvertable, JsValueFacade};
    use std::thread;
    use std::time::Duration;

    #[test]
    fn test_ex_nat() {
        // check if stacktrace is preserved when invoking native methods

        let rt = init_test_rt();
        let res = rt.eval_sync(
            None,
            Script::new(
                "ex.js",
                "console.log('foo');\nconsole.log('bar');let a = __c_v__ * 7;",
            ),
        );
        let ex = res.expect_err("script should have failed;");

        #[cfg(feature = "bellard")]
        assert_eq!(ex.get_message(), "'__c_v__' is not defined");
        #[cfg(feature = "quickjs-ng")]
        assert_eq!(ex.get_message(), "__c_v__ is not defined");
    }

    #[test]
    fn test_ex0() {
        // check if stacktrace is preserved when invoking native methods

        let rt = init_test_rt();
        let res = rt.eval_sync(
            None,
            Script::new(
                "ex.js",
                "console.log('foo');\nconsole.log('bar');let a = __c_v__ * 7;",
            ),
        );
        let ex = res.expect_err("script should have failed;");

        #[cfg(feature = "bellard")]
        assert_eq!(ex.get_message(), "'__c_v__' is not defined");
        #[cfg(feature = "quickjs-ng")]
        assert_eq!(ex.get_message(), "__c_v__ is not defined");
    }

    #[test]
    fn test_ex1() {
        // check if stacktrace is preserved when invoking native methods

        let rt = init_test_rt();
        rt.set_function(&[], "test_consume", move |_realm, args| {
            // args[0] is a function i'll want to call
            let func_jsvf = &args[0];
            match func_jsvf {
                JsValueFacade::JsFunction { cached_function } => {
                    let _ = cached_function.invoke_function_sync(vec![12.to_js_value_facade()]);
                    Ok(0.to_js_value_facade())
                }
                _ => Err(JsError::new_str("poof")),
            }
        })
        .expect("could not set function");
        let s_res = rt.eval_sync(
            None,
            Script::new(
                "test_ex.es",
                "let consumer = function() {\n
        console.log('consuming');\n
        throw Error('oh dear stuff failed at line 3 in consumer');\n
        };\n
        console.log('calling consume from line 5');test_consume(consumer);\n
        console.log('should never reach line 7')",
            ),
        );
        if s_res.is_err() {
            let e = format!("script failed: {}", s_res.err().unwrap());
            log::error!("{}", e);
            //panic!("{}", e);
        }
        std::thread::sleep(Duration::from_secs(1));
    }

    #[test]
    fn test_ex3() {
        let rt = init_test_rt();
        rt.eval_sync(
            None,
            Script::new(
                "test_ex3.js",
                r#"              
async function asyncDebugStackPreserve(delegate, invokerName) {

    const startStack = new Error().stack;
    try {
        return await delegate();
    } catch (ex) {
        const err = Error(ex.message);
        
        err.fileName = ex.fileName;
        err.lineNumber = ex.lineNumber;
        err.columnNumber = ex.columnNumber;
        err.cause = ex.cause;
        
        err.stack = ex.stack + startStack;
        throw err;
    }

}

async function sleep(ms) {
    return new Promise((res) => {
        setTimeout(res, ms);
    });
}

async function a(){
	
	return new Promise(async (res, rej) => {
		try {
			let ret = await asyncDebugStackPreserve(async function aInner() {
			    await sleep(100);
			    let ret = await b();
     		});
			res(ret);
		} catch(ex) {
			rej(ex);
			}
		});
	}
	
	async function b(){
        throw Error("poof");
	}
	
		a().catch((ex) => {
			console.error(ex);
			});
	
       
       
        "#,
            ),
        )
        .expect("script failed");
        thread::sleep(Duration::from_secs(1));
    }

    #[test]
    fn test_ex_stack() {
        let rt = init_test_rt();
        rt.exe_rt_task_in_event_loop(|rt| {
            let realm = rt.get_main_realm();
            realm
                .install_closure(
                    &[],
                    "myFunc",
                    |_rt, realm, _this, _args| crate::quickjs_utils::errors::get_stack(realm),
                    0,
                )
                .expect("could not install func");

            let res = realm
                .eval(Script::new(
                    "runMyFunc.js",
                    r#"
                function a(){
                    return b();                
                }
                function b(){
                    return myFunc();
                }
                a()
            "#,
                ))
                .expect("script failed");

            log::info!("test_ex_stack res = {}", res.to_string().unwrap());
        });
    }

    #[test]
    fn test_ex2() {
        // check if stacktrace is preserved when invoking native methods

        //simple_logging::log_to_stderr(LevelFilter::Info);

        let rt = init_test_rt();
        rt.exe_rt_task_in_event_loop(|q_js_rt| {
            let q_ctx = q_js_rt.get_main_realm();

            q_ctx
                .eval(Script::new(
                    "test_ex2_pre.es",
                    "console.log('before ex test');",
                ))
                .expect("test_ex2_pre failed");
            {
                let func_ref1 = q_ctx
                    .eval(Script::new(
                        "test_ex2f1.es",
                        "(function(){\nconsole.log('running f1');});",
                    ))
                    .expect("script failed");
                assert!(functions::is_function_q(q_ctx, &func_ref1));
                let res = functions::call_function_q(q_ctx, &func_ref1, &[], None);
                match res {
                    Ok(_) => {}
                    Err(e) => {
                        log::error!("func1 failed: {}", e);
                    }
                }
            }
            // why the f does this fail with a stack overflow if i remove the block above?
            let func_ref2 = q_ctx
                .eval(Script::new(
                    "test_ex2.es",
                    r#"
                    const f = function(){
                        throw Error('poof');
                    };
                    f
                    "#,
                ))
                .expect("script failed");

            assert!(functions::is_function_q(q_ctx, &func_ref2));
            let res = functions::call_function_q(q_ctx, &func_ref2, &[], None);
            match res {
                Ok(_) => {}
                Err(e) => {
                    log::error!("func2 failed: {}", e);
                }
            }
        });

        #[cfg(feature = "bellard")]
        {
            let mjsvf = rt
                .eval_module_sync(
                    None,
                    Script::new(
                        "test_ex2.es",
                        r#"
                                throw Error('poof');
                                "#,
                    ),
                )
                .map_err(|e| {
                    log::error!("script compilation failed: {e}");
                    e
                })
                .expect("script compilation failed");
            match mjsvf {
                JsValueFacade::JsPromise { cached_promise } => {
                    let pres = cached_promise
                        .get_promise_result_sync()
                        .expect("promise timed out");
                    match pres {
                        Ok(m) => {
                            log::info!("prom resolved to {}", m.stringify())
                        }
                        Err(e) => {
                            log::info!("prom rejected to {}", e.stringify())
                        }
                    }
                }
                _ => {
                    panic!("not a prom")
                }
            }
        }

        std::thread::sleep(Duration::from_secs(1));
    }
}