#![warn(missing_docs)]
#![allow(clippy::needless_borrows_for_generic_args)]
#![cfg_attr(not(feature = "std"), no_std)]
extern crate alloc;
#[cfg(feature = "std")]
pub mod embedded_executor;
#[cfg(feature = "std")]
pub use self::embedded_executor as default_executor;
#[cfg(not(feature = "std"))]
#[cfg(target_arch = "wasm32")]
pub mod host_executor;
#[cfg(not(feature = "std"))]
#[cfg(target_arch = "wasm32")]
pub use self::host_executor as default_executor;
pub use gear_sandbox_env as env;
pub use gear_sandbox_env::HostError;
pub use sp_wasm_interface_common::{IntoValue, ReturnValue, TryFromValue, Value};
use alloc::string::String;
use sp_core::RuntimeDebug;
use sp_std::prelude::*;
use sp_wasm_interface_common::HostPointer;
const TARGET: &str = "runtime::sandbox";
#[derive(RuntimeDebug)]
pub enum Error {
Module,
OutOfBounds,
MemoryGrow,
Execution,
}
impl From<Error> for HostError {
fn from(_e: Error) -> HostError {
HostError
}
}
pub type HostFuncType<T> =
fn(&mut default_executor::Caller<'_, T>, &[Value]) -> Result<env::WasmReturnValue, HostError>;
pub trait SandboxStore: AsContextExt {
fn new(state: Self::State) -> Self;
}
pub trait AsContextExt: default_executor::AsContext {
type State;
fn data_mut(&mut self) -> &mut Self::State;
}
pub trait SandboxMemory<T>: Sized + Clone {
fn new(
store: &mut default_executor::Store<T>,
initial: u32,
maximum: Option<u32>,
) -> Result<Self, Error>;
fn read<Context>(&self, ctx: &Context, ptr: u32, buf: &mut [u8]) -> Result<(), Error>
where
Context: AsContextExt<State = T>;
fn write<Context>(&self, ctx: &mut Context, ptr: u32, value: &[u8]) -> Result<(), Error>
where
Context: AsContextExt<State = T>;
fn grow<Context>(&self, ctx: &mut Context, pages: u32) -> Result<u32, Error>
where
Context: AsContextExt<State = T>;
fn size<Context>(&self, ctx: &Context) -> u32
where
Context: AsContextExt<State = T>;
unsafe fn get_buff<Context>(&self, ctx: &Context) -> HostPointer
where
Context: AsContextExt<State = T>;
}
pub trait SandboxEnvironmentBuilder<State, Memory>: Sized {
fn new() -> Self;
fn add_host_func<N1, N2>(&mut self, module: N1, field: N2, f: HostFuncType<State>)
where
N1: Into<String>,
N2: Into<String>;
fn add_memory<N1, N2>(&mut self, module: N1, field: N2, mem: Memory)
where
N1: Into<String>,
N2: Into<String>;
}
#[derive(RuntimeDebug)]
pub enum GlobalsSetError {
NotFound,
Other,
}
pub trait SandboxInstance<State>: Sized {
type Memory: SandboxMemory<State>;
type EnvironmentBuilder: SandboxEnvironmentBuilder<State, Self::Memory>;
fn new(
store: &mut default_executor::Store<State>,
code: &[u8],
env_def_builder: &Self::EnvironmentBuilder,
) -> Result<Self, Error>;
fn invoke(
&mut self,
store: &mut default_executor::Store<State>,
name: &str,
args: &[Value],
) -> Result<ReturnValue, Error>;
fn get_global_val(
&self,
store: &mut default_executor::Store<State>,
name: &str,
) -> Option<Value>;
fn set_global_val(
&self,
store: &mut default_executor::Store<State>,
name: &str,
value: Value,
) -> Result<(), GlobalsSetError>;
fn get_instance_ptr(&self) -> HostPointer;
}