From f60d65f1a428b8bb98b05960f228bcc387377dee Mon Sep 17 00:00:00 2001 From: BlockChainDev Date: Thu, 28 Feb 2019 13:51:02 +0000 Subject: [PATCH] - Add guide to stack readme - Add testReadInt64 --- pkg/vm/stack/Readme.md | 24 ++++++++++++++++++++++++ pkg/vm/stack/bytearray.go | 2 -- pkg/vm/stack/testhelper.go | 9 +++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 pkg/vm/stack/Readme.md diff --git a/pkg/vm/stack/Readme.md b/pkg/vm/stack/Readme.md new file mode 100644 index 000000000..2e1b6ba78 --- /dev/null +++ b/pkg/vm/stack/Readme.md @@ -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 +} diff --git a/pkg/vm/stack/bytearray.go b/pkg/vm/stack/bytearray.go index 4ff318e2b..7d57b12f0 100644 --- a/pkg/vm/stack/bytearray.go +++ b/pkg/vm/stack/bytearray.go @@ -42,8 +42,6 @@ func (ba *ByteArray) Integer() (*Int, error) { ba.abstractItem, integerVal, }, nil - - // return ba, nil } // Boolean will convert diff --git a/pkg/vm/stack/testhelper.go b/pkg/vm/stack/testhelper.go index 443a522ce..15c6f87de 100644 --- a/pkg/vm/stack/testhelper.go +++ b/pkg/vm/stack/testhelper.go @@ -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 +}