Skip to content

Commit

Permalink
interp: support conversion of runtime func into interp func
Browse files Browse the repository at this point in the history
Conversion of interp func into runtime func already worked, but added a
test anyway to watch out for regressions.

Fixes #941
  • Loading branch information
mpl authored Nov 9, 2020
1 parent d0a34d4 commit ed626f3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
17 changes: 17 additions & 0 deletions _test/convert1.go
Original file line number Diff line number Diff line change
@@ -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 changes: 19 additions & 0 deletions _test/convert2.go
Original file line number Diff line number Diff line change
@@ -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
15 changes: 12 additions & 3 deletions interp/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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
}
}
Expand Down

0 comments on commit ed626f3

Please sign in to comment.