-
Notifications
You must be signed in to change notification settings - Fork 154
Guest function improvements and macros #851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d0edd66
move function type traits to hyperlight_common
jprendes 725f5f5
Use function type traits to improve registration ergonomics
jprendes ba27fe1
Add from_result method to ResultType
jprendes d4d8225
Add guest macro crate
jprendes aee43a7
use guest_function and host_function macros in simpleguest
jprendes fa4e815
update test guests Cargo.lock files
jprendes 4bddfe9
Move result unwrap used in host_function macro from hl-common to hl-g…
jprendes 31a0249
Add comments on tricky areas
jprendes 62895b2
update README.md to use new macros
jprendes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| Copyright 2025 The Hyperlight Authors. | ||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| use alloc::string::String; | ||
|
|
||
| use thiserror::Error; | ||
|
|
||
| use crate::func::{ParameterValue, ReturnValue}; | ||
|
|
||
| /// The error type for Hyperlight operations | ||
| #[derive(Error, Debug)] | ||
| pub enum Error { | ||
| /// Failed to get value from parameter value | ||
| #[error("Failed To Convert Parameter Value {0:?} to {1:?}")] | ||
| ParameterValueConversionFailure(ParameterValue, &'static str), | ||
|
|
||
| /// Failed to get value from return value | ||
| #[error("Failed To Convert Return Value {0:?} to {1:?}")] | ||
| ReturnValueConversionFailure(ReturnValue, &'static str), | ||
|
|
||
| /// A function was called with an incorrect number of arguments | ||
| #[error("The number of arguments to the function is wrong: got {0:?} expected {1:?}")] | ||
| UnexpectedNoOfArguments(usize, usize), | ||
|
|
||
| /// The parameter value type is unexpected | ||
| #[error("The parameter value type is unexpected got {0:?} expected {1:?}")] | ||
| UnexpectedParameterValueType(ParameterValue, String), | ||
|
|
||
| /// The return value type is unexpected | ||
| #[error("The return value type is unexpected got {0:?} expected {1:?}")] | ||
| UnexpectedReturnValueType(ReturnValue, String), | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| Copyright 2025 The Hyperlight Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| use super::utils::for_each_tuple; | ||
| use super::{Error, ParameterTuple, ResultType, SupportedReturnType}; | ||
|
|
||
| pub trait Function<Output: SupportedReturnType, Args: ParameterTuple, E: From<Error>> { | ||
| fn call(&self, args: Args) -> Result<Output, E>; | ||
| } | ||
|
|
||
| macro_rules! impl_function { | ||
| ([$N:expr] ($($p:ident: $P:ident),*)) => { | ||
| impl<F, R, E, $($P),*> Function<R::ReturnType, ($($P,)*), E> for F | ||
| where | ||
| F: Fn($($P),*) -> R, | ||
| ($($P,)*): ParameterTuple, | ||
| R: ResultType<E>, | ||
| E: From<Error> + core::fmt::Debug, | ||
| { | ||
| fn call(&self, ($($p,)*): ($($P,)*)) -> Result<R::ReturnType, E> { | ||
| (self)($($p),*).into_result() | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| for_each_tuple!(impl_function); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| Copyright 2025 The Hyperlight Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| /// Error types related to function support | ||
| pub(crate) mod error; | ||
| /// Definitions and functionality to enable guest-to-host function calling, | ||
| /// also called "host functions" | ||
| /// | ||
| /// This module includes functionality to do the following | ||
| /// | ||
| /// - Define several prototypes for what a host function must look like, | ||
| /// including the number of arguments (arity) they can have, supported argument | ||
| /// types, and supported return types | ||
| /// - Registering host functions to be callable by the guest | ||
| /// - Dynamically dispatching a call from the guest to the appropriate | ||
| /// host function | ||
| pub(crate) mod functions; | ||
| /// Definitions and functionality for supported parameter types | ||
| pub(crate) mod param_type; | ||
| /// Definitions and functionality for supported return types | ||
| pub(crate) mod ret_type; | ||
|
|
||
| pub use error::Error; | ||
| /// Re-export for `HostFunction` trait | ||
| pub use functions::Function; | ||
| pub use param_type::{ParameterTuple, SupportedParameterType}; | ||
| pub use ret_type::{ResultType, SupportedReturnType}; | ||
|
|
||
| /// Re-export for `ParameterValue` enum | ||
| pub use crate::flatbuffer_wrappers::function_types::ParameterValue; | ||
| /// Re-export for `ReturnType` enum | ||
| pub use crate::flatbuffer_wrappers::function_types::ReturnType; | ||
| /// Re-export for `ReturnType` enum | ||
| pub use crate::flatbuffer_wrappers::function_types::ReturnValue; | ||
|
|
||
| mod utils; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.