Skip to content

Commit dcccf9b

Browse files
committed
Catch panics from vm
1 parent 56baf07 commit dcccf9b

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

‎main.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ func main(){
6262
}
6363
code:=obj.(*py.Code)
6464
v:=vm.NewVm()
65-
v.Run(code)
65+
err=v.Run(code)
66+
iferr!=nil{
67+
log.Fatal(err)
68+
}
6669

6770
}

‎vm/eval.go‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package vm
33

44
import (
5+
"errors"
56
"fmt"
67
"github.com/ncw/gpython/py"
78
)
@@ -777,7 +778,21 @@ func do_CALL_FUNCTION_VAR_KW(vm *Vm, argc int32){
777778
}
778779

779780
// Run the virtual machine on the code object
780-
func (vm*Vm) Run(co*py.Code){
781+
//
782+
// FIXME figure out how we are going to signal exceptions!
783+
func (vm*Vm) Run(co*py.Code) (errerror){
784+
deferfunc(){
785+
ifr:=recover(); r!=nil{
786+
switchx:=r.(type){
787+
caseerror:
788+
err=x
789+
casestring:
790+
err=errors.New(x)
791+
default:
792+
err=errors.New(fmt.Sprintf("Unknown error '%s'", x))
793+
}
794+
}
795+
}()
781796
vm.co=co
782797
ip:=0
783798
varopcodebyte
@@ -801,5 +816,5 @@ func (vm *Vm) Run(co *py.Code){
801816
vm.extended=false
802817
jumpTable[opcode](vm, arg)
803818
}
804-
// Return something?
819+
returnnil
805820
}

0 commit comments

Comments
(0)