|
13 | 13 | /// } |
14 | 14 | /// ``` |
15 | 15 | public struct JSException: Error, Equatable, CustomStringConvertible { |
| 16 | + #if compiler(>=6.3) |
| 17 | + /// Boxes the exception payload in a class so `JSException` stays within the direct |
| 18 | + /// typed-error convention on wasm32. The boxed layout crashes the Swift 6.1 wasm IRGen and is unverified on 6.2, |
| 19 | + /// so older compilers keep the original stored properties. |
| 20 | + private final class Storage { |
| 21 | + let thrownValue: JSValue |
| 22 | + let description: String |
| 23 | + let stack: String? |
| 24 | + |
| 25 | + init(thrownValue: JSValue, description: String, stack: String?) { |
| 26 | + self.thrownValue = thrownValue |
| 27 | + self.description = description |
| 28 | + self.stack = stack |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + /// The boxed payload of the exception. |
| 33 | + /// |
| 34 | + /// Marked as `nonisolated(unsafe)` to satisfy `Sendable` requirement |
| 35 | + /// from `Error` protocol. |
| 36 | + private nonisolated(unsafe) let storage: Storage |
| 37 | + |
| 38 | + /// The value thrown from JavaScript. |
| 39 | + /// This can be any JavaScript value (error object, string, number, etc.). |
| 40 | + public var thrownValue: JSValue { |
| 41 | + return storage.thrownValue |
| 42 | + } |
| 43 | + |
| 44 | + /// A description of the exception. |
| 45 | + public var description: String { |
| 46 | + return storage.description |
| 47 | + } |
| 48 | + |
| 49 | + /// The stack trace of the exception. |
| 50 | + public var stack: String? { |
| 51 | + return storage.stack |
| 52 | + } |
| 53 | + |
| 54 | + /// Initializes a new JSException instance with a value thrown from JavaScript. |
| 55 | + /// |
| 56 | + /// Only available within the package. This must be called on the thread where the exception object created. |
| 57 | + /// The stringified representation is captured on the object owner thread to bring useful info |
| 58 | + /// to the catching thread even if they are different threads. |
| 59 | + @usableFromInline |
| 60 | + package init(_ thrownValue: JSValue) { |
| 61 | + if let errorObject = thrownValue.object, let stack = errorObject.stack.string { |
| 62 | + self.storage = Storage( |
| 63 | + thrownValue: thrownValue, |
| 64 | + description: "JSException(\(stack))", |
| 65 | + stack: stack |
| 66 | + ) |
| 67 | + } else { |
| 68 | + self.storage = Storage( |
| 69 | + thrownValue: thrownValue, |
| 70 | + description: "JSException(\(thrownValue))", |
| 71 | + stack: nil |
| 72 | + ) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public static func == (lhs: JSException, rhs: JSException) -> Bool { |
| 77 | + return lhs.storage.thrownValue == rhs.storage.thrownValue |
| 78 | + && lhs.storage.description == rhs.storage.description |
| 79 | + && lhs.storage.stack == rhs.storage.stack |
| 80 | + } |
| 81 | + #else |
16 | 82 | /// The value thrown from JavaScript. |
17 | 83 | /// This can be any JavaScript value (error object, string, number, etc.). |
18 | 84 | public var thrownValue: JSValue { |
@@ -47,6 +113,7 @@ public struct JSException: Error, Equatable, CustomStringConvertible { |
47 | 113 | self.stack = nil |
48 | 114 | } |
49 | 115 | } |
| 116 | + #endif |
50 | 117 |
|
51 | 118 | /// Initializes a new JavaScript `Error` instance with a message and prepare it to be thrown. |
52 | 119 | /// |
|
0 commit comments