Skip to content

Commit b187d55

Browse files
LiviaMedeirosdanielleadams
authored andcommitted
util: add kEmptyObject to internal/util
PR-URL: #43159 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 8c0fe1e commit b187d55

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

lib/internal/util.js

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
ObjectGetOwnPropertyDescriptor,
1414
ObjectGetOwnPropertyDescriptors,
1515
ObjectGetPrototypeOf,
16+
ObjectFreeze,
1617
ObjectSetPrototypeOf,
1718
Promise,
1819
ReflectApply,
@@ -504,6 +505,8 @@ const lazyDOMException = hideStackFrames((message, name) => {
504505
const kEnumerableProperty = ObjectCreate(null);
505506
kEnumerableProperty.enumerable = true;
506507

508+
const kEmptyObject = ObjectFreeze(ObjectCreate(null));
509+
507510
module.exports = {
508511
assertCrypto,
509512
cachedResult,
@@ -544,5 +547,6 @@ module.exports = {
544547
kIsEncodingSymbol: Symbol('kIsEncodingSymbol'),
545548
kVmBreakFirstLineSymbol: Symbol('kVmBreakFirstLineSymbol'),
546549

550+
kEmptyObject,
547551
kEnumerableProperty,
548552
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
require('../common');
4+
5+
// Test helper objects from internal/util
6+
7+
const assert = require('assert');
8+
const {
9+
kEnumerableProperty,
10+
kEmptyObject,
11+
} = require('internal/util');
12+
13+
Object.prototype.blep = 'blop';
14+
15+
{
16+
assert.strictEqual(
17+
kEnumerableProperty.blep,
18+
undefined
19+
);
20+
assert.strictEqual(
21+
kEnumerableProperty.enumerable,
22+
true
23+
);
24+
assert.strictEqual(
25+
Object.getPrototypeOf(kEnumerableProperty),
26+
null
27+
);
28+
assert.deepStrictEqual(
29+
Object.getOwnPropertyNames(kEnumerableProperty),
30+
[ 'enumerable' ]
31+
);
32+
}
33+
34+
{
35+
assert.strictEqual(
36+
kEmptyObject.blep,
37+
undefined
38+
);
39+
assert.strictEqual(
40+
kEmptyObject.prototype,
41+
undefined
42+
);
43+
assert.strictEqual(
44+
Object.getPrototypeOf(kEmptyObject),
45+
null
46+
);
47+
assert.strictEqual(
48+
kEmptyObject instanceof Object,
49+
false
50+
);
51+
assert.deepStrictEqual(
52+
Object.getOwnPropertyDescriptors(kEmptyObject),
53+
{}
54+
);
55+
assert.deepStrictEqual(
56+
Object.getOwnPropertyNames(kEmptyObject),
57+
[]
58+
);
59+
assert.deepStrictEqual(
60+
Object.getOwnPropertySymbols(kEmptyObject),
61+
[]
62+
);
63+
assert.strictEqual(
64+
Object.isExtensible(kEmptyObject),
65+
false
66+
);
67+
assert.strictEqual(
68+
Object.isSealed(kEmptyObject),
69+
true
70+
);
71+
assert.strictEqual(
72+
Object.isFrozen(kEmptyObject),
73+
true
74+
);
75+
76+
assert.throws(
77+
() => kEmptyObject.foo = 'bar',
78+
TypeError
79+
);
80+
assert.throws(
81+
() => Object.assign(kEmptyObject, { foo: 'bar' }),
82+
TypeError
83+
);
84+
assert.throws(
85+
() => Object.defineProperty(kEmptyObject, 'foo', {}),
86+
TypeError
87+
);
88+
}
89+
90+
delete Object.prototype.blep;

0 commit comments

Comments
 (0)