Compare commits
54 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
24b5375636 | ||
|
|
a83f492309 | ||
|
|
02c30482cc | ||
|
|
9e1da978b0 | ||
|
|
662838fd80 | ||
|
|
92d65c22f0 | ||
|
|
2db4579b6f | ||
|
|
101633c380 | ||
|
|
1e0f6ece6e | ||
|
|
662d2a6afe | ||
|
|
b25ee3f809 | ||
|
|
81e1e5f206 | ||
|
|
81d8339132 | ||
|
|
d494f9e420 | ||
|
|
6da1107c39 | ||
|
|
38a7331bf9 | ||
|
|
13783889cb | ||
|
|
ed626f3fb9 | ||
|
|
d0a34d467b | ||
|
|
83676577ac | ||
|
|
f0fc907269 | ||
|
|
61f4704925 | ||
|
|
b1ccfbf47f | ||
|
|
0ed4b362dc | ||
|
|
98807387a4 | ||
|
|
c817823ba1 | ||
|
|
3cb8bca81a | ||
|
|
a38d19288f | ||
|
|
7f8ffa6719 | ||
|
|
513f5e37aa | ||
|
|
9520a92241 | ||
|
|
d47821bfaa | ||
|
|
d7ede8ed5c | ||
|
|
22c63b225c | ||
|
|
c0eaab0891 | ||
|
|
c74d050c5a | ||
|
|
804664c631 | ||
|
|
f6d8261a8a | ||
|
|
68c02ce054 | ||
|
|
4b3e9ee231 | ||
|
|
8916618a81 | ||
|
|
57b49f40d6 | ||
|
|
190dade469 | ||
|
|
6b652ea485 | ||
|
|
473bc63588 | ||
|
|
e32da38ad0 | ||
|
|
b2b519c2fd | ||
|
|
9491e58920 | ||
|
|
f362237ac5 | ||
|
|
a83ec1f925 | ||
|
|
155ca4e6ad | ||
|
|
ca196a5768 | ||
|
|
b78d55c66b | ||
|
|
16f5586a11 |
@@ -45,3 +45,19 @@ archives:
|
||||
format: zip
|
||||
files:
|
||||
- LICENSE
|
||||
|
||||
brews:
|
||||
- github:
|
||||
owner: traefik
|
||||
name: homebrew-tap
|
||||
commit_author:
|
||||
name: traefiker
|
||||
email: 30906710+traefiker@users.noreply.github.com
|
||||
folder: Formula
|
||||
homepage: https://github.com/traefik/yaegi
|
||||
description: |
|
||||
Yaegi is Another Elegant Go Interpreter.
|
||||
It powers executable Go scripts and plugins, in embedded interpreters
|
||||
or interactive shells, on top of the Go runtime.
|
||||
test: |
|
||||
system "#{bin}/yaegi version"
|
||||
|
||||
9
Makefile
9
Makefile
@@ -16,12 +16,15 @@ internal/cmd/extract/extract:
|
||||
generate: gen_all_syscall
|
||||
go generate
|
||||
|
||||
install:
|
||||
GOFLAGS=-ldflags=-X=main.version=$$(git describe --tags) go install ./...
|
||||
|
||||
tests:
|
||||
GO111MODULE=off go test -v ./...
|
||||
GO111MODULE=off go test -race ./interp
|
||||
go test -v ./...
|
||||
go test -race ./interp
|
||||
|
||||
# https://github.com/goreleaser/godownloader
|
||||
install.sh: .goreleaser.yml
|
||||
godownloader --repo=traefik/yaegi -o install.sh .goreleaser.yml
|
||||
|
||||
.PHONY: check gen_all_syscall gen_tests generate_downloader internal/cmd/extract/extract
|
||||
.PHONY: check gen_all_syscall gen_tests generate_downloader internal/cmd/extract/extract install
|
||||
|
||||
14
README.md
14
README.md
@@ -128,10 +128,20 @@ Hello World
|
||||
>
|
||||
```
|
||||
|
||||
Or interpret Go files:
|
||||
Note that in interactive mode, all stdlib package are pre-imported,
|
||||
you can use them directly:
|
||||
|
||||
```console
|
||||
$ yaegi cmd/yaegi/yaegi.go
|
||||
$ yaegi
|
||||
> reflect.TypeOf(time.Date)
|
||||
: func(int, time.Month, int, int, int, int, int, *time.Location) time.Time
|
||||
>
|
||||
```
|
||||
|
||||
Or interpret Go packages, directories or files, including itself:
|
||||
|
||||
```console
|
||||
$ yaegi -syscall -unsafe -unrestricted github.com/traefik/yaegi/cmd/yaegi
|
||||
>
|
||||
```
|
||||
|
||||
|
||||
17
_test/a43.go
Normal file
17
_test/a43.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type T [l1 + l2]int
|
||||
|
||||
const (
|
||||
l1 = 2
|
||||
l2 = 3
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(T{})
|
||||
}
|
||||
|
||||
// Output:
|
||||
// [0 0 0 0 0]
|
||||
12
_test/a44.go
Normal file
12
_test/a44.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
var a = [max]int{}
|
||||
|
||||
const max = 32
|
||||
|
||||
func main() {
|
||||
println(len(a))
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 32
|
||||
22
_test/addr1.go
Normal file
22
_test/addr1.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type T struct {
|
||||
A int
|
||||
B int
|
||||
}
|
||||
|
||||
func main() {
|
||||
a := &[]T{
|
||||
{1, 2},
|
||||
{3, 4},
|
||||
}
|
||||
fmt.Println("a:", a)
|
||||
x := &(*a)[1:][0]
|
||||
fmt.Println("x:", x)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// a: &[{1 2} {3 4}]
|
||||
// x: &{3 4}
|
||||
29
_test/addr2.go
Normal file
29
_test/addr2.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Email struct {
|
||||
Where string `xml:"where,attr"`
|
||||
Addr string
|
||||
}
|
||||
|
||||
func f(s string, r interface{}) error {
|
||||
return xml.Unmarshal([]byte(s), &r)
|
||||
}
|
||||
|
||||
func main() {
|
||||
data := `
|
||||
<Email where='work'>
|
||||
<Addr>bob@work.com</Addr>
|
||||
</Email>
|
||||
`
|
||||
v := Email{}
|
||||
err := f(data, &v)
|
||||
fmt.Println(err, v)
|
||||
}
|
||||
|
||||
// Ouput:
|
||||
// <nil> {work bob@work.com}
|
||||
15
_test/append1.go
Normal file
15
_test/append1.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := bufio.NewScanner(bytes.NewReader([]byte("Hello\nTest\nLine3")))
|
||||
s.Scan()
|
||||
println(string(append(s.Bytes(), []byte(" World")...)))
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Hello World
|
||||
15
_test/append2.go
Normal file
15
_test/append2.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := bufio.NewScanner(bytes.NewReader([]byte("Hello\nTest\nLine3")))
|
||||
s.Scan()
|
||||
println(string(append(s.Bytes(), " World"...)))
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Hello World
|
||||
125
_test/assert0.go
Normal file
125
_test/assert0.go
Normal file
@@ -0,0 +1,125 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
||||
type MyWriter interface {
|
||||
Write(p []byte) (i int, err error)
|
||||
}
|
||||
|
||||
type TestStruct struct{}
|
||||
|
||||
func (t TestStruct) Write(p []byte) (n int, err error) {
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
func usesWriter(w MyWriter) {
|
||||
n, _ := w.Write([]byte("hello world"))
|
||||
fmt.Println(n)
|
||||
}
|
||||
|
||||
type MyStringer interface {
|
||||
String() string
|
||||
}
|
||||
|
||||
func usesStringer(s MyStringer) {
|
||||
fmt.Println(s.String())
|
||||
}
|
||||
|
||||
func main() {
|
||||
aType := reflect.TypeOf((*MyWriter)(nil)).Elem()
|
||||
|
||||
var t interface{}
|
||||
t = TestStruct{}
|
||||
var tw MyWriter
|
||||
var ok bool
|
||||
tw, ok = t.(MyWriter)
|
||||
if !ok {
|
||||
fmt.Println("TestStruct does not implement MyWriter")
|
||||
} else {
|
||||
fmt.Println("TestStruct implements MyWriter")
|
||||
usesWriter(tw)
|
||||
}
|
||||
n, _ := t.(MyWriter).Write([]byte("hello world"))
|
||||
fmt.Println(n)
|
||||
bType := reflect.TypeOf(TestStruct{})
|
||||
fmt.Println(bType.Implements(aType))
|
||||
|
||||
// not redundant with the above, because it goes through a slightly different code path.
|
||||
if _, ok := t.(MyWriter); !ok {
|
||||
fmt.Println("TestStruct does not implement MyWriter")
|
||||
return
|
||||
} else {
|
||||
fmt.Println("TestStruct implements MyWriter")
|
||||
}
|
||||
|
||||
t = 42
|
||||
foo, ok := t.(MyWriter)
|
||||
if !ok {
|
||||
fmt.Println("42 does not implement MyWriter")
|
||||
} else {
|
||||
fmt.Println("42 implements MyWriter")
|
||||
}
|
||||
_ = foo
|
||||
|
||||
if _, ok := t.(MyWriter); !ok {
|
||||
fmt.Println("42 does not implement MyWriter")
|
||||
} else {
|
||||
fmt.Println("42 implements MyWriter")
|
||||
}
|
||||
|
||||
var tt interface{}
|
||||
tt = time.Nanosecond
|
||||
var myD MyStringer
|
||||
myD, ok = tt.(MyStringer)
|
||||
if !ok {
|
||||
fmt.Println("time.Nanosecond does not implement MyStringer")
|
||||
} else {
|
||||
fmt.Println("time.Nanosecond implements MyStringer")
|
||||
usesStringer(myD)
|
||||
}
|
||||
fmt.Println(tt.(MyStringer).String())
|
||||
cType := reflect.TypeOf((*MyStringer)(nil)).Elem()
|
||||
dType := reflect.TypeOf(time.Nanosecond)
|
||||
fmt.Println(dType.Implements(cType))
|
||||
|
||||
if _, ok := tt.(MyStringer); !ok {
|
||||
fmt.Println("time.Nanosecond does not implement MyStringer")
|
||||
} else {
|
||||
fmt.Println("time.Nanosecond implements MyStringer")
|
||||
}
|
||||
|
||||
tt = 42
|
||||
bar, ok := tt.(MyStringer)
|
||||
if !ok {
|
||||
fmt.Println("42 does not implement MyStringer")
|
||||
} else {
|
||||
fmt.Println("42 implements MyStringer")
|
||||
}
|
||||
_ = bar
|
||||
|
||||
if _, ok := tt.(MyStringer); !ok {
|
||||
fmt.Println("42 does not implement MyStringer")
|
||||
} else {
|
||||
fmt.Println("42 implements MyStringer")
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// TestStruct implements MyWriter
|
||||
// 11
|
||||
// 11
|
||||
// true
|
||||
// TestStruct implements MyWriter
|
||||
// 42 does not implement MyWriter
|
||||
// 42 does not implement MyWriter
|
||||
// time.Nanosecond implements MyStringer
|
||||
// 1ns
|
||||
// 1ns
|
||||
// true
|
||||
// time.Nanosecond implements MyStringer
|
||||
// 42 does not implement MyStringer
|
||||
// 42 does not implement MyStringer
|
||||
85
_test/assert1.go
Normal file
85
_test/assert1.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
||||
type TestStruct struct{}
|
||||
|
||||
func (t TestStruct) String() string {
|
||||
return "hello world"
|
||||
}
|
||||
|
||||
func main() {
|
||||
aType := reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
|
||||
|
||||
var t interface{}
|
||||
t = time.Nanosecond
|
||||
s, ok := t.(fmt.Stringer)
|
||||
if !ok {
|
||||
fmt.Println("time.Nanosecond does not implement fmt.Stringer")
|
||||
return
|
||||
}
|
||||
fmt.Println(s.String())
|
||||
fmt.Println(t.(fmt.Stringer).String())
|
||||
bType := reflect.TypeOf(time.Nanosecond)
|
||||
fmt.Println(bType.Implements(aType))
|
||||
|
||||
// not redundant with the above, because it goes through a slightly different code path.
|
||||
if _, ok := t.(fmt.Stringer); !ok {
|
||||
fmt.Println("time.Nanosecond does not implement fmt.Stringer")
|
||||
return
|
||||
} else {
|
||||
fmt.Println("time.Nanosecond implements fmt.Stringer")
|
||||
}
|
||||
|
||||
t = 42
|
||||
foo, ok := t.(fmt.Stringer)
|
||||
if !ok {
|
||||
fmt.Println("42 does not implement fmt.Stringer")
|
||||
} else {
|
||||
fmt.Println("42 implements fmt.Stringer")
|
||||
return
|
||||
}
|
||||
_ = foo
|
||||
|
||||
if _, ok := t.(fmt.Stringer); !ok {
|
||||
fmt.Println("42 does not implement fmt.Stringer")
|
||||
} else {
|
||||
fmt.Println("42 implements fmt.Stringer")
|
||||
return
|
||||
}
|
||||
|
||||
var tt interface{}
|
||||
tt = TestStruct{}
|
||||
ss, ok := tt.(fmt.Stringer)
|
||||
if !ok {
|
||||
fmt.Println("TestStuct does not implement fmt.Stringer")
|
||||
return
|
||||
}
|
||||
fmt.Println(ss.String())
|
||||
fmt.Println(tt.(fmt.Stringer).String())
|
||||
// TODO(mpl): uncomment when fixed
|
||||
// cType := reflect.TypeOf(TestStruct{})
|
||||
// fmt.Println(cType.Implements(aType))
|
||||
|
||||
if _, ok := tt.(fmt.Stringer); !ok {
|
||||
fmt.Println("TestStuct does not implement fmt.Stringer")
|
||||
return
|
||||
} else {
|
||||
fmt.Println("TestStuct implements fmt.Stringer")
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 1ns
|
||||
// 1ns
|
||||
// true
|
||||
// time.Nanosecond implements fmt.Stringer
|
||||
// 42 does not implement fmt.Stringer
|
||||
// 42 does not implement fmt.Stringer
|
||||
// hello world
|
||||
// hello world
|
||||
// TestStuct implements fmt.Stringer
|
||||
15
_test/assign16.go
Normal file
15
_test/assign16.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
type H struct {
|
||||
bits uint
|
||||
}
|
||||
|
||||
func main() {
|
||||
h := &H{8}
|
||||
var x uint = (1 << h.bits) >> 6
|
||||
|
||||
println(x)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 4
|
||||
17
_test/binstruct_ptr_map0.go
Normal file
17
_test/binstruct_ptr_map0.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
)
|
||||
|
||||
func main() {
|
||||
v := map[string]*image.Point{
|
||||
"foo": {X: 3, Y: 2},
|
||||
"bar": {X: 4, Y: 5},
|
||||
}
|
||||
fmt.Println(v["foo"], v["bar"])
|
||||
}
|
||||
|
||||
// Output:
|
||||
// (3,2) (4,5)
|
||||
17
_test/binstruct_ptr_slice0.go
Normal file
17
_test/binstruct_ptr_slice0.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
)
|
||||
|
||||
func main() {
|
||||
v := []*image.Point{
|
||||
{X: 3, Y: 2},
|
||||
{X: 4, Y: 5},
|
||||
}
|
||||
fmt.Println(v)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// [(3,2) (4,5)]
|
||||
16
_test/binstruct_slice0.go
Normal file
16
_test/binstruct_slice0.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
)
|
||||
|
||||
func main() {
|
||||
v := []image.Point{
|
||||
{X: 3, Y: 2},
|
||||
}
|
||||
fmt.Println(v)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// [(3,2)]
|
||||
15
_test/composite14.go
Normal file
15
_test/composite14.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type T struct {
|
||||
b []byte
|
||||
}
|
||||
|
||||
func main() {
|
||||
t := T{nil}
|
||||
fmt.Println(t)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// {[]}
|
||||
50
_test/composite15.go
Normal file
50
_test/composite15.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func interfaceAsInts() {
|
||||
var a interface{}
|
||||
b := 2
|
||||
c := 3
|
||||
a = []int{b, c}
|
||||
|
||||
d, ok := a.([]int)
|
||||
if !ok {
|
||||
println("nope")
|
||||
return
|
||||
}
|
||||
|
||||
for _, v := range d {
|
||||
fmt.Println(v)
|
||||
}
|
||||
}
|
||||
|
||||
func interfaceAsInterfaces() {
|
||||
var a, b, c interface{}
|
||||
b = 2
|
||||
c = 3
|
||||
a = []interface{}{b, c}
|
||||
|
||||
d, ok := a.([]interface{})
|
||||
if !ok {
|
||||
println("nope")
|
||||
return
|
||||
}
|
||||
|
||||
for _, v := range d {
|
||||
fmt.Println(v)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
interfaceAsInts()
|
||||
interfaceAsInterfaces()
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 2
|
||||
// 3
|
||||
// 2
|
||||
// 3
|
||||
16
_test/composite16.go
Normal file
16
_test/composite16.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func main() {
|
||||
body := url.Values{
|
||||
"Action": {"none"},
|
||||
}
|
||||
fmt.Println(body)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// map[Action:[none]]
|
||||
14
_test/const17.go
Normal file
14
_test/const17.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
var t [7/3]int
|
||||
|
||||
func main() {
|
||||
t[0] = 3/2
|
||||
t[1] = 5/2
|
||||
fmt.Println(t)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// [1 2]
|
||||
11
_test/const18.go
Normal file
11
_test/const18.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
import "time"
|
||||
|
||||
func main() {
|
||||
a := int64(time.Second)
|
||||
println(a)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 1000000000
|
||||
17
_test/const19.go
Normal file
17
_test/const19.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func get10Hours() time.Duration {
|
||||
return 10 * time.Hour
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(get10Hours().String())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 10h0m0s
|
||||
12
_test/const20.go
Normal file
12
_test/const20.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
const maxLen = int64(int(^uint(0) >> 1))
|
||||
|
||||
func main() {
|
||||
fmt.Println(maxLen)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 9223372036854775807
|
||||
12
_test/const21.go
Normal file
12
_test/const21.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
const a = 64
|
||||
|
||||
var b uint = a * a / 2
|
||||
|
||||
func main() {
|
||||
println(b)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 2048
|
||||
21
_test/convert0.go
Normal file
21
_test/convert0.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
type T struct {
|
||||
v int
|
||||
}
|
||||
|
||||
type comparator func(T, T) bool
|
||||
|
||||
func sort(items []T, comp comparator) {
|
||||
println("in sort")
|
||||
}
|
||||
|
||||
func compT(t0, t1 T) bool { return t0.v < t1.v }
|
||||
|
||||
func main() {
|
||||
a := []T{}
|
||||
sort(a, comparator(compT))
|
||||
}
|
||||
|
||||
// Output:
|
||||
// in sort
|
||||
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
|
||||
16
_test/for15.go
Normal file
16
_test/for15.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
func f() int { println("in f"); return 1 }
|
||||
|
||||
func main() {
|
||||
for i := f(); ; {
|
||||
println("in loop")
|
||||
if i > 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// in f
|
||||
// in loop
|
||||
16
_test/for16.go
Normal file
16
_test/for16.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
max := 1
|
||||
for ; ; max-- {
|
||||
if max == 0 {
|
||||
break
|
||||
}
|
||||
println("in for")
|
||||
}
|
||||
println("bye")
|
||||
}
|
||||
|
||||
// Output:
|
||||
// in for
|
||||
// bye
|
||||
10
_test/fun23.go
Normal file
10
_test/fun23.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package main
|
||||
|
||||
func f(x int) { return x }
|
||||
|
||||
func main() {
|
||||
print("hello")
|
||||
}
|
||||
|
||||
// Error:
|
||||
// 3:17: too many arguments to return
|
||||
10
_test/fun24.go
Normal file
10
_test/fun24.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package main
|
||||
|
||||
func f(x int) (int, int) { return x, "foo" }
|
||||
|
||||
func main() {
|
||||
print("hello")
|
||||
}
|
||||
|
||||
// Error:
|
||||
// cannot use "foo" (type stringT) as type intT in return argument
|
||||
10
_test/fun25.go
Normal file
10
_test/fun25.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package main
|
||||
|
||||
func f(x string) (a int, b int) { return x, 5 }
|
||||
|
||||
func main() {
|
||||
print("hello")
|
||||
}
|
||||
|
||||
// Error:
|
||||
// cannot use x (type stringT) as type intT in return argument
|
||||
17
_test/fun26.go
Normal file
17
_test/fun26.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
type F func() (int, error)
|
||||
|
||||
func f1() (int, error) { return 3, nil }
|
||||
|
||||
func f2(a string, f F) {
|
||||
c, _ := f()
|
||||
println(a, c)
|
||||
}
|
||||
|
||||
func main() {
|
||||
f2("hello", F(f1))
|
||||
}
|
||||
|
||||
// Output:
|
||||
// hello 3
|
||||
12
_test/goto1.go
Normal file
12
_test/goto1.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
if true {
|
||||
goto here
|
||||
}
|
||||
here:
|
||||
println("ok")
|
||||
}
|
||||
|
||||
// Output:
|
||||
// ok
|
||||
32
_test/interface47.go
Normal file
32
_test/interface47.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
type Doer interface {
|
||||
Do() error
|
||||
}
|
||||
|
||||
type T struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func (t *T) Do() error { println("in do"); return nil }
|
||||
|
||||
func f() (Doer, error) { return &T{"truc"}, nil }
|
||||
|
||||
type Ev struct {
|
||||
doer func() (Doer, error)
|
||||
}
|
||||
|
||||
func (e *Ev) do() {
|
||||
d, _ := e.doer()
|
||||
d.Do()
|
||||
}
|
||||
|
||||
func main() {
|
||||
e := &Ev{f}
|
||||
println(e != nil)
|
||||
e.do()
|
||||
}
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// in do
|
||||
23
_test/issue-880.go
Normal file
23
_test/issue-880.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var buf1 = make([]byte, 1024)
|
||||
var buf2 []byte
|
||||
buf1 = []byte("Hallo\nTest\nLine3")
|
||||
|
||||
s := bufio.NewScanner(bytes.NewReader(buf1))
|
||||
for s.Scan() {
|
||||
buf2 = append(buf2, append(s.Bytes(), []byte("\n")...)...)
|
||||
}
|
||||
print(string(buf2))
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Hallo
|
||||
// Test
|
||||
// Line3
|
||||
15
_test/issue-993.go
Normal file
15
_test/issue-993.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
var m map[string]int64
|
||||
|
||||
func initVar() {
|
||||
m = make(map[string]int64)
|
||||
}
|
||||
|
||||
func main() {
|
||||
initVar()
|
||||
println(len(m))
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 0
|
||||
28
_test/nil3.go
Normal file
28
_test/nil3.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
type I interface {
|
||||
Hello()
|
||||
}
|
||||
|
||||
type T struct {
|
||||
h I
|
||||
}
|
||||
|
||||
func (t *T) Hello() { println("Hello") }
|
||||
|
||||
func main() {
|
||||
t := &T{}
|
||||
println(t.h != nil)
|
||||
println(t.h == nil)
|
||||
t.h = t
|
||||
println(t.h != nil)
|
||||
println(t.h == nil)
|
||||
t.h.Hello()
|
||||
}
|
||||
|
||||
// Output:
|
||||
// false
|
||||
// true
|
||||
// true
|
||||
// false
|
||||
// Hello
|
||||
@@ -1,18 +1,20 @@
|
||||
package main
|
||||
|
||||
import "time"
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
c1 := make(chan string)
|
||||
c2 := make(chan string)
|
||||
|
||||
go func() {
|
||||
time.Sleep(1e9)
|
||||
time.Sleep(1e7)
|
||||
c1 <- "one"
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(2e9)
|
||||
time.Sleep(2e7)
|
||||
c2 <- "two"
|
||||
}()
|
||||
|
||||
|
||||
44
_test/select14.go
Normal file
44
_test/select14.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
period = 100 * time.Millisecond
|
||||
precision = 5 * time.Millisecond
|
||||
)
|
||||
|
||||
func main() {
|
||||
counter := 0
|
||||
p := time.Now()
|
||||
ticker := time.NewTicker(period)
|
||||
ch := make(chan int)
|
||||
|
||||
go func() {
|
||||
for i := 0; i < 3; i++ {
|
||||
select {
|
||||
case t := <-ticker.C:
|
||||
counter = counter + 1
|
||||
ch <- counter
|
||||
if d := t.Sub(p) - period; d < -precision || d > precision {
|
||||
fmt.Println("wrong delay", d)
|
||||
}
|
||||
p = t
|
||||
}
|
||||
}
|
||||
ch <- 0
|
||||
}()
|
||||
for c := range ch {
|
||||
if c == 0 {
|
||||
break
|
||||
}
|
||||
println(c)
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 1
|
||||
// 2
|
||||
// 3
|
||||
23
_test/select15.go
Normal file
23
_test/select15.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
type T struct {
|
||||
c1 chan string
|
||||
c2 chan string
|
||||
}
|
||||
|
||||
func main() {
|
||||
t := &T{}
|
||||
t.c2 = make(chan string)
|
||||
|
||||
go func(c chan string) { c <- "done" }(t.c2)
|
||||
|
||||
select {
|
||||
case msg := <-t.c1:
|
||||
println("received from c1:", msg)
|
||||
case <-t.c2:
|
||||
}
|
||||
println("Bye")
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Bye
|
||||
11
_test/slice.go
Normal file
11
_test/slice.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
a := [2][2]int{{0, 1}, {2, 3}}
|
||||
fmt.Println(a[0][0:])
|
||||
}
|
||||
|
||||
// Output:
|
||||
// [0 1]
|
||||
20
_test/time14.go
Normal file
20
_test/time14.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
var t time.Time
|
||||
|
||||
func f() time.Time {
|
||||
time := t
|
||||
return time
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(f())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 0001-01-01 00:00:00 +0000 UTC
|
||||
15
_test/time15.go
Normal file
15
_test/time15.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import "time"
|
||||
|
||||
type TimeValue time.Time
|
||||
|
||||
func (v *TimeValue) decode() { println("in decode") }
|
||||
|
||||
func main() {
|
||||
var tv TimeValue
|
||||
tv.decode()
|
||||
}
|
||||
|
||||
// Output:
|
||||
// in decode
|
||||
15
_test/var15.go
Normal file
15
_test/var15.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
var a int = 2
|
||||
|
||||
func inca() {
|
||||
a = a + 1
|
||||
}
|
||||
|
||||
func main() {
|
||||
inca()
|
||||
println(a)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 3
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/traefik/yaegi/internal/extract"
|
||||
"github.com/traefik/yaegi/extract"
|
||||
)
|
||||
|
||||
func extractCmd(arg []string) error {
|
||||
|
||||
@@ -14,6 +14,7 @@ The commands are:
|
||||
help print usage information
|
||||
run execute a Go program from source
|
||||
test execute test functions in a Go package
|
||||
version print version
|
||||
|
||||
Use "yaegi help <command>" for more information about a command.
|
||||
|
||||
@@ -37,6 +38,9 @@ func help(arg []string) error {
|
||||
return run([]string{"-h"})
|
||||
case Test:
|
||||
return test([]string{"-h"})
|
||||
case Version:
|
||||
fmt.Println("Usage: yaegi version")
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("help: invalid yaegi command: %v", cmd)
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"go/build"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/traefik/yaegi/interp"
|
||||
@@ -17,19 +18,21 @@ import (
|
||||
|
||||
func run(arg []string) error {
|
||||
var interactive bool
|
||||
var useSyscall bool
|
||||
var useUnrestricted bool
|
||||
var useUnsafe bool
|
||||
var tags string
|
||||
var cmd string
|
||||
var err error
|
||||
|
||||
// The following flags are initialized from environment.
|
||||
useSyscall, _ := strconv.ParseBool(os.Getenv("YAEGI_SYSCALL"))
|
||||
useUnrestricted, _ := strconv.ParseBool(os.Getenv("YAEGI_UNRESTRICTED"))
|
||||
useUnsafe, _ := strconv.ParseBool(os.Getenv("YAEGI_UNSAFE"))
|
||||
|
||||
rflag := flag.NewFlagSet("run", flag.ContinueOnError)
|
||||
rflag.BoolVar(&interactive, "i", false, "start an interactive REPL")
|
||||
rflag.BoolVar(&useSyscall, "syscall", false, "include syscall symbols")
|
||||
rflag.BoolVar(&useUnrestricted, "unrestricted", false, "include unrestricted symbols")
|
||||
rflag.BoolVar(&useSyscall, "syscall", useSyscall, "include syscall symbols")
|
||||
rflag.BoolVar(&useUnrestricted, "unrestricted", useUnrestricted, "include unrestricted symbols")
|
||||
rflag.StringVar(&tags, "tags", "", "set a list of build tags")
|
||||
rflag.BoolVar(&useUnsafe, "unsafe", false, "include usafe symbols")
|
||||
rflag.BoolVar(&useUnsafe, "unsafe", useUnsafe, "include unsafe symbols")
|
||||
rflag.StringVar(&cmd, "e", "", "set the command to be executed (instead of script or/and shell)")
|
||||
rflag.Usage = func() {
|
||||
fmt.Println("Usage: yaegi run [options] [path] [args]")
|
||||
@@ -46,24 +49,33 @@ func run(arg []string) error {
|
||||
i.Use(interp.Symbols)
|
||||
if useSyscall {
|
||||
i.Use(syscall.Symbols)
|
||||
// Using a environment var allows a nested interpreter to import the syscall package.
|
||||
if err := os.Setenv("YAEGI_SYSCALL", "1"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if useUnsafe {
|
||||
i.Use(unsafe.Symbols)
|
||||
if err := os.Setenv("YAEGI_UNSAFE", "1"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if useUnrestricted {
|
||||
// Use of unrestricted symbols should always follow stdlib and syscall symbols, to update them.
|
||||
i.Use(unrestricted.Symbols)
|
||||
if err := os.Setenv("YAEGI_UNRESTRICTED", "1"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if cmd != "" {
|
||||
_, err = i.Eval(cmd)
|
||||
showError(err)
|
||||
}
|
||||
|
||||
if len(args) == 0 {
|
||||
if interactive || cmd == "" {
|
||||
_, err = i.REPL()
|
||||
showError(err)
|
||||
_, err = i.REPL()
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -78,7 +90,6 @@ func run(arg []string) error {
|
||||
} else {
|
||||
_, err = i.EvalPath(path)
|
||||
}
|
||||
showError(err)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -86,7 +97,6 @@ func run(arg []string) error {
|
||||
|
||||
if interactive {
|
||||
_, err = i.REPL()
|
||||
showError(err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"go/build"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -18,22 +21,24 @@ import (
|
||||
|
||||
func test(arg []string) (err error) {
|
||||
var (
|
||||
bench string
|
||||
benchmem bool
|
||||
benchtime string
|
||||
count string
|
||||
cpu string
|
||||
failfast bool
|
||||
run string
|
||||
short bool
|
||||
tags string
|
||||
useUnrestricted bool
|
||||
useUnsafe bool
|
||||
useSyscall bool
|
||||
timeout string
|
||||
verbose bool
|
||||
bench string
|
||||
benchmem bool
|
||||
benchtime string
|
||||
count string
|
||||
cpu string
|
||||
failfast bool
|
||||
run string
|
||||
short bool
|
||||
tags string
|
||||
timeout string
|
||||
verbose bool
|
||||
)
|
||||
|
||||
// The following flags are initialized from environment.
|
||||
useSyscall, _ := strconv.ParseBool(os.Getenv("YAEGI_SYSCALL"))
|
||||
useUnrestricted, _ := strconv.ParseBool(os.Getenv("YAEGI_UNRESTRICTED"))
|
||||
useUnsafe, _ := strconv.ParseBool(os.Getenv("YAEGI_UNSAFE"))
|
||||
|
||||
tflag := flag.NewFlagSet("test", flag.ContinueOnError)
|
||||
tflag.StringVar(&bench, "bench", "", "Run only those benchmarks matching a regular expression.")
|
||||
tflag.BoolVar(&benchmem, "benchmem", false, "Print memory allocation statistics for benchmarks.")
|
||||
@@ -45,9 +50,9 @@ func test(arg []string) (err error) {
|
||||
tflag.BoolVar(&short, "short", false, "Tell long-running tests to shorten their run time.")
|
||||
tflag.StringVar(&tags, "tags", "", "Set a list of build tags.")
|
||||
tflag.StringVar(&timeout, "timeout", "", "If a test binary runs longer than duration d, panic.")
|
||||
tflag.BoolVar(&useUnrestricted, "unrestricted", false, "Include unrestricted symbols.")
|
||||
tflag.BoolVar(&useUnsafe, "unsafe", false, "Include usafe symbols.")
|
||||
tflag.BoolVar(&useSyscall, "syscall", false, "Include syscall symbols.")
|
||||
tflag.BoolVar(&useUnrestricted, "unrestricted", useUnrestricted, "Include unrestricted symbols.")
|
||||
tflag.BoolVar(&useUnsafe, "unsafe", useUnsafe, "Include usafe symbols.")
|
||||
tflag.BoolVar(&useSyscall, "syscall", useSyscall, "Include syscall symbols.")
|
||||
tflag.BoolVar(&verbose, "v", false, "Verbose output: log all tests as they are run.")
|
||||
tflag.Usage = func() {
|
||||
fmt.Println("Usage: yaegi test [options] [path]")
|
||||
@@ -98,18 +103,40 @@ func test(arg []string) (err error) {
|
||||
testing.Init()
|
||||
os.Args = tf
|
||||
flag.Parse()
|
||||
path += string(filepath.Separator)
|
||||
var dir string
|
||||
|
||||
switch strings.Split(path, string(filepath.Separator))[0] {
|
||||
case ".", "..", string(filepath.Separator):
|
||||
dir = path
|
||||
default:
|
||||
dir = filepath.Join(build.Default.GOPATH, "src", path)
|
||||
}
|
||||
if err = os.Chdir(dir); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
i := interp.New(interp.Options{GoPath: build.Default.GOPATH, BuildTags: strings.Split(tags, ",")})
|
||||
i.Use(stdlib.Symbols)
|
||||
i.Use(interp.Symbols)
|
||||
if useSyscall {
|
||||
i.Use(syscall.Symbols)
|
||||
// Using a environment var allows a nested interpreter to import the syscall package.
|
||||
if err := os.Setenv("YAEGI_SYSCALL", "1"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if useUnrestricted {
|
||||
i.Use(unrestricted.Symbols)
|
||||
if err := os.Setenv("YAEGI_UNRESTRICTED", "1"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if useUnsafe {
|
||||
i.Use(unsafe.Symbols)
|
||||
if err := os.Setenv("YAEGI_UNSAFE", "1"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err = i.EvalTest(path); err != nil {
|
||||
return err
|
||||
@@ -117,7 +144,11 @@ func test(arg []string) (err error) {
|
||||
|
||||
benchmarks := []testing.InternalBenchmark{}
|
||||
tests := []testing.InternalTest{}
|
||||
for name, sym := range i.Symbols(path) {
|
||||
syms, ok := i.Symbols(path)[path]
|
||||
if !ok {
|
||||
return errors.New("No tests found")
|
||||
}
|
||||
for name, sym := range syms {
|
||||
switch fun := sym.Interface().(type) {
|
||||
case func(*testing.B):
|
||||
benchmarks = append(benchmarks, testing.InternalBenchmark{name, fun})
|
||||
|
||||
@@ -71,7 +71,16 @@ Options:
|
||||
-unsafe
|
||||
include unsafe symbols.
|
||||
|
||||
Debugging support (may be removed at any time):
|
||||
Environment variables:
|
||||
YAEGI_SYSCALL=1
|
||||
Include syscall symbols (same as -syscall flag).
|
||||
YAEGI_UNRESTRICTED=1
|
||||
Include unrestricted symbols (same as -unrestricted flag).
|
||||
YAEGI_UNSAFE=1
|
||||
Include unsafe symbols (same as -unsafe flag).
|
||||
YAEGI_PROMPT=1
|
||||
Force enable the printing of the REPL prompt and the result of last instruction,
|
||||
even if stdin is not a terminal.
|
||||
YAEGI_AST_DOT=1
|
||||
Generate and display graphviz dot of AST with dotty(1)
|
||||
YAEGI_CFG_DOT=1
|
||||
@@ -90,6 +99,8 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/traefik/yaegi/interp"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -97,8 +108,11 @@ const (
|
||||
Help = "help"
|
||||
Run = "run"
|
||||
Test = "test"
|
||||
Version = "version"
|
||||
)
|
||||
|
||||
var version = "devel" // This may be overwritten at build time.
|
||||
|
||||
func main() {
|
||||
var cmd string
|
||||
var err error
|
||||
@@ -119,6 +133,8 @@ func main() {
|
||||
err = run(os.Args[2:])
|
||||
case Test:
|
||||
err = test(os.Args[2:])
|
||||
case Version:
|
||||
fmt.Println(version)
|
||||
default:
|
||||
// If no command is given, fallback to default "run" command.
|
||||
// This allows scripts starting with "#!/usr/bin/env yaegi",
|
||||
@@ -129,8 +145,10 @@ func main() {
|
||||
}
|
||||
|
||||
if err != nil && !errors.Is(err, flag.ErrHelp) {
|
||||
err = fmt.Errorf("%s: %w", cmd, err)
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
fmt.Fprintln(os.Stderr, fmt.Errorf("%s: %w", cmd, err))
|
||||
if p, ok := err.(interp.Panic); ok {
|
||||
fmt.Fprintln(os.Stderr, string(p.Stack))
|
||||
}
|
||||
exitCode = 1
|
||||
}
|
||||
os.Exit(exitCode)
|
||||
|
||||
13
example/pkg/_pkg13/src/guthib.com/foo/bar/main.go
Normal file
13
example/pkg/_pkg13/src/guthib.com/foo/bar/main.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"guthib.com/bat/baz"
|
||||
)
|
||||
|
||||
func main() {
|
||||
t := baz.NewT()
|
||||
|
||||
fmt.Printf("%s", t.A3)
|
||||
}
|
||||
22
example/pkg/_pkg13/src/guthib.com/foo/bar/vendor/guthib.com/bat/baz/baz.go
generated
vendored
Normal file
22
example/pkg/_pkg13/src/guthib.com/foo/bar/vendor/guthib.com/bat/baz/baz.go
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
package baz
|
||||
|
||||
func NewT() *T {
|
||||
return &T{
|
||||
A1: make([]U, 0),
|
||||
A3: "foobar",
|
||||
}
|
||||
}
|
||||
|
||||
type T struct {
|
||||
A1 []U
|
||||
A3 string
|
||||
}
|
||||
|
||||
type U struct {
|
||||
B1 V
|
||||
B2 V
|
||||
}
|
||||
|
||||
type V struct {
|
||||
C1 string
|
||||
}
|
||||
@@ -93,6 +93,12 @@ func TestPackages(t *testing.T) {
|
||||
expected: "Yo hello",
|
||||
evalFile: "./_pkg12/src/guthib.com/foo/main.go",
|
||||
},
|
||||
{
|
||||
desc: "eval main with vendor",
|
||||
goPath: "./_pkg13/",
|
||||
expected: "foobar",
|
||||
evalFile: "./_pkg13/src/guthib.com/foo/bar/main.go",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
|
||||
@@ -436,10 +436,12 @@ func GetMinor(part string) string {
|
||||
return minor
|
||||
}
|
||||
|
||||
const defaultMinorVersion = 15
|
||||
|
||||
func genBuildTags() (string, error) {
|
||||
version := runtime.Version()
|
||||
if version == "devel" {
|
||||
return "", nil
|
||||
if strings.HasPrefix(version, "devel") {
|
||||
return "", fmt.Errorf("extracting only supported with stable releases of Go, not %v", version)
|
||||
}
|
||||
parts := strings.Split(version, ".")
|
||||
|
||||
@@ -452,6 +454,11 @@ func genBuildTags() (string, error) {
|
||||
return "", fmt.Errorf("failed to parse version: %v", err)
|
||||
}
|
||||
|
||||
// Only append an upper bound if we are not on the latest go
|
||||
if minor >= defaultMinorVersion {
|
||||
return currentGoVersion, nil
|
||||
}
|
||||
|
||||
nextGoVersion := parts[0] + "." + strconv.Itoa(minor+1)
|
||||
|
||||
return currentGoVersion + ",!" + nextGoVersion, nil
|
||||
@@ -1,6 +1,6 @@
|
||||
package yaegi
|
||||
|
||||
//go:generate go generate github.com/traefik/yaegi/internal/extract
|
||||
//go:generate go generate github.com/traefik/yaegi/internal/cmd/extract
|
||||
//go:generate go generate github.com/traefik/yaegi/interp
|
||||
//go:generate go generate github.com/traefik/yaegi/stdlib
|
||||
//go:generate go generate github.com/traefik/yaegi/stdlib/syscall
|
||||
|
||||
@@ -28,7 +28,7 @@ import (
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/traefik/yaegi/internal/extract"
|
||||
"github.com/traefik/yaegi/extract"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -84,11 +84,12 @@ func main() {
|
||||
oFile = strings.ReplaceAll(importPath, "/", "_") + ".go"
|
||||
}
|
||||
|
||||
prefix := runtime.Version()
|
||||
if runtime.Version() != "devel" {
|
||||
parts := strings.Split(runtime.Version(), ".")
|
||||
prefix = parts[0] + "_" + extract.GetMinor(parts[1])
|
||||
version := runtime.Version()
|
||||
if strings.HasPrefix(version, "devel") {
|
||||
log.Fatalf("extracting only supported with stable releases of Go, not %v", version)
|
||||
}
|
||||
parts := strings.Split(version, ".")
|
||||
prefix := parts[0] + "_" + extract.GetMinor(parts[1])
|
||||
|
||||
f, err := os.Create(prefix + "_" + oFile)
|
||||
if err != nil {
|
||||
|
||||
@@ -196,6 +196,18 @@ func {{$name}}Const(n *node) {
|
||||
{{- if $op.Shift}}
|
||||
v := constant.Shift(vConstantValue(v0), token.{{tokenFromName $name}}, uint(vUint(v1)))
|
||||
n.rval.Set(reflect.ValueOf(v))
|
||||
{{- else if (eq $op.Name "/")}}
|
||||
var operator token.Token
|
||||
// When the result of the operation is expected to be an int (because both
|
||||
// operands are ints), we want to force the type of the whole expression to be an
|
||||
// int (and not a float), which is achieved by using the QUO_ASSIGN operator.
|
||||
if n.typ.untyped && isInt(n.typ.rtype) {
|
||||
operator = token.QUO_ASSIGN
|
||||
} else {
|
||||
operator = token.QUO
|
||||
}
|
||||
v := constant.BinaryOp(vConstantValue(v0), operator, vConstantValue(v1))
|
||||
n.rval.Set(reflect.ValueOf(v))
|
||||
{{- else}}
|
||||
v := constant.BinaryOp(vConstantValue(v0), token.{{tokenFromName $name}}, vConstantValue(v1))
|
||||
n.rval.Set(reflect.ValueOf(v))
|
||||
@@ -395,10 +407,10 @@ func {{$name}}Const(n *node) {
|
||||
case isConst:
|
||||
v := constant.UnaryOp(token.{{tokenFromName $name}}, vConstantValue(v0), 0)
|
||||
n.rval.Set(reflect.ValueOf(v))
|
||||
case isInt(t):
|
||||
n.rval.SetInt({{$op.Name}} v0.Int())
|
||||
case isUint(t):
|
||||
n.rval.SetUint({{$op.Name}} v0.Uint())
|
||||
case isInt(t):
|
||||
n.rval.SetInt({{$op.Name}} v0.Int())
|
||||
{{- if $op.Float}}
|
||||
case isFloat(t):
|
||||
n.rval.SetFloat({{$op.Name}} v0.Float())
|
||||
|
||||
@@ -51,12 +51,14 @@ const (
|
||||
fieldList
|
||||
fileStmt
|
||||
forStmt0 // for {}
|
||||
forStmt1 // for cond {}
|
||||
forStmt2 // for init; cond; {}
|
||||
forStmt3 // for ; cond; post {}
|
||||
forStmt3a // for init; ; post {}
|
||||
forStmt4 // for init; cond; post {}
|
||||
forRangeStmt // for range
|
||||
forStmt1 // for init; ; {}
|
||||
forStmt2 // for cond {}
|
||||
forStmt3 // for init; cond; {}
|
||||
forStmt4 // for ; ; post {}
|
||||
forStmt5 // for ; cond; post {}
|
||||
forStmt6 // for init; ; post {}
|
||||
forStmt7 // for init; cond; post {}
|
||||
forRangeStmt // for range {}
|
||||
funcDecl
|
||||
funcLit
|
||||
funcType
|
||||
@@ -134,8 +136,10 @@ var kinds = [...]string{
|
||||
forStmt1: "forStmt1",
|
||||
forStmt2: "forStmt2",
|
||||
forStmt3: "forStmt3",
|
||||
forStmt3a: "forStmt3a",
|
||||
forStmt4: "forStmt4",
|
||||
forStmt5: "forStmt5",
|
||||
forStmt6: "forStmt6",
|
||||
forStmt7: "forStmt7",
|
||||
forRangeStmt: "forRangeStmt",
|
||||
funcDecl: "funcDecl",
|
||||
funcType: "funcType",
|
||||
@@ -654,23 +658,23 @@ func (interp *Interpreter) ast(src, name string, inc bool) (string, *node, error
|
||||
case *ast.ForStmt:
|
||||
// Disambiguate variants of FOR statements with a node kind per variant
|
||||
var kind nkind
|
||||
if a.Cond == nil {
|
||||
if a.Init != nil && a.Post != nil {
|
||||
kind = forStmt3a
|
||||
} else {
|
||||
kind = forStmt0
|
||||
}
|
||||
} else {
|
||||
switch {
|
||||
case a.Init == nil && a.Post == nil:
|
||||
kind = forStmt1
|
||||
case a.Init != nil && a.Post == nil:
|
||||
kind = forStmt2
|
||||
case a.Init == nil && a.Post != nil:
|
||||
kind = forStmt3
|
||||
default:
|
||||
kind = forStmt4
|
||||
}
|
||||
switch {
|
||||
case a.Cond == nil && a.Init == nil && a.Post == nil:
|
||||
kind = forStmt0
|
||||
case a.Cond == nil && a.Init != nil && a.Post == nil:
|
||||
kind = forStmt1
|
||||
case a.Cond != nil && a.Init == nil && a.Post == nil:
|
||||
kind = forStmt2
|
||||
case a.Cond != nil && a.Init != nil && a.Post == nil:
|
||||
kind = forStmt3
|
||||
case a.Cond == nil && a.Init == nil && a.Post != nil:
|
||||
kind = forStmt4
|
||||
case a.Cond != nil && a.Init == nil && a.Post != nil:
|
||||
kind = forStmt5
|
||||
case a.Cond == nil && a.Init != nil && a.Post != nil:
|
||||
kind = forStmt6
|
||||
case a.Cond != nil && a.Init != nil && a.Post != nil:
|
||||
kind = forStmt7
|
||||
}
|
||||
st.push(addChild(&root, anc, pos, kind, aNop), nod)
|
||||
|
||||
|
||||
230
interp/cfg.go
230
interp/cfg.go
@@ -66,6 +66,44 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
return false
|
||||
}
|
||||
switch n.kind {
|
||||
case binaryExpr, unaryExpr, parenExpr:
|
||||
if isBoolAction(n) {
|
||||
break
|
||||
}
|
||||
// Gather assigned type if set, to give context for type propagation at post-order.
|
||||
switch n.anc.kind {
|
||||
case assignStmt, defineStmt:
|
||||
a := n.anc
|
||||
i := childPos(n) - a.nright
|
||||
if i < 0 {
|
||||
break
|
||||
}
|
||||
if len(a.child) > a.nright+a.nleft {
|
||||
i--
|
||||
}
|
||||
dest := a.child[i]
|
||||
if dest.typ != nil && !isInterface(dest.typ) {
|
||||
// Interface type are not propagated, and will be resolved at post-order.
|
||||
n.typ = dest.typ
|
||||
}
|
||||
case binaryExpr, unaryExpr, parenExpr:
|
||||
n.typ = n.anc.typ
|
||||
}
|
||||
|
||||
case defineStmt:
|
||||
// Determine type of variables initialized at declaration, so it can be propagated.
|
||||
if n.nleft+n.nright == len(n.child) {
|
||||
// No type was specified on the left hand side, it will resolved at post-order.
|
||||
break
|
||||
}
|
||||
n.typ, err = nodeType(interp, sc, n.child[n.nleft])
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
for i := 0; i < n.nleft; i++ {
|
||||
n.child[i].typ = n.typ
|
||||
}
|
||||
|
||||
case blockStmt:
|
||||
if n.anc != nil && n.anc.kind == rangeStmt {
|
||||
// For range block: ensure that array or map type is propagated to iterators
|
||||
@@ -159,7 +197,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
|
||||
case breakStmt, continueStmt, gotoStmt:
|
||||
if len(n.child) > 0 {
|
||||
// Handle labeled statements
|
||||
// Handle labeled statements.
|
||||
label := n.child[0].ident
|
||||
if sym, _, ok := sc.lookup(label); ok {
|
||||
if sym.kind != labelSym {
|
||||
@@ -176,25 +214,23 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
|
||||
case labeledStmt:
|
||||
label := n.child[0].ident
|
||||
if sym, _, ok := sc.lookup(label); ok {
|
||||
if sym.kind != labelSym {
|
||||
err = n.child[0].cfgErrorf("label %s not defined", label)
|
||||
break
|
||||
}
|
||||
// TODO(marc): labels must be stored outside of symbols to avoid collisions
|
||||
// Used labels are searched in current and sub scopes, not upper ones.
|
||||
if sym, ok := sc.lookdown(label); ok {
|
||||
sym.node = n
|
||||
n.sym = sym
|
||||
} else {
|
||||
n.sym = &symbol{kind: labelSym, node: n, index: -1}
|
||||
sc.sym[label] = n.sym
|
||||
}
|
||||
sc.sym[label] = n.sym
|
||||
|
||||
case caseClause:
|
||||
sc = sc.pushBloc()
|
||||
if sn := n.anc.anc; sn.kind == typeSwitch && sn.child[1].action == aAssign {
|
||||
// Type switch clause with a var defined in switch guard
|
||||
// Type switch clause with a var defined in switch guard.
|
||||
var typ *itype
|
||||
if len(n.child) == 2 {
|
||||
// 1 type in clause: define the var with this type in the case clause scope
|
||||
// 1 type in clause: define the var with this type in the case clause scope.
|
||||
switch {
|
||||
case n.child[0].ident == nilIdent:
|
||||
typ = sc.getType("interface{}")
|
||||
@@ -204,7 +240,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
typ, err = nodeType(interp, sc, n.child[0])
|
||||
}
|
||||
} else {
|
||||
// define the var with the type in the switch guard expression
|
||||
// Define the var with the type in the switch guard expression.
|
||||
typ = sn.child[1].child[1].child[0].typ
|
||||
}
|
||||
if err != nil {
|
||||
@@ -252,8 +288,12 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
// Get type from ancestor (implicit type)
|
||||
if n.anc.kind == keyValueExpr && n == n.anc.child[0] {
|
||||
n.typ = n.anc.typ.key
|
||||
} else if n.anc.typ != nil {
|
||||
n.typ = n.anc.typ.val
|
||||
} else if atyp := n.anc.typ; atyp != nil {
|
||||
if atyp.cat == valueT {
|
||||
n.typ = &itype{cat: valueT, rtype: atyp.rtype.Elem()}
|
||||
} else {
|
||||
n.typ = atyp.val
|
||||
}
|
||||
}
|
||||
if n.typ == nil {
|
||||
err = n.cfgErrorf("undefined type")
|
||||
@@ -285,11 +325,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
}
|
||||
}
|
||||
|
||||
case forStmt0, forRangeStmt:
|
||||
sc = sc.pushBloc()
|
||||
sc.loop, sc.loopRestart = n, n.child[0]
|
||||
|
||||
case forStmt1, forStmt2, forStmt3, forStmt3a, forStmt4:
|
||||
case forStmt0, forStmt1, forStmt2, forStmt3, forStmt4, forStmt5, forStmt6, forStmt7, forRangeStmt:
|
||||
sc = sc.pushBloc()
|
||||
sc.loop, sc.loopRestart = n, n.lastChild()
|
||||
|
||||
@@ -386,9 +422,10 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
return false
|
||||
}
|
||||
|
||||
if n.child[1].kind == identExpr {
|
||||
switch n.child[1].kind {
|
||||
case identExpr, selectorExpr:
|
||||
n.typ = &itype{cat: aliasT, val: typ, name: typeName}
|
||||
} else {
|
||||
default:
|
||||
n.typ = typ
|
||||
n.typ.name = typeName
|
||||
}
|
||||
@@ -443,14 +480,11 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
n.gen = nop
|
||||
break
|
||||
}
|
||||
if n.anc.kind == commClause {
|
||||
n.gen = nop
|
||||
break
|
||||
}
|
||||
|
||||
var atyp *itype
|
||||
if n.nleft+n.nright < len(n.child) {
|
||||
if atyp, err = nodeType(interp, sc, n.child[n.nleft]); err != nil {
|
||||
return
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
@@ -554,6 +588,14 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
n.gen = nop
|
||||
src.findex = dest.findex
|
||||
src.level = level
|
||||
case n.action == aAssign && len(n.child) < 4 && !src.rval.IsValid() && isArithmeticAction(src):
|
||||
// Optimize single assignments from some arithmetic operations.
|
||||
// Skip the assign operation entirely, the source frame index is set
|
||||
// to destination index, avoiding extra memory alloc and duplication.
|
||||
src.typ = dest.typ
|
||||
src.findex = dest.findex
|
||||
src.level = level
|
||||
n.gen = nop
|
||||
case src.kind == basicLit && !src.rval.IsValid():
|
||||
// Assign to nil.
|
||||
src.rval = reflect.New(dest.typ.TypeOf()).Elem()
|
||||
@@ -569,7 +611,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
}
|
||||
if n.anc.kind == constDecl {
|
||||
n.gen = nop
|
||||
n.findex = -1
|
||||
n.findex = notInFrame
|
||||
if sym, _, ok := sc.lookup(dest.ident); ok {
|
||||
sym.kind = constSym
|
||||
}
|
||||
@@ -610,7 +652,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
if n.child[0].ident == "_" {
|
||||
lc.gen = typeAssertStatus
|
||||
} else {
|
||||
lc.gen = typeAssert2
|
||||
lc.gen = typeAssertLong
|
||||
}
|
||||
n.gen = nop
|
||||
case unaryExpr:
|
||||
@@ -639,7 +681,12 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
}
|
||||
|
||||
switch n.action {
|
||||
case aRem, aShl, aShr:
|
||||
case aRem:
|
||||
n.typ = c0.typ
|
||||
case aShl, aShr:
|
||||
if c0.typ.untyped {
|
||||
break
|
||||
}
|
||||
n.typ = c0.typ
|
||||
case aEqual, aNotEqual:
|
||||
n.typ = sc.getType("bool")
|
||||
@@ -670,7 +717,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
// This operation involved constants, and the result is already computed
|
||||
// by constOp and available in n.rval. Nothing else to do at execution.
|
||||
n.gen = nop
|
||||
n.findex = -1
|
||||
n.findex = notInFrame
|
||||
case n.anc.kind == assignStmt && n.anc.action == aAssign:
|
||||
// To avoid a copy in frame, if the result is to be assigned, store it directly
|
||||
// at the frame location of destination.
|
||||
@@ -811,7 +858,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
}
|
||||
switch {
|
||||
case n.typ.cat == builtinT:
|
||||
n.findex = -1
|
||||
n.findex = notInFrame
|
||||
n.val = nil
|
||||
case n.anc.kind == returnStmt:
|
||||
// Store result directly to frame output location, to avoid a frame copy.
|
||||
@@ -853,9 +900,14 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
n.rval = c1.rval
|
||||
case c1.rval.IsValid() && isConstType(c0.typ):
|
||||
n.gen = nop
|
||||
n.findex = -1
|
||||
n.findex = notInFrame
|
||||
n.typ = c0.typ
|
||||
n.rval = c1.rval
|
||||
if c, ok := c1.rval.Interface().(constant.Value); ok {
|
||||
i, _ := constant.Int64Val(constant.ToInt(c))
|
||||
n.rval = reflect.ValueOf(i).Convert(c0.typ.rtype)
|
||||
} else {
|
||||
n.rval = c1.rval.Convert(c0.typ.rtype)
|
||||
}
|
||||
default:
|
||||
n.gen = convert
|
||||
n.typ = c0.typ
|
||||
@@ -911,7 +963,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
n.findex = -1
|
||||
n.findex = notInFrame
|
||||
}
|
||||
}
|
||||
|
||||
@@ -982,7 +1034,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
|
||||
n.findex = sc.add(n.typ)
|
||||
// TODO: Check that composite literal expr matches corresponding type
|
||||
n.gen = compositeGenerator(n, n.typ)
|
||||
n.gen = compositeGenerator(n, n.typ, nil)
|
||||
|
||||
case fallthroughtStmt:
|
||||
if n.anc.kind != caseBody {
|
||||
@@ -992,7 +1044,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
case fileStmt:
|
||||
wireChild(n, varDecl)
|
||||
sc = sc.pop()
|
||||
n.findex = -1
|
||||
n.findex = notInFrame
|
||||
|
||||
case forStmt0: // for {}
|
||||
body := n.child[0]
|
||||
@@ -1000,7 +1052,14 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
body.tnext = n.start
|
||||
sc = sc.pop()
|
||||
|
||||
case forStmt1: // for cond {}
|
||||
case forStmt1: // for init; ; {}
|
||||
init, body := n.child[0], n.child[1]
|
||||
n.start = init.start
|
||||
init.tnext = body.start
|
||||
body.tnext = n.start
|
||||
sc = sc.pop()
|
||||
|
||||
case forStmt2: // for cond {}
|
||||
cond, body := n.child[0], n.child[1]
|
||||
if !isBool(cond.typ) {
|
||||
err = cond.cfgErrorf("non-bool used as for condition")
|
||||
@@ -1019,7 +1078,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
setFNext(cond, n)
|
||||
sc = sc.pop()
|
||||
|
||||
case forStmt2: // for init; cond; {}
|
||||
case forStmt3: // for init; cond; {}
|
||||
init, cond, body := n.child[0], n.child[1], n.child[2]
|
||||
if !isBool(cond.typ) {
|
||||
err = cond.cfgErrorf("non-bool used as for condition")
|
||||
@@ -1041,7 +1100,14 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
setFNext(cond, n)
|
||||
sc = sc.pop()
|
||||
|
||||
case forStmt3: // for ; cond; post {}
|
||||
case forStmt4: // for ; ; post {}
|
||||
post, body := n.child[0], n.child[1]
|
||||
n.start = body.start
|
||||
post.tnext = body.start
|
||||
body.tnext = post.start
|
||||
sc = sc.pop()
|
||||
|
||||
case forStmt5: // for ; cond; post {}
|
||||
cond, post, body := n.child[0], n.child[1], n.child[2]
|
||||
if !isBool(cond.typ) {
|
||||
err = cond.cfgErrorf("non-bool used as for condition")
|
||||
@@ -1061,7 +1127,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
body.tnext = post.start
|
||||
sc = sc.pop()
|
||||
|
||||
case forStmt3a: // for init; ; post {}
|
||||
case forStmt6: // for init; ; post {}
|
||||
init, post, body := n.child[0], n.child[1], n.child[2]
|
||||
n.start = init.start
|
||||
init.tnext = body.start
|
||||
@@ -1069,7 +1135,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
post.tnext = body.start
|
||||
sc = sc.pop()
|
||||
|
||||
case forStmt4: // for init; cond; post {}
|
||||
case forStmt7: // for init; cond; post {}
|
||||
init, cond, post, body := n.child[0], n.child[1], n.child[2], n.child[3]
|
||||
if !isBool(cond.typ) {
|
||||
err = cond.cfgErrorf("non-bool used as for condition")
|
||||
@@ -1129,7 +1195,6 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
sym, level, found := sc.lookup(n.ident)
|
||||
if !found {
|
||||
// retry with the filename, in case ident is a package name.
|
||||
// TODO(mpl): maybe we improve lookup itself so it can deal with that.
|
||||
sym, level, found = sc.lookup(filepath.Join(n.ident, baseName))
|
||||
if !found {
|
||||
err = n.cfgErrorf("undefined: %s", n.ident)
|
||||
@@ -1302,7 +1367,12 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
}
|
||||
|
||||
case returnStmt:
|
||||
if mustReturnValue(sc.def.child[2]) {
|
||||
if len(n.child) > sc.def.typ.numOut() {
|
||||
err = n.cfgErrorf("too many arguments to return")
|
||||
break
|
||||
}
|
||||
returnSig := sc.def.child[2]
|
||||
if mustReturnValue(returnSig) {
|
||||
nret := len(n.child)
|
||||
if nret == 1 && isCall(n.child[0]) {
|
||||
nret = n.child[0].child[0].typ.numOut()
|
||||
@@ -1316,13 +1386,19 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
n.tnext = nil
|
||||
n.val = sc.def
|
||||
for i, c := range n.child {
|
||||
var typ *itype
|
||||
typ, err = nodeType(interp, sc.upperLevel(), returnSig.child[1].fieldType(i))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// TODO(mpl): move any of that code to typecheck?
|
||||
c.typ.node = c
|
||||
if !c.typ.assignableTo(typ) {
|
||||
err = fmt.Errorf("cannot use %v (type %v) as type %v in return argument", c.ident, c.typ.cat, typ.cat)
|
||||
return
|
||||
}
|
||||
if c.typ.cat == nilT {
|
||||
// nil: Set node value to zero of return type
|
||||
f := sc.def
|
||||
var typ *itype
|
||||
if typ, err = nodeType(interp, sc, f.child[2].child[1].fieldType(i)); err != nil {
|
||||
return
|
||||
}
|
||||
if typ.cat == funcT {
|
||||
// Wrap the typed nil value in a node, as per other interpreter functions
|
||||
c.rval = reflect.ValueOf(&node{kind: basicLit, rval: reflect.New(typ.TypeOf()).Elem()})
|
||||
@@ -1426,6 +1502,9 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
// Resolve source package symbol
|
||||
if sym, ok := interp.srcPkg[pkg][name]; ok {
|
||||
n.findex = sym.index
|
||||
if sym.global {
|
||||
n.level = globalFrame
|
||||
}
|
||||
n.val = sym.node
|
||||
n.gen = nop
|
||||
n.action = aGetSym
|
||||
@@ -1440,7 +1519,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
if n.child[0].isType(sc) {
|
||||
// Handle method as a function with receiver in 1st argument
|
||||
n.val = m
|
||||
n.findex = -1
|
||||
n.findex = notInFrame
|
||||
n.gen = nop
|
||||
n.typ = &itype{}
|
||||
*n.typ = *m.typ
|
||||
@@ -1753,7 +1832,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
switch {
|
||||
case n.rval.IsValid():
|
||||
n.gen = nop
|
||||
n.findex = -1
|
||||
n.findex = notInFrame
|
||||
case n.anc.kind == assignStmt && n.anc.action == aAssign:
|
||||
dest := n.anc.child[childPos(n)-n.anc.nright]
|
||||
n.typ = dest.typ
|
||||
@@ -1780,6 +1859,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
if sc.global {
|
||||
// Global object allocation is already performed in GTA.
|
||||
index = sc.sym[c.ident].index
|
||||
c.level = globalFrame
|
||||
} else {
|
||||
index = sc.add(n.typ)
|
||||
sc.sym[c.ident] = &symbol{index: index, kind: varSym, typ: n.typ}
|
||||
@@ -1806,8 +1886,15 @@ func compDefineX(sc *scope, n *node) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for funtype.cat == valueT && funtype.val != nil {
|
||||
// Retrieve original interpreter type from a wrapped function.
|
||||
// Struct fields of function types are always wrapped in valueT to ensure
|
||||
// their possible use in runtime. In that case, the val field retains the
|
||||
// original interpreter type, which is used now.
|
||||
funtype = funtype.val
|
||||
}
|
||||
if funtype.cat == valueT {
|
||||
// Handle functions imported from runtime
|
||||
// Handle functions imported from runtime.
|
||||
for i := 0; i < funtype.rtype.NumOut(); i++ {
|
||||
types = append(types, &itype{cat: valueT, rtype: funtype.rtype.Out(i)})
|
||||
}
|
||||
@@ -1831,7 +1918,7 @@ func compDefineX(sc *scope, n *node) error {
|
||||
if n.child[0].ident == "_" {
|
||||
n.child[l].gen = typeAssertStatus
|
||||
} else {
|
||||
n.child[l].gen = typeAssert2
|
||||
n.child[l].gen = typeAssertLong
|
||||
}
|
||||
types = append(types, n.child[l].child[1].typ, sc.getType("bool"))
|
||||
n.gen = nop
|
||||
@@ -2263,7 +2350,7 @@ func isCall(n *node) bool {
|
||||
}
|
||||
|
||||
func isBinCall(n *node) bool {
|
||||
return n.kind == callExpr && n.child[0].typ.cat == valueT && n.child[0].typ.rtype.Kind() == reflect.Func
|
||||
return isCall(n) && n.child[0].typ.cat == valueT && n.child[0].typ.rtype.Kind() == reflect.Func
|
||||
}
|
||||
|
||||
func mustReturnValue(n *node) bool {
|
||||
@@ -2279,7 +2366,7 @@ func mustReturnValue(n *node) bool {
|
||||
}
|
||||
|
||||
func isRegularCall(n *node) bool {
|
||||
return n.kind == callExpr && n.child[0].typ.cat == funcT
|
||||
return isCall(n) && n.child[0].typ.cat == funcT
|
||||
}
|
||||
|
||||
func variadicPos(n *node) int {
|
||||
@@ -2360,10 +2447,10 @@ func gotoLabel(s *symbol) {
|
||||
}
|
||||
}
|
||||
|
||||
func compositeGenerator(n *node, typ *itype) (gen bltnGenerator) {
|
||||
func compositeGenerator(n *node, typ *itype, rtyp reflect.Type) (gen bltnGenerator) {
|
||||
switch typ.cat {
|
||||
case aliasT, ptrT:
|
||||
gen = compositeGenerator(n, n.typ.val)
|
||||
gen = compositeGenerator(n, n.typ.val, rtyp)
|
||||
case arrayT:
|
||||
gen = arrayLit
|
||||
case mapT:
|
||||
@@ -2386,11 +2473,23 @@ func compositeGenerator(n *node, typ *itype) (gen bltnGenerator) {
|
||||
}
|
||||
}
|
||||
case valueT:
|
||||
switch k := n.typ.rtype.Kind(); k {
|
||||
if rtyp == nil {
|
||||
rtyp = n.typ.rtype
|
||||
}
|
||||
switch k := rtyp.Kind(); k {
|
||||
case reflect.Struct:
|
||||
gen = compositeBinStruct
|
||||
if n.nleft == 1 {
|
||||
gen = compositeBinStruct
|
||||
} else {
|
||||
gen = compositeBinStructNotype
|
||||
}
|
||||
case reflect.Map:
|
||||
// TODO(mpl): maybe needs a NoType version too
|
||||
gen = compositeBinMap
|
||||
case reflect.Ptr:
|
||||
gen = compositeGenerator(n, typ, n.typ.val.rtype)
|
||||
case reflect.Slice:
|
||||
gen = compositeBinSlice
|
||||
default:
|
||||
log.Panic(n.cfgErrorf("compositeGenerator not implemented for type kind: %s", k))
|
||||
}
|
||||
@@ -2432,3 +2531,22 @@ func isValueUntyped(v reflect.Value) bool {
|
||||
}
|
||||
return t.String() == t.Kind().String()
|
||||
}
|
||||
|
||||
// isArithmeticAction returns true if the node action is an arithmetic operator.
|
||||
func isArithmeticAction(n *node) bool {
|
||||
switch n.action {
|
||||
case aAdd, aAnd, aAndNot, aBitNot, aMul, aQuo, aRem, aShl, aShr, aSub, aXor:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func isBoolAction(n *node) bool {
|
||||
switch n.action {
|
||||
case aEqual, aGreater, aGreaterEqual, aLand, aLor, aLower, aLowerEqual, aNot, aNotEqual:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,6 +115,7 @@ func (interp *Interpreter) gta(root *node, rpath, importPath string) ([]*node, e
|
||||
sc.sym[c.ident] = &symbol{index: sc.add(n.typ), kind: varSym, global: true, typ: n.typ, node: n}
|
||||
continue
|
||||
}
|
||||
c.level = globalFrame
|
||||
|
||||
// redeclaration error
|
||||
if sym.typ.node != nil && sym.typ.node.anc != nil {
|
||||
@@ -145,7 +146,7 @@ func (interp *Interpreter) gta(root *node, rpath, importPath string) ([]*node, e
|
||||
elementType := sc.getType(typeName)
|
||||
if elementType == nil {
|
||||
// Add type if necessary, so method can be registered
|
||||
sc.sym[typeName] = &symbol{kind: typeSym, typ: &itype{name: typeName, path: rpath, incomplete: true, node: rtn.child[0], scope: sc}}
|
||||
sc.sym[typeName] = &symbol{kind: typeSym, typ: &itype{name: typeName, path: importPath, incomplete: true, node: rtn.child[0], scope: sc}}
|
||||
elementType = sc.sym[typeName].typ
|
||||
}
|
||||
rcvrtype = &itype{cat: ptrT, val: elementType, incomplete: elementType.incomplete, node: rtn, scope: sc}
|
||||
@@ -154,7 +155,7 @@ func (interp *Interpreter) gta(root *node, rpath, importPath string) ([]*node, e
|
||||
rcvrtype = sc.getType(typeName)
|
||||
if rcvrtype == nil {
|
||||
// Add type if necessary, so method can be registered
|
||||
sc.sym[typeName] = &symbol{kind: typeSym, typ: &itype{name: typeName, path: rpath, incomplete: true, node: rtn, scope: sc}}
|
||||
sc.sym[typeName] = &symbol{kind: typeSym, typ: &itype{name: typeName, path: importPath, incomplete: true, node: rtn, scope: sc}}
|
||||
rcvrtype = sc.sym[typeName].typ
|
||||
}
|
||||
}
|
||||
@@ -248,16 +249,19 @@ func (interp *Interpreter) gta(root *node, rpath, importPath string) ([]*node, e
|
||||
typeName := n.child[0].ident
|
||||
var typ *itype
|
||||
if typ, err = nodeType(interp, sc, n.child[1]); err != nil {
|
||||
err = nil
|
||||
revisit = append(revisit, n)
|
||||
return false
|
||||
}
|
||||
|
||||
if n.child[1].kind == identExpr {
|
||||
n.typ = &itype{cat: aliasT, val: typ, name: typeName, path: rpath, field: typ.field, incomplete: typ.incomplete, scope: sc, node: n.child[0]}
|
||||
switch n.child[1].kind {
|
||||
case identExpr, selectorExpr:
|
||||
n.typ = &itype{cat: aliasT, val: typ, name: typeName, path: importPath, field: typ.field, incomplete: typ.incomplete, scope: sc, node: n.child[0]}
|
||||
copy(n.typ.method, typ.method)
|
||||
} else {
|
||||
default:
|
||||
n.typ = typ
|
||||
n.typ.name = typeName
|
||||
n.typ.path = rpath
|
||||
n.typ.path = importPath
|
||||
}
|
||||
|
||||
asImportName := filepath.Join(typeName, baseName)
|
||||
|
||||
@@ -2,8 +2,6 @@ package interp
|
||||
|
||||
import "reflect"
|
||||
|
||||
const hooksPath = "github.com/traefik/yaegi"
|
||||
|
||||
// convertFn is the signature of a symbol converter.
|
||||
type convertFn func(from, to reflect.Type) func(src, dest reflect.Value)
|
||||
|
||||
|
||||
106
interp/interp.go
106
interp/interp.go
@@ -65,7 +65,8 @@ type frame struct {
|
||||
// Located at start of struct to ensure proper aligment.
|
||||
id uint64
|
||||
|
||||
anc *frame // ancestor frame (global space)
|
||||
root *frame // global space
|
||||
anc *frame // ancestor frame (caller space)
|
||||
data []reflect.Value // values
|
||||
|
||||
mutex sync.RWMutex
|
||||
@@ -80,8 +81,11 @@ func newFrame(anc *frame, len int, id uint64) *frame {
|
||||
data: make([]reflect.Value, len),
|
||||
id: id,
|
||||
}
|
||||
if anc != nil {
|
||||
if anc == nil {
|
||||
f.root = f
|
||||
} else {
|
||||
f.done = anc.done
|
||||
f.root = anc.root
|
||||
}
|
||||
return f
|
||||
}
|
||||
@@ -93,6 +97,7 @@ func (f *frame) clone() *frame {
|
||||
defer f.mutex.RUnlock()
|
||||
return &frame{
|
||||
anc: f.anc,
|
||||
root: f.root,
|
||||
data: f.data,
|
||||
deferred: f.deferred,
|
||||
recovered: f.recovered,
|
||||
@@ -155,8 +160,9 @@ type Interpreter struct {
|
||||
}
|
||||
|
||||
const (
|
||||
mainID = "main"
|
||||
selfPath = "github.com/traefik/yaegi/interp"
|
||||
mainID = "main"
|
||||
selfPrefix = "github.com/traefik/yaegi"
|
||||
selfPath = selfPrefix + "/interp"
|
||||
// DefaultSourceName is the name used by default when the name of the input
|
||||
// source file has not been specified for an Eval.
|
||||
// TODO(mpl): something even more special as a name?
|
||||
@@ -175,6 +181,7 @@ var Symbols = Exports{
|
||||
|
||||
"Interpreter": reflect.ValueOf((*Interpreter)(nil)),
|
||||
"Options": reflect.ValueOf((*Options)(nil)),
|
||||
"Panic": reflect.ValueOf((*Panic)(nil)),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -237,7 +244,7 @@ type Options struct {
|
||||
func New(options Options) *Interpreter {
|
||||
i := Interpreter{
|
||||
opt: opt{context: build.Default},
|
||||
frame: &frame{data: []reflect.Value{}},
|
||||
frame: newFrame(nil, 0, 0),
|
||||
fset: token.NewFileSet(),
|
||||
universe: initUniverse(),
|
||||
scopes: map[string]*scope{},
|
||||
@@ -401,32 +408,59 @@ func (interp *Interpreter) EvalTest(path string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// Symbols returns a map of interpreter exported symbol values for the given path.
|
||||
func (interp *Interpreter) Symbols(path string) map[string]reflect.Value {
|
||||
m := map[string]reflect.Value{}
|
||||
|
||||
// Symbols returns a map of interpreter exported symbol values for the given
|
||||
// import path. If the argument is the empty string, all known symbols are
|
||||
// returned.
|
||||
func (interp *Interpreter) Symbols(importPath string) Exports {
|
||||
m := map[string]map[string]reflect.Value{}
|
||||
interp.mutex.RLock()
|
||||
if interp.scopes[path] == nil {
|
||||
interp.mutex.RUnlock()
|
||||
return m
|
||||
}
|
||||
sym := interp.scopes[path].sym
|
||||
interp.mutex.RUnlock()
|
||||
defer interp.mutex.RUnlock()
|
||||
|
||||
for n, s := range sym {
|
||||
if !canExport(n) {
|
||||
// Skip private non-exported symbols.
|
||||
for k, v := range interp.srcPkg {
|
||||
if importPath != "" && k != importPath {
|
||||
continue
|
||||
}
|
||||
switch s.kind {
|
||||
case constSym:
|
||||
m[n] = s.rval
|
||||
case funcSym:
|
||||
m[n] = genFunctionWrapper(s.node)(interp.frame)
|
||||
case varSym:
|
||||
m[n] = interp.frame.data[s.index]
|
||||
syms := map[string]reflect.Value{}
|
||||
for n, s := range v {
|
||||
if !canExport(n) {
|
||||
// Skip private non-exported symbols.
|
||||
continue
|
||||
}
|
||||
switch s.kind {
|
||||
case constSym:
|
||||
syms[n] = s.rval
|
||||
case funcSym:
|
||||
syms[n] = genFunctionWrapper(s.node)(interp.frame)
|
||||
case varSym:
|
||||
syms[n] = interp.frame.data[s.index]
|
||||
case typeSym:
|
||||
syms[n] = reflect.New(s.typ.TypeOf())
|
||||
}
|
||||
}
|
||||
|
||||
if len(syms) > 0 {
|
||||
m[k] = syms
|
||||
}
|
||||
|
||||
if importPath != "" {
|
||||
return m
|
||||
}
|
||||
}
|
||||
|
||||
if importPath != "" && len(m) > 0 {
|
||||
return m
|
||||
}
|
||||
|
||||
for k, v := range interp.binPkg {
|
||||
if importPath != "" && k != importPath {
|
||||
continue
|
||||
}
|
||||
m[k] = v
|
||||
if importPath != "" {
|
||||
return m
|
||||
}
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
@@ -603,7 +637,7 @@ func (interp *Interpreter) getWrapper(t reflect.Type) reflect.Type {
|
||||
// they can be used in interpreted code.
|
||||
func (interp *Interpreter) Use(values Exports) {
|
||||
for k, v := range values {
|
||||
if k == hooksPath {
|
||||
if k == selfPrefix {
|
||||
interp.hooks.Parse(v)
|
||||
continue
|
||||
}
|
||||
@@ -785,20 +819,28 @@ func (interp *Interpreter) REPL() (reflect.Value, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func doPrompt(out io.Writer) func(v reflect.Value) {
|
||||
return func(v reflect.Value) {
|
||||
if v.IsValid() {
|
||||
fmt.Fprintln(out, ":", v)
|
||||
}
|
||||
fmt.Fprint(out, "> ")
|
||||
}
|
||||
}
|
||||
|
||||
// getPrompt returns a function which prints a prompt only if input is a terminal.
|
||||
func getPrompt(in io.Reader, out io.Writer) func(reflect.Value) {
|
||||
forcePrompt, _ := strconv.ParseBool(os.Getenv("YAEGI_PROMPT"))
|
||||
if forcePrompt {
|
||||
return doPrompt(out)
|
||||
}
|
||||
s, ok := in.(interface{ Stat() (os.FileInfo, error) })
|
||||
if !ok {
|
||||
return func(reflect.Value) {}
|
||||
}
|
||||
stat, err := s.Stat()
|
||||
if err == nil && stat.Mode()&os.ModeCharDevice != 0 {
|
||||
return func(v reflect.Value) {
|
||||
if v.IsValid() {
|
||||
fmt.Fprintln(out, ":", v)
|
||||
}
|
||||
fmt.Fprint(out, "> ")
|
||||
}
|
||||
return doPrompt(out)
|
||||
}
|
||||
return func(reflect.Value) {}
|
||||
}
|
||||
|
||||
@@ -43,6 +43,9 @@ func TestInterpConsistencyBuild(t *testing.T) {
|
||||
file.Name() == "for7.go" || // expect error
|
||||
file.Name() == "fun21.go" || // expect error
|
||||
file.Name() == "fun22.go" || // expect error
|
||||
file.Name() == "fun23.go" || // expect error
|
||||
file.Name() == "fun24.go" || // expect error
|
||||
file.Name() == "fun25.go" || // expect error
|
||||
file.Name() == "if2.go" || // expect error
|
||||
file.Name() == "import6.go" || // expect error
|
||||
file.Name() == "init1.go" || // expect error
|
||||
@@ -201,6 +204,11 @@ func TestInterpErrorConsistency(t *testing.T) {
|
||||
expectedInterp: "6:2: not enough arguments in call to time.Date",
|
||||
expectedExec: "6:11: not enough arguments in call to time.Date",
|
||||
},
|
||||
{
|
||||
fileName: "fun23.go",
|
||||
expectedInterp: "3:17: too many arguments to return",
|
||||
expectedExec: "3:17: too many arguments to return",
|
||||
},
|
||||
{
|
||||
fileName: "op1.go",
|
||||
expectedInterp: "5:2: invalid operation: mismatched types int and float64",
|
||||
|
||||
@@ -71,6 +71,27 @@ func TestEvalArithmetic(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestEvalShift(t *testing.T) {
|
||||
i := interp.New(interp.Options{})
|
||||
runTests(t, i, []testCase{
|
||||
{src: "a, b, m := uint32(1), uint32(2), uint32(0); m = a + (1 << b)", res: "5"},
|
||||
{src: "c := uint(1); d := uint(+(-(1 << c)))", res: "18446744073709551614"},
|
||||
{src: "e, f := uint32(0), uint32(0); f = 1 << -(e * 2)", res: "1"},
|
||||
{src: "p := uint(0xdead); byte((1 << (p & 7)) - 1)", res: "31"},
|
||||
{pre: func() { eval(t, i, "const k uint = 1 << 17") }, src: "int(k)", res: "131072"},
|
||||
})
|
||||
}
|
||||
|
||||
func TestOpVarConst(t *testing.T) {
|
||||
i := interp.New(interp.Options{})
|
||||
runTests(t, i, []testCase{
|
||||
{pre: func() { eval(t, i, "const a uint = 8 + 2") }, src: "a", res: "10"},
|
||||
{src: "b := uint(5); a+b", res: "15"},
|
||||
{src: "b := uint(5); b+a", res: "15"},
|
||||
{src: "b := uint(5); b>a", res: "false"},
|
||||
})
|
||||
}
|
||||
|
||||
func TestEvalStar(t *testing.T) {
|
||||
i := interp.New(interp.Options{})
|
||||
runTests(t, i, []testCase{
|
||||
@@ -89,6 +110,7 @@ func TestEvalAssign(t *testing.T) {
|
||||
{src: "f := int64(3.2)", err: "1:39: cannot convert expression of type float64 to type int64"},
|
||||
{src: "g := 1; g <<= 8", res: "256"},
|
||||
{src: "h := 1; h >>= 8", res: "0"},
|
||||
{src: "i := 1; j := &i; (*j) = 2", res: "2"},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -423,19 +445,15 @@ func TestEvalSliceExpression(t *testing.T) {
|
||||
{src: `a := []int{0,1,2}[:]`, res: "[0 1 2]"},
|
||||
{src: `a := []int{0,1,2,3}[1:3:4]`, res: "[1 2]"},
|
||||
{src: `a := []int{0,1,2,3}[:3:4]`, res: "[0 1 2]"},
|
||||
{src: `ar := [3]int{0,1,2}
|
||||
a := ar[1:3]`, res: "[1 2]"},
|
||||
{src: `ar := [3]int{0,1,2}; a := ar[1:3]`, res: "[1 2]"},
|
||||
{src: `a := (&[3]int{0,1,2})[1:3]`, res: "[1 2]"},
|
||||
{src: `a := (&[3]int{0,1,2})[1:3]`, res: "[1 2]"},
|
||||
{src: `s := "hello"[1:3]`, res: "el"},
|
||||
{src: `str := "hello"
|
||||
s := str[1:3]`, res: "el"},
|
||||
{src: `str := "hello"; s := str[1:3]`, res: "el"},
|
||||
{src: `a := int(1)[0:1]`, err: "1:33: cannot slice type int"},
|
||||
{src: `a := ([3]int{0,1,2})[1:3]`, err: "1:33: cannot slice type [3]int"},
|
||||
{src: `a := (&[]int{0,1,2,3})[1:3]`, err: "1:33: cannot slice type *[]int"},
|
||||
{src: `a := "hello"[1:3:4]`, err: "1:45: invalid operation: 3-index slice of string"},
|
||||
{src: `ar := [3]int{0,1,2}
|
||||
a := ar[:4]`, err: "2:16: index int is out of bounds"},
|
||||
{src: `ar := [3]int{0,1,2}; a := ar[:4]`, err: "1:58: index int is out of bounds"},
|
||||
{src: `a := []int{0,1,2,3}[1::4]`, err: "1:49: 2nd index required in 3-index slice"},
|
||||
{src: `a := []int{0,1,2,3}[1:3:]`, err: "1:51: 3rd index required in 3-index slice"},
|
||||
{src: `a := []int{0,1,2}[3:1]`, err: "invalid index values, must be low <= high <= max"},
|
||||
@@ -986,6 +1004,9 @@ func TestConcurrentEvals(t *testing.T) {
|
||||
// called by EvalWithContext is sequential. And that there is no data race for the
|
||||
// interp package global vars or the interpreter fields in this case.
|
||||
func TestConcurrentEvals2(t *testing.T) {
|
||||
if testing.Short() {
|
||||
return
|
||||
}
|
||||
pin, pout := io.Pipe()
|
||||
defer func() {
|
||||
_ = pin.Close()
|
||||
@@ -1045,6 +1066,9 @@ func TestConcurrentEvals2(t *testing.T) {
|
||||
// - when calling Interpreter.Use, the symbols given as argument should be
|
||||
// copied when being inserted into interp.binPkg, and not directly used as-is.
|
||||
func TestConcurrentEvals3(t *testing.T) {
|
||||
if testing.Short() {
|
||||
return
|
||||
}
|
||||
allDone := make(chan bool)
|
||||
runREPL := func() {
|
||||
done := make(chan error)
|
||||
@@ -1123,6 +1147,9 @@ func TestConcurrentComposite2(t *testing.T) {
|
||||
}
|
||||
|
||||
func testConcurrentComposite(t *testing.T, filePath string) {
|
||||
if testing.Short() {
|
||||
return
|
||||
}
|
||||
pin, pout := io.Pipe()
|
||||
i := interp.New(interp.Options{Stdout: pout})
|
||||
i.Use(stdlib.Symbols)
|
||||
@@ -1160,6 +1187,9 @@ func testConcurrentComposite(t *testing.T, filePath string) {
|
||||
}
|
||||
|
||||
func TestEvalScanner(t *testing.T) {
|
||||
if testing.Short() {
|
||||
return
|
||||
}
|
||||
type testCase struct {
|
||||
desc string
|
||||
src []string
|
||||
@@ -1331,3 +1361,94 @@ func applyCIMultiplier(timeout time.Duration) time.Duration {
|
||||
}
|
||||
return time.Duration(float64(timeout) * CITimeoutMultiplier)
|
||||
}
|
||||
|
||||
func TestREPLDivision(t *testing.T) {
|
||||
if testing.Short() {
|
||||
return
|
||||
}
|
||||
_ = os.Setenv("YAEGI_PROMPT", "1")
|
||||
defer func() {
|
||||
_ = os.Setenv("YAEGI_PROMPT", "0")
|
||||
}()
|
||||
allDone := make(chan bool)
|
||||
runREPL := func() {
|
||||
done := make(chan error)
|
||||
pinin, poutin := io.Pipe()
|
||||
pinout, poutout := io.Pipe()
|
||||
i := interp.New(interp.Options{Stdin: pinin, Stdout: poutout})
|
||||
i.Use(stdlib.Symbols)
|
||||
|
||||
go func() {
|
||||
_, _ = i.REPL()
|
||||
}()
|
||||
|
||||
defer func() {
|
||||
_ = pinin.Close()
|
||||
_ = poutin.Close()
|
||||
_ = pinout.Close()
|
||||
_ = poutout.Close()
|
||||
allDone <- true
|
||||
}()
|
||||
|
||||
input := []string{
|
||||
`1/1`,
|
||||
`7/3`,
|
||||
`16/5`,
|
||||
`3./2`, // float
|
||||
}
|
||||
output := []string{
|
||||
`1`,
|
||||
`2`,
|
||||
`3`,
|
||||
`1.5`,
|
||||
}
|
||||
|
||||
go func() {
|
||||
sc := bufio.NewScanner(pinout)
|
||||
k := 0
|
||||
for sc.Scan() {
|
||||
l := sc.Text()
|
||||
if l != "> : "+output[k] {
|
||||
done <- fmt.Errorf("unexpected output, want %q, got %q", output[k], l)
|
||||
return
|
||||
}
|
||||
k++
|
||||
if k > 3 {
|
||||
break
|
||||
}
|
||||
}
|
||||
done <- nil
|
||||
}()
|
||||
|
||||
for _, v := range input {
|
||||
in := strings.NewReader(v + "\n")
|
||||
if _, err := io.Copy(poutin, in); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
select {
|
||||
case err := <-done:
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return
|
||||
default:
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
if err := <-done; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
go func() {
|
||||
runREPL()
|
||||
}()
|
||||
|
||||
timeout := time.NewTimer(10 * time.Second)
|
||||
select {
|
||||
case <-allDone:
|
||||
case <-timeout.C:
|
||||
t.Fatal("timeout")
|
||||
}
|
||||
}
|
||||
|
||||
23
interp/op.go
23
interp/op.go
@@ -701,7 +701,16 @@ func quoConst(n *node) {
|
||||
n.rval = reflect.New(t).Elem()
|
||||
switch {
|
||||
case isConst:
|
||||
v := constant.BinaryOp(vConstantValue(v0), token.QUO, vConstantValue(v1))
|
||||
var operator token.Token
|
||||
// When the result of the operation is expected to be an int (because both
|
||||
// operands are ints), we want to force the type of the whole expression to be an
|
||||
// int (and not a float), which is achieved by using the QUO_ASSIGN operator.
|
||||
if n.typ.untyped && isInt(n.typ.rtype) {
|
||||
operator = token.QUO_ASSIGN
|
||||
} else {
|
||||
operator = token.QUO
|
||||
}
|
||||
v := constant.BinaryOp(vConstantValue(v0), operator, vConstantValue(v1))
|
||||
n.rval.Set(reflect.ValueOf(v))
|
||||
case isComplex(t):
|
||||
n.rval.SetComplex(vComplex(v0) / vComplex(v1))
|
||||
@@ -1957,10 +1966,10 @@ func bitNotConst(n *node) {
|
||||
case isConst:
|
||||
v := constant.UnaryOp(token.XOR, vConstantValue(v0), 0)
|
||||
n.rval.Set(reflect.ValueOf(v))
|
||||
case isInt(t):
|
||||
n.rval.SetInt(^v0.Int())
|
||||
case isUint(t):
|
||||
n.rval.SetUint(^v0.Uint())
|
||||
case isInt(t):
|
||||
n.rval.SetInt(^v0.Int())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1976,10 +1985,10 @@ func negConst(n *node) {
|
||||
case isConst:
|
||||
v := constant.UnaryOp(token.SUB, vConstantValue(v0), 0)
|
||||
n.rval.Set(reflect.ValueOf(v))
|
||||
case isInt(t):
|
||||
n.rval.SetInt(-v0.Int())
|
||||
case isUint(t):
|
||||
n.rval.SetUint(-v0.Uint())
|
||||
case isInt(t):
|
||||
n.rval.SetInt(-v0.Int())
|
||||
case isFloat(t):
|
||||
n.rval.SetFloat(-v0.Float())
|
||||
case isComplex(t):
|
||||
@@ -2015,10 +2024,10 @@ func posConst(n *node) {
|
||||
case isConst:
|
||||
v := constant.UnaryOp(token.ADD, vConstantValue(v0), 0)
|
||||
n.rval.Set(reflect.ValueOf(v))
|
||||
case isInt(t):
|
||||
n.rval.SetInt(+v0.Int())
|
||||
case isUint(t):
|
||||
n.rval.SetUint(+v0.Uint())
|
||||
case isInt(t):
|
||||
n.rval.SetInt(+v0.Int())
|
||||
case isFloat(t):
|
||||
n.rval.SetFloat(+v0.Float())
|
||||
case isComplex(t):
|
||||
|
||||
793
interp/run.go
793
interp/run.go
File diff suppressed because it is too large
Load Diff
@@ -72,26 +72,28 @@ type symbol struct {
|
||||
// execution to the index in frame, created exactly from the types layout.
|
||||
//
|
||||
type scope struct {
|
||||
anc *scope // Ancestor upper scope
|
||||
anc *scope // ancestor upper scope
|
||||
child []*scope // included scopes
|
||||
def *node // function definition node this scope belongs to, or nil
|
||||
loop *node // loop exit node for break statement
|
||||
loopRestart *node // loop restart node for continue statement
|
||||
pkgID string // unique id of package in which scope is defined
|
||||
types []reflect.Type // Frame layout, may be shared by same level scopes
|
||||
level int // Frame level: number of frame indirections to access var during execution
|
||||
sym map[string]*symbol // Map of symbols defined in this current scope
|
||||
types []reflect.Type // frame layout, may be shared by same level scopes
|
||||
level int // frame level: number of frame indirections to access var during execution
|
||||
sym map[string]*symbol // map of symbols defined in this current scope
|
||||
global bool // true if scope refers to global space (single frame for universe and package level scopes)
|
||||
iota int // iota value in this scope
|
||||
}
|
||||
|
||||
// push creates a new scope and chain it to the current one.
|
||||
// push creates a new child scope and chain it to the current one.
|
||||
func (s *scope) push(indirect bool) *scope {
|
||||
sc := scope{anc: s, level: s.level, sym: map[string]*symbol{}}
|
||||
sc := &scope{anc: s, level: s.level, sym: map[string]*symbol{}}
|
||||
s.child = append(s.child, sc)
|
||||
if indirect {
|
||||
sc.types = []reflect.Type{}
|
||||
sc.level = s.level + 1
|
||||
} else {
|
||||
// propagate size, types, def and global as scopes at same level share the same frame
|
||||
// Propagate size, types, def and global as scopes at same level share the same frame.
|
||||
sc.types = s.types
|
||||
sc.def = s.def
|
||||
sc.global = s.global
|
||||
@@ -99,7 +101,7 @@ func (s *scope) push(indirect bool) *scope {
|
||||
}
|
||||
// inherit loop state and pkgID from ancestor
|
||||
sc.loop, sc.loopRestart, sc.pkgID = s.loop, s.loopRestart, s.pkgID
|
||||
return &sc
|
||||
return sc
|
||||
}
|
||||
|
||||
func (s *scope) pushBloc() *scope { return s.push(false) }
|
||||
@@ -107,12 +109,20 @@ func (s *scope) pushFunc() *scope { return s.push(true) }
|
||||
|
||||
func (s *scope) pop() *scope {
|
||||
if s.level == s.anc.level {
|
||||
// propagate size and types, as scopes at same level share the same frame
|
||||
// Propagate size and types, as scopes at same level share the same frame.
|
||||
s.anc.types = s.types
|
||||
}
|
||||
return s.anc
|
||||
}
|
||||
|
||||
func (s *scope) upperLevel() *scope {
|
||||
level := s.level
|
||||
for s != nil && s.level == level {
|
||||
s = s.anc
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// lookup searches for a symbol in the current scope, and upper ones if not found
|
||||
// it returns the symbol, the number of indirections level from the current scope
|
||||
// and status (false if no result).
|
||||
@@ -120,6 +130,9 @@ func (s *scope) lookup(ident string) (*symbol, int, bool) {
|
||||
level := s.level
|
||||
for {
|
||||
if sym, ok := s.sym[ident]; ok {
|
||||
if sym.global {
|
||||
return sym, globalFrame, true
|
||||
}
|
||||
return sym, level - s.level, true
|
||||
}
|
||||
if s.anc == nil {
|
||||
@@ -130,6 +143,20 @@ func (s *scope) lookup(ident string) (*symbol, int, bool) {
|
||||
return nil, 0, false
|
||||
}
|
||||
|
||||
// lookdown searches for a symbol in the current scope and included ones, recursively.
|
||||
// It returns the first found symbol and true, or nil and false.
|
||||
func (s *scope) lookdown(ident string) (*symbol, bool) {
|
||||
if sym, ok := s.sym[ident]; ok {
|
||||
return sym, true
|
||||
}
|
||||
for _, c := range s.child {
|
||||
if sym, ok := c.lookdown(ident); ok {
|
||||
return sym, true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (s *scope) rangeChanType(n *node) *itype {
|
||||
if sym, _, found := s.lookup(n.child[1].ident); found {
|
||||
if t := sym.typ; len(n.child) == 3 && t != nil && (t.cat == chanT || t.cat == chanRecvT) {
|
||||
|
||||
@@ -33,17 +33,12 @@ func (interp *Interpreter) importSrc(rPath, importPath string, skipTest bool) (s
|
||||
rPath = "."
|
||||
}
|
||||
dir = filepath.Join(filepath.Dir(interp.name), rPath, importPath)
|
||||
} else {
|
||||
var root string
|
||||
if rPath == mainID {
|
||||
root, err = interp.rootFromSourceLocation()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
} else {
|
||||
root = rPath
|
||||
} else if dir, rPath, err = pkgDir(interp.context.GOPATH, rPath, importPath); err != nil {
|
||||
// Try again, assuming a root dir at the source location.
|
||||
if rPath, err = interp.rootFromSourceLocation(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if dir, rPath, err = pkgDir(interp.context.GOPATH, root, importPath); err != nil {
|
||||
if dir, rPath, err = pkgDir(interp.context.GOPATH, rPath, importPath); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
136
interp/type.go
136
interp/type.go
@@ -166,56 +166,68 @@ func nodeType(interp *Interpreter, sc *scope, n *node) (*itype, error) {
|
||||
|
||||
case arrayType:
|
||||
t.cat = arrayT
|
||||
if len(n.child) > 1 {
|
||||
v := n.child[0].rval
|
||||
switch {
|
||||
case v.IsValid():
|
||||
// constant size
|
||||
if isConstantValue(v.Type()) {
|
||||
c := v.Interface().(constant.Value)
|
||||
t.size = constToInt(c)
|
||||
} else {
|
||||
t.size = int(v.Int())
|
||||
}
|
||||
case n.child[0].kind == ellipsisExpr:
|
||||
// [...]T expression
|
||||
t.size = arrayTypeLen(n.anc)
|
||||
default:
|
||||
if sym, _, ok := sc.lookup(n.child[0].ident); ok {
|
||||
if sym.kind != constSym {
|
||||
return nil, n.child[0].cfgErrorf("non-constant array bound %q", n.child[0].ident)
|
||||
}
|
||||
// Resolve symbol to get size value
|
||||
if sym.typ != nil && sym.typ.cat == intT {
|
||||
if v, ok := sym.rval.Interface().(int); ok {
|
||||
t.size = v
|
||||
} else if c, ok := sym.rval.Interface().(constant.Value); ok {
|
||||
t.size = constToInt(c)
|
||||
} else {
|
||||
t.incomplete = true
|
||||
}
|
||||
} else {
|
||||
t.incomplete = true
|
||||
}
|
||||
} else {
|
||||
// Evaluate constant array size expression
|
||||
if _, err = interp.cfg(n.child[0], sc.pkgID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t.incomplete = true
|
||||
}
|
||||
}
|
||||
if t.val, err = nodeType(interp, sc, n.child[1]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t.sizedef = true
|
||||
t.incomplete = t.incomplete || t.val.incomplete
|
||||
} else {
|
||||
if t.val, err = nodeType(interp, sc, n.child[0]); err != nil {
|
||||
c0 := n.child[0]
|
||||
if len(n.child) == 1 {
|
||||
// Array size is not defined.
|
||||
if t.val, err = nodeType(interp, sc, c0); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t.incomplete = t.val.incomplete
|
||||
break
|
||||
}
|
||||
// Array size is defined.
|
||||
switch v := c0.rval; {
|
||||
case v.IsValid():
|
||||
// Size if defined by a constant litteral value.
|
||||
if isConstantValue(v.Type()) {
|
||||
c := v.Interface().(constant.Value)
|
||||
t.size = constToInt(c)
|
||||
} else {
|
||||
t.size = int(v.Int())
|
||||
}
|
||||
case c0.kind == ellipsisExpr:
|
||||
// [...]T expression, get size from the length of composite array.
|
||||
t.size = arrayTypeLen(n.anc)
|
||||
case c0.kind == identExpr:
|
||||
sym, _, ok := sc.lookup(c0.ident)
|
||||
if !ok {
|
||||
t.incomplete = true
|
||||
break
|
||||
}
|
||||
// Size is defined by a symbol which must be a constant integer.
|
||||
if sym.kind != constSym {
|
||||
return nil, c0.cfgErrorf("non-constant array bound %q", c0.ident)
|
||||
}
|
||||
if sym.typ == nil || sym.typ.cat != intT {
|
||||
t.incomplete = true
|
||||
break
|
||||
}
|
||||
if v, ok := sym.rval.Interface().(int); ok {
|
||||
t.size = v
|
||||
break
|
||||
}
|
||||
if c, ok := sym.rval.Interface().(constant.Value); ok {
|
||||
t.size = constToInt(c)
|
||||
break
|
||||
}
|
||||
t.incomplete = true
|
||||
default:
|
||||
// Size is defined by a numeric constant expression.
|
||||
if _, err = interp.cfg(c0, sc.pkgID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
v, ok := c0.rval.Interface().(constant.Value)
|
||||
if !ok {
|
||||
t.incomplete = true
|
||||
break
|
||||
}
|
||||
t.size = constToInt(v)
|
||||
}
|
||||
if t.val, err = nodeType(interp, sc, n.child[1]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t.sizedef = true
|
||||
t.incomplete = t.incomplete || t.val.incomplete
|
||||
|
||||
case basicLit:
|
||||
switch v := n.rval.Interface().(type) {
|
||||
@@ -545,12 +557,12 @@ func nodeType(interp *Interpreter, sc *scope, n *node) (*itype, error) {
|
||||
if v, ok := pkg[name]; ok {
|
||||
t.cat = valueT
|
||||
t.rtype = v.Type()
|
||||
if isBinType(v) { // a bin type is encoded as a pointer on nil value
|
||||
if isBinType(v) {
|
||||
// A bin type is encoded as a pointer on a typed nil value.
|
||||
t.rtype = t.rtype.Elem()
|
||||
}
|
||||
} else {
|
||||
err = n.cfgErrorf("undefined selector %s.%s", lt.path, name)
|
||||
panic(err)
|
||||
}
|
||||
case srcPkgT:
|
||||
pkg := interp.srcPkg[lt.path]
|
||||
@@ -725,7 +737,7 @@ func (t *itype) finalize() (*itype, error) {
|
||||
}
|
||||
|
||||
// ReferTo returns true if the type contains a reference to a
|
||||
// full type name. It allows to asses a type recursive status.
|
||||
// full type name. It allows to assess a type recursive status.
|
||||
func (t *itype) referTo(name string, seen map[*itype]bool) bool {
|
||||
if t.path+"/"+t.name == name {
|
||||
return true
|
||||
@@ -915,7 +927,23 @@ func (t *itype) assignableTo(o *itype) bool {
|
||||
if t.isNil() && o.hasNil() || o.isNil() && t.hasNil() {
|
||||
return true
|
||||
}
|
||||
return t.TypeOf().AssignableTo(o.TypeOf())
|
||||
|
||||
if t.TypeOf().AssignableTo(o.TypeOf()) {
|
||||
return true
|
||||
}
|
||||
|
||||
n := t.node
|
||||
if n == nil || !n.rval.IsValid() {
|
||||
return false
|
||||
}
|
||||
con, ok := n.rval.Interface().(constant.Value)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if con == nil || !isConstType(o) {
|
||||
return false
|
||||
}
|
||||
return representableConst(con, o.TypeOf())
|
||||
}
|
||||
|
||||
// convertibleTo returns true if t is convertible to o.
|
||||
@@ -924,7 +952,7 @@ func (t *itype) convertibleTo(o *itype) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// unsafe checkes
|
||||
// unsafe checks
|
||||
tt, ot := t.TypeOf(), o.TypeOf()
|
||||
if (tt.Kind() == reflect.Ptr || tt.Kind() == reflect.Uintptr) && ot.Kind() == reflect.UnsafePointer {
|
||||
return true
|
||||
@@ -1180,7 +1208,11 @@ func (t *itype) lookupBinField(name string) (s reflect.StructField, index []int,
|
||||
if !isStruct(t) {
|
||||
return
|
||||
}
|
||||
s, ok = t.TypeOf().FieldByName(name)
|
||||
rt := t.rtype
|
||||
if t.cat == valueT && rt.Kind() == reflect.Ptr {
|
||||
rt = rt.Elem()
|
||||
}
|
||||
s, ok = rt.FieldByName(name)
|
||||
if !ok {
|
||||
for i, f := range t.field {
|
||||
if f.embed {
|
||||
|
||||
@@ -91,7 +91,10 @@ func (check typecheck) addressExpr(n *node) error {
|
||||
case selectorExpr:
|
||||
c0 = c0.child[1]
|
||||
continue
|
||||
case indexExpr:
|
||||
case starExpr:
|
||||
c0 = c0.child[0]
|
||||
continue
|
||||
case indexExpr, sliceExpr:
|
||||
c := c0.child[0]
|
||||
if isArray(c.typ) || isMap(c.typ) {
|
||||
c0 = c
|
||||
@@ -101,7 +104,7 @@ func (check typecheck) addressExpr(n *node) error {
|
||||
found = true
|
||||
continue
|
||||
}
|
||||
return n.cfgErrorf("invalid operation: cannot take address of %s", c0.typ.id())
|
||||
return n.cfgErrorf("invalid operation: cannot take address of %s [kind: %s]", c0.typ.id(), kinds[c0.kind])
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -148,7 +151,7 @@ func (check typecheck) shift(n *node) error {
|
||||
t0, t1 := c0.typ.TypeOf(), c1.typ.TypeOf()
|
||||
|
||||
var v0 constant.Value
|
||||
if c0.typ.untyped {
|
||||
if c0.typ.untyped && c0.rval.IsValid() {
|
||||
v0 = constant.ToInt(c0.rval.Interface().(constant.Value))
|
||||
c0.rval = reflect.ValueOf(v0)
|
||||
}
|
||||
@@ -214,6 +217,7 @@ var binaryOpPredicates = opPredicates{
|
||||
// binaryExpr type checks a binary expression.
|
||||
func (check typecheck) binaryExpr(n *node) error {
|
||||
c0, c1 := n.child[0], n.child[1]
|
||||
|
||||
a := n.action
|
||||
if isAssignAction(a) {
|
||||
a--
|
||||
@@ -223,6 +227,21 @@ func (check typecheck) binaryExpr(n *node) error {
|
||||
return check.shift(n)
|
||||
}
|
||||
|
||||
switch n.action {
|
||||
case aRem:
|
||||
if zeroConst(c1) {
|
||||
return n.cfgErrorf("invalid operation: division by zero")
|
||||
}
|
||||
case aQuo:
|
||||
if zeroConst(c1) {
|
||||
return n.cfgErrorf("invalid operation: division by zero")
|
||||
}
|
||||
if c0.rval.IsValid() && c1.rval.IsValid() {
|
||||
// Avoid constant conversions below to ensure correct constant integer quotient.
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
_ = check.convertUntyped(c0, c1.typ)
|
||||
_ = check.convertUntyped(c1, c0.typ)
|
||||
|
||||
@@ -238,16 +257,13 @@ func (check typecheck) binaryExpr(n *node) error {
|
||||
if err := check.op(binaryOpPredicates, a, n, c0, t0); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch n.action {
|
||||
case aQuo, aRem:
|
||||
if (c0.typ.untyped || isInt(t0)) && c1.typ.untyped && constant.Sign(c1.rval.Interface().(constant.Value)) == 0 {
|
||||
return n.cfgErrorf("invalid operation: division by zero")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func zeroConst(n *node) bool {
|
||||
return n.typ.untyped && constant.Sign(n.rval.Interface().(constant.Value)) == 0
|
||||
}
|
||||
|
||||
func (check typecheck) index(n *node, max int) error {
|
||||
if err := check.convertUntyped(n, &itype{cat: intT, name: "int"}); err != nil {
|
||||
return err
|
||||
@@ -483,9 +499,7 @@ func (check typecheck) sliceExpr(n *node) error {
|
||||
case reflect.Array:
|
||||
valid = true
|
||||
l = t.Len()
|
||||
if c.kind != selectorExpr && (c.sym == nil || c.sym.kind != varSym) {
|
||||
return c.cfgErrorf("cannot slice type %s", c.typ.id())
|
||||
}
|
||||
// TODO(marc): check addressable status of array object (i.e. composite arrays are not).
|
||||
case reflect.Slice:
|
||||
valid = true
|
||||
case reflect.Ptr:
|
||||
@@ -618,16 +632,13 @@ func (check typecheck) conversion(n *node, typ *itype) error {
|
||||
if !ok {
|
||||
return n.cfgErrorf("cannot convert expression of type %s to type %s", n.typ.id(), typ.id())
|
||||
}
|
||||
|
||||
if n.typ.untyped {
|
||||
if isInterface(typ) || c != nil && !isConstType(typ) {
|
||||
typ = n.typ.defaultType()
|
||||
}
|
||||
if err := check.convertUntyped(n, typ); err != nil {
|
||||
return err
|
||||
}
|
||||
if !n.typ.untyped || c == nil {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
if isInterface(typ) || !isConstType(typ) {
|
||||
typ = n.typ.defaultType()
|
||||
}
|
||||
return check.convertUntyped(n, typ)
|
||||
}
|
||||
|
||||
type param struct {
|
||||
@@ -1040,24 +1051,10 @@ func (check typecheck) convertUntyped(n *node, typ *itype) error {
|
||||
return convErr
|
||||
}
|
||||
|
||||
isFloatToIntDivision := false
|
||||
if err := check.representable(n, rtyp); err != nil {
|
||||
if !isInt(rtyp) || n.action != aQuo {
|
||||
return err
|
||||
}
|
||||
// retry in the case of a division, and pretend we want a float. Because if we
|
||||
// can represent a float, then it follows that we can represent the integer
|
||||
// part of that float as an int.
|
||||
if err := check.representable(n, reflect.TypeOf(1.0)); err != nil {
|
||||
return err
|
||||
}
|
||||
isFloatToIntDivision = true
|
||||
}
|
||||
if isFloatToIntDivision {
|
||||
n.rval, err = check.convertConstFloatToInt(n.rval)
|
||||
} else {
|
||||
n.rval, err = check.convertConst(n.rval, rtyp)
|
||||
return err
|
||||
}
|
||||
n.rval, err = check.convertConst(n.rval, rtyp)
|
||||
if err != nil {
|
||||
if errors.Is(err, errCantConvert) {
|
||||
return convErr
|
||||
@@ -1099,22 +1096,6 @@ func (check typecheck) representable(n *node, t reflect.Type) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (check typecheck) convertConstFloatToInt(v reflect.Value) (reflect.Value, error) {
|
||||
if !v.IsValid() {
|
||||
return v, errors.New("invalid float reflect value")
|
||||
}
|
||||
c, ok := v.Interface().(constant.Value)
|
||||
if !ok {
|
||||
return v, errors.New("unexpected non-constant value")
|
||||
}
|
||||
|
||||
if constant.ToFloat(c).Kind() != constant.Float {
|
||||
return v, errors.New("const value cannot be converted to float")
|
||||
}
|
||||
fl, _ := constant.Float64Val(c)
|
||||
return reflect.ValueOf(int(fl)).Convert(reflect.TypeOf(1.0)), nil
|
||||
}
|
||||
|
||||
func (check typecheck) convertConst(v reflect.Value, t reflect.Type) (reflect.Value, error) {
|
||||
if !v.IsValid() {
|
||||
// TODO(nick): This should be an error as the const is in the frame which is undesirable.
|
||||
|
||||
@@ -5,8 +5,15 @@ import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
const (
|
||||
notInFrame = -1 // value of node.findex for literal values (not in frame)
|
||||
globalFrame = -1 // value of node.level for global symbols
|
||||
)
|
||||
|
||||
func valueGenerator(n *node, i int) func(*frame) reflect.Value {
|
||||
switch n.level {
|
||||
case globalFrame:
|
||||
return func(f *frame) reflect.Value { return valueOf(f.root.data, i) }
|
||||
case 0:
|
||||
return func(f *frame) reflect.Value { return valueOf(f.data, i) }
|
||||
case 1:
|
||||
@@ -33,6 +40,30 @@ func valueOf(data []reflect.Value, i int) reflect.Value {
|
||||
return reflect.Value{}
|
||||
}
|
||||
|
||||
func genValueBinMethodOnInterface(n *node, defaultGen func(*frame) reflect.Value) func(*frame) reflect.Value {
|
||||
if n == nil || n.child == nil || n.child[0] == nil ||
|
||||
n.child[0].child == nil || n.child[0].child[0] == nil {
|
||||
return defaultGen
|
||||
}
|
||||
if n.child[0].child[1] == nil || n.child[0].child[1].ident == "" {
|
||||
return defaultGen
|
||||
}
|
||||
value0 := genValue(n.child[0].child[0])
|
||||
|
||||
return func(f *frame) reflect.Value {
|
||||
val, ok := value0(f).Interface().(valueInterface)
|
||||
if !ok {
|
||||
return defaultGen(f)
|
||||
}
|
||||
typ := val.node.typ
|
||||
if typ.node != nil || typ.cat != valueT {
|
||||
return defaultGen(f)
|
||||
}
|
||||
meth, _ := typ.rtype.MethodByName(n.child[0].child[1].ident)
|
||||
return meth.Func
|
||||
}
|
||||
}
|
||||
|
||||
func genValueRecvIndirect(n *node) func(*frame) reflect.Value {
|
||||
v := genValueRecv(n)
|
||||
return func(f *frame) reflect.Value { return v(f).Elem() }
|
||||
@@ -55,6 +86,35 @@ func genValueRecv(n *node) func(*frame) reflect.Value {
|
||||
}
|
||||
}
|
||||
|
||||
func genValueBinRecv(n *node, recv *receiver) func(*frame) reflect.Value {
|
||||
value := genValue(n)
|
||||
binValue := genValue(recv.node)
|
||||
|
||||
v := func(f *frame) reflect.Value {
|
||||
if def, ok := value(f).Interface().(*node); ok {
|
||||
if def != nil && def.recv != nil && def.recv.val.IsValid() {
|
||||
return def.recv.val
|
||||
}
|
||||
}
|
||||
|
||||
ival, _ := binValue(f).Interface().(valueInterface)
|
||||
return ival.value
|
||||
}
|
||||
|
||||
fi := recv.index
|
||||
if len(fi) == 0 {
|
||||
return v
|
||||
}
|
||||
|
||||
return func(f *frame) reflect.Value {
|
||||
r := v(f)
|
||||
if r.Kind() == reflect.Ptr {
|
||||
r = r.Elem()
|
||||
}
|
||||
return r.FieldByIndex(fi)
|
||||
}
|
||||
}
|
||||
|
||||
func genValueRecvInterfacePtr(n *node) func(*frame) reflect.Value {
|
||||
v := genValue(n.recv.node)
|
||||
fi := n.recv.index
|
||||
@@ -118,18 +178,16 @@ func genValue(n *node) func(*frame) reflect.Value {
|
||||
return func(f *frame) reflect.Value { return v }
|
||||
}
|
||||
if n.sym != nil {
|
||||
if n.sym.index < 0 {
|
||||
i := n.sym.index
|
||||
if i < 0 {
|
||||
return genValue(n.sym.node)
|
||||
}
|
||||
i := n.sym.index
|
||||
if n.sym.global {
|
||||
return func(f *frame) reflect.Value {
|
||||
return n.interp.frame.data[i]
|
||||
}
|
||||
return func(f *frame) reflect.Value { return f.root.data[i] }
|
||||
}
|
||||
return valueGenerator(n, i)
|
||||
}
|
||||
if n.findex < 0 {
|
||||
if n.findex == notInFrame {
|
||||
var v reflect.Value
|
||||
if w, ok := n.val.(reflect.Value); ok {
|
||||
v = w
|
||||
@@ -244,6 +302,17 @@ func genValueOutput(n *node, t reflect.Type) func(*frame) reflect.Value {
|
||||
return value
|
||||
}
|
||||
|
||||
func valueInterfaceValue(v reflect.Value) reflect.Value {
|
||||
for {
|
||||
vv, ok := v.Interface().(valueInterface)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
v = vv.value
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func genValueInterfaceValue(n *node) func(*frame) reflect.Value {
|
||||
value := genValue(n)
|
||||
|
||||
@@ -254,7 +323,7 @@ func genValueInterfaceValue(n *node) func(*frame) reflect.Value {
|
||||
v.Set(zeroInterfaceValue())
|
||||
v = value(f)
|
||||
}
|
||||
return v.Interface().(valueInterface).value
|
||||
return valueInterfaceValue(v)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -342,6 +411,10 @@ func vInt(v reflect.Value) (i int64) {
|
||||
}
|
||||
|
||||
func vUint(v reflect.Value) (i uint64) {
|
||||
if c := vConstantValue(v); c != nil {
|
||||
i, _ = constant.Uint64Val(constant.ToInt(c))
|
||||
return i
|
||||
}
|
||||
switch v.Type().Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
i = uint64(v.Int())
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by 'yaegi extract archive/tar'. DO NOT EDIT.
|
||||
|
||||
// +build go1.15,!go1.16
|
||||
// +build go1.15
|
||||
|
||||
package stdlib
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by 'yaegi extract archive/zip'. DO NOT EDIT.
|
||||
|
||||
// +build go1.15,!go1.16
|
||||
// +build go1.15
|
||||
|
||||
package stdlib
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by 'yaegi extract bufio'. DO NOT EDIT.
|
||||
|
||||
// +build go1.15,!go1.16
|
||||
// +build go1.15
|
||||
|
||||
package stdlib
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by 'yaegi extract bytes'. DO NOT EDIT.
|
||||
|
||||
// +build go1.15,!go1.16
|
||||
// +build go1.15
|
||||
|
||||
package stdlib
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by 'yaegi extract compress/bzip2'. DO NOT EDIT.
|
||||
|
||||
// +build go1.15,!go1.16
|
||||
// +build go1.15
|
||||
|
||||
package stdlib
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by 'yaegi extract compress/flate'. DO NOT EDIT.
|
||||
|
||||
// +build go1.15,!go1.16
|
||||
// +build go1.15
|
||||
|
||||
package stdlib
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by 'yaegi extract compress/gzip'. DO NOT EDIT.
|
||||
|
||||
// +build go1.15,!go1.16
|
||||
// +build go1.15
|
||||
|
||||
package stdlib
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by 'yaegi extract compress/lzw'. DO NOT EDIT.
|
||||
|
||||
// +build go1.15,!go1.16
|
||||
// +build go1.15
|
||||
|
||||
package stdlib
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by 'yaegi extract compress/zlib'. DO NOT EDIT.
|
||||
|
||||
// +build go1.15,!go1.16
|
||||
// +build go1.15
|
||||
|
||||
package stdlib
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by 'yaegi extract container/heap'. DO NOT EDIT.
|
||||
|
||||
// +build go1.15,!go1.16
|
||||
// +build go1.15
|
||||
|
||||
package stdlib
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by 'yaegi extract container/list'. DO NOT EDIT.
|
||||
|
||||
// +build go1.15,!go1.16
|
||||
// +build go1.15
|
||||
|
||||
package stdlib
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by 'yaegi extract container/ring'. DO NOT EDIT.
|
||||
|
||||
// +build go1.15,!go1.16
|
||||
// +build go1.15
|
||||
|
||||
package stdlib
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by 'yaegi extract context'. DO NOT EDIT.
|
||||
|
||||
// +build go1.15,!go1.16
|
||||
// +build go1.15
|
||||
|
||||
package stdlib
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by 'yaegi extract crypto'. DO NOT EDIT.
|
||||
|
||||
// +build go1.15,!go1.16
|
||||
// +build go1.15
|
||||
|
||||
package stdlib
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by 'yaegi extract crypto/aes'. DO NOT EDIT.
|
||||
|
||||
// +build go1.15,!go1.16
|
||||
// +build go1.15
|
||||
|
||||
package stdlib
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by 'yaegi extract crypto/cipher'. DO NOT EDIT.
|
||||
|
||||
// +build go1.15,!go1.16
|
||||
// +build go1.15
|
||||
|
||||
package stdlib
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user