- Add guide to stack readme

- Add testReadInt64
This commit is contained in:
BlockChainDev 2019-02-28 13:51:02 +00:00
parent 5789aba4b2
commit f60d65f1a4
3 changed files with 33 additions and 2 deletions

24
pkg/vm/stack/Readme.md Normal file
View file

@ -0,0 +1,24 @@
## VM - Stack
- How do i implement a new StackItem?
Answer: You add it's type to the Item interface, then you implement the default return method on the abstract stack item, this should be the behaviour of the stack item, if it is not the new type. Then you embed the abstract item in the new struct and override the method.
For example, If I wanted to add a new type called `HashMap`
type Item interface{
HashMap()(*HashMap, error)
}
func (a *abstractItem) HashMap() (*HashMap, error) {
return nil, errors.New(This stack item is not a hashmap)
}
type HashMap struct {
*abstractItem
// Variables needed for hashmap
}
func (h *HashMap) HashMap()(*HashMap, error) {
// logic to override default behaviour
}

View file

@ -42,8 +42,6 @@ func (ba *ByteArray) Integer() (*Int, error) {
ba.abstractItem,
integerVal,
}, nil
// return ba, nil
}
// Boolean will convert

View file

@ -1,6 +1,8 @@
package stack
import (
"bytes"
"encoding/binary"
"math/big"
"testing"
@ -33,3 +35,10 @@ func testMakeStackInt(t *testing.T, num int64) *Int {
assert.Nil(t, err)
return a
}
func testReadInt64(data []byte) int64 {
var ret int64
buf := bytes.NewBuffer(data)
binary.Read(buf, binary.LittleEndian, &ret)
return ret
}