@@ -14,7 +14,6 @@ See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
1616
17- use alloc:: format;
1817use alloc:: string:: { String , ToString } ;
1918use alloc:: vec:: Vec ;
2019
@@ -73,21 +72,16 @@ pub(crate) fn hostfunc_type(d: &HostFunctionDefinition, e: &Engine) -> Result<Fu
7372 ReturnType :: Void => { }
7473 ReturnType :: Int | ReturnType :: UInt => results. push ( ValType :: I32 ) ,
7574 ReturnType :: Long | ReturnType :: ULong => results. push ( ValType :: I64 ) ,
76- /* hyperlight_guest_bin::host_function_call::get_host_value_return_as_{bool,float,double,string} are missing */
77- // TODO: this comment is outdated, these are now implemented. Implement the other types
75+ ReturnType :: Bool => results. push ( ValType :: I32 ) ,
76+ ReturnType :: Float => results. push ( ValType :: F32 ) ,
77+ ReturnType :: Double => results. push ( ValType :: F64 ) ,
78+ ReturnType :: String => results. push ( ValType :: I32 ) ,
79+ // TODO: this comment about using i64 for VecBytes doesn't seem to match with what
80+ // hl_return_to_val was doing, check if this is still correct.
7881 /* For compatibility with old host, we return
7982 * a packed i64 with a (wasm32) pointer in the lower half and
8083 * a length in the upper half. */
8184 ReturnType :: VecBytes => results. push ( ValType :: I64 ) ,
82- _ => {
83- return Err ( HyperlightGuestError :: new (
84- ErrorCode :: GuestError ,
85- format ! (
86- "Host function return type {:?} must be (u)int or (u)long, if present" ,
87- d. return_type
88- ) ,
89- ) ) ;
90- }
9185 }
9286 Ok ( FuncType :: new ( e, params, results) )
9387}
@@ -106,7 +100,39 @@ pub(crate) fn call(
106100 marshal:: val_to_hl_param ( & mut c, |c, n| c. get_export ( n) , s, t)
107101 } )
108102 . collect ( ) ;
109- call_host_function :: < ReturnValue > ( & d. function_name , Some ( params) , d. return_type )
103+
104+ let rv = call_host_function :: < ReturnValue > ( & d. function_name , Some ( params) , d. return_type )
110105 . expect ( "Host function call failed" ) ;
111- marshal:: hl_return_to_val ( & mut c, |c, n| c. get_export ( n) , & d. return_type , rs)
106+
107+ assert ! (
108+ return_type_from_val( & rv) == d. return_type,
109+ "Host function return type mismatch"
110+ ) ;
111+
112+ if rs. is_empty ( ) {
113+ assert ! (
114+ d. return_type == ReturnType :: Void ,
115+ "Host function return type mismatch"
116+ ) ;
117+ return Ok ( ( ) ) ;
118+ }
119+
120+ rs[ 0 ] = marshal:: hl_return_to_val ( & mut c, |c, n| c. get_export ( n) , rv) ?;
121+
122+ Ok ( ( ) )
123+ }
124+
125+ fn return_type_from_val ( val : & ReturnValue ) -> ReturnType {
126+ match val {
127+ ReturnValue :: Int ( _) => ReturnType :: Int ,
128+ ReturnValue :: UInt ( _) => ReturnType :: UInt ,
129+ ReturnValue :: Long ( _) => ReturnType :: Long ,
130+ ReturnValue :: ULong ( _) => ReturnType :: ULong ,
131+ ReturnValue :: Float ( _) => ReturnType :: Float ,
132+ ReturnValue :: Double ( _) => ReturnType :: Double ,
133+ ReturnValue :: String ( _) => ReturnType :: String ,
134+ ReturnValue :: VecBytes ( _) => ReturnType :: VecBytes ,
135+ ReturnValue :: Bool ( _) => ReturnType :: Bool ,
136+ ReturnValue :: Void ( _) => ReturnType :: Void ,
137+ }
112138}
0 commit comments