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 = "test-helpers")]
26use crate::{
27    interpreter::EcalHandler,
28    storage::MemoryStorage,
29};
30
31impl<M, S, Tx, Ecal, V> Interpreter<M, S, Tx, Ecal, V>
32where
33    Tx: Default,
34    Ecal: Default,
35    V: Default,
36{
37    /// Create a new interpreter instance out of a storage implementation.
38    ///
39    /// If the provided storage implements
40    /// [`crate::storage::InterpreterStorage`], the returned interpreter
41    /// will provide full functionality.
42    pub fn with_storage(
43        memory: M,
44        storage: S,
45        interpreter_params: InterpreterParams,
46    ) -> Self {
47        Self::with_storage_and_ecal(memory, storage, interpreter_params, Ecal::default())
48    }
49}
50
51impl<M, S, Tx, Ecal, V> Interpreter<M, S, Tx, Ecal, V>
52where
53    Tx: Default,
54    V: Default,
55{
56    /// Create a new interpreter instance out of a storage implementation.
57    ///
58    /// If the provided storage implements
59    /// [`crate::storage::InterpreterStorage`], the returned interpreter
60    /// will provide full functionality.
61    pub fn with_storage_and_ecal(
62        memory: M,
63        storage: S,
64        interpreter_params: InterpreterParams,
65        ecal_state: Ecal,
66    ) -> Self {
67        Self {
68            registers: [0; VM_REGISTER_COUNT],
69            memory,
70            frames: vec![],
71            receipts: Default::default(),
72            tx: Default::default(),
73            input_contracts: Default::default(),
74            input_contracts_index_to_output_index: Default::default(),
75            initial_balances: Default::default(),
76            storage,
77            debugger: Debugger::default(),
78            context: Context::default(),
79            balances: RuntimeBalances::default(),
80            interpreter_params,
81            panic_context: PanicContext::None,
82            ecal_state,
83            verifier: Default::default(),
84        }
85    }
86}
87
88#[cfg(any(test, feature = "test-helpers"))]
89impl<S, Tx, Ecal, V> Default for Interpreter<MemoryInstance, S, Tx, Ecal, V>
90where
91    S: Default,
92    Tx: ExecutableTransaction,
93    Ecal: EcalHandler + Default,
94    V: Default,
95{
96    fn default() -> Self {
97        Interpreter::<_, S, Tx, Ecal, V>::with_storage(
98            MemoryInstance::new(),
99            Default::default(),
100            InterpreterParams::default(),
101        )
102    }
103}
104
105#[cfg(any(test, feature = "test-helpers"))]
106impl<Tx, Ecal, V> Interpreter<MemoryInstance, (), Tx, Ecal, V>
107where
108    Tx: ExecutableTransaction,
109    Ecal: EcalHandler + Default,
110    V: Default,
111{
112    /// Create a new interpreter without a storage backend.
113    ///
114    /// It will have restricted capabilities.
115    pub fn without_storage() -> Self {
116        Self::default()
117    }
118}
119
120#[cfg(feature = "test-helpers")]
121impl<Tx, Ecal, V> Interpreter<MemoryInstance, MemoryStorage, Tx, Ecal, V>
122where
123    Tx: ExecutableTransaction,
124    Ecal: EcalHandler + Default,
125    V: Default,
126{
127    /// Create a new storage with a provided in-memory storage.
128    ///
129    /// It will have full capabilities.
130    pub fn with_memory_storage() -> Self {
131        Self::default()
132    }
133}
134
135#[cfg(feature = "test-helpers")]
136impl<Tx, Ecal, V> Interpreter<MemoryInstance, MemoryStorage, Tx, Ecal, V>
137where
138    Tx: ExecutableTransaction,
139    Ecal: EcalHandler,
140    V: Default,
141{
142    /// Create a new storage with a provided in-memory storage.
143    ///
144    /// It will have full capabilities.
145    pub fn with_memory_storage_and_ecal(ecal: Ecal) -> Self {
146        Interpreter::<_, MemoryStorage, Tx, Ecal, V>::with_storage_and_ecal(
147            MemoryInstance::new(),
148            Default::default(),
149            InterpreterParams::default(),
150            ecal,
151        )
152    }
153}