Skip to content

Commit 14591b7

Browse files
committed
bootstrap: freeze more intrinsics
1 parent 14aed60 commit 14591b7

File tree

2 files changed

+138
-105
lines changed

2 files changed

+138
-105
lines changed

lib/internal/freeze_intrinsics.js

Lines changed: 131 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
'use strict';
2424

2525
const {
26+
AggregateError,
27+
AggregateErrorPrototype,
2628
Array,
2729
ArrayBuffer,
2830
ArrayBufferPrototype,
31+
ArrayIteratorPrototype,
2932
ArrayPrototype,
3033
ArrayPrototypeForEach,
3134
ArrayPrototypePush,
@@ -45,6 +48,8 @@ const {
4548
ErrorPrototype,
4649
EvalError,
4750
EvalErrorPrototype,
51+
FinalizationRegistry,
52+
FinalizationRegistryPrototype,
4853
Float32Array,
4954
Float32ArrayPrototype,
5055
Float64Array,
@@ -84,9 +89,11 @@ const {
8489
Set,
8590
SetPrototype,
8691
String,
92+
StringIteratorPrototype,
8793
StringPrototype,
8894
Symbol,
8995
SymbolIterator,
96+
SymbolMatchAll,
9097
SyntaxError,
9198
SyntaxErrorPrototype,
9299
TypeError,
@@ -105,12 +112,15 @@ const {
105112
URIErrorPrototype,
106113
WeakMap,
107114
WeakMapPrototype,
115+
WeakRef,
116+
WeakRefPrototype,
108117
WeakSet,
109118
WeakSetPrototype,
110119
decodeURI,
111120
decodeURIComponent,
112121
encodeURI,
113122
encodeURIComponent,
123+
globalThis,
114124
} = primordials;
115125

116126
module.exports = function() {
@@ -124,52 +134,37 @@ module.exports = function() {
124134
} = require('timers');
125135

126136
const intrinsicPrototypes = [
127-
// Anonymous Intrinsics
128-
// IteratorPrototype
129-
ObjectGetPrototypeOf(
130-
ObjectGetPrototypeOf(new Array()[SymbolIterator]())
131-
),
132-
// ArrayIteratorPrototype
133-
ObjectGetPrototypeOf(new Array()[SymbolIterator]()),
134-
// StringIteratorPrototype
135-
ObjectGetPrototypeOf(new String()[SymbolIterator]()),
136-
// MapIteratorPrototype
137-
ObjectGetPrototypeOf(new Map()[SymbolIterator]()),
138-
// SetIteratorPrototype
139-
ObjectGetPrototypeOf(new Set()[SymbolIterator]()),
140-
// GeneratorFunction
141-
ObjectGetPrototypeOf(function* () {}),
142-
// AsyncFunction
143-
ObjectGetPrototypeOf(async function() {}),
144-
// AsyncGeneratorFunction
145-
ObjectGetPrototypeOf(async function* () {}),
146-
// TypedArray
147-
TypedArrayPrototype,
148-
149-
// 19 Fundamental Objects
150-
ObjectPrototype, // 19.1
151-
FunctionPrototype, // 19.2
152-
BooleanPrototype, // 19.3
137+
// 21 Fundamental Objects
138+
ObjectPrototype, // 20.1
139+
FunctionPrototype, // 20.2
140+
BooleanPrototype, // 20.3
153141

154-
ErrorPrototype, // 19.5
142+
ErrorPrototype, // 20.5
143+
AggregateErrorPrototype,
155144
EvalErrorPrototype,
156145
RangeErrorPrototype,
157146
ReferenceErrorPrototype,
158147
SyntaxErrorPrototype,
159148
TypeErrorPrototype,
160149
URIErrorPrototype,
161150

162-
// 20 Numbers and Dates
163-
NumberPrototype, // 20.1
164-
DatePrototype, // 20.3
151+
// 21 Numbers and Dates
152+
NumberPrototype, // 21.1
153+
BigIntPrototype, // 21.2
154+
DatePrototype, // 21.4
165155

166-
// 21 Text Processing
167-
StringPrototype, // 21.1
168-
RegExpPrototype, // 21.2
156+
// 22 Text Processing
157+
StringPrototype, // 22.1
158+
StringIteratorPrototype, // 22.1.5
159+
RegExpPrototype, // 22.2
160+
// 22.2.7 RegExpStringIteratorPrototype
161+
ObjectGetPrototypeOf(/e/[SymbolMatchAll]()),
169162

170-
// 22 Indexed Collections
171-
ArrayPrototype, // 22.1
163+
// 23 Indexed Collections
164+
ArrayPrototype, // 23.1
165+
ArrayIteratorPrototype, // 23.1.5
172166

167+
TypedArrayPrototype, // 23.2
173168
Int8ArrayPrototype,
174169
Uint8ArrayPrototype,
175170
Uint8ClampedArrayPrototype,
@@ -182,55 +177,50 @@ module.exports = function() {
182177
BigInt64ArrayPrototype,
183178
BigUint64ArrayPrototype,
184179

185-
// 23 Keyed Collections
186-
MapPrototype, // 23.1
187-
SetPrototype, // 23.2
188-
WeakMapPrototype, // 23.3
189-
WeakSetPrototype, // 23.4
190-
191-
// 24 Structured Data
192-
ArrayBufferPrototype, // 24.1
193-
DataViewPrototype, // 24.3
194-
PromisePrototype, // 25.4
180+
// 24 Keyed Collections
181+
MapPrototype, // 24.1
182+
// 24.1.5 MapIteratorPrototype
183+
ObjectGetPrototypeOf(new Map()[SymbolIterator]()),
184+
SetPrototype, // 24.2
185+
// 24.2.5 SetIteratorPrototype
186+
ObjectGetPrototypeOf(new Set()[SymbolIterator]()),
187+
WeakMapPrototype, // 24.3
188+
WeakSetPrototype, // 24.4
189+
190+
// 25 Structured Data
191+
ArrayBufferPrototype, // 25.1
192+
SharedArrayBuffer.prototype, // 25.2
193+
DataViewPrototype, // 25.3
194+
195+
// 26 Managing Memory
196+
WeakRefPrototype, // 26.1
197+
FinalizationRegistryPrototype, // 26.2
198+
199+
// 27 Control Abstraction Objects
200+
// 27.1 Iteration
201+
ObjectGetPrototypeOf(ArrayIteratorPrototype), // 27.1.2 IteratorPrototype
202+
// 27.1.3 AsyncIteratorPrototype
203+
ObjectGetPrototypeOf(ObjectGetPrototypeOf(ObjectGetPrototypeOf(
204+
(async function*() {})()
205+
))),
206+
PromisePrototype, // 27.2
195207

196208
// Other APIs / Web Compatibility
197209
console.Console.prototype,
198-
BigIntPrototype,
199210
WebAssembly.Module.prototype,
200211
WebAssembly.Instance.prototype,
201212
WebAssembly.Table.prototype,
202213
WebAssembly.Memory.prototype,
203214
WebAssembly.CompileError.prototype,
204215
WebAssembly.LinkError.prototype,
205216
WebAssembly.RuntimeError.prototype,
206-
SharedArrayBuffer.prototype,
207217
];
208218
const intrinsics = [
209-
// Anonymous Intrinsics
210-
// ThrowTypeError
219+
// 10.2.4.1 ThrowTypeError
211220
ObjectGetOwnPropertyDescriptor(FunctionPrototype, 'caller').get,
212-
// IteratorPrototype
213-
ObjectGetPrototypeOf(
214-
ObjectGetPrototypeOf(new Array()[SymbolIterator]())
215-
),
216-
// ArrayIteratorPrototype
217-
ObjectGetPrototypeOf(new Array()[SymbolIterator]()),
218-
// StringIteratorPrototype
219-
ObjectGetPrototypeOf(new String()[SymbolIterator]()),
220-
// MapIteratorPrototype
221-
ObjectGetPrototypeOf(new Map()[SymbolIterator]()),
222-
// SetIteratorPrototype
223-
ObjectGetPrototypeOf(new Set()[SymbolIterator]()),
224-
// GeneratorFunction
225-
ObjectGetPrototypeOf(function* () {}),
226-
// AsyncFunction
227-
ObjectGetPrototypeOf(async function() {}),
228-
// AsyncGeneratorFunction
229-
ObjectGetPrototypeOf(async function* () {}),
230-
// TypedArray
231-
TypedArray,
232221

233-
// 18 The Global Object
222+
// 19 The Global Object
223+
// 19.2 Function Properties of the Global Object
234224
eval,
235225
// eslint-disable-next-line node-core/prefer-primordials
236226
isFinite,
@@ -240,38 +230,46 @@ module.exports = function() {
240230
parseFloat,
241231
// eslint-disable-next-line node-core/prefer-primordials
242232
parseInt,
233+
// 19.2.6 URI Handling Functions
243234
decodeURI,
244235
decodeURIComponent,
245236
encodeURI,
246237
encodeURIComponent,
247238

248-
// 19 Fundamental Objects
249-
Object, // 19.1
250-
Function, // 19.2
251-
Boolean, // 19.3
252-
Symbol, // 19.4
239+
// 20 Fundamental Objects
240+
Object, // 20.1
241+
Function, // 20.2
242+
Boolean, // 20.3
243+
Symbol, // 20.4
253244

254-
Error, // 19.5
245+
Error, // 20.5
246+
AggregateError,
255247
EvalError,
256248
RangeError,
257249
ReferenceError,
258250
SyntaxError,
259251
TypeError,
260252
URIError,
261253

262-
// 20 Numbers and Dates
263-
Number, // 20.1
254+
// 21 Numbers and Dates
255+
Number, // 21.1
256+
BigInt, // 21.2
264257
// eslint-disable-next-line node-core/prefer-primordials
265-
Math, // 20.2
266-
Date, // 20.3
267-
268-
// 21 Text Processing
269-
String, // 21.1
270-
RegExp, // 21.2
271-
272-
// 22 Indexed Collections
273-
Array, // 22.1
274-
258+
Math, // 21.3
259+
Date, // 21.4
260+
261+
// 22 Text Processing
262+
String, // 22.1
263+
StringIteratorPrototype, // 22.1.5
264+
RegExp, // 22.2
265+
// 22.2.7 RegExpStringIteratorPrototype
266+
ObjectGetPrototypeOf(/e/[SymbolMatchAll]()),
267+
268+
// 23 Indexed Collections
269+
Array, // 23.1
270+
ArrayIteratorPrototype, // 23.1.5
271+
// 23.2 TypedArray
272+
TypedArray,
275273
Int8Array,
276274
Uint8Array,
277275
Uint8ClampedArray,
@@ -284,23 +282,47 @@ module.exports = function() {
284282
BigInt64Array,
285283
BigUint64Array,
286284

287-
// 23 Keyed Collections
288-
Map, // 23.1
289-
Set, // 23.2
290-
WeakMap, // 23.3
291-
WeakSet, // 23.4
292-
293-
// 24 Structured Data
294-
ArrayBuffer, // 24.1
295-
DataView, // 24.3
285+
// 24 Keyed Collections
286+
Map, // 24.1
287+
// 24.1.5 MapIteratorPrototype
288+
ObjectGetPrototypeOf(new Map()[SymbolIterator]()),
289+
Set, // 24.2
290+
// 24.2.5 SetIteratorPrototype
291+
ObjectGetPrototypeOf(new Set()[SymbolIterator]()),
292+
WeakMap, // 24.3
293+
WeakSet, // 24.4
294+
295+
// 25 Structured Data
296+
ArrayBuffer, // 25.1
297+
SharedArrayBuffer, // 25.2
298+
DataView, // 25.3
299+
Atomics, // 25.4
296300
// eslint-disable-next-line node-core/prefer-primordials
297-
JSON, // 24.5
298-
Promise, // 25.4
301+
JSON, // 25.5
302+
303+
// 26 Managing Memory
304+
WeakRef, // 26.1
305+
FinalizationRegistry, // 26.2
306+
307+
// 27 Control Abstraction Objects
308+
// 27.1 Iteration
309+
ObjectGetPrototypeOf(ArrayIteratorPrototype), // 27.1.2 IteratorPrototype
310+
// 27.1.3 AsyncIteratorPrototype
311+
ObjectGetPrototypeOf(ObjectGetPrototypeOf(ObjectGetPrototypeOf(
312+
(async function*() {})()
313+
))),
314+
Promise, // 27.2
315+
// 27.3 GeneratorFunction
316+
ObjectGetPrototypeOf(function* () {}),
317+
// 27.4 AsyncGeneratorFunction
318+
ObjectGetPrototypeOf(async function* () {}),
319+
// 27.7 AsyncFunction
320+
ObjectGetPrototypeOf(async function() {}),
299321

300-
// 26 Reflection
322+
// 28 Reflection
301323
// eslint-disable-next-line node-core/prefer-primordials
302-
Reflect, // 26.1
303-
Proxy, // 26.2
324+
Reflect, // 28.1
325+
Proxy, // 28.2
304326

305327
// B.2.1
306328
escape,
@@ -314,10 +336,7 @@ module.exports = function() {
314336
setInterval,
315337
setTimeout,
316338
console,
317-
BigInt,
318-
Atomics,
319339
WebAssembly,
320-
SharedArrayBuffer,
321340
];
322341

323342
if (typeof Intl !== 'undefined') {
@@ -337,6 +356,13 @@ module.exports = function() {
337356
const frozenSet = new WeakSet();
338357
ArrayPrototypeForEach(intrinsics, deepFreeze);
339358

359+
// 19.1 Value Properties of the Global Object
360+
ObjectDefineProperty(globalThis, 'globalThis', {
361+
configurable: false,
362+
writable: false,
363+
value: globalThis,
364+
});
365+
340366
// Objects that are deeply frozen.
341367
function deepFreeze(root) {
342368
/**

test/parallel/test-freeze-intrinsics.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,10 @@ assert.throws(
3030
assert.strictEqual(Object.getOwnPropertyDescriptor(o, 'toString').enumerable,
3131
true);
3232
}
33+
34+
// Ensure we can not override globalThis
35+
{
36+
assert.throws(() => { globalThis.globalThis = null; },
37+
{ name: 'TypeError' });
38+
assert.notStrictEqual(globalThis, null);
39+
}

0 commit comments

Comments
 (0)