-
Notifications
You must be signed in to change notification settings - Fork 187
fix: host_bindgen! returns Result instead of panicking on guest errors #1317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,6 +46,7 @@ fn emit_export_extern_decl<'a, 'b, 'c>( | |
| .map(|p| rtypes::emit_func_param(s, p)) | ||
| .collect::<Vec<_>>(); | ||
| let result_decl = rtypes::emit_func_result(s, &ft.result); | ||
| let result_decl = quote! { ::std::result::Result<#result_decl, ::hyperlight_host::error::HyperlightError> }; | ||
| let hln = emit_fn_hl_name(s, ed.kebab_name); | ||
| let ret = format_ident!("ret"); | ||
| let marshal = ft | ||
|
|
@@ -66,11 +67,11 @@ fn emit_export_extern_decl<'a, 'b, 'c>( | |
| #hln, | ||
| marshalled, | ||
| ); | ||
| let ::std::result::Result::Ok(#ret) = #ret else { panic!("bad return from guest {:?}", #ret) }; | ||
| let #ret = #ret?; | ||
| #[allow(clippy::unused_unit)] | ||
| let mut rts = self.rt.lock().unwrap(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will still panic--fine? |
||
| #[allow(clippy::unused_unit)] | ||
| #unmarshal | ||
| Ok(#unmarshal) | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -121,7 +122,7 @@ fn emit_export_instance<'a, 'b, 'c>(s: &'c mut State<'a, 'b>, wn: WitName, it: & | |
|
|
||
| let ns = wn.namespace_path(); | ||
| let nsi = wn.namespace_idents(); | ||
| let trait_name = kebab_to_type(wn.name); | ||
| let trait_name = kebab_to_exports_name(wn.name); | ||
| let r#trait = s.r#trait(&nsi, trait_name.clone()); | ||
| let tvs = r#trait | ||
| .tvs | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -641,6 +641,11 @@ fn emit_extern_decl<'a, 'b, 'c>( | |
| .map(|p| emit_func_param(&mut s, p)) | ||
| .collect::<Vec<_>>(); | ||
| let result = emit_func_result(&mut s, &ft.result); | ||
| let result = if !s.is_guest && s.is_export { | ||
| quote! { ::std::result::Result<#result, ::hyperlight_host::error::HyperlightError> } | ||
| } else { | ||
| result | ||
| }; | ||
|
Comment on lines
+644
to
+648
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might want a helper for this? It seems to repeat enough times.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it should be pushed into Are there any times where we use that where we don't want it? |
||
| quote! { | ||
| fn #n(&mut self, #(#params),*) -> #result; | ||
| } | ||
|
|
@@ -663,12 +668,22 @@ fn emit_extern_decl<'a, 'b, 'c>( | |
| } | ||
| ResourceItemName::Method(n) => { | ||
| let result = emit_func_result(&mut sv, &ft.result); | ||
| let result = if !sv.is_guest && sv.is_export { | ||
| quote! { ::std::result::Result<#result, ::hyperlight_host::error::HyperlightError> } | ||
| } else { | ||
| result | ||
| }; | ||
| sv.cur_trait().items.extend(quote! { | ||
| fn #n(&mut self, #(#params),*) -> #result; | ||
| }); | ||
| } | ||
| ResourceItemName::Static(n) => { | ||
| let result = emit_func_result(&mut sv, &ft.result); | ||
| let result = if !sv.is_guest && sv.is_export { | ||
| quote! { ::std::result::Result<#result, ::hyperlight_host::error::HyperlightError> } | ||
| } else { | ||
| result | ||
| }; | ||
| sv.cur_trait().items.extend(quote! { | ||
| fn #n(&mut self, #(#params),*) -> #result; | ||
| }); | ||
|
|
@@ -691,6 +706,9 @@ fn emit_extern_decl<'a, 'b, 'c>( | |
| ) -> TokenStream { | ||
| let id = kebab_to_type(ed.kebab_name); | ||
| let mut s = s.helper(); | ||
| if !s.cur_mod().emitted_type_names.insert(id.clone()) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this being used for? It seems like it will simply drop some definitions, which doesn't seem ideal? |
||
| return TokenStream::new(); | ||
| } | ||
|
|
||
| let t = emit_defined(&mut s, v, id, t); | ||
| s.cur_mod().items.extend(t); | ||
|
|
@@ -712,6 +730,9 @@ fn emit_extern_decl<'a, 'b, 'c>( | |
| let rn = kebab_to_type(ed.kebab_name); | ||
| s.add_helper_supertrait(rn.clone()); | ||
| let mut s = s.helper(); | ||
| if !s.cur_mod().emitted_type_names.insert(rn.clone()) { | ||
| return quote! {}; | ||
| } | ||
| s.cur_trait = Some(rn.clone()); | ||
| s.cur_trait().items.extend(quote! { | ||
| type T: ::core::marker::Send; | ||
|
|
@@ -729,7 +750,12 @@ fn emit_extern_decl<'a, 'b, 'c>( | |
| emit_instance(&mut s, wn.clone(), it); | ||
|
|
||
| let nsids = wn.namespace_idents(); | ||
| let repr = s.r#trait(&nsids, kebab_to_type(wn.name)); | ||
| let trait_name = if !s.is_guest && s.is_export { | ||
| kebab_to_exports_name(wn.name) | ||
| } else { | ||
| kebab_to_type(wn.name) | ||
| }; | ||
| let repr = s.r#trait(&nsids, trait_name.clone()); | ||
|
jsturtevant marked this conversation as resolved.
|
||
| let vs = if !repr.tvs.is_empty() { | ||
| let vs = repr.tvs.clone(); | ||
| let tvs = vs | ||
|
|
@@ -745,9 +771,9 @@ fn emit_extern_decl<'a, 'b, 'c>( | |
| let tns = wn.namespace_path(); | ||
| let tn = kebab_to_type(wn.name); | ||
| let trait_bound = if tns.is_empty() { | ||
| quote! { #rp #tn } | ||
| quote! { #rp #trait_name } | ||
| } else { | ||
| quote! { #rp #tns::#tn } | ||
| quote! { #rp #tns::#trait_name } | ||
| }; | ||
| quote! { | ||
| type #tn: #trait_bound #vs; | ||
|
|
@@ -766,7 +792,11 @@ fn emit_instance<'a, 'b, 'c>(s: &'c mut State<'a, 'b>, wn: WitName, it: &'c Inst | |
| tracing::debug!("emitting instance {:?}", wn); | ||
| let mut s = s.with_cursor(wn.namespace_idents()); | ||
|
|
||
| let name = kebab_to_type(wn.name); | ||
| let name = if !s.is_guest && s.is_export { | ||
| kebab_to_exports_name(wn.name) | ||
| } else { | ||
| kebab_to_type(wn.name) | ||
| }; | ||
|
|
||
| s.cur_helper_mod = Some(kebab_to_namespace(wn.name)); | ||
| s.cur_trait = Some(name.clone()); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't follow what this is being used for?