@@ -9,6 +9,8 @@ public protocol TypedArrayElement: JSValueConvertible, JSValueConstructible {
9
9
static var typedArrayClass : JSFunction { get }
10
10
}
11
11
12
+ /// A wrapper around [the JavaScript TypedArray class](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/TypedArray)
13
+ /// that exposes its properties in a type-safe and Swifty way.
12
14
public class JSTypedArray < Element> : JSValueConvertible , ExpressibleByArrayLiteral where Element: TypedArrayElement {
13
15
let ref : JSObject
14
16
public func jsValue( ) -> JSValue {
@@ -29,11 +31,18 @@ public class JSTypedArray<Element>: JSValueConvertible, ExpressibleByArrayLitera
29
31
self . ref = object
30
32
}
31
33
34
+ /// Construct a `JSTypedArray` from TypedArray `JSObject`.
35
+ /// Return `nil` if the object is not TypedArray.
36
+ ///
37
+ /// - Parameter object: A `JSObject` expected to be TypedArray
32
38
public init ? ( _ object: JSObject ) {
33
39
guard object. isInstanceOf ( Element . typedArrayClass) else { return nil }
34
40
self . ref = object
35
41
}
36
42
43
+ /// Initialize a new instance of TypedArray in JavaScript environment with given length zero value.
44
+ ///
45
+ /// - Parameter length: The length of elements that will be allocated.
37
46
public convenience init ( length: Int ) {
38
47
let jsObject = Element . typedArrayClass. new ( length)
39
48
self . init ( unsafe: jsObject)
@@ -42,17 +51,21 @@ public class JSTypedArray<Element>: JSValueConvertible, ExpressibleByArrayLitera
42
51
required public convenience init ( arrayLiteral elements: Element ... ) {
43
52
self . init ( elements)
44
53
}
45
-
54
+
55
+ /// Initialize a new instance of TypedArray in JavaScript environment with given elements.
56
+ ///
57
+ /// - Parameter array: The array that will be copied to create a new instance of TypedArray
46
58
public convenience init ( _ array: [ Element ] ) {
47
59
var resultObj = JavaScriptObjectRef ( )
48
60
array. withUnsafeBufferPointer { ptr in
49
61
_create_typed_array ( Element . typedArrayKind, ptr. baseAddress!, Int32 ( array. count) , & resultObj)
50
62
}
51
63
self . init ( unsafe: JSObject ( id: resultObj) )
52
64
}
53
-
54
- public convenience init ( _ stride: StrideTo < Element > ) where Element: Strideable {
55
- self . init ( stride. map ( { $0 } ) )
65
+
66
+ /// Convenience initializer for `Sequence`.
67
+ public convenience init < S: Sequence > ( _ sequence: S ) {
68
+ self . init ( sequence. map ( { $0 } ) )
56
69
}
57
70
}
58
71
0 commit comments