-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.rs
More file actions
58 lines (48 loc) · 1.66 KB
/
main.rs
File metadata and controls
58 lines (48 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#![no_std]
#![no_main]
extern crate alloc;
const _: () = {
#[cfg(not(hyperlight))]
compile_error!("This crate can only be compiled for hyperlight targets");
};
use alloc::format;
use alloc::vec::Vec;
use hyperlight_common::flatbuffer_wrappers::function_call::*;
use hyperlight_common::flatbuffer_wrappers::function_types::*;
use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
use hyperlight_common::flatbuffer_wrappers::util::*;
use hyperlight_guest::error::{HyperlightGuestError, Result};
use hyperlight_guest_bin::guest_function::definition::*;
use hyperlight_guest_bin::guest_function::register::*;
mod ffi {
#![allow(dead_code, non_camel_case_types)] // generated code
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}
fn host_print(s: impl AsRef<[u8]>) -> i32 {
let s = s.as_ref();
unsafe { ffi::host_print(s.as_ptr() as _, s.len()) }
}
pub fn say_hello(func: &FunctionCall) -> Result<Vec<u8>> {
let params = func.parameters.as_deref().unwrap_or_default();
let Some(ParameterValue::String(name)) = params.first() else {
return Err(HyperlightGuestError::new(
ErrorCode::GuestError,
"Expected a string parameter".into(),
));
};
let n = host_print(format!("Hello {name}\n"));
Ok(get_flatbuffer_result(n))
}
#[unsafe(no_mangle)]
pub extern "C" fn hyperlight_main() {
register_function(GuestFunctionDefinition::new(
"SayHello".into(),
[ParameterType::String].into(),
ReturnType::Int,
say_hello as usize,
));
}
#[unsafe(no_mangle)]
pub fn guest_dispatch_function(_: FunctionCall) -> Result<Vec<u8>> {
panic!("Invalid guest function call");
}