|
| 1 | +use examples_common::get_wasm_module_path; |
| 2 | +use hyperlight_wasm::SandboxBuilder; |
| 3 | + |
| 4 | +use crate::bindings::example::runcomponent::Guest; |
| 5 | + |
| 6 | +extern crate alloc; |
| 7 | +mod bindings { |
| 8 | + hyperlight_component_macro::host_bindgen!( |
| 9 | + "../../src/wasmsamples/components/runcomponent-world.wasm" |
| 10 | + ); |
| 11 | +} |
| 12 | + |
| 13 | +pub struct State {} |
| 14 | +impl State { |
| 15 | + pub fn new() -> Self { |
| 16 | + State {} |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +impl Default for State { |
| 21 | + fn default() -> Self { |
| 22 | + Self::new() |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +impl bindings::example::runcomponent::Host for State { |
| 27 | + fn r#get_time_since_boot_microsecond(&mut self) -> i64 { |
| 28 | + let res = std::time::SystemTime::now() |
| 29 | + .duration_since(std::time::SystemTime::UNIX_EPOCH) |
| 30 | + .unwrap() |
| 31 | + .as_micros(); |
| 32 | + i64::try_from(res).unwrap() |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl bindings::example::runcomponent::RuncomponentImports for State { |
| 37 | + type Host = State; |
| 38 | + |
| 39 | + fn r#host(&mut self) -> impl ::core::borrow::BorrowMut<Self::Host> { |
| 40 | + self |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +fn main() { |
| 45 | + let state = State::new(); |
| 46 | + let mut sandbox = SandboxBuilder::new() |
| 47 | + .with_guest_input_buffer_size(70000000) |
| 48 | + .with_guest_heap_size(200000000) |
| 49 | + .with_guest_stack_size(100000000) |
| 50 | + //.with_debugging_enabled(8080) |
| 51 | + .build() |
| 52 | + .unwrap(); |
| 53 | + let rt = bindings::register_host_functions(&mut sandbox, state); |
| 54 | + |
| 55 | + let sb = sandbox.load_runtime().unwrap(); |
| 56 | + |
| 57 | + let mod_path = get_wasm_module_path("runcomponent.aot").unwrap(); |
| 58 | + let sb = sb.load_module(mod_path).unwrap(); |
| 59 | + |
| 60 | + let mut wrapped = bindings::RuncomponentSandbox { sb, rt }; |
| 61 | + let instance = bindings::example::runcomponent::RuncomponentExports::guest(&mut wrapped); |
| 62 | + let echo = instance.echo("Hello World!".to_string()); |
| 63 | + println!("{}", echo); |
| 64 | + |
| 65 | + let result = instance.round_to_nearest_int(1.331, 24.0); |
| 66 | + println!("rounded result {}", result); |
| 67 | + assert_eq!(result, 32); |
| 68 | +} |
0 commit comments