vm: implement storage slots

This commit is contained in:
Evgenii Stratonikov 2020-05-07 11:45:24 +03:00
parent 8e916f5ab8
commit 81cbf183af
2 changed files with 50 additions and 0 deletions

29
pkg/vm/slot.go Normal file
View file

@ -0,0 +1,29 @@
package vm
// Slot is a fixed-size slice of stack items.
type Slot struct {
storage []StackItem
}
// newSlot returns new slot of n items.
func newSlot(n int) *Slot {
return &Slot{
storage: make([]StackItem, n),
}
}
// Set sets i-th storage slot.
func (s *Slot) Set(i int, item StackItem) {
s.storage[i] = item
}
// Get returns item contained in i-th slot.
func (s *Slot) Get(i int) StackItem {
if item := s.storage[i]; item != nil {
return item
}
return NullItem{}
}
// Size returns slot size.
func (s *Slot) Size() int { return len(s.storage) }

21
pkg/vm/slot_test.go Normal file
View file

@ -0,0 +1,21 @@
package vm
import (
"math/big"
"testing"
"github.com/stretchr/testify/require"
)
func TestSlot_Get(t *testing.T) {
s := newSlot(3)
require.NotNil(t, s)
require.Equal(t, 3, s.Size())
// NullItem is the default
item := s.Get(2)
require.Equal(t, NullItem{}, item)
s.Set(1, NewBigIntegerItem(big.NewInt(42)))
require.Equal(t, NewBigIntegerItem(big.NewInt(42)), s.Get(1))
}