Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Incorrect line when throwing string #799

Open
ABBAPOH opened this issue Jan 7, 2025 · 13 comments
Open

[Question] Incorrect line when throwing string #799

ABBAPOH opened this issue Jan 7, 2025 · 13 comments

Comments

@ABBAPOH
Copy link
Contributor

ABBAPOH commented Jan 7, 2025

Let's say I have a file test.js

function foo() {
    var path = "foo";
    if (true)
        throw "fail1";
    return path;
}
foo();

Now I call JS_EvalThis on this piece of code and get a backtrace

    at /Users/abbapoh/Developer/qbs/qbs/tests/auto/language/testdata/test.js:1:1
    at <eval> (/Users/abbapoh/Developer/qbs/qbs/tests/auto/language/testdata/test.js:7:1

When changing the line to new throw Error("fail1");, I get test.js:4:19, as expected.
In order to catch strings as errors, we added a wrapper to turn them into Errors like this https://github.com/qbs/quickjs-ng/blob/qbs/quickjs.c#L17483-L17489
This used to be working with the old engine. From what I debugged, find_line_num can't guess the line anymore.
Am I doing this wrong?

@bnoordhuis
Copy link
Contributor

This used to be working with the old engine

What engine/commit is that? bellard/quickjs@...? And what API did you use to obtain that stack trace?

@ABBAPOH
Copy link
Contributor Author

ABBAPOH commented Jan 7, 2025

Yes, Bellard's one.
We get the "stack" property from the exception object and parse it using regexps^^

@bnoordhuis
Copy link
Contributor

You're patching quickjs.c so I suspect it's something on your side. I don't think bellard/master and quickjs-ng have diverged in that respect.

@ABBAPOH
Copy link
Contributor Author

ABBAPOH commented Jan 8, 2025

I tried applying the patch on top of vanilla master and then run git bisect with those changes.
I bisected the behaviour change to this commit 73cc00e.
Though, the reported line is not entirely correct (from the next instruction?), it's not 1.
I used the qsj binary with the above script to test.

@bnoordhuis
Copy link
Contributor

Can you demonstrate the issue with an unmodified copy of quickjs? Because I'd be happy to take a look in that case.

@ABBAPOH
Copy link
Contributor Author

ABBAPOH commented Jan 8, 2025

That lead us to the second issue - QuickJS does not record stack trace when throwing primitive types.
We do throw strings a lot (rather than throw new Error("message") mostly from user code, but also when reporting errors from c++ extensions.
Maybe fix that first? It might be our patch is not entirely correct.
nodejs reports the line in the example above; this also worked with v8 engine.

bnoordhuis added a commit to bnoordhuis/quickjs that referenced this issue Jan 8, 2025
Consider this script that throws an uncaught exception:

    function f() {
        throw "fail"
    }
    f()

Running with `qjs --script t.js` used to print merely:

    fail

But now prints:

    fail
        at f (t.js:2:11)
        at f (t.js:4:1)

qjs has been updated but no public API yet because I still need to think
through the corner cases.

Refs: quickjs-ng#799
@bnoordhuis
Copy link
Contributor

nodejs reports the line in the example above; this also worked with v8 engine.

I'm aware - I wrote that code for node circa 2010, 2011 :-)

Recording the stack trace for primitives: I opened #805. It's not perfect yet but it's 80% there.

@ABBAPOH
Copy link
Contributor Author

ABBAPOH commented Jan 9, 2025

Looks promising! I'll try updating out code to extract backtrace from the new variable and see it it works in our tests.

@saghul
Copy link
Contributor

saghul commented Jan 9, 2025

TBH I'm not 100% convinced this needs fixing. Node seems to be the only thing out there which gets it right:

Screenshot 2025-01-09 at 13 48 22 Screenshot 2025-01-09 at 13 48 18

@ABBAPOH
Copy link
Contributor Author

ABBAPOH commented Jan 9, 2025

That works, thanks! And the lines are correct now, as they were.

@bnoordhuis
Copy link
Contributor

I'm not 100% convinced this needs fixing

The reason I added it to node back then is because people sometimes throw what they think is an exception object but isn't, and that's very hard to debug when you don't get a stack trace. It's a quality-of-life improvement.

@saghul
Copy link
Contributor

saghul commented Jan 9, 2025

Ah I can see how that makes sense!

How did you do that?

@bnoordhuis
Copy link
Contributor

In node? Honestly, I don't remember, too long ago. It's possible V8 already had an API for it but maybe I added one.

ABBAPOH pushed a commit to qbs/quickjs-ng that referenced this issue Jan 11, 2025
Consider this script that throws an uncaught exception:

    function f() {
        throw "fail"
    }
    f()

Running with `qjs --script t.js` used to print merely:

    fail

But now prints:

    fail
        at f (t.js:2:11)
        at f (t.js:4:1)

qjs has been updated but no public API yet because I still need to think
through the corner cases.

Refs: quickjs-ng#799
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants