@@ -9,6 +9,8 @@ public protocol TypedArrayElement: JSValueConvertible, JSValueConstructible {
99 static var typedArrayClass : JSFunction { get }
1010}
1111
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.
1214public class JSTypedArray < Element> : JSValueConvertible , ExpressibleByArrayLiteral where Element: TypedArrayElement {
1315 let ref : JSObject
1416 public func jsValue( ) -> JSValue {
@@ -29,11 +31,18 @@ public class JSTypedArray<Element>: JSValueConvertible, ExpressibleByArrayLitera
2931 self . ref = object
3032 }
3133
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
3238 public init ? ( _ object: JSObject ) {
3339 guard object. isInstanceOf ( Element . typedArrayClass) else { return nil }
3440 self . ref = object
3541 }
3642
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.
3746 public convenience init ( length: Int ) {
3847 let jsObject = Element . typedArrayClass. new ( length)
3948 self . init ( unsafe: jsObject)
@@ -42,17 +51,21 @@ public class JSTypedArray<Element>: JSValueConvertible, ExpressibleByArrayLitera
4251 required public convenience init ( arrayLiteral elements: Element ... ) {
4352 self . init ( elements)
4453 }
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
4658 public convenience init ( _ array: [ Element ] ) {
4759 var resultObj = JavaScriptObjectRef ( )
4860 array. withUnsafeBufferPointer { ptr in
4961 _create_typed_array ( Element . typedArrayKind, ptr. baseAddress!, Int32 ( array. count) , & resultObj)
5062 }
5163 self . init ( unsafe: JSObject ( id: resultObj) )
5264 }
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ドル } ) )
5669 }
5770}
5871
0 commit comments