1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
use crate::{
bindings::hostfxr::{
get_function_pointer_fn, hostfxr_delegate_type, hostfxr_handle,
load_assembly_and_get_function_pointer_fn,
},
pdcstring::{PdCStr, PdCString},
Error,
};
use std::{
collections::HashMap,
ffi::c_void,
marker::PhantomData,
mem::{self, MaybeUninit},
ptr::{self, NonNull},
};
use super::{
AssemblyDelegateLoader, DelegateLoader, HostExitCode, Hostfxr, KnownHostExitCode,
MethodWithUnknownSignature,
};
/// A marker struct indicating that the context was initialized with a runtime config.
/// This means that it is not possible to run the application associated with the context.
pub struct InitializedForRuntimeConfig;
/// A marker struct indicating that the context was initialized for the dotnet command line.
/// This means that it is possible to run the application associated with the context.
pub struct InitializedForCommandLine;
/// Handle of a loaded [`HostfxrContext`].
#[derive(Debug, Clone, Copy)]
pub(crate) struct HostfxrHandle(NonNull<c_void>);
impl HostfxrHandle {
// pub(crate) fn new(ptr: hostfxr_handle) -> Option<Self> {
// NonNull::new(ptr as *mut _).map(Self)
// }
pub(crate) unsafe fn new_unchecked(ptr: hostfxr_handle) -> Self {
Self(unsafe { NonNull::new_unchecked(ptr as *mut _) })
}
pub(crate) fn as_raw(&self) -> hostfxr_handle {
self.0.as_ptr()
}
}
impl From<HostfxrHandle> for hostfxr_handle {
fn from(handle: HostfxrHandle) -> Self {
handle.as_raw()
}
}
/// State which hostfxr creates and maintains and represents a logical operation on the hosting components.
#[derive(Clone)]
pub struct HostfxrContext<'a, I> {
handle: HostfxrHandle,
hostfxr: &'a Hostfxr,
context_type: PhantomData<&'a I>,
}
impl<'a, I> HostfxrContext<'a, I> {
pub(crate) fn new(handle: HostfxrHandle, hostfxr: &'a Hostfxr) -> Self {
Self {
handle,
hostfxr,
context_type: PhantomData,
}
}
/// Gets the runtime property value for the given key of this host context.
pub fn get_runtime_property_value_owned(
&self,
name: impl AsRef<PdCStr>,
) -> Result<PdCString, Error> {
unsafe { self.get_runtime_property_value_borrowed(name) }.map(PdCStr::to_owned)
}
/// Gets the runtime property value for the given key of this host context.
///
/// # Safety
/// The value string is owned by the host context. The lifetime of the buffer is only
/// guaranteed until any of the below occur:
/// * [`run_app`] is called for this host context
/// * properties are changed via [`set_runtime_property_value`] or [`remove_runtime_property_value`]
/// * the host context is dropped
///
/// [`run_app`]: HostfxrContext::run_app
/// [`set_runtime_property_value`]: HostfxrContext::set_runtime_property_value
/// [`remove_runtime_property_value`]: HostfxrContext::remove_runtime_property_value
pub unsafe fn get_runtime_property_value_borrowed(
&self,
name: impl AsRef<PdCStr>,
) -> Result<&'a PdCStr, Error> {
let mut value = MaybeUninit::uninit();
let result = unsafe {
self.hostfxr.lib.hostfxr_get_runtime_property_value(
self.handle.as_raw(),
name.as_ref().as_ptr(),
value.as_mut_ptr(),
)
};
HostExitCode::from(result).to_result()?;
Ok(unsafe { PdCStr::from_str_ptr(value.assume_init()) })
}
/// Sets the value of a runtime property for this host context.
pub fn set_runtime_property_value(
&self,
name: impl AsRef<PdCStr>,
value: impl AsRef<PdCStr>,
) -> Result<(), Error> {
let result = unsafe {
self.hostfxr.lib.hostfxr_set_runtime_property_value(
self.handle.as_raw(),
name.as_ref().as_ptr(),
value.as_ref().as_ptr(),
)
};
HostExitCode::from(result).to_result()
}
/// Remove a runtime property for this host context.
pub fn remove_runtime_property_value(&self, name: impl AsRef<PdCStr>) -> Result<(), Error> {
let result = unsafe {
self.hostfxr.lib.hostfxr_set_runtime_property_value(
self.handle.as_raw(),
name.as_ref().as_ptr(),
ptr::null(),
)
};
HostExitCode::from(result).to_result()
}
/// Get all runtime properties for this host context.
///
/// # Safety
/// The strings returned are owned by the host context. The lifetime of the buffers is only
/// guaranteed until any of the below occur:
/// * [`run_app`] is called for this host context
/// * properties are changed via [`set_runtime_property_value`] or [`remove_runtime_property_value`]
/// * the host context is dropped
///
/// [`run_app`]: HostfxrContext::run_app
/// [`set_runtime_property_value`]: HostfxrContext::set_runtime_property_value
/// [`remove_runtime_property_value`]: HostfxrContext::remove_runtime_property_value
pub unsafe fn get_runtime_properties_borrowed(
&self,
) -> Result<(Vec<&'a PdCStr>, Vec<&'a PdCStr>), Error> {
// get count
let mut count = MaybeUninit::uninit();
let result = unsafe {
self.hostfxr.lib.hostfxr_get_runtime_properties(
self.handle.as_raw(),
count.as_mut_ptr(),
ptr::null_mut(),
ptr::null_mut(),
)
};
// ignore buffer too small error
match HostExitCode::from(result).to_result() {
Err(Error::Hostfxr(HostExitCode::Known(KnownHostExitCode::HostApiBufferTooSmall))) => {
Ok(())
}
res => res,
}?;
// get values / fill buffer
let mut count = unsafe { count.assume_init() };
let mut keys = Vec::with_capacity(count);
let mut values = Vec::with_capacity(count);
let result = unsafe {
self.hostfxr.lib.hostfxr_get_runtime_properties(
self.handle.as_raw(),
&mut count,
keys.as_mut_ptr(),
values.as_mut_ptr(),
)
};
HostExitCode::from(result).to_result()?;
unsafe { keys.set_len(count) };
unsafe { values.set_len(count) };
let keys = keys
.into_iter()
.map(|e| unsafe { PdCStr::from_str_ptr(e) })
.collect();
let values = values
.into_iter()
.map(|e| unsafe { PdCStr::from_str_ptr(e) })
.collect();
Ok((keys, values))
}
/// Get all runtime properties for this host context as owned strings.
pub fn get_runtime_properties_owned(&self) -> Result<(Vec<PdCString>, Vec<PdCString>), Error> {
unsafe { self.get_runtime_properties_borrowed() }.map(|(keys, values)| {
let owned_keys = keys.into_iter().map(|key| key.to_owned()).collect();
let owned_values = values.into_iter().map(|value| value.to_owned()).collect();
(owned_keys, owned_values)
})
}
/// Get all runtime properties for this host context as an iterator over borrowed key-value pairs.
///
/// # Safety
/// The strings returned are owned by the host context. The lifetime of the buffers is only
/// guaranteed until any of the below occur:
/// * [`run_app`] is called for this host context
/// * properties are changed via [`set_runtime_property_value`] or [`remove_runtime_property_value`]
/// * the host context is dropped
///
/// [`run_app`]: HostfxrContext::run_app
/// [`set_runtime_property_value`]: HostfxrContext::set_runtime_property_value
/// [`remove_runtime_property_value`]: HostfxrContext::remove_runtime_property_value
pub unsafe fn get_runtime_properties_iter_borrowed(
&self,
) -> Result<impl Iterator<Item = (&'a PdCStr, &'a PdCStr)>, Error> {
unsafe { self.get_runtime_properties_borrowed() }
.map(|(keys, values)| keys.into_iter().zip(values.into_iter()))
}
/// Get all runtime properties for this host context as an iterator over owned key-value pairs.
pub fn get_runtime_properties_iter_owned(
&self,
) -> Result<impl Iterator<Item = (PdCString, PdCString)>, Error> {
self.get_runtime_properties_owned()
.map(|(keys, values)| keys.into_iter().zip(values.into_iter()))
}
/// Get all runtime properties for this host context as an hashmap of borrowed strings.
///
/// # Safety
/// The strings returned are owned by the host context. The lifetime of the buffers is only
/// guaranteed until any of the below occur:
/// * [`run_app`] is called for this host context
/// * properties are changed via [`set_runtime_property_value`] or [`remove_runtime_property_value`]
/// * the host context is dropped
///
/// [`run_app`]: HostfxrContext::run_app
/// [`set_runtime_property_value`]: HostfxrContext::set_runtime_property_value
/// [`remove_runtime_property_value`]: HostfxrContext::remove_runtime_property_value
pub unsafe fn get_runtime_properties_borrowed_as_map(
&self,
) -> Result<HashMap<&'a PdCStr, &'a PdCStr>, Error> {
unsafe { self.get_runtime_properties_borrowed() }
.map(|(keys, values)| keys.into_iter().zip(values.into_iter()).collect())
}
/// Get all runtime properties for this host context as an hashmap of owned strings.
pub fn get_runtime_properties_owned_as_map(
&self,
) -> Result<HashMap<PdCString, PdCString>, Error> {
self.get_runtime_properties_iter_owned()
.map(|iter| iter.collect())
}
/// Gets a typed delegate from the currently loaded CoreCLR or from a newly created one.
/// You propably want to use [`get_delegate_loader`] or [`get_delegate_loader_for_assembly`]
/// instead of this function if you want to load function pointers.
///
/// # Remarks
/// If the context was initialized using [`initialize_for_runtime_config`], then all delegate types are supported.
/// If it was initialized using [`initialize_for_dotnet_command_line`], then only the following
/// delegate types are currently supported:
/// * [`hdt_load_assembly_and_get_function_pointer`]
/// * [`hdt_get_function_pointer`]
///
/// [`get_delegate_loader`]: HostfxrContext::get_delegate_loader
/// [`get_delegate_loader_for_assembly`]: HostfxrContext::get_delegate_loader_for_assembly
/// [`hdt_load_assembly_and_get_function_pointer`]: hostfxr_delegate_type::hdt_load_assembly_and_get_function_pointer
/// [`hdt_get_function_pointer`]: hostfxr_delegate_type::hdt_get_function_pointer
/// [`initialize_for_runtime_config`]: Hostfxr::initialize_for_runtime_config
/// [`initialize_for_dotnet_command_line`]: Hostfxr::initialize_for_dotnet_command_line
pub fn get_runtime_delegate(
&self,
r#type: hostfxr_delegate_type,
) -> Result<MethodWithUnknownSignature, Error> {
let mut delegate = MaybeUninit::uninit();
let result = unsafe {
self.hostfxr.lib.hostfxr_get_runtime_delegate(
self.handle.as_raw(),
r#type,
delegate.as_mut_ptr(),
)
};
HostExitCode::from(result).to_result()?;
Ok(unsafe { mem::transmute(delegate.assume_init()) })
}
fn get_load_assembly_and_get_function_pointer_delegate(
&self,
) -> Result<load_assembly_and_get_function_pointer_fn, Error> {
unsafe {
self.get_runtime_delegate(
hostfxr_delegate_type::hdt_load_assembly_and_get_function_pointer,
)
.map(|ptr| mem::transmute(ptr))
}
}
fn get_get_function_pointer_delegate(&self) -> Result<get_function_pointer_fn, Error> {
unsafe {
self.get_runtime_delegate(hostfxr_delegate_type::hdt_get_function_pointer)
.map(|ptr| mem::transmute(ptr))
}
}
/// Gets a delegate loader for loading an assembly and contained function pointers.
pub fn get_delegate_loader(&self) -> Result<DelegateLoader, Error> {
Ok(DelegateLoader {
get_load_assembly_and_get_function_pointer: self
.get_load_assembly_and_get_function_pointer_delegate()?,
get_function_pointer: self.get_get_function_pointer_delegate()?,
})
}
/// Gets a delegate loader for loading function pointers of the assembly with the given path.
/// The assembly will be loaded lazily when the first function pointer is loaded.
pub fn get_delegate_loader_for_assembly<A: AsRef<PdCStr>>(
&self,
assembly_path: A,
) -> Result<AssemblyDelegateLoader<A>, Error> {
self.get_delegate_loader()
.map(|loader| AssemblyDelegateLoader::new(loader, assembly_path))
}
unsafe fn close(&self) -> Result<(), Error> {
unsafe { self.hostfxr.lib.hostfxr_close(self.handle.as_raw()) };
Ok(())
}
}
impl<'a> HostfxrContext<'a, InitializedForCommandLine> {
/// Load CoreCLR and run the application.
///
/// # Return value
/// If the app was successfully run, the exit code of the application. Otherwise, the error code result.
pub fn run_app(&self) -> HostExitCode {
let result = unsafe { self.hostfxr.lib.hostfxr_run_app(self.handle.as_raw()) };
HostExitCode::from(result)
}
}
impl<I> Drop for HostfxrContext<'_, I> {
fn drop(&mut self) {
let _ = unsafe { self.close() };
}
}