diff options
author | Caleb Sander <caleb.sander@gmail.com> | 2018-08-10 23:38:21 -0700 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2018-08-20 12:03:08 +0200 |
commit | 439b75b9c07405457798c1d6acb7dc144f46ea32 (patch) | |
tree | 0769e04cfe415003006a66b8a29267566242d8c7 /lib | |
parent | 6bc43aeea79353a42ea1d678040fc3a64be9af4a (diff) | |
download | android-node-v8-439b75b9c07405457798c1d6acb7dc144f46ea32.tar.gz android-node-v8-439b75b9c07405457798c1d6acb7dc144f46ea32.tar.bz2 android-node-v8-439b75b9c07405457798c1d6acb7dc144f46ea32.zip |
assert, util: *DeepEqual() handles ArrayBuffers
Previously, all ArrayBuffers were considered equal in assert.deepEqual()
and assert.deepStrictEqual().
Now, ArrayBuffers and SharedArrayBuffers must have the same byte lengths
and contents to be considered equal.
In loose mode, an ArrayBuffer is considered equal to a SharedArrayBuffer
if they have the same contents, whereas in strict mode, the buffers must
be both ArrayBuffers or both SharedArrayBuffers.
PR-URL: https://github.com/nodejs/node/pull/22266
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/internal/util/comparisons.js | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/lib/internal/util/comparisons.js b/lib/internal/util/comparisons.js index 729bd1c31e..9844e9d59c 100644 --- a/lib/internal/util/comparisons.js +++ b/lib/internal/util/comparisons.js @@ -3,7 +3,13 @@ const { compare } = process.binding('buffer'); const { isArrayBufferView } = require('internal/util/types'); const { internalBinding } = require('internal/bootstrap/loaders'); -const { isDate, isMap, isRegExp, isSet } = internalBinding('types'); +const { + isAnyArrayBuffer, + isDate, + isMap, + isRegExp, + isSet +} = internalBinding('types'); const { getOwnNonIndexProperties } = process.binding('util'); const ReflectApply = Reflect.apply; @@ -55,6 +61,11 @@ function areSimilarTypedArrays(a, b) { new Uint8Array(b.buffer, b.byteOffset, b.byteLength)) === 0; } +function areEqualArrayBuffers(buf1, buf2) { + return buf1.byteLength === buf2.byteLength && + compare(new Uint8Array(buf1), new Uint8Array(buf2)) === 0; +} + function isFloatTypedArrayTag(tag) { return tag === '[object Float32Array]' || tag === '[object Float64Array]'; } @@ -154,6 +165,10 @@ function strictDeepEqual(val1, val2, memos) { return false; } return keyCheck(val1, val2, kStrict, memos, kIsMap); + } else if (isAnyArrayBuffer(val1)) { + if (!areEqualArrayBuffers(val1, val2)) { + return false; + } // TODO: Make the valueOf checks safe. } else if (typeof val1.valueOf === 'function') { const val1Value = val1.valueOf(); @@ -217,6 +232,11 @@ function looseDeepEqual(val1, val2, memos) { } else if (isSet(val2) || isMap(val2)) { return false; } + if (isAnyArrayBuffer(val1) && isAnyArrayBuffer(val2)) { + if (!areEqualArrayBuffers(val1, val2)) { + return false; + } + } return keyCheck(val1, val2, kLoose, memos, kNoIterator); } |