1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use primitive_types::H256;
use alloc::vec::Vec;
use crate::ExitError;
#[derive(Clone, Debug)]
pub struct Stack {
data: Vec<H256>,
limit: usize,
}
impl Stack {
pub fn new(limit: usize) -> Self {
Self {
data: Vec::new(),
limit,
}
}
#[inline]
pub fn limit(&self) -> usize {
self.limit
}
#[inline]
pub fn len(&self) -> usize {
self.data.len()
}
#[inline]
pub fn pop(&mut self) -> Result<H256, ExitError> {
self.data.pop().ok_or(ExitError::StackUnderflow)
}
#[inline]
pub fn push(&mut self, value: H256) -> Result<(), ExitError> {
if self.data.len() + 1 > self.limit {
return Err(ExitError::StackOverflow)
}
self.data.push(value);
Ok(())
}
#[inline]
pub fn peek(&self, no_from_top: usize) -> Result<H256, ExitError> {
if self.data.len() > no_from_top {
Ok(self.data[self.data.len() - no_from_top - 1])
} else {
Err(ExitError::StackUnderflow)
}
}
#[inline]
pub fn set(&mut self, no_from_top: usize, val: H256) -> Result<(), ExitError> {
if self.data.len() > no_from_top {
let len = self.data.len();
self.data[len - no_from_top - 1] = val;
Ok(())
} else {
Err(ExitError::StackUnderflow)
}
}
}