Skip to content

Add ArrayBuffer, JSTypedArray, and Data conversion #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
27 changes: 24 additions & 3 deletions Sources/DOMKit/ECMAScript/ArrayBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Created by Manuel Burghard. Licensed unter MIT.
//

import JavaScriptKit
import _CJavaScriptKit
import JavaScriptKit

public typealias Int8Array = JSTypedArray<Int8>
public typealias Int16Array = JSTypedArray<Int16>
Expand All @@ -15,7 +15,6 @@ public typealias Float32Array = JSTypedArray<Float32>
public typealias Float64Array = JSTypedArray<Float64>

public class ArrayBuffer: JSBridgedClass {

public class var constructor: JSFunction { JSObject.global.ArrayBuffer.function! }

public let jsObject: JSObject
Expand All @@ -25,10 +24,32 @@ public class ArrayBuffer: JSBridgedClass {
}

public convenience init(length: Int) {
self.init(unsafelyWrapping: Self.constructor.new( length))
self.init(unsafelyWrapping: Self.constructor.new(length))
}

public static func isView(_ object: JSValueCompatible) -> Bool {
JSObject.global.ArrayBuffer.object!.isView!(object).fromJSValue()!
}
}

public extension JSTypedArray {
convenience init(_ arrayBuffer: ArrayBuffer) {
self.init(unsafelyWrapping: Self.constructor.new(arrayBuffer))
}

var buffer: ArrayBuffer {
ArrayBuffer(unsafelyWrapping: jsObject.buffer.object!)
}
}

#if canImport(Foundation)
import Foundation

public extension Data {
init(_ arrayBuffer: ArrayBuffer) {
self = JSTypedArray<UInt8>(arrayBuffer).withUnsafeBytes {
Data(buffer: $0)
}
}
}
#endif
33 changes: 31 additions & 2 deletions Tests/DOMKitTests/DOMKitTests.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import XCTest
import DOMKit
import Foundation
import JavaScriptKit
import XCTest

final class DOMKitTests: XCTestCase {
func testExample() {
func testQuerySelector() {
let document = globalThis.document
let button = document.createElement(localName: "button")
button.textContent = "Hello, world!"
Expand All @@ -11,4 +13,31 @@ final class DOMKitTests: XCTestCase {
let queriedButton = document.querySelector(selectors: "body button")
XCTAssertEqual(queriedButton?.textContent, "Hello, world!")
}

func testDataToTypedArray() {
let array: [UInt8] = [1, 2, 3, 4, 5]
let data = Data(array)
let typedArray = JSTypedArray(data)

typedArray.withUnsafeBytes {
XCTAssertEqual($0.count, data.count)
for index in $0.indices {
XCTAssertEqual(array[index], $0[index])
}
}
}

func testTypedArrayToData() {
let array: [UInt8] = [1, 2, 3, 4, 5]
let typedArray = JSTypedArray(array)

let data = Data(typedArray.buffer)

typedArray.withUnsafeBytes {
XCTAssertEqual($0.count, data.count)
for index in $0.indices {
XCTAssertEqual(array[index], $0[index])
}
}
}
}