Adds function to_substrate_wasm_fn_return_value (#3905)

* Adds function `to_substrate_wasm_fn_return_value`

Instead of replicating this piece of code over and over again, just move
it to a function that does it.

* Feedback

* Comment
This commit is contained in:
Bastian Köcher
2019-10-25 15:18:20 +02:00
committed by Gavin Wood
parent 7c0592a9b6
commit ce71b7554d
13 changed files with 187 additions and 72 deletions
@@ -83,8 +83,7 @@ fn generate_impl_call(
)*
#[allow(deprecated)]
let output = <#runtime as #impl_trait>::#fn_name(#( #pborrow #pnames2 ),*);
#c::runtime_api::Encode::encode(&output)
<#runtime as #impl_trait>::#fn_name(#( #pborrow #pnames2 ),*)
)
)
}
@@ -175,11 +174,12 @@ fn generate_impl_calls(
/// Generate the dispatch function that is used in native to call into the runtime.
fn generate_dispatch_function(impls: &[ItemImpl]) -> Result<TokenStream> {
let data = Ident::new("data", Span::call_site());
let c = generate_crate_access(HIDDEN_INCLUDES_ID);
let impl_calls = generate_impl_calls(impls, &data)?
.into_iter()
.map(|(trait_, fn_name, impl_)| {
let name = prefix_function_with_trait(&trait_, &fn_name);
quote!( #name => Some({ #impl_ }), )
quote!( #name => Some(#c::runtime_api::Encode::encode(&{ #impl_ })), )
});
Ok(quote!(
@@ -218,13 +218,7 @@ fn generate_wasm_interface(impls: &[ItemImpl]) -> Result<TokenStream> {
};
let output = { #impl_ };
let res = output.as_ptr() as u64 + ((output.len() as u64) << 32);
// Leak the output vector to avoid it being freed.
// This is fine in a WASM context since the heap
// will be discarded after the call.
#c::runtime_api::mem::forget(output);
res
#c::runtime_api::to_substrate_wasm_fn_return_value(&output)
}
)
});