Skip to content

Commit c50ae64

Browse files
authored
Add ArrayBuffer, JSTypedArray, and Data conversion (#13)
Resolves #7.
1 parent a40aacc commit c50ae64

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

Sources/DOMKit/ECMAScript/ArrayBuffer.swift

+24-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Created by Manuel Burghard. Licensed unter MIT.
33
//
44

5-
import JavaScriptKit
65
import _CJavaScriptKit
6+
import JavaScriptKit
77

88
public typealias Int8Array = JSTypedArray<Int8>
99
public typealias Int16Array = JSTypedArray<Int16>
@@ -15,7 +15,6 @@ public typealias Float32Array = JSTypedArray<Float32>
1515
public typealias Float64Array = JSTypedArray<Float64>
1616

1717
public class ArrayBuffer: JSBridgedClass {
18-
1918
public class var constructor: JSFunction { JSObject.global.ArrayBuffer.function! }
2019

2120
public let jsObject: JSObject
@@ -25,10 +24,32 @@ public class ArrayBuffer: JSBridgedClass {
2524
}
2625

2726
public convenience init(length: Int) {
28-
self.init(unsafelyWrapping: Self.constructor.new( length))
27+
self.init(unsafelyWrapping: Self.constructor.new(length))
2928
}
3029

3130
public static func isView(_ object: JSValueCompatible) -> Bool {
3231
JSObject.global.ArrayBuffer.object!.isView!(object).fromJSValue()!
3332
}
3433
}
34+
35+
public extension JSTypedArray {
36+
convenience init(_ arrayBuffer: ArrayBuffer) {
37+
self.init(unsafelyWrapping: Self.constructor.new(arrayBuffer))
38+
}
39+
40+
var buffer: ArrayBuffer {
41+
ArrayBuffer(unsafelyWrapping: jsObject.buffer.object!)
42+
}
43+
}
44+
45+
#if canImport(Foundation)
46+
import Foundation
47+
48+
public extension Data {
49+
init(_ arrayBuffer: ArrayBuffer) {
50+
self = JSTypedArray<UInt8>(arrayBuffer).withUnsafeBytes {
51+
Data(buffer: $0)
52+
}
53+
}
54+
}
55+
#endif

Tests/DOMKitTests/DOMKitTests.swift

+31-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import XCTest
21
import DOMKit
2+
import Foundation
3+
import JavaScriptKit
4+
import XCTest
35

46
final class DOMKitTests: XCTestCase {
5-
func testExample() {
7+
func testQuerySelector() {
68
let document = globalThis.document
79
let button = document.createElement(localName: "button")
810
button.textContent = "Hello, world!"
@@ -11,4 +13,31 @@ final class DOMKitTests: XCTestCase {
1113
let queriedButton = document.querySelector(selectors: "body button")
1214
XCTAssertEqual(queriedButton?.textContent, "Hello, world!")
1315
}
16+
17+
func testDataToTypedArray() {
18+
let array: [UInt8] = [1, 2, 3, 4, 5]
19+
let data = Data(array)
20+
let typedArray = JSTypedArray(data)
21+
22+
typedArray.withUnsafeBytes {
23+
XCTAssertEqual($0.count, data.count)
24+
for index in $0.indices {
25+
XCTAssertEqual(array[index], $0[index])
26+
}
27+
}
28+
}
29+
30+
func testTypedArrayToData() {
31+
let array: [UInt8] = [1, 2, 3, 4, 5]
32+
let typedArray = JSTypedArray(array)
33+
34+
let data = Data(typedArray.buffer)
35+
36+
typedArray.withUnsafeBytes {
37+
XCTAssertEqual($0.count, data.count)
38+
for index in $0.indices {
39+
XCTAssertEqual(array[index], $0[index])
40+
}
41+
}
42+
}
1443
}

0 commit comments

Comments
 (0)