Skip to content

Commit 575e1ef

Browse files
[CAS] Add a callback version of the makeGlobal API
Provide a callback version of the makeGlobal API for uploading build artifacts on top of the async verison.
1 parent 284250c commit 575e1ef

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

Sources/SwiftDriver/SwiftScan/SwiftScanCAS.swift

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,36 @@ public final class CachedCompilation {
3737
lib.api.swiftscan_cached_compilation_is_uncacheable(ptr)
3838
}
3939

40+
public func makeGlobal(_ callback: @escaping (Swift.Error?) -> ()) {
41+
class CallbackContext {
42+
func retain() -> UnsafeMutableRawPointer {
43+
return Unmanaged.passRetained(self).toOpaque()
44+
}
45+
46+
let comp: CachedCompilation
47+
let callback: (Swift.Error?) -> ()
48+
init(_ compilation: CachedCompilation, _ callback: @escaping (Swift.Error?) -> ()) {
49+
self.comp = compilation
50+
self.callback = callback
51+
}
52+
}
53+
54+
func callbackFunc(_ context: UnsafeMutableRawPointer?, _ error: swiftscan_string_ref_t) {
55+
let obj = Unmanaged<CallbackContext>.fromOpaque(context!).takeRetainedValue()
56+
if error.length != 0 {
57+
if let err = try? obj.comp.lib.toSwiftString(error) {
58+
obj.callback(DependencyScanningError.casError(err))
59+
} else {
60+
obj.callback(DependencyScanningError.casError("unknown makeGlobal error"))
61+
}
62+
}
63+
obj.callback(nil)
64+
}
65+
66+
let context = CallbackContext(self, callback)
67+
lib.api.swiftscan_cached_compilation_make_global_async(ptr, context.retain(), callbackFunc, nil)
68+
}
69+
4070
deinit {
4171
lib.api.swiftscan_cached_compilation_dispose(ptr)
4272
}
@@ -235,35 +265,15 @@ extension swiftscan_cache_replay_result_t {
235265
#if canImport(_Concurrency)
236266
// Async API Vendor
237267
extension CachedCompilation {
238-
public func makeGlobal() async throws -> Bool {
239-
class CallbackContext {
240-
func retain() -> UnsafeMutableRawPointer {
241-
return Unmanaged.passRetained(self).toOpaque()
242-
}
243-
244-
let continuation: CheckedContinuation<Bool, Swift.Error>
245-
let comp: CachedCompilation
246-
init(_ continuation: CheckedContinuation<Bool, Swift.Error>, compilation: CachedCompilation) {
247-
self.continuation = continuation
248-
self.comp = compilation
249-
}
250-
}
251-
252-
func callbackFunc(_ context: UnsafeMutableRawPointer?, _ error: swiftscan_string_ref_t) {
253-
let obj = Unmanaged<CallbackContext>.fromOpaque(context!).takeRetainedValue()
254-
if error.length != 0 {
255-
if let err = try? obj.comp.lib.toSwiftString(error) {
256-
obj.continuation.resume(throwing: DependencyScanningError.casError(err))
268+
public func makeGlobal() async throws {
269+
return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Swift.Error>) in
270+
makeGlobal { (error: Swift.Error?) in
271+
if let err = error {
272+
continuation.resume(throwing: err)
257273
} else {
258-
obj.continuation.resume(throwing: DependencyScanningError.casError("unknown makeGlobal error"))
274+
continuation.resume(returning: ())
259275
}
260276
}
261-
obj.continuation.resume(returning: true)
262-
}
263-
264-
return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Bool, Swift.Error>) in
265-
let context = CallbackContext(continuation, compilation: self)
266-
lib.api.swiftscan_cached_compilation_make_global_async(ptr, context.retain(), callbackFunc, nil)
267277
}
268278
}
269279
}

0 commit comments

Comments
 (0)