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
This commit is contained in:
mpl
2020-11-10 00:40:04 +01:00
committed by GitHub
parent d0a34d467b
commit ed626f3fb9
3 changed files with 48 additions and 3 deletions

17
_test/convert1.go Normal file
View File

@@ -0,0 +1,17 @@
package main
import "strconv"
type atoidef func(s string) (int, error)
func main() {
stdatoi := atoidef(strconv.Atoi)
n, err := stdatoi("7")
if err != nil {
panic(err)
}
println(n)
}
// Output:
// 7

19
_test/convert2.go Normal file
View File

@@ -0,0 +1,19 @@
package main
import "bufio"
func fakeSplitFunc(data []byte, atEOF bool) (advance int, token []byte, err error) {
return 7, nil, nil
}
func main() {
splitfunc := bufio.SplitFunc(fakeSplitFunc)
n, _, err := splitfunc(nil, true)
if err != nil {
panic(err)
}
println(n)
}
// Output:
// 7

View File

@@ -405,10 +405,15 @@ func convert(n *node) {
return
}
doConvert := true
var value func(*frame) reflect.Value
if c.typ.cat == funcT {
switch {
case c.typ.cat == funcT:
value = genFunctionWrapper(c)
} else {
case n.child[0].typ.cat == funcT && c.typ.cat == valueT:
doConvert = false
value = genValueNode(c)
default:
value = genValue(c)
}
@@ -429,7 +434,11 @@ func convert(n *node) {
}
n.exec = func(f *frame) bltn {
dest(f).Set(value(f).Convert(typ))
if doConvert {
dest(f).Set(value(f).Convert(typ))
} else {
dest(f).Set(value(f))
}
return next
}
}