neo-go/pkg/vm/output.go
Roman Khimov 94776b8a1f vm: add MarshalJSON to the Stack
To easily dump it in a known format.
2019-10-29 18:26:59 +03:00

26 lines
554 B
Go

package vm
import "encoding/json"
// StackOutput holds information about the stack, used for pretty printing
// the stack.
type stackItem struct {
Value interface{} `json:"value"`
Type string `json:"type"`
}
func stackToArray(s *Stack) []stackItem {
items := make([]stackItem, 0, s.Len())
s.Iter(func(e *Element) {
items = append(items, stackItem{
Value: e.value,
Type: e.value.String(),
})
})
return items
}
func buildStackOutput(s *Stack) string {
b, _ := json.MarshalIndent(stackToArray(s), "", " ")
return string(b)
}