Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: catch panic in try catch #455

Merged
merged 1 commit into from
Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -928,3 +928,18 @@ func Test_issue386(t *testing.T) {
})
}
}

func Test_issue382(t *testing.T) {
vm := New()
vm.Set("panicFunc", func(call FunctionCall) Value {
panic("test")
})
_, err := vm.Run(`
try {
panicFunc()
} catch (err) {
console.log("panic triggered:", err)
}
`)
require.NoError(t, err)
}
8 changes: 4 additions & 4 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (self *_runtime) tryCatchEvaluate(inner func() Value) (tryValue Value, exce
// throw = Something was thrown
// throwValue = The value of what was thrown
// other = Something that changes flow (return, break, continue) that is not a throw
// Otherwise, some sort of unknown panic happened, we'll just propagate it
// Otherwise, some sort of unknown panic happened, we'll just propagate it.
defer func() {
if caught := recover(); caught != nil {
if exception, ok := caught.(*_exception); ok {
Expand All @@ -135,13 +135,13 @@ func (self *_runtime) tryCatchEvaluate(inner func() Value) (tryValue Value, exce
exception = true
tryValue = caught
default:
panic(caught)
exception = true
tryValue = toValue(caught)
}
}
}()

tryValue = inner()
return
return inner(), false
}

// toObject
Expand Down