• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const {
4  ArrayIsArray,
5  BigIntPrototypeValueOf,
6  BooleanPrototypeValueOf,
7  DatePrototypeGetTime,
8  Error,
9  Map,
10  NumberIsNaN,
11  NumberPrototypeValueOf,
12  ObjectGetOwnPropertySymbols,
13  ObjectGetPrototypeOf,
14  ObjectIs,
15  ObjectKeys,
16  ObjectPrototypeHasOwnProperty,
17  ObjectPrototypePropertyIsEnumerable,
18  ObjectPrototypeToString,
19  Set,
20  StringPrototypeValueOf,
21  SymbolPrototypeValueOf,
22  SymbolToStringTag,
23  Uint8Array,
24} = primordials;
25
26const { compare } = internalBinding('buffer');
27const assert = require('internal/assert');
28const types = require('internal/util/types');
29const {
30  isAnyArrayBuffer,
31  isArrayBufferView,
32  isDate,
33  isMap,
34  isRegExp,
35  isSet,
36  isNativeError,
37  isBoxedPrimitive,
38  isNumberObject,
39  isStringObject,
40  isBooleanObject,
41  isBigIntObject,
42  isSymbolObject,
43  isFloat32Array,
44  isFloat64Array,
45  isUint8Array,
46  isUint8ClampedArray,
47  isUint16Array,
48  isUint32Array,
49  isInt8Array,
50  isInt16Array,
51  isInt32Array,
52  isBigInt64Array,
53  isBigUint64Array
54} = types;
55const {
56  getOwnNonIndexProperties,
57  propertyFilter: {
58    ONLY_ENUMERABLE,
59    SKIP_SYMBOLS
60  }
61} = internalBinding('util');
62
63const kStrict = true;
64const kLoose = false;
65
66const kNoIterator = 0;
67const kIsArray = 1;
68const kIsSet = 2;
69const kIsMap = 3;
70
71// Check if they have the same source and flags
72function areSimilarRegExps(a, b) {
73  return a.source === b.source && a.flags === b.flags;
74}
75
76function areSimilarFloatArrays(a, b) {
77  if (a.byteLength !== b.byteLength) {
78    return false;
79  }
80  for (let offset = 0; offset < a.byteLength; offset++) {
81    if (a[offset] !== b[offset]) {
82      return false;
83    }
84  }
85  return true;
86}
87
88function areSimilarTypedArrays(a, b) {
89  if (a.byteLength !== b.byteLength) {
90    return false;
91  }
92  return compare(new Uint8Array(a.buffer, a.byteOffset, a.byteLength),
93                 new Uint8Array(b.buffer, b.byteOffset, b.byteLength)) === 0;
94}
95
96function areEqualArrayBuffers(buf1, buf2) {
97  return buf1.byteLength === buf2.byteLength &&
98    compare(new Uint8Array(buf1), new Uint8Array(buf2)) === 0;
99}
100
101function isEqualBoxedPrimitive(val1, val2) {
102  if (isNumberObject(val1)) {
103    return isNumberObject(val2) &&
104           ObjectIs(NumberPrototypeValueOf(val1),
105                    NumberPrototypeValueOf(val2));
106  }
107  if (isStringObject(val1)) {
108    return isStringObject(val2) &&
109           StringPrototypeValueOf(val1) === StringPrototypeValueOf(val2);
110  }
111  if (isBooleanObject(val1)) {
112    return isBooleanObject(val2) &&
113           BooleanPrototypeValueOf(val1) === BooleanPrototypeValueOf(val2);
114  }
115  if (isBigIntObject(val1)) {
116    return isBigIntObject(val2) &&
117           BigIntPrototypeValueOf(val1) === BigIntPrototypeValueOf(val2);
118  }
119  if (isSymbolObject(val1)) {
120    return isSymbolObject(val2) &&
121          SymbolPrototypeValueOf(val1) === SymbolPrototypeValueOf(val2);
122  }
123  /* c8 ignore next */
124  assert.fail(`Unknown boxed type ${val1}`);
125}
126
127function isIdenticalTypedArrayType(a, b) {
128  // Fast path to reduce type checks in the common case.
129  const check = types[`is${a[SymbolToStringTag]}`];
130  if (check !== undefined && check(a)) {
131    return check(b);
132  }
133  // Manipulated Symbol.toStringTag.
134  for (const check of [
135    isUint16Array,
136    isUint32Array,
137    isInt8Array,
138    isInt16Array,
139    isInt32Array,
140    isFloat32Array,
141    isFloat64Array,
142    isBigInt64Array,
143    isBigUint64Array,
144    isUint8ClampedArray,
145    isUint8Array,
146  ]) {
147    if (check(a)) {
148      return check(b);
149    }
150  }
151  /* c8 ignore next 4 */
152  assert.fail(
153    `Unknown TypedArray type checking ${a[SymbolToStringTag]} ${a}\n` +
154    `and ${b[SymbolToStringTag]} ${b}`
155  );
156}
157
158// Notes: Type tags are historical [[Class]] properties that can be set by
159// FunctionTemplate::SetClassName() in C++ or Symbol.toStringTag in JS
160// and retrieved using Object.prototype.toString.call(obj) in JS
161// See https://tc39.github.io/ecma262/#sec-object.prototype.tostring
162// for a list of tags pre-defined in the spec.
163// There are some unspecified tags in the wild too (e.g. typed array tags).
164// Since tags can be altered, they only serve fast failures
165//
166// For strict comparison, objects should have
167// a) The same built-in type tag.
168// b) The same prototypes.
169
170function innerDeepEqual(val1, val2, strict, memos) {
171  // All identical values are equivalent, as determined by ===.
172  if (val1 === val2) {
173    if (val1 !== 0)
174      return true;
175    return strict ? ObjectIs(val1, val2) : true;
176  }
177
178  // Check more closely if val1 and val2 are equal.
179  if (strict) {
180    if (typeof val1 !== 'object') {
181      return typeof val1 === 'number' && NumberIsNaN(val1) &&
182        NumberIsNaN(val2);
183    }
184    if (typeof val2 !== 'object' || val1 === null || val2 === null) {
185      return false;
186    }
187    if (ObjectGetPrototypeOf(val1) !== ObjectGetPrototypeOf(val2)) {
188      return false;
189    }
190  } else {
191    if (val1 === null || typeof val1 !== 'object') {
192      if (val2 === null || typeof val2 !== 'object') {
193        // eslint-disable-next-line eqeqeq
194        return val1 == val2 || (NumberIsNaN(val1) && NumberIsNaN(val2));
195      }
196      return false;
197    }
198    if (val2 === null || typeof val2 !== 'object') {
199      return false;
200    }
201  }
202  const val1Tag = ObjectPrototypeToString(val1);
203  const val2Tag = ObjectPrototypeToString(val2);
204
205  if (val1Tag !== val2Tag) {
206    return false;
207  }
208
209  if (ArrayIsArray(val1)) {
210    // Check for sparse arrays and general fast path
211    if (!ArrayIsArray(val2) || val1.length !== val2.length) {
212      return false;
213    }
214    const filter = strict ? ONLY_ENUMERABLE : ONLY_ENUMERABLE | SKIP_SYMBOLS;
215    const keys1 = getOwnNonIndexProperties(val1, filter);
216    const keys2 = getOwnNonIndexProperties(val2, filter);
217    if (keys1.length !== keys2.length) {
218      return false;
219    }
220    return keyCheck(val1, val2, strict, memos, kIsArray, keys1);
221  } else if (val1Tag === '[object Object]') {
222    return keyCheck(val1, val2, strict, memos, kNoIterator);
223  } else if (isDate(val1)) {
224    if (!isDate(val2) ||
225        DatePrototypeGetTime(val1) !== DatePrototypeGetTime(val2)) {
226      return false;
227    }
228  } else if (isRegExp(val1)) {
229    if (!isRegExp(val2) || !areSimilarRegExps(val1, val2)) {
230      return false;
231    }
232  } else if (isNativeError(val1) || val1 instanceof Error) {
233    // Do not compare the stack as it might differ even though the error itself
234    // is otherwise identical.
235    if ((!isNativeError(val2) && !(val2 instanceof Error)) ||
236        val1.message !== val2.message ||
237        val1.name !== val2.name) {
238      return false;
239    }
240  } else if (isArrayBufferView(val1)) {
241    if (!isIdenticalTypedArrayType(val1, val2))
242      return false;
243    if (!strict && (isFloat32Array(val1) || isFloat64Array(val1))) {
244      if (!areSimilarFloatArrays(val1, val2)) {
245        return false;
246      }
247    } else if (!areSimilarTypedArrays(val1, val2)) {
248      return false;
249    }
250    // Buffer.compare returns true, so val1.length === val2.length. If they both
251    // only contain numeric keys, we don't need to exam further than checking
252    // the symbols.
253    const filter = strict ? ONLY_ENUMERABLE : ONLY_ENUMERABLE | SKIP_SYMBOLS;
254    const keys1 = getOwnNonIndexProperties(val1, filter);
255    const keys2 = getOwnNonIndexProperties(val2, filter);
256    if (keys1.length !== keys2.length) {
257      return false;
258    }
259    return keyCheck(val1, val2, strict, memos, kNoIterator, keys1);
260  } else if (isSet(val1)) {
261    if (!isSet(val2) || val1.size !== val2.size) {
262      return false;
263    }
264    return keyCheck(val1, val2, strict, memos, kIsSet);
265  } else if (isMap(val1)) {
266    if (!isMap(val2) || val1.size !== val2.size) {
267      return false;
268    }
269    return keyCheck(val1, val2, strict, memos, kIsMap);
270  } else if (isAnyArrayBuffer(val1)) {
271    if (!isAnyArrayBuffer(val2) || !areEqualArrayBuffers(val1, val2)) {
272      return false;
273    }
274  } else if (isBoxedPrimitive(val1)) {
275    if (!isEqualBoxedPrimitive(val1, val2)) {
276      return false;
277    }
278  } else if (ArrayIsArray(val2) ||
279             isArrayBufferView(val2) ||
280             isSet(val2) ||
281             isMap(val2) ||
282             isDate(val2) ||
283             isRegExp(val2) ||
284             isAnyArrayBuffer(val2) ||
285             isBoxedPrimitive(val2) ||
286             isNativeError(val2) ||
287             val2 instanceof Error) {
288    return false;
289  }
290  return keyCheck(val1, val2, strict, memos, kNoIterator);
291}
292
293function getEnumerables(val, keys) {
294  return keys.filter((k) => ObjectPrototypePropertyIsEnumerable(val, k));
295}
296
297function keyCheck(val1, val2, strict, memos, iterationType, aKeys) {
298  // For all remaining Object pairs, including Array, objects and Maps,
299  // equivalence is determined by having:
300  // a) The same number of owned enumerable properties
301  // b) The same set of keys/indexes (although not necessarily the same order)
302  // c) Equivalent values for every corresponding key/index
303  // d) For Sets and Maps, equal contents
304  // Note: this accounts for both named and indexed properties on Arrays.
305  if (arguments.length === 5) {
306    aKeys = ObjectKeys(val1);
307    const bKeys = ObjectKeys(val2);
308
309    // The pair must have the same number of owned properties.
310    if (aKeys.length !== bKeys.length) {
311      return false;
312    }
313  }
314
315  // Cheap key test
316  let i = 0;
317  for (; i < aKeys.length; i++) {
318    if (!ObjectPrototypePropertyIsEnumerable(val2, aKeys[i])) {
319      return false;
320    }
321  }
322
323  if (strict && arguments.length === 5) {
324    const symbolKeysA = ObjectGetOwnPropertySymbols(val1);
325    if (symbolKeysA.length !== 0) {
326      let count = 0;
327      for (i = 0; i < symbolKeysA.length; i++) {
328        const key = symbolKeysA[i];
329        if (ObjectPrototypePropertyIsEnumerable(val1, key)) {
330          if (!ObjectPrototypePropertyIsEnumerable(val2, key)) {
331            return false;
332          }
333          aKeys.push(key);
334          count++;
335        } else if (ObjectPrototypePropertyIsEnumerable(val2, key)) {
336          return false;
337        }
338      }
339      const symbolKeysB = ObjectGetOwnPropertySymbols(val2);
340      if (symbolKeysA.length !== symbolKeysB.length &&
341          getEnumerables(val2, symbolKeysB).length !== count) {
342        return false;
343      }
344    } else {
345      const symbolKeysB = ObjectGetOwnPropertySymbols(val2);
346      if (symbolKeysB.length !== 0 &&
347          getEnumerables(val2, symbolKeysB).length !== 0) {
348        return false;
349      }
350    }
351  }
352
353  if (aKeys.length === 0 &&
354      (iterationType === kNoIterator ||
355        (iterationType === kIsArray && val1.length === 0) ||
356        val1.size === 0)) {
357    return true;
358  }
359
360  // Use memos to handle cycles.
361  if (memos === undefined) {
362    memos = {
363      val1: new Map(),
364      val2: new Map(),
365      position: 0
366    };
367  } else {
368    // We prevent up to two map.has(x) calls by directly retrieving the value
369    // and checking for undefined. The map can only contain numbers, so it is
370    // safe to check for undefined only.
371    const val2MemoA = memos.val1.get(val1);
372    if (val2MemoA !== undefined) {
373      const val2MemoB = memos.val2.get(val2);
374      if (val2MemoB !== undefined) {
375        return val2MemoA === val2MemoB;
376      }
377    }
378    memos.position++;
379  }
380
381  memos.val1.set(val1, memos.position);
382  memos.val2.set(val2, memos.position);
383
384  const areEq = objEquiv(val1, val2, strict, aKeys, memos, iterationType);
385
386  memos.val1.delete(val1);
387  memos.val2.delete(val2);
388
389  return areEq;
390}
391
392function setHasEqualElement(set, val1, strict, memo) {
393  // Go looking.
394  for (const val2 of set) {
395    if (innerDeepEqual(val1, val2, strict, memo)) {
396      // Remove the matching element to make sure we do not check that again.
397      set.delete(val2);
398      return true;
399    }
400  }
401
402  return false;
403}
404
405// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#Loose_equality_using
406// Sadly it is not possible to detect corresponding values properly in case the
407// type is a string, number, bigint or boolean. The reason is that those values
408// can match lots of different string values (e.g., 1n == '+00001').
409function findLooseMatchingPrimitives(prim) {
410  switch (typeof prim) {
411    case 'undefined':
412      return null;
413    case 'object': // Only pass in null as object!
414      return undefined;
415    case 'symbol':
416      return false;
417    case 'string':
418      prim = +prim;
419      // Loose equal entries exist only if the string is possible to convert to
420      // a regular number and not NaN.
421      // Fall through
422    case 'number':
423      if (NumberIsNaN(prim)) {
424        return false;
425      }
426  }
427  return true;
428}
429
430function setMightHaveLoosePrim(a, b, prim) {
431  const altValue = findLooseMatchingPrimitives(prim);
432  if (altValue != null)
433    return altValue;
434
435  return b.has(altValue) && !a.has(altValue);
436}
437
438function mapMightHaveLoosePrim(a, b, prim, item, memo) {
439  const altValue = findLooseMatchingPrimitives(prim);
440  if (altValue != null) {
441    return altValue;
442  }
443  const curB = b.get(altValue);
444  if ((curB === undefined && !b.has(altValue)) ||
445      !innerDeepEqual(item, curB, false, memo)) {
446    return false;
447  }
448  return !a.has(altValue) && innerDeepEqual(item, curB, false, memo);
449}
450
451function setEquiv(a, b, strict, memo) {
452  // This is a lazily initiated Set of entries which have to be compared
453  // pairwise.
454  let set = null;
455  for (const val of a) {
456    // Note: Checking for the objects first improves the performance for object
457    // heavy sets but it is a minor slow down for primitives. As they are fast
458    // to check this improves the worst case scenario instead.
459    if (typeof val === 'object' && val !== null) {
460      if (set === null) {
461        set = new Set();
462      }
463      // If the specified value doesn't exist in the second set it's a non-null
464      // object (or non strict only: a not matching primitive) we'll need to go
465      // hunting for something that's deep-(strict-)equal to it. To make this
466      // O(n log n) complexity we have to copy these values in a new set first.
467      set.add(val);
468    } else if (!b.has(val)) {
469      if (strict)
470        return false;
471
472      // Fast path to detect missing string, symbol, undefined and null values.
473      if (!setMightHaveLoosePrim(a, b, val)) {
474        return false;
475      }
476
477      if (set === null) {
478        set = new Set();
479      }
480      set.add(val);
481    }
482  }
483
484  if (set !== null) {
485    for (const val of b) {
486      // We have to check if a primitive value is already
487      // matching and only if it's not, go hunting for it.
488      if (typeof val === 'object' && val !== null) {
489        if (!setHasEqualElement(set, val, strict, memo))
490          return false;
491      } else if (!strict &&
492                 !a.has(val) &&
493                 !setHasEqualElement(set, val, strict, memo)) {
494        return false;
495      }
496    }
497    return set.size === 0;
498  }
499
500  return true;
501}
502
503function mapHasEqualEntry(set, map, key1, item1, strict, memo) {
504  // To be able to handle cases like:
505  //   Map([[{}, 'a'], [{}, 'b']]) vs Map([[{}, 'b'], [{}, 'a']])
506  // ... we need to consider *all* matching keys, not just the first we find.
507  for (const key2 of set) {
508    if (innerDeepEqual(key1, key2, strict, memo) &&
509      innerDeepEqual(item1, map.get(key2), strict, memo)) {
510      set.delete(key2);
511      return true;
512    }
513  }
514
515  return false;
516}
517
518function mapEquiv(a, b, strict, memo) {
519  let set = null;
520
521  for (const { 0: key, 1: item1 } of a) {
522    if (typeof key === 'object' && key !== null) {
523      if (set === null) {
524        set = new Set();
525      }
526      set.add(key);
527    } else {
528      // By directly retrieving the value we prevent another b.has(key) check in
529      // almost all possible cases.
530      const item2 = b.get(key);
531      if (((item2 === undefined && !b.has(key)) ||
532          !innerDeepEqual(item1, item2, strict, memo))) {
533        if (strict)
534          return false;
535        // Fast path to detect missing string, symbol, undefined and null
536        // keys.
537        if (!mapMightHaveLoosePrim(a, b, key, item1, memo))
538          return false;
539        if (set === null) {
540          set = new Set();
541        }
542        set.add(key);
543      }
544    }
545  }
546
547  if (set !== null) {
548    for (const { 0: key, 1: item } of b) {
549      if (typeof key === 'object' && key !== null) {
550        if (!mapHasEqualEntry(set, a, key, item, strict, memo))
551          return false;
552      } else if (!strict &&
553                 (!a.has(key) ||
554                   !innerDeepEqual(a.get(key), item, false, memo)) &&
555                 !mapHasEqualEntry(set, a, key, item, false, memo)) {
556        return false;
557      }
558    }
559    return set.size === 0;
560  }
561
562  return true;
563}
564
565function objEquiv(a, b, strict, keys, memos, iterationType) {
566  // Sets and maps don't have their entries accessible via normal object
567  // properties.
568  let i = 0;
569
570  if (iterationType === kIsSet) {
571    if (!setEquiv(a, b, strict, memos)) {
572      return false;
573    }
574  } else if (iterationType === kIsMap) {
575    if (!mapEquiv(a, b, strict, memos)) {
576      return false;
577    }
578  } else if (iterationType === kIsArray) {
579    for (; i < a.length; i++) {
580      if (ObjectPrototypeHasOwnProperty(a, i)) {
581        if (!ObjectPrototypeHasOwnProperty(b, i) ||
582            !innerDeepEqual(a[i], b[i], strict, memos)) {
583          return false;
584        }
585      } else if (ObjectPrototypeHasOwnProperty(b, i)) {
586        return false;
587      } else {
588        // Array is sparse.
589        const keysA = ObjectKeys(a);
590        for (; i < keysA.length; i++) {
591          const key = keysA[i];
592          if (!ObjectPrototypeHasOwnProperty(b, key) ||
593              !innerDeepEqual(a[key], b[key], strict, memos)) {
594            return false;
595          }
596        }
597        if (keysA.length !== ObjectKeys(b).length) {
598          return false;
599        }
600        return true;
601      }
602    }
603  }
604
605  // The pair must have equivalent values for every corresponding key.
606  // Possibly expensive deep test:
607  for (i = 0; i < keys.length; i++) {
608    const key = keys[i];
609    if (!innerDeepEqual(a[key], b[key], strict, memos)) {
610      return false;
611    }
612  }
613  return true;
614}
615
616function isDeepEqual(val1, val2) {
617  return innerDeepEqual(val1, val2, kLoose);
618}
619
620function isDeepStrictEqual(val1, val2) {
621  return innerDeepEqual(val1, val2, kStrict);
622}
623
624module.exports = {
625  isDeepEqual,
626  isDeepStrictEqual
627};
628