Skip to content

Commit

Permalink
Add support for loading older hostfxrs with newer feature flags
Browse files Browse the repository at this point in the history
  • Loading branch information
OpenByteDev committed Aug 13, 2023
1 parent 206cb63 commit 0c2e22c
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ keywords = ["nethost", "hostfxr", "dotnet", "bindings", "coreclr"]
num_enum = { version = "0.6", default-features = false }
thiserror = { version = "1.0", default-features = false }
derive_more = { version = "0.99", features = ["deref", "from", "display"], default-features = false }
hostfxr-sys = { version = "0.9", features = ["enum-map", "undocumented"], default-features = false }
hostfxr-sys = { version = "0.10", features = ["enum-map", "undocumented", "wrapper", "optional-apis"], default-features = false }
coreclr-hosting-shared = { version = "0.1", default-features = false }
destruct-drop = { version = "0.2", default-features = false }
ffi-opaque = { version = "2.0", default-features = false }
Expand Down
6 changes: 3 additions & 3 deletions src/hostfxr/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ impl<I> HostfxrContext<I> {
r#type,
delegate.as_mut_ptr(),
)
};
}.unwrap();

HostingResult::from(result).into_result()?;

Expand Down Expand Up @@ -303,7 +303,7 @@ impl<I> HostfxrContext<I> {

/// Internal non-consuming version of [`close`](HostfxrContext::close)
unsafe fn _close(&self) -> Result<HostingSuccess, HostingError> {
let result = unsafe { self.hostfxr.hostfxr_close(self.handle.as_raw()) };
let result = unsafe { self.hostfxr.hostfxr_close(self.handle.as_raw()) }.unwrap();
HostingResult::from(result).into_result()
}
}
Expand All @@ -315,7 +315,7 @@ impl HostfxrContext<InitializedForCommandLine> {
/// If the app was successfully run, the exit code of the application. Otherwise, the error code result.
#[must_use]
pub fn run_app(self) -> AppOrHostingResult {
let result = unsafe { self.hostfxr.hostfxr_run_app(self.handle.as_raw()) };
let result = unsafe { self.hostfxr.hostfxr_run_app(self.handle.as_raw()) }.unwrap();
AppOrHostingResult::from(result)
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/hostfxr/library.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
dlopen2::wrapper::Container, error::HostingResult, nethost::LoadHostfxrError,
dlopen2::wrapper::Container, error::{HostingResult, HostingError}, nethost::LoadHostfxrError,
pdcstring::PdCString,
};
use derive_more::From;
Expand All @@ -10,8 +10,9 @@ use std::{
rc::Rc,
};

pub(crate) type HostfxrLibrary = Container<crate::bindings::hostfxr::wrapper::Hostfxr>;
pub(crate) type HostfxrLibrary = Container<crate::bindings::hostfxr::wrapper_option::Hostfxr>;
pub(crate) type SharedHostfxrLibrary = Rc<HostfxrLibrary>;
pub(crate) const UNSUPPORTED_HOST_VERSION_ERROR_CODE: i32 = HostingError::HostApiUnsupportedVersion.value() as i32;

/// A struct representing a loaded hostfxr library.
#[derive(Clone, From)]
Expand Down
4 changes: 3 additions & 1 deletion src/hostfxr/library1_0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use crate::{
pdcstring::PdCStr,
};

use super::UNSUPPORTED_HOST_VERSION_ERROR_CODE;

impl Hostfxr {
/// Run an application.
///
Expand Down Expand Up @@ -48,7 +50,7 @@ impl Hostfxr {
let result = unsafe {
self.lib
.hostfxr_main(args.len().try_into().unwrap(), args.as_ptr())
};
}.unwrap_or(UNSUPPORTED_HOST_VERSION_ERROR_CODE);

AppOrHostingResult::from(result)
}
Expand Down
8 changes: 5 additions & 3 deletions src/hostfxr/library2_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use std::{
ptr, slice,
};

use super::UNSUPPORTED_HOST_VERSION_ERROR_CODE;

impl Hostfxr {
/// Run an application.
///
Expand Down Expand Up @@ -61,7 +63,7 @@ impl Hostfxr {
dotnet_root.as_ptr(),
app_path.as_ptr(),
)
};
}.unwrap_or(UNSUPPORTED_HOST_VERSION_ERROR_CODE);

