Stack struct implements the EVM stack with a maximum capacity of 1024 elements. It’s implemented as a linked list for efficient push/pop operations.
Structure
Constants
- STACK_SIZE_LIMIT: 1024 elements (maximum stack depth)
Methods
new()
Creates a new empty stack.Returns a new stack instance with length 0
push()
Pushes an item onto the top of the stack.The item to push onto the stack
Returns the index of the pushed item or StackError::StackOverflow if the stack is full
pop()
Removes and returns the top item from the stack.Returns a tuple of (index, item) or StackError::StackUnderflow if the stack is empty
dup()
Duplicates the item at the specified index onto the top of the stack.The index from the top to duplicate (0 = top item, 1 = second item, etc.)
Returns (duplicated_index, duplicated_value) or an error if index is out of bounds
swap()
Swaps the item at the specified index with the top item. Note: This method is marked asunsafe and should be used with caution.
The index to swap with the top (1 = swap top with second, 2 = swap top with third, etc.)
Returns ([head_index, swapped_index], [head_item, swapped_item]) or an error
peek()
Returns a reference to the top item without removing it.Returns Some(&item) if the stack is not empty, None otherwise
is_empty()
Checks if the stack is empty.Returns true if the stack has no elements
Error Handling
The stack can return the following errors:- StackOverflow: Attempting to push when the stack has 1024 elements
- StackUnderflow: Attempting to pop from an empty stack
- StackSizeExceeded: Index out of bounds in dup() or swap()
- StackIsEmpty: Attempting operations on an empty stack
- WrongIndex: Using index 0 with swap() (invalid operation)