Skip to content

Commit b062494

Browse files
committed
Fix: correctly check error type of func return types
1 parent 629453d commit b062494

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

expr_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -1646,6 +1646,19 @@ func TestFastCall(t *testing.T) {
16461646
assert.Equal(t, float64(8), out)
16471647
}
16481648

1649+
func TestRun_custom_func_returns_an_error_as_second_arg(t *testing.T) {
1650+
env := map[string]interface{}{
1651+
"semver": func(value string, cmp string) (bool, error) { return true, nil },
1652+
}
1653+
1654+
p, err := expr.Compile(`semver("1.2.3", "= 1.2.3")`, expr.Env(env))
1655+
assert.NoError(t, err)
1656+
1657+
out, err := expr.Run(p, env)
1658+
assert.NoError(t, err)
1659+
assert.Equal(t, true, out)
1660+
}
1661+
16491662
// Mock types
16501663

16511664
type mockEnv struct {

vm/vm.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ import (
1212
"github.com/antonmedv/expr/vm/runtime"
1313
)
1414

15-
var (
16-
MemoryBudget int = 1e6
17-
)
15+
var MemoryBudget int = 1e6
16+
var errorType = reflect.TypeOf((*error)(nil)).Elem()
1817

1918
func Run(program *Program, env interface{}) (interface{}, error) {
2019
if program == nil {
@@ -302,7 +301,7 @@ func (vm *VM) Run(program *Program, env interface{}) (out interface{}, err error
302301
}
303302
}
304303
out := fn.Call(in)
305-
if len(out) == 2 && !runtime.IsNil(out[1]) {
304+
if len(out) == 2 && out[1].Type() == errorType && !out[1].IsNil() {
306305
panic(out[1].Interface().(error))
307306
}
308307
vm.push(out[0].Interface())

0 commit comments

Comments
 (0)