Ok(AppOrHostingResult::from(result))
}
Expand Down Expand Up @@ -91,7 +93,7 @@ impl Hostfxr {
flags,
resolve_sdk2_callback,
)
};
}.unwrap_or(UNSUPPORTED_HOST_VERSION_ERROR_CODE);
HostingResult::from(result).into_result()?;

let sdk_path = RESOLVE_SDK2_DATA
Expand Down Expand Up @@ -159,7 +161,7 @@ impl Hostfxr {
buffer.spare_capacity_mut().len().try_into().unwrap(),
&mut required_buffer_size,
)
};
}.unwrap_or_else(|| HostingError::HostApiUnsupportedVersion.value() as i32);
HostingResult::from(result).into_result()?;
unsafe { buffer.set_len(required_buffer_size.try_into().unwrap()) };

Expand Down
6 changes: 4 additions & 2 deletions src/hostfxr/library3_0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use crate::{
};
use std::{iter, mem::MaybeUninit, ptr};

use super::UNSUPPORTED_HOST_VERSION_ERROR_CODE;

impl Hostfxr {
/// Initializes the hosting components for a dotnet command line running an application
///
Expand Down Expand Up @@ -236,7 +238,7 @@ impl Hostfxr {
parameters,
hostfxr_handle.as_mut_ptr(),
)
};
}.unwrap_or(UNSUPPORTED_HOST_VERSION_ERROR_CODE);

let success_code = HostingResult::from(result).into_result()?;

Expand Down Expand Up @@ -363,7 +365,7 @@ impl Hostfxr {
parameters,
hostfxr_handle.as_mut_ptr(),
)
};
}.unwrap_or(UNSUPPORTED_HOST_VERSION_ERROR_CODE);

let success_code = HostingResult::from(result).into_result()?;

Expand Down
4 changes: 3 additions & 1 deletion src/hostfxr/library6_0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::{
};
use std::{ffi::c_void, mem::MaybeUninit, path::PathBuf, ptr, slice};

use super::UNSUPPORTED_HOST_VERSION_ERROR_CODE;

/// Information about the current dotnet environment loaded using [Hostfxr::get_dotnet_environment_info].
#[derive(Debug, Clone)]
pub struct EnvironmentInfo {
Expand Down Expand Up @@ -110,7 +112,7 @@ impl Hostfxr {
get_dotnet_environment_info_callback,
info.as_mut_ptr().cast(),
)
};
}.unwrap_or(UNSUPPORTED_HOST_VERSION_ERROR_CODE);
HostingResult::from(result).into_result()?;
let info = unsafe { MaybeUninit::assume_init(info) };
Ok(info)
Expand Down
10 changes: 5 additions & 5 deletions src/hostfxr/runtime_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl<I> HostfxrContext<I> {
name.as_ref().as_ptr(),
value.as_mut_ptr(),
)
};
}.unwrap();
HostingResult::from(result).into_result()?;

Ok(unsafe { PdCStr::from_str_ptr(value.assume_init()) })
Expand All @@ -39,7 +39,7 @@ impl<I> HostfxrContext<I> {
name.as_ref().as_ptr(),
value.as_ref().as_ptr(),
)
};
}.unwrap();
HostingResult::from(result).into_result().map(|_| ())
}

Expand All @@ -54,7 +54,7 @@ impl<I> HostfxrContext<I> {
name.as_ref().as_ptr(),
ptr::null(),
)
};
}.unwrap();
HostingResult::from(result).into_result().map(|_| ())
}

Expand All @@ -69,7 +69,7 @@ impl<I> HostfxrContext<I> {
ptr::null_mut(),
ptr::null_mut(),
)
};
}.unwrap();

// ignore buffer too small error as the first call is only to get the required buffer size.
match HostingResult::from(result).into_result() {
Expand All @@ -88,7 +88,7 @@ impl<I> HostfxrContext<I> {
keys.as_mut_ptr(),
values.as_mut_ptr(),
)
};
}.unwrap();
HostingResult::from(result).into_result()?;

unsafe { keys.set_len(count) };
Expand Down

0 comments on commit 0c2e22c

Please sign in to comment.