44
55import _CJavaScriptKit
66
7+ /// A protocol that allows a Swift numeric type to be mapped to the JavaScript TypedArray that holds integers of its type
78public protocol TypedArrayElement : JSValueConvertible , JSValueConstructible {
9+ /// The constructor function for the TypedArray class for this particular kind of number
810 static var typedArrayClass : JSFunction { get }
911}
1012
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
1818
1919 public subscript( _ index: Int ) -> Element {
2020 get {
21- return Element . construct ( from: getJSValue ( this : ref , index: Int32 ( index ) ) ) !
21+ return Element . construct ( from: jsObject [ index] ) !
2222 }
2323 set {
24- setJSValue ( this : ref , index: Int32 ( index ) , value : newValue. jsValue ( ) )
24+ self . jsObject [ index] = newValue. jsValue ( )
2525 }
2626 }
2727
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.
3530 ///
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)
4034 }
4135
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
4838 }
4939
5040 required public convenience init ( arrayLiteral elements: Element ... ) {
5141 self . init ( elements)
5242 }
53- 5443 /// Initialize a new instance of TypedArray in JavaScript environment with given elements.
5544 ///
5645 /// - 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
5948 array. withUnsafeBufferPointer { ptr in
6049 _create_typed_array ( Element . typedArrayClass. id, ptr. baseAddress!, Int32 ( array. count) , & resultObj)
6150 }
62- self . init ( unsafe : JSObject ( id: resultObj) )
51+ self . init ( unsafelyWrapping : JSObject ( id: resultObj) )
6352 }
6453
6554 /// Convenience initializer for `Sequence`.
@@ -90,8 +79,6 @@ extension UInt: TypedArrayElement {
9079 valueForBitWidth ( typeName: " UInt " , bitWidth: Int . bitWidth, when32: JSObject . global. Uint32Array) . function!
9180}
9281
93- // MARK: - Concrete TypedArray classes
94- 9582extension Int8 : TypedArrayElement {
9683 public static var typedArrayClass = JSObject . global. Int8Array. function!
9784}
0 commit comments