1pub mod bindings;
13#[cfg(all(feature = "coreclr", target_arch = "x86_64"))]
14pub mod attach;
15pub mod console;
16#[cfg(feature = "coreclr")]
17mod coreclr;
18mod error;
19#[cfg(feature = "coreclr")]
20pub mod profiler;
21#[cfg(feature = "hao")]
22pub mod rewrite;
23#[cfg(feature = "mono")]
24mod mono;
25
26use std::{ffi::c_void, path::Path, sync::Arc};
27
28pub use bindings::{BoundAssembly, Object};
29pub use error::{Error, Result};
30
31#[cfg(feature = "coreclr")]
32pub use coreclr::{find_dotnet_root, CoreClrOptions};
33#[cfg(feature = "mono")]
34pub use mono::MonoOptions;
35
36#[derive(Debug, Clone, Copy, PartialEq, Eq)]
37#[cfg_attr(feature = "reflect", derive(facet::Facet))]
38#[cfg_attr(feature = "reflect", repr(C))]
39pub enum Backend {
40 CoreClr,
41 Mono,
42}
43
44enum Inner {
45 #[cfg(feature = "coreclr")]
46 CoreClr(coreclr::CoreClr),
47 #[cfg(feature = "mono")]
48 Mono(mono::Mono),
49}
50
51#[derive(Clone)]
53pub struct Runtime(Arc<Inner>);
54
55impl std::fmt::Debug for Runtime {
56 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57 write!(f, "Runtime({:?})", self.backend())
58 }
59}
60
61impl Runtime {
62 #[cfg(feature = "coreclr")]
65 pub fn coreclr() -> Result<Self> {
66 Self::coreclr_with(&CoreClrOptions::default())
67 }
68
69 #[cfg(feature = "coreclr")]
71 pub fn coreclr_with(options: &CoreClrOptions) -> Result<Self> {
72 Self::coreclr_from(|| coreclr::CoreClr::new(options))
73 }
74
75 #[cfg(feature = "coreclr")]
82 pub fn coreclr_attach(
83 host_handle: *mut c_void,
84 domain_id: u32,
85 create_delegate: *const c_void,
86 major: u16,
87 ) -> Result<Self> {
88 Self::coreclr_from(|| {
89 coreclr::CoreClr::attach(host_handle, domain_id, create_delegate, major)
90 })
91 }
92
93 #[cfg(feature = "coreclr")]
94 fn coreclr_from(make: impl FnOnce() -> Result<coreclr::CoreClr>) -> Result<Self> {
95 static RUNTIME: std::sync::Mutex<Option<Runtime>> = std::sync::Mutex::new(None);
96 let mut runtime = RUNTIME.lock().unwrap_or_else(|e| e.into_inner());
97 if let Some(runtime) = runtime.as_ref() {
98 return Ok(runtime.clone());
99 }
100 let started = Runtime(Arc::new(Inner::CoreClr(make()?)));
101 *runtime = Some(started.clone());
102 Ok(started)
103 }
104
105 #[cfg(feature = "mono")]
107 pub fn mono(options: &MonoOptions) -> Result<Self> {
108 Self::mono_with(|| mono::Mono::new(options))
109 }
110
111 #[cfg(feature = "mono")]
116 pub fn mono_attach(options: &MonoOptions) -> Result<Self> {
117 Self::mono_with(|| mono::Mono::attach(options))
118 }
119
120 #[cfg(feature = "mono")]
121 fn mono_with(make: impl FnOnce() -> Result<mono::Mono>) -> Result<Self> {
122 static RUNTIME: std::sync::Mutex<Option<Runtime>> = std::sync::Mutex::new(None);
123 let mut runtime = RUNTIME.lock().unwrap_or_else(|e| e.into_inner());
124 if let Some(runtime) = runtime.as_ref() {
125 return Ok(runtime.clone());
126 }
127 let started = Runtime(Arc::new(Inner::Mono(make()?)));
128 *runtime = Some(started.clone());
129 Ok(started)
130 }
131
132 pub fn backend(&self) -> Backend {
133 match &*self.0 {
134 #[cfg(feature = "coreclr")]
135 Inner::CoreClr(_) => Backend::CoreClr,
136 #[cfg(feature = "mono")]
137 Inner::Mono(_) => Backend::Mono,
138 }
139 }
140
141 pub fn attach_thread(&self) {
144 match &*self.0 {
145 #[cfg(feature = "coreclr")]
146 Inner::CoreClr(_) => {}
147 #[cfg(feature = "mono")]
148 Inner::Mono(mono) => mono.attach_thread(),
149 }
150 }
151
152 pub fn load_assembly(&self, path: impl AsRef<Path>) -> Result<()> {
154 match &*self.0 {
155 #[cfg(feature = "coreclr")]
156 Inner::CoreClr(clr) => clr.load_assembly(path.as_ref()),
157 #[cfg(feature = "mono")]
158 Inner::Mono(mono) => mono.load_assembly(path.as_ref()),
159 }
160 }
161
162 pub fn load_assembly_bytes(&self, name: &str, bytes: &[u8]) -> Result<()> {
164 match &*self.0 {
165 #[cfg(feature = "coreclr")]
166 Inner::CoreClr(clr) => {
167 let _ = name;
168 clr.load_assembly_bytes(bytes)
169 }
170 #[cfg(feature = "mono")]
171 Inner::Mono(mono) => mono.load_assembly_bytes(name, bytes),
172 }
173 }
174
175 #[cfg(feature = "hao")]
177 pub fn load_module(&self, module: &hao::Module) -> Result<()> {
178 let bytes = module
179 .write()
180 .map_err(|e| Error::Other(format!("could not write {}: {}", module.name(), e)))?;
181 let name = match &module.assembly {
182 Some(assembly) => assembly.get().name.clone(),
183 None => module.name().trim_end_matches(".dll").to_string(),
184 };
185 self.load_assembly_bytes(&name, &bytes)
186 }
187
188 pub fn function_pointer(
195 &self,
196 assembly: &str,
197 type_name: &str,
198 method: &str,
199 ) -> Result<*const c_void> {
200 match &*self.0 {
201 #[cfg(feature = "coreclr")]
202 Inner::CoreClr(clr) => clr.function_pointer(assembly, type_name, method),
203 #[cfg(feature = "mono")]
204 Inner::Mono(mono) => mono.function_pointer(assembly, type_name, method),
205 }
206 }
207
208 pub fn method_pointer(
218 &self,
219 assembly: &str,
220 type_name: &str,
221 method: &str,
222 ) -> Result<*const c_void> {
223 match &*self.0 {
224 #[cfg(feature = "coreclr")]
225 Inner::CoreClr(clr) => clr.method_pointer(assembly, type_name, method),
226 #[cfg(feature = "mono")]
227 Inner::Mono(mono) => mono.method_pointer(assembly, type_name, method),
228 }
229 }
230}