use alloc::{
borrow::ToOwned,
vec::Vec,
};
use crate::{
call::CallFrame,
consts::*,
interpreter::{
InitialBalances,
Interpreter,
},
};
use derivative::Derivative;
use crate::interpreter::{
Memory,
MemoryInstance,
};
use fuel_tx::ScriptExecutionResult;
use fuel_types::{
ContractId,
Word,
};
#[derive(Derivative)]
#[derivative(Debug)]
pub struct Backtrace {
call_stack: Vec<CallFrame>,
contract: ContractId,
registers: [Word; VM_REGISTER_COUNT],
memory: MemoryInstance,
result: ScriptExecutionResult,
initial_balances: InitialBalances,
}
impl Backtrace {
pub fn from_vm_error<M, S, Tx, Ecal>(
vm: &Interpreter<M, S, Tx, Ecal>,
result: ScriptExecutionResult,
) -> Self
where
M: Memory,
{
let call_stack = vm.call_stack().to_owned();
let contract = vm.internal_contract().unwrap_or_default();
let memory = vm.memory().clone();
let initial_balances = vm.initial_balances().clone();
let mut registers = [0; VM_REGISTER_COUNT];
registers.copy_from_slice(vm.registers());
Self {
call_stack,
contract,
registers,
memory,
result,
initial_balances,
}
}
pub fn call_stack(&self) -> &[CallFrame] {
self.call_stack.as_slice()
}
pub const fn contract(&self) -> &ContractId {
&self.contract
}
pub const fn registers(&self) -> &[Word] {
&self.registers
}
pub fn memory(&self) -> &MemoryInstance {
&self.memory
}
pub const fn result(&self) -> &ScriptExecutionResult {
&self.result
}
pub const fn initial_balances(&self) -> &InitialBalances {
&self.initial_balances
}
pub fn into_inner(
self,
) -> (
Vec<CallFrame>,
ContractId,
[Word; VM_REGISTER_COUNT],
MemoryInstance,
ScriptExecutionResult,
InitialBalances,
) {
let Self {
call_stack,
contract,
registers,
memory,
result,
initial_balances,
} = self;
(
call_stack,
contract,
registers,
memory,
result,
initial_balances,
)
}
}