b6fdee8bfc
Consolidated release: the cross-platform Linux port (engine, generators, CLI, REPL, NRT, portable DSP), live ALSA audio + ALSA MIDI + POSIX OSC + portable WAV codec, the GTK4 GUI (LyrebirdUI on the SwiftOpenUI backend), and the Linux live-coding scratchpad (LiveCompile). Apple platforms unchanged; all additive. See RELEASES.md and LINUX_README.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
57 lines
2.5 KiB
Swift
57 lines
2.5 KiB
Swift
// SnippetHarness.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// LyrebirdLiveCompile
|
|
//
|
|
// `SnippetHarness.wrap(userBody:)` produces the full Swift source that
|
|
// `LiveCompiler.compile(source:)` feeds to swiftc. The preamble imports the
|
|
// modules the snippet needs, declares the C ABI entry, and binds `context: SnippetContext`
|
|
// from the opaque pointer the host hands in. The user's body is dropped in as
|
|
// the function body.
|
|
|
|
#if os(macOS) || os(Linux)
|
|
|
|
public enum SnippetHarness {
|
|
|
|
/// Wraps `userBody` in the standard preamble.
|
|
///
|
|
/// The user body runs inside `MainActor.assumeIsolated { … }`: snippets call
|
|
/// `@MainActor` API — `context.play(_:)` and `SynthesizerBase` subclass construction
|
|
/// (both `@MainActor`) — 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 Foundation
|
|
import LyrebirdSynth
|
|
import LyrebirdLiveCompile
|
|
|
|
@_cdecl("lyrebird_snippet_entry")
|
|
public func lyrebird_snippet_entry(_ ctx: UnsafeMutableRawPointer?) {
|
|
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). 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
|
|
// 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)
|
|
}
|
|
}
|
|
|
|
#endif
|