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

Define function toString #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ruifigueira
Copy link
Contributor

@ruifigueira ruifigueira commented Feb 11, 2024

Calling toString on a function would return the source code of the interpreter instead of the actual function body as declared.

It's possible to extract the function code from the node itself, using acorn options like sourceFile.

@ruifigueira ruifigueira marked this pull request as draft February 11, 2024 22:08
@ruifigueira ruifigueira marked this pull request as ready for review February 13, 2024 18:21
@Siubaak
Copy link
Owner

Siubaak commented Apr 18, 2024

The location and sourceFile options enormously reduce parse efficiency, leading to a low perf of evaluation. I think we should find a more efficient way to generate the string of a function

@ruifigueira
Copy link
Contributor Author

ruifigueira commented Apr 18, 2024

I understand. I reverted the implicit parsing with locations: true, sourceFile: code.
But if the user explicitly includes the source file by provdiing a parser function, a function's .toString will be able to use that source.

Example:

  it('should serialize functions with toString', () => { 
    const parser =  (code) => parse(code, { ecmaVersion: 'latest', locations: true, sourceFile: code });
    const interpreter = new Sval()
    interpreter.import({ expect })
    const parsedCode = interpreter.parse(`
      expect((function x(a, b) { return a + b }).toString()).toEqual('function x(a, b) { return a + b }')
    `, parser);
    interpreter.run(parsedCode)
  })

@ruifigueira
Copy link
Contributor Author

hi @Siubaak did you have a chance to look into my new proposal?

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

Successfully merging this pull request may close these issues.

2 participants