Skip to content

Commit ed626f3

Browse files
authored
interp: support conversion of runtime func into interp func
Conversion of interp func into runtime func already worked, but added a test anyway to watch out for regressions. Fixes #941
1 parent d0a34d4 commit ed626f3

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

_test/convert1.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import "strconv"
4+
5+
type atoidef func(s string) (int, error)
6+
7+
func main() {
8+
stdatoi := atoidef(strconv.Atoi)
9+
n, err := stdatoi("7")
10+
if err != nil {
11+
panic(err)
12+
}
13+
println(n)
14+
}
15+
16+
// Output:
17+
// 7

_test/convert2.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import "bufio"
4+
5+
func fakeSplitFunc(data []byte, atEOF bool) (advance int, token []byte, err error) {
6+
return 7, nil, nil
7+
}
8+
9+
func main() {
10+
splitfunc := bufio.SplitFunc(fakeSplitFunc)
11+
n, _, err := splitfunc(nil, true)
12+
if err != nil {
13+
panic(err)
14+
}
15+
println(n)
16+
}
17+
18+
// Output:
19+
// 7

interp/run.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,10 +405,15 @@ func convert(n *node) {
405405
return
406406
}
407407

408+
doConvert := true
408409
var value func(*frame) reflect.Value
409-
if c.typ.cat == funcT {
410+
switch {
411+
case c.typ.cat == funcT:
410412
value = genFunctionWrapper(c)
411-
} else {
413+
case n.child[0].typ.cat == funcT && c.typ.cat == valueT:
414+
doConvert = false
415+
value = genValueNode(c)
416+
default:
412417
value = genValue(c)
413418
}
414419

@@ -429,7 +434,11 @@ func convert(n *node) {
429434
}
430435

431436
n.exec = func(f *frame) bltn {
432-
dest(f).Set(value(f).Convert(typ))
437+
if doConvert {
438+
dest(f).Set(value(f).Convert(typ))
439+
} else {
440+
dest(f).Set(value(f))
441+
}
433442
return next
434443
}
435444
}

0 commit comments

Comments
 (0)