// CompiledSnippet.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT // LyrebirdLiveCompile — core // // `CompiledSnippet` owns the `dlopen` handle and the resolved entry function // pointer for one snippet's dylib. Held by the live session for the lifetime of // the process — dylib unload is unsafe while live `Synth` instances may still // reference types defined in the dylib. (See plan in // `memory/project_live_compile_phases.md` non-goals.) import Foundation #if os(macOS) || os(Linux) /// Handle to one successfully compiled and loaded snippet. /// /// Intentionally a `final class` (not a `struct`): owning the `dlopen` handle /// via reference identity means moves / copies cannot duplicate or accidentally /// drop ownership. There is no `deinit` that calls `dlclose` — see the file /// header. /// /// `@unchecked Sendable`: every stored field is `let`; the `dlopen` handle and /// the resolved entry pointer are both thread-safe to hold and read after the /// compile completes (the loader serialises symbol resolution). Allows /// `LiveSession.compileAndRunAsync` to return one across a `Task.detached` /// boundary. public final class CompiledSnippet: @unchecked Sendable { /// Module name passed to `swiftc -module-name`; useful for diagnostics. public let moduleName: String /// Path to the dylib that was `dlopen`'d. public let dylibPath: String /// Resolved entry function pointer. The snippet harness calls this. public let entry: SnippetEntry /// Raw `dlopen` handle. Kept so the OS keeps the dylib mapped. public let handle: UnsafeMutableRawPointer init( moduleName: String, dylibPath: String, entry: SnippetEntry, handle: UnsafeMutableRawPointer ) { self.moduleName = moduleName self.dylibPath = dylibPath self.entry = entry self.handle = handle } } #endif