|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +/// Dumps an object's contents using its mirror to the specified output stream. |
| 14 | +@discardableResult |
| 15 | +public func dump<T, TargetStream : TextOutputStream>( |
| 16 | + _ value: T, |
| 17 | + to target: inout TargetStream, |
| 18 | + name: String? = nil, |
| 19 | + indent: Int = 0, |
| 20 | + maxDepth: Int = .max, |
| 21 | + maxItems: Int = .max |
| 22 | +) -> T { |
| 23 | + var maxItemCounter = maxItems |
| 24 | + var visitedItems = [ObjectIdentifier : Int]() |
| 25 | + target._lock() |
| 26 | + defer { target._unlock() } |
| 27 | + _dump_unlocked( |
| 28 | + value, |
| 29 | + to: &target, |
| 30 | + name: name, |
| 31 | + indent: indent, |
| 32 | + maxDepth: maxDepth, |
| 33 | + maxItemCounter: &maxItemCounter, |
| 34 | + visitedItems: &visitedItems) |
| 35 | + return value |
| 36 | +} |
| 37 | + |
| 38 | +/// Dumps an object's contents using its mirror to standard output. |
| 39 | +@discardableResult |
| 40 | +public func dump<T>( |
| 41 | + _ value: T, |
| 42 | + name: String? = nil, |
| 43 | + indent: Int = 0, |
| 44 | + maxDepth: Int = .max, |
| 45 | + maxItems: Int = .max |
| 46 | +) -> T { |
| 47 | + var stdoutStream = _Stdout() |
| 48 | + return dump( |
| 49 | + value, |
| 50 | + to: &stdoutStream, |
| 51 | + name: name, |
| 52 | + indent: indent, |
| 53 | + maxDepth: maxDepth, |
| 54 | + maxItems: maxItems) |
| 55 | +} |
| 56 | + |
| 57 | +/// Dump an object's contents. User code should use dump(). |
| 58 | +internal func _dump_unlocked<TargetStream : TextOutputStream>( |
| 59 | + _ value: Any, |
| 60 | + to target: inout TargetStream, |
| 61 | + name: String?, |
| 62 | + indent: Int, |
| 63 | + maxDepth: Int, |
| 64 | + maxItemCounter: inout Int, |
| 65 | + visitedItems: inout [ObjectIdentifier : Int] |
| 66 | +) { |
| 67 | + guard maxItemCounter > 0 else { return } |
| 68 | + maxItemCounter -= 1 |
| 69 | + |
| 70 | + for _ in 0..<indent { target.write(" ") } |
| 71 | + |
| 72 | + let mirror = Mirror(reflecting: value) |
| 73 | + let count = mirror.children.count |
| 74 | + let bullet = count == 0 ? "-" |
| 75 | + : maxDepth <= 0 ? "▹" : "▿" |
| 76 | + target.write(bullet) |
| 77 | + target.write(" ") |
| 78 | + |
| 79 | + if let nam = name { |
| 80 | + target.write(nam) |
| 81 | + target.write(": ") |
| 82 | + } |
| 83 | + // This takes the place of the old mirror API's 'summary' property |
| 84 | + _dumpPrint_unlocked(value, mirror, &target) |
| 85 | + |
| 86 | + let id: ObjectIdentifier? |
| 87 | + if type(of: value) is AnyObject.Type { |
| 88 | + // Object is a class (but not an ObjC-bridged struct) |
| 89 | + id = ObjectIdentifier(_unsafeDowncastToAnyObject(fromAny: value)) |
| 90 | + } else if let metatypeInstance = value as? Any.Type { |
| 91 | + // Object is a metatype |
| 92 | + id = ObjectIdentifier(metatypeInstance) |
| 93 | + } else { |
| 94 | + id = nil |
| 95 | + } |
| 96 | + if let theId = id { |
| 97 | + if let previous = visitedItems[theId] { |
| 98 | + target.write(" #") |
| 99 | + _print_unlocked(previous, &target) |
| 100 | + target.write("\n") |
| 101 | + return |
| 102 | + } |
| 103 | + let identifier = visitedItems.count |
| 104 | + visitedItems[theId] = identifier |
| 105 | + target.write(" #") |
| 106 | + _print_unlocked(identifier, &target) |
| 107 | + } |
| 108 | + |
| 109 | + target.write("\n") |
| 110 | + |
| 111 | + guard maxDepth > 0 else { return } |
| 112 | + |
| 113 | + if let superclassMirror = mirror.superclassMirror { |
| 114 | + _dumpSuperclass_unlocked( |
| 115 | + mirror: superclassMirror, |
| 116 | + to: &target, |
| 117 | + indent: indent + 2, |
| 118 | + maxDepth: maxDepth - 1, |
| 119 | + maxItemCounter: &maxItemCounter, |
| 120 | + visitedItems: &visitedItems) |
| 121 | + } |
| 122 | + |
| 123 | + var currentIndex = mirror.children.startIndex |
| 124 | + for i in 0..<count { |
| 125 | + if maxItemCounter <= 0 { |
| 126 | + for _ in 0..<(indent+4) { |
| 127 | + _print_unlocked(" ", &target) |
| 128 | + } |
| 129 | + let remainder = count - i |
| 130 | + target.write("(") |
| 131 | + _print_unlocked(remainder, &target) |
| 132 | + if i > 0 { target.write(" more") } |
| 133 | + if remainder == 1 { |
| 134 | + target.write(" child)\n") |
| 135 | + } else { |
| 136 | + target.write(" children)\n") |
| 137 | + } |
| 138 | + return |
| 139 | + } |
| 140 | + |
| 141 | + let (name, child) = mirror.children[currentIndex] |
| 142 | + mirror.children.formIndex(after: ¤tIndex) |
| 143 | + _dump_unlocked( |
| 144 | + child, |
| 145 | + to: &target, |
| 146 | + name: name, |
| 147 | + indent: indent + 2, |
| 148 | + maxDepth: maxDepth - 1, |
| 149 | + maxItemCounter: &maxItemCounter, |
| 150 | + visitedItems: &visitedItems) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +/// Dump information about an object's superclass, given a mirror reflecting |
| 155 | +/// that superclass. |
| 156 | +internal func _dumpSuperclass_unlocked<TargetStream : TextOutputStream>( |
| 157 | + mirror: Mirror, |
| 158 | + to target: inout TargetStream, |
| 159 | + indent: Int, |
| 160 | + maxDepth: Int, |
| 161 | + maxItemCounter: inout Int, |
| 162 | + visitedItems: inout [ObjectIdentifier : Int] |
| 163 | +) { |
| 164 | + guard maxItemCounter > 0 else { return } |
| 165 | + maxItemCounter -= 1 |
| 166 | + |
| 167 | + for _ in 0..<indent { target.write(" ") } |
| 168 | + |
| 169 | + let count = mirror.children.count |
| 170 | + let bullet = count == 0 ? "-" |
| 171 | + : maxDepth <= 0 ? "▹" : "▿" |
| 172 | + target.write(bullet) |
| 173 | + target.write(" super: ") |
| 174 | + _debugPrint_unlocked(mirror.subjectType, &target) |
| 175 | + target.write("\n") |
| 176 | + |
| 177 | + guard maxDepth > 0 else { return } |
| 178 | + |
| 179 | + if let superclassMirror = mirror.superclassMirror { |
| 180 | + _dumpSuperclass_unlocked( |
| 181 | + mirror: superclassMirror, |
| 182 | + to: &target, |
| 183 | + indent: indent + 2, |
| 184 | + maxDepth: maxDepth - 1, |
| 185 | + maxItemCounter: &maxItemCounter, |
| 186 | + visitedItems: &visitedItems) |
| 187 | + } |
| 188 | + |
| 189 | + var currentIndex = mirror.children.startIndex |
| 190 | + for i in 0..<count { |
| 191 | + if maxItemCounter <= 0 { |
| 192 | + for _ in 0..<(indent+4) { |
| 193 | + target.write(" ") |
| 194 | + } |
| 195 | + let remainder = count - i |
| 196 | + target.write("(") |
| 197 | + _print_unlocked(remainder, &target) |
| 198 | + if i > 0 { target.write(" more") } |
| 199 | + if remainder == 1 { |
| 200 | + target.write(" child)\n") |
| 201 | + } else { |
| 202 | + target.write(" children)\n") |
| 203 | + } |
| 204 | + return |
| 205 | + } |
| 206 | + |
| 207 | + let (name, child) = mirror.children[currentIndex] |
| 208 | + mirror.children.formIndex(after: ¤tIndex) |
| 209 | + _dump_unlocked( |
| 210 | + child, |
| 211 | + to: &target, |
| 212 | + name: name, |
| 213 | + indent: indent + 2, |
| 214 | + maxDepth: maxDepth - 1, |
| 215 | + maxItemCounter: &maxItemCounter, |
| 216 | + visitedItems: &visitedItems) |
| 217 | + } |
| 218 | +} |
| 219 | + |
0 commit comments