4
4
5
5
import _CJavaScriptKit
6
6
7
+ /// A protocol that allows a Swift numeric type to be mapped to the JavaScript TypedArray that holds integers of its type
7
8
public protocol TypedArrayElement : JSValueConvertible , JSValueConstructible {
9
+ /// The constructor function for the TypedArray class for this particular kind of number
8
10
static var typedArrayClass : JSFunction { get }
9
11
}
10
12
11
- /// A wrapper around [the JavaScript TypedArray class](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/TypedArray)
12
- /// that exposes its properties in a type-safe and Swifty way.
13
- public class JSTypedArray < Element> : JSValueConvertible , ExpressibleByArrayLiteral where Element: TypedArrayElement {
14
- let ref : JSObject
15
- public func jsValue( ) -> JSValue {
16
- . object( ref)
17
- }
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)
15
+ public class JSTypedArray < Element> : JSBridgedClass , ExpressibleByArrayLiteral where Element: TypedArrayElement {
16
+ public static var constructor : JSFunction { Element . typedArrayClass }
17
+ public var jsObject : JSObject
18
18
19
19
public subscript( _ index: Int ) -> Element {
20
20
get {
21
- return Element . construct ( from: getJSValue ( this : ref , index: Int32 ( index ) ) ) !
21
+ return Element . construct ( from: jsObject [ index] ) !
22
22
}
23
23
set {
24
- setJSValue ( this : ref , index: Int32 ( index ) , value : newValue. jsValue ( ) )
24
+ self . jsObject [ index] = newValue. jsValue ( )
25
25
}
26
26
}
27
27
28
- // This private initializer assumes that the passed object is TypedArray
29
- private init ( unsafe object: JSObject ) {
30
- self . ref = object
31
- }
32
-
33
- /// Construct a `JSTypedArray` from TypedArray `JSObject`.
34
- /// Return `nil` if the object is not TypedArray.
28
+ /// Initialize a new instance of TypedArray in JavaScript environment with given length.
29
+ /// All the elements will be initialized to zero.
35
30
///
36
- /// - Parameter object: A `JSObject` expected to be TypedArray
37
- public init ? ( _ object: JSObject ) {
38
- guard object. isInstanceOf ( Element . typedArrayClass) else { return nil }
39
- self . ref = object
31
+ /// - Parameter length: The number of elements that will be allocated.
32
+ public init ( length: Int ) {
33
+ jsObject = Element . typedArrayClass. new ( length)
40
34
}
41
35
42
- /// Initialize a new instance of TypedArray in JavaScript environment with given length zero value.
43
- ///
44
- /// - Parameter length: The length of elements that will be allocated.
45
- public convenience init ( length: Int ) {
46
- let jsObject = Element . typedArrayClass. new ( length)
47
- self . init ( unsafe: jsObject)
36
+ required public init ( unsafelyWrapping jsObject: JSObject ) {
37
+ self . jsObject = jsObject
48
38
}
49
39
50
40
required public convenience init ( arrayLiteral elements: Element ... ) {
51
41
self . init ( elements)
52
42
}
53
-
54
43
/// Initialize a new instance of TypedArray in JavaScript environment with given elements.
55
44
///
56
45
/// - Parameter array: The array that will be copied to create a new instance of TypedArray
@@ -59,7 +48,7 @@ public class JSTypedArray<Element>: JSValueConvertible, ExpressibleByArrayLitera
59
48
array. withUnsafeBufferPointer { ptr in
60
49
_create_typed_array ( Element . typedArrayClass. id, ptr. baseAddress!, Int32 ( array. count) , & resultObj)
61
50
}
62
- self . init ( unsafe : JSObject ( id: resultObj) )
51
+ self . init ( unsafelyWrapping : JSObject ( id: resultObj) )
63
52
}
64
53
65
54
/// Convenience initializer for `Sequence`.
@@ -90,8 +79,6 @@ extension UInt: TypedArrayElement {
90
79
valueForBitWidth ( typeName: " UInt " , bitWidth: Int . bitWidth, when32: JSObject . global. Uint32Array) . function!
91
80
}
92
81
93
- // MARK: - Concrete TypedArray classes
94
-
95
82
extension Int8 : TypedArrayElement {
96
83
public static var typedArrayClass = JSObject . global. Int8Array. function!
97
84
}
0 commit comments