Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 0cd7260

Browse files
authored
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
1 parent e0f3fa3 commit 0cd7260

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed

core/executor/src/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ impl From<String> for Error {
9999
}
100100
}
101101

102+
impl From<WasmError> for Error {
103+
fn from(err: WasmError) -> Error {
104+
Error::Other(err.to_string())
105+
}
106+
}
107+
102108
/// Type for errors occurring during Wasm runtime construction.
103109
#[derive(Debug, derive_more::Display)]
104110
pub enum WasmError {

core/executor/src/lib.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,33 @@ pub use primitives::traits::Externalities;
5050
pub use wasm_interface;
5151
pub use wasm_runtime::WasmExecutionMethod;
5252

53+
/// Call the given `function` in the given wasm `code`.
54+
///
55+
/// The signature of `function` needs to follow the default Substrate function signature.
56+
///
57+
/// - `call_data`: Will be given as input parameters to `function`
58+
/// - `execution_method`: The execution method to use.
59+
/// - `ext`: The externalities that should be set while executing the wasm function.
60+
/// - `heap_pages`: The number of heap pages to allocate.
61+
///
62+
/// Returns the `Vec<u8>` that contains the return value of the function.
63+
pub fn call_in_wasm<E: Externalities>(
64+
function: &str,
65+
call_data: &[u8],
66+
execution_method: WasmExecutionMethod,
67+
ext: &mut E,
68+
code: &[u8],
69+
heap_pages: u64,
70+
) -> error::Result<Vec<u8>> {
71+
let mut instance = wasm_runtime::create_wasm_runtime_with_code(
72+
ext,
73+
execution_method,
74+
heap_pages,
75+
code,
76+
)?;
77+
instance.call(ext, function, call_data)
78+
}
79+
5380
/// Provides runtime information.
5481
pub trait RuntimeInfo {
5582
/// Native runtime information.
@@ -61,3 +88,24 @@ pub trait RuntimeInfo {
6188
ext: &mut E,
6289
) -> Option<RuntimeVersion>;
6390
}
91+
92+
#[cfg(test)]
93+
mod tests {
94+
use super::*;
95+
use runtime_test::WASM_BINARY;
96+
use runtime_io::TestExternalities;
97+
98+
#[test]
99+
fn call_in_interpreted_wasm_works() {
100+
let mut ext = TestExternalities::default();
101+
let res = call_in_wasm(
102+
"test_empty_return",
103+
&[],
104+
WasmExecutionMethod::Interpreted,
105+
&mut ext,
106+
&WASM_BINARY,
107+
8,
108+
).unwrap();
109+
assert_eq!(res, vec![0u8; 0]);
110+
}
111+
}

core/executor/src/wasm_runtime.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,27 @@ impl RuntimesCache {
157157
}
158158
}
159159

160-
fn create_wasm_runtime<E: Externalities>(
160+
/// Create a wasm runtime with the given `code`.
161+
pub fn create_wasm_runtime_with_code<E: Externalities>(
161162
ext: &mut E,
162163
wasm_method: WasmExecutionMethod,
163164
heap_pages: u64,
165+
code: &[u8],
164166
) -> Result<Box<dyn WasmRuntime>, WasmError> {
165-
let code = ext
166-
.original_storage(well_known_keys::CODE)
167-
.ok_or(WasmError::CodeNotFound)?;
168167
match wasm_method {
169168
WasmExecutionMethod::Interpreted =>
170-
wasmi_execution::create_instance(ext, &code, heap_pages)
169+
wasmi_execution::create_instance(ext, code, heap_pages)
171170
.map(|runtime| -> Box<dyn WasmRuntime> { Box::new(runtime) }),
172171
}
173172
}
173+
174+
fn create_wasm_runtime<E: Externalities>(
175+
ext: &mut E,
176+
wasm_method: WasmExecutionMethod,
177+
heap_pages: u64,
178+
) -> Result<Box<dyn WasmRuntime>, WasmError> {
179+
let code = ext
180+
.original_storage(well_known_keys::CODE)
181+
.ok_or(WasmError::CodeNotFound)?;
182+
create_wasm_runtime_with_code(ext, wasm_method, heap_pages, &code)
183+
}

0 commit comments

Comments
 (0)