GeezerVison is showing github.com — util: add `kEmptyObject` to internal/util · nodejs/node@b187d55
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/util.js +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,6 +13,7 @@ const { | 13 | 13 | ObjectGetOwnPropertyDescriptor, | 14 | 14 | ObjectGetOwnPropertyDescriptors, | 15 | 15 | ObjectGetPrototypeOf, | | 16 | + ObjectFreeze, | 16 | 17 | ObjectSetPrototypeOf, | 17 | 18 | Promise, | 18 | 19 | ReflectApply, | @@ -504,6 +505,8 @@ const lazyDOMException = hideStackFrames((message, name) => { | 504 | 505 | const kEnumerableProperty = ObjectCreate(null); | 505 | 506 | kEnumerableProperty.enumerable = true; | 506 | 507 |
| | 508 | +const kEmptyObject = ObjectFreeze(ObjectCreate(null)); | | 509 | + | 507 | 510 | module.exports = { | 508 | 511 | assertCrypto, | 509 | 512 | cachedResult, | @@ -544,5 +547,6 @@ module.exports = { | 544 | 547 | kIsEncodingSymbol: Symbol('kIsEncodingSymbol'), | 545 | 548 | kVmBreakFirstLineSymbol: Symbol('kVmBreakFirstLineSymbol'), | 546 | 549 |
| | 550 | + kEmptyObject, | 547 | 551 | kEnumerableProperty, | 548 | 552 | }; |
Collapse file Copy file name to clipboard +90Lines changed: 90 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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; |
|