@@ -6,13 +6,12 @@ import _CJavaScriptKit
6
6
7
7
/// A protocol that allows a Swift numeric type to be mapped to the JavaScript TypedArray that holds integers of its type
8
8
public protocol TypedArrayElement : JSValueConvertible , JSValueConstructible {
9
- /// The kind of typed array that should be created on the JS side
10
- static var typedArrayKind : JavaScriptTypedArrayKind { get }
11
9
/// The constructor function for the TypedArray class for this particular kind of number
12
10
static var typedArrayClass : JSFunction { get }
13
11
}
14
12
15
- /// A wrapper around all JavaScript [`TypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) classes that exposes their properties in a type-safe way.
13
+ /// A wrapper around all JavaScript [TypedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) classes that exposes their properties in a type-safe way.
14
+ /// FIXME: the BigInt-based TypedArrays are not supported (https://github.com/swiftwasm/JavaScriptKit/issues/56)
16
15
public class JSTypedArray < Element> : JSBridgedClass , ExpressibleByArrayLiteral where Element: TypedArrayElement {
17
16
public static var constructor : JSFunction { Element . typedArrayClass }
18
17
public var jsObject : JSObject
@@ -26,7 +25,10 @@ public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral wh
26
25
}
27
26
}
28
27
29
- /// Create a TypedArray with the provided number of elements allocated. All the elements will be initialized to zero.
28
+ /// Initialize a new instance of TypedArray in JavaScript environment with given length.
29
+ /// All the elements will be initialized to zero.
30
+ ///
31
+ /// - Parameter length: The number of elements that will be allocated.
30
32
public init ( length: Int ) {
31
33
jsObject = Element . typedArrayClass. new ( length)
32
34
}
@@ -38,18 +40,20 @@ public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral wh
38
40
required public convenience init ( arrayLiteral elements: Element ... ) {
39
41
self . init ( elements)
40
42
}
41
-
42
- /// Convert an array of numbers into a JavaScript TypedArray
43
+ /// Initialize a new instance of TypedArray in JavaScript environment with given elements.
44
+ ///
45
+ /// - Parameter array: The array that will be copied to create a new instance of TypedArray
43
46
public convenience init ( _ array: [ Element ] ) {
44
47
var resultObj = JavaScriptObjectRef ( )
45
48
array. withUnsafeBufferPointer { ptr in
46
- _create_typed_array ( Element . typedArrayKind , ptr. baseAddress!, Int32 ( array. count) , & resultObj)
49
+ _create_typed_array ( Element . typedArrayClass . id , ptr. baseAddress!, Int32 ( array. count) , & resultObj)
47
50
}
48
51
self . init ( withCompatibleObject: JSObject ( id: resultObj) )
49
52
}
50
-
51
- public convenience init ( _ stride: StrideTo < Element > ) where Element: Strideable {
52
- self . init ( stride. map ( { $0 } ) )
53
+
54
+ /// Convenience initializer for `Sequence`.
55
+ public convenience init < S: Sequence > ( _ sequence: S ) {
56
+ self . init ( sequence. map ( { $0 } ) )
53
57
}
54
58
}
55
59
@@ -67,67 +71,47 @@ func valueForBitWidth<T>(typeName: String, bitWidth: Int, when32: T) -> T {
67
71
}
68
72
69
73
extension Int : TypedArrayElement {
70
- public static var typedArrayClass : JSFunction {
74
+ public static var typedArrayClass : JSFunction =
71
75
valueForBitWidth ( typeName: " Int " , bitWidth: Int . bitWidth, when32: JSObject . global. Int32Array) . function!
72
- }
73
- public static var typedArrayKind : JavaScriptTypedArrayKind {
74
- valueForBitWidth ( typeName: " Int " , bitWidth: Int . bitWidth, when32: . int32)
75
- }
76
76
}
77
77
extension UInt : TypedArrayElement {
78
- public static var typedArrayClass : JSFunction {
78
+ public static var typedArrayClass : JSFunction =
79
79
valueForBitWidth ( typeName: " UInt " , bitWidth: Int . bitWidth, when32: JSObject . global. Uint32Array) . function!
80
- }
81
- public static var typedArrayKind : JavaScriptTypedArrayKind {
82
- valueForBitWidth ( typeName: " UInt " , bitWidth: UInt . bitWidth, when32: . uint32)
83
- }
84
80
}
85
81
86
- // MARK: - Concrete TypedArray classes
87
-
88
82
extension Int8 : TypedArrayElement {
89
- public static var typedArrayClass : JSFunction { JSObject . global. Int8Array. function! }
90
- public static var typedArrayKind : JavaScriptTypedArrayKind { . int8 }
83
+ public static var typedArrayClass = JSObject . global. Int8Array. function!
91
84
}
92
85
extension UInt8 : TypedArrayElement {
93
- public static var typedArrayClass : JSFunction { JSObject . global. Uint8Array. function! }
94
- public static var typedArrayKind : JavaScriptTypedArrayKind { . uint8 }
86
+ public static var typedArrayClass = JSObject . global. Uint8Array. function!
95
87
}
96
88
// TODO: Support Uint8ClampedArray?
97
89
98
90
extension Int16 : TypedArrayElement {
99
- public static var typedArrayClass : JSFunction { JSObject . global. Int16Array. function! }
100
- public static var typedArrayKind : JavaScriptTypedArrayKind { . int16 }
91
+ public static var typedArrayClass = JSObject . global. Int16Array. function!
101
92
}
102
93
extension UInt16 : TypedArrayElement {
103
- public static var typedArrayClass : JSFunction { JSObject . global. Uint16Array. function! }
104
- public static var typedArrayKind : JavaScriptTypedArrayKind { . uint16 }
94
+ public static var typedArrayClass = JSObject . global. Uint16Array. function!
105
95
}
106
96
107
97
extension Int32 : TypedArrayElement {
108
- public static var typedArrayClass : JSFunction { JSObject . global. Int32Array. function! }
109
- public static var typedArrayKind : JavaScriptTypedArrayKind { . int32 }
98
+ public static var typedArrayClass = JSObject . global. Int32Array. function!
110
99
}
111
100
extension UInt32 : TypedArrayElement {
112
- public static var typedArrayClass : JSFunction { JSObject . global. Uint32Array. function! }
113
- public static var typedArrayKind : JavaScriptTypedArrayKind { . uint32 }
101
+ public static var typedArrayClass = JSObject . global. Uint32Array. function!
114
102
}
115
103
116
104
// FIXME: Support passing BigInts across the bridge
117
105
//extension Int64: TypedArrayElement {
118
- // public static var typedArrayClass: JSFunction { JSObject.global.BigInt64Array.function! }
119
- // public static var type: JavaScriptTypedArrayKind { .bigInt64 }
106
+ // public static var typedArrayClass = JSObject.global.BigInt64Array.function!
120
107
//}
121
108
//extension UInt64: TypedArrayElement {
122
- // public static var typedArrayClass: JSFunction { JSObject.global.BigUint64Array.function! }
123
- // public static var type: JavaScriptTypedArrayKind { .bigUint64 }
109
+ // public static var typedArrayClass = JSObject.global.BigUint64Array.function!
124
110
//}
125
111
126
112
extension Float32 : TypedArrayElement {
127
- public static var typedArrayClass : JSFunction { JSObject . global. Float32Array. function! }
128
- public static var typedArrayKind : JavaScriptTypedArrayKind { . float32 }
113
+ public static var typedArrayClass = JSObject . global. Float32Array. function!
129
114
}
130
115
extension Float64 : TypedArrayElement {
131
- public static var typedArrayClass : JSFunction { JSObject . global. Float64Array. function! }
132
- public static var typedArrayKind : JavaScriptTypedArrayKind { . float64 }
116
+ public static var typedArrayClass = JSObject . global. Float64Array. function!
133
117
}
0 commit comments