Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Docs for JSTypedArray
  • Loading branch information
j-f1 committed Sep 15, 2020
commit de5f52e88d39f1a579123fd6d8f78bff69939646
7 changes: 7 additions & 0 deletions Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@

import _CJavaScriptKit

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

/// 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.
public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral where Element: TypedArrayElement {
public static var constructor: JSFunction { Element.typedArrayClass }
public var jsObject: JSObject

public subscript(_ index: Int) -> Element {
get {
return Element.construct(from: jsObject[index])!
Expand All @@ -21,6 +26,7 @@ public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral wh
}
}

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

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