Skip to content

Instantly share code, notes, and snippets.

@koush
Last active November 21, 2019 06:18
Show Gist options
  • Save koush/938bf8926d816aae7866969aca337d67 to your computer and use it in GitHub Desktop.
Save koush/938bf8926d816aae7866969aca337d67 to your computer and use it in GitHub Desktop.
function foo1() {
throw new Error();
}
function foo2() {
try {
foo1();
}
catch (e) {
throw e;
}
}
function foo3() {
try {
foo2();
}
finally {
}
}
function foo4() {
try {
foo3();
}
finally {
}
}
foo4();
@koush
Copy link
Author

koush commented Nov 21, 2019

backtrace is rebuilt every time it is rethrown:

diff --git a/quickjs.c b/quickjs.c
index 4392020..00b19cb 100644
--- a/quickjs.c
+++ b/quickjs.c
@@ -5923,7 +5923,14 @@ JSValue JS_Throw(JSContext *ctx, JSValue obj)
 {
     JS_FreeValue(ctx, ctx->current_exception);
     ctx->current_exception = obj;
-    ctx->exception_needs_backtrace = JS_IsError(ctx, obj);
+    if (JS_IsError(ctx, obj)) {
+        JSValue stack = JS_GetProperty(ctx, obj, JS_ATOM_stack);
+        ctx->exception_needs_backtrace = JS_IsUndefined(stack);
+        JS_FreeValue(ctx, stack);
+    }
+    else {
+        ctx->exception_needs_backtrace = FALSE;
+    }
     js_debugger_exception(ctx);
     return JS_EXCEPTION;
 }

@koush
Copy link
Author

koush commented Nov 21, 2019

This is correct.

Mac-3:quickjs-2019-10-27$ ./qjs test.js
Error
    at foo1 (test.js:2)
    at foo2 (test.js:7)
    at foo3 (test.js:15)
    at foo4 (test.js:22)
    at <eval> (test.js:28)

@Coldzer0
Copy link

@koush
Thanks man, this fix helps a lot <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment