11//
22// Created by Manuel Burghard. Licensed unter MIT.
33//
4- #if !hasFeature(Embedded)
54import _CJavaScriptKit
65
76/// 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
99 /// The constructor function for the TypedArray class for this particular kind of number
1010 static var typedArrayClass : JSFunction { get }
1111}
1212
1313/// A wrapper around all [JavaScript `TypedArray`
1414/// classes](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/TypedArray)
1515/// 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 }
1819 public var jsObject : JSObject
1920
2021 public subscript( _ index: Int ) -> Element {
@@ -139,33 +140,28 @@ public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral wh
139140 }
140141}
141142
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-
157143extension 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+ }
162153}
163154
164155extension 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+ }
169165}
170166
171167extension Int8 : TypedArrayElement {
@@ -176,13 +172,6 @@ extension UInt8: TypedArrayElement {
176172 public static var typedArrayClass : JSFunction { JSObject . global. Uint8Array. function! }
177173}
178174
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-
186175extension Int16 : TypedArrayElement {
187176 public static var typedArrayClass : JSFunction { JSObject . global. Int16Array. function! }
188177}
@@ -206,4 +195,10 @@ extension Float32: TypedArrayElement {
206195extension Float64 : TypedArrayElement {
207196 public static var typedArrayClass : JSFunction { JSObject . global. Float64Array. function! }
208197}
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