Skip to content

Commit b8a4f77

Browse files
Fix typed-throws JSRemote compilation on 6.3 build-examples
Co-authored-by: Shelley <shelley@exe.dev>
1 parent d783df1 commit b8a4f77

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

Sources/JavaScriptEventLoop/JSRemote.swift

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,48 @@ extension JSRemote where T == JSObject {
3535
public func withJSObject<R: Sendable, E: Error>(
3636
_ body: @Sendable @escaping (JSObject) throws(E) -> R
3737
) async throws(E) -> sending R {
38-
do throws(E) {
39-
return try await _withJSObjectAnyError { object in
40-
try body(object)
41-
}
42-
} catch let error as E {
43-
throw error
44-
} catch {
45-
preconditionFailure("Unexpected JSRemote error type: \(error)")
38+
#if _runtime(_multithreaded)
39+
if storage.sourceTid == swjs_get_worker_thread_id_cached() {
40+
return try body(storage.sourceObject)
4641
}
42+
return try await withCheckedThrowingContinuation { continuation in
43+
let context = _JSRemoteContext(
44+
sourceObject: storage.sourceObject,
45+
body: body,
46+
continuation: continuation
47+
)
48+
swjs_request_remote_jsobject_body(
49+
storage.sourceTid,
50+
Unmanaged.passRetained(context).toOpaque()
51+
)
52+
}
53+
#else
54+
return try body(storage.sourceObject)
55+
#endif
56+
}
57+
58+
@available(*, deprecated, message: "Use typed-throws withJSObject(_:)")
59+
private func _withJSObjectAnyError<R: Sendable>(
60+
_ body: @Sendable @escaping (JSObject) throws(any Error) -> R
61+
) async throws(any Error) -> sending R {
62+
#if _runtime(_multithreaded)
63+
if storage.sourceTid == swjs_get_worker_thread_id_cached() {
64+
return try body(storage.sourceObject)
65+
}
66+
return try await withCheckedThrowingContinuation { continuation in
67+
let context = _JSRemoteContext(
68+
sourceObject: storage.sourceObject,
69+
body: body,
70+
continuation: continuation
71+
)
72+
swjs_request_remote_jsobject_body(
73+
storage.sourceTid,
74+
Unmanaged.passRetained(context).toOpaque()
75+
)
76+
}
77+
#else
78+
return try body(storage.sourceObject)
79+
#endif
4780
}
4881

4982
private func _withJSObjectAnyError<R: Sendable>(

0 commit comments

Comments
 (0)