neoneo-go/pkg/vm/stack_item.go
Anthony De Meulemeester 931388b687
Cross platform virtual machine implementation (#60)
* Virtual machine for the NEO blockhain.

* fixed big.Int numeric operation pointer issue.

* added appcall

* Added README for vm package.

* removed main.go

* started VM cli (prompt) integration

* added support for printing the stack.

* moved cli to vm package

* fixed vet errors

* updated readme

* added more test for VM and fixed some edge cases.

* bumped version -> 0.37.0
2018-03-30 18:15:06 +02:00

113 lines
1.7 KiB
Go

package vm
import (
"fmt"
"math/big"
"reflect"
)
// A StackItem represents the "real" value that is pushed on the stack.
type StackItem interface {
fmt.Stringer
Value() interface{}
}
func makeStackItem(v interface{}) StackItem {
switch val := v.(type) {
case int:
return &bigIntegerItem{
value: big.NewInt(int64(val)),
}
case []byte:
return &byteArrayItem{
value: val,
}
case bool:
return &boolItem{
value: val,
}
case []StackItem:
return &arrayItem{
value: val,
}
case *big.Int:
return &bigIntegerItem{
value: val,
}
case StackItem:
return val
default:
panic(
fmt.Sprintf(
"invalid stack item type: %v (%s)",
val,
reflect.TypeOf(val),
),
)
}
}
type structItem struct {
value []StackItem
}
// Value implements StackItem interface.
func (i *structItem) Value() interface{} {
return i.value
}
func (i *structItem) String() string {
return "Struct"
}
type bigIntegerItem struct {
value *big.Int
}
// Value implements StackItem interface.
func (i *bigIntegerItem) Value() interface{} {
return i.value
}
func (i *bigIntegerItem) String() string {
return "BigInteger"
}
type boolItem struct {
value bool
}
// Value implements StackItem interface.
func (i *boolItem) Value() interface{} {
return i.value
}
func (i *boolItem) String() string {
return "Bool"
}
type byteArrayItem struct {
value []byte
}
// Value implements StackItem interface.
func (i *byteArrayItem) Value() interface{} {
return i.value
}
func (i *byteArrayItem) String() string {
return "ByteArray"
}
type arrayItem struct {
value []StackItem
}
// Value implements StackItem interface.
func (i *arrayItem) Value() interface{} {
return i.value
}
func (i *arrayItem) String() string {
return "Array"
}