mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 08:41:07 +00:00
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:
committed by
Gavin Wood
parent
7c0592a9b6
commit
ce71b7554d
@@ -287,3 +287,25 @@ impl From<LogLevel> for log::Level {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Encodes the given value into a buffer and returns the pointer and the length as a single `u64`.
|
||||
///
|
||||
/// When Substrate calls into Wasm it expects a fixed signature for functions exported
|
||||
/// from the Wasm blob. The return value of this signature is always a `u64`.
|
||||
/// This `u64` stores the pointer to the encoded return value and the length of this encoded value.
|
||||
/// The low `32bits` are reserved for the pointer, followed by `32bit` for the length.
|
||||
#[cfg(not(feature = "std"))]
|
||||
pub fn to_substrate_wasm_fn_return_value(value: &impl Encode) -> u64 {
|
||||
let encoded = value.encode();
|
||||
|
||||
let ptr = encoded.as_ptr() as u64;
|
||||
let length = encoded.len() as u64;
|
||||
let res = ptr | (length << 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.
|
||||
rstd::mem::forget(encoded);
|
||||
|
||||
res
|
||||
}
|
||||
|
||||
@@ -196,15 +196,7 @@ macro_rules! wasm_export_functions {
|
||||
$( $fn_impl )*
|
||||
}
|
||||
|
||||
// We need to return *something*
|
||||
let output = Vec::<u8>::new();
|
||||
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.
|
||||
$crate::rstd::mem::forget(output);
|
||||
res
|
||||
$crate::to_substrate_wasm_fn_return_value(&())
|
||||
}
|
||||
};
|
||||
(@IMPL
|
||||
@@ -232,14 +224,7 @@ macro_rules! wasm_export_functions {
|
||||
$( $fn_impl )*
|
||||
};
|
||||
|
||||
let output = $crate::Encode::encode(&output);
|
||||
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.
|
||||
$crate::rstd::mem::forget(output);
|
||||
res
|
||||
$crate::to_substrate_wasm_fn_return_value(&output)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user