1
1
//
2
2
// Created by Manuel Burghard. Licensed unter MIT.
3
3
//
4
- #if !hasFeature(Embedded)
5
4
import _CJavaScriptKit
6
5
7
6
/// A protocol that allows a Swift numeric type to be mapped to the JavaScript TypedArray that holds integers of its type
8
- public protocol TypedArrayElement : ConvertibleToJSValue , ConstructibleFromJSValue {
7
+ public protocol TypedArrayElement {
8
+ associatedtype Element : ConvertibleToJSValue , Construct ibleFromJSValue = Self
9
9
/// The constructor function for the TypedArray class for this particular kind of number
10
10
static var typedArrayClass : JSFunction { get }
11
11
}
12
12
13
13
/// A wrapper around all [JavaScript `TypedArray`
14
14
/// classes](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/TypedArray)
15
15
/// that exposes their properties in a type-safe way.
16
- public class JSTypedArray < Element> : JSBridgedClass , ExpressibleByArrayLiteral where Element: TypedArrayElement {
17
- public class var constructor : JSFunction ? { Element . typedArrayClass }
16
+ public final class JSTypedArray < Traits> : JSBridgedClass , ExpressibleByArrayLiteral where Traits: TypedArrayElement {
17
+ public typealias Element = Traits . Element
18
+ public class var constructor : JSFunction ? { Traits . typedArrayClass }
18
19
public var jsObject : JSObject
19
20
20
21
public subscript( _ index: Int ) -> Element {
@@ -139,33 +140,28 @@ public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral wh
139
140
}
140
141
}
141
142
142
- // MARK: - Int and UInt support
143
-
144
- // FIXME: Should be updated to support wasm64 when that becomes available.
145
- func valueForBitWidth< T> ( typeName: String , bitWidth: Int , when32: T ) -> T {
146
- if bitWidth == 32 {
147
- return when32
148
- } else if bitWidth == 64 {
149
- fatalError ( " 64-bit \( typeName) s are not yet supported in JSTypedArray " )
150
- } else {
151
- fatalError (
152
- " Unsupported bit width for type \( typeName) : \( bitWidth) (hint: stick to fixed-size \( typeName) s to avoid this issue) "
153
- )
154
- }
155
- }
156
-
157
143
extension Int : TypedArrayElement {
158
- public static var typedArrayClass : JSFunction { _typedArrayClass. wrappedValue }
159
- private static let _typedArrayClass = LazyThreadLocal ( initialize: {
160
- valueForBitWidth ( typeName: " Int " , bitWidth: Int . bitWidth, when32: JSObject . global. Int32Array) . function!
161
- } )
144
+ public static var typedArrayClass : JSFunction {
145
+ #if _pointerBitWidth(_32)
146
+ return JSObject . global. Int32Array. function!
147
+ #elseif _pointerBitWidth(_64)
148
+ return JSObject . global. Int64Array. function!
149
+ #else
150
+ #error("Unsupported pointer width")
151
+ #endif
152
+ }
162
153
}
163
154
164
155
extension UInt : TypedArrayElement {
165
- public static var typedArrayClass : JSFunction { _typedArrayClass. wrappedValue }
166
- private static let _typedArrayClass = LazyThreadLocal ( initialize: {
167
- valueForBitWidth ( typeName: " UInt " , bitWidth: Int . bitWidth, when32: JSObject . global. Uint32Array) . function!
168
- } )
156
+ public static var typedArrayClass : JSFunction {
157
+ #if _pointerBitWidth(_32)
158
+ return JSObject . global. Uint32Array. function!
159
+ #elseif _pointerBitWidth(_64)
160
+ return JSObject . global. Uint64Array. function!
161
+ #else
162
+ #error("Unsupported pointer width")
163
+ #endif
164
+ }
169
165
}
170
166
171
167
extension Int8 : TypedArrayElement {
@@ -176,13 +172,6 @@ extension UInt8: TypedArrayElement {
176
172
public static var typedArrayClass : JSFunction { JSObject . global. Uint8Array. function! }
177
173
}
178
174
179
- /// A wrapper around [the JavaScript `Uint8ClampedArray`
180
- /// class](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)
181
- /// that exposes its properties in a type-safe and Swifty way.
182
- public class JSUInt8ClampedArray : JSTypedArray < UInt8 > {
183
- override public class var constructor : JSFunction ? { JSObject . global. Uint8ClampedArray. function! }
184
- }
185
-
186
175
extension Int16 : TypedArrayElement {
187
176
public static var typedArrayClass : JSFunction { JSObject . global. Int16Array. function! }
188
177
}
@@ -206,4 +195,10 @@ extension Float32: TypedArrayElement {
206
195
extension Float64 : TypedArrayElement {
207
196
public static var typedArrayClass : JSFunction { JSObject . global. Float64Array. function! }
208
197
}
209
- #endif
198
+
199
+ public enum JSUInt8Clamped : TypedArrayElement {
200
+ public typealias Element = UInt8
201
+ public static var typedArrayClass : JSFunction { JSObject . global. Uint8ClampedArray. function! }
202
+ }
203
+
204
+ public typealias JSUInt8ClampedArray = JSTypedArray < JSUInt8Clamped >
0 commit comments