Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vm: introduce vanilla contexts via vm.constants.DONT_CONTEXTIFY #54394

Merged
merged 4 commits into from
Aug 29, 2024

Commits on Aug 23, 2024

  1. 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.
    
    ```js
    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 committed Aug 23, 2024
    Configuration menu
    Copy the full SHA
    e5b0f87 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    c6cb027 View commit details
    Browse the repository at this point in the history

Commits on Aug 27, 2024

  1. Configuration menu
    Copy the full SHA
    85c7090 View commit details
    Browse the repository at this point in the history

Commits on Aug 28, 2024

  1. fixup! vm: introduce vanilla contexts via vm.constants.DONT_CONTEXTIFY

    Co-authored-by: Chengzhong Wu <legendecas@gmail.com>
    joyeecheung and legendecas authored Aug 28, 2024
    Configuration menu
    Copy the full SHA
    d43c426 View commit details
    Browse the repository at this point in the history