Skip to content

Commit a6ffa36

Browse files
committed
Fix host call with no return type
Signed-off-by: James Sturtevant <jstur@microsoft.com>
1 parent 0ea8191 commit a6ffa36

6 files changed

Lines changed: 65 additions & 16 deletions

File tree

Justfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ fmt-check:
5757
fmt:
5858
rustup toolchain install nightly -c rustfmt
5959
cargo +nightly fmt --all
60-
cd src/rust_wasm_samples && cargo +nightly fmt
61-
cd src/wasi_samples && cargo +nightly fmt
62-
cd src/wasm_runtime && cargo +nightly fmt
60+
cd src/rust_wasm_samples && cargo +nightly fmt -v --all
61+
cd src/wasi_samples && cargo +nightly fmt -v --all
62+
cd src/wasm_runtime && cargo +nightly fmt -v --all
6363

6464
clippy target=default-target: (check target)
6565
cargo clippy --profile={{ if target == "debug" {"dev"} else { target } }} --all-targets --all-features -- -D warnings

src/hyperlight_wasm/examples/wasi_examples/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#![allow(renamed_and_removed_lints)]
2+
#![allow(unused_unit)]
3+
14
use bindings::wasi_sample::example::Adder;
25
use examples_common::get_wasm_module_path;
36

