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

Use node's vm module to run in clean context #55

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

Conversation

jhawthorn
Copy link
Member

As suggested by Joshua Peek in a previous issue. Node's vm module gives us full control over the globals passed to the context the code is run in.

In order to do this we store our code as a JS string, wrap it in an IFFE (to support the return symantics we use for all runtimes)

vm.runInNewContext is called with {filename: "(execjs)"} in order to have the proper filename appear in backtraces. Normally this string is inserted into backtraces via a gsub in external_runtime.rb which
replaces the filename.

See https://nodejs.org/api/vm.html

@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @arthurnn (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

As suggested by Joshua Peek in a previous issue. Node's vm module gives
us full control over the globals passed to the context the code is run
in.

In order to do this we store our code as a JS string, wrap it in an IFFE
(to support the `return` symantics we use for all runtimes)

vm.runInNewContext is called with {filename: "(execjs)"} in order to
have the proper filename appear in backtraces. Normally this string is
inserted into backtraces via a gsub in external_runtime.rb which
replaces the filename.

See https://nodejs.org/api/vm.html
@jhawthorn
Copy link
Member Author

This is unfortunate. It seems that this hits an issue with node 0.10 where it does not show line numbers for syntax errors when using the vm module.

@guilleiguaran
Copy link
Member

I don't think we should care about node 0.10 since it will be stop receiving updates in a couple of weeks: https://github.com/nodejs/LTS#lts-schedule

@jhawthorn
Copy link
Member Author

I did some manual bisecting between node versions. It seems like Node 0.11.12 is the first version for which syntax errors inside of vm.run* are reported properly (everything >= 0.12.0 should work).

The unfortunate thing is that widely used ubuntu versions, precise (12.04) and trusty (14.04), still ship with old node versions (this is what our travis workers are using).

However, the breakage here is pretty minor. The only issue is that syntax errors don't include the correct stacktrace. Maybe that is an acceptable compromise for these old node versions.

@guilleiguaran guilleiguaran self-assigned this Oct 19, 2016
@guilleiguaran
Copy link
Member

@jhawthorn not sure why but according to Travis tests are passing fine in Node v0.10 (linux) and failing with Node 6.0.0 (osx).

I updated the .travis.yml to display the version of Node used.

@sfcgeorge
Copy link

This really would be a fantastic improvement. I just used ExecJS for something and was baffled to get a fresh environment each time.
Someone else has had a similar go at this if it helps: https://github.com/mavenlink/alaska

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.

None yet

5 participants