Skip to content

Commit de5f52e

Browse files
committed
Docs for JSTypedArray
1 parent 8b181c7 commit de5f52e

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44

55
import _CJavaScriptKit
66

7+
/// A protocol that allows a Swift numeric type to be mapped to the JavaScript TypedArray that holds integers of its type
78
public protocol TypedArrayElement: JSValueConvertible, JSValueConstructible {
9+
/// The kind of typed array that should be created on the JS side
810
static var typedArrayKind: JavaScriptTypedArrayKind { get }
11+
/// The constructor function for the TypedArray class for this particular kind of number
912
static var typedArrayClass: JSFunction { get }
1013
}
1114

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.
1216
public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral where Element: TypedArrayElement {
1317
public static var constructor: JSFunction { Element.typedArrayClass }
1418
public var jsObject: JSObject
19+
1520
public subscript(_ index: Int) -> Element {
1621
get {
1722
return Element.construct(from: jsObject[index])!
@@ -21,6 +26,7 @@ public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral wh
2126
}
2227
}
2328

29+
/// Create a TypedArray with the provided number of elements allocated. All the elements will be initialized to zero.
2430
public init(length: Int) {
2531
jsObject = Element.typedArrayClass.new(length)
2632
}
@@ -33,6 +39,7 @@ public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral wh
3339
self.init(elements)
3440
}
3541

42+
/// Convert an array of numbers into a JavaScript TypedArray
3643
public convenience init(_ array: [Element]) {
3744
var resultObj = JavaScriptObjectRef()
3845
array.withUnsafeBufferPointer { ptr in

0 commit comments

Comments
 (0)