mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 10:31:04 +00:00
Make wasmi_execution public to use it from tests (#3829)
* Make `wasmi_execution` public to use it from tests * Make `WasmRuntime` accessible as well * Add `call_in_wasm` instead of making stuff public * Use `WasmRuntime` * Move test * More feedback
This commit is contained in:
@@ -99,6 +99,12 @@ impl From<String> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<WasmError> for Error {
|
||||
fn from(err: WasmError) -> Error {
|
||||
Error::Other(err.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
/// Type for errors occurring during Wasm runtime construction.
|
||||
#[derive(Debug, derive_more::Display)]
|
||||
pub enum WasmError {
|
||||
|
||||
@@ -50,6 +50,33 @@ pub use primitives::traits::Externalities;
|
||||
pub use wasm_interface;
|
||||
pub use wasm_runtime::WasmExecutionMethod;
|
||||
|
||||
/// Call the given `function` in the given wasm `code`.
|
||||
///
|
||||
/// The signature of `function` needs to follow the default Substrate function signature.
|
||||
///
|
||||
/// - `call_data`: Will be given as input parameters to `function`
|
||||
/// - `execution_method`: The execution method to use.
|
||||
/// - `ext`: The externalities that should be set while executing the wasm function.
|
||||
/// - `heap_pages`: The number of heap pages to allocate.
|
||||
///
|
||||
/// Returns the `Vec<u8>` that contains the return value of the function.
|
||||
pub fn call_in_wasm<E: Externalities>(
|
||||
function: &str,
|
||||
call_data: &[u8],
|
||||
execution_method: WasmExecutionMethod,
|
||||
ext: &mut E,
|
||||
code: &[u8],
|
||||
heap_pages: u64,
|
||||
) -> error::Result<Vec<u8>> {
|
||||
let mut instance = wasm_runtime::create_wasm_runtime_with_code(
|
||||
ext,
|
||||
execution_method,
|
||||
heap_pages,
|
||||
code,
|
||||
)?;
|
||||
instance.call(ext, function, call_data)
|
||||
}
|
||||
|
||||
/// Provides runtime information.
|
||||
pub trait RuntimeInfo {
|
||||
/// Native runtime information.
|
||||
@@ -61,3 +88,24 @@ pub trait RuntimeInfo {
|
||||
ext: &mut E,
|
||||
) -> Option<RuntimeVersion>;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use runtime_test::WASM_BINARY;
|
||||
use runtime_io::TestExternalities;
|
||||
|
||||
#[test]
|
||||
fn call_in_interpreted_wasm_works() {
|
||||
let mut ext = TestExternalities::default();
|
||||
let res = call_in_wasm(
|
||||
"test_empty_return",
|
||||
&[],
|
||||
WasmExecutionMethod::Interpreted,
|
||||
&mut ext,
|
||||
&WASM_BINARY,
|
||||
8,
|
||||
).unwrap();
|
||||
assert_eq!(res, vec![0u8; 0]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,6 +157,20 @@ impl RuntimesCache {
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a wasm runtime with the given `code`.
|
||||
pub fn create_wasm_runtime_with_code<E: Externalities>(
|
||||
ext: &mut E,
|
||||
wasm_method: WasmExecutionMethod,
|
||||
heap_pages: u64,
|
||||
code: &[u8],
|
||||
) -> Result<Box<dyn WasmRuntime>, WasmError> {
|
||||
match wasm_method {
|
||||
WasmExecutionMethod::Interpreted =>
|
||||
wasmi_execution::create_instance(ext, code, heap_pages)
|
||||
.map(|runtime| -> Box<dyn WasmRuntime> { Box::new(runtime) }),
|
||||
}
|
||||
}
|
||||
|
||||
fn create_wasm_runtime<E: Externalities>(
|
||||
ext: &mut E,
|
||||
wasm_method: WasmExecutionMethod,
|
||||
@@ -165,9 +179,5 @@ fn create_wasm_runtime<E: Externalities>(
|
||||
let code = ext
|
||||
.original_storage(well_known_keys::CODE)
|
||||
.ok_or(WasmError::CodeNotFound)?;
|
||||
match wasm_method {
|
||||
WasmExecutionMethod::Interpreted =>
|
||||
wasmi_execution::create_instance(ext, &code, heap_pages)
|
||||
.map(|runtime| -> Box<dyn WasmRuntime> { Box::new(runtime) }),
|
||||
}
|
||||
create_wasm_runtime_with_code(ext, wasm_method, heap_pages, &code)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user