From 3b04b6d23852cd43768aeac8bf55bc829e878258 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Wed, 8 Sep 2021 18:51:34 +0300 Subject: [PATCH] vm: refactor stack dump commands --- pkg/vm/cli/cli.go | 14 ++++++++++++-- pkg/vm/vm.go | 21 ++++++++++++--------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/pkg/vm/cli/cli.go b/pkg/vm/cli/cli.go index e5e6e915a..671693834 100644 --- a/pkg/vm/cli/cli.go +++ b/pkg/vm/cli/cli.go @@ -301,7 +301,17 @@ func handleBreak(c *ishell.Context) { func handleXStack(c *ishell.Context) { v := getVMFromContext(c) - c.Println(v.Stack(c.Cmd.Name)) + var stackDump string + switch c.Cmd.Name { + case "estack": + stackDump = v.DumpEStack() + case "istack": + stackDump = v.DumpIStack() + default: + c.Err(errors.New("unknown stack")) + return + } + c.Println(stackDump) } func handleSlots(c *ishell.Context) { @@ -470,7 +480,7 @@ func runVMWithHandling(c *ishell.Context, v *vm.VM) { case v.HasFailed(): message = "" // the error will be printed on return case v.HasHalted(): - message = v.Stack("estack") + message = v.DumpEStack() case v.AtBreakpoint(): ctx := v.Context() if ctx.NextIP() < ctx.LenInstr() { diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index 5b2d83254..0211ce79e 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -334,15 +334,18 @@ func (v *VM) PopResult() interface{} { return v.estack.Pop().Value() } -// Stack returns json formatted representation of the given stack. -func (v *VM) Stack(n string) string { - var s *Stack - if n == "istack" { - s = &v.istack - } - if n == "estack" { - s = v.estack - } +// DumpIStack returns json formatted representation of the invocation stack. +func (v *VM) DumpIStack() string { + return dumpStack(&v.istack) +} + +// DumpEStack returns json formatted representation of the execution stack. +func (v *VM) DumpEStack() string { + return dumpStack(v.estack) +} + +// dumpStack returns json formatted representation of the given stack. +func dumpStack(s *Stack) string { b, _ := json.MarshalIndent(s, "", " ") return string(b) }