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

better stack traces #77

Open
doodlesbykumbi opened this issue Oct 7, 2021 · 5 comments
Open

better stack traces #77

doodlesbykumbi opened this issue Oct 7, 2021 · 5 comments
Labels
enhancement New feature or request

Comments

@doodlesbykumbi
Copy link

doodlesbykumbi commented Oct 7, 2021

It would be nice if errors created and thrown by code running via sval could have better stack traces. Below I've shared an approach I used in a POC. All the information to create rich stack traces is available. I suspect it's even possible to have stack traces that combine traces from scripts within sval and scripts outside (i.e. when an outside script calls an inside script and vice-versa)

@doodlesbykumbi
Copy link
Author

doodlesbykumbi commented Oct 7, 2021

I did a small proof of concept where I modified run to include a sourceFile argument and to generate the AST with locations.

const ast = parse(input, {...this.options, locations: true, sourceFile})

With the locations present on each node I then

  1. Modify CallExpression to push the call's top-most object AST node (window in window.console.log) to a stack, that I keep on the chain of scopes connecting nested CallExpressions, before the call is made and to pop from the stack after the call is done (using try-finaly)
  2. Override the stack on errors generated by ThrowStatement by collecting the AST nodes in the stack, and using the location information.

Running this

const firstJs = `
    function a () {
        throw new Error(3);
    }
    function c() {
        b();
    }
    function b() {
        a();
    }

    b();
`

const Sval = require('./dist/lib/index.js').default;

const interpreter = new Sval({sandbox: false})
interpreter.run(firstJs, "first.js")

Yields this

Error: 3
    at first.js:3:8
    at a (first.js:9:8)
    at b (first.js:12:4)

@Siubaak Siubaak added the enhancement New feature or request label Oct 8, 2021
@doodlesbykumbi
Copy link
Author

@Siubaak I would love to get some thoughts on this from you, particularly any ideas around approaches

@scgm0
Copy link

scgm0 commented Mar 11, 2022

wow,I think this is very good!So you can send it out?Thank you very much!(^▽^)

@dashakureru
Copy link

any updates on this? :)

@crlsmsq
Copy link

crlsmsq commented Apr 27, 2022

hi @doodlesbykumbi! your work is awesome. can you publish your work ? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants