Skip to content

Commit f0fb6b4

Browse files
Copilotjsturtevant
andauthored
Fix README.md host function calling examples to match current API (#682)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: jsturtevant <[email protected]>
1 parent 907fb8f commit f0fb6b4

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

README.md

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,16 @@ It is followed by an example of a simple guest application using the Hyperlight
3333
### Host
3434

3535
```rust
36-
use std::{thread, sync::{Arc, Mutex}};
36+
use std::thread;
3737

38-
use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterValue, ReturnType};
39-
use hyperlight_host::{UninitializedSandbox, MultiUseSandbox, func::HostFunction0, sandbox_state::transition::Noop, sandbox_state::sandbox::EvolvableSandbox};
38+
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
39+
use hyperlight_host::sandbox_state::transition::Noop;
40+
use hyperlight_host::{MultiUseSandbox, UninitializedSandbox};
4041

4142
fn main() -> hyperlight_host::Result<()> {
4243
// Create an uninitialized sandbox with a guest binary
4344
let mut uninitialized_sandbox = UninitializedSandbox::new(
44-
hyperlight_host::GuestBinary::FilePath(hyperlight_testing::simple_guest_as_string().unwrap()),
45+
hyperlight_host::GuestBinary::FilePath("path/to/your/guest/binary".to_string()),
4546
None // default configuration
4647
)?;
4748

@@ -59,12 +60,10 @@ fn main() -> hyperlight_host::Result<()> {
5960
let message = "Hello, World! I am executing inside of a VM :)\n".to_string();
6061
// in order to call a function it first must be defined in the guest and exposed so that
6162
// the host can call it
62-
let result: i32 = multi_use_sandbox.call_guest_function_by_name(
63+
multi_use_sandbox.call_guest_function_by_name::<i32>(
6364
"PrintOutput",
6465
message,
65-
);
66-
67-
assert!(result.is_ok());
66+
)?;
6867

6968
Ok(())
7069
}
@@ -84,22 +83,21 @@ use hyperlight_common::flatbuffer_wrappers::function_types::{
8483
ParameterType, ParameterValue, ReturnType,
8584
};
8685
use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
87-
use hyperlight_common::flatbuffer_wrappers::util::get_flatbuffer_result_from_int;
86+
use hyperlight_common::flatbuffer_wrappers::util::get_flatbuffer_result;
8887

8988
use hyperlight_guest::error::{HyperlightGuestError, Result};
9089
use hyperlight_guest_bin::guest_function::definition::GuestFunctionDefinition;
9190
use hyperlight_guest_bin::guest_function::register::register_function;
92-
use hyperlight_guest_bin::host_comm::{call_host_function, call_host_function_without_returning_result};
91+
use hyperlight_guest_bin::host_comm::call_host_function;
9392

9493
fn print_output(function_call: &FunctionCall) -> Result<Vec<u8>> {
9594
if let ParameterValue::String(message) = function_call.parameters.clone().unwrap()[0].clone() {
96-
call_host_function(
95+
let result = call_host_function::<i32>(
9796
"HostPrint",
9897
Some(Vec::from(&[ParameterValue::String(message.to_string())])),
9998
ReturnType::Int,
10099
)?;
101-
let result = get_host_value_return_as_int()?;
102-
Ok(get_flatbuffer_result_from_int(result))
100+
Ok(get_flatbuffer_result(result))
103101
} else {
104102
Err(HyperlightGuestError::new(
105103
ErrorCode::GuestFunctionParameterTypeMismatch,
@@ -114,7 +112,7 @@ pub extern "C" fn hyperlight_main() {
114112
"PrintOutput".to_string(),
115113
Vec::from(&[ParameterType::String]),
116114
ReturnType::Int,
117-
print_output as i64,
115+
print_output as usize,
118116
);
119117
register_function(print_output_def);
120118
}
@@ -129,6 +127,28 @@ pub fn guest_dispatch_function(function_call: FunctionCall) -> Result<Vec<u8>> {
129127
}
130128
```
131129

130+
**Note**: Guest applications require a specific build configuration. Create a `.cargo/config.toml` file in your guest project with the following content:
131+
132+
```toml
133+
[build]
134+
target = "x86_64-unknown-none"
135+
136+
[target.x86_64-unknown-none]
137+
rustflags = [
138+
"-C",
139+
"code-model=small",
140+
"-C",
141+
"link-args=-e entrypoint",
142+
]
143+
linker = "rust-lld"
144+
145+
[profile.release]
146+
panic = "abort"
147+
148+
[profile.dev]
149+
panic = "abort"
150+
```
151+
132152
For additional examples of using the Hyperlight host Rust library, see
133153
the [./src/hyperlight_host/examples](./src/hyperlight_host/examples) directory.
134154

0 commit comments

Comments
 (0)