diff --git a/lib/internal/util.js b/lib/internal/util.js index 61e58a5d69d1cf..030af62be8761e 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -13,6 +13,7 @@ const { ObjectGetOwnPropertyDescriptor, ObjectGetOwnPropertyDescriptors, ObjectGetPrototypeOf, + ObjectFreeze, ObjectSetPrototypeOf, Promise, ReflectApply, @@ -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, @@ -544,5 +547,6 @@ module.exports = { kIsEncodingSymbol: Symbol('kIsEncodingSymbol'), kVmBreakFirstLineSymbol: Symbol('kVmBreakFirstLineSymbol'), + kEmptyObject, kEnumerableProperty, }; diff --git a/test/parallel/test-internal-util-objects.js b/test/parallel/test-internal-util-objects.js new file mode 100644 index 00000000000000..092deb9e09464b --- /dev/null +++ b/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;