diff --git a/doc/api/vm.md b/doc/api/vm.md index 6259b9b2ffd3f5..f05a767450af52 100644 --- a/doc/api/vm.md +++ b/doc/api/vm.md @@ -655,8 +655,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 f46193d77510b7..9c09bbb5fff3d1 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -1053,7 +1053,7 @@ void ContextifyContext::CompileFunction( } MaybeLocal 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 fun; diff --git a/test/parallel/test-vm-basic.js b/test/parallel/test-vm-basic.js index c5adde4e5b2c4b..54b7c45ff8043a 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 :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 :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; }