node icon indicating copy to clipboard operation
node copied to clipboard

vm: introduce vanilla contexts via vm.constants.DONT_CONTEXTIFY

Open joyeecheung opened this issue 1 year ago • 9 comments

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]

joyeecheung avatar Aug 15 '24 14:08 joyeecheung

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.

github-actions[bot] avatar Aug 15 '24 14:08 github-actions[bot]

CI: https://ci.nodejs.org/job/node-test-pull-request/61139/

nodejs-github-bot avatar Aug 15 '24 14:08 nodejs-github-bot

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:

... and 46 files with indirect coverage changes

codecov[bot] avatar Aug 15 '24 16:08 codecov[bot]

CI: https://ci.nodejs.org/job/node-test-pull-request/61242/

nodejs-github-bot avatar Aug 19 '24 16:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61343/

nodejs-github-bot avatar Aug 22 '24 13:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61446/

nodejs-github-bot avatar Aug 25 '24 12:08 nodejs-github-bot

Fixed the misplaced DCHECK

joyeecheung avatar Aug 27 '24 09:08 joyeecheung

CI: https://ci.nodejs.org/job/node-test-pull-request/61536/

nodejs-github-bot avatar Aug 27 '24 09:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61557/

nodejs-github-bot avatar Aug 27 '24 16:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61591/

nodejs-github-bot avatar Aug 28 '24 13:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61596/

nodejs-github-bot avatar Aug 28 '24 17:08 nodejs-github-bot

CI is finally green. Can I get some re-review please? @addaleax @jasnell @legendecas ?

joyeecheung avatar Aug 28 '24 22:08 joyeecheung

Landed in d98cfcc5816189507722c47ad69bda45c5072d4b

nodejs-github-bot avatar Aug 29 '24 09:08 nodejs-github-bot