Add a new host function for reporting fatal errors; make WASM backtraces readable when printing out errors (#10741)

* Add a new host function for reporting fatal errors

* Fix one of the wasmtime executor tests

* Have `#[runtime_interface(wasm_only)]` actually mean WASM-only, and not no_std-only

* Print out errors through `Display` instead of `Debug`

* Switch one more trait to require `Error` for its error instead of only `Debug`

* Align to review comments
This commit is contained in:
Koute
2022-02-09 18:12:55 +09:00
committed by GitHub
parent bd261d57c4
commit 9a31b2c341
68 changed files with 554 additions and 249 deletions
@@ -32,11 +32,12 @@
use crate::utils::{
create_exchangeable_host_function_ident, create_function_ident_with_version,
generate_crate_access, get_function_argument_names, get_function_arguments,
get_runtime_interface,
get_runtime_interface, RuntimeInterfaceFunction,
};
use syn::{
parse_quote, spanned::Spanned, FnArg, Ident, ItemTrait, Result, Signature, TraitItemMethod,
parse_quote, spanned::Spanned, FnArg, Ident, ItemTrait, Result, Signature, Token,
TraitItemMethod,
};
use proc_macro2::{Span, TokenStream};
@@ -74,14 +75,14 @@ pub fn generate(trait_def: &ItemTrait, is_wasm_only: bool, tracing: bool) -> Res
/// Generates the bare function implementation for the given method for the host and wasm side.
fn function_for_method(
method: &TraitItemMethod,
method: &RuntimeInterfaceFunction,
latest_version: u32,
is_wasm_only: bool,
) -> Result<TokenStream> {
let std_impl =
if !is_wasm_only { function_std_latest_impl(method, latest_version)? } else { quote!() };
let no_std_impl = function_no_std_impl(method)?;
let no_std_impl = function_no_std_impl(method, is_wasm_only)?;
Ok(quote! {
#std_impl
@@ -91,20 +92,46 @@ fn function_for_method(
}
/// Generates the bare function implementation for `cfg(not(feature = "std"))`.
fn function_no_std_impl(method: &TraitItemMethod) -> Result<TokenStream> {
fn function_no_std_impl(
method: &RuntimeInterfaceFunction,
is_wasm_only: bool,
) -> Result<TokenStream> {
let function_name = &method.sig.ident;
let host_function_name = create_exchangeable_host_function_ident(&method.sig.ident);
let args = get_function_arguments(&method.sig);
let arg_names = get_function_argument_names(&method.sig);
let return_value = &method.sig.output;
let return_value = if method.should_trap_on_return() {
syn::ReturnType::Type(
<Token![->]>::default(),
Box::new(syn::TypeNever { bang_token: <Token![!]>::default() }.into()),
)
} else {
method.sig.output.clone()
};
let maybe_unreachable = if method.should_trap_on_return() {
quote! {
; core::arch::wasm32::unreachable();
}
} else {
quote! {}
};
let attrs = method.attrs.iter().filter(|a| !a.path.is_ident("version"));
let cfg_wasm_only = if is_wasm_only {
quote! { #[cfg(target_arch = "wasm32")] }
} else {
quote! {}
};
Ok(quote! {
#cfg_wasm_only
#[cfg(not(feature = "std"))]
#( #attrs )*
pub fn #function_name( #( #args, )* ) #return_value {
// Call the host function
#host_function_name.get()( #( #arg_names, )* )
#maybe_unreachable
}
})
}
@@ -26,7 +26,7 @@ use crate::utils::{
create_host_function_ident, generate_crate_access, get_function_argument_names,
get_function_argument_names_and_types_without_ref, get_function_argument_types,
get_function_argument_types_ref_and_mut, get_function_argument_types_without_ref,
get_function_arguments, get_runtime_interface,
get_function_arguments, get_runtime_interface, RuntimeInterfaceFunction,
};
use syn::{
@@ -205,7 +205,7 @@ fn generate_host_functions_struct(
/// implementation of the function.
fn generate_host_function_implementation(
trait_name: &Ident,
method: &TraitItemMethod,
method: &RuntimeInterfaceFunction,
version: u32,
is_wasm_only: bool,
) -> Result<(TokenStream, Ident, TokenStream)> {
@@ -153,7 +153,7 @@ fn impl_trait_for_externalities(trait_def: &ItemTrait, is_wasm_only: bool) -> Re
let crate_ = generate_crate_access();
let interface = get_runtime_interface(trait_def)?;
let methods = interface.all_versions().map(|(version, method)| {
let mut cloned = method.clone();
let mut cloned = (*method).clone();
cloned.attrs.retain(|a| !a.path.is_ident("version"));
cloned.sig.ident = create_function_ident_with_version(&cloned.sig.ident, version);
cloned