gear_backend_common/
runtime.rs1use crate::{
22 memory::{MemoryAccessRecorder, MemoryOwner},
23 BackendExternalities, BackendState, BackendSyscallError, UndefinedTerminationReason,
24};
25use gear_core::{costs::RuntimeCosts, pages::WasmPage};
26use gear_core_errors::ExtError as FallibleExtError;
27
28#[derive(Debug, Clone)]
30pub enum RunFallibleError {
31 UndefinedTerminationReason(UndefinedTerminationReason),
32 FallibleExt(FallibleExtError),
33}
34
35impl<E> From<E> for RunFallibleError
36where
37 E: BackendSyscallError,
38{
39 fn from(err: E) -> Self {
40 err.into_run_fallible_error()
41 }
42}
43
44pub trait Runtime<Ext: BackendExternalities>:
45 MemoryOwner + MemoryAccessRecorder + BackendState
46{
47 type Error;
48
49 fn unreachable_error() -> Self::Error;
50
51 fn ext_mut(&mut self) -> &mut Ext;
52
53 fn run_any<T, F>(
54 &mut self,
55 gas: u64,
56 cost: RuntimeCosts,
57 f: F,
58 ) -> Result<(u64, T), Self::Error>
59 where
60 F: FnOnce(&mut Self) -> Result<T, UndefinedTerminationReason>;
61
62 fn run_fallible<T: Sized, F, R>(
63 &mut self,
64 gas: u64,
65 res_ptr: u32,
66 cost: RuntimeCosts,
67 f: F,
68 ) -> Result<(u64, ()), Self::Error>
69 where
70 F: FnOnce(&mut Self) -> Result<T, RunFallibleError>,
71 R: From<Result<T, u32>> + Sized;
72
73 fn alloc(&mut self, pages: u32) -> Result<WasmPage, Ext::AllocError>;
74}