Fix for #1035: Giving the eval
sandbox a real Module
instance
#1487
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Read my comment at #1035 first.
This patch makes CoffeeScript's REPL behave a bit more like Node's. Node creates the
module
instance for its REPL like so:Beyond that, things get a bit tricky.
require
is actually defined inside of amodule
function and stores an internal reference tothis
; there's no way to get either therequire
function from amodule
or vice versa. Fortunately,require
is just a one-liner:In this patch, it's reverse-engineered as
Ideally we wouldn't use Node's internal API, but I don't see a good alternative...
This patch also
repl.coffee
'srun
andcoffee-script.coffee
'seval
. Both were definingsandbox
identically, even thoughrun
always callseval
. With this patch,run
just setssandbox
to{}
, andeval
fills in the gaps (currently by testing whethersandbox.require
is defined).exports
is undefined instead of{}
, and__dirname
and__filename
can be modified.[Edit: The issue described below was fixed by the second commit in the pull request, which further emulates the way
require
is instantiated by Node.]Everything seems to run smoothly under this patch, with the exception of one notable quirk: Neither
module.paths
norrequire.paths
are defined. However, lines likerun fine. I'm not sure what's going on there, and being able to modify
require.paths
in the REPL (as it can be in Node's REPL) certainly seems desirable. Further investigation required.