diff --git a/Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift b/Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift index f82864e8..963419c9 100644 --- a/Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift +++ b/Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift @@ -79,12 +79,16 @@ public class JSTypedArray: JSBridgedClass, ExpressibleByArrayLiteral wh /// - Returns: The return value, if any, of the `body` closure parameter. public func withUnsafeBytes(_ body: (UnsafeBufferPointer) throws -> R) rethrows -> R { let bytesLength = lengthInBytes - let rawBuffer = malloc(bytesLength)! - defer { free(rawBuffer) } - _load_typed_array(jsObject.id, rawBuffer.assumingMemoryBound(to: UInt8.self)) - let length = lengthInBytes / MemoryLayout.size - let boundPtr = rawBuffer.bindMemory(to: Element.self, capacity: length) - let bufferPtr = UnsafeBufferPointer(start: boundPtr, count: length) + let rawBuffer = UnsafeMutableBufferPointer.allocate(capacity: bytesLength) + defer { rawBuffer.deallocate() } + let baseAddress = rawBuffer.baseAddress! + _load_typed_array(jsObject.id, baseAddress) + let length = bytesLength / MemoryLayout.size + let rawBaseAddress = UnsafeRawPointer(baseAddress) + let bufferPtr = UnsafeBufferPointer( + start: rawBaseAddress.assumingMemoryBound(to: Element.self), + count: length + ) let result = try body(bufferPtr) return result } @@ -106,12 +110,16 @@ public class JSTypedArray: JSBridgedClass, ExpressibleByArrayLiteral wh @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) public func withUnsafeBytesAsync(_ body: (UnsafeBufferPointer) async throws -> R) async rethrows -> R { let bytesLength = lengthInBytes - let rawBuffer = malloc(bytesLength)! - defer { free(rawBuffer) } - _load_typed_array(jsObject.id, rawBuffer.assumingMemoryBound(to: UInt8.self)) - let length = lengthInBytes / MemoryLayout.size - let boundPtr = rawBuffer.bindMemory(to: Element.self, capacity: length) - let bufferPtr = UnsafeBufferPointer(start: boundPtr, count: length) + let rawBuffer = UnsafeMutableBufferPointer.allocate(capacity: bytesLength) + defer { rawBuffer.deallocate() } + let baseAddress = rawBuffer.baseAddress! + _load_typed_array(jsObject.id, baseAddress) + let length = bytesLength / MemoryLayout.size + let rawBaseAddress = UnsafeRawPointer(baseAddress) + let bufferPtr = UnsafeBufferPointer( + start: rawBaseAddress.assumingMemoryBound(to: Element.self), + count: length + ) let result = try await body(bufferPtr) return result }