All files / src/internal cycle.ts

100% Statements 39/39
96.29% Branches 26/27
100% Functions 5/5
100% Lines 32/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104                                                                865x 865x 1677572x 1677572x 1677041x 1677041x 1677041x 1677041x 1677041x   1677041x       1677041x 18599x             108x       571003x               572252x 11618x 11618x 572252x                             286668x 283612x 283612x 283612x 283612x 283612x 282444x 282444x 282444x 282444x   282444x   283559x   283612x 283612x      
// In-progress guards that keep equality, hashing and printing of self-containing collections from
// recursing forever. Each operation has its own guard, so user code running inside one operation
// (an element's `toString` that compares or hashes the collection) gets a real result from another.
 
import {
  createWeakMap,
  createWeakSet,
  weakMapGet,
  weakMapSet,
  weakSetAdd,
  weakSetDelete,
  weakSetHas,
} from './intrinsics'
 
/**
 * Runs `body` with `target` marked as in progress; if `target` is already in progress under the
 * same guard (a cycle), returns `onCycle()` instead. The mark is removed when `body` returns or
 * throws, and every mark is dropped when the outermost guarded call returns or throws.
 *
 * @internal
 */
export type Guard = <T>(target: object, onCycle: () => T, body: () => T) => T
 
/**
 * Returns a new guard with its own in-progress set, created on first use.
 *
 * Time: O(1); each guarded call is O(1) plus `body`.
 *
 * @internal
 */
export function createGuard(): Guard {
  let inProgress: WeakSet<object> | undefined
  let depth = 0
  return <T>(target: object, onCycle: () => T, body: () => T): T => {
    if (inProgress === undefined) inProgress = createWeakSet<object>()
    if (weakSetHas(inProgress, target)) return onCycle()
    const marks = inProgress
    weakSetAdd(marks, target)
    depth++
    try {
      return body()
    } finally {
      depth--
      // The outermost call drops every mark with a plain assignment, which cannot fail. Removing
      // one mark is a call, and at the edge of the stack (unwinding a RangeError) it can itself
      // overflow; the mark it leaves behind is dropped here.
      if (depth === 0) inProgress = undefined
      else weakSetDelete(marks, target)
    }
  }
}
 
/** For each collection proven equal to others, each partner and both revisions at that time. */
let proven: WeakMap<object, WeakMap<object, readonly [number, number]>> | undefined
let comparisonDepth = 0
 
/** A collection's `revision`, which every change to its contents advances. */
function revisionOf(collection: object): number {
  return (collection as { readonly revision: number }).revision
}
 
/**
 * Whether `a` was found equal to `b` during the current outermost comparison, and neither has
 * changed since.
 */
function isProven(a: object, b: object): boolean {
  if (proven === undefined) return false
  const partners = weakMapGet(proven, a)
  const seen = partners === undefined ? undefined : weakMapGet(partners, b)
  return seen !== undefined && seen[0] === revisionOf(a) && seen[1] === revisionOf(b)
}
 
/**
 * Runs `compare` for the collections `a` and `b` and remembers a `true` result until the outermost
 * comparison returns, so a pair met again (either way round) and unchanged since is answered
 * without comparing again. Set and map equality check each member from both sides, so without
 * this, the work doubles at every level of nesting. The memory is dropped when the outermost call
 * returns or throws.
 *
 * Time: O(1) plus `compare` on a pair not yet proven equal.
 *
 * @internal
 */
export function rememberEqual(a: object, b: object, compare: () => boolean): boolean {
  if (isProven(a, b) || isProven(b, a)) return true
  if (comparisonDepth === 0) proven = createWeakMap()
  comparisonDepth++
  try {
    const result = compare()
    if (result && proven !== undefined) {
      let partners = weakMapGet(proven, a)
      Eif (partners === undefined) {
        partners = createWeakMap<object, readonly [number, number]>()
        weakMapSet(proven, a, partners)
      }
      weakMapSet(partners, b, [revisionOf(a), revisionOf(b)] as const)
    }
    return result
  } finally {
    comparisonDepth--
    if (comparisonDepth === 0) proven = undefined
  }
}