Repository navigation
Expand file tree
/
Copy pathArrayBuffer.swift
More file actions
57 lines (46 loc) · 1.57 KB
/
Copy pathArrayBuffer.swift
File metadata and controls
57 lines (46 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
// Created by Manuel Burghard. Licensed unter MIT.
//
import _CJavaScriptKit
import JavaScriptKit
public typealias Int8Array = JSTypedArray<Int8>
public typealias Int16Array = JSTypedArray<Int16>
public typealias Int32Array = JSTypedArray<Int32>
public typealias Uint8Array = JSTypedArray<UInt8>
public typealias Uint16Array = JSTypedArray<UInt16>
public typealias Uint32Array = JSTypedArray<UInt32>
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
public required init(unsafelyWrapping jsObject: JSObject) {
self.jsObject = jsObject
}
public convenience init(length: Int) {
self.init(unsafelyWrapping: Self.constructor!.new(length))
}
@inlinable
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))
}
@inlinable
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