Skip to content

Commit

Permalink
util: add kEmptyObject to internal/util
Browse files Browse the repository at this point in the history
PR-URL: #43159
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
LiviaMedeiros authored and danielleadams committed Jun 13, 2022
1 parent 8c0fe1e commit b187d55
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/internal/util.js
Expand Up @@ -13,6 +13,7 @@ const {
ObjectGetOwnPropertyDescriptor,
ObjectGetOwnPropertyDescriptors,
ObjectGetPrototypeOf,
ObjectFreeze,
ObjectSetPrototypeOf,
Promise,
ReflectApply,
Expand Down Expand Up @@ -504,6 +505,8 @@ const lazyDOMException = hideStackFrames((message, name) => {
const kEnumerableProperty = ObjectCreate(null);
kEnumerableProperty.enumerable = true;

const kEmptyObject = ObjectFreeze(ObjectCreate(null));

module.exports = {
assertCrypto,
cachedResult,
Expand Down Expand Up @@ -544,5 +547,6 @@ module.exports = {
kIsEncodingSymbol: Symbol('kIsEncodingSymbol'),
kVmBreakFirstLineSymbol: Symbol('kVmBreakFirstLineSymbol'),

kEmptyObject,
kEnumerableProperty,
};
90 changes: 90 additions & 0 deletions test/parallel/test-internal-util-objects.js
@@ -0,0 +1,90 @@
// Flags: --expose-internals
'use strict';
require('../common');

// Test helper objects from internal/util

const assert = require('assert');
const {
kEnumerableProperty,
kEmptyObject,
} = require('internal/util');

Object.prototype.blep = 'blop';

{
assert.strictEqual(
kEnumerableProperty.blep,
undefined
);
assert.strictEqual(
kEnumerableProperty.enumerable,
true
);
assert.strictEqual(
Object.getPrototypeOf(kEnumerableProperty),
null
);
assert.deepStrictEqual(
Object.getOwnPropertyNames(kEnumerableProperty),
[ 'enumerable' ]
);
}

{
assert.strictEqual(
kEmptyObject.blep,
undefined
);
assert.strictEqual(
kEmptyObject.prototype,
undefined
);
assert.strictEqual(
Object.getPrototypeOf(kEmptyObject),
null
);
assert.strictEqual(
kEmptyObject instanceof Object,
false
);
assert.deepStrictEqual(
Object.getOwnPropertyDescriptors(kEmptyObject),
{}
);
assert.deepStrictEqual(
Object.getOwnPropertyNames(kEmptyObject),
[]
);
assert.deepStrictEqual(
Object.getOwnPropertySymbols(kEmptyObject),
[]
);
assert.strictEqual(
Object.isExtensible(kEmptyObject),
false
);
assert.strictEqual(
Object.isSealed(kEmptyObject),
true
);
assert.strictEqual(
Object.isFrozen(kEmptyObject),
true
);

assert.throws(
() => kEmptyObject.foo = 'bar',
TypeError
);
assert.throws(
() => Object.assign(kEmptyObject, { foo: 'bar' }),
TypeError
);
assert.throws(
() => Object.defineProperty(kEmptyObject, 'foo', {}),
TypeError
);
}

delete Object.prototype.blep;

0 comments on commit b187d55

Please sign in to comment.