vm: introduce vanilla contexts via vm.constants.DONT_CONTEXTIFY
vm: introduce vanilla contexts via vm.constants.DONT_CONTEXTIFY
This implements a flavor of vm.createContext() and friends that creates a context without contextifying its global object. This is suitable when users want to freeze the context (impossible when the global is contextified i.e. has interceptors installed) or speed up the global access if they don't need the interceptor behavior.
const vm = require('node:vm');
const context = vm.createContext(vm.constants.DONT_CONTEXTIFY);
// In contexts with contextified global objects, this is false.
// In vanilla contexts this is true.
console.log(vm.runInContext('globalThis', context) === context);
// In contexts with contextified global objects, this would throw,
// but in vanilla contexts freezing the global object works.
vm.runInContext('Object.freeze(globalThis);', context);
// In contexts with contextified global objects, freezing throws
// and won't be effective. In vanilla contexts, freezing works
// and prevents scripts from accidentally leaking globals.
try {
vm.runInContext('globalThis.foo = 1; foo;', context);
} catch(e) {
console.log(e); // Uncaught ReferenceError: foo is not defined
}
console.log(context.Array); // [Function: Array]
The https://github.com/nodejs/node/labels/notable-change label has been added by @joyeecheung.
Please suggest a text for the release notes if you'd like to include a more detailed summary, then proceed to update the PR description with the text or a link to the notable change suggested text comment. Otherwise, the commit will be placed in the Other Notable Changes section.
CI: https://ci.nodejs.org/job/node-test-pull-request/61139/
Codecov Report
Attention: Patch coverage is 82.14286% with 10 lines in your changes missing coverage. Please review.
Project coverage is 87.33%. Comparing base (
9edf4a0) to head (d43c426). Report is 62 commits behind head on main.
| Files with missing lines | Patch % | Lines |
|---|---|---|
| src/node_contextify.cc | 80.43% | 1 Missing and 8 partials :warning: |
| src/node_contextify.h | 75.00% | 0 Missing and 1 partial :warning: |
Additional details and impacted files
@@ Coverage Diff @@
## main #54394 +/- ##
=======================================
Coverage 87.32% 87.33%
=======================================
Files 649 649
Lines 182603 182590 -13
Branches 35042 35039 -3
=======================================
+ Hits 159464 159470 +6
+ Misses 16400 16391 -9
+ Partials 6739 6729 -10
| Files with missing lines | Coverage Δ | |
|---|---|---|
| lib/vm.js | 99.28% <100.00%> (+<0.01%) |
:arrow_up: |
| src/node_contextify.h | 82.35% <75.00%> (-3.37%) |
:arrow_down: |
| src/node_contextify.cc | 80.84% <80.43%> (+0.21%) |
:arrow_up: |
CI: https://ci.nodejs.org/job/node-test-pull-request/61242/
CI: https://ci.nodejs.org/job/node-test-pull-request/61343/
CI: https://ci.nodejs.org/job/node-test-pull-request/61446/
Fixed the misplaced DCHECK
CI: https://ci.nodejs.org/job/node-test-pull-request/61536/
CI: https://ci.nodejs.org/job/node-test-pull-request/61557/
CI: https://ci.nodejs.org/job/node-test-pull-request/61591/
CI: https://ci.nodejs.org/job/node-test-pull-request/61596/
CI is finally green. Can I get some re-review please? @addaleax @jasnell @legendecas ?
Landed in d98cfcc5816189507722c47ad69bda45c5072d4b