Skip to content
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

Add withUnsafeBytesAsync function to JSTypedArray #222

Merged
merged 3 commits into from
Mar 13, 2023
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
9 changes: 6 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ jobs:
matrix:
entry:
# Ensure that all host can install toolchain, build project, and run tests
- { os: macos-10.15, toolchain: wasm-5.6.0-RELEASE, wasi-backend: Node }
- { os: macos-11, toolchain: wasm-5.6.0-RELEASE, wasi-backend: Node }
- { os: macos-12, toolchain: wasm-5.6.0-RELEASE, wasi-backend: Node }
- { os: macos-10.15, toolchain: wasm-5.6.0-RELEASE, wasi-backend: Node, xcode: Xcode_12.4.app }
- { os: macos-11, toolchain: wasm-5.6.0-RELEASE, wasi-backend: Node, xcode: Xcode_13.2.1.app }
- { os: macos-12, toolchain: wasm-5.6.0-RELEASE, wasi-backend: Node, xcode: Xcode_13.4.1.app }
- { os: ubuntu-18.04, toolchain: wasm-5.6.0-RELEASE, wasi-backend: Node }

# Ensure that test succeeds with all toolchains and wasi backend combinations
Expand All @@ -33,6 +33,9 @@ jobs:
uses: actions/checkout@master
with:
fetch-depth: 1
- name: Select SDKROOT
if: ${{ matrix.entry.xcode }}
run: sudo xcode-select -s /Applications/${{ matrix.entry.xcode }}
- uses: swiftwasm/setup-swiftwasm@v1
with:
swift-version: ${{ matrix.entry.toolchain }}
Expand Down
28 changes: 28 additions & 0 deletions Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,34 @@ public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral wh
let result = try body(bufferPtr)
return result
}

#if compiler(>=5.5)
/// Calls the given async closure with a pointer to a copy of the underlying bytes of the
/// array's storage.
///
/// - Note: The pointer passed as an argument to `body` is valid only for the
/// lifetime of the closure. Do not escape it from the async closure for later
/// use.
///
/// - Parameter body: A closure with an `UnsafeBufferPointer` parameter
/// that points to the contiguous storage for the array.
/// If `body` has a return value, that value is also
/// used as the return value for the `withUnsafeBytes(_:)` method. The
/// argument is valid only for the duration of the closure's execution.
/// - Returns: The return value, if any, of the `body`async closure parameter.
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
public func withUnsafeBytesAsync<R>(_ body: (UnsafeBufferPointer<Element>) 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<Element>.size
let boundPtr = rawBuffer.bindMemory(to: Element.self, capacity: length)
let bufferPtr = UnsafeBufferPointer<Element>(start: boundPtr, count: length)
let result = try await body(bufferPtr)
return result
}
#endif
}

// MARK: - Int and UInt support
Expand Down