Skip to content

Commit 451000d

Browse files
committed
Fix warnings with untyped throws, fix npm install error
# Conflicts: # Examples/EmbeddedConcurrency/build.sh
1 parent ef9ab92 commit 451000d

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

Examples/EmbeddedConcurrency/build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ package_dir="$(cd "$(dirname "$0")" && pwd)"
44
swift package --package-path "$package_dir" \
55
--swift-sdk "${SWIFT_SDK_ID_wasm32_unknown_wasip1:-${SWIFT_SDK_ID:-wasm32-unknown-wasip1}}-embedded" \
66
js --default-platform node
7+
npm -C "$package_dir/.build/plugins/PackageToJS/outputs/Package" install
78
node "$package_dir/run.mjs"

Sources/JavaScriptEventLoop/JSSending.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,32 @@ extension JSSending {
226226
/// - Parameter isolation: The actor isolation context for this call, used in Swift concurrency.
227227
/// - Returns: The received object of type `T`.
228228
/// - Throws: `JSSendingError` if the sending operation fails, or `JSException` if a JavaScript error occurs.
229+
#if compiler(>=6.4)
230+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
231+
public func receive(
232+
isolation: isolated (any Actor)? = #isolation,
233+
file: StaticString = #file,
234+
line: UInt = #line
235+
) async throws(JSException) -> T {
236+
#if _runtime(_multithreaded)
237+
let idInDestination = try await withCheckedThrowingContinuation { continuation in
238+
let context = _JSSendingContext(continuation: continuation)
239+
let idInSource = self.storage.idInSource
240+
let transferring = self.storage.transferring ? [idInSource] : []
241+
swjs_request_sending_object(
242+
idInSource,
243+
transferring,
244+
Int32(transferring.count),
245+
self.storage.sourceTid,
246+
Unmanaged.passRetained(context).toOpaque()
247+
)
248+
}
249+
return storage.construct(JSObject(id: idInDestination))
250+
#else
251+
return storage.construct(storage.sourceObject)
252+
#endif
253+
}
254+
#else
229255
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
230256
public func receive(
231257
isolation: isolated (any Actor)? = #isolation,
@@ -250,6 +276,7 @@ extension JSSending {
250276
return storage.construct(storage.sourceObject)
251277
#endif
252278
}
279+
#endif
253280

254281
// 6.0 and below can't compile the following without a compiler crash.
255282
#if compiler(>=6.1)
@@ -341,11 +368,19 @@ extension JSSending {
341368

342369
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
343370
private final class _JSSendingContext: Sendable {
371+
#if compiler(>=6.4)
372+
let continuation: CheckedContinuation<JavaScriptObjectRef, JSException>
373+
374+
init(continuation: CheckedContinuation<JavaScriptObjectRef, JSException>) {
375+
self.continuation = continuation
376+
}
377+
#else
344378
let continuation: CheckedContinuation<JavaScriptObjectRef, Error>
345379

346380
init(continuation: CheckedContinuation<JavaScriptObjectRef, Error>) {
347381
self.continuation = continuation
348382
}
383+
#endif
349384
}
350385

351386
/// Error type representing failures during JavaScript object sending operations.

Sources/JavaScriptEventLoop/JavaScriptEventLoop+ExecutorFactory.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ extension JavaScriptEventLoop: SchedulingExecutor {
4040
tolerance: C.Duration?,
4141
clock: C
4242
) {
43+
#if !hasFeature(Embedded)
4344
let duration: Duration
4445
// Handle clocks we know
45-
#if !hasFeature(Embedded)
4646
if let _ = clock as? ContinuousClock {
4747
duration = delay as! ContinuousClock.Duration
4848
} else if let _ = clock as? SuspendingClock {
@@ -62,14 +62,14 @@ extension JavaScriptEventLoop: SchedulingExecutor {
6262
fatalError("Unsupported clock type; only ContinuousClock and SuspendingClock are supported")
6363
#endif
6464
}
65-
#else
66-
fatalError("SchedulingExecutor.enqueue is not supported in embedded mode")
67-
#endif
6865
let milliseconds = Self.delayInMilliseconds(from: duration)
6966
self.enqueue(
7067
UnownedJob(job),
7168
withDelay: milliseconds
7269
)
70+
#else
71+
fatalError("SchedulingExecutor.enqueue is not supported in embedded mode")
72+
#endif
7373
}
7474

7575
private static func delayInMilliseconds(from swiftDuration: Duration) -> Double {

Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public final class JSTypedArray<Traits>: JSBridgedClass, ExpressibleByArrayLiter
9898
/// used as the return value for the `withUnsafeBytes(_:)` method. The
9999
/// argument is valid only for the duration of the closure's execution.
100100
/// - Returns: The return value, if any, of the `body` closure parameter.
101-
public func withUnsafeBytes<R>(_ body: (UnsafeBufferPointer<Element>) throws -> R) rethrows -> R {
101+
public func withUnsafeBytes<R, E: Error>(_ body: (UnsafeBufferPointer<Element>) throws(E) -> R) throws(E) -> R {
102102
let buffer = UnsafeMutableBufferPointer<Element>.allocate(capacity: length)
103103
defer { buffer.deallocate() }
104104
copyMemory(to: buffer)
@@ -121,7 +121,7 @@ public final class JSTypedArray<Traits>: JSBridgedClass, ExpressibleByArrayLiter
121121
/// argument is valid only for the duration of the closure's execution.
122122
/// - Returns: The return value, if any, of the `body`async closure parameter.
123123
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
124-
public func withUnsafeBytesAsync<R>(_ body: (UnsafeBufferPointer<Element>) async throws -> R) async rethrows -> R {
124+
public func withUnsafeBytesAsync<R, E: Error>(_ body: (UnsafeBufferPointer<Element>) async throws(E) -> R) async throws(E) -> R {
125125
let buffer = UnsafeMutableBufferPointer<Element>.allocate(capacity: length)
126126
defer { buffer.deallocate() }
127127
copyMemory(to: buffer)

0 commit comments

Comments
 (0)