fuel_vm/interpreter/
constructors.rs

1//! Exposed constructors API for the [`Interpreter`]
2#![allow(clippy::default_constructed_unit_structs)] // need for ::default() depends on cfg
3
4#[cfg(any(test, feature = "test-helpers"))]
5use super::{
6    ExecutableTransaction,
7    MemoryInstance,
8};
9use super::{
10    Interpreter,
11    RuntimeBalances,
12};
13use crate::{
14    consts::*,
15    context::Context,
16    interpreter::{
17        InterpreterParams,
18        PanicContext,
19    },
20    state::Debugger,
21};
22
23use alloc::vec;
24
25#[cfg(feature = "profile-any")]
26use crate::profiler::ProfileReceiver;
27
28use crate::profiler::Profiler;
29
30#[cfg(feature = "test-helpers")]
31use crate::{
32    interpreter::EcalHandler,
33    storage::MemoryStorage,
34};
35
36impl<M, S, Tx, Ecal> Interpreter<M, S, Tx, Ecal>
37where
38    Tx: Default,
39    Ecal: Default,
40{
41    /// Create a new interpreter instance out of a storage implementation.
42    ///
43    /// If the provided storage implements
44    /// [`crate::storage::InterpreterStorage`], the returned interpreter
45    /// will provide full functionality.
46    pub fn with_storage(
47        memory: M,
48        storage: S,
49        interpreter_params: InterpreterParams,
50    ) -> Self {
51        Self::with_storage_and_ecal(memory, storage, interpreter_params, Ecal::default())
52    }
53}
54
55impl<M, S, Tx, Ecal> Interpreter<M, S, Tx, Ecal>
56where
57    Tx: Default,
58{
59    /// Create a new interpreter instance out of a storage implementation.
60    ///
61    /// If the provided storage implements
62    /// [`crate::storage::InterpreterStorage`], the returned interpreter
63    /// will provide full functionality.
64    pub fn with_storage_and_ecal(
65        memory: M,
66        storage: S,
67        interpreter_params: InterpreterParams,
68        ecal_state: Ecal,
69    ) -> Self {
70        Self {
71            registers: [0; VM_REGISTER_COUNT],
72            memory,
73            frames: vec![],
74            receipts: Default::default(),
75            tx: Default::default(),
76            input_contracts: Default::default(),
77            input_contracts_index_to_output_index: Default::default(),
78            initial_balances: Default::default(),
79            storage,
80            debugger: Debugger::default(),
81            context: Context::default(),
82            balances: RuntimeBalances::default(),
83            profiler: Profiler::default(),
84            interpreter_params,
85            panic_context: PanicContext::None,
86            ecal_state,
87        }
88    }
89}
90
91impl<M, S, Tx, Ecal> Interpreter<M, S, Tx, Ecal> {
92    /// Sets a profiler for the VM
93    #[cfg(feature = "profile-any")]
94    pub fn with_profiler<P>(&mut self, receiver: P) -> &mut Self
95    where
96        P: ProfileReceiver + Send + Sync + 'static,
97    {
98        self.profiler.set_receiver(alloc::boxed::Box::new(receiver));
99        self
100    }
101}
102
103#[cfg(any(test, feature = "test-helpers"))]
104impl<S, Tx, Ecal> Default for Interpreter<MemoryInstance, S, Tx, Ecal>
105where
106    S: Default,
107    Tx: ExecutableTransaction,
108    Ecal: EcalHandler + Default,
109{
110    fn default() -> Self {
111        Interpreter::<_, S, Tx, Ecal>::with_storage(
112            MemoryInstance::new(),
113            Default::default(),
114            InterpreterParams::default(),
115        )
116    }
117}
118
119#[cfg(any(test, feature = "test-helpers"))]
120impl<Tx, Ecal> Interpreter<MemoryInstance, (), Tx, Ecal>
121where
122    Tx: ExecutableTransaction,
123    Ecal: EcalHandler + Default,
124{
125    /// Create a new interpreter without a storage backend.
126    ///
127    /// It will have restricted capabilities.
128    pub fn without_storage() -> Self {
129        Self::default()
130    }
131}
132
133#[cfg(feature = "test-helpers")]
134impl<Tx, Ecal> Interpreter<MemoryInstance, MemoryStorage, Tx, Ecal>
135where
136    Tx: ExecutableTransaction,
137    Ecal: EcalHandler + Default,
138{
139    /// Create a new storage with a provided in-memory storage.
140    ///
141    /// It will have full capabilities.
142    pub fn with_memory_storage() -> Self {
143        Self::default()
144    }
145}
146
147#[cfg(feature = "test-helpers")]
148impl<Tx, Ecal> Interpreter<MemoryInstance, MemoryStorage, Tx, Ecal>
149where
150    Tx: ExecutableTransaction,
151    Ecal: EcalHandler,
152{
153    /// Create a new storage with a provided in-memory storage.
154    ///
155    /// It will have full capabilities.
156    pub fn with_memory_storage_and_ecal(ecal: Ecal) -> Self {
157        Interpreter::<_, MemoryStorage, Tx, Ecal>::with_storage_and_ecal(
158            MemoryInstance::new(),
159            Default::default(),
160            InterpreterParams::default(),
161            ecal,
162        )
163    }
164}