diff options
author | Dara Hayes <dara.hayes@redhat.com> | 2018-10-01 19:56:07 +0100 |
---|---|---|
committer | Daniel Bevenius <daniel.bevenius@gmail.com> | 2018-10-11 08:42:49 +0200 |
commit | 8da674df493e2ded8dc812252ac4f1f90c362807 (patch) | |
tree | 5563fc15134c34aaaf123700e833325f1ff9e6ed | |
parent | 700fe5bbc489903578ff4d3d9da88651ae23213c (diff) | |
download | android-node-v8-8da674df493e2ded8dc812252ac4f1f90c362807.tar.gz android-node-v8-8da674df493e2ded8dc812252ac4f1f90c362807.tar.bz2 android-node-v8-8da674df493e2ded8dc812252ac4f1f90c362807.zip |
vm: pass parsing_context to ScriptCompiler::CompileFunctionInContext
ContextifyContext::CompileFunction in src/node_contextify.cc
was incorrectly passing the context variable to
ScriptCompiler::CompileFunctionInContext
This meant that the parsingContext option in vm.compileFunction
was not being applied properly to the compiled function.
fixes: https://github.com/nodejs/node/issues/23194
doc: clarify parsingContext option for vm.compileScript
test: usage of parsingContext in vm.compileFunction
PR-URL: https://github.com/nodejs/node/pull/23206
Fixes: https://github.com/nodejs/node/issues/23194
Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
-rw-r--r-- | doc/api/vm.md | 4 | ||||
-rw-r--r-- | src/node_contextify.cc | 2 | ||||
-rw-r--r-- | test/parallel/test-vm-basic.js | 40 |
3 files changed, 43 insertions, 3 deletions
diff --git a/doc/api/vm.md b/doc/api/vm.md index 07923de585..5bf09de379 100644 --- a/doc/api/vm.md +++ b/doc/api/vm.md @@ -673,8 +673,8 @@ added: v10.10.0 data for the supplied source. * `produceCachedData` {boolean} Specifies whether to produce new cache data. **Default:** `false`. - * `parsingContext` {Object} The sandbox/context in which the said function - should be compiled in. + * `parsingContext` {Object} The [contextified][] sandbox in which the said + function should be compiled in. * `contextExtensions` {Object[]} An array containing a collection of context extensions (objects wrapping the current scope) to be applied while compiling. **Default:** `[]`. diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 023a659ebb..9559747467 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -1070,7 +1070,7 @@ void ContextifyContext::CompileFunction( } MaybeLocal<Function> maybe_fun = ScriptCompiler::CompileFunctionInContext( - context, &source, params.size(), params.data(), + parsing_context, &source, params.size(), params.data(), context_extensions.size(), context_extensions.data(), options); Local<Function> fun; diff --git a/test/parallel/test-vm-basic.js b/test/parallel/test-vm-basic.js index c5adde4e5b..54b7c45ff8 100644 --- a/test/parallel/test-vm-basic.js +++ b/test/parallel/test-vm-basic.js @@ -267,6 +267,46 @@ const vm = require('vm'); stack: 'Error: Sample Error\n at <anonymous>:1:10' }); + assert.strictEqual( + vm.compileFunction( + 'return varInContext', + [], + { + parsingContext: vm.createContext({ varInContext: 'abc' }) + } + )(), + 'abc' + ); + + common.expectsError(() => { + vm.compileFunction( + 'return varInContext', + [] + )(); + }, { + message: 'varInContext is not defined', + stack: 'ReferenceError: varInContext is not defined\n at <anonymous>:1:1' + }); + + assert.notDeepStrictEqual( + vm.compileFunction( + 'return global', + [], + { + parsingContext: vm.createContext({ global: {} }) + } + )(), + global + ); + + assert.deepStrictEqual( + vm.compileFunction( + 'return global', + [] + )(), + global + ); + // Resetting value Error.stackTraceLimit = oldLimit; } |