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

+4-1
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+
if err != nil {
67+
log.Fatal(err)
68+
}
6669

6770
}

vm/eval.go

+17-2
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) (err error) {
784+
defer func() {
785+
if r := recover(); r != nil {
786+
switch x := r.(type) {
787+
case error:
788+
err = x
789+
case string:
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
var opcode byte
@@ -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+
return nil
805820
}

0 commit comments

Comments
 (0)