fixes for Swift 6 and threading concerns
This commit is contained in:
@@ -17,6 +17,14 @@
|
||||
public enum SnippetHarness {
|
||||
|
||||
/// Wraps `userBody` in the standard preamble.
|
||||
///
|
||||
/// The user body runs inside `MainActor.assumeIsolated { … }` (Phase 203): snippets call
|
||||
/// `@MainActor` API — `context.play(_:)` and `SynthesizerBase` subclass construction
|
||||
/// (both `@MainActor` since Phase 201) — which would not compile from the nonisolated
|
||||
/// `@_cdecl` entry under Swift 6. The `assumeIsolated` closure gives the body main-actor
|
||||
/// isolation at compile time; it is safe at runtime because `LiveSession` always invokes
|
||||
/// the entry on the main actor (`compileAndRunAsync` hops via `MainActor.run`;
|
||||
/// `compileAndRun` is `@MainActor`).
|
||||
public static func wrap(userBody: String) -> WrappedSnippet {
|
||||
let preamble: String = """
|
||||
import LyrebirdSynth
|
||||
@@ -27,14 +35,18 @@ public enum SnippetHarness {
|
||||
guard let ctx else { return }
|
||||
let context = Unmanaged<SnippetContext>.fromOpaque(ctx).takeUnretainedValue()
|
||||
_ = context
|
||||
MainActor.assumeIsolated {
|
||||
|
||||
"""
|
||||
// Count newlines in the preamble: the user's body starts on the next line.
|
||||
// `components(separatedBy: "\n")` over a string ending in "\n" yields N+1
|
||||
// elements (the last is empty); offset = count of those elements = lines
|
||||
// before the user body + 1 (the line on which the body starts).
|
||||
// before the user body + 1 (the line on which the body starts). The
|
||||
// `MainActor.assumeIsolated {` line is part of the preamble, so the offset
|
||||
// already accounts for it and diagnostic line-mapping stays correct.
|
||||
let lineOffset: Int = preamble.components(separatedBy: "\n").count
|
||||
let suffix: String = "\n}\n"
|
||||
// Close the `assumeIsolated` closure, then the entry function.
|
||||
let suffix: String = "\n }\n}\n"
|
||||
let source: String = preamble + userBody + suffix
|
||||
return WrappedSnippet(source: source, userBodyLineOffset: lineOffset)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user