On Sat, May 12, 2018 at 6:42 AM, Jane Chen <
jxch...@gmail.com> wrote:
> Thanks Ben.
>
> I want to re-use my context for performance reasons, but some JavaScript
> programs have constant global variables that cannot be re-defined, such as
> those declared with let or const. To work around this, I thought I could
> just evaluate each program in a closure by artificially enclosing the script
> with {}. Do you see any issue with doing it?
I don't see anything wrong with that but if you reuse the context, you
have to scrub globals introduced with `var`, undo any monkey-patching
of builtins, figure out how to cancel pending promises, etc. Using a
new context is probably a lot simpler and robuster.
> Is there any better way to do so?
You could compile your code as an es6 module or with
v8::ScriptCompiler::CompileFunctionInContext() but you'd probably have
to upgrade first. Modules don't exist in V8 5.3 and
CompileFunctionInContext() has a bug where the line and column in
stack traces are wrong.
Modules and functions have different run-time semantics than a
top-level script, of course, so they might not be a good fit if
backwards compatibility is a concern.