1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//! This mod includes executor instances
//!
//! * Memory
//! * Builder
//! * Instance
//!
//! Which have seem methods like the matching trait.
use crate::{
    derive::{self, HostFuncType, HostParcel, ReturnValue, Value},
    Result,
};
use ceres_std::Vec;
use core::ops;

#[cfg(not(feature = "std"))]
use crate::wasmi as e;
#[cfg(feature = "std")]
use crate::wasmtime as e;

/// WASM executor liner memory
#[derive(Clone)]
pub struct Memory(pub e::Memory);

impl ops::Deref for Memory {
    type Target = e::Memory;

    fn deref(&self) -> &e::Memory {
        &self.0
    }
}

impl Memory {
    /// New liner memory
    pub fn new(initial: u32, maximum: Option<u32>) -> Result<Self> {
        Ok(Self(<e::Memory as derive::Memory>::new(initial, maximum)?))
    }

    /// Read a memory area at the address `ptr` with the size of the provided slice `buf`.
    pub fn get(&self, ptr: u32, buf: &mut [u8]) -> Result<()> {
        derive::Memory::get(&self.0, ptr, buf)
    }

    /// Write a memory area at the address `ptr` with contents of the provided slice `buf`.
    pub fn set(&self, ptr: u32, value: &[u8]) -> Result<()> {
        derive::Memory::set(&self.0, ptr, value)
    }
}

/// Ceres environment builder
pub struct Builder<T>(e::Builder<T>);

impl<T> Default for Builder<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T> ops::Deref for Builder<T> {
    type Target = e::Builder<T>;

    fn deref(&self) -> &e::Builder<T> {
        &self.0
    }
}

impl<T> Builder<T> {
    /// New env builder
    pub fn new() -> Self {
        Builder(<e::Builder<T> as derive::Builder<T>>::new())
    }

    /// Register a host function in this environment definition
    pub fn add_host_func<M, F>(&mut self, module: M, field: F, f: HostFuncType<T>)
    where
        F: Into<Vec<u8>>,
        M: Into<Vec<u8>>,
    {
        derive::Builder::add_host_func(&mut self.0, module, field, f);
    }

    /// Shortcut of `add_host_func`
    pub fn add_host_parcel<M, F>(&mut self, parcel: HostParcel<M, F, T>)
    where
        F: Into<Vec<u8>>,
        M: Into<Vec<u8>>,
    {
        self.add_host_func(parcel.0, parcel.1, parcel.2)
    }

    /// Shortcut of `add_host_func`
    pub fn add_host_parcels<M, F>(mut self, parcels: Vec<HostParcel<M, F, T>>) -> Self
    where
        F: Into<Vec<u8>>,
        M: Into<Vec<u8>>,
    {
        for parcel in parcels {
            self.add_host_func(parcel.0, parcel.1, parcel.2)
        }

        self
    }

    /// Register a memory in this environment definition.
    pub fn add_memory<M, F>(&mut self, module: M, field: F, mem: Memory)
    where
        M: Into<Vec<u8>>,
        F: Into<Vec<u8>>,
    {
        derive::Builder::add_memory(&mut self.0, module, field, mem.0);
    }
}

/// Instance instance
pub struct Instance<T>(e::Instance<T>);

impl<T> Instance<T> {
    /// Instantiate a module with the given env builder
    pub fn new(code: &[u8], builder: &Builder<T>, state: &mut T) -> Result<Self> {
        Ok(Instance(<e::Instance<T> as derive::Instance<T>>::new(
            code, &builder, state,
        )?))
    }

    /// invoke an exported function
    pub fn invoke(&mut self, name: &str, args: &[Value], state: &mut T) -> Result<ReturnValue> {
        derive::Instance::invoke(&mut self.0, name, args, state)
    }

    /// Get global value
    pub fn get_global_val(&self, name: &str) -> Option<Value> {
        derive::Instance::get_global_val(&self.0, name)
    }
}