diff --git a/lib/internal/freeze_intrinsics.js b/lib/internal/freeze_intrinsics.js index e7282588551929..ca73559c3eb0cf 100644 --- a/lib/internal/freeze_intrinsics.js +++ b/lib/internal/freeze_intrinsics.js @@ -23,9 +23,12 @@ 'use strict'; const { + AggregateError, + AggregateErrorPrototype, Array, ArrayBuffer, ArrayBufferPrototype, + ArrayIteratorPrototype, ArrayPrototype, ArrayPrototypeForEach, ArrayPrototypePush, @@ -45,6 +48,8 @@ const { ErrorPrototype, EvalError, EvalErrorPrototype, + FinalizationRegistry, + FinalizationRegistryPrototype, Float32Array, Float32ArrayPrototype, Float64Array, @@ -84,9 +89,12 @@ const { Set, SetPrototype, String, + StringIteratorPrototype, StringPrototype, Symbol, SymbolIterator, + SymbolMatchAll, + SymbolPrototype, SyntaxError, SyntaxErrorPrototype, TypeError, @@ -105,12 +113,15 @@ const { URIErrorPrototype, WeakMap, WeakMapPrototype, + WeakRef, + WeakRefPrototype, WeakSet, WeakSetPrototype, decodeURI, decodeURIComponent, encodeURI, encodeURIComponent, + globalThis, } = primordials; module.exports = function() { @@ -124,34 +135,14 @@ module.exports = function() { } = require('timers'); const intrinsicPrototypes = [ - // Anonymous Intrinsics - // IteratorPrototype - ObjectGetPrototypeOf( - ObjectGetPrototypeOf(new Array()[SymbolIterator]()) - ), - // ArrayIteratorPrototype - ObjectGetPrototypeOf(new Array()[SymbolIterator]()), - // StringIteratorPrototype - ObjectGetPrototypeOf(new String()[SymbolIterator]()), - // MapIteratorPrototype - ObjectGetPrototypeOf(new Map()[SymbolIterator]()), - // SetIteratorPrototype - ObjectGetPrototypeOf(new Set()[SymbolIterator]()), - // GeneratorFunction - ObjectGetPrototypeOf(function* () {}), - // AsyncFunction - ObjectGetPrototypeOf(async function() {}), - // AsyncGeneratorFunction - ObjectGetPrototypeOf(async function* () {}), - // TypedArray - TypedArrayPrototype, - - // 19 Fundamental Objects - ObjectPrototype, // 19.1 - FunctionPrototype, // 19.2 - BooleanPrototype, // 19.3 - - ErrorPrototype, // 19.5 + // 20 Fundamental Objects + ObjectPrototype, // 20.1 + FunctionPrototype, // 20.2 + BooleanPrototype, // 20.3 + SymbolPrototype, // 20.4 + + ErrorPrototype, // 20.5 + AggregateErrorPrototype, EvalErrorPrototype, RangeErrorPrototype, ReferenceErrorPrototype, @@ -159,17 +150,23 @@ module.exports = function() { TypeErrorPrototype, URIErrorPrototype, - // 20 Numbers and Dates - NumberPrototype, // 20.1 - DatePrototype, // 20.3 + // 21 Numbers and Dates + NumberPrototype, // 21.1 + BigIntPrototype, // 21.2 + DatePrototype, // 21.4 - // 21 Text Processing - StringPrototype, // 21.1 - RegExpPrototype, // 21.2 + // 22 Text Processing + StringPrototype, // 22.1 + StringIteratorPrototype, // 22.1.5 + RegExpPrototype, // 22.2 + // 22.2.7 RegExpStringIteratorPrototype + ObjectGetPrototypeOf(/e/[SymbolMatchAll]()), - // 22 Indexed Collections - ArrayPrototype, // 22.1 + // 23 Indexed Collections + ArrayPrototype, // 23.1 + ArrayIteratorPrototype, // 23.1.5 + TypedArrayPrototype, // 23.2 Int8ArrayPrototype, Uint8ArrayPrototype, Uint8ClampedArrayPrototype, @@ -182,20 +179,36 @@ module.exports = function() { BigInt64ArrayPrototype, BigUint64ArrayPrototype, - // 23 Keyed Collections - MapPrototype, // 23.1 - SetPrototype, // 23.2 - WeakMapPrototype, // 23.3 - WeakSetPrototype, // 23.4 - - // 24 Structured Data - ArrayBufferPrototype, // 24.1 - DataViewPrototype, // 24.3 - PromisePrototype, // 25.4 + // 24 Keyed Collections + MapPrototype, // 24.1 + // 24.1.5 MapIteratorPrototype + ObjectGetPrototypeOf(new Map()[SymbolIterator]()), + SetPrototype, // 24.2 + // 24.2.5 SetIteratorPrototype + ObjectGetPrototypeOf(new Set()[SymbolIterator]()), + WeakMapPrototype, // 24.3 + WeakSetPrototype, // 24.4 + + // 25 Structured Data + ArrayBufferPrototype, // 25.1 + SharedArrayBuffer.prototype, // 25.2 + DataViewPrototype, // 25.3 + + // 26 Managing Memory + WeakRefPrototype, // 26.1 + FinalizationRegistryPrototype, // 26.2 + + // 27 Control Abstraction Objects + // 27.1 Iteration + ObjectGetPrototypeOf(ArrayIteratorPrototype), // 27.1.2 IteratorPrototype + // 27.1.3 AsyncIteratorPrototype + ObjectGetPrototypeOf(ObjectGetPrototypeOf(ObjectGetPrototypeOf( + (async function*() {})() + ))), + PromisePrototype, // 27.2 // Other APIs / Web Compatibility console.Console.prototype, - BigIntPrototype, WebAssembly.Module.prototype, WebAssembly.Instance.prototype, WebAssembly.Table.prototype, @@ -203,34 +216,13 @@ module.exports = function() { WebAssembly.CompileError.prototype, WebAssembly.LinkError.prototype, WebAssembly.RuntimeError.prototype, - SharedArrayBuffer.prototype, ]; const intrinsics = [ - // Anonymous Intrinsics - // ThrowTypeError + // 10.2.4.1 ThrowTypeError ObjectGetOwnPropertyDescriptor(FunctionPrototype, 'caller').get, - // IteratorPrototype - ObjectGetPrototypeOf( - ObjectGetPrototypeOf(new Array()[SymbolIterator]()) - ), - // ArrayIteratorPrototype - ObjectGetPrototypeOf(new Array()[SymbolIterator]()), - // StringIteratorPrototype - ObjectGetPrototypeOf(new String()[SymbolIterator]()), - // MapIteratorPrototype - ObjectGetPrototypeOf(new Map()[SymbolIterator]()), - // SetIteratorPrototype - ObjectGetPrototypeOf(new Set()[SymbolIterator]()), - // GeneratorFunction - ObjectGetPrototypeOf(function* () {}), - // AsyncFunction - ObjectGetPrototypeOf(async function() {}), - // AsyncGeneratorFunction - ObjectGetPrototypeOf(async function* () {}), - // TypedArray - TypedArray, - // 18 The Global Object + // 19 The Global Object + // 19.2 Function Properties of the Global Object eval, // eslint-disable-next-line node-core/prefer-primordials isFinite, @@ -240,18 +232,20 @@ module.exports = function() { parseFloat, // eslint-disable-next-line node-core/prefer-primordials parseInt, + // 19.2.6 URI Handling Functions decodeURI, decodeURIComponent, encodeURI, encodeURIComponent, - // 19 Fundamental Objects - Object, // 19.1 - Function, // 19.2 - Boolean, // 19.3 - Symbol, // 19.4 + // 20 Fundamental Objects + Object, // 20.1 + Function, // 20.2 + Boolean, // 20.3 + Symbol, // 20.4 - Error, // 19.5 + Error, // 20.5 + AggregateError, EvalError, RangeError, ReferenceError, @@ -259,19 +253,25 @@ module.exports = function() { TypeError, URIError, - // 20 Numbers and Dates - Number, // 20.1 + // 21 Numbers and Dates + Number, // 21.1 + BigInt, // 21.2 // eslint-disable-next-line node-core/prefer-primordials - Math, // 20.2 - Date, // 20.3 - - // 21 Text Processing - String, // 21.1 - RegExp, // 21.2 - - // 22 Indexed Collections - Array, // 22.1 - + Math, // 21.3 + Date, // 21.4 + + // 22 Text Processing + String, // 22.1 + StringIteratorPrototype, // 22.1.5 + RegExp, // 22.2 + // 22.2.7 RegExpStringIteratorPrototype + ObjectGetPrototypeOf(/e/[SymbolMatchAll]()), + + // 23 Indexed Collections + Array, // 23.1 + ArrayIteratorPrototype, // 23.1.5 + // 23.2 TypedArray + TypedArray, Int8Array, Uint8Array, Uint8ClampedArray, @@ -284,23 +284,47 @@ module.exports = function() { BigInt64Array, BigUint64Array, - // 23 Keyed Collections - Map, // 23.1 - Set, // 23.2 - WeakMap, // 23.3 - WeakSet, // 23.4 - - // 24 Structured Data - ArrayBuffer, // 24.1 - DataView, // 24.3 + // 24 Keyed Collections + Map, // 24.1 + // 24.1.5 MapIteratorPrototype + ObjectGetPrototypeOf(new Map()[SymbolIterator]()), + Set, // 24.2 + // 24.2.5 SetIteratorPrototype + ObjectGetPrototypeOf(new Set()[SymbolIterator]()), + WeakMap, // 24.3 + WeakSet, // 24.4 + + // 25 Structured Data + ArrayBuffer, // 25.1 + SharedArrayBuffer, // 25.2 + DataView, // 25.3 + Atomics, // 25.4 // eslint-disable-next-line node-core/prefer-primordials - JSON, // 24.5 - Promise, // 25.4 + JSON, // 25.5 + + // 26 Managing Memory + WeakRef, // 26.1 + FinalizationRegistry, // 26.2 + + // 27 Control Abstraction Objects + // 27.1 Iteration + ObjectGetPrototypeOf(ArrayIteratorPrototype), // 27.1.2 IteratorPrototype + // 27.1.3 AsyncIteratorPrototype + ObjectGetPrototypeOf(ObjectGetPrototypeOf(ObjectGetPrototypeOf( + (async function*() {})() + ))), + Promise, // 27.2 + // 27.3 GeneratorFunction + ObjectGetPrototypeOf(function* () {}), + // 27.4 AsyncGeneratorFunction + ObjectGetPrototypeOf(async function* () {}), + // 27.7 AsyncFunction + ObjectGetPrototypeOf(async function() {}), - // 26 Reflection + // 28 Reflection // eslint-disable-next-line node-core/prefer-primordials - Reflect, // 26.1 - Proxy, // 26.2 + Reflect, // 28.1 + Proxy, // 28.2 // B.2.1 escape, @@ -314,10 +338,7 @@ module.exports = function() { setInterval, setTimeout, console, - BigInt, - Atomics, WebAssembly, - SharedArrayBuffer, ]; if (typeof Intl !== 'undefined') { @@ -337,6 +358,13 @@ module.exports = function() { const frozenSet = new WeakSet(); ArrayPrototypeForEach(intrinsics, deepFreeze); + // 19.1 Value Properties of the Global Object + ObjectDefineProperty(globalThis, 'globalThis', { + configurable: false, + writable: false, + value: globalThis, + }); + // Objects that are deeply frozen. function deepFreeze(root) { /** diff --git a/test/parallel/test-freeze-intrinsics.js b/test/parallel/test-freeze-intrinsics.js index 2e9ff5f4fe2fdc..978a6861a517fc 100644 --- a/test/parallel/test-freeze-intrinsics.js +++ b/test/parallel/test-freeze-intrinsics.js @@ -30,3 +30,10 @@ assert.throws( assert.strictEqual(Object.getOwnPropertyDescriptor(o, 'toString').enumerable, true); } + +// Ensure we can not override globalThis +{ + assert.throws(() => { globalThis.globalThis = null; }, + { name: 'TypeError' }); + assert.strictEqual(globalThis.globalThis, globalThis); +}