Compare commits
67 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6aa4f45c42 | ||
|
|
f3dbce93a4 | ||
|
|
0e3ea5732a | ||
|
|
1679870ea3 | ||
|
|
9b4ea62f69 | ||
|
|
eee72d1aae | ||
|
|
97cf8c4210 | ||
|
|
7bb8b4631f | ||
|
|
9f43170708 | ||
|
|
71112dbe87 | ||
|
|
4a8093609f | ||
|
|
7865c90737 | ||
|
|
e4e3d11772 | ||
|
|
a5242cbb9e | ||
|
|
c4d1bf5029 | ||
|
|
e003140c6e | ||
|
|
6b8c94e6c4 | ||
|
|
143e4a4559 | ||
|
|
dfeddbe823 | ||
|
|
021824930d | ||
|
|
b8301f10a8 | ||
|
|
2e8808317f | ||
|
|
79747f3d6f | ||
|
|
63825e7201 | ||
|
|
03ccda1a69 | ||
|
|
e02621577f | ||
|
|
ab869c8d20 | ||
|
|
b2aa636ea0 | ||
|
|
ae725fb3d9 | ||
|
|
14bc3b56b8 | ||
|
|
255b1cf1de | ||
|
|
d3fc5e990e | ||
|
|
dc082b5ded | ||
|
|
d9c402e20d | ||
|
|
09a1617640 | ||
|
|
cb642c44ba | ||
|
|
f76db27c77 | ||
|
|
996b1e33c8 | ||
|
|
236a0effaf | ||
|
|
eaeb445e17 | ||
|
|
6933ba2b4e | ||
|
|
a61a7d5bcd | ||
|
|
259f64cfd4 | ||
|
|
6c74ab7bec | ||
|
|
d64563edee | ||
|
|
07039262a0 | ||
|
|
4ed9ccb5c4 | ||
|
|
25edcfee7a | ||
|
|
d183f4205e | ||
|
|
821e9ee006 | ||
|
|
00e3f924c1 | ||
|
|
2248851d77 | ||
|
|
f74d1ea6d8 | ||
|
|
606b4c3a37 | ||
|
|
4e77fc9436 | ||
|
|
ad9db379e7 | ||
|
|
7be17d393f | ||
|
|
5665c9a410 | ||
|
|
1cf9d345aa | ||
|
|
f07f25f1ba | ||
|
|
c93b836c77 | ||
|
|
371103f0d1 | ||
|
|
8bd7afbe62 | ||
|
|
8ea3a493f4 | ||
|
|
f2abd346c0 | ||
|
|
c784713aca | ||
|
|
14acf618af |
5
.github/workflows/go-cross.yml
vendored
5
.github/workflows/go-cross.yml
vendored
@@ -17,7 +17,7 @@ jobs:
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
go-version: [ 1.16, 1.17 ]
|
||||
go-version: [ 1.19, '1.20' ]
|
||||
os: [ubuntu-latest, macos-latest, windows-latest]
|
||||
|
||||
include:
|
||||
@@ -34,6 +34,7 @@ jobs:
|
||||
uses: actions/setup-go@v2
|
||||
with:
|
||||
go-version: ${{ matrix.go-version }}
|
||||
stable: true
|
||||
|
||||
# https://github.com/marketplace/actions/checkout
|
||||
- name: Checkout code
|
||||
@@ -43,7 +44,7 @@ jobs:
|
||||
|
||||
# https://github.com/marketplace/actions/cache
|
||||
- name: Cache Go modules
|
||||
uses: actions/cache@v2
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
# In order:
|
||||
# * Module download cache
|
||||
|
||||
12
.github/workflows/main.yml
vendored
12
.github/workflows/main.yml
vendored
@@ -7,8 +7,8 @@ on:
|
||||
pull_request:
|
||||
|
||||
env:
|
||||
GO_VERSION: 1.17
|
||||
GOLANGCI_LINT_VERSION: v1.42.1
|
||||
GO_VERSION: 1.19
|
||||
GOLANGCI_LINT_VERSION: v1.47.1
|
||||
|
||||
jobs:
|
||||
|
||||
@@ -45,12 +45,13 @@ jobs:
|
||||
needs: linting
|
||||
strategy:
|
||||
matrix:
|
||||
go-version: [ 1.16, 1.17 ]
|
||||
go-version: [ 1.19, '1.20' ]
|
||||
steps:
|
||||
- name: Set up Go ${{ matrix.go-version }}
|
||||
uses: actions/setup-go@v2
|
||||
with:
|
||||
go-version: ${{ matrix.go-version }}
|
||||
stable: true
|
||||
|
||||
- name: Check out code
|
||||
uses: actions/checkout@v2
|
||||
@@ -75,13 +76,14 @@ jobs:
|
||||
working-directory: ${{ github.workspace }}/go/src/github.com/traefik/yaegi
|
||||
strategy:
|
||||
matrix:
|
||||
go-version: [ 1.16, 1.17 ]
|
||||
go-version: [ 1.19, '1.20' ]
|
||||
|
||||
steps:
|
||||
- name: Set up Go ${{ matrix.go-version }}
|
||||
uses: actions/setup-go@v2
|
||||
with:
|
||||
go-version: ${{ matrix.go-version }}
|
||||
stable: true
|
||||
|
||||
- name: Check out code
|
||||
uses: actions/checkout@v2
|
||||
@@ -91,7 +93,7 @@ jobs:
|
||||
|
||||
# https://github.com/marketplace/actions/cache
|
||||
- name: Cache Go modules
|
||||
uses: actions/cache@v2
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: ./_test/tmp
|
||||
key: ${{ runner.os }}-yaegi-${{ hashFiles('**//_test/tmp/') }}
|
||||
|
||||
4
.github/workflows/release.yml
vendored
4
.github/workflows/release.yml
vendored
@@ -6,7 +6,7 @@ on:
|
||||
- v[0-9]+.[0-9]+*
|
||||
|
||||
env:
|
||||
GO_VERSION: 1.17
|
||||
GO_VERSION: '1.20'
|
||||
|
||||
jobs:
|
||||
|
||||
@@ -26,7 +26,7 @@ jobs:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Cache Go modules
|
||||
uses: actions/cache@v2
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: ~/go/pkg/mod
|
||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
"scopelint", # deprecated
|
||||
"interfacer", # deprecated
|
||||
"maligned", # deprecated
|
||||
"exhaustivestruct", # deprecated
|
||||
"lll",
|
||||
"gas",
|
||||
"dupl",
|
||||
@@ -49,10 +50,15 @@
|
||||
"wrapcheck",
|
||||
"nestif",
|
||||
"exhaustive",
|
||||
"exhaustivestruct",
|
||||
"exhaustruct",
|
||||
"forbidigo",
|
||||
"ifshort",
|
||||
"forcetypeassert",
|
||||
"varnamelen",
|
||||
"nosnakecase",
|
||||
"nonamedreturns",
|
||||
"nilnil",
|
||||
"maintidx",
|
||||
"errorlint", # TODO: must be reactivate before fixes
|
||||
]
|
||||
|
||||
@@ -81,3 +87,6 @@
|
||||
[[issues.exclude-rules]]
|
||||
path = "interp/interp_eval_test.go"
|
||||
linters = ["thelper"]
|
||||
[[issues.exclude-rules]]
|
||||
path = "interp/debugger.go"
|
||||
linters = ["containedctx"]
|
||||
|
||||
@@ -18,7 +18,7 @@ It powers executable Go scripts and plugins, in embedded interpreters or interac
|
||||
* Works everywhere Go works
|
||||
* All Go & runtime resources accessible from script (with control)
|
||||
* Security: `unsafe` and `syscall` packages neither used nor exported by default
|
||||
* Support Go 1.16 and Go 1.17 (the latest 2 major releases)
|
||||
* Support Go 1.18 and Go 1.19 (the latest 2 major releases)
|
||||
|
||||
## Install
|
||||
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
package alias3
|
||||
|
||||
type T struct {
|
||||
A string
|
||||
}
|
||||
|
||||
func (t *T) Print() {
|
||||
println(t.A)
|
||||
}
|
||||
@@ -5,8 +5,8 @@ import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Defined an interface of stringBuilder that compatible with
|
||||
// strings.Builder(go 1.10) and bytes.Buffer(< go 1.10)
|
||||
// Define an interface of stringBuilder that is compatible with
|
||||
// strings.Builder(go 1.10) and bytes.Buffer(< go 1.10).
|
||||
type stringBuilder interface {
|
||||
WriteRune(r rune) (n int, err error)
|
||||
WriteString(s string) (int, error)
|
||||
|
||||
21
_test/assign17.go
Normal file
21
_test/assign17.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
s := make([]map[string]string, 0)
|
||||
m := make(map[string]string)
|
||||
m["m1"] = "m1"
|
||||
m["m2"] = "m2"
|
||||
s = append(s, m)
|
||||
tmpStr := "start"
|
||||
println(tmpStr)
|
||||
for _, v := range s {
|
||||
tmpStr, ok := v["m1"]
|
||||
println(tmpStr, ok)
|
||||
}
|
||||
println(tmpStr)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// start
|
||||
// m1 true
|
||||
// start
|
||||
21
_test/assign18.go
Normal file
21
_test/assign18.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
s := make([]map[string]string, 0)
|
||||
m := make(map[string]string)
|
||||
m["m1"] = "m1"
|
||||
m["m2"] = "m2"
|
||||
s = append(s, m)
|
||||
tmpStr := "start"
|
||||
println(tmpStr)
|
||||
for _, v := range s {
|
||||
tmpStr, _ := v["m1"]
|
||||
println(tmpStr)
|
||||
}
|
||||
println(tmpStr)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// start
|
||||
// m1
|
||||
// start
|
||||
24
_test/break0.go
Normal file
24
_test/break0.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
n := 2
|
||||
m := 2
|
||||
foo := true
|
||||
OuterLoop:
|
||||
println("Boo")
|
||||
for i := 0; i < n; i++ {
|
||||
println("I: ", i)
|
||||
for j := 0; j < m; j++ {
|
||||
switch foo {
|
||||
case true:
|
||||
println(foo)
|
||||
break OuterLoop
|
||||
case false:
|
||||
println(foo)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Error:
|
||||
// 15:5: invalid break label OuterLoop
|
||||
24
_test/break1.go
Normal file
24
_test/break1.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
n := 2
|
||||
m := 2
|
||||
foo := true
|
||||
OuterLoop:
|
||||
for i := 0; i < n; i++ {
|
||||
println("I: ", i)
|
||||
for j := 0; j < m; j++ {
|
||||
switch foo {
|
||||
case true:
|
||||
println(foo)
|
||||
break OuterLoop
|
||||
case false:
|
||||
println(foo)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// I: 0
|
||||
// true
|
||||
25
_test/break2.go
Normal file
25
_test/break2.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
n := 2
|
||||
m := 2
|
||||
foo := true
|
||||
OuterLoop:
|
||||
for i := 0; i < n; i++ {
|
||||
println("I: ", i)
|
||||
for j := 0; j < m; j++ {
|
||||
switch foo {
|
||||
case true:
|
||||
println(foo)
|
||||
break OuterLoop
|
||||
case false:
|
||||
println(foo)
|
||||
continue OuterLoop
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// I: 0
|
||||
// true
|
||||
27
_test/break3.go
Normal file
27
_test/break3.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
n := 2
|
||||
m := 2
|
||||
foo := true
|
||||
goto OuterLoop
|
||||
println("Boo")
|
||||
OuterLoop:
|
||||
for i := 0; i < n; i++ {
|
||||
println("I: ", i)
|
||||
for j := 0; j < m; j++ {
|
||||
switch foo {
|
||||
case true:
|
||||
println(foo)
|
||||
break OuterLoop
|
||||
case false:
|
||||
println(foo)
|
||||
goto OuterLoop
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// I: 0
|
||||
// true
|
||||
56
_test/cli7.go
Normal file
56
_test/cli7.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
)
|
||||
|
||||
type T struct {
|
||||
http.ResponseWriter
|
||||
}
|
||||
|
||||
type mw1 struct {
|
||||
}
|
||||
|
||||
var obj = map[string]interface{}{}
|
||||
|
||||
func (m *mw1) ServeHTTP(rw http.ResponseWriter, rq *http.Request) {
|
||||
t := &T{
|
||||
ResponseWriter: rw,
|
||||
}
|
||||
x := t.Header()
|
||||
i := obj["m1"].(*mw1)
|
||||
fmt.Fprint(rw, "Welcome to my website!", x, i)
|
||||
}
|
||||
|
||||
func main() {
|
||||
m1 := &mw1{}
|
||||
|
||||
obj["m1"] = m1
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/", m1.ServeHTTP)
|
||||
|
||||
server := httptest.NewServer(mux)
|
||||
defer server.Close()
|
||||
|
||||
client(server.URL)
|
||||
}
|
||||
|
||||
func client(uri string) {
|
||||
resp, err := http.Get(uri)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(string(body))
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Welcome to my website!map[] &{}
|
||||
41
_test/cli8.go
Normal file
41
_test/cli8.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
)
|
||||
|
||||
type T struct {
|
||||
name string
|
||||
next http.Handler
|
||||
}
|
||||
|
||||
func (t *T) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
println("in T.ServeHTTP")
|
||||
if t.next != nil {
|
||||
t.next.ServeHTTP(rw, req)
|
||||
}
|
||||
}
|
||||
|
||||
func New(name string, next http.Handler) (http.Handler, error) { return &T{name, next}, nil }
|
||||
|
||||
func main() {
|
||||
next := func(rw http.ResponseWriter, req *http.Request) {
|
||||
println("in next")
|
||||
}
|
||||
|
||||
t, err := New("test", http.HandlerFunc(next))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
t.ServeHTTP(recorder, req)
|
||||
println(recorder.Result().Status)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// in T.ServeHTTP
|
||||
// in next
|
||||
// 200 OK
|
||||
26
_test/cont2.go
Normal file
26
_test/cont2.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
n := 2
|
||||
m := 2
|
||||
foo := true
|
||||
OuterLoop:
|
||||
for i := 0; i < n; i++ {
|
||||
println("I: ", i)
|
||||
for j := 0; j < m; j++ {
|
||||
switch foo {
|
||||
case true:
|
||||
println(foo)
|
||||
continue OuterLoop
|
||||
case false:
|
||||
println(foo)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// I: 0
|
||||
// true
|
||||
// I: 1
|
||||
// true
|
||||
24
_test/cont3.go
Normal file
24
_test/cont3.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
n := 2
|
||||
m := 2
|
||||
foo := true
|
||||
OuterLoop:
|
||||
println("boo")
|
||||
for i := 0; i < n; i++ {
|
||||
println("I: ", i)
|
||||
for j := 0; j < m; j++ {
|
||||
switch foo {
|
||||
case true:
|
||||
println(foo)
|
||||
continue OuterLoop
|
||||
case false:
|
||||
println(foo)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Error:
|
||||
// 15:5: invalid continue label OuterLoop
|
||||
18
_test/convert3.go
Normal file
18
_test/convert3.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
next := func(rw http.ResponseWriter, req *http.Request) {
|
||||
rw.Header().Set("Cache-Control", "max-age=20")
|
||||
rw.WriteHeader(http.StatusOK)
|
||||
}
|
||||
f := http.HandlerFunc(next)
|
||||
fmt.Printf("%T\n", f.ServeHTTP)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// func(http.ResponseWriter, *http.Request)
|
||||
25
_test/fun28.go
Normal file
25
_test/fun28.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
)
|
||||
|
||||
type T struct {
|
||||
name string
|
||||
}
|
||||
|
||||
func finalize(t *T) { println("finalize") }
|
||||
|
||||
func newT() *T {
|
||||
t := new(T)
|
||||
runtime.SetFinalizer(t, finalize)
|
||||
return t
|
||||
}
|
||||
|
||||
func main() {
|
||||
t := newT()
|
||||
println(t != nil)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// true
|
||||
39
_test/gen1.go
Normal file
39
_test/gen1.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// SumInts adds together the values of m.
|
||||
func SumInts(m map[string]int64) int64 {
|
||||
var s int64
|
||||
for _, v := range m {
|
||||
s += v
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// SumFloats adds together the values of m.
|
||||
func SumFloats(m map[string]float64) float64 {
|
||||
var s float64
|
||||
for _, v := range m {
|
||||
s += v
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Initialize a map for the integer values
|
||||
ints := map[string]int64{
|
||||
"first": 34,
|
||||
"second": 12,
|
||||
}
|
||||
|
||||
// Initialize a map for the float values
|
||||
floats := map[string]float64{
|
||||
"first": 35.98,
|
||||
"second": 26.99,
|
||||
}
|
||||
|
||||
fmt.Printf("Non-Generic Sums: %v and %v\n",
|
||||
SumInts(ints),
|
||||
SumFloats(floats))
|
||||
}
|
||||
12
_test/gen10.go
Normal file
12
_test/gen10.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
func genFunc() (f func()) {
|
||||
return f
|
||||
}
|
||||
|
||||
func main() {
|
||||
println(genFunc() == nil)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// true
|
||||
33
_test/gen11.go
Normal file
33
_test/gen11.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/netip"
|
||||
)
|
||||
|
||||
type Slice[T any] struct {
|
||||
x []T
|
||||
}
|
||||
|
||||
type IPPrefixSlice struct {
|
||||
x Slice[netip.Prefix]
|
||||
}
|
||||
|
||||
func (v Slice[T]) MarshalJSON() ([]byte, error) { return json.Marshal(v.x) }
|
||||
|
||||
// MarshalJSON implements json.Marshaler.
|
||||
func (v IPPrefixSlice) MarshalJSON() ([]byte, error) {
|
||||
return v.x.MarshalJSON()
|
||||
}
|
||||
|
||||
func main() {
|
||||
t := IPPrefixSlice{}
|
||||
fmt.Println(t)
|
||||
b, e := t.MarshalJSON()
|
||||
fmt.Println(string(b), e)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// {{[]}}
|
||||
// null <nil>
|
||||
31
_test/gen12.go
Normal file
31
_test/gen12.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func MapOf[K comparable, V any](m map[K]V) Map[K, V] {
|
||||
return Map[K, V]{m}
|
||||
}
|
||||
|
||||
type Map[K comparable, V any] struct {
|
||||
ж map[K]V
|
||||
}
|
||||
|
||||
func (v MapView) Int() Map[string, int] { return MapOf(v.ж.Int) }
|
||||
|
||||
type VMap struct {
|
||||
Int map[string]int
|
||||
}
|
||||
|
||||
type MapView struct {
|
||||
ж *VMap
|
||||
}
|
||||
|
||||
func main() {
|
||||
mv := MapView{&VMap{}}
|
||||
fmt.Println(mv.ж)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// &{map[]}
|
||||
18
_test/gen13.go
Normal file
18
_test/gen13.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
type Map[K comparable, V any] struct {
|
||||
ж map[K]V
|
||||
}
|
||||
|
||||
func (m Map[K, V]) Has(k K) bool {
|
||||
_, ok := m.ж[k]
|
||||
return ok
|
||||
}
|
||||
|
||||
func main() {
|
||||
m := Map[string, float64]{}
|
||||
println(m.Has("test"))
|
||||
}
|
||||
|
||||
// Output:
|
||||
// false
|
||||
34
_test/gen2.go
Normal file
34
_test/gen2.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// SumIntsOrFloats sums the values of map m. It supports both int64 and float64
|
||||
// as types for map values.
|
||||
func SumIntsOrFloats[K comparable, V int64 | float64](m map[K]V) V {
|
||||
var s V
|
||||
for _, v := range m {
|
||||
s += v
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Initialize a map for the integer values
|
||||
ints := map[string]int64{
|
||||
"first": 34,
|
||||
"second": 12,
|
||||
}
|
||||
|
||||
// Initialize a map for the float values
|
||||
floats := map[string]float64{
|
||||
"first": 35.98,
|
||||
"second": 26.99,
|
||||
}
|
||||
|
||||
fmt.Printf("Generic Sums: %v and %v\n",
|
||||
SumIntsOrFloats[string, int64](ints),
|
||||
SumIntsOrFloats[string, float64](floats))
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Generic Sums: 46 and 62.97
|
||||
22
_test/gen3.go
Normal file
22
_test/gen3.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
type Number interface {
|
||||
int | int64 | ~float64
|
||||
}
|
||||
|
||||
func Sum[T Number](numbers []T) T {
|
||||
var total T
|
||||
for _, x := range numbers {
|
||||
total += x
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
func main() {
|
||||
xs := []int{3, 5, 10}
|
||||
total := Sum(xs)
|
||||
println(total)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 18
|
||||
42
_test/gen4.go
Normal file
42
_test/gen4.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type List[T any] struct {
|
||||
head, tail *element[T]
|
||||
}
|
||||
|
||||
// A recursive generic type.
|
||||
type element[T any] struct {
|
||||
next *element[T]
|
||||
val T
|
||||
}
|
||||
|
||||
func (lst *List[T]) Push(v T) {
|
||||
if lst.tail == nil {
|
||||
lst.head = &element[T]{val: v}
|
||||
lst.tail = lst.head
|
||||
} else {
|
||||
lst.tail.next = &element[T]{val: v}
|
||||
lst.tail = lst.tail.next
|
||||
}
|
||||
}
|
||||
|
||||
func (lst *List[T]) GetAll() []T {
|
||||
var elems []T
|
||||
for e := lst.head; e != nil; e = e.next {
|
||||
elems = append(elems, e.val)
|
||||
}
|
||||
return elems
|
||||
}
|
||||
|
||||
func main() {
|
||||
lst := List[int]{}
|
||||
lst.Push(10)
|
||||
lst.Push(13)
|
||||
lst.Push(23)
|
||||
fmt.Println("list:", lst.GetAll())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// list: [10 13 23]
|
||||
24
_test/gen5.go
Normal file
24
_test/gen5.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Set[Elem comparable] struct {
|
||||
m map[Elem]struct{}
|
||||
}
|
||||
|
||||
func Make[Elem comparable]() Set[Elem] {
|
||||
return Set[Elem]{m: make(map[Elem]struct{})}
|
||||
}
|
||||
|
||||
func (s Set[Elem]) Add(v Elem) {
|
||||
s.m[v] = struct{}{}
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := Make[int]()
|
||||
s.Add(1)
|
||||
fmt.Println(s)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// {map[1:{}]}
|
||||
19
_test/gen6.go
Normal file
19
_test/gen6.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
func MapKeys[K comparable, V any](m map[K]V) []K {
|
||||
r := make([]K, 0, len(m))
|
||||
for k := range m {
|
||||
r = append(r, k)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func main() {
|
||||
var m = map[int]string{1: "2", 2: "4", 4: "8"}
|
||||
|
||||
// Test type inference
|
||||
println(len(MapKeys(m)))
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 3
|
||||
19
_test/gen7.go
Normal file
19
_test/gen7.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
func MapKeys[K comparable, V any](m map[K]V) []K {
|
||||
r := make([]K, 0, len(m))
|
||||
for k := range m {
|
||||
r = append(r, k)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func main() {
|
||||
var m = map[int]string{1: "2", 2: "4", 4: "8"}
|
||||
|
||||
// Test type inference
|
||||
println(len(MapKeys))
|
||||
}
|
||||
|
||||
// Error:
|
||||
// invalid argument for len
|
||||
15
_test/gen8.go
Normal file
15
_test/gen8.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
type Float interface {
|
||||
~float32 | ~float64
|
||||
}
|
||||
|
||||
func add[T Float](a, b T) float64 { return float64(a) + float64(b) }
|
||||
|
||||
func main() {
|
||||
var x, y int = 1, 2
|
||||
println(add(x, y))
|
||||
}
|
||||
|
||||
// Error:
|
||||
// int does not implement main.Float
|
||||
14
_test/gen9.go
Normal file
14
_test/gen9.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
type Float interface {
|
||||
~float32 | ~float64
|
||||
}
|
||||
|
||||
func add[T Float](a, b T) float64 { return float64(a) + float64(b) }
|
||||
|
||||
func main() {
|
||||
println(add(1, 2))
|
||||
}
|
||||
|
||||
// Error:
|
||||
// untyped int does not implement main.Float
|
||||
17
_test/issue-1332.go
Normal file
17
_test/issue-1332.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
func run(fn func(name string)) { fn("test") }
|
||||
|
||||
type T2 struct {
|
||||
name string
|
||||
}
|
||||
|
||||
func (t *T2) f(s string) { println(s, t.name) }
|
||||
|
||||
func main() {
|
||||
t2 := &T2{"foo"}
|
||||
run(t2.f)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// test foo
|
||||
26
_test/issue-1337.go
Normal file
26
_test/issue-1337.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
func f(i interface{}) {
|
||||
switch at := i.(type) {
|
||||
case int, int8:
|
||||
println("integer", at)
|
||||
case io.Reader:
|
||||
println("reader")
|
||||
}
|
||||
println("bye")
|
||||
}
|
||||
|
||||
func main() {
|
||||
var fd *os.File
|
||||
var r io.Reader = fd
|
||||
f(r)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// reader
|
||||
// bye
|
||||
12
_test/issue-1342.go
Normal file
12
_test/issue-1342.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
var a interface{}
|
||||
a = "a"
|
||||
fmt.Println(a, a == "a")
|
||||
}
|
||||
|
||||
// Output:
|
||||
// a true
|
||||
13
_test/issue-1344.go
Normal file
13
_test/issue-1344.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
var m = map[string]interface{}{"a": "a"}
|
||||
a, _ := m["a"]
|
||||
b, ok := a.(string)
|
||||
fmt.Println("a:", a, ", b:", b, ", ok:", ok)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// a: a , b: a , ok: true
|
||||
21
_test/issue-1354.go
Normal file
21
_test/issue-1354.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
println(test()) // Go prints true, Yaegi false
|
||||
}
|
||||
|
||||
func test() bool {
|
||||
if true {
|
||||
goto label
|
||||
}
|
||||
goto label
|
||||
label:
|
||||
println("Go continues here")
|
||||
return true
|
||||
println("Yaegi goes straight to this return (this line is never printed)")
|
||||
return false
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Go continues here
|
||||
// true
|
||||
22
_test/issue-1355.go
Normal file
22
_test/issue-1355.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import "github.com/traefik/yaegi/_test/p2"
|
||||
|
||||
func f(i interface{}) {
|
||||
_, ok := i.(p2.I)
|
||||
println("ok:", ok)
|
||||
}
|
||||
|
||||
func main() {
|
||||
var v *p2.T
|
||||
var i interface{}
|
||||
|
||||
i = v
|
||||
_, ok := i.(p2.I)
|
||||
println("ok:", ok)
|
||||
f(v)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// ok: true
|
||||
// ok: true
|
||||
14
_test/issue-1360.go
Normal file
14
_test/issue-1360.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
. "net"
|
||||
)
|
||||
|
||||
func main() {
|
||||
v := IP{}
|
||||
fmt.Println(v)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// <nil>
|
||||
27
_test/issue-1361.go
Normal file
27
_test/issue-1361.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
type obj struct {
|
||||
num float64
|
||||
}
|
||||
|
||||
type Fun func(o *obj) (r *obj, err error)
|
||||
|
||||
func numFun(fn func(f float64) float64) Fun {
|
||||
return func(o *obj) (*obj, error) {
|
||||
return &obj{fn(o.num)}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
f := numFun(math.Cos)
|
||||
r, err := f(&obj{})
|
||||
fmt.Println(r, err)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// &{1} <nil>
|
||||
16
_test/issue-1364.go
Normal file
16
_test/issue-1364.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var value interface{}
|
||||
var err error
|
||||
value, err = strconv.ParseFloat("123", 64)
|
||||
fmt.Println(value, err)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 123 <nil>
|
||||
18
_test/issue-1365.go
Normal file
18
_test/issue-1365.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
func genInt() (int, error) { return 3, nil }
|
||||
|
||||
func getInt() (value int) {
|
||||
value, err := genInt()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func main() {
|
||||
println(getInt())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 3
|
||||
16
_test/issue-1368.go
Normal file
16
_test/issue-1368.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
const dollar byte = 36
|
||||
|
||||
func main() {
|
||||
var c byte = 36
|
||||
switch true {
|
||||
case c == dollar:
|
||||
println("ok")
|
||||
default:
|
||||
println("not ok")
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// ok
|
||||
18
_test/issue-1371.go
Normal file
18
_test/issue-1371.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type node struct {
|
||||
parent *node
|
||||
child []*node
|
||||
key string
|
||||
}
|
||||
|
||||
func main() {
|
||||
root := &node{key: "root"}
|
||||
root.child = nil
|
||||
fmt.Println("root:", root)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// root: &{<nil> [] root}
|
||||
17
_test/issue-1373.go
Normal file
17
_test/issue-1373.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
)
|
||||
|
||||
func NewBadExpr() ast.Expr {
|
||||
return &ast.BadExpr{}
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Printf("%T\n", NewBadExpr().(*ast.BadExpr))
|
||||
}
|
||||
|
||||
// Output:
|
||||
// *ast.BadExpr
|
||||
38
_test/issue-1375.go
Normal file
38
_test/issue-1375.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Option func(*Struct)
|
||||
|
||||
func WithOption(opt string) Option {
|
||||
return func(s *Struct) {
|
||||
s.opt = opt
|
||||
}
|
||||
}
|
||||
|
||||
type Struct struct {
|
||||
opt string
|
||||
}
|
||||
|
||||
func New(opts ...Option) *Struct {
|
||||
s := new(Struct)
|
||||
for _, opt := range opts {
|
||||
opt(s)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Struct) ShowOption() {
|
||||
fmt.Println(s.opt)
|
||||
}
|
||||
|
||||
func main() {
|
||||
opts := []Option{
|
||||
WithOption("test"),
|
||||
}
|
||||
s := New(opts...)
|
||||
s.ShowOption()
|
||||
}
|
||||
|
||||
// Output:
|
||||
// test
|
||||
21
_test/issue-1378.go
Normal file
21
_test/issue-1378.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
t, err := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fn := func() error {
|
||||
_, err := t.GobEncode()
|
||||
return err
|
||||
}
|
||||
fmt.Println(fn())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// <nil>
|
||||
58
_test/issue-1381.go
Normal file
58
_test/issue-1381.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var bufPtrOne *bytes.Buffer
|
||||
var bufPtrTwo *bytes.Buffer
|
||||
var bufPtrThree *bytes.Buffer
|
||||
var bufPtrFour *bytes.Buffer
|
||||
|
||||
for i := 0; i < 2; i++ {
|
||||
bufOne := bytes.Buffer{}
|
||||
bufTwo := &bytes.Buffer{}
|
||||
var bufThree bytes.Buffer
|
||||
bufFour := new(bytes.Buffer)
|
||||
|
||||
if bufPtrOne == nil {
|
||||
bufPtrOne = &bufOne
|
||||
} else if bufPtrOne == &bufOne {
|
||||
fmt.Println("bufOne was not properly redeclared")
|
||||
} else {
|
||||
fmt.Println("bufOne is properly redeclared")
|
||||
}
|
||||
|
||||
if bufPtrTwo == nil {
|
||||
bufPtrTwo = bufTwo
|
||||
} else if bufPtrTwo == bufTwo {
|
||||
fmt.Println("bufTwo was not properly redeclared")
|
||||
} else {
|
||||
fmt.Println("bufTwo is properly redeclared")
|
||||
}
|
||||
|
||||
if bufPtrThree == nil {
|
||||
bufPtrThree = &bufThree
|
||||
} else if bufPtrThree == &bufThree {
|
||||
fmt.Println("bufThree was not properly redeclared")
|
||||
} else {
|
||||
fmt.Println("bufThree is properly redeclared")
|
||||
}
|
||||
|
||||
if bufPtrFour == nil {
|
||||
bufPtrFour = bufFour
|
||||
} else if bufPtrFour == bufFour {
|
||||
fmt.Println("bufFour was not properly redeclared")
|
||||
} else {
|
||||
fmt.Println("bufFour is properly redeclared")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// bufOne is properly redeclared
|
||||
// bufTwo is properly redeclared
|
||||
// bufThree is properly redeclared
|
||||
// bufFour is properly redeclared
|
||||
22
_test/issue-1404.go
Normal file
22
_test/issue-1404.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
type I interface {
|
||||
inI()
|
||||
}
|
||||
|
||||
type T struct {
|
||||
name string
|
||||
}
|
||||
|
||||
func (t *T) inI() {}
|
||||
|
||||
func main() {
|
||||
var i I = &T{name: "foo"}
|
||||
|
||||
if i, ok := i.(*T); ok {
|
||||
println(i.name)
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// foo
|
||||
16
_test/issue-1408.go
Normal file
16
_test/issue-1408.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
type (
|
||||
Number = int32
|
||||
Number2 = Number
|
||||
)
|
||||
|
||||
func f(n Number2) { println(n) }
|
||||
|
||||
func main() {
|
||||
var n Number = 5
|
||||
f(n)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 5
|
||||
16
_test/issue-1411.go
Normal file
16
_test/issue-1411.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
type Number int32
|
||||
|
||||
func (n Number) IsValid() bool { return true }
|
||||
|
||||
type Number1 = Number
|
||||
|
||||
type Number2 = Number1
|
||||
|
||||
func main() {
|
||||
a := Number2(5)
|
||||
println(a.IsValid())
|
||||
}
|
||||
|
||||
// Output: true
|
||||
17
_test/issue-1416.go
Normal file
17
_test/issue-1416.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
type Number int32
|
||||
|
||||
type Number1 = Number
|
||||
|
||||
type Number2 = Number1
|
||||
|
||||
func (n Number2) IsValid() bool { return true }
|
||||
|
||||
func main() {
|
||||
a := Number(5)
|
||||
println(a.IsValid())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// true
|
||||
9
_test/issue-1421.go
Normal file
9
_test/issue-1421.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package main
|
||||
|
||||
type Number = int
|
||||
|
||||
func main() {
|
||||
println(Number(1) < int(2))
|
||||
}
|
||||
|
||||
// Output: true
|
||||
44
_test/issue-1425.go
Normal file
44
_test/issue-1425.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type WrappedReader struct {
|
||||
reader io.Reader
|
||||
}
|
||||
|
||||
func (wr WrappedReader) Read(p []byte) (n int, err error) {
|
||||
return wr.reader.Read(p)
|
||||
}
|
||||
|
||||
// Of course, this implementation is completely stupid because it does not write
|
||||
// to the intended writer, as any honest WriteTo implementation should. its
|
||||
// implemtion is just to make obvious the divergence of behaviour with yaegi.
|
||||
func (wr WrappedReader) WriteTo(w io.Writer) (n int64, err error) {
|
||||
// Ignore w, send to Stdout to prove whether this WriteTo is used.
|
||||
data, err := io.ReadAll(wr)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
nn, err := os.Stdout.Write(data)
|
||||
return int64(nn), err
|
||||
}
|
||||
|
||||
func main() {
|
||||
f := strings.NewReader("hello world")
|
||||
wr := WrappedReader{reader: f}
|
||||
|
||||
// behind the scenes, io.Copy is supposed to use wr.WriteTo if the implementation exists.
|
||||
// With Go, it works as expected, i.e. the output is sent to os.Stdout.
|
||||
// With Yaegi, it doesn't, i.e. the output is sent to io.Discard.
|
||||
if _, err := io.Copy(io.Discard, wr); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// hello world
|
||||
25
_test/issue-1439.go
Normal file
25
_test/issue-1439.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
type Transformer interface {
|
||||
Reset()
|
||||
}
|
||||
|
||||
type Encoder struct {
|
||||
Transformer
|
||||
}
|
||||
|
||||
type nop struct{}
|
||||
|
||||
func (nop) Reset() { println("Reset") }
|
||||
|
||||
func f(e Transformer) {
|
||||
e.Reset()
|
||||
}
|
||||
|
||||
func main() {
|
||||
e := Encoder{Transformer: nop{}}
|
||||
f(e)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Reset
|
||||
41
_test/issue-1442.go
Normal file
41
_test/issue-1442.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx, _ := context.WithCancel(context.Background())
|
||||
ch := make(chan string, 20)
|
||||
defer close(ch)
|
||||
|
||||
go func(ctx context.Context, ch <-chan string) {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case tmp := <-ch:
|
||||
_ = tmp
|
||||
}
|
||||
}
|
||||
}(ctx, ch)
|
||||
|
||||
for _, i := range "abcdef" {
|
||||
for _, j := range "0123456789" {
|
||||
// i, j := "a", "0"
|
||||
for _, k := range "ABCDEF" {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
default:
|
||||
tmp := string(i) + string(j) + string(k)
|
||||
ch <- tmp
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Output:
|
||||
//
|
||||
20
_test/issue-1447.go
Normal file
20
_test/issue-1447.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type I interface {
|
||||
Name() string
|
||||
}
|
||||
|
||||
type S struct {
|
||||
iMap map[string]I
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := S{}
|
||||
s.iMap = map[string]I{}
|
||||
fmt.Println(s)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// {map[]}
|
||||
19
_test/issue-1451.go
Normal file
19
_test/issue-1451.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
type t1 uint8
|
||||
|
||||
const (
|
||||
n1 t1 = iota
|
||||
n2
|
||||
)
|
||||
|
||||
type T struct {
|
||||
elem [n2 + 1]int
|
||||
}
|
||||
|
||||
func main() {
|
||||
println(len(T{}.elem))
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 2
|
||||
22
_test/issue-1454.go
Normal file
22
_test/issue-1454.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
type I2 interface {
|
||||
I2() string
|
||||
}
|
||||
|
||||
type I interface {
|
||||
I2
|
||||
}
|
||||
|
||||
type S struct{}
|
||||
|
||||
func (*S) I2() string { return "foo" }
|
||||
|
||||
func main() {
|
||||
var i I
|
||||
_, ok := i.(*S)
|
||||
println(ok)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// false
|
||||
22
_test/issue-1459.go
Normal file
22
_test/issue-1459.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type funclistItem func()
|
||||
|
||||
type funclist struct {
|
||||
list []funclistItem
|
||||
}
|
||||
|
||||
func main() {
|
||||
funcs := funclist{}
|
||||
|
||||
funcs.list = append(funcs.list, func() { fmt.Println("first") })
|
||||
|
||||
for _, f := range funcs.list {
|
||||
f()
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// first
|
||||
84
_test/issue-1460.go
Normal file
84
_test/issue-1460.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/netip"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func unmarshalJSON[T any](b []byte, x *[]T) error {
|
||||
if *x != nil {
|
||||
return errors.New("already initialized")
|
||||
}
|
||||
if len(b) == 0 {
|
||||
return nil
|
||||
}
|
||||
return json.Unmarshal(b, x)
|
||||
}
|
||||
|
||||
func SliceOfViews[T ViewCloner[T, V], V StructView[T]](x []T) SliceView[T, V] {
|
||||
return SliceView[T, V]{x}
|
||||
}
|
||||
|
||||
type StructView[T any] interface {
|
||||
Valid() bool
|
||||
AsStruct() T
|
||||
}
|
||||
|
||||
type SliceView[T ViewCloner[T, V], V StructView[T]] struct {
|
||||
ж []T
|
||||
}
|
||||
|
||||
type ViewCloner[T any, V StructView[T]] interface {
|
||||
View() V
|
||||
Clone() T
|
||||
}
|
||||
|
||||
func (v SliceView[T, V]) MarshalJSON() ([]byte, error) { return json.Marshal(v.ж) }
|
||||
|
||||
func (v *SliceView[T, V]) UnmarshalJSON(b []byte) error { return unmarshalJSON(b, &v.ж) }
|
||||
|
||||
type Slice[T any] struct {
|
||||
ж []T
|
||||
}
|
||||
|
||||
func (v Slice[T]) MarshalJSON() ([]byte, error) { return json.Marshal(v.ж) }
|
||||
|
||||
func (v *Slice[T]) UnmarshalJSON(b []byte) error { return unmarshalJSON(b, &v.ж) }
|
||||
|
||||
func SliceOf[T any](x []T) Slice[T] {
|
||||
return Slice[T]{x}
|
||||
}
|
||||
|
||||
type IPPrefixSlice struct {
|
||||
ж Slice[netip.Prefix]
|
||||
}
|
||||
|
||||
type viewStruct struct {
|
||||
Int int
|
||||
Strings Slice[string]
|
||||
StringsPtr *Slice[string] `json:",omitempty"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
ss := SliceOf([]string{"bar"})
|
||||
in := viewStruct{
|
||||
Int: 1234,
|
||||
Strings: ss,
|
||||
StringsPtr: &ss,
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
encoder := json.NewEncoder(&buf)
|
||||
encoder.SetIndent("", "")
|
||||
err1 := encoder.Encode(&in)
|
||||
b := buf.Bytes()
|
||||
var got viewStruct
|
||||
err2 := json.Unmarshal(b, &got)
|
||||
println(err1 == nil, err2 == nil, reflect.DeepEqual(got, in))
|
||||
}
|
||||
|
||||
// Output:
|
||||
// true true true
|
||||
24
_test/issue-1465.go
Normal file
24
_test/issue-1465.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func SomeFunc[T int | string](defaultValue T) T {
|
||||
switch v := any(&defaultValue).(type) {
|
||||
case *string:
|
||||
*v = *v + " abc"
|
||||
case *int:
|
||||
*v -= 234
|
||||
}
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(SomeFunc("test"))
|
||||
fmt.Println(SomeFunc(1234))
|
||||
}
|
||||
|
||||
// Output:
|
||||
// test abc
|
||||
// 1000
|
||||
24
_test/issue-1466.go
Normal file
24
_test/issue-1466.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func SomeFunc(defaultValue interface{}) interface{} {
|
||||
switch v := defaultValue.(type) {
|
||||
case string:
|
||||
return v + " abc"
|
||||
case int:
|
||||
return v - 234
|
||||
}
|
||||
panic("whoops")
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(SomeFunc(1234))
|
||||
fmt.Println(SomeFunc("test"))
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 1000
|
||||
// test abc
|
||||
15
_test/issue-1470.go
Normal file
15
_test/issue-1470.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
type T struct {
|
||||
num [tnum + 2]int
|
||||
}
|
||||
|
||||
const tnum = 23
|
||||
|
||||
func main() {
|
||||
t := T{}
|
||||
println(len(t.num))
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 25
|
||||
12
_test/issue-1475.go
Normal file
12
_test/issue-1475.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
type T uint16
|
||||
|
||||
func f() T { return 0 }
|
||||
|
||||
func main() {
|
||||
println(f())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 0
|
||||
23
_test/issue-1488.go
Normal file
23
_test/issue-1488.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type vector interface {
|
||||
[]int | [3]int
|
||||
}
|
||||
|
||||
func sum[V vector](v V) (out int) {
|
||||
for i := 0; i < len(v); i++ {
|
||||
out += v[i]
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func main() {
|
||||
va := [3]int{1, 2, 3}
|
||||
vs := []int{1, 2, 3}
|
||||
fmt.Println(sum[[3]int](va), sum[[]int](vs))
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 6 6
|
||||
25
_test/issue-435.go
Normal file
25
_test/issue-435.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Foo int
|
||||
|
||||
func (f Foo) String() string {
|
||||
return "foo-" + strconv.Itoa(int(f))
|
||||
}
|
||||
|
||||
func print1(arg interface{}) {
|
||||
fmt.Println(arg)
|
||||
}
|
||||
|
||||
func main() {
|
||||
var arg Foo = 3
|
||||
var f = print1
|
||||
f(arg)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// foo-3
|
||||
@@ -1,6 +1,6 @@
|
||||
package main
|
||||
|
||||
import "github.com/traefik/yaegi/_test/alias3"
|
||||
import "github.com/traefik/yaegi/_test/named3"
|
||||
|
||||
var globalT *T
|
||||
|
||||
@@ -8,10 +8,10 @@ func init() {
|
||||
globalT = &T{A: "test"}
|
||||
}
|
||||
|
||||
type T alias3.T
|
||||
type T named3.T
|
||||
|
||||
func (t *T) PrintT() {
|
||||
(*alias3.T)(t).Print()
|
||||
(*named3.T)(t).Print()
|
||||
}
|
||||
|
||||
func main() {
|
||||
31
_test/named3/named3.go
Normal file
31
_test/named3/named3.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package named3
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type T struct {
|
||||
A string
|
||||
}
|
||||
|
||||
func (t *T) Print() {
|
||||
println(t.A)
|
||||
}
|
||||
|
||||
type A http.Header
|
||||
|
||||
func (a A) ForeachKey() error {
|
||||
for k, vals := range a {
|
||||
for _, v := range vals {
|
||||
fmt.Println(k, v)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a A) Set(k string, v []string) {
|
||||
a[k] = v
|
||||
}
|
||||
@@ -17,11 +17,23 @@ func (b B) Test2() {
|
||||
fmt.Println("test2")
|
||||
}
|
||||
|
||||
func (b B) Test3() {
|
||||
for k, vals := range b {
|
||||
for _, v := range vals {
|
||||
fmt.Println(k, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
b := B{}
|
||||
|
||||
b.Test2()
|
||||
b["test"] = []string{"a", "b"}
|
||||
b.Test3()
|
||||
}
|
||||
|
||||
// Output:
|
||||
// test2
|
||||
// test a
|
||||
// test b
|
||||
9
_test/p2/p2.go
Normal file
9
_test/p2/p2.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package p2
|
||||
|
||||
type I interface {
|
||||
isI()
|
||||
}
|
||||
|
||||
type T struct{}
|
||||
|
||||
func (t *T) isI() {}
|
||||
0
_test/p3/empty
Normal file
0
_test/p3/empty
Normal file
3
_test/p4/p4.go
Normal file
3
_test/p4/p4.go
Normal file
@@ -0,0 +1,3 @@
|
||||
package p4
|
||||
|
||||
var Value1 = "value1"
|
||||
10
_test/p5.go
Normal file
10
_test/p5.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package main
|
||||
|
||||
import "github.com/traefik/yaegi/_test/p5"
|
||||
|
||||
func main() {
|
||||
println(*p5.Value1)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// value1
|
||||
8
_test/p5/p5.go
Normal file
8
_test/p5/p5.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package p5
|
||||
|
||||
import "github.com/traefik/yaegi/_test/p4"
|
||||
|
||||
var (
|
||||
Value1 = &val1
|
||||
val1 = p4.Value1
|
||||
)
|
||||
14
_test/p6.go
Normal file
14
_test/p6.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/traefik/yaegi/_test/p6"
|
||||
)
|
||||
|
||||
func main() {
|
||||
t := p6.IPPrefixSlice{}
|
||||
fmt.Println(t)
|
||||
b, e := t.MarshalJSON()
|
||||
fmt.Println(string(b), e)
|
||||
}
|
||||
21
_test/p6/p6.go
Normal file
21
_test/p6/p6.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package p6
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/netip"
|
||||
)
|
||||
|
||||
type Slice[T any] struct {
|
||||
x []T
|
||||
}
|
||||
|
||||
type IPPrefixSlice struct {
|
||||
x Slice[netip.Prefix]
|
||||
}
|
||||
|
||||
func (v Slice[T]) MarshalJSON() ([]byte, error) { return json.Marshal(v.x) }
|
||||
|
||||
// MarshalJSON implements json.Marshaler.
|
||||
func (v IPPrefixSlice) MarshalJSON() ([]byte, error) {
|
||||
return v.x.MarshalJSON()
|
||||
}
|
||||
@@ -10,11 +10,11 @@ func main() {
|
||||
c2 := make(chan string)
|
||||
|
||||
go func() {
|
||||
time.Sleep(1e7)
|
||||
time.Sleep(1e8)
|
||||
c1 <- "one"
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(2e7)
|
||||
time.Sleep(2e8)
|
||||
c2 <- "two"
|
||||
}()
|
||||
|
||||
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
period = 100 * time.Millisecond
|
||||
precision = 7 * time.Millisecond
|
||||
period = 300 * time.Millisecond
|
||||
precision = 30 * time.Millisecond
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
@@ -25,7 +25,7 @@ func main() {
|
||||
}
|
||||
s.ts["test"] = append(s.ts["test"], &T{s: s})
|
||||
|
||||
t , ok:= s.getT("test")
|
||||
t, ok := s.getT("test")
|
||||
println(t != nil, ok)
|
||||
}
|
||||
|
||||
|
||||
17
_test/switch39.go
Normal file
17
_test/switch39.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
func f(params ...interface{}) {
|
||||
switch p0 := params[0].(type) {
|
||||
case string:
|
||||
println("string:", p0)
|
||||
default:
|
||||
println("not a string")
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
f("Hello")
|
||||
}
|
||||
|
||||
// Output:
|
||||
// string: Hello
|
||||
17
_test/switch40.go
Normal file
17
_test/switch40.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
func f(params ...interface{}) {
|
||||
switch params[0].(type) {
|
||||
case string:
|
||||
println("a string")
|
||||
default:
|
||||
println("not a string")
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
f("Hello")
|
||||
}
|
||||
|
||||
// Output:
|
||||
// a string
|
||||
@@ -43,6 +43,6 @@ func assertValue() {
|
||||
}
|
||||
|
||||
// Output:
|
||||
// interface conversion: interface {} is int, not string
|
||||
// interface conversion: interface {} is nil, not string
|
||||
// interface conversion: *httptest.ResponseRecorder is not http.Pusher: missing method Push
|
||||
// 22:10: interface conversion: interface {} is int, not string
|
||||
// 32:10: interface conversion: interface {} is nil, not string
|
||||
// 42:10: interface conversion: *httptest.ResponseRecorder is not http.Pusher: missing method Push
|
||||
|
||||
19
_test/var16.go
Normal file
19
_test/var16.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
func getArray() ([]int, error) { println("getArray"); return []int{1, 2}, nil }
|
||||
|
||||
func getNum() (int, error) { println("getNum"); return 3, nil }
|
||||
|
||||
func main() {
|
||||
if a, err := getNum(); err != nil {
|
||||
println("#1", a)
|
||||
} else if a, err := getArray(); err != nil {
|
||||
println("#2", a)
|
||||
}
|
||||
println("#3")
|
||||
}
|
||||
|
||||
// Output:
|
||||
// getNum
|
||||
// getArray
|
||||
// #3
|
||||
@@ -2,9 +2,7 @@ package fs1
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
// only available from 1.16.
|
||||
"testing/fstest"
|
||||
"testing/fstest" // only available from 1.16.
|
||||
|
||||
"github.com/traefik/yaegi/interp"
|
||||
"github.com/traefik/yaegi/stdlib"
|
||||
|
||||
@@ -194,6 +194,11 @@ func (e *Extractor) genContent(importPath string, p *types.Package) ([]byte, err
|
||||
case *types.Var:
|
||||
val[name] = Val{pname, true}
|
||||
case *types.TypeName:
|
||||
// Skip type if it is generic.
|
||||
if t, ok := o.Type().(*types.Named); ok && t.TypeParams().Len() > 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
typ[name] = pname
|
||||
if t, ok := o.Type().Underlying().(*types.Interface); ok {
|
||||
var methods []Method
|
||||
@@ -463,7 +468,7 @@ func GetMinor(part string) string {
|
||||
return minor
|
||||
}
|
||||
|
||||
const defaultMinorVersion = 17
|
||||
const defaultMinorVersion = 20
|
||||
|
||||
func genBuildTags() (string, error) {
|
||||
version := runtime.Version()
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// Package yaegi provides a Go interpreter.
|
||||
package yaegi
|
||||
|
||||
//go:generate go generate github.com/traefik/yaegi/internal/cmd/extract
|
||||
|
||||
@@ -11,7 +11,7 @@ Output files are written in the current directory, and prefixed with the go vers
|
||||
|
||||
Usage:
|
||||
|
||||
extract package...
|
||||
extract package...
|
||||
|
||||
The same program is used for all target operating systems and architectures.
|
||||
The GOOS and GOARCH environment variables set the desired target.
|
||||
|
||||
@@ -535,10 +535,11 @@ func {{$name}}(n *node) {
|
||||
typ := n.typ.concrete().TypeOf()
|
||||
isInterface := n.typ.TypeOf().Kind() == reflect.Interface
|
||||
c0, c1 := n.child[0], n.child[1]
|
||||
t0, t1 := c0.typ.TypeOf(), c1.typ.TypeOf()
|
||||
|
||||
{{- if or (eq $op.Name "==") (eq $op.Name "!=") }}
|
||||
|
||||
if c0.typ.cat == aliasT || c1.typ.cat == aliasT {
|
||||
if c0.typ.cat == linkedT || c1.typ.cat == linkedT {
|
||||
switch {
|
||||
case isInterface:
|
||||
v0 := genValue(c0)
|
||||
@@ -621,9 +622,39 @@ func {{$name}}(n *node) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Do not attempt to optimize '==' or '!=' if an operand is an interface.
|
||||
// This will preserve proper dynamic type checking at runtime. For static types,
|
||||
// type checks are already performed, so bypass them if possible.
|
||||
if t0.Kind() == reflect.Interface || t1.Kind() == reflect.Interface {
|
||||
v0 := genValue(c0)
|
||||
v1 := genValue(c1)
|
||||
if n.fnext != nil {
|
||||
fnext := getExec(n.fnext)
|
||||
n.exec = func(f *frame) bltn {
|
||||
i0 := v0(f).Interface()
|
||||
i1 := v1(f).Interface()
|
||||
if i0 {{$op.Name}} i1 {
|
||||
dest(f).SetBool(true)
|
||||
return tnext
|
||||
}
|
||||
dest(f).SetBool(false)
|
||||
return fnext
|
||||
}
|
||||
} else {
|
||||
dest := genValue(n)
|
||||
n.exec = func(f *frame) bltn {
|
||||
i0 := v0(f).Interface()
|
||||
i1 := v1(f).Interface()
|
||||
dest(f).SetBool(i0 {{$op.Name}} i1)
|
||||
return tnext
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
switch t0, t1 := c0.typ.TypeOf(), c1.typ.TypeOf(); {
|
||||
switch {
|
||||
case isString(t0) || isString(t1):
|
||||
switch {
|
||||
case isInterface:
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// Package unsafe2 provides helpers to generate recursive struct types.
|
||||
package unsafe2
|
||||
|
||||
import (
|
||||
|
||||
@@ -72,6 +72,7 @@ const (
|
||||
importSpec
|
||||
incDecStmt
|
||||
indexExpr
|
||||
indexListExpr
|
||||
interfaceType
|
||||
keyValueExpr
|
||||
labeledStmt
|
||||
@@ -155,6 +156,7 @@ var kinds = [...]string{
|
||||
importSpec: "importSpec",
|
||||
incDecStmt: "incDecStmt",
|
||||
indexExpr: "indexExpr",
|
||||
indexListExpr: "indexListExpr",
|
||||
interfaceType: "interfaceType",
|
||||
keyValueExpr: "keyValueExpr",
|
||||
labeledStmt: "labeledStmt",
|
||||
@@ -282,6 +284,7 @@ var actions = [...]string{
|
||||
aDec: "--",
|
||||
aEqual: "==",
|
||||
aGreater: ">",
|
||||
aGreaterEqual: ">=",
|
||||
aGetFunc: "getFunc",
|
||||
aGetIndex: "getIndex",
|
||||
aGetMethod: "getMethod",
|
||||
@@ -290,6 +293,7 @@ var actions = [...]string{
|
||||
aLand: "&&",
|
||||
aLor: "||",
|
||||
aLower: "<",
|
||||
aLowerEqual: "<=",
|
||||
aMethod: "Method",
|
||||
aMul: "*",
|
||||
aMulAssign: "*=",
|
||||
@@ -692,7 +696,7 @@ func (interp *Interpreter) ast(f ast.Node) (string, *node, error) {
|
||||
n := addChild(&root, anc, pos, funcDecl, aNop)
|
||||
n.val = n
|
||||
if a.Recv == nil {
|
||||
// function is not a method, create an empty receiver list
|
||||
// Function is not a method, create an empty receiver list.
|
||||
addChild(&root, astNode{n, nod}, pos, fieldList, aNop)
|
||||
}
|
||||
st.push(n, nod)
|
||||
@@ -704,7 +708,13 @@ func (interp *Interpreter) ast(f ast.Node) (string, *node, error) {
|
||||
st.push(n, nod)
|
||||
|
||||
case *ast.FuncType:
|
||||
st.push(addChild(&root, anc, pos, funcType, aNop), nod)
|
||||
n := addChild(&root, anc, pos, funcType, aNop)
|
||||
n.val = n
|
||||
if a.TypeParams == nil {
|
||||
// Function has no type parameters, create an empty fied list.
|
||||
addChild(&root, astNode{n, nod}, pos, fieldList, aNop)
|
||||
}
|
||||
st.push(n, nod)
|
||||
|
||||
case *ast.GenDecl:
|
||||
var kind nkind
|
||||
@@ -774,6 +784,9 @@ func (interp *Interpreter) ast(f ast.Node) (string, *node, error) {
|
||||
case *ast.IndexExpr:
|
||||
st.push(addChild(&root, anc, pos, indexExpr, aGetIndex), nod)
|
||||
|
||||
case *ast.IndexListExpr:
|
||||
st.push(addChild(&root, anc, pos, indexListExpr, aNop), nod)
|
||||
|
||||
case *ast.InterfaceType:
|
||||
st.push(addChild(&root, anc, pos, interfaceType, aNop), nod)
|
||||
|
||||
|
||||
@@ -147,10 +147,22 @@ func skipFile(ctx *build.Context, p string, skipTest bool) bool {
|
||||
}
|
||||
a := strings.Split(p[i+1:], "_")
|
||||
last := len(a) - 1
|
||||
if last1 := last - 1; last1 >= 0 && a[last1] == ctx.GOOS && a[last] == ctx.GOARCH {
|
||||
return false
|
||||
if last-1 >= 0 {
|
||||
switch x, y := a[last-1], a[last]; {
|
||||
case x == ctx.GOOS:
|
||||
if knownArch[y] {
|
||||
return y != ctx.GOARCH
|
||||
}
|
||||
return false
|
||||
case knownOs[x] && knownArch[y]:
|
||||
return true
|
||||
case knownArch[y] && y != ctx.GOARCH:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
if s := a[last]; s != ctx.GOOS && s != ctx.GOARCH && knownOs[s] || knownArch[s] {
|
||||
if x := a[last]; knownOs[x] && x != ctx.GOOS || knownArch[x] && x != ctx.GOARCH {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
|
||||
@@ -51,7 +51,7 @@ func TestBuildTag(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildFile(t *testing.T) {
|
||||
func TestSkipFile(t *testing.T) {
|
||||
// Assume a specific OS, arch and go pattern no matter the real underlying system
|
||||
ctx := build.Context{
|
||||
GOARCH: "amd64",
|
||||
@@ -65,10 +65,18 @@ func TestBuildFile(t *testing.T) {
|
||||
{"bar_linux.go", false},
|
||||
{"bar_maix.go", false},
|
||||
{"bar_mlinux.go", false},
|
||||
|
||||
{"bar_aix_foo.go", false},
|
||||
{"bar_linux_foo.go", false},
|
||||
{"bar_foo_amd64.go", false},
|
||||
{"bar_foo_arm.go", true},
|
||||
|
||||
{"bar_aix_s390x.go", true},
|
||||
{"bar_aix_amd64.go", true},
|
||||
{"bar_linux_arm.go", true},
|
||||
|
||||
{"bar_amd64.go", false},
|
||||
{"bar_arm.go", true},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
|
||||
609
interp/cfg.go
609
interp/cfg.go
@@ -145,8 +145,11 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
}
|
||||
|
||||
switch o.typ.cat {
|
||||
case valueT:
|
||||
case valueT, linkedT:
|
||||
typ := o.typ.rtype
|
||||
if o.typ.cat == linkedT {
|
||||
typ = o.typ.val.TypeOf()
|
||||
}
|
||||
switch typ.Kind() {
|
||||
case reflect.Map:
|
||||
n.anc.gen = rangeMap
|
||||
@@ -202,38 +205,50 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
n.findex = -1
|
||||
n.val = nil
|
||||
sc = sc.pushBloc()
|
||||
|
||||
case breakStmt, continueStmt, gotoStmt:
|
||||
if len(n.child) > 0 {
|
||||
// Handle labeled statements.
|
||||
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
|
||||
}
|
||||
sym.from = append(sym.from, n)
|
||||
n.sym = sym
|
||||
} else {
|
||||
n.sym = &symbol{kind: labelSym, from: []*node{n}, index: -1}
|
||||
sc.sym[label] = n.sym
|
||||
// Pre-define symbols for labels defined in this block, so we are sure that
|
||||
// they are already defined when met.
|
||||
// TODO(marc): labels must be stored outside of symbols to avoid collisions.
|
||||
for _, c := range n.child {
|
||||
if c.kind != labeledStmt {
|
||||
continue
|
||||
}
|
||||
label := c.child[0].ident
|
||||
sym := &symbol{kind: labelSym, node: c, index: -1}
|
||||
sc.sym[label] = sym
|
||||
c.sym = sym
|
||||
}
|
||||
// If block is the body of a function, get declared variables in current scope.
|
||||
// This is done in order to add the func signature symbols into sc.sym,
|
||||
// as we will need them in post-processing.
|
||||
if n.anc != nil && n.anc.kind == funcDecl {
|
||||
for k, v := range sc.anc.sym {
|
||||
sc.sym[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
case labeledStmt:
|
||||
case breakStmt, continueStmt, gotoStmt:
|
||||
if len(n.child) == 0 {
|
||||
break
|
||||
}
|
||||
// Handle labeled statements.
|
||||
label := n.child[0].ident
|
||||
// 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
|
||||
if sym, _, ok := sc.lookup(label); ok {
|
||||
if sym.kind != labelSym {
|
||||
err = n.child[0].cfgErrorf("label %s not defined", label)
|
||||
break
|
||||
}
|
||||
n.sym = sym
|
||||
} else {
|
||||
n.sym = &symbol{kind: labelSym, node: n, index: -1}
|
||||
n.sym = &symbol{kind: labelSym, index: -1}
|
||||
sc.sym[label] = n.sym
|
||||
}
|
||||
if n.kind == gotoStmt {
|
||||
n.sym.from = append(n.sym.from, n) // To allow forward goto statements.
|
||||
}
|
||||
sc.sym[label] = n.sym
|
||||
|
||||
case caseClause:
|
||||
sc = sc.pushBloc()
|
||||
@@ -296,7 +311,7 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
// Indicate that the first child is the type.
|
||||
n.nleft = 1
|
||||
} else {
|
||||
// Get type from ancestor (implicit type)
|
||||
// Get type from ancestor (implicit type).
|
||||
if n.anc.kind == keyValueExpr && n == n.anc.child[0] {
|
||||
n.typ = n.anc.typ.key
|
||||
} else if atyp := n.anc.typ; atyp != nil {
|
||||
@@ -307,8 +322,60 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
}
|
||||
}
|
||||
if n.typ == nil {
|
||||
err = n.cfgErrorf("undefined type")
|
||||
return false
|
||||
// A nil type indicates either an error or a generic type.
|
||||
// A child indexExpr or indexListExpr is used for type parameters,
|
||||
// it indicates an instanciated generic.
|
||||
if n.child[0].kind != indexExpr && n.child[0].kind != indexListExpr {
|
||||
err = n.cfgErrorf("undefined type")
|
||||
return false
|
||||
}
|
||||
t0, err1 := nodeType(interp, sc, n.child[0].child[0])
|
||||
if err1 != nil {
|
||||
return false
|
||||
}
|
||||
if t0.cat != genericT {
|
||||
err = n.cfgErrorf("undefined type")
|
||||
return false
|
||||
}
|
||||
// We have a composite literal of generic type, instantiate it.
|
||||
lt := []*itype{}
|
||||
for _, n1 := range n.child[0].child[1:] {
|
||||
t1, err1 := nodeType(interp, sc, n1)
|
||||
if err1 != nil {
|
||||
return false
|
||||
}
|
||||
lt = append(lt, t1)
|
||||
}
|
||||
var g *node
|
||||
g, _, err = genAST(sc, t0.node.anc, lt)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
n.child[0] = g.lastChild()
|
||||
n.typ, err = nodeType(interp, sc, n.child[0])
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
// Generate methods if any.
|
||||
for _, nod := range t0.method {
|
||||
gm, _, err2 := genAST(nod.scope, nod, lt)
|
||||
if err2 != nil {
|
||||
err = err2
|
||||
return false
|
||||
}
|
||||
gm.typ, err = nodeType(interp, nod.scope, gm.child[2])
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if _, err = interp.cfg(gm, sc, sc.pkgID, sc.pkgName); err != nil {
|
||||
return false
|
||||
}
|
||||
if err = genRun(gm); err != nil {
|
||||
return false
|
||||
}
|
||||
n.typ.addMethod(gm)
|
||||
}
|
||||
n.nleft = 1 // Indictate the type of composite literal.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -319,6 +386,10 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
}
|
||||
// Propagate type to children, to handle implicit types
|
||||
for _, c := range child {
|
||||
if isBlank(c) {
|
||||
err = n.cfgErrorf("cannot use _ as value")
|
||||
return false
|
||||
}
|
||||
switch c.kind {
|
||||
case binaryExpr, unaryExpr, compositeLitExpr:
|
||||
// Do not attempt to propagate composite type to operator expressions,
|
||||
@@ -349,16 +420,55 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
fallthrough
|
||||
|
||||
case funcDecl:
|
||||
// Do not allow function declarations without body.
|
||||
if len(n.child) < 4 {
|
||||
err = n.cfgErrorf("function declaration without body is unsupported (linkname or assembly can not be interpreted).")
|
||||
return false
|
||||
}
|
||||
n.val = n
|
||||
|
||||
// Skip substree in case of a generic function.
|
||||
if len(n.child[2].child[0].child) > 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
// Skip subtree if the function is a method with a generic receiver.
|
||||
if len(n.child[0].child) > 0 {
|
||||
recvTypeNode := n.child[0].child[0].lastChild()
|
||||
typ, err := nodeType(interp, sc, recvTypeNode)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if typ.cat == genericT || (typ.val != nil && typ.val.cat == genericT) {
|
||||
return false
|
||||
}
|
||||
if typ.cat == ptrT {
|
||||
rc0 := recvTypeNode.child[0]
|
||||
rt0, err := nodeType(interp, sc, rc0)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if rc0.kind == indexExpr && rt0.cat == structT {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Compute function type before entering local scope to avoid
|
||||
// possible collisions with function argument names.
|
||||
n.child[2].typ, err = nodeType(interp, sc, n.child[2])
|
||||
// Add a frame indirection level as we enter in a func
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
n.typ = n.child[2].typ
|
||||
|
||||
// Add a frame indirection level as we enter in a func.
|
||||
sc = sc.pushFunc()
|
||||
sc.def = n
|
||||
if len(n.child[2].child) == 2 {
|
||||
// Allocate frame space for return values, define output symbols
|
||||
for _, c := range n.child[2].child[1].child {
|
||||
|
||||
// Allocate frame space for return values, define output symbols.
|
||||
if len(n.child[2].child) == 3 {
|
||||
for _, c := range n.child[2].child[2].child {
|
||||
var typ *itype
|
||||
if typ, err = nodeType(interp, sc, c.lastChild()); err != nil {
|
||||
return false
|
||||
@@ -372,14 +482,28 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Define receiver symbol.
|
||||
if len(n.child[0].child) > 0 {
|
||||
// define receiver symbol
|
||||
var typ *itype
|
||||
fr := n.child[0].child[0]
|
||||
recvTypeNode := fr.lastChild()
|
||||
if typ, err = nodeType(interp, sc, recvTypeNode); err != nil {
|
||||
return false
|
||||
}
|
||||
if typ.cat == nilT {
|
||||
// This may happen when instantiating generic methods.
|
||||
s2, _, ok := sc.lookup(typ.id())
|
||||
if !ok {
|
||||
err = n.cfgErrorf("type not found: %s", typ.id())
|
||||
break
|
||||
}
|
||||
typ = s2.typ
|
||||
if typ.cat == nilT {
|
||||
err = n.cfgErrorf("nil type: %s", typ.id())
|
||||
break
|
||||
}
|
||||
}
|
||||
recvTypeNode.typ = typ
|
||||
n.child[2].typ.recv = typ
|
||||
n.typ.recv = typ
|
||||
@@ -388,8 +512,9 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
sc.sym[fr.child[0].ident] = &symbol{index: index, kind: varSym, typ: typ}
|
||||
}
|
||||
}
|
||||
for _, c := range n.child[2].child[0].child {
|
||||
// define input parameter symbols
|
||||
|
||||
// Define input parameter symbols.
|
||||
for _, c := range n.child[2].child[1].child {
|
||||
var typ *itype
|
||||
if typ, err = nodeType(interp, sc, c.lastChild()); err != nil {
|
||||
return false
|
||||
@@ -398,6 +523,7 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
sc.sym[cc.ident] = &symbol{index: sc.add(typ), kind: varSym, typ: typ}
|
||||
}
|
||||
}
|
||||
|
||||
if n.child[1].ident == "init" && len(n.child[0].child) == 0 {
|
||||
initNodes = append(initNodes, n)
|
||||
}
|
||||
@@ -478,6 +604,10 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
|
||||
switch n.kind {
|
||||
case addressExpr:
|
||||
if isBlank(n.child[0]) {
|
||||
err = n.cfgErrorf("cannot use _ as value")
|
||||
break
|
||||
}
|
||||
wireChild(n)
|
||||
|
||||
err = check.addressExpr(n)
|
||||
@@ -513,6 +643,15 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
updateSym := false
|
||||
var sym *symbol
|
||||
var level int
|
||||
|
||||
if dest.rval.IsValid() && isConstType(dest.typ) {
|
||||
err = n.cfgErrorf("cannot assign to %s (%s constant)", dest.rval, dest.typ.str)
|
||||
break
|
||||
}
|
||||
if isBlank(src) {
|
||||
err = n.cfgErrorf("cannot use _ as value")
|
||||
break
|
||||
}
|
||||
if n.kind == defineStmt || (n.kind == assignStmt && dest.ident == "_") {
|
||||
if atyp != nil {
|
||||
dest.typ = atyp
|
||||
@@ -609,7 +748,7 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
n.gen = nop
|
||||
src.findex = dest.findex
|
||||
src.level = level
|
||||
case len(n.child) < 4 && isArithmeticAction(src):
|
||||
case len(n.child) < 4 && isArithmeticAction(src) && !isInterface(dest.typ):
|
||||
// Optimize single assignments from some arithmetic operations.
|
||||
src.typ = dest.typ
|
||||
src.findex = dest.findex
|
||||
@@ -645,6 +784,10 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
}
|
||||
|
||||
case incDecStmt:
|
||||
err = check.unaryExpr(n)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
wireChild(n)
|
||||
n.findex = n.child[0].findex
|
||||
n.level = n.child[0].level
|
||||
@@ -754,24 +897,23 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
case n.anc.kind == returnStmt:
|
||||
// To avoid a copy in frame, if the result is to be returned, store it directly
|
||||
// at the frame location reserved for output arguments.
|
||||
pos := childPos(n)
|
||||
n.typ = sc.def.typ.ret[pos]
|
||||
n.findex = pos
|
||||
n.findex = childPos(n)
|
||||
default:
|
||||
// Allocate a new location in frame, and store the result here.
|
||||
n.findex = sc.add(n.typ)
|
||||
}
|
||||
|
||||
case indexExpr:
|
||||
if isBlank(n.child[0]) {
|
||||
err = n.cfgErrorf("cannot use _ as value")
|
||||
break
|
||||
}
|
||||
wireChild(n)
|
||||
t := n.child[0].typ
|
||||
for t.cat == linkedT {
|
||||
t = t.val
|
||||
}
|
||||
switch t.cat {
|
||||
case aliasT:
|
||||
if isString(t.val.TypeOf()) {
|
||||
n.typ = sc.getType("byte")
|
||||
break
|
||||
}
|
||||
fallthrough
|
||||
case ptrT:
|
||||
n.typ = t.val
|
||||
if t.val.cat == valueT {
|
||||
@@ -787,6 +929,51 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
} else {
|
||||
n.typ = valueTOf(t.rtype.Elem())
|
||||
}
|
||||
case funcT:
|
||||
// A function indexed by a type means an instantiated generic function.
|
||||
c1 := n.child[1]
|
||||
if !c1.isType(sc) {
|
||||
n.typ = t
|
||||
return
|
||||
}
|
||||
g, found, err := genAST(sc, t.node.anc, []*itype{c1.typ})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if !found {
|
||||
if _, err = interp.cfg(g, t.node.anc.scope, importPath, pkgName); err != nil {
|
||||
return
|
||||
}
|
||||
// Generate closures for function body.
|
||||
if err = genRun(g.child[3]); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
// Replace generic func node by instantiated one.
|
||||
n.anc.child[childPos(n)] = g
|
||||
n.typ = g.typ
|
||||
return
|
||||
case genericT:
|
||||
name := t.id() + "[" + n.child[1].typ.id() + "]"
|
||||
sym, _, ok := sc.lookup(name)
|
||||
if !ok {
|
||||
err = n.cfgErrorf("type not found: %s", name)
|
||||
return
|
||||
}
|
||||
n.gen = nop
|
||||
n.typ = sym.typ
|
||||
return
|
||||
case structT:
|
||||
// A struct indexed by a Type means an instantiated generic struct.
|
||||
name := t.name + "[" + n.child[1].ident + "]"
|
||||
sym, _, ok := sc.lookup(name)
|
||||
if ok {
|
||||
n.typ = sym.typ
|
||||
n.findex = sc.add(n.typ)
|
||||
n.gen = nop
|
||||
return
|
||||
}
|
||||
|
||||
default:
|
||||
n.typ = t.val
|
||||
}
|
||||
@@ -842,7 +1029,14 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
}
|
||||
wireChild(n)
|
||||
|
||||
case declStmt, exprStmt, sendStmt:
|
||||
case sendStmt:
|
||||
if !isChan(n.child[0].typ) {
|
||||
err = n.cfgErrorf("invalid operation: cannot send to non-channel %s", n.child[0].typ.id())
|
||||
break
|
||||
}
|
||||
fallthrough
|
||||
|
||||
case declStmt, exprStmt:
|
||||
wireChild(n)
|
||||
l := n.lastChild()
|
||||
n.findex = l.findex
|
||||
@@ -853,34 +1047,94 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
n.rval = l.rval
|
||||
|
||||
case breakStmt:
|
||||
if len(n.child) > 0 {
|
||||
gotoLabel(n.sym)
|
||||
} else {
|
||||
if len(n.child) == 0 {
|
||||
n.tnext = sc.loop
|
||||
break
|
||||
}
|
||||
if !n.hasAnc(n.sym.node) {
|
||||
err = n.cfgErrorf("invalid break label %s", n.child[0].ident)
|
||||
break
|
||||
}
|
||||
n.tnext = n.sym.node
|
||||
|
||||
case continueStmt:
|
||||
if len(n.child) > 0 {
|
||||
gotoLabel(n.sym)
|
||||
} else {
|
||||
if len(n.child) == 0 {
|
||||
n.tnext = sc.loopRestart
|
||||
break
|
||||
}
|
||||
if !n.hasAnc(n.sym.node) {
|
||||
err = n.cfgErrorf("invalid continue label %s", n.child[0].ident)
|
||||
break
|
||||
}
|
||||
n.tnext = n.sym.node.child[1].lastChild().start
|
||||
|
||||
case gotoStmt:
|
||||
gotoLabel(n.sym)
|
||||
if n.sym.node == nil {
|
||||
// It can be only due to a forward goto, to be resolved at labeledStmt.
|
||||
// Invalid goto labels are catched at AST parsing.
|
||||
break
|
||||
}
|
||||
n.tnext = n.sym.node.start
|
||||
|
||||
case labeledStmt:
|
||||
wireChild(n)
|
||||
if len(n.child) > 1 {
|
||||
n.start = n.child[1].start
|
||||
}
|
||||
gotoLabel(n.sym)
|
||||
for _, c := range n.sym.from {
|
||||
c.tnext = n.start // Resolve forward goto.
|
||||
}
|
||||
|
||||
case callExpr:
|
||||
for _, c := range n.child {
|
||||
if isBlank(c) {
|
||||
err = n.cfgErrorf("cannot use _ as value")
|
||||
return
|
||||
}
|
||||
}
|
||||
wireChild(n)
|
||||
switch {
|
||||
switch c0 := n.child[0]; {
|
||||
case c0.kind == indexListExpr:
|
||||
// Instantiate a generic function then call it.
|
||||
fun := c0.child[0].sym.node
|
||||
lt := []*itype{}
|
||||
for _, c := range c0.child[1:] {
|
||||
lt = append(lt, c.typ)
|
||||
}
|
||||
g, found, err := genAST(sc, fun, lt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if !found {
|
||||
_, err = interp.cfg(g, fun.scope, importPath, pkgName)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = genRun(g.child[3]) // Generate closures for function body.
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
n.child[0] = g
|
||||
c0 = n.child[0]
|
||||
wireChild(n)
|
||||
if typ := c0.typ; len(typ.ret) > 0 {
|
||||
n.typ = typ.ret[0]
|
||||
if n.anc.kind == returnStmt && n.typ.id() == sc.def.typ.ret[0].id() {
|
||||
// Store the result directly to the return value area of frame.
|
||||
// It can be done only if no type conversion at return is involved.
|
||||
n.findex = childPos(n)
|
||||
} else {
|
||||
n.findex = sc.add(n.typ)
|
||||
for _, t := range typ.ret[1:] {
|
||||
sc.add(t)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
n.findex = notInFrame
|
||||
}
|
||||
|
||||
case isBuiltinCall(n, sc):
|
||||
c0 := n.child[0]
|
||||
bname := c0.ident
|
||||
err = check.builtin(bname, n, n.child[1:], n.action == aCallSlice)
|
||||
if err != nil {
|
||||
@@ -888,7 +1142,7 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
}
|
||||
|
||||
n.gen = c0.sym.builtin
|
||||
c0.typ = &itype{cat: builtinT}
|
||||
c0.typ = &itype{cat: builtinT, name: bname}
|
||||
if n.typ, err = nodeType(interp, sc, n); err != nil {
|
||||
return
|
||||
}
|
||||
@@ -931,9 +1185,10 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
if op, ok := constBltn[bname]; ok && n.anc.action != aAssign {
|
||||
op(n) // pre-compute non-assigned constant :
|
||||
}
|
||||
case n.child[0].isType(sc):
|
||||
|
||||
case c0.isType(sc):
|
||||
// Type conversion expression
|
||||
c0, c1 := n.child[0], n.child[1]
|
||||
c1 := n.child[1]
|
||||
switch len(n.child) {
|
||||
case 1:
|
||||
err = n.cfgErrorf("missing argument in conversion to %s", c0.typ.id())
|
||||
@@ -978,16 +1233,17 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
n.typ = c0.typ
|
||||
n.findex = sc.add(n.typ)
|
||||
}
|
||||
|
||||
case isBinCall(n, sc):
|
||||
err = check.arguments(n, n.child[1:], n.child[0], n.action == aCallSlice)
|
||||
err = check.arguments(n, n.child[1:], c0, n.action == aCallSlice)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
n.gen = callBin
|
||||
typ := n.child[0].typ.rtype
|
||||
typ := c0.typ.rtype
|
||||
if typ.NumOut() > 0 {
|
||||
if funcType := n.child[0].typ.val; funcType != nil {
|
||||
if funcType := c0.typ.val; funcType != nil {
|
||||
// Use the original unwrapped function type, to allow future field and
|
||||
// methods resolutions, otherwise impossible on the opaque bin type.
|
||||
n.typ = funcType.ret[0]
|
||||
@@ -1007,7 +1263,8 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
}
|
||||
}
|
||||
}
|
||||
case isOffsetof(n):
|
||||
|
||||
case isOffsetof(c0):
|
||||
if len(n.child) != 2 || n.child[1].kind != selectorExpr || !isStruct(n.child[1].child[0].typ) {
|
||||
err = n.cfgErrorf("Offsetof argument: invalid expression")
|
||||
break
|
||||
@@ -1021,17 +1278,48 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
n.typ = valueTOf(reflect.TypeOf(field.Offset))
|
||||
n.rval = reflect.ValueOf(field.Offset)
|
||||
n.gen = nop
|
||||
|
||||
default:
|
||||
err = check.arguments(n, n.child[1:], n.child[0], n.action == aCallSlice)
|
||||
// The call may be on a generic function. In that case, replace the
|
||||
// generic function AST by an instantiated one before going further.
|
||||
if isGeneric(c0.typ) {
|
||||
fun := c0.typ.node.anc
|
||||
var g *node
|
||||
var types []*itype
|
||||
var found bool
|
||||
|
||||
// Infer type parameter from function call arguments.
|
||||
if types, err = inferTypesFromCall(sc, fun, n.child[1:]); err != nil {
|
||||
break
|
||||
}
|
||||
// Generate an instantiated AST from the generic function one.
|
||||
if g, found, err = genAST(sc, fun, types); err != nil {
|
||||
break
|
||||
}
|
||||
if !found {
|
||||
// Compile the generated function AST, so it becomes part of the scope.
|
||||
if _, err = interp.cfg(g, fun.scope, importPath, pkgName); err != nil {
|
||||
break
|
||||
}
|
||||
// AST compilation part 2: Generate closures for function body.
|
||||
if err = genRun(g.child[3]); err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
n.child[0] = g
|
||||
c0 = n.child[0]
|
||||
}
|
||||
|
||||
err = check.arguments(n, n.child[1:], c0, n.action == aCallSlice)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
if n.child[0].action == aGetFunc {
|
||||
if c0.action == aGetFunc {
|
||||
// Allocate a frame entry to store the anonymous function definition.
|
||||
sc.add(n.child[0].typ)
|
||||
sc.add(c0.typ)
|
||||
}
|
||||
if typ := n.child[0].typ; len(typ.ret) > 0 {
|
||||
if typ := c0.typ; len(typ.ret) > 0 {
|
||||
n.typ = typ.ret[0]
|
||||
if n.anc.kind == returnStmt && n.typ.id() == sc.def.typ.ret[0].id() {
|
||||
// Store the result directly to the return value area of frame.
|
||||
@@ -1249,7 +1537,7 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
n.types, n.scope = sc.types, sc
|
||||
sc = sc.pop()
|
||||
funcName := n.child[1].ident
|
||||
if sym := sc.sym[funcName]; !isMethod(n) && sym != nil {
|
||||
if sym := sc.sym[funcName]; !isMethod(n) && sym != nil && !isGeneric(sym.typ) {
|
||||
sym.index = -1 // to force value to n.val
|
||||
sym.typ = n.typ
|
||||
sym.kind = funcSym
|
||||
@@ -1275,10 +1563,14 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
|
||||
sym, level, found := sc.lookup(n.ident)
|
||||
if !found {
|
||||
if n.typ != nil {
|
||||
// Node is a generic instance with an already populated type.
|
||||
break
|
||||
}
|
||||
// retry with the filename, in case ident is a package name.
|
||||
sym, level, found = sc.lookup(filepath.Join(n.ident, baseName))
|
||||
if !found {
|
||||
err = n.cfgErrorf("undefined: %s %d", n.ident, n.index)
|
||||
err = n.cfgErrorf("undefined: %s", n.ident)
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -1392,9 +1684,17 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
sc = sc.pop()
|
||||
|
||||
case keyValueExpr:
|
||||
if isBlank(n.child[1]) {
|
||||
err = n.cfgErrorf("cannot use _ as value")
|
||||
break
|
||||
}
|
||||
wireChild(n)
|
||||
|
||||
case landExpr:
|
||||
if isBlank(n.child[0]) || isBlank(n.child[1]) {
|
||||
err = n.cfgErrorf("cannot use _ as value")
|
||||
break
|
||||
}
|
||||
n.start = n.child[0].start
|
||||
n.child[0].tnext = n.child[1].start
|
||||
setFNext(n.child[0], n)
|
||||
@@ -1406,6 +1706,10 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
}
|
||||
|
||||
case lorExpr:
|
||||
if isBlank(n.child[0]) || isBlank(n.child[1]) {
|
||||
err = n.cfgErrorf("cannot use _ as value")
|
||||
break
|
||||
}
|
||||
n.start = n.child[0].start
|
||||
n.child[0].tnext = n
|
||||
setFNext(n.child[0], n.child[1].start)
|
||||
@@ -1451,6 +1755,12 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
err = n.cfgErrorf("too many arguments to return")
|
||||
break
|
||||
}
|
||||
for _, c := range n.child {
|
||||
if isBlank(c) {
|
||||
err = n.cfgErrorf("cannot use _ as value")
|
||||
return
|
||||
}
|
||||
}
|
||||
returnSig := sc.def.child[2]
|
||||
if mustReturnValue(returnSig) {
|
||||
nret := len(n.child)
|
||||
@@ -1467,7 +1777,7 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
n.val = sc.def
|
||||
for i, c := range n.child {
|
||||
var typ *itype
|
||||
typ, err = nodeType(interp, sc.upperLevel(), returnSig.child[1].fieldType(i))
|
||||
typ, err = nodeType(interp, sc.upperLevel(), returnSig.child[2].fieldType(i))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -1479,12 +1789,7 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
}
|
||||
if c.typ.cat == nilT {
|
||||
// nil: Set node value to zero of return type
|
||||
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()})
|
||||
} else {
|
||||
c.rval = reflect.New(typ.TypeOf()).Elem()
|
||||
}
|
||||
c.rval = reflect.New(typ.TypeOf()).Elem()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1598,7 +1903,7 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
if n.typ.cat == valueT || n.typ.cat == errorT {
|
||||
switch method, ok := n.typ.rtype.MethodByName(n.child[1].ident); {
|
||||
case ok:
|
||||
hasRecvType := n.typ.rtype.Kind() != reflect.Interface
|
||||
hasRecvType := n.typ.TypeOf().Kind() != reflect.Interface
|
||||
n.val = method.Index
|
||||
n.gen = getIndexBinMethod
|
||||
n.action = aGetMethod
|
||||
@@ -1607,7 +1912,7 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
if hasRecvType {
|
||||
n.typ.recv = n.typ
|
||||
}
|
||||
case n.typ.rtype.Kind() == reflect.Ptr:
|
||||
case n.typ.TypeOf().Kind() == reflect.Ptr:
|
||||
if field, ok := n.typ.rtype.Elem().FieldByName(n.child[1].ident); ok {
|
||||
n.typ = valueTOf(field.Type)
|
||||
n.val = field.Index
|
||||
@@ -1615,7 +1920,7 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
break
|
||||
}
|
||||
err = n.cfgErrorf("undefined field or method: %s", n.child[1].ident)
|
||||
case n.typ.rtype.Kind() == reflect.Struct:
|
||||
case n.typ.TypeOf().Kind() == reflect.Struct:
|
||||
if field, ok := n.typ.rtype.FieldByName(n.child[1].ident); ok {
|
||||
n.typ = valueTOf(field.Type)
|
||||
n.val = field.Index
|
||||
@@ -1660,7 +1965,7 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
} else if m, lind := n.typ.lookupMethod(n.child[1].ident); m != nil {
|
||||
n.action = aGetMethod
|
||||
if n.child[0].isType(sc) {
|
||||
// Handle method as a function with receiver in 1st argument
|
||||
// Handle method as a function with receiver in 1st argument.
|
||||
n.val = m
|
||||
n.findex = notInFrame
|
||||
n.gen = nop
|
||||
@@ -1668,7 +1973,7 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
*n.typ = *m.typ
|
||||
n.typ.arg = append([]*itype{n.child[0].typ}, m.typ.arg...)
|
||||
} else {
|
||||
// Handle method with receiver
|
||||
// Handle method with receiver.
|
||||
n.gen = getMethod
|
||||
n.val = m
|
||||
n.typ = m.typ
|
||||
@@ -1691,7 +1996,7 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
err = n.cfgErrorf("undefined selector: %s", n.child[1].ident)
|
||||
}
|
||||
}
|
||||
if err == nil && n.findex != -1 {
|
||||
if err == nil && n.findex != -1 && n.typ.cat != genericT {
|
||||
n.findex = sc.add(n.typ)
|
||||
}
|
||||
|
||||
@@ -1699,9 +2004,13 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
wireChild(n)
|
||||
// Move action to block statement, so select node can be an exit point.
|
||||
n.child[0].gen = _select
|
||||
// Chain channel init actions in commClauses prior to invoke select.
|
||||
// Chain channel init actions in commClauses prior to invoking select.
|
||||
var cur *node
|
||||
for _, c := range n.child[0].child {
|
||||
if c.kind == commClauseDefault {
|
||||
// No channel init in this case.
|
||||
continue
|
||||
}
|
||||
var an, pn *node // channel init action nodes
|
||||
if len(c.child) > 0 {
|
||||
switch c0 := c.child[0]; {
|
||||
@@ -1742,6 +2051,10 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
}
|
||||
|
||||
case starExpr:
|
||||
if isBlank(n.child[0]) {
|
||||
err = n.cfgErrorf("cannot use _ as value")
|
||||
break
|
||||
}
|
||||
switch {
|
||||
case n.anc.kind == defineStmt && len(n.anc.child) == 3 && n.anc.child[1] == n:
|
||||
// pointer type expression in a var definition
|
||||
@@ -1832,8 +2145,16 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
setFNext(c, clauses[i+1])
|
||||
}
|
||||
}
|
||||
sbn.start = clauses[0].start
|
||||
n.start = n.child[0].start
|
||||
n.child[0].tnext = sbn.start
|
||||
if n.kind == typeSwitch {
|
||||
// Handle the typeSwitch init (the type assert expression).
|
||||
init := n.child[1].lastChild().child[0]
|
||||
init.tnext = sbn.start
|
||||
n.child[0].tnext = init.start
|
||||
} else {
|
||||
n.child[0].tnext = sbn.start
|
||||
}
|
||||
|
||||
case switchIfStmt: // like an if-else chain
|
||||
sc = sc.pop()
|
||||
@@ -1886,6 +2207,10 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
|
||||
wireChild(n)
|
||||
c0, c1 := n.child[0], n.child[1]
|
||||
if isBlank(c0) || isBlank(c1) {
|
||||
err = n.cfgErrorf("cannot use _ as value")
|
||||
break
|
||||
}
|
||||
if c1.typ == nil {
|
||||
if c1.typ, err = nodeType(interp, sc, c1); err != nil {
|
||||
return
|
||||
@@ -2059,13 +2384,44 @@ func compDefineX(sc *scope, n *node) error {
|
||||
return n.cfgErrorf("unsupported assign expression")
|
||||
}
|
||||
|
||||
// Handle redeclarations: find out new symbols vs existing ones.
|
||||
symIsNew := map[string]bool{}
|
||||
hasNewSymbol := false
|
||||
for i := range types {
|
||||
id := n.child[i].ident
|
||||
if id == "_" || id == "" {
|
||||
continue
|
||||
}
|
||||
if _, found := symIsNew[id]; found {
|
||||
return n.cfgErrorf("%s repeated on left side of :=", id)
|
||||
}
|
||||
// A new symbol doesn't exist in current scope. Upper scopes are not
|
||||
// taken into accout here, as a new symbol can shadow an existing one.
|
||||
if _, found := sc.sym[id]; found {
|
||||
symIsNew[id] = false
|
||||
} else {
|
||||
symIsNew[id] = true
|
||||
hasNewSymbol = true
|
||||
}
|
||||
}
|
||||
|
||||
for i, t := range types {
|
||||
index := sc.add(t)
|
||||
sc.sym[n.child[i].ident] = &symbol{index: index, kind: varSym, typ: t}
|
||||
var index int
|
||||
id := n.child[i].ident
|
||||
// A variable can be redeclared if at least one other not blank variable is created.
|
||||
// The redeclared variable must be of same type (it is reassigned, not created).
|
||||
// Careful to not reuse a variable which has been shadowed (it must not be a newSym).
|
||||
sym, level, ok := sc.lookup(id)
|
||||
canRedeclare := hasNewSymbol && len(symIsNew) > 1 && !symIsNew[id] && ok
|
||||
if canRedeclare && level == n.child[i].level && sym.kind == varSym && sym.typ.id() == t.id() {
|
||||
index = sym.index
|
||||
} else {
|
||||
index = sc.add(t)
|
||||
sc.sym[id] = &symbol{index: index, kind: varSym, typ: t}
|
||||
}
|
||||
n.child[i].typ = t
|
||||
n.child[i].findex = index
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2099,11 +2455,13 @@ func (n *node) cfgErrorf(format string, a ...interface{}) *cfgError {
|
||||
|
||||
func genRun(nod *node) error {
|
||||
var err error
|
||||
seen := map[*node]bool{}
|
||||
|
||||
nod.Walk(func(n *node) bool {
|
||||
if err != nil {
|
||||
if err != nil || seen[n] {
|
||||
return false
|
||||
}
|
||||
seen[n] = true
|
||||
switch n.kind {
|
||||
case funcType:
|
||||
if len(n.anc.child) == 4 {
|
||||
@@ -2192,15 +2550,22 @@ func genGlobalVarDecl(nodes []*node, sc *scope) (*node, error) {
|
||||
|
||||
func getVarDependencies(nod *node, sc *scope) (deps []*node) {
|
||||
nod.Walk(func(n *node) bool {
|
||||
if n.kind == identExpr {
|
||||
if sym, _, ok := sc.lookup(n.ident); ok {
|
||||
if sym.kind != varSym || !sym.global || sym.node == nod {
|
||||
return false
|
||||
}
|
||||
deps = append(deps, sym.node)
|
||||
}
|
||||
if n.kind != identExpr {
|
||||
return true
|
||||
}
|
||||
return true
|
||||
// Process ident nodes, and avoid false dependencies.
|
||||
if n.anc.kind == selectorExpr && childPos(n) == 1 {
|
||||
return false
|
||||
}
|
||||
sym, _, ok := sc.lookup(n.ident)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if sym.kind != varSym || !sym.global || sym.node == nod {
|
||||
return false
|
||||
}
|
||||
deps = append(deps, sym.node)
|
||||
return false
|
||||
}, nil)
|
||||
return deps
|
||||
}
|
||||
@@ -2269,6 +2634,10 @@ func (n *node) isType(sc *scope) bool {
|
||||
}
|
||||
case identExpr:
|
||||
return sc.getType(n.ident) != nil
|
||||
case indexExpr:
|
||||
// Maybe a generic type.
|
||||
sym, _, ok := sc.lookup(n.child[0].ident)
|
||||
return ok && sym.kind == typeSym
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -2419,6 +2788,15 @@ func (n *node) fieldType(m int) *node {
|
||||
// lastChild returns the last child of a node.
|
||||
func (n *node) lastChild() *node { return n.child[len(n.child)-1] }
|
||||
|
||||
func (n *node) hasAnc(nod *node) bool {
|
||||
for a := n.anc; a != nil; a = a.anc {
|
||||
if a == nod {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isKey(n *node) bool {
|
||||
return n.anc.kind == fileStmt ||
|
||||
(n.anc.kind == selectorExpr && n.anc.child[0] != n) ||
|
||||
@@ -2431,6 +2809,17 @@ func isField(n *node) bool {
|
||||
return n.kind == selectorExpr && len(n.child) > 0 && n.child[0].typ != nil && isStruct(n.child[0].typ)
|
||||
}
|
||||
|
||||
func isInInterfaceType(n *node) bool {
|
||||
anc := n.anc
|
||||
for anc != nil {
|
||||
if anc.kind == interfaceType {
|
||||
return true
|
||||
}
|
||||
anc = anc.anc
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isInConstOrTypeDecl(n *node) bool {
|
||||
anc := n.anc
|
||||
for anc != nil {
|
||||
@@ -2497,14 +2886,14 @@ func isBinCall(n *node, sc *scope) bool {
|
||||
}
|
||||
|
||||
func isOffsetof(n *node) bool {
|
||||
return isCall(n) && n.child[0].typ.cat == valueT && n.child[0].rval.String() == "Offsetof"
|
||||
return n.typ != nil && n.typ.cat == valueT && n.rval.String() == "Offsetof"
|
||||
}
|
||||
|
||||
func mustReturnValue(n *node) bool {
|
||||
if len(n.child) < 2 {
|
||||
if len(n.child) < 3 {
|
||||
return false
|
||||
}
|
||||
for _, f := range n.child[1].child {
|
||||
for _, f := range n.child[2].child {
|
||||
if len(f.child) > 1 {
|
||||
return false
|
||||
}
|
||||
@@ -2585,20 +2974,9 @@ func typeSwichAssign(n *node) bool {
|
||||
return ts.kind == typeSwitch && ts.child[1].action == aAssign
|
||||
}
|
||||
|
||||
func gotoLabel(s *symbol) {
|
||||
if s.node == nil {
|
||||
return
|
||||
}
|
||||
for _, c := range s.from {
|
||||
if c.tnext == nil {
|
||||
c.tnext = s.node.start
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func compositeGenerator(n *node, typ *itype, rtyp reflect.Type) (gen bltnGenerator) {
|
||||
switch typ.cat {
|
||||
case aliasT, ptrT:
|
||||
case linkedT, ptrT:
|
||||
gen = compositeGenerator(n, typ.val, rtyp)
|
||||
case arrayT, sliceT:
|
||||
gen = arrayLit
|
||||
@@ -2623,11 +3001,7 @@ func compositeGenerator(n *node, typ *itype, rtyp reflect.Type) (gen bltnGenerat
|
||||
}
|
||||
case valueT:
|
||||
if rtyp == nil {
|
||||
rtyp = n.typ.rtype
|
||||
}
|
||||
// TODO(mpl): I do not understand where this side-effect is coming from, and why it happens. quickfix for now.
|
||||
if rtyp == nil {
|
||||
rtyp = n.typ.val.rtype
|
||||
rtyp = n.typ.TypeOf()
|
||||
}
|
||||
switch k := rtyp.Kind(); k {
|
||||
case reflect.Struct:
|
||||
@@ -2728,3 +3102,10 @@ func isBoolAction(n *node) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isBlank(n *node) bool {
|
||||
if n.kind == parenExpr && len(n.child) > 0 {
|
||||
return isBlank(n.child[0])
|
||||
}
|
||||
return n.ident == "_"
|
||||
}
|
||||
|
||||
@@ -10,7 +10,8 @@ import (
|
||||
)
|
||||
|
||||
func TestCompileAST(t *testing.T) {
|
||||
file, err := parser.ParseFile(token.NewFileSet(), "_.go", `
|
||||
i := New(Options{})
|
||||
file, err := parser.ParseFile(i.FileSet(), "_.go", `
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
@@ -51,7 +52,7 @@ func TestCompileAST(t *testing.T) {
|
||||
node ast.Node
|
||||
skip string
|
||||
}{
|
||||
{desc: "file", node: file},
|
||||
{desc: "file", node: file, skip: "temporary ignore"},
|
||||
{desc: "import", node: file.Imports[0]},
|
||||
{desc: "type", node: dType},
|
||||
{desc: "var", node: dVar, skip: "not supported"},
|
||||
@@ -61,7 +62,6 @@ func TestCompileAST(t *testing.T) {
|
||||
{desc: "expr", node: dFunc.Body.List[0]},
|
||||
}
|
||||
|
||||
i := New(Options{})
|
||||
_ = i.Use(stdlib.Symbols)
|
||||
|
||||
for _, c := range cases {
|
||||
|
||||
@@ -4,7 +4,7 @@ Package interp provides a complete Go interpreter.
|
||||
For the Go language itself, refer to the official Go specification
|
||||
https://golang.org/ref/spec.
|
||||
|
||||
Importing packages
|
||||
# Importing packages
|
||||
|
||||
Packages can be imported in source or binary form, using the standard
|
||||
Go import statement. In source form, packages are searched first in the
|
||||
@@ -16,7 +16,7 @@ Binary form packages are compiled and linked with the interpreter
|
||||
executable, and exposed to scripts with the Use method. The extract
|
||||
subcommand of yaegi can be used to generate package wrappers.
|
||||
|
||||
Custom build tags
|
||||
# Custom build tags
|
||||
|
||||
Custom build tags allow to control which files in imported source
|
||||
packages are interpreted, in the same way as the "-tags" option of the
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user