Skip to content

Commit

Permalink
Fix CallFastTyped detection
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Jan 20, 2023
1 parent 5366e57 commit 9009d4d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
6 changes: 3 additions & 3 deletions checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,11 +583,11 @@ func (v *visitor) checkFunc(fn reflect.Type, method bool, node *ast.CallNode, na
continue funcTypes
}
}
if typed.NumIn() != len(arguments) {
if typed.NumIn() != fn.NumIn() {
continue
}
for j, arg := range arguments {
if typed.In(j) != arg.Type() {
for j := 0; j < typed.NumIn(); j++ {
if typed.In(j) != fn.In(j) {
continue funcTypes
}
}
Expand Down
19 changes: 19 additions & 0 deletions checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,3 +703,22 @@ func TestCheck_TypeWeights(t *testing.T) {
}
}
}

func TestCheck_CallFastTyped(t *testing.T) {
env := map[string]interface{}{
"fn": func([]interface{}, string) string {
return "foo"
},
}

tree, err := parser.Parse("fn([1, 2], 'bar')")
require.NoError(t, err)

config := conf.New(env)

_, err = checker.Check(tree, config)
require.NoError(t, err)

require.False(t, tree.Node.(*ast.CallNode).Fast)
require.Equal(t, 22, tree.Node.(*ast.CallNode).Typed)
}
17 changes: 17 additions & 0 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1630,7 +1630,24 @@ func TestCompile_allow_to_use_interface_to_get_an_element_from_map(t *testing.T)
})
}

func TestFastCall(t *testing.T) {
env := map[string]interface{}{
"func": func(in interface{}) float64 {
return 8
},
}
code := `func("8")`

program, err := expr.Compile(code, expr.Env(env))
assert.NoError(t, err)

out, err := expr.Run(program, env)
assert.NoError(t, err)
assert.Equal(t, float64(8), out)
}

// Mock types

type mockEnv struct {
Any interface{}
Int, One, Two, Three int
Expand Down

0 comments on commit 9009d4d

Please sign in to comment.