Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ffadcc1

Browse filesBrowse files
Dara HayesMylesBorins
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 0b39975 commit ffadcc1
Copy full SHA for ffadcc1

File tree

Expand file treeCollapse file tree

3 files changed

+43
-3
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+43
-3
lines changed
Open diff view settings
Collapse file

‎doc/api/vm.md‎

Copy file name to clipboardExpand all lines: doc/api/vm.md
+2-2Lines changed: 2 additions & 2 deletions
  • Display the source diff
  • Display the rich diff
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:** `[]`.
Collapse file

‎src/node_contextify.cc‎

Copy file name to clipboardExpand all lines: src/node_contextify.cc
+1-1Lines changed: 1 addition & 1 deletion
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;
Collapse file

‎test/parallel/test-vm-basic.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-vm-basic.js
+40Lines changed: 40 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.