vm: provide writer in PrintOps()

Make it more flexible and testable. Fallback to using
stdout if no writer is provided.
This commit is contained in:
Evgenii Stratonikov 2020-12-01 16:52:23 +03:00
parent 9a4183abb9
commit 2f39701d76
4 changed files with 10 additions and 6 deletions

View file

@ -678,7 +678,7 @@ func inspect(ctx *cli.Context) error {
}
v := vm.New()
v.LoadScript(b)
v.PrintOps()
v.PrintOps(ctx.App.Writer)
return nil
}

View file

@ -429,7 +429,9 @@ func handleOps(c *ishell.Context) {
return
}
v := getVMFromContext(c)
v.PrintOps()
out := bytes.NewBuffer(nil)
v.PrintOps(out)
c.Println(out.String())
}
func changePrompt(c ishell.Actions, v *vm.VM) {

View file

@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"math"
"math/big"
@ -151,8 +152,11 @@ func (v *VM) LoadArgs(method []byte, args []stackitem.Item) {
}
// PrintOps prints the opcodes of the current loaded program to stdout.
func (v *VM) PrintOps() {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0)
func (v *VM) PrintOps(out io.Writer) {
if out == nil {
out = os.Stdout
}
w := tabwriter.NewWriter(out, 0, 0, 4, ' ', 0)
fmt.Fprintln(w, "INDEX\tOPCODE\tPARAMETER\t")
realctx := v.Context()
ctx := realctx.Copy()

View file

@ -225,8 +225,6 @@ func TestISTYPE(t *testing.T) {
func testCONVERT(to stackitem.Type, item, res stackitem.Item) func(t *testing.T) {
return func(t *testing.T) {
prog := []byte{byte(opcode.CONVERT), byte(to)}
v := load(prog)
v.PrintOps()
runWithArgs(t, prog, res, item)
}
}