From 2f39701d76a0b394313ba319ad0a397f20bd10db Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 1 Dec 2020 16:52:23 +0300 Subject: [PATCH] vm: provide writer in `PrintOps()` Make it more flexible and testable. Fallback to using stdout if no writer is provided. --- cli/smartcontract/smart_contract.go | 2 +- pkg/vm/cli/cli.go | 4 +++- pkg/vm/vm.go | 8 ++++++-- pkg/vm/vm_test.go | 2 -- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/cli/smartcontract/smart_contract.go b/cli/smartcontract/smart_contract.go index a556570de..dffb75829 100644 --- a/cli/smartcontract/smart_contract.go +++ b/cli/smartcontract/smart_contract.go @@ -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 } diff --git a/pkg/vm/cli/cli.go b/pkg/vm/cli/cli.go index 61e00d585..53d6bc8df 100644 --- a/pkg/vm/cli/cli.go +++ b/pkg/vm/cli/cli.go @@ -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) { diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index 9fd3d4540..63a020b05 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -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() diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index 046bc9548..56d8598e9 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -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) } }