github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/reflect/deepequal.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Deep equality test via reflection 6 7 package reflect 8 9 import "unsafe" 10 11 // During deepValueEqual, must keep track of checks that are 12 // in progress. The comparison algorithm assumes that all 13 // checks in progress are true when it reencounters them. 14 // Visited comparisons are stored in a map indexed by visit. 15 type visit struct { 16 a1 unsafe.Pointer 17 a2 unsafe.Pointer 18 typ *rawType 19 } 20 21 // Tests for deep equality using reflected types. The map argument tracks 22 // comparisons that have already been seen, which allows short circuiting on 23 // recursive types. 24 func deepValueEqual(v1, v2 Value, visited map[visit]struct{}) bool { 25 if !v1.IsValid() || !v2.IsValid() { 26 return v1.IsValid() == v2.IsValid() 27 } 28 if v1.typecode != v2.typecode { 29 return false 30 } 31 32 // We want to avoid putting more in the visited map than we need to. 33 // For any possible reference cycle that might be encountered, 34 // hard(v1, v2) needs to return true for at least one of the types in the cycle, 35 // and it's safe and valid to get Value's internal pointer. 36 hard := func(v1, v2 Value) bool { 37 switch v1.Kind() { 38 case Map, Slice, Ptr, Interface: 39 // Nil pointers cannot be cyclic. Avoid putting them in the visited map. 40 return !v1.IsNil() && !v2.IsNil() 41 } 42 return false 43 } 44 45 if hard(v1, v2) { 46 addr1 := v1.pointer() 47 addr2 := v2.pointer() 48 if uintptr(addr1) > uintptr(addr2) { 49 // Canonicalize order to reduce number of entries in visited. 50 // Assumes non-moving garbage collector. 51 addr1, addr2 = addr2, addr1 52 } 53 54 // Short circuit if references are already seen. 55 v := visit{addr1, addr2, v1.typecode} 56 if _, ok := visited[v]; ok { 57 return true 58 } 59 60 // Remember for later. 61 visited[v] = struct{}{} 62 } 63 64 switch v1.Kind() { 65 case Array: 66 for i := 0; i < v1.Len(); i++ { 67 if !deepValueEqual(v1.Index(i), v2.Index(i), visited) { 68 return false 69 } 70 } 71 return true 72 case Slice: 73 if v1.IsNil() != v2.IsNil() { 74 return false 75 } 76 if v1.Len() != v2.Len() { 77 return false 78 } 79 if v1.UnsafePointer() == v2.UnsafePointer() { 80 return true 81 } 82 for i := 0; i < v1.Len(); i++ { 83 if !deepValueEqual(v1.Index(i), v2.Index(i), visited) { 84 return false 85 } 86 } 87 return true 88 case Interface: 89 if v1.IsNil() || v2.IsNil() { 90 return v1.IsNil() == v2.IsNil() 91 } 92 return deepValueEqual(v1.Elem(), v2.Elem(), visited) 93 case Ptr: 94 if v1.UnsafePointer() == v2.UnsafePointer() { 95 return true 96 } 97 return deepValueEqual(v1.Elem(), v2.Elem(), visited) 98 case Struct: 99 for i, n := 0, v1.NumField(); i < n; i++ { 100 if !deepValueEqual(v1.Field(i), v2.Field(i), visited) { 101 return false 102 } 103 } 104 return true 105 case Map: 106 if v1.IsNil() != v2.IsNil() { 107 return false 108 } 109 if v1.Len() != v2.Len() { 110 return false 111 } 112 if v1.UnsafePointer() == v2.UnsafePointer() { 113 return true 114 } 115 for _, k := range v1.MapKeys() { 116 val1 := v1.MapIndex(k) 117 val2 := v2.MapIndex(k) 118 if !val1.IsValid() || !val2.IsValid() || !deepValueEqual(val1, val2, visited) { 119 return false 120 } 121 } 122 return true 123 case Func: 124 if v1.IsNil() && v2.IsNil() { 125 return true 126 } 127 // Can't do better than this: 128 return false 129 default: 130 // Normal equality suffices 131 return valueInterfaceUnsafe(v1) == valueInterfaceUnsafe(v2) 132 } 133 } 134 135 // DeepEqual reports whether x and y are “deeply equal”, defined as follows. 136 // Two values of identical type are deeply equal if one of the following cases applies. 137 // Values of distinct types are never deeply equal. 138 // 139 // Array values are deeply equal when their corresponding elements are deeply equal. 140 // 141 // Struct values are deeply equal if their corresponding fields, 142 // both exported and unexported, are deeply equal. 143 // 144 // Func values are deeply equal if both are nil; otherwise they are not deeply equal. 145 // 146 // Interface values are deeply equal if they hold deeply equal concrete values. 147 // 148 // Map values are deeply equal when all of the following are true: 149 // they are both nil or both non-nil, they have the same length, 150 // and either they are the same map object or their corresponding keys 151 // (matched using Go equality) map to deeply equal values. 152 // 153 // Pointer values are deeply equal if they are equal using Go's == operator 154 // or if they point to deeply equal values. 155 // 156 // Slice values are deeply equal when all of the following are true: 157 // they are both nil or both non-nil, they have the same length, 158 // and either they point to the same initial entry of the same underlying array 159 // (that is, &x[0] == &y[0]) or their corresponding elements (up to length) are deeply equal. 160 // Note that a non-nil empty slice and a nil slice (for example, []byte{} and []byte(nil)) 161 // are not deeply equal. 162 // 163 // Other values - numbers, bools, strings, and channels - are deeply equal 164 // if they are equal using Go's == operator. 165 // 166 // In general DeepEqual is a recursive relaxation of Go's == operator. 167 // However, this idea is impossible to implement without some inconsistency. 168 // Specifically, it is possible for a value to be unequal to itself, 169 // either because it is of func type (uncomparable in general) 170 // or because it is a floating-point NaN value (not equal to itself in floating-point comparison), 171 // or because it is an array, struct, or interface containing 172 // such a value. 173 // On the other hand, pointer values are always equal to themselves, 174 // even if they point at or contain such problematic values, 175 // because they compare equal using Go's == operator, and that 176 // is a sufficient condition to be deeply equal, regardless of content. 177 // DeepEqual has been defined so that the same short-cut applies 178 // to slices and maps: if x and y are the same slice or the same map, 179 // they are deeply equal regardless of content. 180 // 181 // As DeepEqual traverses the data values it may find a cycle. The 182 // second and subsequent times that DeepEqual compares two pointer 183 // values that have been compared before, it treats the values as 184 // equal rather than examining the values to which they point. 185 // This ensures that DeepEqual terminates. 186 func DeepEqual(x, y interface{}) bool { 187 if x == nil || y == nil { 188 return x == y 189 } 190 v1 := ValueOf(x) 191 v2 := ValueOf(y) 192 if v1.typecode != v2.typecode { 193 return false 194 } 195 return deepValueEqual(v1, v2, make(map[visit]struct{})) 196 }