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) {
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() {

View file

@ -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)
}