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:
17
_test/convert1.go
Normal file
17
_test/convert1.go
Normal 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
19
_test/convert2.go
Normal 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
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user