|
5 | 5 |
|
6 | 6 | import JavaScriptKit |
7 | 7 |
|
8 | | -public typealias ArrayBufferView = Int8ArrayOrInt16ArrayOrInt32ArrayOrUint8ArrayOrUint16ArrayOrUint32ArrayOrUint8ClampedArrayOrFloat32ArrayOrFloat64ArrayOrDataView |
| 8 | +public enum ArrayBufferView: JSBridgedType { |
| 9 | + case int8Array(JSTypedArray<Int8>) |
| 10 | + case int16Array(JSTypedArray<Int16>) |
| 11 | + case int32Array(JSTypedArray<Int32>) |
| 12 | + case uint8Array(JSTypedArray<UInt8>) |
| 13 | + case uint16Array(JSTypedArray<UInt16>) |
| 14 | + case uint32Array(JSTypedArray<UInt32>) |
| 15 | + case uint8ClampedArray(JSTypedArray<UInt8>) |
| 16 | + case float32Array(JSTypedArray<Float>) |
| 17 | + case float64Array(JSTypedArray<Double>) |
| 18 | + case dataView(DataView) |
| 19 | + |
| 20 | + public init?(from value: JSValue) { |
| 21 | + if let decoded: JSTypedArray<Int8> = value.fromJSValue() { |
| 22 | + self = .int8Array(decoded) |
| 23 | + } else if let decoded: JSTypedArray<Int16> = value.fromJSValue() { |
| 24 | + self = .int16Array(decoded) |
| 25 | + } else if let decoded: JSTypedArray<Int32> = value.fromJSValue() { |
| 26 | + self = .int32Array(decoded) |
| 27 | + } else if let decoded: JSTypedArray<UInt8> = value.fromJSValue() { |
| 28 | + self = .uint8Array(decoded) |
| 29 | + } else if let decoded: JSTypedArray<UInt16> = value.fromJSValue() { |
| 30 | + self = .uint16Array(decoded) |
| 31 | + } else if let decoded: JSTypedArray<UInt32> = value.fromJSValue() { |
| 32 | + self = .uint32Array(decoded) |
| 33 | + } else if let decoded: JSTypedArray<UInt8> = value.fromJSValue() { |
| 34 | + self = .uint8ClampedArray(decoded) |
| 35 | + } else if let decoded: JSTypedArray<Float> = value.fromJSValue() { |
| 36 | + self = .float32Array(decoded) |
| 37 | + } else if let decoded: JSTypedArray<Double> = value.fromJSValue() { |
| 38 | + self = .float64Array(decoded) |
| 39 | + } else if let decoded: DataView = value.fromJSValue() { |
| 40 | + self = .dataView(decoded) |
| 41 | + } else { |
| 42 | + return nil |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public var value: JSValue { jsValue() } |
| 47 | + |
| 48 | + public func jsValue() -> JSValue { |
| 49 | + switch self { |
| 50 | + case let .int8Array(v): return v.jsValue() |
| 51 | + case let .int16Array(v): return v.jsValue() |
| 52 | + case let .int32Array(v): return v.jsValue() |
| 53 | + case let .uint8Array(v): return v.jsValue() |
| 54 | + case let .uint16Array(v): return v.jsValue() |
| 55 | + case let .uint32Array(v): return v.jsValue() |
| 56 | + case let .uint8ClampedArray(v): return v.jsValue() |
| 57 | + case let .float32Array(v): return v.jsValue() |
| 58 | + case let .float64Array(v): return v.jsValue() |
| 59 | + case let .dataView(v): return v.jsValue() |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments