@@ -6,13 +6,12 @@ import _CJavaScriptKit
66
77/// A protocol that allows a Swift numeric type to be mapped to the JavaScript TypedArray that holds integers of its type
88public protocol TypedArrayElement : JSValueConvertible , JSValueConstructible {
9- /// The kind of typed array that should be created on the JS side
10- static var typedArrayKind : JavaScriptTypedArrayKind { get }
119 /// The constructor function for the TypedArray class for this particular kind of number
1210 static var typedArrayClass : JSFunction { get }
1311}
1412
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)
1615public class JSTypedArray < Element> : JSBridgedClass , ExpressibleByArrayLiteral where Element: TypedArrayElement {
1716 public static var constructor : JSFunction { Element . typedArrayClass }
1817 public var jsObject : JSObject
@@ -26,7 +25,10 @@ public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral wh
2625 }
2726 }
2827
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.
3032 public init ( length: Int ) {
3133 jsObject = Element . typedArrayClass. new ( length)
3234 }
@@ -38,18 +40,20 @@ public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral wh
3840 required public convenience init ( arrayLiteral elements: Element ... ) {
3941 self . init ( elements)
4042 }
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
4346 public convenience init ( _ array: [ Element ] ) {
4447 var resultObj = JavaScriptObjectRef ( )
4548 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)
4750 }
4851 self . init ( withCompatibleObject: JSObject ( id: resultObj) )
4952 }
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ドル } ) )
5357 }
5458}
5559
@@ -67,67 +71,47 @@ func valueForBitWidth<T>(typeName: String, bitWidth: Int, when32: T) -> T {
6771}
6872
6973extension Int : TypedArrayElement {
70- public static var typedArrayClass : JSFunction {
74+ public static var typedArrayClass : JSFunction =
7175 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- }
7676}
7777extension UInt : TypedArrayElement {
78- public static var typedArrayClass : JSFunction {
78+ public static var typedArrayClass : JSFunction =
7979 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- }
8480}
8581
86- // MARK: - Concrete TypedArray classes
87- 8882extension 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!
9184}
9285extension 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!
9587}
9688// TODO: Support Uint8ClampedArray?
9789
9890extension 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!
10192}
10293extension 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!
10595}
10696
10797extension 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!
11099}
111100extension 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!
114102}
115103
116104// FIXME: Support passing BigInts across the bridge
117105//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!
120107//}
121108//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!
124110//}
125111
126112extension 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!
129114}
130115extension 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!
133117}
0 commit comments