Skip to content

Commit

Permalink
1148: invoke user recover with implicit panics
Browse files Browse the repository at this point in the history
  • Loading branch information
petar-dambovaliev committed Nov 4, 2024
1 parent af05780 commit 77b4ed3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
33 changes: 33 additions & 0 deletions gnovm/pkg/gnolang/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,15 @@ func (m *Machine) RunFunc(fn Name) {
func (m *Machine) RunMain() {
defer func() {
if r := recover(); r != nil {
// we panicked because there was an invalid operation
// if the user had a recover, do it
hasRecover := m.setupRecoverPanic()

if hasRecover {
m.Run()
return
}

switch r := r.(type) {
case UnhandledPanicError:
fmt.Printf("Machine.RunMain() panic: %s\nStacktrace: %s\n",
Expand Down Expand Up @@ -2179,6 +2188,30 @@ func (m *Machine) CheckEmpty() error {
}
}

func (m *Machine) setupRuntimePanic(ex TypedValue) bool {
frm := m.LastCallFrame(1)

if frm == nil {
return false
}

m.Exceptions = append(
m.Exceptions,
Exception{
Value: ex,
Frame: frm,
Stacktrace: m.Stacktrace(),
},
)

m.PanicScope++
m.PopUntilLastCallFrame()
m.PushOp(OpPanic2)
m.PushOp(OpReturnCallDefers)

return true
}

func (m *Machine) Panic(ex TypedValue) {
m.Exceptions = append(
m.Exceptions,
Expand Down
7 changes: 7 additions & 0 deletions gnovm/pkg/gnolang/op_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,13 @@ func (m *Machine) doOpDefer() {
}
}

func (m *Machine) setupRecoverPanic() bool {
// Pop exception
ex := m.PopValue().Copy(m.Alloc)
// Panic
return m.setupRuntimePanic(ex)
}

func (m *Machine) doOpPanic1() {
// Pop exception
var ex TypedValue = m.PopValue().Copy(m.Alloc)
Expand Down

0 comments on commit 77b4ed3

Please sign in to comment.