lean_rs_host/host/capabilities.rs
1//! `LeanCapabilities` — loaded user, generic interop, and host shim
2//! dylibs with session symbol addresses pre-resolved.
3//!
4//! [`LeanCapabilities`] owns three [`lean_rs::module::LeanLibrary`]
5//! handles and caches the session symbol addresses that
6//! [`crate::host::LeanSession`] dispatches through:
7//!
8//! - The **user's capability dylib** is the artefact the consumer
9//! built with `lake build` and named in
10//! [`crate::host::LeanHost::load_capabilities`]. It contains the
11//! user's own `@[export]` symbols ([`crate::LeanSession::call_capability`]
12//! dispatches here).
13//! - The **generic interop dylib** is
14//! `liblean__rs__interop__shims_LeanRsInterop.dylib`; it carries
15//! reusable callback helpers imported by the host progress shims.
16//! - The **shim dylib** is `liblean__rs__host__shims_LeanRsHostShims.dylib`,
17//! built from the `lean-rs-host` crate's bundled shim sources. It contains
18//! the 26 mandatory + 4 optional `lean_rs_host_*` `@[export]` symbols that
19//! every typed `LeanSession` method dispatches through. Lake does *not*
20//! transitively bundle the shim's `@[export]` symbols into the user's dylib
21//! (verified at carve-out time, 2026-05-18 — `LeanLib.sharedFacet` is a
22//! per-package shared library, not a transitive merge), so the host stack
23//! loads the generic interop dylib, host shim dylib, and user dylib
24//! explicitly. All dylibs share one Lean runtime; each per-module
25//! `initialize_<Module>` short-circuits idempotently on its own flag.
26//!
27//! A missing mandatory shim symbol fails capability load; a missing
28//! meta-service symbol degrades to a synthesised
29//! [`crate::host::meta::LeanMetaResponse::Unsupported`] at the
30//! [`crate::LeanSession::run_meta`] call site. Pre-resolution at
31//! construction means each later query is one struct-field read and
32//! one FFI call — no per-query `dlsym`.
33//!
34//! Construction goes through [`crate::host::LeanHost::load_capabilities`];
35//! [`LeanCapabilities::session`] then imports a module list and returns
36//! the long-lived [`crate::host::LeanSession`] handle.
37
38use core::fmt;
39
40use crate::host::cancellation::LeanCancellationToken;
41use crate::host::host::LeanHost;
42use crate::host::progress::LeanProgressSink;
43use crate::host::session::{LeanSession, SessionSymbols};
44use lean_rs::error::LeanResult;
45use lean_rs::module::LeanLibrary;
46
47/// Loaded capability, generic interop, and host shim dylibs with session
48/// symbol addresses pre-resolved.
49///
50/// Owns the [`LeanLibrary`] handles so callers do not have to track
51/// the dylibs' lifetimes separately. Borrows from the parent
52/// [`LeanHost`] for the runtime + project context. Neither [`Send`] nor
53/// [`Sync`]: inherited from the contained `LeanLibrary` handles.
54pub struct LeanCapabilities<'lean, 'h> {
55 host: &'h LeanHost<'lean>,
56 /// User's capability dylib — the one named in `load_capabilities`.
57 /// `pub(crate)` accessor below exposes it to
58 /// [`crate::LeanSession::call_capability`] for ad-hoc dispatch on
59 /// user-authored `@[export]` symbols.
60 user_library: LeanLibrary<'lean>,
61 /// Generic interop shim dylib carrying reusable callback helpers used by
62 /// host progress shims. Loaded globally before the host shim dylib so
63 /// generated `LeanRsInterop.*` initializer references resolve.
64 #[allow(dead_code, reason = "Drop releases the dylib; field is structurally required")]
65 interop_library: LeanLibrary<'lean>,
66 /// Shim dylib carrying the 26+4 `lean_rs_host_*` `@[export]`
67 /// symbols. RAII anchor only — the addresses inside `symbols`
68 /// outlive any direct read of this field.
69 #[allow(dead_code, reason = "Drop releases the dylib; field is structurally required")]
70 shim_library: LeanLibrary<'lean>,
71 symbols: SessionSymbols,
72}
73
74impl fmt::Debug for LeanCapabilities<'_, '_> {
75 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76 f.debug_struct("LeanCapabilities").finish_non_exhaustive()
77 }
78}
79
80impl<'lean, 'h> LeanCapabilities<'lean, 'h> {
81 /// Build a [`LeanCapabilities`] from an opened user-capability
82 /// library, opening the generic interop and host shim dylibs alongside it.
83 ///
84 /// Initializes the root module of the user's dylib (idempotent
85 /// through Lean's `_G_initialized` short-circuit), opens and
86 /// initializes the generic interop and host shim dylibs built from the
87 /// crate-owned shim sources, and resolves the
88 /// session-dispatch symbol addresses from the **shim** dylib: 26
89 /// mandatory baseline symbols (load failure on miss) and 4
90 /// optional meta-service symbols (missing entries stored as
91 /// `None`). Both [`lean_rs::module::LeanModule`] handles are
92 /// dropped at the end of this call — the cached symbol addresses
93 /// outlive any single module borrow.
94 ///
95 /// # Errors
96 ///
97 /// Returns [`lean_rs::LeanError::Host`] with stage
98 /// [`lean_rs::HostStage::Load`] /
99 /// [`lean_rs::LeanDiagnosticCode::ModuleInit`] if the bundled shim dylibs
100 /// cannot be built or located. Returns [`lean_rs::HostStage::Link`] if the
101 /// initializer or any of the 26 **mandatory** symbols is missing from the
102 /// shim dylib. Missing optional meta-service symbols never fail capability
103 /// load.
104 pub(crate) fn new(
105 host: &'h LeanHost<'lean>,
106 user_library: LeanLibrary<'lean>,
107 package: &str,
108 lib_name: &str,
109 ) -> LeanResult<Self> {
110 // Load order matters. The host shim imports LeanRsInterop.Callback,
111 // so the generic interop dylib must be global before the host shim
112 // initializer runs. Consumers no longer require host shims in their
113 // Lake package; the host stack opens and initializes both bundled shim
114 // dylibs explicitly before the user dylib initializes.
115 let interop_dylib_path = crate::host::lake::LakeProject::interop_dylib()?;
116 let interop_library = LeanLibrary::open_globally(host.runtime(), &interop_dylib_path)?;
117 let _interop_module = interop_library.initialize_module(
118 crate::host::lake::INTEROP_PACKAGE_NAME,
119 crate::host::lake::INTEROP_LIB_NAME,
120 )?;
121
122 let shim_dylib_path = crate::host::lake::LakeProject::shim_dylib()?;
123 let shim_library = LeanLibrary::open_globally(host.runtime(), &shim_dylib_path)?;
124 // Explicitly initialize the shim's root module too, so the
125 // shim's @[export] functions are live regardless of whether
126 // the user's chain reaches them transitively.
127 let _shim_module =
128 shim_library.initialize_module(crate::host::lake::SHIM_PACKAGE_NAME, crate::host::lake::SHIM_LIB_NAME)?;
129
130 // Now the user dylib. It does not need to depend on the host shims;
131 // ad-hoc user exports still resolve from this library.
132 let _user_module = user_library.initialize_module(package, lib_name)?;
133
134 // The 26 mandatory + 4 optional `lean_rs_host_*` symbols live
135 // in the shim dylib; resolve them there.
136 // `LeanSession::call_capability` (separately) routes ad-hoc
137 // user-authored `@[export]` symbols through `user_library`.
138 let symbols = SessionSymbols::resolve(&shim_library)?;
139 Ok(Self {
140 host,
141 user_library,
142 interop_library,
143 shim_library,
144 symbols,
145 })
146 }
147
148 /// Import the named modules into a fresh Lean environment and
149 /// return a session over the result.
150 ///
151 /// Imports happen exactly once per `session()` call. The returned
152 /// [`LeanSession`] owns the imported environment and reuses this
153 /// capability's cached symbol addresses for every query.
154 ///
155 /// # Errors
156 ///
157 /// Returns [`lean_rs::LeanError::Cancelled`] if `cancellation` is
158 /// already cancelled before import dispatch.
159 ///
160 /// Returns [`lean_rs::LeanError::LeanException`] if the Lean-side
161 /// import raises (missing `.olean`, malformed module name, …),
162 /// with the bounded message Lean surfaced.
163 pub fn session<'c>(
164 &'c self,
165 imports: &[&str],
166 cancellation: Option<&LeanCancellationToken>,
167 progress: Option<&dyn LeanProgressSink>,
168 ) -> LeanResult<LeanSession<'lean, 'c>> {
169 LeanSession::import(self, imports, cancellation, progress)
170 }
171
172 /// The capability's parent host (for runtime + project access by
173 /// the session dispatch).
174 pub(crate) fn host(&self) -> &'h LeanHost<'lean> {
175 self.host
176 }
177
178 /// The pre-resolved session symbol addresses.
179 pub(crate) fn symbols(&self) -> &SessionSymbols {
180 &self.symbols
181 }
182
183 /// The user's owned capability [`LeanLibrary`].
184 ///
185 /// `pub(crate)` so [`crate::LeanSession::call_capability`] can
186 /// resolve ad-hoc function symbols on the user's dylib without
187 /// holding a separate library borrow. Ad-hoc calls always go to
188 /// the user's dylib, not the shim dylib: the shim dylib hosts a
189 /// fixed contract (the 26+4 `lean_rs_host_*` symbols pre-resolved
190 /// in `symbols`); arbitrary user `@[export]` symbols live in the
191 /// user's dylib.
192 pub(crate) fn library(&self) -> &LeanLibrary<'lean> {
193 &self.user_library
194 }
195}