ceres_executor/
builder.rs

1//! Environment builder
2#[cfg(not(feature = "std"))]
3use crate::wasmi as e;
4#[cfg(feature = "std")]
5use crate::wasmtime as e;
6use crate::{
7    derive::{self, HostCall, HostFuncType},
8    memory::Memory,
9};
10use ceres_std::Vec;
11use core::ops;
12
13/// Ceres environment builder
14pub struct Builder<T>(e::Builder<T>);
15
16impl<T> Default for Builder<T> {
17    fn default() -> Self {
18        Self::new()
19    }
20}
21
22impl<T> ops::Deref for Builder<T> {
23    type Target = e::Builder<T>;
24
25    fn deref(&self) -> &e::Builder<T> {
26        &self.0
27    }
28}
29
30impl<T> Builder<T> {
31    /// New env builder
32    pub fn new() -> Self {
33        Builder(<e::Builder<T> as derive::Builder<T>>::new())
34    }
35
36    /// Register a host function in this environment definition
37    pub fn add_host_func<M, F>(&mut self, module: M, field: F, f: HostFuncType<T>)
38    where
39        F: Into<Vec<u8>>,
40        M: Into<Vec<u8>>,
41    {
42        derive::Builder::add_host_func(&mut self.0, module, field, f);
43    }
44
45    /// Shortcut of `add_host_func`
46    pub fn add_host_parcel<M, F>(&mut self, call: HostCall<M, F, T>)
47    where
48        F: Into<Vec<u8>>,
49        M: Into<Vec<u8>>,
50    {
51        self.add_host_func(call.0, call.1, call.2)
52    }
53
54    /// Shortcut of `add_host_func`
55    pub fn add_host_parcels<M, F>(mut self, calls: Vec<HostCall<M, F, T>>) -> Self
56    where
57        F: Into<Vec<u8>>,
58        M: Into<Vec<u8>>,
59    {
60        for call in calls {
61            self.add_host_func(call.0, call.1, call.2)
62        }
63
64        self
65    }
66
67    /// Register a memory in this environment definition.
68    pub fn add_memory<M, F>(&mut self, module: M, field: F, mem: Memory)
69    where
70        M: Into<Vec<u8>>,
71        F: Into<Vec<u8>>,
72    {
73        derive::Builder::add_memory(&mut self.0, module, field, mem.0);
74    }
75}