6
6
7
7
const {
8
8
ArrayPrototypeEvery,
9
+ ArrayPrototypeForEach,
10
+ ArrayPrototypeIncludes,
9
11
ArrayPrototypeMap,
10
12
ArrayPrototypePush,
11
13
ArrayPrototypeSplice,
12
14
BigUint64Array,
13
15
Float64Array,
14
16
NumberMAX_SAFE_INTEGER,
15
- ObjectDefineProperty,
16
17
ObjectFreeze,
17
18
ReflectApply,
18
19
RegExpPrototypeTest,
19
- SafeSet,
20
+ SafeArrayIterator,
21
+ Set,
22
+ SetPrototypeEntries,
23
+ SetPrototypeValues,
20
24
StringPrototypeEndsWith,
21
25
StringPrototypeReplace,
22
26
StringPrototypeSlice,
23
27
StringPrototypeStartsWith,
28
+ Symbol,
29
+ SymbolIterator,
24
30
Uint32Array,
25
31
} = primordials ;
26
32
@@ -41,6 +47,8 @@ const {
41
47
} = require ( 'internal/validators' ) ;
42
48
const constants = internalBinding ( 'constants' ) . os . signals ;
43
49
50
+ const kInternal = Symbol ( 'internal properties' ) ;
51
+
44
52
function assert ( x , msg ) {
45
53
if ( ! x ) throw new ERR_ASSERTION ( msg || 'assertion error' ) ;
46
54
}
@@ -293,18 +301,18 @@ function buildAllowedFlags() {
293
301
294
302
// Save these for comparison against flags provided to
295
303
// process.allowedNodeEnvironmentFlags.has() which lack leading dashes.
296
- const nodeFlags = new SafeSet ( ArrayPrototypeMap ( allowedNodeEnvironmentFlags ,
297
- trimLeadingDashes ) ) ;
298
-
299
- class NodeEnvironmentFlagsSet extends SafeSet {
300
- constructor ( ... args ) {
301
- super ( ... args ) ;
302
-
303
- // The super constructor consumes `add`, but
304
- // disallow any future adds.
305
- ObjectDefineProperty ( this , ' add' , {
306
- value : ( ) => this
307
- } ) ;
304
+ const nodeFlags = ArrayPrototypeMap ( allowedNodeEnvironmentFlags ,
305
+ trimLeadingDashes ) ;
306
+
307
+ class NodeEnvironmentFlagsSet extends Set {
308
+ constructor ( array ) {
309
+ super ( ) ;
310
+ this [ kInternal ] = { array } ;
311
+ }
312
+
313
+ add ( ) {
314
+ // No-op, `Set` API compatible
315
+ return this ;
308
316
}
309
317
310
318
delete ( ) {
@@ -313,7 +321,7 @@ function buildAllowedFlags() {
313
321
}
314
322
315
323
clear ( ) {
316
- // No-op
324
+ // No-op, `Set` API compatible
317
325
}
318
326
319
327
has ( key ) {
@@ -328,13 +336,39 @@ function buildAllowedFlags() {
328
336
key = StringPrototypeReplace ( key , replaceUnderscoresRegex , '-' ) ;
329
337
if ( RegExpPrototypeTest ( leadingDashesRegex , key ) ) {
330
338
key = StringPrototypeReplace ( key , trailingValuesRegex , '' ) ;
331
- return super . has ( key ) ;
339
+ return ArrayPrototypeIncludes ( this [ kInternal ] . array , key ) ;
332
340
}
333
- return nodeFlags . has ( key ) ;
341
+ return ArrayPrototypeIncludes ( nodeFlags , key ) ;
334
342
}
335
343
return false ;
336
344
}
345
+
346
+ entries ( ) {
347
+ this [ kInternal ] . set ??=
348
+ new Set ( new SafeArrayIterator ( this [ kInternal ] . array ) ) ;
349
+ return SetPrototypeEntries ( this [ kInternal ] . set ) ;
350
+ }
351
+
352
+ forEach ( callback , thisArg = undefined ) {
353
+ ArrayPrototypeForEach (
354
+ this [ kInternal ] . array ,
355
+ ( v ) => ReflectApply ( callback , thisArg , [ v , v , this ] )
356
+ ) ;
357
+ }
358
+
359
+ get size ( ) {
360
+ return this [ kInternal ] . array . length ;
361
+ }
362
+
363
+ values ( ) {
364
+ this [ kInternal ] . set ??=
365
+ new Set ( new SafeArrayIterator ( this [ kInternal ] . array ) ) ;
366
+ return SetPrototypeValues ( this [ kInternal ] . set ) ;
367
+ }
337
368
}
369
+ NodeEnvironmentFlagsSet . prototype . keys =
370
+ NodeEnvironmentFlagsSet . prototype [ SymbolIterator ] =
371
+ NodeEnvironmentFlagsSet . prototype . values ;
338
372
339
373
ObjectFreeze ( NodeEnvironmentFlagsSet . prototype . constructor ) ;
340
374
ObjectFreeze ( NodeEnvironmentFlagsSet . prototype ) ;
0 commit comments