@@ -15,48 +15,43 @@ limitations under the License.
1515*/
1616
1717use std:: sync:: { Arc , Mutex } ;
18- use std:: thread;
1918
2019use hyperlight_common:: flatbuffer_wrappers:: function_types:: { ParameterValue , ReturnType } ;
21- use hyperlight_host:: func:: HostFunction0 ;
20+ use hyperlight_host:: func:: HostFunction2 ;
21+ use hyperlight_host:: sandbox:: sandbox_builder:: SandboxBuilder ;
2222use hyperlight_host:: sandbox_state:: sandbox:: EvolvableSandbox ;
2323use hyperlight_host:: sandbox_state:: transition:: Noop ;
24- use hyperlight_host:: { MultiUseSandbox , UninitializedSandbox } ;
24+ use hyperlight_host:: { GuestBinary , MultiUseSandbox } ;
25+ use hyperlight_testing:: simple_guest_as_string;
2526
2627fn main ( ) -> hyperlight_host:: Result < ( ) > {
2728 // Create an uninitialized sandbox with a guest binary
28- let mut uninitialized_sandbox = UninitializedSandbox :: new (
29- hyperlight_host:: GuestBinary :: FilePath (
30- hyperlight_testing:: simple_guest_as_string ( ) . unwrap ( ) ,
31- ) ,
32- None , // default configuration
33- None , // default run options
34- None , // default host print function
35- ) ?;
29+ let sandbox_builder = SandboxBuilder :: new ( GuestBinary :: FilePath ( simple_guest_as_string ( ) ?) ) ?;
30+
31+ let mut uninitialized_sandbox = sandbox_builder. build ( ) ?;
3632
37- // Register a host functions
38- fn sleep_5_secs ( ) -> hyperlight_host:: Result < ( ) > {
39- thread:: sleep ( std:: time:: Duration :: from_secs ( 5 ) ) ;
40- Ok ( ( ) )
33+ // Register a host function
34+ fn add ( a : i32 , b : i32 ) -> hyperlight_host:: Result < i32 > {
35+ Ok ( a + b)
4136 }
37+ let host_function = Arc :: new ( Mutex :: new ( add) ) ;
38+ host_function. register ( & mut uninitialized_sandbox, "HostAdd" ) ?;
4239
43- let host_function = Arc :: new ( Mutex :: new ( sleep_5_secs ) ) ;
40+ let host_function = Arc :: new ( Mutex :: new ( add ) ) ;
4441
45- host_function. register ( & mut uninitialized_sandbox, "Sleep5Secs" ) ?;
46- // Note: This function is unused, it's just here for demonstration purposes
42+ host_function. register ( & mut uninitialized_sandbox, "HostAdd" ) ?;
4743
4844 // Initialize sandbox to be able to call host functions
4945 let mut multi_use_sandbox: MultiUseSandbox = uninitialized_sandbox. evolve ( Noop :: default ( ) ) ?;
5046
5147 // Call guest function
52- let message = "Hello, World! I am executing inside of a VM :)\n " . to_string ( ) ;
5348 let result = multi_use_sandbox. call_guest_function_by_name (
54- "PrintOutput " , // function must be defined in the guest binary
49+ "Add " , // function must be defined in the guest binary
5550 ReturnType :: Int ,
56- Some ( vec ! [ ParameterValue :: String ( message . clone ( ) ) ] ) ,
57- ) ;
51+ Some ( vec ! [ ParameterValue :: Int ( 1 ) , ParameterValue :: Int ( 41 ) ] ) ,
52+ ) ? ;
5853
59- assert ! ( result. is_ok ( ) ) ;
54+ println ! ( "Guest function result: 1 + 41 = {:?}" , result ) ;
6055
6156 Ok ( ( ) )
6257}
0 commit comments