Skip to content

Commit e9c7243

Browse files
Dara HayesMylesBorins
Dara Hayes
authored andcommitted
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: #23194 doc: clarify parsingContext option for vm.compileScript test: usage of parsingContext in vm.compileFunction PR-URL: #23206 Fixes: #23194 Reviewed-By: Ujjwal Sharma <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]>
1 parent a22ef72 commit e9c7243

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

doc/api/vm.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -655,8 +655,8 @@ added: v10.10.0
655655
data for the supplied source.
656656
* `produceCachedData` {boolean} Specifies whether to produce new cache data.
657657
**Default:** `false`.
658-
* `parsingContext` {Object} The sandbox/context in which the said function
659-
should be compiled in.
658+
* `parsingContext` {Object} The [contextified][] sandbox in which the said
659+
function should be compiled in.
660660
* `contextExtensions` {Object[]} An array containing a collection of context
661661
extensions (objects wrapping the current scope) to be applied while
662662
compiling. **Default:** `[]`.

src/node_contextify.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,7 @@ void ContextifyContext::CompileFunction(
10531053
}
10541054

10551055
MaybeLocal<Function> maybe_fun = ScriptCompiler::CompileFunctionInContext(
1056-
context, &source, params.size(), params.data(),
1056+
parsing_context, &source, params.size(), params.data(),
10571057
context_extensions.size(), context_extensions.data(), options);
10581058

10591059
Local<Function> fun;

test/parallel/test-vm-basic.js

+40
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,46 @@ const vm = require('vm');
267267
stack: 'Error: Sample Error\n at <anonymous>:1:10'
268268
});
269269

270+
assert.strictEqual(
271+
vm.compileFunction(
272+
'return varInContext',
273+
[],
274+
{
275+
parsingContext: vm.createContext({ varInContext: 'abc' })
276+
}
277+
)(),
278+
'abc'
279+
);
280+
281+
common.expectsError(() => {
282+
vm.compileFunction(
283+
'return varInContext',
284+
[]
285+
)();
286+
}, {
287+
message: 'varInContext is not defined',
288+
stack: 'ReferenceError: varInContext is not defined\n at <anonymous>:1:1'
289+
});
290+
291+
assert.notDeepStrictEqual(
292+
vm.compileFunction(
293+
'return global',
294+
[],
295+
{
296+
parsingContext: vm.createContext({ global: {} })
297+
}
298+
)(),
299+
global
300+
);
301+
302+
assert.deepStrictEqual(
303+
vm.compileFunction(
304+
'return global',
305+
[]
306+
)(),
307+
global
308+
);
309+
270310
// Resetting value
271311
Error.stackTraceLimit = oldLimit;
272312
}

0 commit comments

Comments
 (0)