vm: refactor stack dump commands

This commit is contained in:
Anna Shaleva 2021-09-08 18:51:34 +03:00
parent 6da458365d
commit 3b04b6d238
2 changed files with 24 additions and 11 deletions

View file

@ -301,7 +301,17 @@ func handleBreak(c *ishell.Context) {
func handleXStack(c *ishell.Context) { func handleXStack(c *ishell.Context) {
v := getVMFromContext(c) 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) { func handleSlots(c *ishell.Context) {
@ -470,7 +480,7 @@ func runVMWithHandling(c *ishell.Context, v *vm.VM) {
case v.HasFailed(): case v.HasFailed():
message = "" // the error will be printed on return message = "" // the error will be printed on return
case v.HasHalted(): case v.HasHalted():
message = v.Stack("estack") message = v.DumpEStack()
case v.AtBreakpoint(): case v.AtBreakpoint():
ctx := v.Context() ctx := v.Context()
if ctx.NextIP() < ctx.LenInstr() { if ctx.NextIP() < ctx.LenInstr() {

View file

@ -334,15 +334,18 @@ func (v *VM) PopResult() interface{} {
return v.estack.Pop().Value() return v.estack.Pop().Value()
} }
// Stack returns json formatted representation of the given stack. // DumpIStack returns json formatted representation of the invocation stack.
func (v *VM) Stack(n string) string { func (v *VM) DumpIStack() string {
var s *Stack return dumpStack(&v.istack)
if n == "istack" { }
s = &v.istack
} // DumpEStack returns json formatted representation of the execution stack.
if n == "estack" { func (v *VM) DumpEStack() string {
s = v.estack return dumpStack(v.estack)
} }
// dumpStack returns json formatted representation of the given stack.
func dumpStack(s *Stack) string {
b, _ := json.MarshalIndent(s, "", " ") b, _ := json.MarshalIndent(s, "", " ")
return string(b) return string(b)
} }