(self: object, that: object)
| 210 | } |
| 211 | |
| 212 | function compareObjects(self: object, that: object): boolean { |
| 213 | const depth = pathLeft.length |
| 214 | const prev = seen.get(self) |
| 215 | if (prev === that) return true |
| 216 | let known: WeakMap<object, boolean> | undefined |
| 217 | if (!depth || prev !== undefined) { |
| 218 | known = results.get(self) |
| 219 | const cached = known?.get(that) |
| 220 | if (cached !== undefined) return cached |
| 221 | } |
| 222 | let revisited = false |
| 223 | for (let i = depth; i-- > 0;) { |
| 224 | const sameLeft = pathLeft[i] === self |
| 225 | if (sameLeft || pathRight[i] === that) { |
| 226 | addBackEdge() |
| 227 | // A repeated pair closes a cycle under coinductive equality. |
| 228 | if (sameLeft && pathRight[i] === that) return true |
| 229 | revisited = true |
| 230 | } |
| 231 | } |
| 232 | // Reject mismatched cycle lengths instead of unrolling both loops. |
| 233 | if (revisited && Hash.hash(self) !== Hash.hash(that)) return false |
| 234 | const seenBack = backEdges |
| 235 | pathLeft.push(self) |
| 236 | pathRight.push(that) |
| 237 | let result: boolean |
| 238 | try { |
| 239 | result = compareStructure(self, that) |
| 240 | } finally { |
| 241 | pathLeft.pop() |
| 242 | pathRight.pop() |
| 243 | if (!depth) seen.clear() |
| 244 | } |
| 245 | // Cyclic hashes depend on entry point; verify hash agreement. |
| 246 | if (seenBack !== backEdges) return result && Hash.hash(self) === Hash.hash(that) |
| 247 | // Only cache acyclic results so nested lookups never skip a back-edge. |
| 248 | if (depth && prev === undefined) { |
| 249 | if (result) seen.set(self, that) |
| 250 | } else { |
| 251 | if (!known) results.set(self, known = new WeakMap()) |
| 252 | known.set(that, result) |
| 253 | } |
| 254 | return result |
| 255 | } |
| 256 | |
| 257 | // The pairs on the current comparison's path (the coinductive assumptions). |
| 258 | const pathLeft: Array<object> = [] |
no test coverage detected