1use crate::prelude::Vec;
2use crate::utils::USIZE_MAX;
3use crate::ExitError;
4use primitive_types::{H256, U256};
5
6#[derive(Clone, Debug)]
8pub struct Stack {
9 data: Vec<U256>,
10 limit: usize,
11}
12
13impl Stack {
14 #[must_use]
16 pub const fn new(limit: usize) -> Self {
17 Self {
18 data: Vec::new(),
19 limit,
20 }
21 }
22
23 #[inline]
25 #[must_use]
26 pub const fn limit(&self) -> usize {
27 self.limit
28 }
29
30 #[inline]
32 #[must_use]
33 pub fn len(&self) -> usize {
34 self.data.len()
35 }
36
37 #[inline]
39 #[must_use]
40 pub fn is_empty(&self) -> bool {
41 self.data.is_empty()
42 }
43
44 #[inline]
46 #[must_use]
47 pub const fn data(&self) -> &Vec<U256> {
48 &self.data
49 }
50
51 #[inline]
57 pub fn pop(&mut self) -> Result<U256, ExitError> {
58 self.data.pop().ok_or(ExitError::StackUnderflow)
59 }
60
61 #[inline]
64 pub fn pop_h256(&mut self) -> Result<H256, ExitError> {
65 self.pop().map(|it| H256(it.to_big_endian()))
66 }
67
68 #[inline]
74 pub fn push(&mut self, value: U256) -> Result<(), ExitError> {
75 if self.data.len() + 1 > self.limit {
76 return Err(ExitError::StackOverflow);
77 }
78 self.data.push(value);
79 Ok(())
80 }
81
82 #[inline]
89 pub fn peek(&self, no_from_top: usize) -> Result<U256, ExitError> {
90 if self.data.len() > no_from_top {
91 Ok(self.data[self.data.len() - no_from_top - 1])
92 } else {
93 Err(ExitError::StackUnderflow)
94 }
95 }
96
97 #[inline]
98 pub fn peek_h256(&self, no_from_top: usize) -> Result<H256, ExitError> {
105 self.peek(no_from_top).map(|it| H256(it.to_big_endian()))
106 }
107
108 #[inline]
115 pub fn peek_usize(&self, no_from_top: usize) -> Result<usize, ExitError> {
116 let u = self.peek(no_from_top)?;
117 if u > USIZE_MAX {
118 return Err(ExitError::OutOfGas);
119 }
120 Ok(u.as_usize())
121 }
122
123 #[inline]
130 pub fn set(&mut self, no_from_top: usize, val: U256) -> Result<(), ExitError> {
131 if self.data.len() > no_from_top {
132 let len = self.data.len();
133 self.data[len - no_from_top - 1] = val;
134 Ok(())
135 } else {
136 Err(ExitError::StackUnderflow)
137 }
138 }
139}