Skip to content

Commit 2f83ddc

Browse files
Dara Hayesjasnell
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 <usharma1998@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
1 parent 4d21e34 commit 2f83ddc

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
@@ -673,8 +673,8 @@ added: v10.10.0
673673
data for the supplied source.
674674
* `produceCachedData` {boolean} Specifies whether to produce new cache data.
675675
**Default:** `false`.
676-
* `parsingContext` {Object} The sandbox/context in which the said function
677-
should be compiled in.
676+
* `parsingContext` {Object} The [contextified][] sandbox in which the said
677+
function should be compiled in.
678678
* `contextExtensions` {Object[]} An array containing a collection of context
679679
extensions (objects wrapping the current scope) to be applied while
680680
compiling. **Default:** `[]`.

src/node_contextify.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ void ContextifyContext::CompileFunction(
10701070
}
10711071

10721072
MaybeLocal<Function> maybe_fun = ScriptCompiler::CompileFunctionInContext(
1073-
context, &source, params.size(), params.data(),
1073+
parsing_context, &source, params.size(), params.data(),
10741074
context_extensions.size(), context_extensions.data(), options);
10751075

10761076
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)