@@ -15,7 +15,6 @@ limitations under the License.
1515*/
1616
1717use alloc:: collections:: BTreeMap ;
18- use alloc:: format;
1918use alloc:: string:: { String , ToString } ;
2019use alloc:: vec:: Vec ;
2120
@@ -88,21 +87,16 @@ pub(crate) fn hostfunc_type(d: &HostFunctionDefinition, e: &Engine) -> Result<Fu
8887 ReturnType :: Void => { }
8988 ReturnType :: Int | ReturnType :: UInt => results. push ( ValType :: I32 ) ,
9089 ReturnType :: Long | ReturnType :: ULong => results. push ( ValType :: I64 ) ,
91- /* hyperlight_guest_bin::host_function_call::get_host_value_return_as_{bool,float,double,string} are missing */
92- // TODO: this comment is outdated, these are now implemented. Implement the other types
90+ ReturnType :: Bool => results. push ( ValType :: I32 ) ,
91+ ReturnType :: Float => results. push ( ValType :: F32 ) ,
92+ ReturnType :: Double => results. push ( ValType :: F64 ) ,
93+ ReturnType :: String => results. push ( ValType :: I32 ) ,
94+ // TODO: this comment about using i64 for VecBytes doesn't seem to match with what
95+ // hl_return_to_val was doing, check if this is still correct.
9396 /* For compatibility with old host, we return
9497 * a packed i64 with a (wasm32) pointer in the lower half and
9598 * a length in the upper half. */
9699 ReturnType :: VecBytes => results. push ( ValType :: I64 ) ,
97- _ => {
98- return Err ( HyperlightGuestError :: new (
99- ErrorCode :: GuestError ,
100- format ! (
101- "Host function return type {:?} must be (u)int or (u)long, if present" ,
102- d. return_type
103- ) ,
104- ) ) ;
105- }
106100 }
107101 Ok ( FuncType :: new ( e, params, results) )
108102}
@@ -121,7 +115,39 @@ pub(crate) fn call(
121115 marshal:: val_to_hl_param ( & mut c, |c, n| c. get_export ( n) , s, t)
122116 } )
123117 . collect ( ) ;
124- call_host_function :: < ReturnValue > ( & d. function_name , Some ( params) , d. return_type )
118+
119+ let rv = call_host_function :: < ReturnValue > ( & d. function_name , Some ( params) , d. return_type )
125120 . expect ( "Host function call failed" ) ;
126- marshal:: hl_return_to_val ( & mut c, |c, n| c. get_export ( n) , & d. return_type , rs)
121+
122+ assert ! (
123+ return_type_from_val( & rv) == d. return_type,
124+ "Host function return type mismatch"
125+ ) ;
126+
127+ if rs. is_empty ( ) {
128+ assert ! (
129+ d. return_type == ReturnType :: Void ,
130+ "Host function return type mismatch"
131+ ) ;
132+ return Ok ( ( ) ) ;
133+ }
134+
135+ rs[ 0 ] = marshal:: hl_return_to_val ( & mut c, |c, n| c. get_export ( n) , rv) ?;
136+
137+ Ok ( ( ) )
138+ }
139+
140+ fn return_type_from_val ( val : & ReturnValue ) -> ReturnType {
141+ match val {
142+ ReturnValue :: Int ( _) => ReturnType :: Int ,
143+ ReturnValue :: UInt ( _) => ReturnType :: UInt ,
144+ ReturnValue :: Long ( _) => ReturnType :: Long ,
145+ ReturnValue :: ULong ( _) => ReturnType :: ULong ,
146+ ReturnValue :: Float ( _) => ReturnType :: Float ,
147+ ReturnValue :: Double ( _) => ReturnType :: Double ,
148+ ReturnValue :: String ( _) => ReturnType :: String ,
149+ ReturnValue :: VecBytes ( _) => ReturnType :: VecBytes ,
150+ ReturnValue :: Bool ( _) => ReturnType :: Bool ,
151+ ReturnValue :: Void ( _) => ReturnType :: Void ,
152+ }
127153}
0 commit comments