@@ -21,6 +24,7 @@ impl Default for State {
2124

2225
impl bindings::wasi_sample::example::Host for State {
2326
fn r#print(&mut self, message: alloc::string::String) {
27+
assert_eq!("42", message);
2428
println!("Logged from component: {message}");
2529
}
2630

@@ -62,6 +66,7 @@ fn main() {
6266
let result = instance.add(4, 3);
6367
assert_eq!(7, result);
6468
println!("Add result is {result}");
69+
instance.do_something(42);
6570

6671
let result = instance.call_host("Hello".to_string());
6772
assert_eq!("Hello from component and the host!", result);

src/hyperlight_wasm_macro/src/wasmguest.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use hyperlight_component_util::hl::{
3535
emit_hl_unmarshal_result,
3636
};
3737
use hyperlight_component_util::{resource, rtypes};
38+
use proc_macro::Ident;
3839
use proc_macro2::TokenStream;
3940
use quote::{format_ident, quote};
4041
use syn::ext::IdentExt;
@@ -154,7 +155,6 @@ fn emit_export_extern_decl<'a, 'b, 'c>(
154155
.iter()
155156
.map(|p| rtypes::emit_value(s, &p.ty))
156157
.collect::<Vec<_>>();
157-
let rwt = rtypes::emit_func_result(s, &ft.result);
158158
let (pds, pus) = ft.params.iter().enumerate()
159159
.map(|(i, p)| {
160160
let id = kebab_to_var(p.name.name);
@@ -166,7 +166,7 @@ fn emit_export_extern_decl<'a, 'b, 'c>(
166166
let get_instance = path.iter().map(|export| quote! {
167167
let instance_idx = Some(instance.get_export(&mut *store, instance_idx.as_ref(), #export).unwrap());
168168
}).collect::<Vec<_>>();
169-
let ret = format_ident!("ret");
169+
let (function_call, ret) = emit_wasm_function_call(s, &ft.result, pwts, pus);
170170
let marshal_result = emit_hl_marshal_result(s, ret.clone(), &ft.result);
171171
quote! {
172172
fn #n(fc: &::hyperlight_common::flatbuffer_wrappers::function_call::FunctionCall) -> ::hyperlight_guest::error::Result<::alloc::vec::Vec<u8>> {
@@ -176,8 +176,7 @@ fn emit_export_extern_decl<'a, 'b, 'c>(
176176
let instance_idx = None;
177177
#(#get_instance;)*
178178
let func_idx = instance.get_export(&mut *store, instance_idx.as_ref(), #nlit).unwrap();
179-
let #ret = instance.get_typed_func::<(#(#pwts,)*), (#rwt,)>(&mut *store, func_idx)?
180-
.call(&mut *store, (#(#pus,)*))?.0;
179+
#function_call
181180
::core::result::Result::Ok(::hyperlight_common::flatbuffer_wrappers::util::get_flatbuffer_result::<&[u8]>(&#marshal_result))
182181
}
183182
::hyperlight_guest_bin::guest_function::register::register_function(
@@ -206,6 +205,34 @@ fn emit_export_extern_decl<'a, 'b, 'c>(
206205
}
207206
}
208207

208+
fn emit_wasm_function_call(
209+
s: &mut State,
210+
result: &etypes::Result,
211+
pwts: Vec<TokenStream>,
212+
pus: Vec<TokenStream>,
213+
) -> (TokenStream, proc_macro2::Ident) {
214+
let ret = format_ident!("ret");
215+
216+
// if the result is empty we don't want a return result with `get_typed_func`
217+
let rwt = match result {
218+
etypes::Result::Named(rs) if rs.is_empty() => {
219+
quote! {
220+
instance.get_typed_func::<(#(#pwts,)*), ()>(&mut *store, func_idx)?
221+
.call(&mut *store, (#(#pus,)*))?;
222+
}
223+
}
224+
_ => {
225+
let r = rtypes::emit_func_result(s, result);
226+
quote! {
227+
let #ret = instance.get_typed_func::<(#(#pwts,)*), ((#r,))>(&mut *store, func_idx)?
228+
.call(&mut *store, (#(#pus,)*))?.0;
229+
}
230+
}
231+
};
232+
233+
(rwt, ret)
234+
}
235+
209236
// Emit code to register each export of the given instance with the
210237
// wasmtime linker, calling through Hyperlight.
211238
//

src/wasi_samples/src/bindings.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,16 @@ pub mod exports {
118118
.cast::<usize>();
119119
_rt::cabi_dealloc(l0, l1, 1);
120120
}
121+
#[doc(hidden)]
122+
#[allow(non_snake_case)]
123+
pub unsafe fn _export_do_something_cabi<T: Guest>(arg0: i32) {
124+
#[cfg(target_arch = "wasm32")] _rt::run_ctors_once();
125+
T::do_something(arg0 as u32);
126+
}
121127
pub trait Guest {
122128
fn add(left: u32, right: u32) -> u32;
123129
fn call_host(input: _rt::String) -> _rt::String;
130+
fn do_something(number: u32) -> ();
124131
}
125132
#[doc(hidden)]
126133
macro_rules! __export_wasi_sample_example_adder_cabi {
@@ -136,7 +143,10 @@ pub mod exports {
136143
(export_name = "cabi_post_wasi-sample:example/adder#call-host")]
137144
unsafe extern "C" fn _post_return_call_host(arg0 : * mut u8,) {
138145
unsafe { $($path_to_types)*:: __post_return_call_host::<$ty >
139-
(arg0) } } };
146+
(arg0) } } #[unsafe (export_name =
147+
"wasi-sample:example/adder#do-something")] unsafe extern "C" fn
148+
export_do_something(arg0 : i32,) { unsafe { $($path_to_types)*::
149+
_export_do_something_cabi::<$ty > (arg0) } } };
140150
};
141151
}
142152
#[doc(hidden)]
@@ -272,17 +282,20 @@ macro_rules! __export_example_impl {
272282
#[doc(inline)]
273283
pub(crate) use __export_example_impl as export;
274284
#[cfg(target_arch = "wasm32")]
275-
#[unsafe(link_section = "component-type:wit-bindgen:0.41.0:wasi-sample:example:example:encoded world")]
285+
#[unsafe(
286+
link_section = "component-type:wit-bindgen:0.41.0:wasi-sample:example:example:encoded world"
287+
)]
276288
#[doc(hidden)]
277289
#[allow(clippy::octal_escapes)]
278-
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 335] = *b"\
279-
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xd1\x01\x01A\x02\x01\
290+
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 365] = *b"\
291+
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xef\x01\x01A\x02\x01\
280292
A\x04\x01B\x04\x01@\x01\x07messages\x01\0\x04\0\x05print\x01\0\x01@\x01\x05input\
281293
s\0s\x04\0\x0dhost-function\x01\x01\x03\0\x18wasi-sample:example/host\x05\0\x01B\
282-
\x04\x01@\x02\x04lefty\x05righty\0y\x04\0\x03add\x01\0\x01@\x01\x05inputs\0s\x04\
283-
\0\x09call-host\x01\x01\x04\0\x19wasi-sample:example/adder\x05\x01\x04\0\x1bwasi\
284-
-sample:example/example\x04\0\x0b\x0d\x01\0\x07example\x03\0\0\0G\x09producers\x01\
285-
\x0cprocessed-by\x02\x0dwit-component\x070.227.1\x10wit-bindgen-rust\x060.41.0";
294+
\x06\x01@\x02\x04lefty\x05righty\0y\x04\0\x03add\x01\0\x01@\x01\x05inputs\0s\x04\
295+
\0\x09call-host\x01\x01\x01@\x01\x06numbery\x01\0\x04\0\x0cdo-something\x01\x02\x04\
296+
\0\x19wasi-sample:example/adder\x05\x01\x04\0\x1bwasi-sample:example/example\x04\
297+
\0\x0b\x0d\x01\0\x07example\x03\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dw\
298+
it-component\x070.227.1\x10wit-bindgen-rust\x060.41.0";
286299
#[inline(never)]
287300
#[doc(hidden)]
288301
pub fn __link_custom_section_describing_imports() {

src/wasi_samples/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ impl Guest for Component {
1414

1515
fn call_host(input: String) -> String {
1616
let host_result = host_function(&format!("{} from component", &input));
17-
print(&host_result);
1817
host_result.to_string()
1918
}
19+
20+
fn do_something(number: u32) {
21+
print(&format!("{number}"));
22+
}
2023
}
2124

2225
bindings::export!(Component with_types_in bindings);

src/wasi_samples/wit/example.wit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ world example {
88
interface adder {
99
add: func(left: u32, right: u32) -> u32;
1010
call-host: func(input: string) -> string;
11+
do-something: func(number: u32);
1112
}
1213

1314
interface host {

0 commit comments

Comments
 (0)