Skip to content

Commit

Permalink
fix: assign binary func to func type var
Browse files Browse the repository at this point in the history
  • Loading branch information
mvertes authored and traefiker committed Oct 20, 2019
1 parent ac504a2 commit 1568687
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
13 changes: 13 additions & 0 deletions _test/time10.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import "time"

var UnixTime func(int64, int64) time.Time

func main() {
UnixTime = time.Unix
println(UnixTime(1e9, 0).In(time.UTC).Minute())
}

// Output:
// 46
2 changes: 1 addition & 1 deletion cmd/goexports/goexports.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:generate go build

/*
Goexports generates wrappers of package exported symbols
Goexports generates wrappers of package exported symbols.
Output files are written in the current directory, and prefixed with the go version.
Expand Down
4 changes: 2 additions & 2 deletions interp/doc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Package interp provides a complete Go interpreter
Package interp provides a complete Go interpreter.
For the Go language itself, refer to the official Go specification
https://golang.org/ref/spec.
Expand Down Expand Up @@ -32,4 +32,4 @@ And include files containing
*/
package interp

// BUG(marc): Type checking is not implemented yet
// BUG(marc): Type checking is not implemented yet.
25 changes: 25 additions & 0 deletions interp/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ func assign(n *node) {
svalue[i] = genInterfaceWrapper(src, dest.typ.rtype)
case dest.typ.cat == valueT && src.typ.cat == funcT:
svalue[i] = genFunctionWrapper(src)
case dest.typ.cat == funcT && src.typ.cat == valueT:
svalue[i] = genValueNode(src)
case src.kind == basicLit && src.val == nil:
t := dest.typ.TypeOf()
svalue[i] = func(*frame) reflect.Value { return reflect.New(t).Elem() }
Expand Down Expand Up @@ -649,6 +651,29 @@ func call(n *node) {

n.exec = func(f *frame) bltn {
def := value(f).Interface().(*node)

// Call bin func if defined
if def.rval.IsValid() {
in := make([]reflect.Value, len(values))
for i, v := range values {
in[i] = v(f)
}
if goroutine {
go def.rval.Call(in)
return tnext
}
out := def.rval.Call(in)
for i, v := range rvalues {
if v != nil {
v(f).Set(out[i])
}
}
if fnext != nil && !out[0].Bool() {
return fnext
}
return tnext
}

anc := f
// Get closure frame context (if any)
if def.frame != nil {
Expand Down
8 changes: 8 additions & 0 deletions interp/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ func genValueInterfaceValue(n *node) func(*frame) reflect.Value {
}
}

func genValueNode(n *node) func(*frame) reflect.Value {
value := genValue(n)

return func(f *frame) reflect.Value {
return reflect.ValueOf(&node{rval: value(f)})
}
}

func vInt(v reflect.Value) (i int64) {
switch v.Type().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
Expand Down

0 comments on commit 1568687

Please sign in to comment.