@@ -14,16 +14,17 @@ See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
1616
17+ use alloc:: format;
1718use alloc:: string:: ToString ;
1819use alloc:: vec:: Vec ;
1920use core:: arch:: global_asm;
20- use hyperlight_common:: flatbuffer_wrappers:: util:: get_flatbuffer_result;
2121
2222use hyperlight_common:: flatbuffer_wrappers:: function_call:: { FunctionCall , FunctionCallType } ;
2323use hyperlight_common:: flatbuffer_wrappers:: function_types:: {
2424 ParameterValue , ReturnType , ReturnValue ,
2525} ;
2626use hyperlight_common:: flatbuffer_wrappers:: guest_error:: ErrorCode ;
27+ use hyperlight_common:: flatbuffer_wrappers:: util:: get_flatbuffer_result;
2728use hyperlight_common:: mem:: RunMode ;
2829
2930use crate :: error:: { HyperlightGuestError , Result } ;
@@ -39,94 +40,20 @@ pub enum OutBAction {
3940 Abort = 102 ,
4041}
4142
42- pub fn get_host_value_return_as_void ( ) -> Result < ( ) > {
43+ /// Get a return value from a host function call.
44+ /// This usually requires a host function to be called first using `call_host_function`.
45+ pub fn get_host_return_value < T : TryFrom < ReturnValue > > ( ) -> Result < T > {
4346 let return_value = try_pop_shared_input_data_into :: < ReturnValue > ( )
4447 . expect ( "Unable to deserialize a return value from host" ) ;
45- if let ReturnValue :: Void = return_value {
46- Ok ( ( ) )
47- } else {
48- Err ( HyperlightGuestError :: new (
49- ErrorCode :: GuestError ,
50- "Host return value was not void as expected" . to_string ( ) ,
51- ) )
52- }
53- }
54-
55- pub fn get_host_value_return_as_int ( ) -> Result < i32 > {
56- let return_value = try_pop_shared_input_data_into :: < ReturnValue > ( )
57- . expect ( "Unable to deserialize return value from host" ) ;
58-
59- // check that return value is an int and return
60- if let ReturnValue :: Int ( i) = return_value {
61- Ok ( i)
62- } else {
63- Err ( HyperlightGuestError :: new (
64- ErrorCode :: GuestError ,
65- "Host return value was not an int as expected" . to_string ( ) ,
66- ) )
67- }
68- }
69-
70- pub fn get_host_value_return_as_uint ( ) -> Result < u32 > {
71- let return_value = try_pop_shared_input_data_into :: < ReturnValue > ( )
72- . expect ( "Unable to deserialize return value from host" ) ;
73-
74- // check that return value is an int and return
75- if let ReturnValue :: UInt ( ui) = return_value {
76- Ok ( ui)
77- } else {
78- Err ( HyperlightGuestError :: new (
79- ErrorCode :: GuestError ,
80- "Host return value was not a uint as expected" . to_string ( ) ,
81- ) )
82- }
83- }
84-
85- pub fn get_host_value_return_as_long ( ) -> Result < i64 > {
86- let return_value = try_pop_shared_input_data_into :: < ReturnValue > ( )
87- . expect ( "Unable to deserialize return value from host" ) ;
88-
89- // check that return value is an int and return
90- if let ReturnValue :: Long ( l) = return_value {
91- Ok ( l)
92- } else {
93- Err ( HyperlightGuestError :: new (
94- ErrorCode :: GuestError ,
95- "Host return value was not a long as expected" . to_string ( ) ,
96- ) )
97- }
98- }
99-
100- pub fn get_host_value_return_as_ulong ( ) -> Result < u64 > {
101- let return_value = try_pop_shared_input_data_into :: < ReturnValue > ( )
102- . expect ( "Unable to deserialize return value from host" ) ;
103-
104- // check that return value is an int and return
105- if let ReturnValue :: ULong ( ul) = return_value {
106- Ok ( ul)
107- } else {
108- Err ( HyperlightGuestError :: new (
48+ T :: try_from ( return_value) . map_err ( |_| {
49+ HyperlightGuestError :: new (
10950 ErrorCode :: GuestError ,
110- "Host return value was not a ulong as expected" . to_string ( ) ,
111- ) )
112- }
113- }
114-
115- // TODO: Make this generic, return a Result<T, ErrorCode>
116-
117- pub fn get_host_value_return_as_vecbytes ( ) -> Result < Vec < u8 > > {
118- let return_value = try_pop_shared_input_data_into :: < ReturnValue > ( )
119- . expect ( "Unable to deserialize return value from host" ) ;
120-
121- // check that return value is an Vec<u8> and return
122- if let ReturnValue :: VecBytes ( v) = return_value {
123- Ok ( v)
124- } else {
125- Err ( HyperlightGuestError :: new (
126- ErrorCode :: GuestError ,
127- "Host return value was not an VecBytes as expected" . to_string ( ) ,
128- ) )
129- }
51+ format ! (
52+ "Host return value was not a {} as expected" ,
53+ core:: any:: type_name:: <T >( )
54+ ) ,
55+ )
56+ } )
13057}
13158
13259// TODO: Make this generic, return a Result<T, ErrorCode> this should allow callers to call this function and get the result type they expect
@@ -194,7 +121,7 @@ pub fn print_output_as_guest_function(function_call: &FunctionCall) -> Result<Ve
194121 Some ( Vec :: from ( & [ ParameterValue :: String ( message. to_string ( ) ) ] ) ) ,
195122 ReturnType :: Int ,
196123 ) ?;
197- let res_i = get_host_value_return_as_int ( ) ?;
124+ let res_i = get_host_return_value :: < i32 > ( ) ?;
198125 Ok ( get_flatbuffer_result ( res_i) )
199126 } else {
200127 Err ( HyperlightGuestError :: new (
0 commit comments