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            owner_ptr: None,
85        }
86    }
87}
88
89#[cfg(any(test, feature = "test-helpers"))]
90impl<S, Tx, Ecal, V> Default for Interpreter<MemoryInstance, S, Tx, Ecal, V>
91where
92    S: Default,
93    Tx: ExecutableTransaction,
94    Ecal: EcalHandler + Default,
95    V: Default,
96{
97    fn default() -> Self {
98        Interpreter::<_, S, Tx, Ecal, V>::with_storage(
99            MemoryInstance::new(),
100            Default::default(),
101            InterpreterParams::default(),
102        )
103    }
104}
105
106#[cfg(any(test, feature = "test-helpers"))]
107impl<Tx, Ecal, V> Interpreter<MemoryInstance, (), Tx, Ecal, V>
108where
109    Tx: ExecutableTransaction,
110    Ecal: EcalHandler + Default,
111    V: Default,
112{
113    /// Create a new interpreter without a storage backend.
114    ///
115    /// It will have restricted capabilities.
116    pub fn without_storage() -> Self {
117        Self::default()
118    }
119}
120
121#[cfg(feature = "test-helpers")]
122impl<Tx, Ecal, V> Interpreter<MemoryInstance, MemoryStorage, Tx, Ecal, V>
123where
124    Tx: ExecutableTransaction,
125    Ecal: EcalHandler + Default,
126    V: Default,
127{
128    /// Create a new storage with a provided in-memory storage.
129    ///
130    /// It will have full capabilities.
131    pub fn with_memory_storage() -> Self {
132        Self::default()
133    }
134}
135
136#[cfg(feature = "test-helpers")]
137impl<Tx, Ecal, V> Interpreter<MemoryInstance, MemoryStorage, Tx, Ecal, V>
138where
139    Tx: ExecutableTransaction,
140    Ecal: EcalHandler,
141    V: Default,
142{
143    /// Create a new storage with a provided in-memory storage.
144    ///
145    /// It will have full capabilities.
146    pub fn with_memory_storage_and_ecal(ecal: Ecal) -> Self {
147        Interpreter::<_, MemoryStorage, Tx, Ecal, V>::with_storage_and_ecal(
148            MemoryInstance::new(),
149            Default::default(),
150            InterpreterParams::default(),
151            ecal,
152        )
153    }
154}