vm: removed mute mode and pushed logging to upper lvl

VM should be responsible for code execution and in case anyone interested in additional logging or errors they could handle them like we do it iin cli.
This commit is contained in:
Vsevolod Brekelov 2019-10-22 13:44:14 +03:00
parent f2805541cb
commit e2bfff8666
7 changed files with 326 additions and 456 deletions

View file

@ -169,7 +169,7 @@ type VMCLI struct {
// New returns a new VMCLI object.
func New() *VMCLI {
vmcli := VMCLI{
vm: vm.New(0),
vm: vm.New(),
shell: ishell.New(),
}
vmcli.shell.Set(vmKey, vmcli.vm)
@ -286,16 +286,40 @@ func handleRun(c *ishell.Context) {
}
v.LoadArgs(method, params)
}
v.Run()
runVMWithHandling(c, v)
changePrompt(c, v)
}
// runVMWithHandling runs VM with handling errors and additional state messages.
func runVMWithHandling(c *ishell.Context, v *vm.VM) {
err := v.Run()
if err != nil {
c.Err(err)
return
}
var message string
switch {
case v.HasFailed():
message = "FAILED"
case v.HasHalted():
message = v.Stack("estack")
case v.AtBreakpoint():
ctx := v.Context()
i, op := ctx.CurrInstr()
message = fmt.Sprintf("at breakpoint %d (%s)\n", i, op.String())
}
if message != "" {
c.Printf(message)
}
}
func handleCont(c *ishell.Context) {
if !checkVMIsReady(c) {
return
}
v := getVMFromContext(c)
v.Run()
runVMWithHandling(c, v)
changePrompt(c, v)
}
@ -317,7 +341,7 @@ func handleStep(c *ishell.Context) {
}
}
v.AddBreakPointRel(n)
v.Run()
runVMWithHandling(c, v)
changePrompt(c, v)
}
@ -338,14 +362,19 @@ func handleStepType(c *ishell.Context, stepType string) {
return
}
v := getVMFromContext(c)
var err error
switch stepType {
case "into":
v.StepInto()
err = v.StepInto()
case "out":
v.StepOut()
err = v.StepOut()
case "over":
v.StepOver()
err = v.StepOver()
}
if err != nil {
c.Err(err)
}
handleIP(c)
changePrompt(c, v)
}