Skip to content

Commit

Permalink
fix argument evaluation when calling variadic functions
Browse files Browse the repository at this point in the history
fixes #171
  • Loading branch information
sauerbraten committed Aug 15, 2020
1 parent f67efbc commit e642e94
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
9 changes: 6 additions & 3 deletions eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -1278,8 +1278,7 @@ func (st *Runtime) evaluateArgs(fnType reflect.Type, args CallArgs, pipedArg *re
}
}

slot, i := 0, 0
var term reflect.Value
slot := 0 // index in argument values (evaluated expressions combined with piped argument if applicable)

if !args.HasPipeSlot && pipedArg != nil {
in := fnType.In(slot)
Expand All @@ -1290,8 +1289,11 @@ func (st *Runtime) evaluateArgs(fnType reflect.Type, args CallArgs, pipedArg *re
slot++
}

for i < len(args.Exprs) {
i := 0 // index in parsed argument expression list

for slot < numArgsRequired {
in := fnType.In(slot)
var term reflect.Value
if args.Exprs[i].Type() == NodeUnderscore {
term = *pipedArg
} else {
Expand All @@ -1308,6 +1310,7 @@ func (st *Runtime) evaluateArgs(fnType reflect.Type, args CallArgs, pipedArg *re
if isVariadic {
in := fnType.In(numArgsRequired).Elem()
for i < len(args.Exprs) {
var term reflect.Value
if args.Exprs[i].Type() == NodeUnderscore {
term = *pipedArg
} else {
Expand Down
5 changes: 5 additions & 0 deletions eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,13 @@ func TestEvalActionNode(t *testing.T) {
"José Santos", "[email protected]",
})

data.Set("print", fmt.Sprint)
data.Set("printf", fmt.Sprintf)

RunJetTest(t, nil, nil, "actionNode", `hello {{"world"}}`, `hello world`)
RunJetTest(t, data, nil, "actionNode_func", `hello {{lower: "WORLD"}}`, `hello world`)
RunJetTest(t, data, nil, "actionNode_func_variadic", `{{ print("hello world") }}`, `hello world`)
RunJetTest(t, data, nil, "actionNode_func_variadic2", `{{ printf("hello %s", "world") }}`, `hello world`)
RunJetTest(t, data, nil, "actionNode_funcPipe", `hello {{lower: "WORLD" |upper}}`, `hello WORLD`)
RunJetTest(t, data, nil, "actionNode_funcPipe_parens", `{{ "foo" | repeat(2) }}`, `foofoo`)
RunJetTest(t, data, nil, "actionNode_funcPipe_parens2", `hello {{lower ( "WORLD" ) |upper}}`, `hello WORLD`)
Expand Down

0 comments on commit e642e94

Please sign in to comment.