Compare commits
85 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e32b2ab6bd | ||
|
|
1df5dc2e93 | ||
|
|
36594014c9 | ||
|
|
8f9eccdd61 | ||
|
|
dd03989709 | ||
|
|
b2a11eaf2a | ||
|
|
25c2a435f5 | ||
|
|
3d1a21094a | ||
|
|
2a0d29a390 | ||
|
|
db955e671f | ||
|
|
ab44c38298 | ||
|
|
93e2db7085 | ||
|
|
b1ef9251d4 | ||
|
|
b19afbfe93 | ||
|
|
a6762d500c | ||
|
|
c4174a7167 | ||
|
|
2f9fe7003a | ||
|
|
c86436afa6 | ||
|
|
29e912e90b | ||
|
|
e29de04513 | ||
|
|
c6945514cb | ||
|
|
847cd7ed2b | ||
|
|
fbf897b047 | ||
|
|
45c7b8008a | ||
|
|
4788775f8c | ||
|
|
bcb8546e91 | ||
|
|
befa5a2b54 | ||
|
|
0ba64fc318 | ||
|
|
d16bd4bcdb | ||
|
|
33a532ee01 | ||
|
|
cdc6b773c2 | ||
|
|
17d5f1814a | ||
|
|
5f8be70066 | ||
|
|
5530eca17d | ||
|
|
c8d9e25085 | ||
|
|
a241119bf7 | ||
|
|
3e3f8d5c2f | ||
|
|
9aeb78fc36 | ||
|
|
7863456d52 | ||
|
|
428b658160 | ||
|
|
350cf80bbf | ||
|
|
d92051d40f | ||
|
|
aa2621f6c6 | ||
|
|
2b1d6f0e7a | ||
|
|
992676722d | ||
|
|
451c754068 | ||
|
|
84ad46751a | ||
|
|
ec5392d566 | ||
|
|
7d8fdbc1fc | ||
|
|
fdfcb9c1df | ||
|
|
a988459dcd | ||
|
|
51e0b46256 | ||
|
|
eb06aeeb26 | ||
|
|
8bb5daf60e | ||
|
|
ac80d1b3ed | ||
|
|
2e17cfab4f | ||
|
|
3f4e1665b1 | ||
|
|
b9b0897d95 | ||
|
|
6337f8bc01 | ||
|
|
ccb8072759 | ||
|
|
d73111cda1 | ||
|
|
ff521ecb1a | ||
|
|
61b4980077 | ||
|
|
100d090853 | ||
|
|
bd60de5995 | ||
|
|
274eecdf18 | ||
|
|
8fa00f826c | ||
|
|
a64fe5b210 | ||
|
|
5c59dc425f | ||
|
|
8ad14d8ea4 | ||
|
|
8a1f9ef44e | ||
|
|
5cd1e11379 | ||
|
|
24b5375636 | ||
|
|
a83f492309 | ||
|
|
02c30482cc | ||
|
|
9e1da978b0 | ||
|
|
662838fd80 | ||
|
|
92d65c22f0 | ||
|
|
2db4579b6f | ||
|
|
101633c380 | ||
|
|
1e0f6ece6e | ||
|
|
662d2a6afe | ||
|
|
b25ee3f809 | ||
|
|
81e1e5f206 | ||
|
|
81d8339132 |
67
.github/workflows/go-cross.yml
vendored
Normal file
67
.github/workflows/go-cross.yml
vendored
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
name: Build Cross OS
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
|
||||||
|
cross:
|
||||||
|
name: Go
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
defaults:
|
||||||
|
run:
|
||||||
|
working-directory: ${{ github.workspace }}/go/src/github.com/traefik/yaegi
|
||||||
|
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
go-version: [ 1.15, 1.16 ]
|
||||||
|
os: [ubuntu-latest, macos-latest, windows-latest]
|
||||||
|
|
||||||
|
include:
|
||||||
|
- os: ubuntu-latest
|
||||||
|
go-path-suffix: /go
|
||||||
|
- os: macos-latest
|
||||||
|
go-path-suffix: /go
|
||||||
|
- os: windows-latest
|
||||||
|
go-path-suffix: \go
|
||||||
|
|
||||||
|
steps:
|
||||||
|
# https://github.com/marketplace/actions/setup-go-environment
|
||||||
|
- name: Set up Go ${{ matrix.go-version }}
|
||||||
|
uses: actions/setup-go@v2
|
||||||
|
with:
|
||||||
|
go-version: ${{ matrix.go-version }}
|
||||||
|
|
||||||
|
# https://github.com/marketplace/actions/checkout
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
with:
|
||||||
|
path: go/src/github.com/traefik/yaegi
|
||||||
|
|
||||||
|
# https://github.com/marketplace/actions/cache
|
||||||
|
- name: Cache Go modules
|
||||||
|
uses: actions/cache@v2
|
||||||
|
with:
|
||||||
|
path: |
|
||||||
|
~/go/pkg/mod # Module download cache
|
||||||
|
~/.cache/go-build # Build cache (Linux)
|
||||||
|
~/Library/Caches/go-build # Build cache (Mac)
|
||||||
|
'%LocalAppData%\go-build' # Build cache (Windows)
|
||||||
|
key: ${{ runner.os }}-${{ matrix.go-version }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-${{ matrix.go-version }}-go-
|
||||||
|
|
||||||
|
- name: Setup GOPATH
|
||||||
|
run: go env -w GOPATH=${{ github.workspace }}${{ matrix.go-path-suffix }}
|
||||||
|
|
||||||
|
# TODO fail on windows
|
||||||
|
# - name: Tests
|
||||||
|
# run: go test -v -cover ./...
|
||||||
|
# env:
|
||||||
|
# GOPATH: ${{ github.workspace }}${{ matrix.go-path }}
|
||||||
|
|
||||||
|
- name: Build
|
||||||
|
run: go build -race -v -ldflags "-s -w" -trimpath
|
||||||
110
.github/workflows/main.yml
vendored
Normal file
110
.github/workflows/main.yml
vendored
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
name: Main
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
env:
|
||||||
|
GO_VERSION: 1.16
|
||||||
|
GOLANGCI_LINT_VERSION: v1.36.0
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
|
||||||
|
linting:
|
||||||
|
name: Linting
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Set up Go ${{ env.GO_VERSION }}
|
||||||
|
uses: actions/setup-go@v2
|
||||||
|
with:
|
||||||
|
go-version: ${{ env.GO_VERSION }}
|
||||||
|
|
||||||
|
- name: Check out code
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Check and get dependencies
|
||||||
|
run: |
|
||||||
|
go mod tidy
|
||||||
|
git diff --exit-code go.mod
|
||||||
|
# git diff --exit-code go.sum
|
||||||
|
go mod download
|
||||||
|
|
||||||
|
- name: Install golangci-lint ${{ env.GOLANGCI_LINT_VERSION }}
|
||||||
|
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin ${GOLANGCI_LINT_VERSION}
|
||||||
|
|
||||||
|
- name: Run golangci-lint ${{ env.GOLANGCI_LINT_VERSION }}
|
||||||
|
run: make check
|
||||||
|
|
||||||
|
generate:
|
||||||
|
name: Checks code and generated code
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: linting
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
go-version: [ 1.15, 1.16 ]
|
||||||
|
steps:
|
||||||
|
- name: Set up Go ${{ matrix.go-version }}
|
||||||
|
uses: actions/setup-go@v2
|
||||||
|
with:
|
||||||
|
go-version: ${{ matrix.go-version }}
|
||||||
|
|
||||||
|
- name: Check out code
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Check generated code
|
||||||
|
run: |
|
||||||
|
rm -f interp/op.go
|
||||||
|
make generate
|
||||||
|
git update-index -q --refresh
|
||||||
|
CHANGED=$(git diff-index --name-only HEAD --)
|
||||||
|
test -z "$CHANGED" || echo $CHANGED
|
||||||
|
test -z "$CHANGED"
|
||||||
|
|
||||||
|
main:
|
||||||
|
name: Build and Test
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: linting
|
||||||
|
defaults:
|
||||||
|
run:
|
||||||
|
working-directory: ${{ github.workspace }}/go/src/github.com/traefik/yaegi
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
go-version: [ 1.15, 1.16 ]
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Set up Go ${{ matrix.go-version }}
|
||||||
|
uses: actions/setup-go@v2
|
||||||
|
with:
|
||||||
|
go-version: ${{ matrix.go-version }}
|
||||||
|
|
||||||
|
- name: Check out code
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
with:
|
||||||
|
path: go/src/github.com/traefik/yaegi
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
# https://github.com/marketplace/actions/cache
|
||||||
|
- name: Cache Go modules
|
||||||
|
uses: actions/cache@v2
|
||||||
|
with:
|
||||||
|
path: ./_test/tmp
|
||||||
|
key: ${{ runner.os }}-yaegi-${{ hashFiles('**//_test/tmp/') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-yaegi-
|
||||||
|
|
||||||
|
- name: Setup GOPATH
|
||||||
|
run: go env -w GOPATH=${{ github.workspace }}/go
|
||||||
|
|
||||||
|
- name: Build
|
||||||
|
run: go build -v ./...
|
||||||
|
|
||||||
|
- name: Run tests
|
||||||
|
run: make tests
|
||||||
|
env:
|
||||||
|
GOPATH: ${{ github.workspace }}/go
|
||||||
42
.github/workflows/release.yml
vendored
Normal file
42
.github/workflows/release.yml
vendored
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
name: Release
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- v[0-9]+.[0-9]+*
|
||||||
|
|
||||||
|
env:
|
||||||
|
GO_VERSION: 1.16
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
|
||||||
|
release:
|
||||||
|
name: Create a release
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Set up Go ${{ env.GO_VERSION }}
|
||||||
|
uses: actions/setup-go@v2
|
||||||
|
with:
|
||||||
|
go-version: ${{ env.GO_VERSION }}
|
||||||
|
|
||||||
|
- name: Check out code
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Cache Go modules
|
||||||
|
uses: actions/cache@v2
|
||||||
|
with:
|
||||||
|
path: ~/go/pkg/mod
|
||||||
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-go-
|
||||||
|
|
||||||
|
- name: Run GoReleaser
|
||||||
|
uses: goreleaser/goreleaser-action@v2
|
||||||
|
with:
|
||||||
|
version: latest
|
||||||
|
args: release --rm-dist
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GH_TOKEN_REPO }}
|
||||||
@@ -33,16 +33,23 @@
|
|||||||
"gochecknoinits",
|
"gochecknoinits",
|
||||||
"gochecknoglobals",
|
"gochecknoglobals",
|
||||||
"wsl",
|
"wsl",
|
||||||
|
"nlreturn",
|
||||||
"godox",
|
"godox",
|
||||||
"funlen",
|
"funlen",
|
||||||
"gocognit",
|
"gocognit",
|
||||||
"stylecheck",
|
"stylecheck",
|
||||||
"gomnd",
|
"gomnd",
|
||||||
"testpackage",
|
"testpackage",
|
||||||
|
"paralleltest",
|
||||||
|
"tparallel",
|
||||||
"goerr113",
|
"goerr113",
|
||||||
|
"wrapcheck",
|
||||||
"nestif",
|
"nestif",
|
||||||
"exhaustive",
|
"exhaustive",
|
||||||
"nlreturn",
|
"exhaustivestruct",
|
||||||
|
"forbidigo",
|
||||||
|
"ifshort",
|
||||||
|
"errorlint", # TODO: must be reactivate before fixes
|
||||||
]
|
]
|
||||||
|
|
||||||
[issues]
|
[issues]
|
||||||
@@ -61,3 +68,6 @@
|
|||||||
[[issues.exclude-rules]]
|
[[issues.exclude-rules]]
|
||||||
path = "interp/interp.go"
|
path = "interp/interp.go"
|
||||||
text = "`out` can be `io.Writer`"
|
text = "`out` can be `io.Writer`"
|
||||||
|
[[issues.exclude-rules]]
|
||||||
|
path = "interp/interp_eval_test.go"
|
||||||
|
linters = ["thelper"]
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ archives:
|
|||||||
- LICENSE
|
- LICENSE
|
||||||
|
|
||||||
brews:
|
brews:
|
||||||
- github:
|
- tap:
|
||||||
owner: traefik
|
owner: traefik
|
||||||
name: homebrew-tap
|
name: homebrew-tap
|
||||||
commit_author:
|
commit_author:
|
||||||
|
|||||||
60
.travis.yml
60
.travis.yml
@@ -1,60 +0,0 @@
|
|||||||
language: go
|
|
||||||
|
|
||||||
dist: xenial
|
|
||||||
|
|
||||||
branches:
|
|
||||||
only:
|
|
||||||
- master
|
|
||||||
- /^v\d+\.\d+(\.\d+)?(-\S*)?$/
|
|
||||||
|
|
||||||
notifications:
|
|
||||||
email:
|
|
||||||
on_success: never
|
|
||||||
on_failure: change
|
|
||||||
|
|
||||||
cache:
|
|
||||||
directories:
|
|
||||||
- $GOPATH/pkg/mod
|
|
||||||
|
|
||||||
matrix:
|
|
||||||
fast_finish: true
|
|
||||||
include:
|
|
||||||
- go: 1.14.x
|
|
||||||
- go: 1.15.x
|
|
||||||
env: STABLE=true
|
|
||||||
|
|
||||||
env:
|
|
||||||
global:
|
|
||||||
- GO111MODULE=on
|
|
||||||
|
|
||||||
go_import_path: github.com/traefik/yaegi
|
|
||||||
|
|
||||||
before_install:
|
|
||||||
# Install linters and misspell
|
|
||||||
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b $GOPATH/bin ${GOLANGCI_LINT_VERSION}
|
|
||||||
- golangci-lint --version
|
|
||||||
|
|
||||||
install:
|
|
||||||
- echo "TRAVIS_GO_VERSION=$TRAVIS_GO_VERSION"
|
|
||||||
- go mod download
|
|
||||||
|
|
||||||
before_script:
|
|
||||||
- rm -f interp/op.go
|
|
||||||
- make generate
|
|
||||||
- git update-index -q --refresh
|
|
||||||
- CHANGED=$(git diff-index --name-only HEAD --)
|
|
||||||
- test -z "$CHANGED" || echo $CHANGED
|
|
||||||
- test -z "$CHANGED"
|
|
||||||
|
|
||||||
script:
|
|
||||||
- make check
|
|
||||||
- go build -v ./...
|
|
||||||
- make tests
|
|
||||||
|
|
||||||
deploy:
|
|
||||||
- provider: script
|
|
||||||
skip_cleanup: true
|
|
||||||
script: curl -sL https://git.io/goreleaser | bash
|
|
||||||
on:
|
|
||||||
tags: true
|
|
||||||
condition: $STABLE = true
|
|
||||||
@@ -1,10 +1,41 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
b := 2
|
b := 2 // int
|
||||||
var a interface{} = 5 + b
|
|
||||||
|
var c int = 5 + b
|
||||||
|
println(c)
|
||||||
|
|
||||||
|
var d int32 = 6 + int32(b)
|
||||||
|
println(d)
|
||||||
|
|
||||||
|
var a interface{} = 7 + b
|
||||||
println(a.(int))
|
println(a.(int))
|
||||||
|
|
||||||
|
var e int32 = 2
|
||||||
|
var f interface{} = 8 + e
|
||||||
|
println(f.(int32))
|
||||||
|
|
||||||
|
a = 9 + e
|
||||||
|
println(a.(int32))
|
||||||
|
|
||||||
|
var g int = 2
|
||||||
|
a = 10 + g
|
||||||
|
println(a.(int))
|
||||||
|
|
||||||
|
// multiple assignment
|
||||||
|
var foo interface{}
|
||||||
|
foo, a = "hello", 11 + g
|
||||||
|
println(a.(int))
|
||||||
|
println(foo.(string))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 7
|
// 7
|
||||||
|
// 8
|
||||||
|
// 9
|
||||||
|
// 10
|
||||||
|
// 11
|
||||||
|
// 12
|
||||||
|
// 13
|
||||||
|
// hello
|
||||||
|
|||||||
63
_test/addr2.go
Normal file
63
_test/addr2.go
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Email struct {
|
||||||
|
Where string `xml:"where,attr"`
|
||||||
|
Addr string
|
||||||
|
}
|
||||||
|
|
||||||
|
func f(r interface{}) error {
|
||||||
|
return withPointerAsInterface(&r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func withPointerAsInterface(r interface{}) error {
|
||||||
|
_ = (r).(*interface{})
|
||||||
|
rp, ok := (r).(*interface{})
|
||||||
|
if !ok {
|
||||||
|
return errors.New("cannot assert to *interface{}")
|
||||||
|
}
|
||||||
|
em, ok := (*rp).(*Email)
|
||||||
|
if !ok {
|
||||||
|
return errors.New("cannot assert to *Email")
|
||||||
|
}
|
||||||
|
em.Where = "work"
|
||||||
|
em.Addr = "bob@work.com"
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ff(s string, r interface{}) error {
|
||||||
|
return xml.Unmarshal([]byte(s), r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func fff(s string, r interface{}) error {
|
||||||
|
return xml.Unmarshal([]byte(s), &r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
data := `
|
||||||
|
<Email where='work'>
|
||||||
|
<Addr>bob@work.com</Addr>
|
||||||
|
</Email>
|
||||||
|
`
|
||||||
|
v := Email{}
|
||||||
|
err := f(&v)
|
||||||
|
fmt.Println(err, v)
|
||||||
|
|
||||||
|
vv := Email{}
|
||||||
|
err = ff(data, &vv)
|
||||||
|
fmt.Println(err, vv)
|
||||||
|
|
||||||
|
vvv := Email{}
|
||||||
|
err = ff(data, &vvv)
|
||||||
|
fmt.Println(err, vvv)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// <nil> {work bob@work.com}
|
||||||
|
// <nil> {work bob@work.com}
|
||||||
|
// <nil> {work bob@work.com}
|
||||||
24
_test/addr3.go
Normal file
24
_test/addr3.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var a interface{}
|
||||||
|
a = 2
|
||||||
|
fmt.Println(a)
|
||||||
|
|
||||||
|
var b *interface{}
|
||||||
|
b = &a
|
||||||
|
fmt.Println(*b)
|
||||||
|
|
||||||
|
var c **interface{}
|
||||||
|
c = &b
|
||||||
|
fmt.Println(**c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2
|
||||||
|
// 2
|
||||||
|
// 2
|
||||||
114
_test/addr4.go
Normal file
114
_test/addr4.go
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
const jsonData = `[
|
||||||
|
"foo",
|
||||||
|
"bar"
|
||||||
|
]`
|
||||||
|
|
||||||
|
const jsonData2 = `[
|
||||||
|
{"foo": "foo"},
|
||||||
|
{"bar": "bar"}
|
||||||
|
]`
|
||||||
|
|
||||||
|
const jsonData3 = `{
|
||||||
|
"foo": "foo",
|
||||||
|
"bar": "bar"
|
||||||
|
}`
|
||||||
|
|
||||||
|
func fromSlice() {
|
||||||
|
var a []interface{}
|
||||||
|
var c, d interface{}
|
||||||
|
c = 2
|
||||||
|
d = 3
|
||||||
|
a = []interface{}{c, d}
|
||||||
|
|
||||||
|
if err := json.Unmarshal([]byte(jsonData), &a); err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range a {
|
||||||
|
fmt.Println(k, ":", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func fromEmpty() {
|
||||||
|
var a interface{}
|
||||||
|
var c, d interface{}
|
||||||
|
c = 2
|
||||||
|
d = 3
|
||||||
|
a = []interface{}{c, d}
|
||||||
|
|
||||||
|
if err := json.Unmarshal([]byte(jsonData), &a); err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
b := a.([]interface{})
|
||||||
|
|
||||||
|
for k, v := range b {
|
||||||
|
fmt.Println(k, ":", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func sliceOfObjects() {
|
||||||
|
var a interface{}
|
||||||
|
|
||||||
|
if err := json.Unmarshal([]byte(jsonData2), &a); err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
b := a.([]interface{})
|
||||||
|
|
||||||
|
for k, v := range b {
|
||||||
|
fmt.Println(k, ":", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func intoMap() {
|
||||||
|
var a interface{}
|
||||||
|
|
||||||
|
if err := json.Unmarshal([]byte(jsonData3), &a); err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
b := a.(map[string]interface{})
|
||||||
|
|
||||||
|
seenFoo := false
|
||||||
|
for k, v := range b {
|
||||||
|
vv := v.(string)
|
||||||
|
if vv != "foo" {
|
||||||
|
if seenFoo {
|
||||||
|
fmt.Println(k, ":", vv)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
kk := k
|
||||||
|
vvv := vv
|
||||||
|
defer fmt.Println(kk, ":", vvv)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
seenFoo = true
|
||||||
|
fmt.Println(k, ":", vv)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fromSlice()
|
||||||
|
fromEmpty()
|
||||||
|
sliceOfObjects()
|
||||||
|
intoMap()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 0 : foo
|
||||||
|
// 1 : bar
|
||||||
|
// 0 : foo
|
||||||
|
// 1 : bar
|
||||||
|
// 0 : map[foo:foo]
|
||||||
|
// 1 : map[bar:bar]
|
||||||
|
// foo : foo
|
||||||
|
// bar : bar
|
||||||
62
_test/addr5.go
Normal file
62
_test/addr5.go
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
body := []byte(`{
|
||||||
|
"BODY_1": "VALUE_1",
|
||||||
|
"BODY_2": "VALUE_2",
|
||||||
|
"BODY_3": null,
|
||||||
|
"BODY_4": {
|
||||||
|
"BODY_1": "VALUE_1",
|
||||||
|
"BODY_2": "VALUE_2",
|
||||||
|
"BODY_3": null
|
||||||
|
},
|
||||||
|
"BODY_5": [
|
||||||
|
"VALUE_1",
|
||||||
|
"VALUE_2",
|
||||||
|
"VALUE_3"
|
||||||
|
]
|
||||||
|
}`)
|
||||||
|
|
||||||
|
values := url.Values{}
|
||||||
|
|
||||||
|
var rawData map[string]interface{}
|
||||||
|
err := json.Unmarshal(body, &rawData)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("can't parse body")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for key, val := range rawData {
|
||||||
|
switch val.(type) {
|
||||||
|
case string, bool, float64:
|
||||||
|
values.Add(key, fmt.Sprint(val))
|
||||||
|
case nil:
|
||||||
|
values.Add(key, "")
|
||||||
|
case map[string]interface{}, []interface{}:
|
||||||
|
jsonVal, err := json.Marshal(val)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("can't encode json")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
values.Add(key, string(jsonVal))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println(values.Get("BODY_1"))
|
||||||
|
fmt.Println(values.Get("BODY_2"))
|
||||||
|
fmt.Println(values.Get("BODY_3"))
|
||||||
|
fmt.Println(values.Get("BODY_4"))
|
||||||
|
fmt.Println(values.Get("BODY_5"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// VALUE_1
|
||||||
|
// VALUE_2
|
||||||
|
//
|
||||||
|
// {"BODY_1":"VALUE_1","BODY_2":"VALUE_2","BODY_3":null}
|
||||||
|
// ["VALUE_1","VALUE_2","VALUE_3"]
|
||||||
14
_test/append3.go
Normal file
14
_test/append3.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
a := []int{1, 2}
|
||||||
|
b := [2]int{3, 4}
|
||||||
|
fmt.Println(append(a, b[:]...))
|
||||||
|
fmt.Println(append(a, []int{5, 6}...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [1 2 3 4]
|
||||||
|
// [1 2 5 6]
|
||||||
12
_test/append4.go
Normal file
12
_test/append4.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
a := []*int{}
|
||||||
|
a = append(a, nil)
|
||||||
|
fmt.Println(a)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [<nil>]
|
||||||
128
_test/assert0.go
Normal file
128
_test/assert0.go
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MyWriter interface {
|
||||||
|
Write(p []byte) (i int, err error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type DummyWriter interface {
|
||||||
|
Write(p []byte) (i int, err error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type TestStruct struct{}
|
||||||
|
|
||||||
|
func (t TestStruct) Write(p []byte) (n int, err error) {
|
||||||
|
return len(p), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func usesWriter(w MyWriter) {
|
||||||
|
n, _ := w.Write([]byte("hello world"))
|
||||||
|
fmt.Println(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
type MyStringer interface {
|
||||||
|
String() string
|
||||||
|
}
|
||||||
|
|
||||||
|
type DummyStringer interface {
|
||||||
|
String() string
|
||||||
|
}
|
||||||
|
|
||||||
|
func usesStringer(s MyStringer) {
|
||||||
|
fmt.Println(s.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// TODO(mpl): restore when we can deal with empty interface.
|
||||||
|
// var t interface{}
|
||||||
|
var t DummyWriter
|
||||||
|
t = TestStruct{}
|
||||||
|
var tw MyWriter
|
||||||
|
var ok bool
|
||||||
|
tw, ok = t.(MyWriter)
|
||||||
|
if !ok {
|
||||||
|
fmt.Println("TestStruct does not implement MyWriter")
|
||||||
|
} else {
|
||||||
|
fmt.Println("TestStruct implements MyWriter")
|
||||||
|
usesWriter(tw)
|
||||||
|
}
|
||||||
|
n, _ := t.(MyWriter).Write([]byte("hello world"))
|
||||||
|
fmt.Println(n)
|
||||||
|
|
||||||
|
// not redundant with the above, because it goes through a slightly different code path.
|
||||||
|
if _, ok := t.(MyWriter); !ok {
|
||||||
|
fmt.Println("TestStruct does not implement MyWriter")
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
fmt.Println("TestStruct implements MyWriter")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO(mpl): restore
|
||||||
|
/*
|
||||||
|
t = 42
|
||||||
|
foo, ok := t.(MyWriter)
|
||||||
|
if !ok {
|
||||||
|
fmt.Println("42 does not implement MyWriter")
|
||||||
|
} else {
|
||||||
|
fmt.Println("42 implements MyWriter")
|
||||||
|
}
|
||||||
|
_ = foo
|
||||||
|
|
||||||
|
if _, ok := t.(MyWriter); !ok {
|
||||||
|
fmt.Println("42 does not implement MyWriter")
|
||||||
|
} else {
|
||||||
|
fmt.Println("42 implements MyWriter")
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
// var tt interface{}
|
||||||
|
var tt DummyStringer
|
||||||
|
tt = time.Nanosecond
|
||||||
|
var myD MyStringer
|
||||||
|
myD, ok = tt.(MyStringer)
|
||||||
|
if !ok {
|
||||||
|
fmt.Println("time.Nanosecond does not implement MyStringer")
|
||||||
|
} else {
|
||||||
|
fmt.Println("time.Nanosecond implements MyStringer")
|
||||||
|
usesStringer(myD)
|
||||||
|
}
|
||||||
|
fmt.Println(tt.(MyStringer).String())
|
||||||
|
|
||||||
|
if _, ok := tt.(MyStringer); !ok {
|
||||||
|
fmt.Println("time.Nanosecond does not implement MyStringer")
|
||||||
|
} else {
|
||||||
|
fmt.Println("time.Nanosecond implements MyStringer")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO(mpl): restore
|
||||||
|
/*
|
||||||
|
tt = 42
|
||||||
|
bar, ok := tt.(MyStringer)
|
||||||
|
if !ok {
|
||||||
|
fmt.Println("42 does not implement MyStringer")
|
||||||
|
} else {
|
||||||
|
fmt.Println("42 implements MyStringer")
|
||||||
|
}
|
||||||
|
_ = bar
|
||||||
|
|
||||||
|
if _, ok := tt.(MyStringer); !ok {
|
||||||
|
fmt.Println("42 does not implement MyStringer")
|
||||||
|
} else {
|
||||||
|
fmt.Println("42 implements MyStringer")
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// TestStruct implements MyWriter
|
||||||
|
// 11
|
||||||
|
// 11
|
||||||
|
// TestStruct implements MyWriter
|
||||||
|
// time.Nanosecond implements MyStringer
|
||||||
|
// 1ns
|
||||||
|
// 1ns
|
||||||
|
// time.Nanosecond implements MyStringer
|
||||||
88
_test/assert1.go
Normal file
88
_test/assert1.go
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TestStruct struct{}
|
||||||
|
|
||||||
|
func (t TestStruct) String() string {
|
||||||
|
return "hello world"
|
||||||
|
}
|
||||||
|
|
||||||
|
type DummyStringer interface{
|
||||||
|
String() string
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
aType := reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
|
||||||
|
|
||||||
|
var t interface{}
|
||||||
|
t = time.Nanosecond
|
||||||
|
s, ok := t.(fmt.Stringer)
|
||||||
|
if !ok {
|
||||||
|
fmt.Println("time.Nanosecond does not implement fmt.Stringer")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println(s.String())
|
||||||
|
fmt.Println(t.(fmt.Stringer).String())
|
||||||
|
bType := reflect.TypeOf(time.Nanosecond)
|
||||||
|
fmt.Println(bType.Implements(aType))
|
||||||
|
|
||||||
|
// not redundant with the above, because it goes through a slightly different code path.
|
||||||
|
if _, ok := t.(fmt.Stringer); !ok {
|
||||||
|
fmt.Println("time.Nanosecond does not implement fmt.Stringer")
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
fmt.Println("time.Nanosecond implements fmt.Stringer")
|
||||||
|
}
|
||||||
|
|
||||||
|
t = 42
|
||||||
|
foo, ok := t.(fmt.Stringer)
|
||||||
|
if !ok {
|
||||||
|
fmt.Println("42 does not implement fmt.Stringer")
|
||||||
|
} else {
|
||||||
|
fmt.Println("42 implements fmt.Stringer")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_ = foo
|
||||||
|
|
||||||
|
if _, ok := t.(fmt.Stringer); !ok {
|
||||||
|
fmt.Println("42 does not implement fmt.Stringer")
|
||||||
|
} else {
|
||||||
|
fmt.Println("42 implements fmt.Stringer")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO(mpl): restore when fixed
|
||||||
|
// var tt interface{}
|
||||||
|
var tt DummyStringer
|
||||||
|
tt = TestStruct{}
|
||||||
|
ss, ok := tt.(fmt.Stringer)
|
||||||
|
if !ok {
|
||||||
|
fmt.Println("TestStuct does not implement fmt.Stringer")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println(ss.String())
|
||||||
|
fmt.Println(tt.(fmt.Stringer).String())
|
||||||
|
|
||||||
|
if _, ok := tt.(fmt.Stringer); !ok {
|
||||||
|
fmt.Println("TestStuct does not implement fmt.Stringer")
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
fmt.Println("TestStuct implements fmt.Stringer")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1ns
|
||||||
|
// 1ns
|
||||||
|
// true
|
||||||
|
// time.Nanosecond implements fmt.Stringer
|
||||||
|
// 42 does not implement fmt.Stringer
|
||||||
|
// 42 does not implement fmt.Stringer
|
||||||
|
// hello world
|
||||||
|
// hello world
|
||||||
|
// TestStuct implements fmt.Stringer
|
||||||
@@ -4,10 +4,16 @@ func f(a []int) interface{} {
|
|||||||
return cap(a)
|
return cap(a)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func g(a []int) int {
|
||||||
|
return cap(a)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
a := []int{1, 2}
|
a := []int{1, 2}
|
||||||
|
println(g(a))
|
||||||
println(f(a).(int))
|
println(f(a).(int))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 2
|
// 2
|
||||||
|
// 2
|
||||||
|
|||||||
18
_test/closure10.go
Normal file
18
_test/closure10.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
foos := []func(){}
|
||||||
|
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
a, b := i, i
|
||||||
|
foos = append(foos, func() { println(i, a, b) })
|
||||||
|
}
|
||||||
|
foos[0]()
|
||||||
|
foos[1]()
|
||||||
|
foos[2]()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 3 0 0
|
||||||
|
// 3 1 1
|
||||||
|
// 3 2 2
|
||||||
22
_test/closure11.go
Normal file
22
_test/closure11.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type T struct {
|
||||||
|
F func()
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
foos := []T{}
|
||||||
|
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
a := i
|
||||||
|
foos = append(foos, T{func() { println(i, a) }})
|
||||||
|
}
|
||||||
|
foos[0].F()
|
||||||
|
foos[1].F()
|
||||||
|
foos[2].F()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 3 0
|
||||||
|
// 3 1
|
||||||
|
// 3 2
|
||||||
25
_test/closure12.go
Normal file
25
_test/closure12.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type T struct {
|
||||||
|
F func()
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
foos := []T{}
|
||||||
|
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
a := i
|
||||||
|
n := fmt.Sprintf("i=%d", i)
|
||||||
|
foos = append(foos, T{func() { println(i, a, n) }})
|
||||||
|
}
|
||||||
|
foos[0].F()
|
||||||
|
foos[1].F()
|
||||||
|
foos[2].F()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 3 0 i=0
|
||||||
|
// 3 1 i=1
|
||||||
|
// 3 2 i=2
|
||||||
18
_test/closure9.go
Normal file
18
_test/closure9.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
foos := []func(){}
|
||||||
|
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
a := i
|
||||||
|
foos = append(foos, func() { println(i, a) })
|
||||||
|
}
|
||||||
|
foos[0]()
|
||||||
|
foos[1]()
|
||||||
|
foos[2]()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 3 0
|
||||||
|
// 3 1
|
||||||
|
// 3 2
|
||||||
52
_test/composite15.go
Normal file
52
_test/composite15.go
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func interfaceAsInts() {
|
||||||
|
var a interface{}
|
||||||
|
b := 2
|
||||||
|
c := 3
|
||||||
|
a = []int{b, c}
|
||||||
|
|
||||||
|
d, ok := a.([]int)
|
||||||
|
if !ok {
|
||||||
|
println("nope")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range d {
|
||||||
|
fmt.Println(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func interfaceAsInterfaces() {
|
||||||
|
var a, b, c interface{}
|
||||||
|
b = 2
|
||||||
|
c = 3
|
||||||
|
a = []interface{}{b, c}
|
||||||
|
|
||||||
|
d, ok := a.([]interface{})
|
||||||
|
if !ok {
|
||||||
|
println("nope")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println(d)
|
||||||
|
|
||||||
|
for _, v := range d {
|
||||||
|
fmt.Println(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
interfaceAsInts()
|
||||||
|
interfaceAsInterfaces()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2
|
||||||
|
// 3
|
||||||
|
// [2 3]
|
||||||
|
// 2
|
||||||
|
// 3
|
||||||
16
_test/composite16.go
Normal file
16
_test/composite16.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
body := url.Values{
|
||||||
|
"Action": {"none"},
|
||||||
|
}
|
||||||
|
fmt.Println(body)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// map[Action:[none]]
|
||||||
30
_test/composite17.go
Normal file
30
_test/composite17.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"html/template"
|
||||||
|
)
|
||||||
|
|
||||||
|
var str = `{{ stringOr .Data "test" }}`
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
_, err := template.New("test").
|
||||||
|
Funcs(template.FuncMap{
|
||||||
|
"stringOr": stringOr,
|
||||||
|
}).
|
||||||
|
Parse(str)
|
||||||
|
if err != nil {
|
||||||
|
println(err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
println("success")
|
||||||
|
}
|
||||||
|
|
||||||
|
func stringOr(v, def string) string {
|
||||||
|
if v == "" {
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// success
|
||||||
15
_test/const22.go
Normal file
15
_test/const22.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
const (
|
||||||
|
numDec uint8 = (1 << iota) / 2
|
||||||
|
numHex
|
||||||
|
numOct
|
||||||
|
numFloat
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
println(13 & (numHex | numOct))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
12
_test/const23.go
Normal file
12
_test/const23.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
const maxlen = len("hello")
|
||||||
|
|
||||||
|
var gfm = [maxlen]byte{}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
println(len(gfm))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 5
|
||||||
14
_test/const24.go
Normal file
14
_test/const24.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
var aa = [...]int{1, 2, 3}
|
||||||
|
|
||||||
|
const maxlen = cap(aa)
|
||||||
|
|
||||||
|
var gfm = [maxlen]byte{}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
println(len(gfm))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 3
|
||||||
11
_test/const25.go
Normal file
11
_test/const25.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
const (
|
||||||
|
FGBlack Attribute = iota + 30
|
||||||
|
)
|
||||||
|
|
||||||
|
type Attribute int
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
println(FGBlack)
|
||||||
|
}
|
||||||
@@ -2,10 +2,15 @@ package main
|
|||||||
|
|
||||||
func f1(a int) interface{} { return a + 1 }
|
func f1(a int) interface{} { return a + 1 }
|
||||||
|
|
||||||
|
func f2(a int64) interface{} { return a + 1 }
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
c := f1(3)
|
c := f1(3)
|
||||||
println(c.(int))
|
println(c.(int))
|
||||||
|
b := f2(3)
|
||||||
|
println(b.(int64))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 4
|
// 4
|
||||||
|
// 4
|
||||||
|
|||||||
@@ -2,7 +2,13 @@ package main
|
|||||||
|
|
||||||
func f1(a int) int { return a + 1 }
|
func f1(a int) int { return a + 1 }
|
||||||
|
|
||||||
func f2(a int) interface{} { return f1(a) }
|
func f2(a int) interface{} {
|
||||||
|
// TODO: re-enable the optimized case below, once we've figured out why it
|
||||||
|
// interferes with the empty interface model.
|
||||||
|
// return f1(a)
|
||||||
|
var foo interface{} = f1(a)
|
||||||
|
return foo
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
c := f2(3)
|
c := f2(3)
|
||||||
|
|||||||
32
_test/interface47.go
Normal file
32
_test/interface47.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type Doer interface {
|
||||||
|
Do() error
|
||||||
|
}
|
||||||
|
|
||||||
|
type T struct {
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *T) Do() error { println("in do"); return nil }
|
||||||
|
|
||||||
|
func f() (Doer, error) { return &T{"truc"}, nil }
|
||||||
|
|
||||||
|
type Ev struct {
|
||||||
|
doer func() (Doer, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Ev) do() {
|
||||||
|
d, _ := e.doer()
|
||||||
|
d.Do()
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
e := &Ev{f}
|
||||||
|
println(e != nil)
|
||||||
|
e.do()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// in do
|
||||||
17
_test/interface48.go
Normal file
17
_test/interface48.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type I1 interface{ A }
|
||||||
|
|
||||||
|
type A = I2
|
||||||
|
|
||||||
|
type I2 interface{ F() I1 }
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var i I1
|
||||||
|
fmt.Println(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// <nil>
|
||||||
45
_test/interface49.go
Normal file
45
_test/interface49.go
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type Descriptor interface {
|
||||||
|
ParentFile() FileDescriptor
|
||||||
|
}
|
||||||
|
|
||||||
|
type FileDescriptor interface {
|
||||||
|
Enums() EnumDescriptors
|
||||||
|
Services() ServiceDescriptors
|
||||||
|
}
|
||||||
|
|
||||||
|
type EnumDescriptors interface {
|
||||||
|
Get(i int) EnumDescriptor
|
||||||
|
}
|
||||||
|
|
||||||
|
type EnumDescriptor interface {
|
||||||
|
Values() EnumValueDescriptors
|
||||||
|
}
|
||||||
|
|
||||||
|
type EnumValueDescriptors interface {
|
||||||
|
Get(i int) EnumValueDescriptor
|
||||||
|
}
|
||||||
|
|
||||||
|
type EnumValueDescriptor interface {
|
||||||
|
Descriptor
|
||||||
|
}
|
||||||
|
|
||||||
|
type ServiceDescriptors interface {
|
||||||
|
Get(i int) ServiceDescriptor
|
||||||
|
}
|
||||||
|
|
||||||
|
type ServiceDescriptor interface {
|
||||||
|
Descriptor
|
||||||
|
isServiceDescriptor
|
||||||
|
}
|
||||||
|
|
||||||
|
type isServiceDescriptor interface{ ProtoType(ServiceDescriptor) }
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var d Descriptor
|
||||||
|
println(d == nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
13
_test/interface50.go
Normal file
13
_test/interface50.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
a := true
|
||||||
|
var b interface{} = 5
|
||||||
|
println(b.(int))
|
||||||
|
b = a == true
|
||||||
|
println(b.(bool))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 5
|
||||||
|
// true
|
||||||
23
_test/interface51.go
Normal file
23
_test/interface51.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type Error interface {
|
||||||
|
error
|
||||||
|
Message() string
|
||||||
|
}
|
||||||
|
|
||||||
|
type T struct {
|
||||||
|
Msg string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *T) Error() string { return t.Msg }
|
||||||
|
func (t *T) Message() string { return "message:" + t.Msg }
|
||||||
|
|
||||||
|
func newError() Error { return &T{"test"} }
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
e := newError()
|
||||||
|
println(e.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// test
|
||||||
40
_test/issue-1007.go
Normal file
40
_test/issue-1007.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type TypeA struct {
|
||||||
|
B TypeB
|
||||||
|
}
|
||||||
|
|
||||||
|
type TypeB struct {
|
||||||
|
C1 *TypeC
|
||||||
|
C2 *TypeC
|
||||||
|
}
|
||||||
|
|
||||||
|
type TypeC struct {
|
||||||
|
Val string
|
||||||
|
D *TypeD
|
||||||
|
D2 *TypeD
|
||||||
|
}
|
||||||
|
|
||||||
|
type TypeD struct {
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func build() *TypeA {
|
||||||
|
return &TypeA{
|
||||||
|
B: TypeB{
|
||||||
|
C2: &TypeC{Val: "22"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Bar(s string) string {
|
||||||
|
a := build()
|
||||||
|
return s + "-" + a.B.C2.Val
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
println(Bar("test"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// test-22
|
||||||
22
_test/issue-1010.go
Normal file
22
_test/issue-1010.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MyJsonMarshaler struct{ n int }
|
||||||
|
|
||||||
|
func (m MyJsonMarshaler) MarshalJSON() ([]byte, error) {
|
||||||
|
return []byte(fmt.Sprintf(`{"num": %d}`, m.n)), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ch := make(chan json.Marshaler, 1)
|
||||||
|
ch <- MyJsonMarshaler{2}
|
||||||
|
m, err := json.Marshal(<-ch)
|
||||||
|
fmt.Println(string(m), err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// {"num":2} <nil>
|
||||||
17
_test/issue-1022.go
Normal file
17
_test/issue-1022.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
defer func() {
|
||||||
|
r := recover()
|
||||||
|
if r != nil {
|
||||||
|
fmt.Println(r)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
panic("Ho Ho Ho!")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// Ho Ho Ho!
|
||||||
23
_test/issue-1052.go
Normal file
23
_test/issue-1052.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
a, b := 1, 1
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
fmt.Println(a)
|
||||||
|
a, b = b, a+b
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
|
// 1
|
||||||
|
// 2
|
||||||
|
// 3
|
||||||
|
// 5
|
||||||
|
// 8
|
||||||
|
// 13
|
||||||
|
// 21
|
||||||
|
// 34
|
||||||
|
// 55
|
||||||
20
_test/issue-1065.go
Normal file
20
_test/issue-1065.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type AST struct {
|
||||||
|
Num int
|
||||||
|
Children []AST
|
||||||
|
}
|
||||||
|
|
||||||
|
func newAST(num int, root AST, children ...AST) AST {
|
||||||
|
return AST{num, append([]AST{root}, children...)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ast := newAST(1, AST{}, AST{})
|
||||||
|
fmt.Println(ast)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// {1 [{0 []} {0 []}]}
|
||||||
19
_test/issue-1068.go
Normal file
19
_test/issue-1068.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type I interface {
|
||||||
|
Hello()
|
||||||
|
}
|
||||||
|
|
||||||
|
type T struct{}
|
||||||
|
|
||||||
|
func (t T) Hello() { println("hello") }
|
||||||
|
|
||||||
|
type I2 I
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var i I2 = T{}
|
||||||
|
i.Hello()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
14
_test/issue-1088.go
Normal file
14
_test/issue-1088.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
for i, ch := range "日本語" {
|
||||||
|
fmt.Printf("%#U starts at byte position %d\n", ch, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// U+65E5 '日' starts at byte position 0
|
||||||
|
// U+672C '本' starts at byte position 3
|
||||||
|
// U+8A9E '語' starts at byte position 6
|
||||||
13
_test/issue-1089.go
Normal file
13
_test/issue-1089.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(`"` + time.RFC3339Nano + `"`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// "2006-01-02T15:04:05.999999999Z07:00"
|
||||||
13
_test/issue-1093.go
Normal file
13
_test/issue-1093.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func b() string {
|
||||||
|
return "b"
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var x int
|
||||||
|
x = "a" + b()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error:
|
||||||
|
// 9:6: cannot use type string as type int in assignment
|
||||||
12
_test/issue-1094.go
Normal file
12
_test/issue-1094.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var x interface{}
|
||||||
|
x = "a" + fmt.Sprintf("b")
|
||||||
|
fmt.Printf("%v %T\n", x, x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// ab string
|
||||||
17
_test/issue-1101.go
Normal file
17
_test/issue-1101.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
method := "POST"
|
||||||
|
switch method {
|
||||||
|
case http.MethodPost:
|
||||||
|
fmt.Println("It's a post!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// It's a post!
|
||||||
24
_test/issue-1115.go
Normal file
24
_test/issue-1115.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
outer:
|
||||||
|
for y := 0; y < 10; y++ {
|
||||||
|
for x := 0; x < 10; x++ {
|
||||||
|
if x == 5 && y == 5 {
|
||||||
|
break outer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println(y)
|
||||||
|
}
|
||||||
|
fmt.Println("Yay! I finished!")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 0
|
||||||
|
// 1
|
||||||
|
// 2
|
||||||
|
// 3
|
||||||
|
// 4
|
||||||
|
// Yay! I finished!
|
||||||
23
_test/issue-1126.go
Normal file
23
_test/issue-1126.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
err := errors.New("hello there")
|
||||||
|
|
||||||
|
switch true {
|
||||||
|
case err == nil:
|
||||||
|
break
|
||||||
|
case strings.Contains(err.Error(), "hello"):
|
||||||
|
fmt.Println("True!")
|
||||||
|
default:
|
||||||
|
fmt.Println("False!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// True!
|
||||||
10
_test/issue-1128.go
Normal file
10
_test/issue-1128.go
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "net"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
c := append(net.Buffers{}, []byte{})
|
||||||
|
println(len(c))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output: 1
|
||||||
20
_test/issue-1134.go
Normal file
20
_test/issue-1134.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type I interface {
|
||||||
|
Hello()
|
||||||
|
}
|
||||||
|
|
||||||
|
type T struct {
|
||||||
|
Name string
|
||||||
|
Child []*T
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *T) Hello() { println("Hello", t.Name) }
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var i I = new(T)
|
||||||
|
i.Hello()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// Hello
|
||||||
22
_test/issue-1136.go
Normal file
22
_test/issue-1136.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
type T struct {
|
||||||
|
r io.Reader
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *T) Read(p []byte) (n int, err error) { n, err = t.r.Read(p); return }
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
x := io.LimitedReader{}
|
||||||
|
y := io.Reader(&x)
|
||||||
|
y = &T{y}
|
||||||
|
fmt.Println(y.Read([]byte("")))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 0 EOF
|
||||||
14
_test/issue-1145.go
Normal file
14
_test/issue-1145.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "sync"
|
||||||
|
|
||||||
|
type F func()
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
var f F = wg.Done
|
||||||
|
println(f != nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
25
_test/issue-1156.go
Normal file
25
_test/issue-1156.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type myInterface interface {
|
||||||
|
myFunc() string
|
||||||
|
}
|
||||||
|
|
||||||
|
type V struct{}
|
||||||
|
|
||||||
|
func (v *V) myFunc() string { return "hello" }
|
||||||
|
|
||||||
|
type U struct {
|
||||||
|
v myInterface
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *U) myFunc() string { return u.v.myFunc() }
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
x := V{}
|
||||||
|
y := myInterface(&x)
|
||||||
|
y = &U{y}
|
||||||
|
println(y.myFunc())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
15
_test/issue-981.go
Normal file
15
_test/issue-981.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
dp := make(map[int]int)
|
||||||
|
dp[0] = 1
|
||||||
|
for i := 1; i < 10; i++ {
|
||||||
|
dp[i] = dp[i-1] + dp[i-2]
|
||||||
|
}
|
||||||
|
fmt.Printf("%v\n", dp)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// map[0:1 1:1 2:2 3:3 4:5 5:8 6:13 7:21 8:34 9:55]
|
||||||
15
_test/issue-993.go
Normal file
15
_test/issue-993.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
var m map[string]int64
|
||||||
|
|
||||||
|
func initVar() {
|
||||||
|
m = make(map[string]int64)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
initVar()
|
||||||
|
println(len(m))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 0
|
||||||
27
_test/map30.go
Normal file
27
_test/map30.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
func f(s string) string { return "hello " + s }
|
||||||
|
|
||||||
|
func g(s string) string { return "hi " + s }
|
||||||
|
|
||||||
|
var methods = map[string]func(string) string{
|
||||||
|
"f": f,
|
||||||
|
"h": strings.ToLower,
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
methods["i"] = strings.ToUpper
|
||||||
|
methods["g"] = g
|
||||||
|
println(methods["f"]("test"))
|
||||||
|
println(methods["g"]("test"))
|
||||||
|
println(methods["i"]("test"))
|
||||||
|
println(methods["h"]("TEST"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello test
|
||||||
|
// hi test
|
||||||
|
// TEST
|
||||||
|
// test
|
||||||
@@ -12,10 +12,16 @@ type Hi interface {
|
|||||||
Hello() string
|
Hello() string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Hey interface {
|
||||||
|
Hello() string
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Root) Hello() string { return "Hello " + r.Name }
|
func (r *Root) Hello() string { return "Hello " + r.Name }
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var one interface{} = &One{Root{Name: "test2"}}
|
// TODO(mpl): restore when type assertions work again.
|
||||||
|
// var one interface{} = &One{Root{Name: "test2"}}
|
||||||
|
var one Hey = &One{Root{Name: "test2"}}
|
||||||
println(one.(Hi).Hello())
|
println(one.(Hi).Hello())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
26
_test/method36.go
Normal file
26
_test/method36.go
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type I interface{ Hello() }
|
||||||
|
|
||||||
|
type T struct{ Name string }
|
||||||
|
|
||||||
|
func (t *T) Hello() { println("Hello", t.Name) }
|
||||||
|
|
||||||
|
type FT func(i I)
|
||||||
|
|
||||||
|
type ST struct{ Handler FT }
|
||||||
|
|
||||||
|
func newF() FT {
|
||||||
|
return func(i I) {
|
||||||
|
i.Hello()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
st := &ST{}
|
||||||
|
st.Handler = newF()
|
||||||
|
st.Handler(&T{"test"})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// Hello test
|
||||||
19
_test/method37.go
Normal file
19
_test/method37.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func writeBufs(bufs ...[]byte) error {
|
||||||
|
b := net.Buffers(bufs)
|
||||||
|
_, err := b.WriteTo(os.Stdout)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
writeBufs([]byte("hello"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
28
_test/ret8.go
Normal file
28
_test/ret8.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type CustomError string
|
||||||
|
|
||||||
|
func (s CustomError) Error() string {
|
||||||
|
return string(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCustomError(errorText string) CustomError {
|
||||||
|
return CustomError(errorText)
|
||||||
|
}
|
||||||
|
|
||||||
|
func fail() (err error) {
|
||||||
|
return NewCustomError("Everything is going wrong!")
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(fail())
|
||||||
|
var myError error
|
||||||
|
myError = NewCustomError("ok")
|
||||||
|
fmt.Println(myError)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// Everything is going wrong!
|
||||||
|
// ok
|
||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
period = 100 * time.Millisecond
|
period = 100 * time.Millisecond
|
||||||
precision = 5 * time.Millisecond
|
precision = 7 * time.Millisecond
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
41
_test/struct59.go
Normal file
41
_test/struct59.go
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type A struct {
|
||||||
|
B map[string]*B
|
||||||
|
C map[string]*C
|
||||||
|
}
|
||||||
|
|
||||||
|
type C struct {
|
||||||
|
D *D
|
||||||
|
E *E
|
||||||
|
}
|
||||||
|
|
||||||
|
type D struct {
|
||||||
|
F *F
|
||||||
|
G []G
|
||||||
|
}
|
||||||
|
|
||||||
|
type E struct {
|
||||||
|
H []H
|
||||||
|
F *F
|
||||||
|
}
|
||||||
|
|
||||||
|
type B struct{}
|
||||||
|
type F struct{}
|
||||||
|
type G struct{}
|
||||||
|
type H struct{}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
conf := &A{
|
||||||
|
B: make(map[string]*B),
|
||||||
|
C: make(map[string]*C),
|
||||||
|
}
|
||||||
|
fmt.Println(conf)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// &{map[] map[]}
|
||||||
@@ -2,12 +2,26 @@ package main
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var a interface{}
|
var a interface{}
|
||||||
|
a = []int{3}
|
||||||
switch a.(type) {
|
switch a.(type) {
|
||||||
case []int:
|
case []int:
|
||||||
|
println("a is []int")
|
||||||
case []string:
|
case []string:
|
||||||
|
println("a is []string")
|
||||||
|
}
|
||||||
|
|
||||||
|
var b interface{}
|
||||||
|
b = []string{"hello"}
|
||||||
|
switch b.(type) {
|
||||||
|
case []int:
|
||||||
|
println("b is []int")
|
||||||
|
case []string:
|
||||||
|
println("b is []string")
|
||||||
}
|
}
|
||||||
println("bye")
|
println("bye")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
|
// a is []int
|
||||||
|
// b is []string
|
||||||
// bye
|
// bye
|
||||||
|
|||||||
15
_test/time15.go
Normal file
15
_test/time15.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type TimeValue time.Time
|
||||||
|
|
||||||
|
func (v *TimeValue) decode() { println("in decode") }
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var tv TimeValue
|
||||||
|
tv.decode()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// in decode
|
||||||
11
_test/time16.go
Normal file
11
_test/time16.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
localTime := time.ANSIC
|
||||||
|
println(localTime)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// Mon Jan _2 15:04:05 2006
|
||||||
@@ -12,11 +12,13 @@ type S struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
size := unsafe.Sizeof(S{})
|
x := S{}
|
||||||
align := unsafe.Alignof(S{})
|
size := unsafe.Sizeof(x)
|
||||||
|
align := unsafe.Alignof(x.Y)
|
||||||
|
offset := unsafe.Offsetof(x.Z)
|
||||||
|
|
||||||
fmt.Println(size, align)
|
fmt.Println(size, align, offset)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 24 8
|
// 24 8 16
|
||||||
|
|||||||
15
_test/var15.go
Normal file
15
_test/var15.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
var a int = 2
|
||||||
|
|
||||||
|
func inca() {
|
||||||
|
a = a + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
inca()
|
||||||
|
println(a)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 3
|
||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/traefik/yaegi/extract"
|
"github.com/traefik/yaegi/extract"
|
||||||
@@ -50,7 +50,7 @@ func extractCmd(arg []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if name == "" {
|
if name == "" {
|
||||||
name = path.Base(wd)
|
name = filepath.Base(wd)
|
||||||
}
|
}
|
||||||
ext := extract.Extractor{
|
ext := extract.Extractor{
|
||||||
Dest: name,
|
Dest: name,
|
||||||
@@ -102,7 +102,7 @@ func genLicense(fname string) (string, error) {
|
|||||||
|
|
||||||
f, err := os.Open(fname)
|
f, err := os.Open(fname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("could not open LICENSE file: %v", err)
|
return "", fmt.Errorf("could not open LICENSE file: %w", err)
|
||||||
}
|
}
|
||||||
defer func() { _ = f.Close() }()
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
@@ -116,7 +116,7 @@ func genLicense(fname string) (string, error) {
|
|||||||
license.WriteString("//" + txt + "\n")
|
license.WriteString("//" + txt + "\n")
|
||||||
}
|
}
|
||||||
if sc.Err() != nil {
|
if sc.Err() != nil {
|
||||||
return "", fmt.Errorf("could not scan LICENSE file: %v", err)
|
return "", fmt.Errorf("could not scan LICENSE file: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return license.String(), nil
|
return license.String(), nil
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"go/build"
|
"go/build"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -18,6 +19,7 @@ import (
|
|||||||
|
|
||||||
func run(arg []string) error {
|
func run(arg []string) error {
|
||||||
var interactive bool
|
var interactive bool
|
||||||
|
var noAutoImport bool
|
||||||
var tags string
|
var tags string
|
||||||
var cmd string
|
var cmd string
|
||||||
var err error
|
var err error
|
||||||
@@ -33,6 +35,7 @@ func run(arg []string) error {
|
|||||||
rflag.BoolVar(&useUnrestricted, "unrestricted", useUnrestricted, "include unrestricted symbols")
|
rflag.BoolVar(&useUnrestricted, "unrestricted", useUnrestricted, "include unrestricted symbols")
|
||||||
rflag.StringVar(&tags, "tags", "", "set a list of build tags")
|
rflag.StringVar(&tags, "tags", "", "set a list of build tags")
|
||||||
rflag.BoolVar(&useUnsafe, "unsafe", useUnsafe, "include unsafe symbols")
|
rflag.BoolVar(&useUnsafe, "unsafe", useUnsafe, "include unsafe symbols")
|
||||||
|
rflag.BoolVar(&noAutoImport, "noautoimport", false, "do not auto import pre-compiled packages. Import names that would result in collisions (e.g. rand from crypto/rand and rand from math/rand) are automatically renamed (crypto_rand and math_rand)")
|
||||||
rflag.StringVar(&cmd, "e", "", "set the command to be executed (instead of script or/and shell)")
|
rflag.StringVar(&cmd, "e", "", "set the command to be executed (instead of script or/and shell)")
|
||||||
rflag.Usage = func() {
|
rflag.Usage = func() {
|
||||||
fmt.Println("Usage: yaegi run [options] [path] [args]")
|
fmt.Println("Usage: yaegi run [options] [path] [args]")
|
||||||
@@ -45,48 +48,68 @@ func run(arg []string) error {
|
|||||||
args := rflag.Args()
|
args := rflag.Args()
|
||||||
|
|
||||||
i := interp.New(interp.Options{GoPath: build.Default.GOPATH, BuildTags: strings.Split(tags, ",")})
|
i := interp.New(interp.Options{GoPath: build.Default.GOPATH, BuildTags: strings.Split(tags, ",")})
|
||||||
i.Use(stdlib.Symbols)
|
if err := i.Use(stdlib.Symbols); err != nil {
|
||||||
i.Use(interp.Symbols)
|
return err
|
||||||
|
}
|
||||||
|
if err := i.Use(interp.Symbols); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if useSyscall {
|
if useSyscall {
|
||||||
i.Use(syscall.Symbols)
|
if err := i.Use(syscall.Symbols); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
// Using a environment var allows a nested interpreter to import the syscall package.
|
// Using a environment var allows a nested interpreter to import the syscall package.
|
||||||
if err := os.Setenv("YAEGI_SYSCALL", "1"); err != nil {
|
if err := os.Setenv("YAEGI_SYSCALL", "1"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if useUnsafe {
|
if useUnsafe {
|
||||||
i.Use(unsafe.Symbols)
|
if err := i.Use(unsafe.Symbols); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if err := os.Setenv("YAEGI_UNSAFE", "1"); err != nil {
|
if err := os.Setenv("YAEGI_UNSAFE", "1"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if useUnrestricted {
|
if useUnrestricted {
|
||||||
// Use of unrestricted symbols should always follow stdlib and syscall symbols, to update them.
|
// Use of unrestricted symbols should always follow stdlib and syscall symbols, to update them.
|
||||||
i.Use(unrestricted.Symbols)
|
if err := i.Use(unrestricted.Symbols); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if err := os.Setenv("YAEGI_UNRESTRICTED", "1"); err != nil {
|
if err := os.Setenv("YAEGI_UNRESTRICTED", "1"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmd != "" {
|
if cmd != "" {
|
||||||
_, err = i.Eval(cmd)
|
if !noAutoImport {
|
||||||
|
i.ImportUsed()
|
||||||
|
}
|
||||||
|
var v reflect.Value
|
||||||
|
v, err = i.Eval(cmd)
|
||||||
|
if len(args) == 0 && v.IsValid() {
|
||||||
|
fmt.Println(v)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
if interactive || cmd == "" {
|
if cmd == "" || interactive {
|
||||||
showError(err)
|
showError(err)
|
||||||
|
if !noAutoImport {
|
||||||
|
i.ImportUsed()
|
||||||
|
}
|
||||||
_, err = i.REPL()
|
_, err = i.REPL()
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skip first os arg to set command line as expected by interpreted main
|
// Skip first os arg to set command line as expected by interpreted main.
|
||||||
path := args[0]
|
path := args[0]
|
||||||
os.Args = arg
|
os.Args = arg
|
||||||
flag.CommandLine = flag.NewFlagSet(path, flag.ExitOnError)
|
flag.CommandLine = flag.NewFlagSet(path, flag.ExitOnError)
|
||||||
|
|
||||||
if isFile(path) {
|
if isFile(path) {
|
||||||
err = runFile(i, path)
|
err = runFile(i, path, noAutoImport)
|
||||||
} else {
|
} else {
|
||||||
_, err = i.EvalPath(path)
|
_, err = i.EvalPath(path)
|
||||||
}
|
}
|
||||||
@@ -106,7 +129,7 @@ func isFile(path string) bool {
|
|||||||
return err == nil && fi.Mode().IsRegular()
|
return err == nil && fi.Mode().IsRegular()
|
||||||
}
|
}
|
||||||
|
|
||||||
func runFile(i *interp.Interpreter, path string) error {
|
func runFile(i *interp.Interpreter, path string, noAutoImport bool) error {
|
||||||
b, err := ioutil.ReadFile(path)
|
b, err := ioutil.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -115,6 +138,9 @@ func runFile(i *interp.Interpreter, path string) error {
|
|||||||
if s := string(b); strings.HasPrefix(s, "#!") {
|
if s := string(b); strings.HasPrefix(s, "#!") {
|
||||||
// Allow executable go scripts, Have the same behavior as in interactive mode.
|
// Allow executable go scripts, Have the same behavior as in interactive mode.
|
||||||
s = strings.Replace(s, "#!", "//", 1)
|
s = strings.Replace(s, "#!", "//", 1)
|
||||||
|
if !noAutoImport {
|
||||||
|
i.ImportUsed()
|
||||||
|
}
|
||||||
_, err = i.Eval(s)
|
_, err = i.Eval(s)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"go/build"
|
"go/build"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -102,25 +103,47 @@ func test(arg []string) (err error) {
|
|||||||
testing.Init()
|
testing.Init()
|
||||||
os.Args = tf
|
os.Args = tf
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
path += string(filepath.Separator)
|
||||||
|
var dir string
|
||||||
|
|
||||||
|
switch strings.Split(path, string(filepath.Separator))[0] {
|
||||||
|
case ".", "..", string(filepath.Separator):
|
||||||
|
dir = path
|
||||||
|
default:
|
||||||
|
dir = filepath.Join(build.Default.GOPATH, "src", path)
|
||||||
|
}
|
||||||
|
if err = os.Chdir(dir); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
i := interp.New(interp.Options{GoPath: build.Default.GOPATH, BuildTags: strings.Split(tags, ",")})
|
i := interp.New(interp.Options{GoPath: build.Default.GOPATH, BuildTags: strings.Split(tags, ",")})
|
||||||
i.Use(stdlib.Symbols)
|
if err := i.Use(stdlib.Symbols); err != nil {
|
||||||
i.Use(interp.Symbols)
|
return err
|
||||||
|
}
|
||||||
|
if err := i.Use(interp.Symbols); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if useSyscall {
|
if useSyscall {
|
||||||
i.Use(syscall.Symbols)
|
if err := i.Use(syscall.Symbols); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
// Using a environment var allows a nested interpreter to import the syscall package.
|
// Using a environment var allows a nested interpreter to import the syscall package.
|
||||||
if err := os.Setenv("YAEGI_SYSCALL", "1"); err != nil {
|
if err := os.Setenv("YAEGI_SYSCALL", "1"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if useUnrestricted {
|
if useUnrestricted {
|
||||||
i.Use(unrestricted.Symbols)
|
if err := i.Use(unrestricted.Symbols); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if err := os.Setenv("YAEGI_UNRESTRICTED", "1"); err != nil {
|
if err := os.Setenv("YAEGI_UNRESTRICTED", "1"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if useUnsafe {
|
if useUnsafe {
|
||||||
i.Use(unsafe.Symbols)
|
if err := i.Use(unsafe.Symbols); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if err := os.Setenv("YAEGI_UNSAFE", "1"); err != nil {
|
if err := os.Setenv("YAEGI_UNSAFE", "1"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,9 +50,9 @@ func TestYaegiCmdCancel(t *testing.T) {
|
|||||||
|
|
||||||
yaegi := filepath.Join(tmp, "yaegi")
|
yaegi := filepath.Join(tmp, "yaegi")
|
||||||
build := exec.Command("go", "build", "-race", "-o", yaegi, ".")
|
build := exec.Command("go", "build", "-race", "-o", yaegi, ".")
|
||||||
err = build.Run()
|
out, err := build.CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to build yaegi command: %v", err)
|
t.Fatalf("failed to build yaegi command: %v: %s", err, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test src must be terminated by a single newline.
|
// Test src must be terminated by a single newline.
|
||||||
@@ -82,7 +82,7 @@ func TestYaegiCmdCancel(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("failed pipe test source to yaegi command: %v", err)
|
t.Errorf("failed pipe test source to yaegi command: %v", err)
|
||||||
}
|
}
|
||||||
Sleep(200 * time.Millisecond)
|
Sleep(500 * time.Millisecond)
|
||||||
err = cmd.Process.Signal(os.Interrupt)
|
err = cmd.Process.Signal(os.Interrupt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("failed to send os.Interrupt to yaegi command: %v", err)
|
t.Errorf("failed to send os.Interrupt to yaegi command: %v", err)
|
||||||
|
|||||||
@@ -9,7 +9,9 @@ import (
|
|||||||
|
|
||||||
func TestFunctionCall(t *testing.T) {
|
func TestFunctionCall(t *testing.T) {
|
||||||
i := interp.New(interp.Options{GoPath: "./_pkg"})
|
i := interp.New(interp.Options{GoPath: "./_pkg"})
|
||||||
i.Use(stdlib.Symbols)
|
if err := i.Use(stdlib.Symbols); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
_, err := i.Eval(`import "foo/bar"`)
|
_, err := i.Eval(`import "foo/bar"`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -10,7 +10,9 @@ import (
|
|||||||
|
|
||||||
func TestGetFunc(t *testing.T) {
|
func TestGetFunc(t *testing.T) {
|
||||||
i := interp.New(interp.Options{GoPath: "./_gopath/"})
|
i := interp.New(interp.Options{GoPath: "./_gopath/"})
|
||||||
i.Use(stdlib.Symbols)
|
if err := i.Use(stdlib.Symbols); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := i.Eval(`import "github.com/foo/bar"`); err != nil {
|
if _, err := i.Eval(`import "github.com/foo/bar"`); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|||||||
@@ -104,18 +104,21 @@ func TestPackages(t *testing.T) {
|
|||||||
for _, test := range testCases {
|
for _, test := range testCases {
|
||||||
test := test
|
test := test
|
||||||
t.Run(test.desc, func(t *testing.T) {
|
t.Run(test.desc, func(t *testing.T) {
|
||||||
goPath, err := filepath.Abs(test.goPath)
|
goPath, err := filepath.Abs(filepath.FromSlash(test.goPath))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var stdout, stderr bytes.Buffer
|
var stdout, stderr bytes.Buffer
|
||||||
i := interp.New(interp.Options{GoPath: goPath, Stdout: &stdout, Stderr: &stderr})
|
i := interp.New(interp.Options{GoPath: goPath, Stdout: &stdout, Stderr: &stderr})
|
||||||
i.Use(stdlib.Symbols) // Use binary standard library
|
// Use binary standard library
|
||||||
|
if err := i.Use(stdlib.Symbols); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
var msg string
|
var msg string
|
||||||
if test.evalFile != "" {
|
if test.evalFile != "" {
|
||||||
if _, err := i.EvalPath(test.evalFile); err != nil {
|
if _, err := i.EvalPath(filepath.FromSlash(test.evalFile)); err != nil {
|
||||||
fatalStderrf(t, "%v", err)
|
fatalStderrf(t, "%v", err)
|
||||||
}
|
}
|
||||||
msg = stdout.String()
|
msg = stdout.String()
|
||||||
@@ -146,6 +149,8 @@ func TestPackages(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func fatalStderrf(t *testing.T, format string, args ...interface{}) {
|
func fatalStderrf(t *testing.T, format string, args ...interface{}) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
fmt.Fprintf(os.Stderr, format+"\n", args...)
|
fmt.Fprintf(os.Stderr, format+"\n", args...)
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
@@ -159,7 +164,7 @@ func TestPackagesError(t *testing.T) {
|
|||||||
{
|
{
|
||||||
desc: "different packages in the same directory",
|
desc: "different packages in the same directory",
|
||||||
goPath: "./_pkg9/",
|
goPath: "./_pkg9/",
|
||||||
expected: "1:21: import \"github.com/foo/pkg\" error: found packages pkg and pkgfalse in _pkg9/src/github.com/foo/pkg",
|
expected: `1:21: import "github.com/foo/pkg" error: found packages pkg and pkgfalse in ` + filepath.FromSlash("_pkg9/src/github.com/foo/pkg"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,7 +173,10 @@ func TestPackagesError(t *testing.T) {
|
|||||||
t.Run(test.desc, func(t *testing.T) {
|
t.Run(test.desc, func(t *testing.T) {
|
||||||
// Init go interpreter
|
// Init go interpreter
|
||||||
i := interp.New(interp.Options{GoPath: test.goPath})
|
i := interp.New(interp.Options{GoPath: test.goPath})
|
||||||
i.Use(stdlib.Symbols) // Use binary standard library
|
// Use binary standard library
|
||||||
|
if err := i.Use(stdlib.Symbols); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
// Load pkg from sources
|
// Load pkg from sources
|
||||||
_, err := i.Eval(`import "github.com/foo/pkg"`)
|
_, err := i.Eval(`import "github.com/foo/pkg"`)
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ import (
|
|||||||
"text/template"
|
"text/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
const model = `// Code generated by 'yaegi extract {{.PkgName}}'. DO NOT EDIT.
|
const model = `// Code generated by 'yaegi extract {{.ImportPath}}'. DO NOT EDIT.
|
||||||
|
|
||||||
{{.License}}
|
{{.License}}
|
||||||
|
|
||||||
@@ -39,7 +39,7 @@ import (
|
|||||||
"{{$key}}"
|
"{{$key}}"
|
||||||
{{- end}}
|
{{- end}}
|
||||||
{{- end}}
|
{{- end}}
|
||||||
"{{.PkgName}}"
|
"{{.ImportPath}}"
|
||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -167,8 +167,8 @@ func (e *Extractor) genContent(importPath string, p *types.Package) ([]byte, err
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
pname := path.Base(importPath) + "." + name
|
pname := p.Name() + "." + name
|
||||||
if rname := path.Base(importPath) + name; restricted[rname] {
|
if rname := p.Name() + name; restricted[rname] {
|
||||||
// Restricted symbol, locally provided by stdlib wrapper.
|
// Restricted symbol, locally provided by stdlib wrapper.
|
||||||
pname = rname
|
pname = rname
|
||||||
}
|
}
|
||||||
@@ -203,7 +203,15 @@ func (e *Extractor) genContent(importPath string, p *types.Package) ([]byte, err
|
|||||||
if args[j] = v.Name(); args[j] == "" {
|
if args[j] = v.Name(); args[j] == "" {
|
||||||
args[j] = fmt.Sprintf("a%d", j)
|
args[j] = fmt.Sprintf("a%d", j)
|
||||||
}
|
}
|
||||||
params[j] = args[j] + " " + types.TypeString(v.Type(), qualify)
|
// process interface method variadic parameter
|
||||||
|
if sign.Variadic() && j == len(args)-1 { // check is last arg
|
||||||
|
// only replace the first "[]" to "..."
|
||||||
|
at := types.TypeString(v.Type(), qualify)[2:]
|
||||||
|
params[j] = args[j] + " ..." + at
|
||||||
|
args[j] += "..."
|
||||||
|
} else {
|
||||||
|
params[j] = args[j] + " " + types.TypeString(v.Type(), qualify)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
arg := "(" + strings.Join(args, ", ") + ")"
|
arg := "(" + strings.Join(args, ", ") + ")"
|
||||||
param := "(" + strings.Join(params, ", ") + ")"
|
param := "(" + strings.Join(params, ", ") + ")"
|
||||||
@@ -241,7 +249,7 @@ func (e *Extractor) genContent(importPath string, p *types.Package) ([]byte, err
|
|||||||
base := template.New("extract")
|
base := template.New("extract")
|
||||||
parse, err := base.Parse(model)
|
parse, err := base.Parse(model)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("template parsing error: %v", err)
|
return nil, fmt.Errorf("template parsing error: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if importPath == "log/syslog" {
|
if importPath == "log/syslog" {
|
||||||
@@ -263,24 +271,25 @@ func (e *Extractor) genContent(importPath string, p *types.Package) ([]byte, err
|
|||||||
|
|
||||||
b := new(bytes.Buffer)
|
b := new(bytes.Buffer)
|
||||||
data := map[string]interface{}{
|
data := map[string]interface{}{
|
||||||
"Dest": e.Dest,
|
"Dest": e.Dest,
|
||||||
"Imports": imports,
|
"Imports": imports,
|
||||||
"PkgName": importPath,
|
"ImportPath": importPath,
|
||||||
"Val": val,
|
"PkgName": path.Join(importPath, p.Name()),
|
||||||
"Typ": typ,
|
"Val": val,
|
||||||
"Wrap": wrap,
|
"Typ": typ,
|
||||||
"BuildTags": buildTags,
|
"Wrap": wrap,
|
||||||
"License": e.License,
|
"BuildTags": buildTags,
|
||||||
|
"License": e.License,
|
||||||
}
|
}
|
||||||
err = parse.Execute(b, data)
|
err = parse.Execute(b, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("template error: %v", err)
|
return nil, fmt.Errorf("template error: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// gofmt
|
// gofmt
|
||||||
source, err := format.Source(b.Bytes())
|
source, err := format.Source(b.Bytes())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to format source: %v: %s", err, b.Bytes())
|
return nil, fmt.Errorf("failed to format source: %w: %s", err, b.Bytes())
|
||||||
}
|
}
|
||||||
return source, nil
|
return source, nil
|
||||||
}
|
}
|
||||||
@@ -436,7 +445,7 @@ func GetMinor(part string) string {
|
|||||||
return minor
|
return minor
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultMinorVersion = 15
|
const defaultMinorVersion = 16
|
||||||
|
|
||||||
func genBuildTags() (string, error) {
|
func genBuildTags() (string, error) {
|
||||||
version := runtime.Version()
|
version := runtime.Version()
|
||||||
@@ -451,7 +460,7 @@ func genBuildTags() (string, error) {
|
|||||||
|
|
||||||
minor, err := strconv.Atoi(minorRaw)
|
minor, err := strconv.Atoi(minorRaw)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("failed to parse version: %v", err)
|
return "", fmt.Errorf("failed to parse version: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only append an upper bound if we are not on the latest go
|
// Only append an upper bound if we are not on the latest go
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
Symbols["guthib.com/baz"] = map[string]reflect.Value{
|
Symbols["guthib.com/baz/baz"] = map[string]reflect.Value{
|
||||||
// function, constant and variable definitions
|
// function, constant and variable definitions
|
||||||
"Hello": reflect.ValueOf(baz.Hello),
|
"Hello": reflect.ValueOf(baz.Hello),
|
||||||
}
|
}
|
||||||
@@ -78,6 +78,48 @@ func TestPackages(t *testing.T) {
|
|||||||
importPath: "guthib.com/baz",
|
importPath: "guthib.com/baz",
|
||||||
expected: expectedOutput,
|
expected: expectedOutput,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
desc: "using relative path, package name is not same as import path",
|
||||||
|
wd: "./testdata/6/src/guthib.com/bar",
|
||||||
|
arg: "../baz-baz",
|
||||||
|
importPath: "guthib.com/baz",
|
||||||
|
expected: expectedOutput,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "using relative path, interface method parameter is variadic",
|
||||||
|
wd: "./testdata/7/src/guthib.com/variadic",
|
||||||
|
arg: "../variadic",
|
||||||
|
importPath: "guthib.com/variadic",
|
||||||
|
expected: `
|
||||||
|
// Code generated by 'yaegi extract guthib.com/variadic'. DO NOT EDIT.
|
||||||
|
|
||||||
|
package variadic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"guthib.com/variadic"
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Symbols["guthib.com/variadic/variadic"] = map[string]reflect.Value{
|
||||||
|
// type definitions
|
||||||
|
"Variadic": reflect.ValueOf((*variadic.Variadic)(nil)),
|
||||||
|
|
||||||
|
// interface wrapper definitions
|
||||||
|
"_Variadic": reflect.ValueOf((*_guthib_com_variadic_Variadic)(nil)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// _guthib_com_variadic_Variadic is an interface wrapper for Variadic type
|
||||||
|
type _guthib_com_variadic_Variadic struct {
|
||||||
|
WCall func(method string, args ...[]interface{}) (interface{}, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (W _guthib_com_variadic_Variadic) Call(method string, args ...[]interface{}) (interface{}, error) {
|
||||||
|
return W.WCall(method, args...)
|
||||||
|
}
|
||||||
|
`[1:],
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range testCases {
|
for _, test := range testCases {
|
||||||
|
|||||||
9
extract/testdata/6/src/guthib.com/bar/main.go
vendored
Normal file
9
extract/testdata/6/src/guthib.com/bar/main.go
vendored
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"guthib.com/baz"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
baz.Hello()
|
||||||
|
}
|
||||||
5
extract/testdata/6/src/guthib.com/baz-baz/baz.go
vendored
Normal file
5
extract/testdata/6/src/guthib.com/baz-baz/baz.go
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package baz
|
||||||
|
|
||||||
|
func Hello() {
|
||||||
|
println("HELLO")
|
||||||
|
}
|
||||||
1
extract/testdata/6/src/guthib.com/baz-baz/go.mod
vendored
Normal file
1
extract/testdata/6/src/guthib.com/baz-baz/go.mod
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
module guthib.com/baz-baz
|
||||||
1
extract/testdata/7/src/guthib.com/variadic/go.mod
vendored
Normal file
1
extract/testdata/7/src/guthib.com/variadic/go.mod
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
module guthib.com/baz-baz/variadic
|
||||||
5
extract/testdata/7/src/guthib.com/variadic/variadic.go
vendored
Normal file
5
extract/testdata/7/src/guthib.com/variadic/variadic.go
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package variadic
|
||||||
|
|
||||||
|
type Variadic interface {
|
||||||
|
Call(method string, args ...[]interface{}) (interface{}, error)
|
||||||
|
}
|
||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
|
|
||||||
const model = `package interp
|
const model = `package interp
|
||||||
|
|
||||||
// Code generated by 'go run ../internal/genop/genop.go'. DO NOT EDIT.
|
// Code generated by 'go run ../internal/cmd/genop/genop.go'. DO NOT EDIT.
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"go/constant"
|
"go/constant"
|
||||||
@@ -24,6 +24,7 @@ import (
|
|||||||
func {{$name}}(n *node) {
|
func {{$name}}(n *node) {
|
||||||
next := getExec(n.tnext)
|
next := getExec(n.tnext)
|
||||||
typ := n.typ.concrete().TypeOf()
|
typ := n.typ.concrete().TypeOf()
|
||||||
|
isInterface := n.typ.TypeOf().Kind() == reflect.Interface
|
||||||
dest := genValueOutput(n, typ)
|
dest := genValueOutput(n, typ)
|
||||||
c0, c1 := n.child[0], n.child[1]
|
c0, c1 := n.child[0], n.child[1]
|
||||||
|
|
||||||
@@ -31,6 +32,13 @@ func {{$name}}(n *node) {
|
|||||||
{{- if $op.Str}}
|
{{- if $op.Str}}
|
||||||
case reflect.String:
|
case reflect.String:
|
||||||
switch {
|
switch {
|
||||||
|
case isInterface:
|
||||||
|
v0 := genValue(c0)
|
||||||
|
v1 := genValue(c1)
|
||||||
|
n.exec = func(f *frame) bltn {
|
||||||
|
dest(f).Set(reflect.ValueOf(v0(f).String() {{$op.Name}} v1(f).String()).Convert(typ))
|
||||||
|
return next
|
||||||
|
}
|
||||||
case c0.rval.IsValid():
|
case c0.rval.IsValid():
|
||||||
s0 := vString(c0.rval)
|
s0 := vString(c0.rval)
|
||||||
v1 := genValue(c1)
|
v1 := genValue(c1)
|
||||||
@@ -56,6 +64,19 @@ func {{$name}}(n *node) {
|
|||||||
{{- end}}
|
{{- end}}
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||||
switch {
|
switch {
|
||||||
|
case isInterface:
|
||||||
|
v0 := genValueInt(c0)
|
||||||
|
{{- if $op.Shift}}
|
||||||
|
v1 := genValueUint(c1)
|
||||||
|
{{else}}
|
||||||
|
v1 := genValueInt(c1)
|
||||||
|
{{end -}}
|
||||||
|
n.exec = func(f *frame) bltn {
|
||||||
|
_, i := v0(f)
|
||||||
|
_, j := v1(f)
|
||||||
|
dest(f).Set(reflect.ValueOf(i {{$op.Name}} j).Convert(typ))
|
||||||
|
return next
|
||||||
|
}
|
||||||
case c0.rval.IsValid():
|
case c0.rval.IsValid():
|
||||||
i := vInt(c0.rval)
|
i := vInt(c0.rval)
|
||||||
{{- if $op.Shift}}
|
{{- if $op.Shift}}
|
||||||
@@ -96,6 +117,15 @@ func {{$name}}(n *node) {
|
|||||||
}
|
}
|
||||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||||
switch {
|
switch {
|
||||||
|
case isInterface:
|
||||||
|
v0 := genValueUint(c0)
|
||||||
|
v1 := genValueUint(c1)
|
||||||
|
n.exec = func(f *frame) bltn {
|
||||||
|
_, i := v0(f)
|
||||||
|
_, j := v1(f)
|
||||||
|
dest(f).Set(reflect.ValueOf(i {{$op.Name}} j).Convert(typ))
|
||||||
|
return next
|
||||||
|
}
|
||||||
case c0.rval.IsValid():
|
case c0.rval.IsValid():
|
||||||
i := vUint(c0.rval)
|
i := vUint(c0.rval)
|
||||||
v1 := genValueUint(c1)
|
v1 := genValueUint(c1)
|
||||||
@@ -125,6 +155,15 @@ func {{$name}}(n *node) {
|
|||||||
{{- if $op.Float}}
|
{{- if $op.Float}}
|
||||||
case reflect.Float32, reflect.Float64:
|
case reflect.Float32, reflect.Float64:
|
||||||
switch {
|
switch {
|
||||||
|
case isInterface:
|
||||||
|
v0 := genValueFloat(c0)
|
||||||
|
v1 := genValueFloat(c1)
|
||||||
|
n.exec = func(f *frame) bltn {
|
||||||
|
_, i := v0(f)
|
||||||
|
_, j := v1(f)
|
||||||
|
dest(f).Set(reflect.ValueOf(i {{$op.Name}} j).Convert(typ))
|
||||||
|
return next
|
||||||
|
}
|
||||||
case c0.rval.IsValid():
|
case c0.rval.IsValid():
|
||||||
i := vFloat(c0.rval)
|
i := vFloat(c0.rval)
|
||||||
v1 := genValueFloat(c1)
|
v1 := genValueFloat(c1)
|
||||||
@@ -153,25 +192,32 @@ func {{$name}}(n *node) {
|
|||||||
}
|
}
|
||||||
case reflect.Complex64, reflect.Complex128:
|
case reflect.Complex64, reflect.Complex128:
|
||||||
switch {
|
switch {
|
||||||
|
case isInterface:
|
||||||
|
v0 := genComplex(c0)
|
||||||
|
v1 := genComplex(c1)
|
||||||
|
n.exec = func(f *frame) bltn {
|
||||||
|
dest(f).Set(reflect.ValueOf(v0(f) {{$op.Name}} v1(f)).Convert(typ))
|
||||||
|
return next
|
||||||
|
}
|
||||||
case c0.rval.IsValid():
|
case c0.rval.IsValid():
|
||||||
r0 := vComplex(c0.rval)
|
r0 := vComplex(c0.rval)
|
||||||
v1 := genValue(c1)
|
v1 := genComplex(c1)
|
||||||
n.exec = func(f *frame) bltn {
|
n.exec = func(f *frame) bltn {
|
||||||
dest(f).SetComplex(r0 {{$op.Name}} v1(f).Complex())
|
dest(f).SetComplex(r0 {{$op.Name}} v1(f))
|
||||||
return next
|
return next
|
||||||
}
|
}
|
||||||
case c1.rval.IsValid():
|
case c1.rval.IsValid():
|
||||||
r1 := vComplex(c1.rval)
|
r1 := vComplex(c1.rval)
|
||||||
v0 := genValue(c0)
|
v0 := genComplex(c0)
|
||||||
n.exec = func(f *frame) bltn {
|
n.exec = func(f *frame) bltn {
|
||||||
dest(f).SetComplex(v0(f).Complex() {{$op.Name}} r1)
|
dest(f).SetComplex(v0(f) {{$op.Name}} r1)
|
||||||
return next
|
return next
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
v0 := genValue(c0)
|
v0 := genComplex(c0)
|
||||||
v1 := genValue(c1)
|
v1 := genComplex(c1)
|
||||||
n.exec = func(f *frame) bltn {
|
n.exec = func(f *frame) bltn {
|
||||||
dest(f).SetComplex(v0(f).Complex() {{$op.Name}} v1(f).Complex())
|
dest(f).SetComplex(v0(f) {{$op.Name}} v1(f))
|
||||||
return next
|
return next
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -209,7 +255,11 @@ func {{$name}}Const(n *node) {
|
|||||||
v := constant.BinaryOp(vConstantValue(v0), operator, vConstantValue(v1))
|
v := constant.BinaryOp(vConstantValue(v0), operator, vConstantValue(v1))
|
||||||
n.rval.Set(reflect.ValueOf(v))
|
n.rval.Set(reflect.ValueOf(v))
|
||||||
{{- else}}
|
{{- else}}
|
||||||
|
{{- if $op.Int}}
|
||||||
|
v := constant.BinaryOp(constant.ToInt(vConstantValue(v0)), token.{{tokenFromName $name}}, constant.ToInt(vConstantValue(v1)))
|
||||||
|
{{- else}}
|
||||||
v := constant.BinaryOp(vConstantValue(v0), token.{{tokenFromName $name}}, vConstantValue(v1))
|
v := constant.BinaryOp(vConstantValue(v0), token.{{tokenFromName $name}}, vConstantValue(v1))
|
||||||
|
{{- end}}
|
||||||
n.rval.Set(reflect.ValueOf(v))
|
n.rval.Set(reflect.ValueOf(v))
|
||||||
{{- end}}
|
{{- end}}
|
||||||
{{- if $op.Str}}
|
{{- if $op.Str}}
|
||||||
@@ -425,12 +475,24 @@ func {{$name}}Const(n *node) {
|
|||||||
func {{$name}}(n *node) {
|
func {{$name}}(n *node) {
|
||||||
tnext := getExec(n.tnext)
|
tnext := getExec(n.tnext)
|
||||||
dest := genValueOutput(n, reflect.TypeOf(true))
|
dest := genValueOutput(n, reflect.TypeOf(true))
|
||||||
|
typ := n.typ.concrete().TypeOf()
|
||||||
|
isInterface := n.typ.TypeOf().Kind() == reflect.Interface
|
||||||
c0, c1 := n.child[0], n.child[1]
|
c0, c1 := n.child[0], n.child[1]
|
||||||
|
|
||||||
{{- if or (eq $op.Name "==") (eq $op.Name "!=") }}
|
{{- if or (eq $op.Name "==") (eq $op.Name "!=") }}
|
||||||
|
|
||||||
if c0.typ.cat == aliasT || c1.typ.cat == aliasT {
|
if c0.typ.cat == aliasT || c1.typ.cat == aliasT {
|
||||||
switch {
|
switch {
|
||||||
|
case isInterface:
|
||||||
|
v0 := genValue(c0)
|
||||||
|
v1 := genValue(c1)
|
||||||
|
dest := genValue(n)
|
||||||
|
n.exec = func(f *frame) bltn {
|
||||||
|
i0 := v0(f).Interface()
|
||||||
|
i1 := v1(f).Interface()
|
||||||
|
dest(f).Set(reflect.ValueOf(i0 {{$op.Name}} i1).Convert(typ))
|
||||||
|
return tnext
|
||||||
|
}
|
||||||
case c0.rval.IsValid():
|
case c0.rval.IsValid():
|
||||||
i0 := c0.rval.Interface()
|
i0 := c0.rval.Interface()
|
||||||
v1 := genValue(c1)
|
v1 := genValue(c1)
|
||||||
@@ -507,9 +569,18 @@ func {{$name}}(n *node) {
|
|||||||
switch t0, t1 := c0.typ.TypeOf(), c1.typ.TypeOf(); {
|
switch t0, t1 := c0.typ.TypeOf(), c1.typ.TypeOf(); {
|
||||||
case isString(t0) || isString(t1):
|
case isString(t0) || isString(t1):
|
||||||
switch {
|
switch {
|
||||||
|
case isInterface:
|
||||||
|
v0 := genValueString(c0)
|
||||||
|
v1 := genValueString(c1)
|
||||||
|
n.exec = func(f *frame) bltn {
|
||||||
|
_, s0 := v0(f)
|
||||||
|
_, s1 := v1(f)
|
||||||
|
dest(f).Set(reflect.ValueOf(s0 {{$op.Name}} s1).Convert(typ))
|
||||||
|
return tnext
|
||||||
|
}
|
||||||
case c0.rval.IsValid():
|
case c0.rval.IsValid():
|
||||||
s0 := vString(c0.rval)
|
s0 := vString(c0.rval)
|
||||||
v1 := genValueString(n.child[1])
|
v1 := genValueString(c1)
|
||||||
if n.fnext != nil {
|
if n.fnext != nil {
|
||||||
fnext := getExec(n.fnext)
|
fnext := getExec(n.fnext)
|
||||||
n.exec = func(f *frame) bltn {
|
n.exec = func(f *frame) bltn {
|
||||||
@@ -530,7 +601,7 @@ func {{$name}}(n *node) {
|
|||||||
}
|
}
|
||||||
case c1.rval.IsValid():
|
case c1.rval.IsValid():
|
||||||
s1 := vString(c1.rval)
|
s1 := vString(c1.rval)
|
||||||
v0 := genValueString(n.child[0])
|
v0 := genValueString(c0)
|
||||||
if n.fnext != nil {
|
if n.fnext != nil {
|
||||||
fnext := getExec(n.fnext)
|
fnext := getExec(n.fnext)
|
||||||
n.exec = func(f *frame) bltn {
|
n.exec = func(f *frame) bltn {
|
||||||
@@ -550,8 +621,8 @@ func {{$name}}(n *node) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
v0 := genValueString(n.child[0])
|
v0 := genValueString(c0)
|
||||||
v1 := genValueString(n.child[1])
|
v1 := genValueString(c1)
|
||||||
if n.fnext != nil {
|
if n.fnext != nil {
|
||||||
fnext := getExec(n.fnext)
|
fnext := getExec(n.fnext)
|
||||||
n.exec = func(f *frame) bltn {
|
n.exec = func(f *frame) bltn {
|
||||||
@@ -575,6 +646,15 @@ func {{$name}}(n *node) {
|
|||||||
}
|
}
|
||||||
case isFloat(t0) || isFloat(t1):
|
case isFloat(t0) || isFloat(t1):
|
||||||
switch {
|
switch {
|
||||||
|
case isInterface:
|
||||||
|
v0 := genValueFloat(c0)
|
||||||
|
v1 := genValueFloat(c1)
|
||||||
|
n.exec = func(f *frame) bltn {
|
||||||
|
_, s0 := v0(f)
|
||||||
|
_, s1 := v1(f)
|
||||||
|
dest(f).Set(reflect.ValueOf(s0 {{$op.Name}} s1).Convert(typ))
|
||||||
|
return tnext
|
||||||
|
}
|
||||||
case c0.rval.IsValid():
|
case c0.rval.IsValid():
|
||||||
s0 := vFloat(c0.rval)
|
s0 := vFloat(c0.rval)
|
||||||
v1 := genValueFloat(c1)
|
v1 := genValueFloat(c1)
|
||||||
@@ -645,6 +725,15 @@ func {{$name}}(n *node) {
|
|||||||
}
|
}
|
||||||
case isUint(t0) || isUint(t1):
|
case isUint(t0) || isUint(t1):
|
||||||
switch {
|
switch {
|
||||||
|
case isInterface:
|
||||||
|
v0 := genValueUint(c0)
|
||||||
|
v1 := genValueUint(c1)
|
||||||
|
n.exec = func(f *frame) bltn {
|
||||||
|
_, s0 := v0(f)
|
||||||
|
_, s1 := v1(f)
|
||||||
|
dest(f).Set(reflect.ValueOf(s0 {{$op.Name}} s1).Convert(typ))
|
||||||
|
return tnext
|
||||||
|
}
|
||||||
case c0.rval.IsValid():
|
case c0.rval.IsValid():
|
||||||
s0 := vUint(c0.rval)
|
s0 := vUint(c0.rval)
|
||||||
v1 := genValueUint(c1)
|
v1 := genValueUint(c1)
|
||||||
@@ -716,6 +805,15 @@ func {{$name}}(n *node) {
|
|||||||
}
|
}
|
||||||
case isInt(t0) || isInt(t1):
|
case isInt(t0) || isInt(t1):
|
||||||
switch {
|
switch {
|
||||||
|
case isInterface:
|
||||||
|
v0 := genValueInt(c0)
|
||||||
|
v1 := genValueInt(c1)
|
||||||
|
n.exec = func(f *frame) bltn {
|
||||||
|
_, s0 := v0(f)
|
||||||
|
_, s1 := v1(f)
|
||||||
|
dest(f).Set(reflect.ValueOf(s0 {{$op.Name}} s1).Convert(typ))
|
||||||
|
return tnext
|
||||||
|
}
|
||||||
case c0.rval.IsValid():
|
case c0.rval.IsValid():
|
||||||
s0 := vInt(c0.rval)
|
s0 := vInt(c0.rval)
|
||||||
v1 := genValueInt(c1)
|
v1 := genValueInt(c1)
|
||||||
@@ -788,6 +886,15 @@ func {{$name}}(n *node) {
|
|||||||
{{- if $op.Complex}}
|
{{- if $op.Complex}}
|
||||||
case isComplex(t0) || isComplex(t1):
|
case isComplex(t0) || isComplex(t1):
|
||||||
switch {
|
switch {
|
||||||
|
case isInterface:
|
||||||
|
v0 := genComplex(c0)
|
||||||
|
v1 := genComplex(c1)
|
||||||
|
n.exec = func(f *frame) bltn {
|
||||||
|
s0 := v0(f)
|
||||||
|
s1 := v1(f)
|
||||||
|
dest(f).Set(reflect.ValueOf(s0 {{$op.Name}} s1).Convert(typ))
|
||||||
|
return tnext
|
||||||
|
}
|
||||||
case c0.rval.IsValid():
|
case c0.rval.IsValid():
|
||||||
s0 := vComplex(c0.rval)
|
s0 := vComplex(c0.rval)
|
||||||
v1 := genComplex(c1)
|
v1 := genComplex(c1)
|
||||||
@@ -832,8 +939,8 @@ func {{$name}}(n *node) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
v0 := genComplex(n.child[0])
|
v0 := genComplex(c0)
|
||||||
v1 := genComplex(n.child[1])
|
v1 := genComplex(c1)
|
||||||
if n.fnext != nil {
|
if n.fnext != nil {
|
||||||
fnext := getExec(n.fnext)
|
fnext := getExec(n.fnext)
|
||||||
n.exec = func(f *frame) bltn {
|
n.exec = func(f *frame) bltn {
|
||||||
@@ -857,6 +964,15 @@ func {{$name}}(n *node) {
|
|||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
switch {
|
switch {
|
||||||
|
case isInterface:
|
||||||
|
v0 := genValue(c0)
|
||||||
|
v1 := genValue(c1)
|
||||||
|
n.exec = func(f *frame) bltn {
|
||||||
|
i0 := v0(f).Interface()
|
||||||
|
i1 := v1(f).Interface()
|
||||||
|
dest(f).Set(reflect.ValueOf(i0 {{$op.Name}} i1).Convert(typ))
|
||||||
|
return tnext
|
||||||
|
}
|
||||||
case c0.rval.IsValid():
|
case c0.rval.IsValid():
|
||||||
i0 := c0.rval.Interface()
|
i0 := c0.rval.Interface()
|
||||||
v1 := genValue(c1)
|
v1 := genValue(c1)
|
||||||
@@ -940,6 +1056,7 @@ type Op struct {
|
|||||||
Complex bool // true if operator applies to complex
|
Complex bool // true if operator applies to complex
|
||||||
Shift bool // true if operator is a shift operation
|
Shift bool // true if operator is a shift operation
|
||||||
Bool bool // true if operator applies to bool
|
Bool bool // true if operator applies to bool
|
||||||
|
Int bool // true if operator applies to int only
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -968,17 +1085,17 @@ func main() {
|
|||||||
b := &bytes.Buffer{}
|
b := &bytes.Buffer{}
|
||||||
data := map[string]interface{}{
|
data := map[string]interface{}{
|
||||||
"Arithmetic": map[string]Op{
|
"Arithmetic": map[string]Op{
|
||||||
"add": {"+", true, true, true, false, false},
|
"add": {"+", true, true, true, false, false, false},
|
||||||
"sub": {"-", false, true, true, false, false},
|
"sub": {"-", false, true, true, false, false, false},
|
||||||
"mul": {"*", false, true, true, false, false},
|
"mul": {"*", false, true, true, false, false, false},
|
||||||
"quo": {"/", false, true, true, false, false},
|
"quo": {"/", false, true, true, false, false, false},
|
||||||
"rem": {"%", false, false, false, false, false},
|
"rem": {"%", false, false, false, false, false, true},
|
||||||
"shl": {"<<", false, false, false, true, false},
|
"shl": {"<<", false, false, false, true, false, true},
|
||||||
"shr": {">>", false, false, false, true, false},
|
"shr": {">>", false, false, false, true, false, true},
|
||||||
"and": {"&", false, false, false, false, false},
|
"and": {"&", false, false, false, false, false, true},
|
||||||
"or": {"|", false, false, false, false, false},
|
"or": {"|", false, false, false, false, false, true},
|
||||||
"xor": {"^", false, false, false, false, false},
|
"xor": {"^", false, false, false, false, false, true},
|
||||||
"andNot": {"&^", false, false, false, false, false},
|
"andNot": {"&^", false, false, false, false, false, true},
|
||||||
},
|
},
|
||||||
"IncDec": map[string]Op{
|
"IncDec": map[string]Op{
|
||||||
"inc": {Name: "+"},
|
"inc": {Name: "+"},
|
||||||
@@ -996,7 +1113,7 @@ func main() {
|
|||||||
"not": {Name: "!", Float: false, Bool: true},
|
"not": {Name: "!", Float: false, Bool: true},
|
||||||
"neg": {Name: "-", Float: true, Bool: false},
|
"neg": {Name: "-", Float: true, Bool: false},
|
||||||
"pos": {Name: "+", Float: true, Bool: false},
|
"pos": {Name: "+", Float: true, Bool: false},
|
||||||
"bitNot": {Name: "^", Float: false, Bool: false},
|
"bitNot": {Name: "^", Float: false, Bool: false, Int: true},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if err = parse.Execute(b, data); err != nil {
|
if err = parse.Execute(b, data); err != nil {
|
||||||
|
|||||||
@@ -357,7 +357,7 @@ func ignoreError(err error, src string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func wrapInMain(src string) string {
|
func wrapInMain(src string) string {
|
||||||
return fmt.Sprintf("package main; func main() {%s}", src)
|
return fmt.Sprintf("package main; func main() {%s\n}", src)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note: no type analysis is performed at this stage, it is done in pre-order
|
// Note: no type analysis is performed at this stage, it is done in pre-order
|
||||||
@@ -716,7 +716,7 @@ func (interp *Interpreter) ast(src, name string, inc bool) (string, *node, error
|
|||||||
n := addChild(&root, anc, pos, identExpr, aNop)
|
n := addChild(&root, anc, pos, identExpr, aNop)
|
||||||
n.ident = a.Name
|
n.ident = a.Name
|
||||||
st.push(n, nod)
|
st.push(n, nod)
|
||||||
if n.anc.kind == defineStmt && n.anc.nright == 0 {
|
if n.anc.kind == defineStmt && n.anc.anc.kind == constDecl && n.anc.nright == 0 {
|
||||||
// Implicit assign expression (in a ConstDecl block).
|
// Implicit assign expression (in a ConstDecl block).
|
||||||
// Clone assign source and type from previous
|
// Clone assign source and type from previous
|
||||||
a := n.anc
|
a := n.anc
|
||||||
@@ -858,7 +858,8 @@ func (interp *Interpreter) ast(src, name string, inc bool) (string, *node, error
|
|||||||
case *ast.ValueSpec:
|
case *ast.ValueSpec:
|
||||||
kind := valueSpec
|
kind := valueSpec
|
||||||
act := aNop
|
act := aNop
|
||||||
if a.Values != nil {
|
switch {
|
||||||
|
case a.Values != nil:
|
||||||
if len(a.Names) > 1 && len(a.Values) == 1 {
|
if len(a.Names) > 1 && len(a.Values) == 1 {
|
||||||
if anc.node.kind == constDecl || anc.node.kind == varDecl {
|
if anc.node.kind == constDecl || anc.node.kind == varDecl {
|
||||||
kind = defineXStmt
|
kind = defineXStmt
|
||||||
@@ -874,7 +875,9 @@ func (interp *Interpreter) ast(src, name string, inc bool) (string, *node, error
|
|||||||
}
|
}
|
||||||
act = aAssign
|
act = aAssign
|
||||||
}
|
}
|
||||||
} else if anc.node.kind == constDecl {
|
case anc.node.kind == constDecl:
|
||||||
|
kind, act = defineStmt, aAssign
|
||||||
|
case anc.node.kind == varDecl && anc.node.anc.kind != fileStmt:
|
||||||
kind, act = defineStmt, aAssign
|
kind, act = defineStmt, aAssign
|
||||||
}
|
}
|
||||||
n := addChild(&root, anc, pos, kind, act)
|
n := addChild(&root, anc, pos, kind, act)
|
||||||
|
|||||||
@@ -163,9 +163,9 @@ var knownOs = map[string]bool{
|
|||||||
"dragonfly": true,
|
"dragonfly": true,
|
||||||
"freebsd": true,
|
"freebsd": true,
|
||||||
"illumos": true,
|
"illumos": true,
|
||||||
|
"ios": true,
|
||||||
"js": true,
|
"js": true,
|
||||||
"linux": true,
|
"linux": true,
|
||||||
"nacl": true,
|
|
||||||
"netbsd": true,
|
"netbsd": true,
|
||||||
"openbsd": true,
|
"openbsd": true,
|
||||||
"plan9": true,
|
"plan9": true,
|
||||||
@@ -176,7 +176,6 @@ var knownOs = map[string]bool{
|
|||||||
var knownArch = map[string]bool{
|
var knownArch = map[string]bool{
|
||||||
"386": true,
|
"386": true,
|
||||||
"amd64": true,
|
"amd64": true,
|
||||||
"amd64p32": true,
|
|
||||||
"arm": true,
|
"arm": true,
|
||||||
"arm64": true,
|
"arm64": true,
|
||||||
"mips": true,
|
"mips": true,
|
||||||
|
|||||||
535
interp/cfg.go
535
interp/cfg.go
@@ -7,7 +7,6 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
)
|
)
|
||||||
@@ -44,8 +43,6 @@ var constBltn = map[string]func(*node){
|
|||||||
bltnReal: realConst,
|
bltnReal: realConst,
|
||||||
}
|
}
|
||||||
|
|
||||||
var identifier = regexp.MustCompile(`([\pL_][\pL_\d]*)$`)
|
|
||||||
|
|
||||||
const nilIdent = "nil"
|
const nilIdent = "nil"
|
||||||
|
|
||||||
// cfg generates a control flow graph (CFG) from AST (wiring successors in AST)
|
// cfg generates a control flow graph (CFG) from AST (wiring successors in AST)
|
||||||
@@ -75,11 +72,21 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
case assignStmt, defineStmt:
|
case assignStmt, defineStmt:
|
||||||
a := n.anc
|
a := n.anc
|
||||||
i := childPos(n) - a.nright
|
i := childPos(n) - a.nright
|
||||||
|
if i < 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
if len(a.child) > a.nright+a.nleft {
|
if len(a.child) > a.nright+a.nleft {
|
||||||
i--
|
i--
|
||||||
}
|
}
|
||||||
dest := a.child[i]
|
dest := a.child[i]
|
||||||
if dest.typ != nil && !isInterface(dest.typ) {
|
if dest.typ == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if dest.typ.incomplete {
|
||||||
|
err = n.cfgErrorf("invalid type declaration")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !isInterface(dest.typ) {
|
||||||
// Interface type are not propagated, and will be resolved at post-order.
|
// Interface type are not propagated, and will be resolved at post-order.
|
||||||
n.typ = dest.typ
|
n.typ = dest.typ
|
||||||
}
|
}
|
||||||
@@ -144,6 +151,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
vtyp = &itype{cat: valueT, rtype: typ.Elem()}
|
vtyp = &itype{cat: valueT, rtype: typ.Elem()}
|
||||||
case reflect.String:
|
case reflect.String:
|
||||||
sc.add(sc.getType("int")) // Add a dummy type to store array shallow copy for range
|
sc.add(sc.getType("int")) // Add a dummy type to store array shallow copy for range
|
||||||
|
sc.add(sc.getType("int")) // Add a dummy type to store index for range
|
||||||
ktyp = sc.getType("int")
|
ktyp = sc.getType("int")
|
||||||
vtyp = sc.getType("rune")
|
vtyp = sc.getType("rune")
|
||||||
case reflect.Array, reflect.Slice:
|
case reflect.Array, reflect.Slice:
|
||||||
@@ -167,9 +175,10 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
}
|
}
|
||||||
case stringT:
|
case stringT:
|
||||||
sc.add(sc.getType("int")) // Add a dummy type to store array shallow copy for range
|
sc.add(sc.getType("int")) // Add a dummy type to store array shallow copy for range
|
||||||
|
sc.add(sc.getType("int")) // Add a dummy type to store index for range
|
||||||
ktyp = sc.getType("int")
|
ktyp = sc.getType("int")
|
||||||
vtyp = sc.getType("rune")
|
vtyp = sc.getType("rune")
|
||||||
case arrayT, variadicT:
|
case arrayT, sliceT, variadicT:
|
||||||
sc.add(sc.getType("int")) // Add a dummy type to store array shallow copy for range
|
sc.add(sc.getType("int")) // Add a dummy type to store array shallow copy for range
|
||||||
ktyp = sc.getType("int")
|
ktyp = sc.getType("int")
|
||||||
vtyp = o.typ.val
|
vtyp = o.typ.val
|
||||||
@@ -285,8 +294,12 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
// Get type from ancestor (implicit type)
|
// Get type from ancestor (implicit type)
|
||||||
if n.anc.kind == keyValueExpr && n == n.anc.child[0] {
|
if n.anc.kind == keyValueExpr && n == n.anc.child[0] {
|
||||||
n.typ = n.anc.typ.key
|
n.typ = n.anc.typ.key
|
||||||
} else if n.anc.typ != nil {
|
} else if atyp := n.anc.typ; atyp != nil {
|
||||||
n.typ = n.anc.typ.val
|
if atyp.cat == valueT {
|
||||||
|
n.typ = &itype{cat: valueT, rtype: atyp.rtype.Elem()}
|
||||||
|
} else {
|
||||||
|
n.typ = atyp.val
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if n.typ == nil {
|
if n.typ == nil {
|
||||||
err = n.cfgErrorf("undefined type")
|
err = n.cfgErrorf("undefined type")
|
||||||
@@ -415,9 +428,10 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if n.child[1].kind == identExpr {
|
switch n.child[1].kind {
|
||||||
|
case identExpr, selectorExpr:
|
||||||
n.typ = &itype{cat: aliasT, val: typ, name: typeName}
|
n.typ = &itype{cat: aliasT, val: typ, name: typeName}
|
||||||
} else {
|
default:
|
||||||
n.typ = typ
|
n.typ = typ
|
||||||
n.typ.name = typeName
|
n.typ.name = typeName
|
||||||
}
|
}
|
||||||
@@ -510,10 +524,6 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
if dest.typ.incomplete {
|
if dest.typ.incomplete {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if dest.typ.sizedef {
|
|
||||||
dest.typ.size = arrayTypeLen(src)
|
|
||||||
dest.typ.rtype = nil
|
|
||||||
}
|
|
||||||
if sc.global {
|
if sc.global {
|
||||||
// Do not overload existing symbols (defined in GTA) in global scope
|
// Do not overload existing symbols (defined in GTA) in global scope
|
||||||
sym, _, _ = sc.lookup(dest.ident)
|
sym, _, _ = sc.lookup(dest.ident)
|
||||||
@@ -547,63 +557,74 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
n.findex = dest.findex
|
n.findex = dest.findex
|
||||||
n.level = dest.level
|
n.level = dest.level
|
||||||
|
|
||||||
// Propagate type
|
// In the following, we attempt to optimize by skipping the assign
|
||||||
// TODO: Check that existing destination type matches source type
|
// operation and setting the source location directly to the destination
|
||||||
|
// location in the frame.
|
||||||
|
//
|
||||||
switch {
|
switch {
|
||||||
case n.action == aAssign && isCall(src) && dest.typ.cat != interfaceT && !isMapEntry(dest) && !isRecursiveField(dest):
|
case n.action != aAssign:
|
||||||
|
// Do not skip assign operation if it is combined with another operator.
|
||||||
|
case src.rval.IsValid():
|
||||||
|
// Do not skip assign operation if setting from a constant value.
|
||||||
|
case isMapEntry(dest):
|
||||||
|
// Setting a map entry requires an additional step, do not optimize.
|
||||||
|
// As we only write, skip the default useless getIndexMap dest action.
|
||||||
|
dest.gen = nop
|
||||||
|
case isFuncField(dest):
|
||||||
|
// Setting a struct field of function type requires an extra step. Do not optimize.
|
||||||
|
case isCall(src) && !isInterfaceSrc(dest.typ) && !isRecursiveField(dest) && n.kind != defineStmt:
|
||||||
// Call action may perform the assignment directly.
|
// Call action may perform the assignment directly.
|
||||||
|
if dest.typ.id() != src.typ.id() {
|
||||||
|
// Skip optimitization if returned type doesn't match assigned one.
|
||||||
|
break
|
||||||
|
}
|
||||||
n.gen = nop
|
n.gen = nop
|
||||||
src.level = level
|
src.level = level
|
||||||
src.findex = dest.findex
|
src.findex = dest.findex
|
||||||
if src.typ.untyped && !dest.typ.untyped {
|
if src.typ.untyped && !dest.typ.untyped {
|
||||||
src.typ = dest.typ
|
src.typ = dest.typ
|
||||||
}
|
}
|
||||||
case n.action == aAssign && src.action == aRecv:
|
case src.action == aRecv:
|
||||||
// Assign by reading from a receiving channel.
|
// Assign by reading from a receiving channel.
|
||||||
n.gen = nop
|
n.gen = nop
|
||||||
src.findex = dest.findex // Set recv address to LHS
|
src.findex = dest.findex // Set recv address to LHS.
|
||||||
dest.typ = src.typ
|
dest.typ = src.typ
|
||||||
case n.action == aAssign && src.action == aCompositeLit && !isMapEntry(dest):
|
case src.action == aCompositeLit:
|
||||||
if dest.typ.cat == valueT && dest.typ.rtype.Kind() == reflect.Interface {
|
if dest.typ.cat == valueT && dest.typ.rtype.Kind() == reflect.Interface {
|
||||||
// Skip optimisation for assigned binary interface or map entry
|
// Skip optimisation for assigned interface.
|
||||||
// which require and additional operation to set the value
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if dest.action == aGetIndex {
|
if dest.action == aGetIndex {
|
||||||
// optimization does not work when assigning to a struct field. Maybe we're not
|
// Skip optimization, as it does not work when assigning to a struct field.
|
||||||
// setting the right frame index or something, and we would end up not writing at
|
|
||||||
// the right place. So disabling it for now.
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
// Skip the assign operation entirely, the source frame index is set
|
|
||||||
// to destination index, avoiding extra memory alloc and duplication.
|
|
||||||
n.gen = nop
|
n.gen = nop
|
||||||
src.findex = dest.findex
|
src.findex = dest.findex
|
||||||
src.level = level
|
src.level = level
|
||||||
case n.action == aAssign && len(n.child) < 4 && !src.rval.IsValid() && isArithmeticAction(src):
|
case len(n.child) < 4 && isArithmeticAction(src):
|
||||||
// Optimize single assignments from some arithmetic operations.
|
// Optimize single assignments from some arithmetic operations.
|
||||||
// Skip the assign operation entirely, the source frame index is set
|
|
||||||
// to destination index, avoiding extra memory alloc and duplication.
|
|
||||||
src.typ = dest.typ
|
src.typ = dest.typ
|
||||||
src.findex = dest.findex
|
src.findex = dest.findex
|
||||||
src.level = level
|
src.level = level
|
||||||
n.gen = nop
|
n.gen = nop
|
||||||
case src.kind == basicLit && !src.rval.IsValid():
|
case src.kind == basicLit:
|
||||||
// Assign to nil.
|
// Assign to nil.
|
||||||
src.rval = reflect.New(dest.typ.TypeOf()).Elem()
|
src.rval = reflect.New(dest.typ.TypeOf()).Elem()
|
||||||
|
case n.nright == 0:
|
||||||
|
n.gen = reset
|
||||||
}
|
}
|
||||||
|
|
||||||
n.typ = dest.typ
|
n.typ = dest.typ
|
||||||
if sym != nil {
|
if sym != nil {
|
||||||
sym.typ = n.typ
|
sym.typ = n.typ
|
||||||
sym.recv = src.recv
|
sym.recv = src.recv
|
||||||
}
|
}
|
||||||
|
|
||||||
n.level = level
|
n.level = level
|
||||||
if isMapEntry(dest) {
|
|
||||||
dest.gen = nop // skip getIndexMap
|
|
||||||
}
|
|
||||||
if n.anc.kind == constDecl {
|
if n.anc.kind == constDecl {
|
||||||
n.gen = nop
|
n.gen = nop
|
||||||
n.findex = -1
|
n.findex = notInFrame
|
||||||
if sym, _, ok := sc.lookup(dest.ident); ok {
|
if sym, _, ok := sc.lookup(dest.ident); ok {
|
||||||
sym.kind = constSym
|
sym.kind = constSym
|
||||||
}
|
}
|
||||||
@@ -644,7 +665,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
if n.child[0].ident == "_" {
|
if n.child[0].ident == "_" {
|
||||||
lc.gen = typeAssertStatus
|
lc.gen = typeAssertStatus
|
||||||
} else {
|
} else {
|
||||||
lc.gen = typeAssert2
|
lc.gen = typeAssertLong
|
||||||
}
|
}
|
||||||
n.gen = nop
|
n.gen = nop
|
||||||
case unaryExpr:
|
case unaryExpr:
|
||||||
@@ -700,7 +721,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if c0.rval.IsValid() && c1.rval.IsValid() && !isInterface(n.typ) && constOp[n.action] != nil {
|
if c0.rval.IsValid() && c1.rval.IsValid() && (!isInterface(n.typ)) && constOp[n.action] != nil {
|
||||||
n.typ.TypeOf() // Force compute of reflection type.
|
n.typ.TypeOf() // Force compute of reflection type.
|
||||||
constOp[n.action](n) // Compute a constant result now rather than during exec.
|
constOp[n.action](n) // Compute a constant result now rather than during exec.
|
||||||
}
|
}
|
||||||
@@ -709,8 +730,8 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
// This operation involved constants, and the result is already computed
|
// This operation involved constants, and the result is already computed
|
||||||
// by constOp and available in n.rval. Nothing else to do at execution.
|
// by constOp and available in n.rval. Nothing else to do at execution.
|
||||||
n.gen = nop
|
n.gen = nop
|
||||||
n.findex = -1
|
n.findex = notInFrame
|
||||||
case n.anc.kind == assignStmt && n.anc.action == aAssign:
|
case n.anc.kind == assignStmt && n.anc.action == aAssign && n.anc.nleft == 1:
|
||||||
// To avoid a copy in frame, if the result is to be assigned, store it directly
|
// To avoid a copy in frame, if the result is to be assigned, store it directly
|
||||||
// at the frame location of destination.
|
// at the frame location of destination.
|
||||||
dest := n.anc.child[childPos(n)-n.anc.nright]
|
dest := n.anc.child[childPos(n)-n.anc.nright]
|
||||||
@@ -732,7 +753,13 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
wireChild(n)
|
wireChild(n)
|
||||||
t := n.child[0].typ
|
t := n.child[0].typ
|
||||||
switch t.cat {
|
switch t.cat {
|
||||||
case aliasT, ptrT:
|
case aliasT:
|
||||||
|
if isString(t.val.TypeOf()) {
|
||||||
|
n.typ = sc.getType("byte")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
fallthrough
|
||||||
|
case ptrT:
|
||||||
n.typ = t.val
|
n.typ = t.val
|
||||||
if t.val.cat == valueT {
|
if t.val.cat == valueT {
|
||||||
n.typ = &itype{cat: valueT, rtype: t.val.rtype.Elem()}
|
n.typ = &itype{cat: valueT, rtype: t.val.rtype.Elem()}
|
||||||
@@ -838,27 +865,47 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
wireChild(n)
|
wireChild(n)
|
||||||
switch {
|
switch {
|
||||||
case interp.isBuiltinCall(n):
|
case interp.isBuiltinCall(n):
|
||||||
err = check.builtin(n.child[0].ident, n, n.child[1:], n.action == aCallSlice)
|
c0 := n.child[0]
|
||||||
|
bname := c0.ident
|
||||||
|
err = check.builtin(bname, n, n.child[1:], n.action == aCallSlice)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
n.gen = n.child[0].sym.builtin
|
n.gen = c0.sym.builtin
|
||||||
n.child[0].typ = &itype{cat: builtinT}
|
c0.typ = &itype{cat: builtinT}
|
||||||
if n.typ, err = nodeType(interp, sc, n); err != nil {
|
if n.typ, err = nodeType(interp, sc, n); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
switch {
|
switch {
|
||||||
case n.typ.cat == builtinT:
|
case n.typ.cat == builtinT:
|
||||||
n.findex = -1
|
n.findex = notInFrame
|
||||||
n.val = nil
|
n.val = nil
|
||||||
case n.anc.kind == returnStmt:
|
case n.anc.kind == returnStmt:
|
||||||
// Store result directly to frame output location, to avoid a frame copy.
|
// Store result directly to frame output location, to avoid a frame copy.
|
||||||
n.findex = 0
|
n.findex = 0
|
||||||
|
case bname == "cap" && isInConstOrTypeDecl(n):
|
||||||
|
switch n.child[1].typ.TypeOf().Kind() {
|
||||||
|
case reflect.Array, reflect.Chan:
|
||||||
|
capConst(n)
|
||||||
|
default:
|
||||||
|
err = n.cfgErrorf("cap argument is not an array or channel")
|
||||||
|
}
|
||||||
|
n.findex = notInFrame
|
||||||
|
n.gen = nop
|
||||||
|
case bname == "len" && isInConstOrTypeDecl(n):
|
||||||
|
switch n.child[1].typ.TypeOf().Kind() {
|
||||||
|
case reflect.Array, reflect.Chan, reflect.String:
|
||||||
|
lenConst(n)
|
||||||
|
default:
|
||||||
|
err = n.cfgErrorf("len argument is not an array, channel or string")
|
||||||
|
}
|
||||||
|
n.findex = notInFrame
|
||||||
|
n.gen = nop
|
||||||
default:
|
default:
|
||||||
n.findex = sc.add(n.typ)
|
n.findex = sc.add(n.typ)
|
||||||
}
|
}
|
||||||
if op, ok := constBltn[n.child[0].ident]; ok && n.anc.action != aAssign {
|
if op, ok := constBltn[bname]; ok && n.anc.action != aAssign {
|
||||||
op(n) // pre-compute non-assigned constant :
|
op(n) // pre-compute non-assigned constant :
|
||||||
}
|
}
|
||||||
case n.child[0].isType(sc):
|
case n.child[0].isType(sc):
|
||||||
@@ -883,16 +930,19 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
if !c1.typ.implements(c0.typ) {
|
if !c1.typ.implements(c0.typ) {
|
||||||
err = n.cfgErrorf("type %v does not implement interface %v", c1.typ.id(), c0.typ.id())
|
err = n.cfgErrorf("type %v does not implement interface %v", c1.typ.id(), c0.typ.id())
|
||||||
}
|
}
|
||||||
// Pass value as is
|
// Convert type to interface while keeping a reference to the original concrete type.
|
||||||
|
// besides type, the node value remains preserved.
|
||||||
n.gen = nop
|
n.gen = nop
|
||||||
n.typ = c1.typ
|
t := *c0.typ
|
||||||
|
n.typ = &t
|
||||||
|
n.typ.val = c1.typ
|
||||||
n.findex = c1.findex
|
n.findex = c1.findex
|
||||||
n.level = c1.level
|
n.level = c1.level
|
||||||
n.val = c1.val
|
n.val = c1.val
|
||||||
n.rval = c1.rval
|
n.rval = c1.rval
|
||||||
case c1.rval.IsValid() && isConstType(c0.typ):
|
case c1.rval.IsValid() && isConstType(c0.typ):
|
||||||
n.gen = nop
|
n.gen = nop
|
||||||
n.findex = -1
|
n.findex = notInFrame
|
||||||
n.typ = c0.typ
|
n.typ = c0.typ
|
||||||
if c, ok := c1.rval.Interface().(constant.Value); ok {
|
if c, ok := c1.rval.Interface().(constant.Value); ok {
|
||||||
i, _ := constant.Int64Val(constant.ToInt(c))
|
i, _ := constant.Int64Val(constant.ToInt(c))
|
||||||
@@ -934,6 +984,20 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
case isOffsetof(n):
|
||||||
|
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
|
||||||
|
}
|
||||||
|
c1 := n.child[1]
|
||||||
|
field, ok := c1.child[0].typ.rtype.FieldByName(c1.child[1].ident)
|
||||||
|
if !ok {
|
||||||
|
err = n.cfgErrorf("struct does not contain field: %s", c1.child[1].ident)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
n.typ = &itype{cat: valueT, rtype: reflect.TypeOf(field.Offset)}
|
||||||
|
n.rval = reflect.ValueOf(field.Offset)
|
||||||
|
n.gen = nop
|
||||||
default:
|
default:
|
||||||
err = check.arguments(n, n.child[1:], n.child[0], n.action == aCallSlice)
|
err = check.arguments(n, n.child[1:], n.child[0], n.action == aCallSlice)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -946,7 +1010,9 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
}
|
}
|
||||||
if typ := n.child[0].typ; len(typ.ret) > 0 {
|
if typ := n.child[0].typ; len(typ.ret) > 0 {
|
||||||
n.typ = typ.ret[0]
|
n.typ = typ.ret[0]
|
||||||
if n.anc.kind == returnStmt {
|
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)
|
n.findex = childPos(n)
|
||||||
} else {
|
} else {
|
||||||
n.findex = sc.add(n.typ)
|
n.findex = sc.add(n.typ)
|
||||||
@@ -955,7 +1021,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
n.findex = -1
|
n.findex = notInFrame
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1003,8 +1069,8 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch n.typ.cat {
|
switch n.typ.cat {
|
||||||
case arrayT:
|
case arrayT, sliceT:
|
||||||
err = check.arrayLitExpr(child, n.typ.val, n.typ.size)
|
err = check.arrayLitExpr(child, n.typ)
|
||||||
case mapT:
|
case mapT:
|
||||||
err = check.mapLitExpr(child, n.typ.key, n.typ.val)
|
err = check.mapLitExpr(child, n.typ.key, n.typ.val)
|
||||||
case structT:
|
case structT:
|
||||||
@@ -1036,7 +1102,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
case fileStmt:
|
case fileStmt:
|
||||||
wireChild(n, varDecl)
|
wireChild(n, varDecl)
|
||||||
sc = sc.pop()
|
sc = sc.pop()
|
||||||
n.findex = -1
|
n.findex = notInFrame
|
||||||
|
|
||||||
case forStmt0: // for {}
|
case forStmt0: // for {}
|
||||||
body := n.child[0]
|
body := n.child[0]
|
||||||
@@ -1386,7 +1452,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
// TODO(mpl): move any of that code to typecheck?
|
// TODO(mpl): move any of that code to typecheck?
|
||||||
c.typ.node = c
|
c.typ.node = c
|
||||||
if !c.typ.assignableTo(typ) {
|
if !c.typ.assignableTo(typ) {
|
||||||
err = fmt.Errorf("cannot use %v (type %v) as type %v in return argument", c.ident, c.typ.cat, typ.cat)
|
err = c.cfgErrorf("cannot use %v (type %v) as type %v in return argument", c.ident, c.typ.cat, typ.cat)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if c.typ.cat == nilT {
|
if c.typ.cat == nilT {
|
||||||
@@ -1408,72 +1474,8 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
err = n.cfgErrorf("undefined type")
|
err = n.cfgErrorf("undefined type")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if n.typ.cat == valueT || n.typ.cat == errorT {
|
switch {
|
||||||
// Handle object defined in runtime, try to find field or method
|
case n.typ.cat == binPkgT:
|
||||||
// Search for method first, as it applies both to types T and *T
|
|
||||||
// Search for field must then be performed on type T only (not *T)
|
|
||||||
switch method, ok := n.typ.rtype.MethodByName(n.child[1].ident); {
|
|
||||||
case ok:
|
|
||||||
hasRecvType := n.typ.rtype.Kind() != reflect.Interface
|
|
||||||
n.val = method.Index
|
|
||||||
n.gen = getIndexBinMethod
|
|
||||||
n.action = aGetMethod
|
|
||||||
n.recv = &receiver{node: n.child[0]}
|
|
||||||
n.typ = &itype{cat: valueT, rtype: method.Type, isBinMethod: true}
|
|
||||||
if hasRecvType {
|
|
||||||
n.typ.recv = n.typ
|
|
||||||
}
|
|
||||||
case n.typ.rtype.Kind() == reflect.Ptr:
|
|
||||||
if field, ok := n.typ.rtype.Elem().FieldByName(n.child[1].ident); ok {
|
|
||||||
n.typ = &itype{cat: valueT, rtype: field.Type}
|
|
||||||
n.val = field.Index
|
|
||||||
n.gen = getPtrIndexSeq
|
|
||||||
} else {
|
|
||||||
err = n.cfgErrorf("undefined field or method: %s", n.child[1].ident)
|
|
||||||
}
|
|
||||||
case n.typ.rtype.Kind() == reflect.Struct:
|
|
||||||
if field, ok := n.typ.rtype.FieldByName(n.child[1].ident); ok {
|
|
||||||
n.typ = &itype{cat: valueT, rtype: field.Type}
|
|
||||||
n.val = field.Index
|
|
||||||
n.gen = getIndexSeq
|
|
||||||
} else {
|
|
||||||
// method lookup failed on type, now lookup on pointer to type
|
|
||||||
pt := reflect.PtrTo(n.typ.rtype)
|
|
||||||
if m2, ok2 := pt.MethodByName(n.child[1].ident); ok2 {
|
|
||||||
n.val = m2.Index
|
|
||||||
n.gen = getIndexBinPtrMethod
|
|
||||||
n.typ = &itype{cat: valueT, rtype: m2.Type, recv: &itype{cat: valueT, rtype: pt}}
|
|
||||||
n.recv = &receiver{node: n.child[0]}
|
|
||||||
n.action = aGetMethod
|
|
||||||
} else {
|
|
||||||
err = n.cfgErrorf("undefined field or method: %s", n.child[1].ident)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
err = n.cfgErrorf("undefined field or method: %s", n.child[1].ident)
|
|
||||||
}
|
|
||||||
} else if n.typ.cat == ptrT && (n.typ.val.cat == valueT || n.typ.val.cat == errorT) {
|
|
||||||
// Handle pointer on object defined in runtime
|
|
||||||
if method, ok := n.typ.val.rtype.MethodByName(n.child[1].ident); ok {
|
|
||||||
n.val = method.Index
|
|
||||||
n.typ = &itype{cat: valueT, rtype: method.Type, recv: n.typ}
|
|
||||||
n.recv = &receiver{node: n.child[0]}
|
|
||||||
n.gen = getIndexBinMethod
|
|
||||||
n.action = aGetMethod
|
|
||||||
} else if method, ok := reflect.PtrTo(n.typ.val.rtype).MethodByName(n.child[1].ident); ok {
|
|
||||||
n.val = method.Index
|
|
||||||
n.gen = getIndexBinMethod
|
|
||||||
n.typ = &itype{cat: valueT, rtype: method.Type, recv: &itype{cat: valueT, rtype: reflect.PtrTo(n.typ.val.rtype)}}
|
|
||||||
n.recv = &receiver{node: n.child[0]}
|
|
||||||
n.action = aGetMethod
|
|
||||||
} else if field, ok := n.typ.val.rtype.FieldByName(n.child[1].ident); ok {
|
|
||||||
n.typ = &itype{cat: valueT, rtype: field.Type}
|
|
||||||
n.val = field.Index
|
|
||||||
n.gen = getPtrIndexSeq
|
|
||||||
} else {
|
|
||||||
err = n.cfgErrorf("undefined selector: %s", n.child[1].ident)
|
|
||||||
}
|
|
||||||
} else if n.typ.cat == binPkgT {
|
|
||||||
// Resolve binary package symbol: a type or a value
|
// Resolve binary package symbol: a type or a value
|
||||||
name := n.child[1].ident
|
name := n.child[1].ident
|
||||||
pkg := n.child[0].sym.typ.path
|
pkg := n.child[0].sym.typ.path
|
||||||
@@ -1481,7 +1483,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
if isBinType(s) {
|
if isBinType(s) {
|
||||||
n.typ = &itype{cat: valueT, rtype: s.Type().Elem()}
|
n.typ = &itype{cat: valueT, rtype: s.Type().Elem()}
|
||||||
} else {
|
} else {
|
||||||
n.typ = &itype{cat: valueT, rtype: s.Type(), untyped: isValueUntyped(s)}
|
n.typ = &itype{cat: valueT, rtype: fixPossibleConstType(s.Type()), untyped: isValueUntyped(s)}
|
||||||
n.rval = s
|
n.rval = s
|
||||||
}
|
}
|
||||||
n.action = aGetSym
|
n.action = aGetSym
|
||||||
@@ -1489,11 +1491,14 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
} else {
|
} else {
|
||||||
err = n.cfgErrorf("package %s \"%s\" has no symbol %s", n.child[0].ident, pkg, name)
|
err = n.cfgErrorf("package %s \"%s\" has no symbol %s", n.child[0].ident, pkg, name)
|
||||||
}
|
}
|
||||||
} else if n.typ.cat == srcPkgT {
|
case n.typ.cat == srcPkgT:
|
||||||
pkg, name := n.child[0].sym.typ.path, n.child[1].ident
|
pkg, name := n.child[0].sym.typ.path, n.child[1].ident
|
||||||
// Resolve source package symbol
|
// Resolve source package symbol
|
||||||
if sym, ok := interp.srcPkg[pkg][name]; ok {
|
if sym, ok := interp.srcPkg[pkg][name]; ok {
|
||||||
n.findex = sym.index
|
n.findex = sym.index
|
||||||
|
if sym.global {
|
||||||
|
n.level = globalFrame
|
||||||
|
}
|
||||||
n.val = sym.node
|
n.val = sym.node
|
||||||
n.gen = nop
|
n.gen = nop
|
||||||
n.action = aGetSym
|
n.action = aGetSym
|
||||||
@@ -1503,66 +1508,139 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
} else {
|
} else {
|
||||||
err = n.cfgErrorf("undefined selector: %s.%s", pkg, name)
|
err = n.cfgErrorf("undefined selector: %s.%s", pkg, name)
|
||||||
}
|
}
|
||||||
} else if m, lind := n.typ.lookupMethod(n.child[1].ident); m != nil {
|
case isStruct(n.typ) || isInterfaceSrc(n.typ):
|
||||||
n.action = aGetMethod
|
// Find a matching field.
|
||||||
if n.child[0].isType(sc) {
|
if ti := n.typ.lookupField(n.child[1].ident); len(ti) > 0 {
|
||||||
// Handle method as a function with receiver in 1st argument
|
n.val = ti
|
||||||
n.val = m
|
switch {
|
||||||
n.findex = -1
|
case isInterfaceSrc(n.typ):
|
||||||
n.gen = nop
|
n.typ = n.typ.fieldSeq(ti)
|
||||||
n.typ = &itype{}
|
n.gen = getMethodByName
|
||||||
*n.typ = *m.typ
|
n.action = aMethod
|
||||||
n.typ.arg = append([]*itype{n.child[0].typ}, m.typ.arg...)
|
case n.typ.cat == ptrT:
|
||||||
} else {
|
n.typ = n.typ.fieldSeq(ti)
|
||||||
// Handle method with receiver
|
n.gen = getPtrIndexSeq
|
||||||
n.gen = getMethod
|
if n.typ.cat == funcT {
|
||||||
n.val = m
|
// Function in a struct field is always wrapped in reflect.Value.
|
||||||
n.typ = m.typ
|
rtype := n.typ.TypeOf()
|
||||||
|
n.typ = &itype{cat: valueT, rtype: rtype, val: n.typ}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
n.gen = getIndexSeq
|
||||||
|
n.typ = n.typ.fieldSeq(ti)
|
||||||
|
if n.typ.cat == funcT {
|
||||||
|
// Function in a struct field is always wrapped in reflect.Value.
|
||||||
|
rtype := n.typ.TypeOf()
|
||||||
|
n.typ = &itype{cat: valueT, rtype: rtype, val: n.typ}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if s, lind, ok := n.typ.lookupBinField(n.child[1].ident); ok {
|
||||||
|
// Handle an embedded binary field into a struct field.
|
||||||
|
n.gen = getIndexSeqField
|
||||||
|
lind = append(lind, s.Index...)
|
||||||
|
n.val = lind
|
||||||
|
n.typ = &itype{cat: valueT, rtype: s.Type}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// No field (embedded or not) matched. Try to match a method.
|
||||||
|
fallthrough
|
||||||
|
default:
|
||||||
|
// Find a matching method.
|
||||||
|
// TODO (marc): simplify the following if/elseif blocks.
|
||||||
|
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
|
||||||
|
n.val = method.Index
|
||||||
|
n.gen = getIndexBinMethod
|
||||||
|
n.action = aGetMethod
|
||||||
|
n.recv = &receiver{node: n.child[0]}
|
||||||
|
n.typ = &itype{cat: valueT, rtype: method.Type, isBinMethod: true}
|
||||||
|
if hasRecvType {
|
||||||
|
n.typ.recv = n.typ
|
||||||
|
}
|
||||||
|
case n.typ.rtype.Kind() == reflect.Ptr:
|
||||||
|
if field, ok := n.typ.rtype.Elem().FieldByName(n.child[1].ident); ok {
|
||||||
|
n.typ = &itype{cat: valueT, rtype: field.Type}
|
||||||
|
n.val = field.Index
|
||||||
|
n.gen = getPtrIndexSeq
|
||||||
|
break
|
||||||
|
}
|
||||||
|
err = n.cfgErrorf("undefined field or method: %s", n.child[1].ident)
|
||||||
|
case n.typ.rtype.Kind() == reflect.Struct:
|
||||||
|
if field, ok := n.typ.rtype.FieldByName(n.child[1].ident); ok {
|
||||||
|
n.typ = &itype{cat: valueT, rtype: field.Type}
|
||||||
|
n.val = field.Index
|
||||||
|
n.gen = getIndexSeq
|
||||||
|
break
|
||||||
|
}
|
||||||
|
fallthrough
|
||||||
|
default:
|
||||||
|
// method lookup failed on type, now lookup on pointer to type
|
||||||
|
pt := reflect.PtrTo(n.typ.rtype)
|
||||||
|
if m2, ok2 := pt.MethodByName(n.child[1].ident); ok2 {
|
||||||
|
n.val = m2.Index
|
||||||
|
n.gen = getIndexBinPtrMethod
|
||||||
|
n.typ = &itype{cat: valueT, rtype: m2.Type, recv: &itype{cat: valueT, rtype: pt}, isBinMethod: true}
|
||||||
|
n.recv = &receiver{node: n.child[0]}
|
||||||
|
n.action = aGetMethod
|
||||||
|
break
|
||||||
|
}
|
||||||
|
err = n.cfgErrorf("undefined field or method: %s", n.child[1].ident)
|
||||||
|
}
|
||||||
|
} else if n.typ.cat == ptrT && (n.typ.val.cat == valueT || n.typ.val.cat == errorT) {
|
||||||
|
// Handle pointer on object defined in runtime
|
||||||
|
if method, ok := n.typ.val.rtype.MethodByName(n.child[1].ident); ok {
|
||||||
|
n.val = method.Index
|
||||||
|
n.typ = &itype{cat: valueT, rtype: method.Type, recv: n.typ, isBinMethod: true}
|
||||||
|
n.recv = &receiver{node: n.child[0]}
|
||||||
|
n.gen = getIndexBinMethod
|
||||||
|
n.action = aGetMethod
|
||||||
|
} else if method, ok := reflect.PtrTo(n.typ.val.rtype).MethodByName(n.child[1].ident); ok {
|
||||||
|
n.val = method.Index
|
||||||
|
n.gen = getIndexBinMethod
|
||||||
|
n.typ = &itype{cat: valueT, rtype: method.Type, recv: &itype{cat: valueT, rtype: reflect.PtrTo(n.typ.val.rtype)}, isBinMethod: true}
|
||||||
|
n.recv = &receiver{node: n.child[0]}
|
||||||
|
n.action = aGetMethod
|
||||||
|
} else if field, ok := n.typ.val.rtype.FieldByName(n.child[1].ident); ok {
|
||||||
|
n.typ = &itype{cat: valueT, rtype: field.Type}
|
||||||
|
n.val = field.Index
|
||||||
|
n.gen = getPtrIndexSeq
|
||||||
|
} else {
|
||||||
|
err = n.cfgErrorf("undefined selector: %s", n.child[1].ident)
|
||||||
|
}
|
||||||
|
} 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
|
||||||
|
n.val = m
|
||||||
|
n.findex = notInFrame
|
||||||
|
n.gen = nop
|
||||||
|
n.typ = &itype{}
|
||||||
|
*n.typ = *m.typ
|
||||||
|
n.typ.arg = append([]*itype{n.child[0].typ}, m.typ.arg...)
|
||||||
|
} else {
|
||||||
|
// Handle method with receiver
|
||||||
|
n.gen = getMethod
|
||||||
|
n.val = m
|
||||||
|
n.typ = m.typ
|
||||||
|
n.recv = &receiver{node: n.child[0], index: lind}
|
||||||
|
}
|
||||||
|
} else if m, lind, isPtr, ok := n.typ.lookupBinMethod(n.child[1].ident); ok {
|
||||||
|
n.action = aGetMethod
|
||||||
|
if isPtr && n.typ.fieldSeq(lind).cat != ptrT {
|
||||||
|
n.gen = getIndexSeqPtrMethod
|
||||||
|
} else {
|
||||||
|
n.gen = getIndexSeqMethod
|
||||||
|
}
|
||||||
n.recv = &receiver{node: n.child[0], index: lind}
|
n.recv = &receiver{node: n.child[0], index: lind}
|
||||||
}
|
n.val = append([]int{m.Index}, lind...)
|
||||||
} else if m, lind, isPtr, ok := n.typ.lookupBinMethod(n.child[1].ident); ok {
|
n.typ = &itype{cat: valueT, rtype: m.Type, recv: n.child[0].typ, isBinMethod: true}
|
||||||
n.action = aGetMethod
|
|
||||||
if isPtr && n.typ.fieldSeq(lind).cat != ptrT {
|
|
||||||
n.gen = getIndexSeqPtrMethod
|
|
||||||
} else {
|
} else {
|
||||||
n.gen = getIndexSeqMethod
|
err = n.cfgErrorf("undefined selector: %s", n.child[1].ident)
|
||||||
}
|
}
|
||||||
n.recv = &receiver{node: n.child[0], index: lind}
|
|
||||||
n.val = append([]int{m.Index}, lind...)
|
|
||||||
n.typ = &itype{cat: valueT, rtype: m.Type, recv: n.child[0].typ}
|
|
||||||
} else if ti := n.typ.lookupField(n.child[1].ident); len(ti) > 0 {
|
|
||||||
// Handle struct field
|
|
||||||
n.val = ti
|
|
||||||
switch {
|
|
||||||
case isInterfaceSrc(n.typ):
|
|
||||||
n.typ = n.typ.fieldSeq(ti)
|
|
||||||
n.gen = getMethodByName
|
|
||||||
n.action = aMethod
|
|
||||||
case n.typ.cat == ptrT:
|
|
||||||
n.typ = n.typ.fieldSeq(ti)
|
|
||||||
n.gen = getPtrIndexSeq
|
|
||||||
if n.typ.cat == funcT {
|
|
||||||
// function in a struct field is always wrapped in reflect.Value
|
|
||||||
rtype := n.typ.TypeOf()
|
|
||||||
n.typ = &itype{cat: valueT, rtype: rtype, val: n.typ}
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
n.gen = getIndexSeq
|
|
||||||
n.typ = n.typ.fieldSeq(ti)
|
|
||||||
if n.typ.cat == funcT {
|
|
||||||
// function in a struct field is always wrapped in reflect.Value
|
|
||||||
rtype := n.typ.TypeOf()
|
|
||||||
n.typ = &itype{cat: valueT, rtype: rtype, val: n.typ}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if s, lind, ok := n.typ.lookupBinField(n.child[1].ident); ok {
|
|
||||||
// Handle an embedded binary field into a struct field
|
|
||||||
n.gen = getIndexSeqField
|
|
||||||
lind = append(lind, s.Index...)
|
|
||||||
n.val = lind
|
|
||||||
n.typ = &itype{cat: valueT, rtype: s.Type}
|
|
||||||
} else {
|
|
||||||
err = n.cfgErrorf("undefined selector: %s", n.child[1].ident)
|
|
||||||
}
|
}
|
||||||
if err == nil && n.findex != -1 {
|
if err == nil && n.findex != -1 {
|
||||||
n.findex = sc.add(n.typ)
|
n.findex = sc.add(n.typ)
|
||||||
@@ -1671,15 +1749,17 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
// Chain case clauses.
|
// Chain case clauses.
|
||||||
for i, c := range clauses[:l-1] {
|
for i := l - 1; i >= 0; i-- {
|
||||||
// Chain to next clause.
|
c := clauses[i]
|
||||||
setFNext(c, clauses[i+1])
|
|
||||||
if len(c.child) == 0 {
|
if len(c.child) == 0 {
|
||||||
c.tnext = n // Clause body is empty, exit.
|
c.tnext = n // Clause body is empty, exit.
|
||||||
} else {
|
} else {
|
||||||
body := c.lastChild()
|
body := c.lastChild()
|
||||||
c.tnext = body.start
|
c.tnext = body.start
|
||||||
if len(body.child) > 0 && body.lastChild().kind == fallthroughtStmt {
|
c.child[0].tnext = c
|
||||||
|
c.start = c.child[0].start
|
||||||
|
|
||||||
|
if i < l-1 && len(body.child) > 0 && body.lastChild().kind == fallthroughtStmt {
|
||||||
if n.kind == typeSwitch {
|
if n.kind == typeSwitch {
|
||||||
err = body.lastChild().cfgErrorf("cannot fallthrough in type switch")
|
err = body.lastChild().cfgErrorf("cannot fallthrough in type switch")
|
||||||
}
|
}
|
||||||
@@ -1692,15 +1772,16 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
body.tnext = n // Exit switch at end of clause body.
|
body.tnext = n // Exit switch at end of clause body.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
c := clauses[l-1] // Last clause.
|
if i == l-1 {
|
||||||
c.fnext = n
|
setFNext(clauses[i], n)
|
||||||
if len(c.child) == 0 {
|
continue
|
||||||
c.tnext = n // Clause body is empty, exit.
|
}
|
||||||
} else {
|
if len(clauses[i+1].child) > 1 {
|
||||||
body := c.lastChild()
|
setFNext(c, clauses[i+1].start)
|
||||||
c.tnext = body.start
|
} else {
|
||||||
body.tnext = n
|
setFNext(c, clauses[i+1])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
n.start = n.child[0].start
|
n.start = n.child[0].start
|
||||||
n.child[0].tnext = sbn.start
|
n.child[0].tnext = sbn.start
|
||||||
@@ -1821,7 +1902,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
switch {
|
switch {
|
||||||
case n.rval.IsValid():
|
case n.rval.IsValid():
|
||||||
n.gen = nop
|
n.gen = nop
|
||||||
n.findex = -1
|
n.findex = notInFrame
|
||||||
case n.anc.kind == assignStmt && n.anc.action == aAssign:
|
case n.anc.kind == assignStmt && n.anc.action == aAssign:
|
||||||
dest := n.anc.child[childPos(n)-n.anc.nright]
|
dest := n.anc.child[childPos(n)-n.anc.nright]
|
||||||
n.typ = dest.typ
|
n.typ = dest.typ
|
||||||
@@ -1843,11 +1924,13 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range n.child[:l] {
|
for _, c := range n.child[:l] {
|
||||||
var index int
|
var index int
|
||||||
if sc.global {
|
if sc.global {
|
||||||
// Global object allocation is already performed in GTA.
|
// Global object allocation is already performed in GTA.
|
||||||
index = sc.sym[c.ident].index
|
index = sc.sym[c.ident].index
|
||||||
|
c.level = globalFrame
|
||||||
} else {
|
} else {
|
||||||
index = sc.add(n.typ)
|
index = sc.add(n.typ)
|
||||||
sc.sym[c.ident] = &symbol{index: index, kind: varSym, typ: n.typ}
|
sc.sym[c.ident] = &symbol{index: index, kind: varSym, typ: n.typ}
|
||||||
@@ -1874,8 +1957,15 @@ func compDefineX(sc *scope, n *node) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
for funtype.cat == valueT && funtype.val != nil {
|
||||||
|
// Retrieve original interpreter type from a wrapped function.
|
||||||
|
// Struct fields of function types are always wrapped in valueT to ensure
|
||||||
|
// their possible use in runtime. In that case, the val field retains the
|
||||||
|
// original interpreter type, which is used now.
|
||||||
|
funtype = funtype.val
|
||||||
|
}
|
||||||
if funtype.cat == valueT {
|
if funtype.cat == valueT {
|
||||||
// Handle functions imported from runtime
|
// Handle functions imported from runtime.
|
||||||
for i := 0; i < funtype.rtype.NumOut(); i++ {
|
for i := 0; i < funtype.rtype.NumOut(); i++ {
|
||||||
types = append(types, &itype{cat: valueT, rtype: funtype.rtype.Out(i)})
|
types = append(types, &itype{cat: valueT, rtype: funtype.rtype.Out(i)})
|
||||||
}
|
}
|
||||||
@@ -1899,7 +1989,7 @@ func compDefineX(sc *scope, n *node) error {
|
|||||||
if n.child[0].ident == "_" {
|
if n.child[0].ident == "_" {
|
||||||
n.child[l].gen = typeAssertStatus
|
n.child[l].gen = typeAssertStatus
|
||||||
} else {
|
} else {
|
||||||
n.child[l].gen = typeAssert2
|
n.child[l].gen = typeAssertLong
|
||||||
}
|
}
|
||||||
types = append(types, n.child[l].child[1].typ, sc.getType("bool"))
|
types = append(types, n.child[l].child[1].typ, sc.getType("bool"))
|
||||||
n.gen = nop
|
n.gen = nop
|
||||||
@@ -2108,7 +2198,10 @@ func (n *node) isType(sc *scope) bool {
|
|||||||
suffixedPkg := filepath.Join(pkg, baseName)
|
suffixedPkg := filepath.Join(pkg, baseName)
|
||||||
sym, _, ok := sc.lookup(suffixedPkg)
|
sym, _, ok := sc.lookup(suffixedPkg)
|
||||||
if !ok {
|
if !ok {
|
||||||
return false
|
sym, _, ok = sc.lookup(pkg)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if sym.kind != pkgSym {
|
if sym.kind != pkgSym {
|
||||||
return false
|
return false
|
||||||
@@ -2298,6 +2391,20 @@ func isRecursiveField(n *node) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isInConstOrTypeDecl(n *node) bool {
|
||||||
|
anc := n.anc
|
||||||
|
for anc != nil {
|
||||||
|
switch anc.kind {
|
||||||
|
case constDecl, typeDecl:
|
||||||
|
return true
|
||||||
|
case varDecl, funcDecl:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
anc = anc.anc
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// isNewDefine returns true if node refers to a new definition.
|
// isNewDefine returns true if node refers to a new definition.
|
||||||
func isNewDefine(n *node, sc *scope) bool {
|
func isNewDefine(n *node, sc *scope) bool {
|
||||||
if n.ident == "_" {
|
if n.ident == "_" {
|
||||||
@@ -2322,6 +2429,10 @@ func isMethod(n *node) bool {
|
|||||||
return len(n.child[0].child) > 0 // receiver defined
|
return len(n.child[0].child) > 0 // receiver defined
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isFuncField(n *node) bool {
|
||||||
|
return isField(n) && isFunc(n.typ)
|
||||||
|
}
|
||||||
|
|
||||||
func isMapEntry(n *node) bool {
|
func isMapEntry(n *node) bool {
|
||||||
return n.action == aGetIndex && isMap(n.child[0].typ)
|
return n.action == aGetIndex && isMap(n.child[0].typ)
|
||||||
}
|
}
|
||||||
@@ -2334,6 +2445,10 @@ func isBinCall(n *node) bool {
|
|||||||
return isCall(n) && n.child[0].typ.cat == valueT && n.child[0].typ.rtype.Kind() == reflect.Func
|
return isCall(n) && n.child[0].typ.cat == valueT && n.child[0].typ.rtype.Kind() == reflect.Func
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isOffsetof(n *node) bool {
|
||||||
|
return isCall(n) && n.child[0].typ.cat == valueT && n.child[0].rval.String() == "Offsetof"
|
||||||
|
}
|
||||||
|
|
||||||
func mustReturnValue(n *node) bool {
|
func mustReturnValue(n *node) bool {
|
||||||
if len(n.child) < 2 {
|
if len(n.child) < 2 {
|
||||||
return false
|
return false
|
||||||
@@ -2424,7 +2539,9 @@ func gotoLabel(s *symbol) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, c := range s.from {
|
for _, c := range s.from {
|
||||||
c.tnext = s.node.start
|
if c.tnext == nil {
|
||||||
|
c.tnext = s.node.start
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2432,7 +2549,7 @@ func compositeGenerator(n *node, typ *itype, rtyp reflect.Type) (gen bltnGenerat
|
|||||||
switch typ.cat {
|
switch typ.cat {
|
||||||
case aliasT, ptrT:
|
case aliasT, ptrT:
|
||||||
gen = compositeGenerator(n, n.typ.val, rtyp)
|
gen = compositeGenerator(n, n.typ.val, rtyp)
|
||||||
case arrayT:
|
case arrayT, sliceT:
|
||||||
gen = arrayLit
|
gen = arrayLit
|
||||||
case mapT:
|
case mapT:
|
||||||
gen = mapLit
|
gen = mapLit
|
||||||
@@ -2457,6 +2574,10 @@ func compositeGenerator(n *node, typ *itype, rtyp reflect.Type) (gen bltnGenerat
|
|||||||
if rtyp == nil {
|
if rtyp == nil {
|
||||||
rtyp = n.typ.rtype
|
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
|
||||||
|
}
|
||||||
switch k := rtyp.Kind(); k {
|
switch k := rtyp.Kind(); k {
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
if n.nleft == 1 {
|
if n.nleft == 1 {
|
||||||
@@ -2469,6 +2590,8 @@ func compositeGenerator(n *node, typ *itype, rtyp reflect.Type) (gen bltnGenerat
|
|||||||
gen = compositeBinMap
|
gen = compositeBinMap
|
||||||
case reflect.Ptr:
|
case reflect.Ptr:
|
||||||
gen = compositeGenerator(n, typ, n.typ.val.rtype)
|
gen = compositeGenerator(n, typ, n.typ.val.rtype)
|
||||||
|
case reflect.Slice, reflect.Array:
|
||||||
|
gen = compositeBinSlice
|
||||||
default:
|
default:
|
||||||
log.Panic(n.cfgErrorf("compositeGenerator not implemented for type kind: %s", k))
|
log.Panic(n.cfgErrorf("compositeGenerator not implemented for type kind: %s", k))
|
||||||
}
|
}
|
||||||
@@ -2480,8 +2603,8 @@ func compositeGenerator(n *node, typ *itype, rtyp reflect.Type) (gen bltnGenerat
|
|||||||
// array variable it is determined from the value's type, otherwise it is
|
// array variable it is determined from the value's type, otherwise it is
|
||||||
// computed from the source definition.
|
// computed from the source definition.
|
||||||
func arrayTypeLen(n *node) int {
|
func arrayTypeLen(n *node) int {
|
||||||
if n.typ != nil && n.typ.sizedef {
|
if n.typ != nil && n.typ.cat == arrayT {
|
||||||
return n.typ.size
|
return n.typ.length
|
||||||
}
|
}
|
||||||
max := -1
|
max := -1
|
||||||
for i, c := range n.child[1:] {
|
for i, c := range n.child[1:] {
|
||||||
@@ -2504,28 +2627,22 @@ func isValueUntyped(v reflect.Value) bool {
|
|||||||
if v.CanSet() {
|
if v.CanSet() {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
t := v.Type()
|
return v.Type().Implements(constVal)
|
||||||
if t.Implements(constVal) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return t.String() == t.Kind().String()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// isArithmeticAction returns true if the node action is an arithmetic operator.
|
// isArithmeticAction returns true if the node action is an arithmetic operator.
|
||||||
func isArithmeticAction(n *node) bool {
|
func isArithmeticAction(n *node) bool {
|
||||||
switch n.action {
|
switch n.action {
|
||||||
case aAdd, aAnd, aAndNot, aBitNot, aMul, aQuo, aRem, aShl, aShr, aSub, aXor:
|
case aAdd, aAnd, aAndNot, aBitNot, aMul, aNeg, aOr, aPos, aQuo, aRem, aShl, aShr, aSub, aXor:
|
||||||
return true
|
return true
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func isBoolAction(n *node) bool {
|
func isBoolAction(n *node) bool {
|
||||||
switch n.action {
|
switch n.action {
|
||||||
case aEqual, aGreater, aGreaterEqual, aLand, aLor, aLower, aLowerEqual, aNot, aNotEqual:
|
case aEqual, aGreater, aGreaterEqual, aLand, aLor, aLower, aLowerEqual, aNot, aNotEqual:
|
||||||
return true
|
return true
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ func (interp *Interpreter) gta(root *node, rpath, importPath string) ([]*node, e
|
|||||||
// values which may be used in further declarations.
|
// values which may be used in further declarations.
|
||||||
if _, err = interp.cfg(n, importPath); err != nil {
|
if _, err = interp.cfg(n, importPath); err != nil {
|
||||||
// No error processing here, to allow recovery in subtree nodes.
|
// No error processing here, to allow recovery in subtree nodes.
|
||||||
|
// TODO(marc): check for a non recoverable error and return it for better diagnostic.
|
||||||
err = nil
|
err = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,9 +80,7 @@ func (interp *Interpreter) gta(root *node, rpath, importPath string) ([]*node, e
|
|||||||
if typ.isBinMethod {
|
if typ.isBinMethod {
|
||||||
typ = &itype{cat: valueT, rtype: typ.methodCallType(), isBinMethod: true, scope: sc}
|
typ = &itype{cat: valueT, rtype: typ.methodCallType(), isBinMethod: true, scope: sc}
|
||||||
}
|
}
|
||||||
if sc.sym[dest.ident] == nil || sc.sym[dest.ident].typ.incomplete {
|
sc.sym[dest.ident] = &symbol{kind: varSym, global: true, index: sc.add(typ), typ: typ, rval: val, node: n}
|
||||||
sc.sym[dest.ident] = &symbol{kind: varSym, global: true, index: sc.add(typ), typ: typ, rval: val, node: n}
|
|
||||||
}
|
|
||||||
if n.anc.kind == constDecl {
|
if n.anc.kind == constDecl {
|
||||||
sc.sym[dest.ident].kind = constSym
|
sc.sym[dest.ident].kind = constSym
|
||||||
if childPos(n) == len(n.anc.child)-1 {
|
if childPos(n) == len(n.anc.child)-1 {
|
||||||
@@ -115,6 +114,7 @@ func (interp *Interpreter) gta(root *node, rpath, importPath string) ([]*node, e
|
|||||||
sc.sym[c.ident] = &symbol{index: sc.add(n.typ), kind: varSym, global: true, typ: n.typ, node: n}
|
sc.sym[c.ident] = &symbol{index: sc.add(n.typ), kind: varSym, global: true, typ: n.typ, node: n}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
c.level = globalFrame
|
||||||
|
|
||||||
// redeclaration error
|
// redeclaration error
|
||||||
if sym.typ.node != nil && sym.typ.node.anc != nil {
|
if sym.typ.node != nil && sym.typ.node.anc != nil {
|
||||||
@@ -187,11 +187,11 @@ func (interp *Interpreter) gta(root *node, rpath, importPath string) ([]*node, e
|
|||||||
}
|
}
|
||||||
// Try to import a binary package first, or a source package
|
// Try to import a binary package first, or a source package
|
||||||
var pkgName string
|
var pkgName string
|
||||||
if interp.binPkg[ipath] != nil {
|
if pkg := interp.binPkg[ipath]; pkg != nil {
|
||||||
switch name {
|
switch name {
|
||||||
case "_": // no import of symbols
|
case "_": // no import of symbols
|
||||||
case ".": // import symbols in current scope
|
case ".": // import symbols in current scope
|
||||||
for n, v := range interp.binPkg[ipath] {
|
for n, v := range pkg {
|
||||||
typ := v.Type()
|
typ := v.Type()
|
||||||
if isBinType(v) {
|
if isBinType(v) {
|
||||||
typ = typ.Elem()
|
typ = typ.Elem()
|
||||||
@@ -200,16 +200,19 @@ func (interp *Interpreter) gta(root *node, rpath, importPath string) ([]*node, e
|
|||||||
}
|
}
|
||||||
default: // import symbols in package namespace
|
default: // import symbols in package namespace
|
||||||
if name == "" {
|
if name == "" {
|
||||||
name = identifier.FindString(ipath)
|
name = interp.pkgNames[ipath]
|
||||||
}
|
}
|
||||||
// imports of a same package are all mapped in the same scope, so we cannot just
|
// Imports of a same package are all mapped in the same scope, so we cannot just
|
||||||
// map them by their names, otherwise we could have collisions from same-name
|
// map them by their names, otherwise we could have collisions from same-name
|
||||||
// imports in different source files of the same package. Therefore, we suffix
|
// imports in different source files of the same package. Therefore, we suffix
|
||||||
// the key with the basename of the source file.
|
// the key with the basename of the source file.
|
||||||
name = filepath.Join(name, baseName)
|
name = filepath.Join(name, baseName)
|
||||||
if _, exists := sc.sym[name]; !exists {
|
if sym, exists := sc.sym[name]; !exists {
|
||||||
sc.sym[name] = &symbol{kind: pkgSym, typ: &itype{cat: binPkgT, path: ipath, scope: sc}}
|
sc.sym[name] = &symbol{kind: pkgSym, typ: &itype{cat: binPkgT, path: ipath, scope: sc}}
|
||||||
break
|
break
|
||||||
|
} else if sym.kind == pkgSym && sym.typ.cat == srcPkgT && sym.typ.path == ipath {
|
||||||
|
// ignore re-import of identical package
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
// redeclaration error. Not caught by the parser.
|
// redeclaration error. Not caught by the parser.
|
||||||
@@ -231,9 +234,12 @@ func (interp *Interpreter) gta(root *node, rpath, importPath string) ([]*node, e
|
|||||||
name = pkgName
|
name = pkgName
|
||||||
}
|
}
|
||||||
name = filepath.Join(name, baseName)
|
name = filepath.Join(name, baseName)
|
||||||
if _, exists := sc.sym[name]; !exists {
|
if sym, exists := sc.sym[name]; !exists {
|
||||||
sc.sym[name] = &symbol{kind: pkgSym, typ: &itype{cat: srcPkgT, path: ipath, scope: sc}}
|
sc.sym[name] = &symbol{kind: pkgSym, typ: &itype{cat: srcPkgT, path: ipath, scope: sc}}
|
||||||
break
|
break
|
||||||
|
} else if sym.kind == pkgSym && sym.typ.cat == srcPkgT && sym.typ.path == ipath {
|
||||||
|
// ignore re-import of identical package
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
// redeclaration error
|
// redeclaration error
|
||||||
@@ -253,10 +259,11 @@ func (interp *Interpreter) gta(root *node, rpath, importPath string) ([]*node, e
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if n.child[1].kind == identExpr {
|
switch n.child[1].kind {
|
||||||
|
case identExpr, selectorExpr:
|
||||||
n.typ = &itype{cat: aliasT, val: typ, name: typeName, path: importPath, field: typ.field, incomplete: typ.incomplete, scope: sc, node: n.child[0]}
|
n.typ = &itype{cat: aliasT, val: typ, name: typeName, path: importPath, field: typ.field, incomplete: typ.incomplete, scope: sc, node: n.child[0]}
|
||||||
copy(n.typ.method, typ.method)
|
copy(n.typ.method, typ.method)
|
||||||
} else {
|
default:
|
||||||
n.typ = typ
|
n.typ = typ
|
||||||
n.typ.name = typeName
|
n.typ.name = typeName
|
||||||
n.typ.path = importPath
|
n.typ.path = importPath
|
||||||
@@ -317,7 +324,50 @@ func (interp *Interpreter) gtaRetry(nodes []*node, importPath string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(revisit) > 0 {
|
if len(revisit) > 0 {
|
||||||
return revisit[0].cfgErrorf("constant definition loop")
|
n := revisit[0]
|
||||||
|
if n.kind == typeSpec {
|
||||||
|
if err := definedType(n.typ); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return n.cfgErrorf("constant definition loop")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func definedType(typ *itype) error {
|
||||||
|
if !typ.incomplete {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
switch typ.cat {
|
||||||
|
case interfaceT, structT:
|
||||||
|
for _, f := range typ.field {
|
||||||
|
if err := definedType(f.typ); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case funcT:
|
||||||
|
for _, t := range typ.arg {
|
||||||
|
if err := definedType(t); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, t := range typ.ret {
|
||||||
|
if err := definedType(t); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case mapT:
|
||||||
|
if err := definedType(typ.key); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fallthrough
|
||||||
|
case aliasT, arrayT, chanT, chanSendT, chanRecvT, ptrT, variadicT:
|
||||||
|
if err := definedType(typ.val); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case nilT:
|
||||||
|
return typ.node.cfgErrorf("undefined: %s", typ.node.ident)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
156
interp/interp.go
156
interp/interp.go
@@ -14,6 +14,8 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
@@ -65,7 +67,8 @@ type frame struct {
|
|||||||
// Located at start of struct to ensure proper aligment.
|
// Located at start of struct to ensure proper aligment.
|
||||||
id uint64
|
id uint64
|
||||||
|
|
||||||
anc *frame // ancestor frame (global space)
|
root *frame // global space
|
||||||
|
anc *frame // ancestor frame (caller space)
|
||||||
data []reflect.Value // values
|
data []reflect.Value // values
|
||||||
|
|
||||||
mutex sync.RWMutex
|
mutex sync.RWMutex
|
||||||
@@ -74,34 +77,46 @@ type frame struct {
|
|||||||
done reflect.SelectCase // for cancellation of channel operations
|
done reflect.SelectCase // for cancellation of channel operations
|
||||||
}
|
}
|
||||||
|
|
||||||
func newFrame(anc *frame, len int, id uint64) *frame {
|
func newFrame(anc *frame, length int, id uint64) *frame {
|
||||||
f := &frame{
|
f := &frame{
|
||||||
anc: anc,
|
anc: anc,
|
||||||
data: make([]reflect.Value, len),
|
data: make([]reflect.Value, length),
|
||||||
id: id,
|
id: id,
|
||||||
}
|
}
|
||||||
if anc != nil {
|
if anc == nil {
|
||||||
|
f.root = f
|
||||||
|
} else {
|
||||||
f.done = anc.done
|
f.done = anc.done
|
||||||
|
f.root = anc.root
|
||||||
}
|
}
|
||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *frame) runid() uint64 { return atomic.LoadUint64(&f.id) }
|
func (f *frame) runid() uint64 { return atomic.LoadUint64(&f.id) }
|
||||||
func (f *frame) setrunid(id uint64) { atomic.StoreUint64(&f.id, id) }
|
func (f *frame) setrunid(id uint64) { atomic.StoreUint64(&f.id, id) }
|
||||||
func (f *frame) clone() *frame {
|
func (f *frame) clone(fork bool) *frame {
|
||||||
f.mutex.RLock()
|
f.mutex.RLock()
|
||||||
defer f.mutex.RUnlock()
|
defer f.mutex.RUnlock()
|
||||||
return &frame{
|
nf := &frame{
|
||||||
anc: f.anc,
|
anc: f.anc,
|
||||||
data: f.data,
|
root: f.root,
|
||||||
deferred: f.deferred,
|
deferred: f.deferred,
|
||||||
recovered: f.recovered,
|
recovered: f.recovered,
|
||||||
id: f.runid(),
|
id: f.runid(),
|
||||||
done: f.done,
|
done: f.done,
|
||||||
}
|
}
|
||||||
|
if fork {
|
||||||
|
nf.data = make([]reflect.Value, len(f.data))
|
||||||
|
copy(nf.data, f.data)
|
||||||
|
} else {
|
||||||
|
nf.data = f.data
|
||||||
|
}
|
||||||
|
return nf
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exports stores the map of binary packages per package path.
|
// Exports stores the map of binary packages per package path.
|
||||||
|
// The package path is the path joined from the import path and the package name
|
||||||
|
// as specified in source files by the "package" statement.
|
||||||
type Exports map[string]map[string]reflect.Value
|
type Exports map[string]map[string]reflect.Value
|
||||||
|
|
||||||
// imports stores the map of source packages per package path.
|
// imports stores the map of source packages per package path.
|
||||||
@@ -113,13 +128,14 @@ type opt struct {
|
|||||||
cfgDot bool // display CFG graph (debug)
|
cfgDot bool // display CFG graph (debug)
|
||||||
// dotCmd is the command to process the dot graph produced when astDot and/or
|
// dotCmd is the command to process the dot graph produced when astDot and/or
|
||||||
// cfgDot is enabled. It defaults to 'dot -Tdot -o <filename>.dot'.
|
// cfgDot is enabled. It defaults to 'dot -Tdot -o <filename>.dot'.
|
||||||
dotCmd string
|
dotCmd string
|
||||||
noRun bool // compile, but do not run
|
noRun bool // compile, but do not run
|
||||||
fastChan bool // disable cancellable chan operations
|
fastChan bool // disable cancellable chan operations
|
||||||
context build.Context // build context: GOPATH, build constraints
|
context build.Context // build context: GOPATH, build constraints
|
||||||
stdin io.Reader // standard input
|
specialStdio bool // Allows os.Stdin, os.Stdout, os.Stderr to not be file descriptors
|
||||||
stdout io.Writer // standard output
|
stdin io.Reader // standard input
|
||||||
stderr io.Writer // standard error
|
stdout io.Writer // standard output
|
||||||
|
stderr io.Writer // standard error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Interpreter contains global resources and state.
|
// Interpreter contains global resources and state.
|
||||||
@@ -157,7 +173,7 @@ type Interpreter struct {
|
|||||||
const (
|
const (
|
||||||
mainID = "main"
|
mainID = "main"
|
||||||
selfPrefix = "github.com/traefik/yaegi"
|
selfPrefix = "github.com/traefik/yaegi"
|
||||||
selfPath = selfPrefix + "/interp"
|
selfPath = selfPrefix + "/interp/interp"
|
||||||
// DefaultSourceName is the name used by default when the name of the input
|
// DefaultSourceName is the name used by default when the name of the input
|
||||||
// source file has not been specified for an Eval.
|
// source file has not been specified for an Eval.
|
||||||
// TODO(mpl): something even more special as a name?
|
// TODO(mpl): something even more special as a name?
|
||||||
@@ -169,6 +185,9 @@ const (
|
|||||||
NoTest = true
|
NoTest = true
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Self points to the current interpreter if accessed from within itself, or is nil.
|
||||||
|
var Self *Interpreter
|
||||||
|
|
||||||
// Symbols exposes interpreter values.
|
// Symbols exposes interpreter values.
|
||||||
var Symbols = Exports{
|
var Symbols = Exports{
|
||||||
selfPath: map[string]reflect.Value{
|
selfPath: map[string]reflect.Value{
|
||||||
@@ -203,7 +222,7 @@ type Panic struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Capture interpreter stack frames also and remove
|
// TODO: Capture interpreter stack frames also and remove
|
||||||
// fmt.Println(n.cfgErrorf("panic")) in runCfg.
|
// fmt.Fprintln(n.interp.stderr, oNode.cfgErrorf("panic")) in runCfg.
|
||||||
|
|
||||||
func (e Panic) Error() string { return fmt.Sprint(e.Value) }
|
func (e Panic) Error() string { return fmt.Sprint(e.Value) }
|
||||||
|
|
||||||
@@ -230,7 +249,7 @@ type Options struct {
|
|||||||
BuildTags []string
|
BuildTags []string
|
||||||
|
|
||||||
// Standard input, output and error streams.
|
// Standard input, output and error streams.
|
||||||
// They default to os.Stding, os.Stdout and os.Stderr respectively.
|
// They default to os.Stdin, os.Stdout and os.Stderr respectively.
|
||||||
Stdin io.Reader
|
Stdin io.Reader
|
||||||
Stdout, Stderr io.Writer
|
Stdout, Stderr io.Writer
|
||||||
}
|
}
|
||||||
@@ -239,7 +258,7 @@ type Options struct {
|
|||||||
func New(options Options) *Interpreter {
|
func New(options Options) *Interpreter {
|
||||||
i := Interpreter{
|
i := Interpreter{
|
||||||
opt: opt{context: build.Default},
|
opt: opt{context: build.Default},
|
||||||
frame: &frame{data: []reflect.Value{}},
|
frame: newFrame(nil, 0, 0),
|
||||||
fset: token.NewFileSet(),
|
fset: token.NewFileSet(),
|
||||||
universe: initUniverse(),
|
universe: initUniverse(),
|
||||||
scopes: map[string]*scope{},
|
scopes: map[string]*scope{},
|
||||||
@@ -283,6 +302,10 @@ func New(options Options) *Interpreter {
|
|||||||
|
|
||||||
// fastChan disables the cancellable version of channel operations in evalWithContext
|
// fastChan disables the cancellable version of channel operations in evalWithContext
|
||||||
i.opt.fastChan, _ = strconv.ParseBool(os.Getenv("YAEGI_FAST_CHAN"))
|
i.opt.fastChan, _ = strconv.ParseBool(os.Getenv("YAEGI_FAST_CHAN"))
|
||||||
|
|
||||||
|
// specialStdio allows to assign directly io.Writer and io.Reader to os.Stdxxx, even if they are not file descriptors.
|
||||||
|
i.opt.specialStdio, _ = strconv.ParseBool(os.Getenv("YAEGI_SPECIAL_STDIO"))
|
||||||
|
|
||||||
return &i
|
return &i
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -630,33 +653,45 @@ func (interp *Interpreter) getWrapper(t reflect.Type) reflect.Type {
|
|||||||
|
|
||||||
// Use loads binary runtime symbols in the interpreter context so
|
// Use loads binary runtime symbols in the interpreter context so
|
||||||
// they can be used in interpreted code.
|
// they can be used in interpreted code.
|
||||||
func (interp *Interpreter) Use(values Exports) {
|
func (interp *Interpreter) Use(values Exports) error {
|
||||||
for k, v := range values {
|
for k, v := range values {
|
||||||
if k == selfPrefix {
|
importPath := path.Dir(k)
|
||||||
|
packageName := path.Base(k)
|
||||||
|
|
||||||
|
if importPath == "." {
|
||||||
|
return fmt.Errorf("export path %[1]q is missing a package name; did you mean '%[1]s/%[1]s'?", k)
|
||||||
|
}
|
||||||
|
|
||||||
|
if importPath == selfPrefix {
|
||||||
interp.hooks.Parse(v)
|
interp.hooks.Parse(v)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if interp.binPkg[k] == nil {
|
if interp.binPkg[importPath] == nil {
|
||||||
interp.binPkg[k] = make(map[string]reflect.Value)
|
interp.binPkg[importPath] = make(map[string]reflect.Value)
|
||||||
|
interp.pkgNames[importPath] = packageName
|
||||||
}
|
}
|
||||||
|
|
||||||
for s, sym := range v {
|
for s, sym := range v {
|
||||||
interp.binPkg[k][s] = sym
|
interp.binPkg[importPath][s] = sym
|
||||||
|
}
|
||||||
|
if k == selfPath {
|
||||||
|
interp.binPkg[importPath]["Self"] = reflect.ValueOf(interp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checks if input values correspond to stdlib packages by looking for one
|
// Checks if input values correspond to stdlib packages by looking for one
|
||||||
// well known stdlib package path.
|
// well known stdlib package path.
|
||||||
if _, ok := values["fmt"]; ok {
|
if _, ok := values["fmt/fmt"]; ok {
|
||||||
fixStdio(interp)
|
fixStdio(interp)
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// fixStdio redefines interpreter stdlib symbols to use the standard input,
|
// fixStdio redefines interpreter stdlib symbols to use the standard input,
|
||||||
// output and errror assigned to the interpreter. The changes are limited to
|
// output and errror assigned to the interpreter. The changes are limited to
|
||||||
// the interpreter only. Global values os.Stdin, os.Stdout and os.Stderr are
|
// the interpreter only.
|
||||||
// not changed. Note that it is possible to escape the virtualized stdio by
|
// Note that it is possible to escape the virtualized stdio by
|
||||||
// read/write directly to file descriptors 0, 1, 2.
|
// read/write directly to file descriptors 0, 1, 2.
|
||||||
func fixStdio(interp *Interpreter) {
|
func fixStdio(interp *Interpreter) {
|
||||||
p := interp.binPkg["fmt"]
|
p := interp.binPkg["fmt"]
|
||||||
@@ -703,9 +738,23 @@ func fixStdio(interp *Interpreter) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if p = interp.binPkg["os"]; p != nil {
|
if p = interp.binPkg["os"]; p != nil {
|
||||||
p["Stdin"] = reflect.ValueOf(&stdin).Elem()
|
if interp.specialStdio {
|
||||||
p["Stdout"] = reflect.ValueOf(&stdout).Elem()
|
// Inherit streams from interpreter even if they do not have a file descriptor.
|
||||||
p["Stderr"] = reflect.ValueOf(&stderr).Elem()
|
p["Stdin"] = reflect.ValueOf(&stdin).Elem()
|
||||||
|
p["Stdout"] = reflect.ValueOf(&stdout).Elem()
|
||||||
|
p["Stderr"] = reflect.ValueOf(&stderr).Elem()
|
||||||
|
} else {
|
||||||
|
// Inherits streams from interpreter only if they have a file descriptor and preserve original type.
|
||||||
|
if s, ok := stdin.(*os.File); ok {
|
||||||
|
p["Stdin"] = reflect.ValueOf(&s).Elem()
|
||||||
|
}
|
||||||
|
if s, ok := stdout.(*os.File); ok {
|
||||||
|
p["Stdout"] = reflect.ValueOf(&s).Elem()
|
||||||
|
}
|
||||||
|
if s, ok := stderr.(*os.File); ok {
|
||||||
|
p["Stderr"] = reflect.ValueOf(&s).Elem()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -725,24 +774,47 @@ func ignoreScannerError(e *scanner.Error, s string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ImportUsed automatically imports pre-compiled packages included by Use().
|
||||||
|
// This is mainly useful for REPLs, or single command lines. In case of an ambiguous default
|
||||||
|
// package name, for example "rand" for crypto/rand and math/rand, the package name is
|
||||||
|
// constructed by replacing the last "/" by a "_", producing crypto_rand and math_rand.
|
||||||
|
// ImportUsed should not be called more than once, and not after a first Eval, as it may
|
||||||
|
// rename packages.
|
||||||
|
func (interp *Interpreter) ImportUsed() {
|
||||||
|
sc := interp.universe
|
||||||
|
for k := range interp.binPkg {
|
||||||
|
// By construction, the package name is the last path element of the key.
|
||||||
|
name := path.Base(k)
|
||||||
|
if sym, ok := sc.sym[name]; ok {
|
||||||
|
// Handle collision by renaming old and new entries.
|
||||||
|
name2 := key2name(fixKey(sym.typ.path))
|
||||||
|
sc.sym[name2] = sym
|
||||||
|
if name2 != name {
|
||||||
|
delete(sc.sym, name)
|
||||||
|
}
|
||||||
|
name = key2name(fixKey(k))
|
||||||
|
}
|
||||||
|
sc.sym[name] = &symbol{kind: pkgSym, typ: &itype{cat: binPkgT, path: k, scope: sc}}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func key2name(name string) string {
|
||||||
|
return filepath.Join(name, DefaultSourceName)
|
||||||
|
}
|
||||||
|
|
||||||
|
func fixKey(k string) string {
|
||||||
|
i := strings.LastIndex(k, "/")
|
||||||
|
if i >= 0 {
|
||||||
|
k = k[:i] + "_" + k[i+1:]
|
||||||
|
}
|
||||||
|
return k
|
||||||
|
}
|
||||||
|
|
||||||
// REPL performs a Read-Eval-Print-Loop on input reader.
|
// REPL performs a Read-Eval-Print-Loop on input reader.
|
||||||
// Results are printed to the output writer of the Interpreter, provided as option
|
// Results are printed to the output writer of the Interpreter, provided as option
|
||||||
// at creation time. Errors are printed to the similarly defined errors writer.
|
// at creation time. Errors are printed to the similarly defined errors writer.
|
||||||
// The last interpreter result value and error are returned.
|
// The last interpreter result value and error are returned.
|
||||||
func (interp *Interpreter) REPL() (reflect.Value, error) {
|
func (interp *Interpreter) REPL() (reflect.Value, error) {
|
||||||
// Preimport used bin packages, to avoid having to import these packages manually
|
|
||||||
// in REPL mode. These packages are already loaded anyway.
|
|
||||||
sc := interp.universe
|
|
||||||
for k := range interp.binPkg {
|
|
||||||
name := identifier.FindString(k)
|
|
||||||
if name == "" || name == "rand" || name == "scanner" || name == "template" || name == "pprof" {
|
|
||||||
// Skip any package with an ambiguous name (i.e crypto/rand vs math/rand).
|
|
||||||
// Those will have to be imported explicitly.
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
sc.sym[name] = &symbol{kind: pkgSym, typ: &itype{cat: binPkgT, path: k, scope: sc}}
|
|
||||||
}
|
|
||||||
|
|
||||||
in, out, errs := interp.stdin, interp.stdout, interp.stderr
|
in, out, errs := interp.stdin, interp.stdout, interp.stderr
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
end := make(chan struct{}) // channel to terminate the REPL
|
end := make(chan struct{}) // channel to terminate the REPL
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ func TestInterpConsistencyBuild(t *testing.T) {
|
|||||||
file.Name() == "import6.go" || // expect error
|
file.Name() == "import6.go" || // expect error
|
||||||
file.Name() == "init1.go" || // expect error
|
file.Name() == "init1.go" || // expect error
|
||||||
file.Name() == "io0.go" || // use random number
|
file.Name() == "io0.go" || // use random number
|
||||||
|
file.Name() == "issue-1093.go" || // expect error
|
||||||
file.Name() == "op1.go" || // expect error
|
file.Name() == "op1.go" || // expect error
|
||||||
file.Name() == "op7.go" || // expect error
|
file.Name() == "op7.go" || // expect error
|
||||||
file.Name() == "op9.go" || // expect error
|
file.Name() == "op9.go" || // expect error
|
||||||
@@ -114,9 +115,15 @@ func TestInterpConsistencyBuild(t *testing.T) {
|
|||||||
os.Stdout = w
|
os.Stdout = w
|
||||||
|
|
||||||
i := interp.New(interp.Options{GoPath: build.Default.GOPATH})
|
i := interp.New(interp.Options{GoPath: build.Default.GOPATH})
|
||||||
i.Use(stdlib.Symbols)
|
if err := i.Use(stdlib.Symbols); err != nil {
|
||||||
i.Use(interp.Symbols)
|
t.Fatal(err)
|
||||||
i.Use(unsafe.Symbols)
|
}
|
||||||
|
if err := i.Use(interp.Symbols); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := i.Use(unsafe.Symbols); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
_, err = i.EvalPath(filePath)
|
_, err = i.EvalPath(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -209,6 +216,11 @@ func TestInterpErrorConsistency(t *testing.T) {
|
|||||||
expectedInterp: "3:17: too many arguments to return",
|
expectedInterp: "3:17: too many arguments to return",
|
||||||
expectedExec: "3:17: too many arguments to return",
|
expectedExec: "3:17: too many arguments to return",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
fileName: "issue-1093.go",
|
||||||
|
expectedInterp: "9:6: cannot use type string as type int in assignment",
|
||||||
|
expectedExec: `9:4: cannot use "a" + b() (type string) as type int in assignment`,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
fileName: "op1.go",
|
fileName: "op1.go",
|
||||||
expectedInterp: "5:2: invalid operation: mismatched types int and float64",
|
expectedInterp: "5:2: invalid operation: mismatched types int and float64",
|
||||||
@@ -254,7 +266,9 @@ func TestInterpErrorConsistency(t *testing.T) {
|
|||||||
filePath := filepath.Join("..", "_test", test.fileName)
|
filePath := filepath.Join("..", "_test", test.fileName)
|
||||||
|
|
||||||
i := interp.New(interp.Options{GoPath: build.Default.GOPATH})
|
i := interp.New(interp.Options{GoPath: build.Default.GOPATH})
|
||||||
i.Use(stdlib.Symbols)
|
if err := i.Use(stdlib.Symbols); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
_, errEval := i.EvalPath(filePath)
|
_, errEval := i.EvalPath(filePath)
|
||||||
if errEval == nil {
|
if errEval == nil {
|
||||||
|
|||||||
@@ -89,6 +89,7 @@ func TestOpVarConst(t *testing.T) {
|
|||||||
{src: "b := uint(5); a+b", res: "15"},
|
{src: "b := uint(5); a+b", res: "15"},
|
||||||
{src: "b := uint(5); b+a", res: "15"},
|
{src: "b := uint(5); b+a", res: "15"},
|
||||||
{src: "b := uint(5); b>a", res: "false"},
|
{src: "b := uint(5); b>a", res: "false"},
|
||||||
|
{src: "const maxlen = cap(aa); var aa = []int{1,2}", err: "1:20: constant definition loop"},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,6 +103,19 @@ func TestEvalStar(t *testing.T) {
|
|||||||
|
|
||||||
func TestEvalAssign(t *testing.T) {
|
func TestEvalAssign(t *testing.T) {
|
||||||
i := interp.New(interp.Options{})
|
i := interp.New(interp.Options{})
|
||||||
|
if err := i.Use(interp.Exports{
|
||||||
|
"testpkg/testpkg": {
|
||||||
|
"val": reflect.ValueOf(int64(11)),
|
||||||
|
},
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, e := i.Eval(`import "testpkg"`)
|
||||||
|
if e != nil {
|
||||||
|
t.Fatal(e)
|
||||||
|
}
|
||||||
|
|
||||||
runTests(t, i, []testCase{
|
runTests(t, i, []testCase{
|
||||||
{src: `a := "Hello"; a += " world"`, res: "Hello world"},
|
{src: `a := "Hello"; a += " world"`, res: "Hello world"},
|
||||||
{src: `b := "Hello"; b += 1`, err: "1:42: invalid operation: mismatched types string and int"},
|
{src: `b := "Hello"; b += 1`, err: "1:42: invalid operation: mismatched types string and int"},
|
||||||
@@ -110,6 +124,9 @@ func TestEvalAssign(t *testing.T) {
|
|||||||
{src: "f := int64(3.2)", err: "1:39: cannot convert expression of type float64 to type int64"},
|
{src: "f := int64(3.2)", err: "1:39: cannot convert expression of type float64 to type int64"},
|
||||||
{src: "g := 1; g <<= 8", res: "256"},
|
{src: "g := 1; g <<= 8", res: "256"},
|
||||||
{src: "h := 1; h >>= 8", res: "0"},
|
{src: "h := 1; h >>= 8", res: "0"},
|
||||||
|
{src: "i := 1; j := &i; (*j) = 2", res: "2"},
|
||||||
|
{src: "i64 := testpkg.val; i64 == 11", res: "true"},
|
||||||
|
{pre: func() { eval(t, i, "k := 1") }, src: `k := "Hello world"`, res: "Hello world"}, // allow reassignment in subsequent evaluations
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,8 +143,8 @@ func TestEvalBuiltin(t *testing.T) {
|
|||||||
{src: `g := cap(a)`, res: "1"},
|
{src: `g := cap(a)`, res: "1"},
|
||||||
{src: `g := len("test")`, res: "4"},
|
{src: `g := len("test")`, res: "4"},
|
||||||
{src: `g := len(map[string]string{"a": "b"})`, res: "1"},
|
{src: `g := len(map[string]string{"a": "b"})`, res: "1"},
|
||||||
{src: `a := len()`, err: "not enough arguments in call to len"},
|
{src: `n := len()`, err: "not enough arguments in call to len"},
|
||||||
{src: `a := len([]int, 0)`, err: "too many arguments for len"},
|
{src: `n := len([]int, 0)`, err: "too many arguments for len"},
|
||||||
{src: `g := cap("test")`, err: "1:37: invalid argument for cap"},
|
{src: `g := cap("test")`, err: "1:37: invalid argument for cap"},
|
||||||
{src: `g := cap(map[string]string{"a": "b"})`, err: "1:37: invalid argument for cap"},
|
{src: `g := cap(map[string]string{"a": "b"})`, err: "1:37: invalid argument for cap"},
|
||||||
{src: `h := make(chan int, 1); close(h); len(h)`, res: "0"},
|
{src: `h := make(chan int, 1); close(h); len(h)`, res: "0"},
|
||||||
@@ -169,6 +186,15 @@ func TestEvalDecl(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEvalDeclWithExpr(t *testing.T) {
|
||||||
|
i := interp.New(interp.Options{})
|
||||||
|
runTests(t, i, []testCase{
|
||||||
|
{src: `a1 := ""; var a2 int; a2 = 2`, res: "2"},
|
||||||
|
{src: `b1 := ""; const b2 = 2; b2`, res: "2"},
|
||||||
|
{src: `c1 := ""; var c2, c3 [8]byte; c3[3]`, res: "0"},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestEvalFunc(t *testing.T) {
|
func TestEvalFunc(t *testing.T) {
|
||||||
i := interp.New(interp.Options{})
|
i := interp.New(interp.Options{})
|
||||||
runTests(t, i, []testCase{
|
runTests(t, i, []testCase{
|
||||||
@@ -182,7 +208,9 @@ func TestEvalFunc(t *testing.T) {
|
|||||||
|
|
||||||
func TestEvalImport(t *testing.T) {
|
func TestEvalImport(t *testing.T) {
|
||||||
i := interp.New(interp.Options{})
|
i := interp.New(interp.Options{})
|
||||||
i.Use(stdlib.Symbols)
|
if err := i.Use(stdlib.Symbols); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
runTests(t, i, []testCase{
|
runTests(t, i, []testCase{
|
||||||
{pre: func() { eval(t, i, `import "time"`) }, src: "2 * time.Second", res: "2s"},
|
{pre: func() { eval(t, i, `import "time"`) }, src: "2 * time.Second", res: "2s"},
|
||||||
})
|
})
|
||||||
@@ -191,7 +219,9 @@ func TestEvalImport(t *testing.T) {
|
|||||||
func TestEvalStdout(t *testing.T) {
|
func TestEvalStdout(t *testing.T) {
|
||||||
var out, err bytes.Buffer
|
var out, err bytes.Buffer
|
||||||
i := interp.New(interp.Options{Stdout: &out, Stderr: &err})
|
i := interp.New(interp.Options{Stdout: &out, Stderr: &err})
|
||||||
i.Use(stdlib.Symbols)
|
if err := i.Use(stdlib.Symbols); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
_, e := i.Eval(`import "fmt"; func main() { fmt.Println("hello") }`)
|
_, e := i.Eval(`import "fmt"; func main() { fmt.Println("hello") }`)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
t.Fatal(e)
|
t.Fatal(e)
|
||||||
@@ -204,7 +234,9 @@ func TestEvalStdout(t *testing.T) {
|
|||||||
|
|
||||||
func TestEvalNil(t *testing.T) {
|
func TestEvalNil(t *testing.T) {
|
||||||
i := interp.New(interp.Options{})
|
i := interp.New(interp.Options{})
|
||||||
i.Use(stdlib.Symbols)
|
if err := i.Use(stdlib.Symbols); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
runTests(t, i, []testCase{
|
runTests(t, i, []testCase{
|
||||||
{desc: "assign nil", src: "a := nil", err: "1:33: use of untyped nil"},
|
{desc: "assign nil", src: "a := nil", err: "1:33: use of untyped nil"},
|
||||||
{desc: "return nil", pre: func() { eval(t, i, "func getNil() error {return nil}") }, src: "getNil()", res: "<nil>"},
|
{desc: "return nil", pre: func() { eval(t, i, "func getNil() error {return nil}") }, src: "getNil()", res: "<nil>"},
|
||||||
@@ -352,7 +384,9 @@ var a = T{
|
|||||||
|
|
||||||
func TestEvalCompositeBin0(t *testing.T) {
|
func TestEvalCompositeBin0(t *testing.T) {
|
||||||
i := interp.New(interp.Options{})
|
i := interp.New(interp.Options{})
|
||||||
i.Use(stdlib.Symbols)
|
if err := i.Use(stdlib.Symbols); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
eval(t, i, `
|
eval(t, i, `
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -444,22 +478,19 @@ func TestEvalSliceExpression(t *testing.T) {
|
|||||||
{src: `a := []int{0,1,2}[:]`, res: "[0 1 2]"},
|
{src: `a := []int{0,1,2}[:]`, res: "[0 1 2]"},
|
||||||
{src: `a := []int{0,1,2,3}[1:3:4]`, res: "[1 2]"},
|
{src: `a := []int{0,1,2,3}[1:3:4]`, res: "[1 2]"},
|
||||||
{src: `a := []int{0,1,2,3}[:3:4]`, res: "[0 1 2]"},
|
{src: `a := []int{0,1,2,3}[:3:4]`, res: "[0 1 2]"},
|
||||||
{src: `ar := [3]int{0,1,2}
|
{src: `ar := [3]int{0,1,2}; a := ar[1:3]`, res: "[1 2]"},
|
||||||
a := ar[1:3]`, res: "[1 2]"},
|
|
||||||
{src: `a := (&[3]int{0,1,2})[1:3]`, res: "[1 2]"},
|
{src: `a := (&[3]int{0,1,2})[1:3]`, res: "[1 2]"},
|
||||||
{src: `a := (&[3]int{0,1,2})[1:3]`, res: "[1 2]"},
|
{src: `a := (&[3]int{0,1,2})[1:3]`, res: "[1 2]"},
|
||||||
{src: `s := "hello"[1:3]`, res: "el"},
|
{src: `s := "hello"[1:3]`, res: "el"},
|
||||||
{src: `str := "hello"
|
{src: `str := "hello"; s := str[1:3]`, res: "el"},
|
||||||
s := str[1:3]`, res: "el"},
|
|
||||||
{src: `a := int(1)[0:1]`, err: "1:33: cannot slice type int"},
|
{src: `a := int(1)[0:1]`, err: "1:33: cannot slice type int"},
|
||||||
{src: `a := ([3]int{0,1,2})[1:3]`, err: "1:33: cannot slice type [3]int"},
|
|
||||||
{src: `a := (&[]int{0,1,2,3})[1:3]`, err: "1:33: cannot slice type *[]int"},
|
{src: `a := (&[]int{0,1,2,3})[1:3]`, err: "1:33: cannot slice type *[]int"},
|
||||||
{src: `a := "hello"[1:3:4]`, err: "1:45: invalid operation: 3-index slice of string"},
|
{src: `a := "hello"[1:3:4]`, err: "1:45: invalid operation: 3-index slice of string"},
|
||||||
{src: `ar := [3]int{0,1,2}
|
{src: `ar := [3]int{0,1,2}; a := ar[:4]`, err: "1:58: index int is out of bounds"},
|
||||||
a := ar[:4]`, err: "2:16: index int is out of bounds"},
|
|
||||||
{src: `a := []int{0,1,2,3}[1::4]`, err: "1:49: 2nd index required in 3-index slice"},
|
{src: `a := []int{0,1,2,3}[1::4]`, err: "1:49: 2nd index required in 3-index slice"},
|
||||||
{src: `a := []int{0,1,2,3}[1:3:]`, err: "1:51: 3rd index required in 3-index slice"},
|
{src: `a := []int{0,1,2,3}[1:3:]`, err: "1:51: 3rd index required in 3-index slice"},
|
||||||
{src: `a := []int{0,1,2}[3:1]`, err: "invalid index values, must be low <= high <= max"},
|
{src: `a := []int{0,1,2}[3:1]`, err: "invalid index values, must be low <= high <= max"},
|
||||||
|
{pre: func() { eval(t, i, `type Str = string; var r Str = "truc"`) }, src: `r[1]`, res: "114"},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -497,12 +528,19 @@ func TestEvalMethod(t *testing.T) {
|
|||||||
Hello() string
|
Hello() string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Hey interface {
|
||||||
|
Hello() string
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Root) Hello() string { return "Hello " + r.Name }
|
func (r *Root) Hello() string { return "Hello " + r.Name }
|
||||||
|
|
||||||
var r = Root{"R"}
|
var r = Root{"R"}
|
||||||
var o = One{r}
|
var o = One{r}
|
||||||
var root interface{} = &Root{Name: "test1"}
|
// TODO(mpl): restore empty interfaces when type assertions work (again) on them.
|
||||||
var one interface{} = &One{Root{Name: "test2"}}
|
// var root interface{} = &Root{Name: "test1"}
|
||||||
|
// var one interface{} = &One{Root{Name: "test2"}}
|
||||||
|
var root Hey = &Root{Name: "test1"}
|
||||||
|
var one Hey = &One{Root{Name: "test2"}}
|
||||||
`)
|
`)
|
||||||
runTests(t, i, []testCase{
|
runTests(t, i, []testCase{
|
||||||
{src: "r.Hello()", res: "Hello R"},
|
{src: "r.Hello()", res: "Hello R"},
|
||||||
@@ -609,7 +647,9 @@ func TestEvalCall(t *testing.T) {
|
|||||||
|
|
||||||
func TestEvalBinCall(t *testing.T) {
|
func TestEvalBinCall(t *testing.T) {
|
||||||
i := interp.New(interp.Options{})
|
i := interp.New(interp.Options{})
|
||||||
i.Use(stdlib.Symbols)
|
if err := i.Use(stdlib.Symbols); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
if _, err := i.Eval(`import "fmt"`); err != nil {
|
if _, err := i.Eval(`import "fmt"`); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -635,9 +675,11 @@ func TestEvalMissingSymbol(t *testing.T) {
|
|||||||
F S2
|
F S2
|
||||||
}
|
}
|
||||||
i := interp.New(interp.Options{})
|
i := interp.New(interp.Options{})
|
||||||
i.Use(interp.Exports{"p": map[string]reflect.Value{
|
if err := i.Use(interp.Exports{"p/p": map[string]reflect.Value{
|
||||||
"S1": reflect.Zero(reflect.TypeOf(&S1{})),
|
"S1": reflect.Zero(reflect.TypeOf(&S1{})),
|
||||||
}})
|
}}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
_, err := i.Eval(`import "p"`)
|
_, err := i.Eval(`import "p"`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to import package: %v", err)
|
t.Fatalf("failed to import package: %v", err)
|
||||||
@@ -706,7 +748,9 @@ func TestEvalWithContext(t *testing.T) {
|
|||||||
go func() {
|
go func() {
|
||||||
defer close(done)
|
defer close(done)
|
||||||
i := interp.New(interp.Options{})
|
i := interp.New(interp.Options{})
|
||||||
i.Use(stdlib.Symbols)
|
if err := i.Use(stdlib.Symbols); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
_, err := i.Eval(`import "sync"`)
|
_, err := i.Eval(`import "sync"`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf(`failed to import "sync": %v`, err)
|
t.Errorf(`failed to import "sync": %v`, err)
|
||||||
@@ -743,6 +787,8 @@ func TestEvalWithContext(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func runTests(t *testing.T, i *interp.Interpreter, tests []testCase) {
|
func runTests(t *testing.T, i *interp.Interpreter, tests []testCase) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
t.Run(test.desc, func(t *testing.T) {
|
t.Run(test.desc, func(t *testing.T) {
|
||||||
if test.skip != "" {
|
if test.skip != "" {
|
||||||
@@ -772,6 +818,8 @@ func eval(t *testing.T, i *interp.Interpreter, src string) reflect.Value {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func assertEval(t *testing.T, i *interp.Interpreter, src, expectedError, expectedRes string) {
|
func assertEval(t *testing.T, i *interp.Interpreter, src, expectedError, expectedRes string) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
res, err := i.Eval(src)
|
res, err := i.Eval(src)
|
||||||
|
|
||||||
if expectedError != "" {
|
if expectedError != "" {
|
||||||
@@ -795,6 +843,7 @@ func assertEval(t *testing.T, i *interp.Interpreter, src, expectedError, expecte
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMultiEval(t *testing.T) {
|
func TestMultiEval(t *testing.T) {
|
||||||
|
t.Skip("fail in CI only ?")
|
||||||
// catch stdout
|
// catch stdout
|
||||||
backupStdout := os.Stdout
|
backupStdout := os.Stdout
|
||||||
defer func() {
|
defer func() {
|
||||||
@@ -804,8 +853,9 @@ func TestMultiEval(t *testing.T) {
|
|||||||
os.Stdout = w
|
os.Stdout = w
|
||||||
|
|
||||||
i := interp.New(interp.Options{})
|
i := interp.New(interp.Options{})
|
||||||
i.Use(stdlib.Symbols)
|
if err := i.Use(stdlib.Symbols); err != nil {
|
||||||
var err error
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
f, err := os.Open(filepath.Join("testdata", "multi", "731"))
|
f, err := os.Open(filepath.Join("testdata", "multi", "731"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -841,9 +891,11 @@ func TestMultiEval(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMultiEvalNoName(t *testing.T) {
|
func TestMultiEvalNoName(t *testing.T) {
|
||||||
|
t.Skip("fail in CI only ?")
|
||||||
i := interp.New(interp.Options{})
|
i := interp.New(interp.Options{})
|
||||||
i.Use(stdlib.Symbols)
|
if err := i.Use(stdlib.Symbols); err != nil {
|
||||||
var err error
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
f, err := os.Open(filepath.Join("testdata", "multi", "731"))
|
f, err := os.Open(filepath.Join("testdata", "multi", "731"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -875,7 +927,9 @@ func TestMultiEvalNoName(t *testing.T) {
|
|||||||
func TestImportPathIsKey(t *testing.T) {
|
func TestImportPathIsKey(t *testing.T) {
|
||||||
// No need to check the results of Eval, as TestFile already does it.
|
// No need to check the results of Eval, as TestFile already does it.
|
||||||
i := interp.New(interp.Options{GoPath: filepath.FromSlash("../_test/testdata/redeclaration-global7")})
|
i := interp.New(interp.Options{GoPath: filepath.FromSlash("../_test/testdata/redeclaration-global7")})
|
||||||
i.Use(stdlib.Symbols)
|
if err := i.Use(stdlib.Symbols); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
filePath := filepath.Join("..", "_test", "ipp_as_key.go")
|
filePath := filepath.Join("..", "_test", "ipp_as_key.go")
|
||||||
if _, err := i.EvalPath(filePath); err != nil {
|
if _, err := i.EvalPath(filePath); err != nil {
|
||||||
@@ -928,9 +982,6 @@ func TestImportPathIsKey(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
packages := i.Packages()
|
packages := i.Packages()
|
||||||
if len(packages) != len(wantPackages) {
|
|
||||||
t.Fatalf("want %d, got %d", len(wantPackages), len(packages))
|
|
||||||
}
|
|
||||||
for k, v := range wantPackages {
|
for k, v := range wantPackages {
|
||||||
pkg := packages[k]
|
pkg := packages[k]
|
||||||
if pkg != v {
|
if pkg != v {
|
||||||
@@ -957,7 +1008,9 @@ func TestConcurrentEvals(t *testing.T) {
|
|||||||
_ = pout.Close()
|
_ = pout.Close()
|
||||||
}()
|
}()
|
||||||
interpr := interp.New(interp.Options{Stdout: pout})
|
interpr := interp.New(interp.Options{Stdout: pout})
|
||||||
interpr.Use(stdlib.Symbols)
|
if err := interpr.Use(stdlib.Symbols); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := interpr.EvalPath("testdata/concurrent/hello1.go"); err != nil {
|
if _, err := interpr.EvalPath("testdata/concurrent/hello1.go"); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -1016,7 +1069,9 @@ func TestConcurrentEvals2(t *testing.T) {
|
|||||||
_ = pout.Close()
|
_ = pout.Close()
|
||||||
}()
|
}()
|
||||||
interpr := interp.New(interp.Options{Stdout: pout})
|
interpr := interp.New(interp.Options{Stdout: pout})
|
||||||
interpr.Use(stdlib.Symbols)
|
if err := interpr.Use(stdlib.Symbols); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
done := make(chan error)
|
done := make(chan error)
|
||||||
go func() {
|
go func() {
|
||||||
@@ -1078,7 +1133,9 @@ func TestConcurrentEvals3(t *testing.T) {
|
|||||||
pinin, poutin := io.Pipe()
|
pinin, poutin := io.Pipe()
|
||||||
pinout, poutout := io.Pipe()
|
pinout, poutout := io.Pipe()
|
||||||
i := interp.New(interp.Options{Stdin: pinin, Stdout: poutout})
|
i := interp.New(interp.Options{Stdin: pinin, Stdout: poutout})
|
||||||
i.Use(stdlib.Symbols)
|
if err := i.Use(stdlib.Symbols); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
_, _ = i.REPL()
|
_, _ = i.REPL()
|
||||||
@@ -1150,12 +1207,16 @@ func TestConcurrentComposite2(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testConcurrentComposite(t *testing.T, filePath string) {
|
func testConcurrentComposite(t *testing.T, filePath string) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
pin, pout := io.Pipe()
|
pin, pout := io.Pipe()
|
||||||
i := interp.New(interp.Options{Stdout: pout})
|
i := interp.New(interp.Options{Stdout: pout})
|
||||||
i.Use(stdlib.Symbols)
|
if err := i.Use(stdlib.Symbols); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
errc := make(chan error)
|
errc := make(chan error)
|
||||||
var output string
|
var output string
|
||||||
@@ -1365,11 +1426,11 @@ func applyCIMultiplier(timeout time.Duration) time.Duration {
|
|||||||
return time.Duration(float64(timeout) * CITimeoutMultiplier)
|
return time.Duration(float64(timeout) * CITimeoutMultiplier)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestREPLDivision(t *testing.T) {
|
func TestREPLCommands(t *testing.T) {
|
||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_ = os.Setenv("YAEGI_PROMPT", "1")
|
_ = os.Setenv("YAEGI_PROMPT", "1") // To force prompts over non-tty streams
|
||||||
defer func() {
|
defer func() {
|
||||||
_ = os.Setenv("YAEGI_PROMPT", "0")
|
_ = os.Setenv("YAEGI_PROMPT", "0")
|
||||||
}()
|
}()
|
||||||
@@ -1379,7 +1440,9 @@ func TestREPLDivision(t *testing.T) {
|
|||||||
pinin, poutin := io.Pipe()
|
pinin, poutin := io.Pipe()
|
||||||
pinout, poutout := io.Pipe()
|
pinout, poutout := io.Pipe()
|
||||||
i := interp.New(interp.Options{Stdin: pinin, Stdout: poutout})
|
i := interp.New(interp.Options{Stdin: pinin, Stdout: poutout})
|
||||||
i.Use(stdlib.Symbols)
|
if err := i.Use(stdlib.Symbols); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
_, _ = i.REPL()
|
_, _ = i.REPL()
|
||||||
@@ -1398,12 +1461,16 @@ func TestREPLDivision(t *testing.T) {
|
|||||||
`7/3`,
|
`7/3`,
|
||||||
`16/5`,
|
`16/5`,
|
||||||
`3./2`, // float
|
`3./2`, // float
|
||||||
|
`reflect.TypeOf(math_rand.Int)`,
|
||||||
|
`reflect.TypeOf(crypto_rand.Int)`,
|
||||||
}
|
}
|
||||||
output := []string{
|
output := []string{
|
||||||
`1`,
|
`1`,
|
||||||
`2`,
|
`2`,
|
||||||
`3`,
|
`3`,
|
||||||
`1.5`,
|
`1.5`,
|
||||||
|
`func() int`,
|
||||||
|
`func(io.Reader, *big.Int) (*big.Int, error)`,
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@@ -1455,3 +1522,108 @@ func TestREPLDivision(t *testing.T) {
|
|||||||
t.Fatal("timeout")
|
t.Fatal("timeout")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStdio(t *testing.T) {
|
||||||
|
i := interp.New(interp.Options{})
|
||||||
|
if err := i.Use(stdlib.Symbols); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
i.ImportUsed()
|
||||||
|
if _, err := i.Eval(`var x = os.Stdout`); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
v, _ := i.Eval(`x`)
|
||||||
|
if _, ok := v.Interface().(*os.File); !ok {
|
||||||
|
t.Fatalf("%v not *os.file", v.Interface())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIssue1142(t *testing.T) {
|
||||||
|
i := interp.New(interp.Options{})
|
||||||
|
runTests(t, i, []testCase{
|
||||||
|
{src: "a := 1; // foo bar", res: "1"},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
type Issue1149Array [3]float32
|
||||||
|
|
||||||
|
func (v Issue1149Array) Foo() string { return "foo" }
|
||||||
|
func (v *Issue1149Array) Bar() string { return "foo" }
|
||||||
|
|
||||||
|
func TestIssue1149(t *testing.T) {
|
||||||
|
i := interp.New(interp.Options{})
|
||||||
|
if err := i.Use(interp.Exports{
|
||||||
|
"pkg/pkg": map[string]reflect.Value{
|
||||||
|
"Type": reflect.ValueOf((*Issue1149Array)(nil)),
|
||||||
|
},
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
i.ImportUsed()
|
||||||
|
|
||||||
|
_, err := i.Eval(`
|
||||||
|
type Type = pkg.Type
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
runTests(t, i, []testCase{
|
||||||
|
{src: "Type{1, 2, 3}.Foo()", res: "foo"},
|
||||||
|
{src: "Type{1, 2, 3}.Bar()", res: "foo"},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIssue1150(t *testing.T) {
|
||||||
|
i := interp.New(interp.Options{})
|
||||||
|
_, err := i.Eval(`
|
||||||
|
type ArrayT [3]float32
|
||||||
|
type SliceT []float32
|
||||||
|
type StructT struct { A, B, C float32 }
|
||||||
|
type StructT2 struct { A, B, C float32 }
|
||||||
|
type FooerT interface { Foo() string }
|
||||||
|
|
||||||
|
func (v ArrayT) Foo() string { return "foo" }
|
||||||
|
func (v SliceT) Foo() string { return "foo" }
|
||||||
|
func (v StructT) Foo() string { return "foo" }
|
||||||
|
func (v *StructT2) Foo() string { return "foo" }
|
||||||
|
|
||||||
|
type Array = ArrayT
|
||||||
|
type Slice = SliceT
|
||||||
|
type Struct = StructT
|
||||||
|
type Struct2 = StructT2
|
||||||
|
type Fooer = FooerT
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
runTests(t, i, []testCase{
|
||||||
|
{desc: "array", src: "Array{1, 2, 3}.Foo()", res: "foo"},
|
||||||
|
{desc: "slice", src: "Slice{1, 2, 3}.Foo()", res: "foo"},
|
||||||
|
{desc: "struct", src: "Struct{1, 2, 3}.Foo()", res: "foo"},
|
||||||
|
{desc: "*struct", src: "Struct2{1, 2, 3}.Foo()", res: "foo"},
|
||||||
|
{desc: "interface", src: "v := Fooer(Array{1, 2, 3}); v.Foo()", res: "foo"},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIssue1151(t *testing.T) {
|
||||||
|
type pkgStruct struct{ X int }
|
||||||
|
type pkgArray [1]int
|
||||||
|
|
||||||
|
i := interp.New(interp.Options{})
|
||||||
|
if err := i.Use(interp.Exports{
|
||||||
|
"pkg/pkg": map[string]reflect.Value{
|
||||||
|
"Struct": reflect.ValueOf((*pkgStruct)(nil)),
|
||||||
|
"Array": reflect.ValueOf((*pkgArray)(nil)),
|
||||||
|
},
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
i.ImportUsed()
|
||||||
|
|
||||||
|
runTests(t, i, []testCase{
|
||||||
|
{src: "x := pkg.Struct{1}", res: "{1}"},
|
||||||
|
{src: "x := pkg.Array{1}", res: "[1]"},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ func Hi(h Helloer) {
|
|||||||
// The method calls will be forwarded to the interpreter.
|
// The method calls will be forwarded to the interpreter.
|
||||||
//
|
//
|
||||||
// Only the Wrap type definition needs to be exported to the interpreter (not
|
// Only the Wrap type definition needs to be exported to the interpreter (not
|
||||||
// the interfaces and methods definitions)
|
// the interfaces and methods definitions).
|
||||||
//
|
//
|
||||||
type Wrap struct {
|
type Wrap struct {
|
||||||
DoHello func() // related to the Hello() method.
|
DoHello func() // related to the Hello() method.
|
||||||
@@ -41,14 +41,53 @@ type Wrap struct {
|
|||||||
|
|
||||||
func (w Wrap) Hello() { w.DoHello() }
|
func (w Wrap) Hello() { w.DoHello() }
|
||||||
|
|
||||||
|
func TestExportsSemantics(t *testing.T) {
|
||||||
|
Foo := &struct{}{}
|
||||||
|
|
||||||
|
t.Run("Correct", func(t *testing.T) {
|
||||||
|
t.Skip()
|
||||||
|
i := interp.New(interp.Options{})
|
||||||
|
|
||||||
|
err := i.Use(interp.Exports{
|
||||||
|
"foo/foo": {"Foo": reflect.ValueOf(Foo)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
i.ImportUsed()
|
||||||
|
|
||||||
|
res, err := i.Eval("foo.Foo")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if res.Interface() != Foo {
|
||||||
|
t.Fatalf("expected foo.Foo to equal local Foo")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Incorrect", func(t *testing.T) {
|
||||||
|
i := interp.New(interp.Options{})
|
||||||
|
|
||||||
|
err := i.Use(interp.Exports{
|
||||||
|
"foo": {"Foo": reflect.ValueOf(Foo)},
|
||||||
|
})
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected error for incorrect Use semantics")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestInterface(t *testing.T) {
|
func TestInterface(t *testing.T) {
|
||||||
i := interp.New(interp.Options{})
|
i := interp.New(interp.Options{})
|
||||||
// export the Wrap type to the interpreter under virtual "wrap" package
|
// export the Wrap type to the interpreter under virtual "wrap" package
|
||||||
i.Use(interp.Exports{
|
err := i.Use(interp.Exports{
|
||||||
"wrap": {
|
"wrap/wrap": {
|
||||||
"Wrap": reflect.ValueOf((*Wrap)(nil)),
|
"Wrap": reflect.ValueOf((*Wrap)(nil)),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
eval(t, i, `
|
eval(t, i, `
|
||||||
import "wrap"
|
import "wrap"
|
||||||
@@ -66,3 +105,29 @@ func NewMyInt(i int) wrap.Wrap {
|
|||||||
w := NewMyInt(4)
|
w := NewMyInt(4)
|
||||||
Hi(w)
|
Hi(w)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type T struct{}
|
||||||
|
|
||||||
|
func (t T) Bar(s ...string) {}
|
||||||
|
|
||||||
|
func TestCallBinVariadicMethod(t *testing.T) {
|
||||||
|
i := interp.New(interp.Options{})
|
||||||
|
err := i.Use(interp.Exports{
|
||||||
|
"mypkg/mypkg": {
|
||||||
|
"T": reflect.ValueOf((*T)(nil)),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
eval(t, i, `
|
||||||
|
package p
|
||||||
|
|
||||||
|
import "mypkg"
|
||||||
|
|
||||||
|
func Foo(x mypkg.T) { x.Bar("s") }
|
||||||
|
`)
|
||||||
|
v := eval(t, i, "p.Foo")
|
||||||
|
bar := v.Interface().(func(t T))
|
||||||
|
bar(T{})
|
||||||
|
}
|
||||||
|
|||||||
@@ -20,6 +20,11 @@ func TestFile(t *testing.T) {
|
|||||||
filePath := "../_test/str.go"
|
filePath := "../_test/str.go"
|
||||||
runCheck(t, filePath)
|
runCheck(t, filePath)
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
_ = os.Setenv("YAEGI_SPECIAL_STDIO", "0")
|
||||||
|
}()
|
||||||
|
_ = os.Setenv("YAEGI_SPECIAL_STDIO", "1")
|
||||||
|
|
||||||
baseDir := filepath.Join("..", "_test")
|
baseDir := filepath.Join("..", "_test")
|
||||||
files, err := ioutil.ReadDir(baseDir)
|
files, err := ioutil.ReadDir(baseDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -37,6 +42,8 @@ func TestFile(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func runCheck(t *testing.T, p string) {
|
func runCheck(t *testing.T, p string) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
wanted, goPath, errWanted := wantedFromComment(p)
|
wanted, goPath, errWanted := wantedFromComment(p)
|
||||||
if wanted == "" {
|
if wanted == "" {
|
||||||
t.Skip(p, "has no comment 'Output:' or 'Error:'")
|
t.Skip(p, "has no comment 'Output:' or 'Error:'")
|
||||||
@@ -48,9 +55,15 @@ func runCheck(t *testing.T, p string) {
|
|||||||
}
|
}
|
||||||
var stdout, stderr bytes.Buffer
|
var stdout, stderr bytes.Buffer
|
||||||
i := interp.New(interp.Options{GoPath: goPath, Stdout: &stdout, Stderr: &stderr})
|
i := interp.New(interp.Options{GoPath: goPath, Stdout: &stdout, Stderr: &stderr})
|
||||||
i.Use(interp.Symbols)
|
if err := i.Use(interp.Symbols); err != nil {
|
||||||
i.Use(stdlib.Symbols)
|
t.Fatal(err)
|
||||||
i.Use(unsafe.Symbols)
|
}
|
||||||
|
if err := i.Use(stdlib.Symbols); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := i.Use(unsafe.Symbols); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
_, err := i.EvalPath(p)
|
_, err := i.EvalPath(p)
|
||||||
if errWanted {
|
if errWanted {
|
||||||
|
|||||||
688
interp/op.go
688
interp/op.go
File diff suppressed because it is too large
Load Diff
1541
interp/run.go
1541
interp/run.go
File diff suppressed because it is too large
Load Diff
@@ -130,6 +130,9 @@ func (s *scope) lookup(ident string) (*symbol, int, bool) {
|
|||||||
level := s.level
|
level := s.level
|
||||||
for {
|
for {
|
||||||
if sym, ok := s.sym[ident]; ok {
|
if sym, ok := s.sym[ident]; ok {
|
||||||
|
if sym.global {
|
||||||
|
return sym, globalFrame, true
|
||||||
|
}
|
||||||
return sym, level - s.level, true
|
return sym, level - s.level, true
|
||||||
}
|
}
|
||||||
if s.anc == nil {
|
if s.anc == nil {
|
||||||
|
|||||||
49
interp/self_example_test.go
Normal file
49
interp/self_example_test.go
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
package interp_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/traefik/yaegi/interp"
|
||||||
|
"github.com/traefik/yaegi/stdlib"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExampleInterpreter_self() {
|
||||||
|
i := interp.New(interp.Options{})
|
||||||
|
|
||||||
|
if err := i.Use(stdlib.Symbols); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := i.Use(interp.Symbols); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := i.Eval(`import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
// Import interp to gain access to Self.
|
||||||
|
"github.com/traefik/yaegi/interp"
|
||||||
|
)`)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = i.Eval(`
|
||||||
|
// Evaluate code directly.
|
||||||
|
fmt.Println("Hello Yaegi from Go")
|
||||||
|
|
||||||
|
// Evaluate code indirectly via the Self access point.
|
||||||
|
_, err := interp.Self.Eval("fmt.Println(\"Hello Yaegi from Yaegi\")")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
//
|
||||||
|
// Hello Yaegi from Go
|
||||||
|
// Hello Yaegi from Yaegi
|
||||||
|
}
|
||||||
@@ -33,12 +33,12 @@ func (interp *Interpreter) importSrc(rPath, importPath string, skipTest bool) (s
|
|||||||
rPath = "."
|
rPath = "."
|
||||||
}
|
}
|
||||||
dir = filepath.Join(filepath.Dir(interp.name), rPath, importPath)
|
dir = filepath.Join(filepath.Dir(interp.name), rPath, importPath)
|
||||||
} else if dir, rPath, err = pkgDir(interp.context.GOPATH, rPath, importPath); err != nil {
|
} else if dir, rPath, err = interp.pkgDir(interp.context.GOPATH, rPath, importPath); err != nil {
|
||||||
// Try again, assuming a root dir at the source location.
|
// Try again, assuming a root dir at the source location.
|
||||||
if rPath, err = interp.rootFromSourceLocation(); err != nil {
|
if rPath, err = interp.rootFromSourceLocation(); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if dir, rPath, err = pkgDir(interp.context.GOPATH, rPath, importPath); err != nil {
|
if dir, rPath, err = interp.pkgDir(interp.context.GOPATH, rPath, importPath); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -181,7 +181,7 @@ func (interp *Interpreter) rootFromSourceLocation() (string, error) {
|
|||||||
|
|
||||||
// pkgDir returns the absolute path in filesystem for a package given its import path
|
// pkgDir returns the absolute path in filesystem for a package given its import path
|
||||||
// and the root of the subtree dependencies.
|
// and the root of the subtree dependencies.
|
||||||
func pkgDir(goPath string, root, importPath string) (string, string, error) {
|
func (interp *Interpreter) pkgDir(goPath string, root, importPath string) (string, string, error) {
|
||||||
rPath := filepath.Join(root, "vendor")
|
rPath := filepath.Join(root, "vendor")
|
||||||
dir := filepath.Join(goPath, "src", rPath, importPath)
|
dir := filepath.Join(goPath, "src", rPath, importPath)
|
||||||
|
|
||||||
@@ -196,6 +196,9 @@ func pkgDir(goPath string, root, importPath string) (string, string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(root) == 0 {
|
if len(root) == 0 {
|
||||||
|
if interp.context.GOPATH == "" {
|
||||||
|
return "", "", fmt.Errorf("unable to find source related to: %q. Either the GOPATH environment variable, or the Interpreter.Options.GoPath needs to be set", importPath)
|
||||||
|
}
|
||||||
return "", "", fmt.Errorf("unable to find source related to: %q", importPath)
|
return "", "", fmt.Errorf("unable to find source related to: %q", importPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -205,7 +208,7 @@ func pkgDir(goPath string, root, importPath string) (string, string, error) {
|
|||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return pkgDir(goPath, prevRoot, importPath)
|
return interp.pkgDir(goPath, prevRoot, importPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
const vendor = "vendor"
|
const vendor = "vendor"
|
||||||
@@ -300,7 +303,7 @@ func effectivePkg(root, path string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// isPathRelative returns true if path starts with "./" or "../".
|
// isPathRelative returns true if path starts with "./" or "../".
|
||||||
|
// It is intended for use on import paths, where "/" is always the directory separator.
|
||||||
func isPathRelative(s string) bool {
|
func isPathRelative(s string) bool {
|
||||||
p := "." + string(filepath.Separator)
|
return strings.HasPrefix(s, "./") || strings.HasPrefix(s, "../")
|
||||||
return strings.HasPrefix(s, p) || strings.HasPrefix(s, "."+p)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -161,6 +161,8 @@ func Test_pkgDir(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interp := &Interpreter{}
|
||||||
|
|
||||||
for _, test := range testCases {
|
for _, test := range testCases {
|
||||||
test := test
|
test := test
|
||||||
t.Run(test.desc, func(t *testing.T) {
|
t.Run(test.desc, func(t *testing.T) {
|
||||||
@@ -178,7 +180,7 @@ func Test_pkgDir(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dir, rPath, err := pkgDir(goPath, test.root, test.path)
|
dir, rPath, err := interp.pkgDir(goPath, test.root, test.path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
319
interp/type.go
319
interp/type.go
@@ -38,6 +38,7 @@ const (
|
|||||||
int64T
|
int64T
|
||||||
mapT
|
mapT
|
||||||
ptrT
|
ptrT
|
||||||
|
sliceT
|
||||||
srcPkgT
|
srcPkgT
|
||||||
stringT
|
stringT
|
||||||
structT
|
structT
|
||||||
@@ -75,6 +76,7 @@ var cats = [...]string{
|
|||||||
int64T: "int64T",
|
int64T: "int64T",
|
||||||
mapT: "mapT",
|
mapT: "mapT",
|
||||||
ptrT: "ptrT",
|
ptrT: "ptrT",
|
||||||
|
sliceT: "sliceT",
|
||||||
srcPkgT: "srcPkgT",
|
srcPkgT: "srcPkgT",
|
||||||
stringT: "stringT",
|
stringT: "stringT",
|
||||||
structT: "structT",
|
structT: "structT",
|
||||||
@@ -109,19 +111,18 @@ type itype struct {
|
|||||||
cat tcat // Type category
|
cat tcat // Type category
|
||||||
field []structField // Array of struct fields if structT or interfaceT
|
field []structField // Array of struct fields if structT or interfaceT
|
||||||
key *itype // Type of key element if MapT or nil
|
key *itype // Type of key element if MapT or nil
|
||||||
val *itype // Type of value element if chanT,chanSendT, chanRecvT, mapT, ptrT, aliasT, arrayT or variadicT
|
val *itype // Type of value element if chanT, chanSendT, chanRecvT, mapT, ptrT, aliasT, arrayT, sliceT or variadicT
|
||||||
recv *itype // Receiver type for funcT or nil
|
recv *itype // Receiver type for funcT or nil
|
||||||
arg []*itype // Argument types if funcT or nil
|
arg []*itype // Argument types if funcT or nil
|
||||||
ret []*itype // Return types if funcT or nil
|
ret []*itype // Return types if funcT or nil
|
||||||
method []*node // Associated methods or nil
|
method []*node // Associated methods or nil
|
||||||
name string // name of type within its package for a defined type
|
name string // name of type within its package for a defined type
|
||||||
path string // for a defined type, the package import path
|
path string // for a defined type, the package import path
|
||||||
size int // Size of array if ArrayT
|
length int // length of array if ArrayT
|
||||||
rtype reflect.Type // Reflection type if ValueT, or nil
|
rtype reflect.Type // Reflection type if ValueT, or nil
|
||||||
incomplete bool // true if type must be parsed again (out of order declarations)
|
incomplete bool // true if type must be parsed again (out of order declarations)
|
||||||
recursive bool // true if the type has an element which refer to itself
|
recursive bool // true if the type has an element which refer to itself
|
||||||
untyped bool // true for a literal value (string or number)
|
untyped bool // true for a literal value (string or number)
|
||||||
sizedef bool // true if array size is computed from type definition
|
|
||||||
isBinMethod bool // true if the type refers to a bin method function
|
isBinMethod bool // true if the type refers to a bin method function
|
||||||
node *node // root AST node of type definition
|
node *node // root AST node of type definition
|
||||||
scope *scope // type declaration scope (in case of re-parse incomplete type)
|
scope *scope // type declaration scope (in case of re-parse incomplete type)
|
||||||
@@ -134,12 +135,13 @@ func untypedInt() *itype { return &itype{cat: intT, name: "int", untyped: tr
|
|||||||
func untypedFloat() *itype { return &itype{cat: float64T, name: "float64", untyped: true} }
|
func untypedFloat() *itype { return &itype{cat: float64T, name: "float64", untyped: true} }
|
||||||
func untypedComplex() *itype { return &itype{cat: complex128T, name: "complex128", untyped: true} }
|
func untypedComplex() *itype { return &itype{cat: complex128T, name: "complex128", untyped: true} }
|
||||||
|
|
||||||
|
func errorMethodType(sc *scope) *itype {
|
||||||
|
return &itype{cat: funcT, ret: []*itype{sc.getType("string")}}
|
||||||
|
}
|
||||||
|
|
||||||
// nodeType returns a type definition for the corresponding AST subtree.
|
// nodeType returns a type definition for the corresponding AST subtree.
|
||||||
func nodeType(interp *Interpreter, sc *scope, n *node) (*itype, error) {
|
func nodeType(interp *Interpreter, sc *scope, n *node) (*itype, error) {
|
||||||
if n.typ != nil && !n.typ.incomplete {
|
if n.typ != nil && !n.typ.incomplete {
|
||||||
if n.kind == sliceExpr {
|
|
||||||
n.typ.sizedef = false
|
|
||||||
}
|
|
||||||
return n.typ, nil
|
return n.typ, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,10 +167,10 @@ func nodeType(interp *Interpreter, sc *scope, n *node) (*itype, error) {
|
|||||||
t.incomplete = t.val.incomplete
|
t.incomplete = t.val.incomplete
|
||||||
|
|
||||||
case arrayType:
|
case arrayType:
|
||||||
t.cat = arrayT
|
|
||||||
c0 := n.child[0]
|
c0 := n.child[0]
|
||||||
if len(n.child) == 1 {
|
if len(n.child) == 1 {
|
||||||
// Array size is not defined.
|
// Array size is not defined.
|
||||||
|
t.cat = sliceT
|
||||||
if t.val, err = nodeType(interp, sc, c0); err != nil {
|
if t.val, err = nodeType(interp, sc, c0); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -176,18 +178,19 @@ func nodeType(interp *Interpreter, sc *scope, n *node) (*itype, error) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
// Array size is defined.
|
// Array size is defined.
|
||||||
|
t.cat = arrayT
|
||||||
switch v := c0.rval; {
|
switch v := c0.rval; {
|
||||||
case v.IsValid():
|
case v.IsValid():
|
||||||
// Size if defined by a constant litteral value.
|
// Size if defined by a constant litteral value.
|
||||||
if isConstantValue(v.Type()) {
|
if isConstantValue(v.Type()) {
|
||||||
c := v.Interface().(constant.Value)
|
c := v.Interface().(constant.Value)
|
||||||
t.size = constToInt(c)
|
t.length = constToInt(c)
|
||||||
} else {
|
} else {
|
||||||
t.size = int(v.Int())
|
t.length = int(v.Int())
|
||||||
}
|
}
|
||||||
case c0.kind == ellipsisExpr:
|
case c0.kind == ellipsisExpr:
|
||||||
// [...]T expression, get size from the length of composite array.
|
// [...]T expression, get size from the length of composite array.
|
||||||
t.size = arrayTypeLen(n.anc)
|
t.length = arrayTypeLen(n.anc)
|
||||||
case c0.kind == identExpr:
|
case c0.kind == identExpr:
|
||||||
sym, _, ok := sc.lookup(c0.ident)
|
sym, _, ok := sc.lookup(c0.ident)
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -198,16 +201,16 @@ func nodeType(interp *Interpreter, sc *scope, n *node) (*itype, error) {
|
|||||||
if sym.kind != constSym {
|
if sym.kind != constSym {
|
||||||
return nil, c0.cfgErrorf("non-constant array bound %q", c0.ident)
|
return nil, c0.cfgErrorf("non-constant array bound %q", c0.ident)
|
||||||
}
|
}
|
||||||
if sym.typ == nil || sym.typ.cat != intT {
|
if sym.typ == nil || sym.typ.cat != intT || !sym.rval.IsValid() {
|
||||||
t.incomplete = true
|
t.incomplete = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if v, ok := sym.rval.Interface().(int); ok {
|
if v, ok := sym.rval.Interface().(int); ok {
|
||||||
t.size = v
|
t.length = v
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if c, ok := sym.rval.Interface().(constant.Value); ok {
|
if c, ok := sym.rval.Interface().(constant.Value); ok {
|
||||||
t.size = constToInt(c)
|
t.length = constToInt(c)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
t.incomplete = true
|
t.incomplete = true
|
||||||
@@ -221,12 +224,11 @@ func nodeType(interp *Interpreter, sc *scope, n *node) (*itype, error) {
|
|||||||
t.incomplete = true
|
t.incomplete = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
t.size = constToInt(v)
|
t.length = constToInt(v)
|
||||||
}
|
}
|
||||||
if t.val, err = nodeType(interp, sc, n.child[1]); err != nil {
|
if t.val, err = nodeType(interp, sc, n.child[1]); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
t.sizedef = true
|
|
||||||
t.incomplete = t.incomplete || t.val.incomplete
|
t.incomplete = t.incomplete || t.val.incomplete
|
||||||
|
|
||||||
case basicLit:
|
case basicLit:
|
||||||
@@ -274,16 +276,27 @@ func nodeType(interp *Interpreter, sc *scope, n *node) (*itype, error) {
|
|||||||
t = t1
|
t = t1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the node is to be assigned or returned, the node type is the destination type.
|
// If the node is to be assigned or returned, the node type is the destination type.
|
||||||
dt := t
|
dt := t
|
||||||
|
|
||||||
switch a := n.anc; {
|
switch a := n.anc; {
|
||||||
|
case a.kind == assignStmt && isEmptyInterface(a.child[0].typ):
|
||||||
|
// Because an empty interface concrete type "mutates" as different values are
|
||||||
|
// assigned to it, we need to make a new itype from scratch everytime a new
|
||||||
|
// assignment is made, and not let different nodes (of the same variable) share the
|
||||||
|
// same itype. Otherwise they would overwrite each other.
|
||||||
|
a.child[0].typ = &itype{cat: interfaceT, val: dt}
|
||||||
|
|
||||||
case a.kind == defineStmt && len(a.child) > a.nleft+a.nright:
|
case a.kind == defineStmt && len(a.child) > a.nleft+a.nright:
|
||||||
if dt, err = nodeType(interp, sc, a.child[a.nleft]); err != nil {
|
if dt, err = nodeType(interp, sc, a.child[a.nleft]); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
case a.kind == returnStmt:
|
case a.kind == returnStmt:
|
||||||
dt = sc.def.typ.ret[childPos(n)]
|
dt = sc.def.typ.ret[childPos(n)]
|
||||||
}
|
}
|
||||||
|
|
||||||
if isInterface(dt) {
|
if isInterface(dt) {
|
||||||
dt.val = t
|
dt.val = t
|
||||||
}
|
}
|
||||||
@@ -449,6 +462,9 @@ func nodeType(interp *Interpreter, sc *scope, n *node) (*itype, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
t = sym.typ
|
t = sym.typ
|
||||||
|
if t.incomplete && t.cat == aliasT && t.val != nil && t.val.cat != nilT {
|
||||||
|
t.incomplete = false
|
||||||
|
}
|
||||||
if t.incomplete && t.node != n {
|
if t.incomplete && t.node != n {
|
||||||
m := t.method
|
m := t.method
|
||||||
if t, err = nodeType(interp, sc, t.node); err != nil {
|
if t, err = nodeType(interp, sc, t.node); err != nil {
|
||||||
@@ -471,7 +487,7 @@ func nodeType(interp *Interpreter, sc *scope, n *node) (*itype, error) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
switch lt.cat {
|
switch lt.cat {
|
||||||
case arrayT, mapT:
|
case arrayT, mapT, sliceT, variadicT:
|
||||||
t = lt.val
|
t = lt.val
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -484,21 +500,28 @@ func nodeType(interp *Interpreter, sc *scope, n *node) (*itype, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, field := range n.child[0].child {
|
for _, field := range n.child[0].child {
|
||||||
|
f0 := field.child[0]
|
||||||
if len(field.child) == 1 {
|
if len(field.child) == 1 {
|
||||||
typ, err := nodeType(interp, sc, field.child[0])
|
if f0.ident == "error" {
|
||||||
|
// Unwrap error interface inplace rather than embedding it, because
|
||||||
|
// "error" is lower case which may cause problems with reflect for method lookup.
|
||||||
|
t.field = append(t.field, structField{name: "Error", typ: errorMethodType(sc)})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
typ, err := nodeType(interp, sc, f0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
t.field = append(t.field, structField{name: fieldName(field.child[0]), embed: true, typ: typ})
|
t.field = append(t.field, structField{name: fieldName(f0), embed: true, typ: typ})
|
||||||
incomplete = incomplete || typ.incomplete
|
|
||||||
} else {
|
|
||||||
typ, err := nodeType(interp, sc, field.child[1])
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
t.field = append(t.field, structField{name: field.child[0].ident, typ: typ})
|
|
||||||
incomplete = incomplete || typ.incomplete
|
incomplete = incomplete || typ.incomplete
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
typ, err := nodeType(interp, sc, field.child[1])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
t.field = append(t.field, structField{name: f0.ident, typ: typ})
|
||||||
|
incomplete = incomplete || typ.incomplete
|
||||||
}
|
}
|
||||||
t.incomplete = incomplete
|
t.incomplete = incomplete
|
||||||
|
|
||||||
@@ -557,7 +580,8 @@ func nodeType(interp *Interpreter, sc *scope, n *node) (*itype, error) {
|
|||||||
if v, ok := pkg[name]; ok {
|
if v, ok := pkg[name]; ok {
|
||||||
t.cat = valueT
|
t.cat = valueT
|
||||||
t.rtype = v.Type()
|
t.rtype = v.Type()
|
||||||
if isBinType(v) { // a bin type is encoded as a pointer on nil value
|
if isBinType(v) {
|
||||||
|
// A bin type is encoded as a pointer on a typed nil value.
|
||||||
t.rtype = t.rtype.Elem()
|
t.rtype = t.rtype.Elem()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -586,15 +610,14 @@ func nodeType(interp *Interpreter, sc *scope, n *node) (*itype, error) {
|
|||||||
|
|
||||||
case sliceExpr:
|
case sliceExpr:
|
||||||
t, err = nodeType(interp, sc, n.child[0])
|
t, err = nodeType(interp, sc, n.child[0])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
if t.cat == ptrT {
|
if t.cat == ptrT {
|
||||||
t = t.val
|
t = t.val
|
||||||
}
|
}
|
||||||
if err == nil && t.size != 0 {
|
if t.cat == arrayT {
|
||||||
t1 := *t
|
t = &itype{cat: sliceT, val: t.val, incomplete: t.incomplete, node: n, scope: sc}
|
||||||
t1.size = 0
|
|
||||||
t1.sizedef = false
|
|
||||||
t1.rtype = nil
|
|
||||||
t = &t1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case structType:
|
case structType:
|
||||||
@@ -736,7 +759,7 @@ func (t *itype) finalize() (*itype, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ReferTo returns true if the type contains a reference to a
|
// ReferTo returns true if the type contains a reference to a
|
||||||
// full type name. It allows to asses a type recursive status.
|
// full type name. It allows to assess a type recursive status.
|
||||||
func (t *itype) referTo(name string, seen map[*itype]bool) bool {
|
func (t *itype) referTo(name string, seen map[*itype]bool) bool {
|
||||||
if t.path+"/"+t.name == name {
|
if t.path+"/"+t.name == name {
|
||||||
return true
|
return true
|
||||||
@@ -746,7 +769,7 @@ func (t *itype) referTo(name string, seen map[*itype]bool) bool {
|
|||||||
}
|
}
|
||||||
seen[t] = true
|
seen[t] = true
|
||||||
switch t.cat {
|
switch t.cat {
|
||||||
case aliasT, arrayT, chanT, chanRecvT, chanSendT, ptrT:
|
case aliasT, arrayT, chanT, chanRecvT, chanSendT, ptrT, sliceT, variadicT:
|
||||||
return t.val.referTo(name, seen)
|
return t.val.referTo(name, seen)
|
||||||
case funcT:
|
case funcT:
|
||||||
for _, a := range t.arg {
|
for _, a := range t.arg {
|
||||||
@@ -854,6 +877,10 @@ func (t *itype) isRecursive() bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *itype) isIndirectRecursive() bool {
|
||||||
|
return t.isRecursive() || t.val != nil && t.val.isIndirectRecursive()
|
||||||
|
}
|
||||||
|
|
||||||
// isVariadic returns true if the function type is variadic.
|
// isVariadic returns true if the function type is variadic.
|
||||||
// If the type is not a function or is not variadic, it will
|
// If the type is not a function or is not variadic, it will
|
||||||
// return false.
|
// return false.
|
||||||
@@ -878,13 +905,19 @@ func isComplete(t *itype, visited map[string]bool) bool {
|
|||||||
}
|
}
|
||||||
name := t.path + "/" + t.name
|
name := t.path + "/" + t.name
|
||||||
if visited[name] {
|
if visited[name] {
|
||||||
return !t.incomplete
|
return true
|
||||||
}
|
}
|
||||||
if t.name != "" {
|
if t.name != "" {
|
||||||
visited[name] = true
|
visited[name] = true
|
||||||
}
|
}
|
||||||
switch t.cat {
|
switch t.cat {
|
||||||
case aliasT, arrayT, chanT, chanRecvT, chanSendT, ptrT:
|
case aliasT:
|
||||||
|
if t.val != nil && t.val.cat != nilT {
|
||||||
|
// A type aliased to a partially defined type is considered complete, to allow recursivity.
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
fallthrough
|
||||||
|
case arrayT, chanT, chanRecvT, chanSendT, ptrT, sliceT, variadicT:
|
||||||
return isComplete(t.val, visited)
|
return isComplete(t.val, visited)
|
||||||
case funcT:
|
case funcT:
|
||||||
complete := true
|
complete := true
|
||||||
@@ -898,6 +931,8 @@ func isComplete(t *itype, visited map[string]bool) bool {
|
|||||||
case interfaceT, structT:
|
case interfaceT, structT:
|
||||||
complete := true
|
complete := true
|
||||||
for _, f := range t.field {
|
for _, f := range t.field {
|
||||||
|
// Field implicit type names must be marked as visited, to break false circles.
|
||||||
|
visited[f.typ.path+"/"+f.typ.name] = true
|
||||||
complete = complete && isComplete(f.typ, visited)
|
complete = complete && isComplete(f.typ, visited)
|
||||||
}
|
}
|
||||||
return complete
|
return complete
|
||||||
@@ -920,7 +955,7 @@ func (t *itype) assignableTo(o *itype) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if t.cat == aliasT && o.cat == aliasT {
|
if t.cat == aliasT && o.cat == aliasT {
|
||||||
// if alias types are not identical, it is not assignable.
|
// If alias types are not identical, it is not assignable.
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if t.isNil() && o.hasNil() || o.isNil() && t.hasNil() {
|
if t.isNil() && o.hasNil() || o.isNil() && t.hasNil() {
|
||||||
@@ -931,6 +966,15 @@ func (t *itype) assignableTo(o *itype) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if isInterface(o) && t.implements(o) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if t.isBinMethod && isFunc(o) {
|
||||||
|
// TODO (marc): check that t without receiver as first parameter is equivalent to o.
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
n := t.node
|
n := t.node
|
||||||
if n == nil || !n.rval.IsValid() {
|
if n == nil || !n.rval.IsValid() {
|
||||||
return false
|
return false
|
||||||
@@ -1016,6 +1060,10 @@ func (t *itype) methods() methodSet {
|
|||||||
seen[typ] = true
|
seen[typ] = true
|
||||||
|
|
||||||
switch typ.cat {
|
switch typ.cat {
|
||||||
|
case aliasT:
|
||||||
|
for k, v := range getMethods(typ.val) {
|
||||||
|
res[k] = v
|
||||||
|
}
|
||||||
case interfaceT:
|
case interfaceT:
|
||||||
// Get methods from recursive analysis of interface fields.
|
// Get methods from recursive analysis of interface fields.
|
||||||
for _, f := range typ.field {
|
for _, f := range typ.field {
|
||||||
@@ -1029,7 +1077,7 @@ func (t *itype) methods() methodSet {
|
|||||||
}
|
}
|
||||||
case valueT, errorT:
|
case valueT, errorT:
|
||||||
// Get method from corresponding reflect.Type.
|
// Get method from corresponding reflect.Type.
|
||||||
for i := typ.rtype.NumMethod() - 1; i >= 0; i-- {
|
for i := typ.TypeOf().NumMethod() - 1; i >= 0; i-- {
|
||||||
m := typ.rtype.Method(i)
|
m := typ.rtype.Method(i)
|
||||||
res[m.Name] = m.Type.String()
|
res[m.Name] = m.Type.String()
|
||||||
}
|
}
|
||||||
@@ -1077,11 +1125,7 @@ func (t *itype) id() (res string) {
|
|||||||
case nilT:
|
case nilT:
|
||||||
res = "nil"
|
res = "nil"
|
||||||
case arrayT:
|
case arrayT:
|
||||||
if t.size == 0 {
|
res = "[" + strconv.Itoa(t.length) + "]" + t.val.id()
|
||||||
res = "[]" + t.val.id()
|
|
||||||
} else {
|
|
||||||
res = "[" + strconv.Itoa(t.size) + "]" + t.val.id()
|
|
||||||
}
|
|
||||||
case chanT:
|
case chanT:
|
||||||
res = "chan " + t.val.id()
|
res = "chan " + t.val.id()
|
||||||
case chanSendT:
|
case chanSendT:
|
||||||
@@ -1114,6 +1158,8 @@ func (t *itype) id() (res string) {
|
|||||||
res = "map[" + t.key.id() + "]" + t.val.id()
|
res = "map[" + t.key.id() + "]" + t.val.id()
|
||||||
case ptrT:
|
case ptrT:
|
||||||
res = "*" + t.val.id()
|
res = "*" + t.val.id()
|
||||||
|
case sliceT:
|
||||||
|
res = "[]" + t.val.id()
|
||||||
case structT:
|
case structT:
|
||||||
res = "struct{"
|
res = "struct{"
|
||||||
for _, t := range t.field {
|
for _, t := range t.field {
|
||||||
@@ -1121,15 +1167,44 @@ func (t *itype) id() (res string) {
|
|||||||
}
|
}
|
||||||
res += "}"
|
res += "}"
|
||||||
case valueT:
|
case valueT:
|
||||||
|
if isConstantValue(t.rtype) {
|
||||||
|
res = fixPossibleConstType(t.rtype).String()
|
||||||
|
break
|
||||||
|
}
|
||||||
res = ""
|
res = ""
|
||||||
if t.rtype.PkgPath() != "" {
|
if t.rtype.PkgPath() != "" {
|
||||||
res += t.rtype.PkgPath() + "."
|
res += t.rtype.PkgPath() + "."
|
||||||
}
|
}
|
||||||
res += t.rtype.Name()
|
res += t.rtype.Name()
|
||||||
|
case variadicT:
|
||||||
|
res = "..." + t.val.id()
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fixPossibleConstType returns the input type if it not a constant value,
|
||||||
|
// otherwise, it returns the default Go type corresponding to the
|
||||||
|
// constant.Value.
|
||||||
|
func fixPossibleConstType(t reflect.Type) (r reflect.Type) {
|
||||||
|
cv, ok := reflect.New(t).Elem().Interface().(constant.Value)
|
||||||
|
if !ok {
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
switch cv.Kind() {
|
||||||
|
case constant.Bool:
|
||||||
|
r = reflect.TypeOf(true)
|
||||||
|
case constant.Int:
|
||||||
|
r = reflect.TypeOf(0)
|
||||||
|
case constant.String:
|
||||||
|
r = reflect.TypeOf("")
|
||||||
|
case constant.Float:
|
||||||
|
r = reflect.TypeOf(float64(0))
|
||||||
|
case constant.Complex:
|
||||||
|
r = reflect.TypeOf(complex128(0))
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
// zero instantiates and return a zero value object for the given type during execution.
|
// zero instantiates and return a zero value object for the given type during execution.
|
||||||
func (t *itype) zero() (v reflect.Value, err error) {
|
func (t *itype) zero() (v reflect.Value, err error) {
|
||||||
if t, err = t.finalize(); err != nil {
|
if t, err = t.finalize(); err != nil {
|
||||||
@@ -1139,7 +1214,7 @@ func (t *itype) zero() (v reflect.Value, err error) {
|
|||||||
case aliasT:
|
case aliasT:
|
||||||
v, err = t.val.zero()
|
v, err = t.val.zero()
|
||||||
|
|
||||||
case arrayT, ptrT, structT:
|
case arrayT, ptrT, structT, sliceT:
|
||||||
v = reflect.New(t.frameType()).Elem()
|
v = reflect.New(t.frameType()).Elem()
|
||||||
|
|
||||||
case valueT:
|
case valueT:
|
||||||
@@ -1179,24 +1254,36 @@ func (t *itype) fieldSeq(seq []int) *itype {
|
|||||||
|
|
||||||
// lookupField returns a list of indices, i.e. a path to access a field in a struct object.
|
// lookupField returns a list of indices, i.e. a path to access a field in a struct object.
|
||||||
func (t *itype) lookupField(name string) []int {
|
func (t *itype) lookupField(name string) []int {
|
||||||
switch t.cat {
|
seen := map[*itype]bool{}
|
||||||
case aliasT, ptrT:
|
var lookup func(*itype) []int
|
||||||
return t.val.lookupField(name)
|
|
||||||
}
|
|
||||||
if fi := t.fieldIndex(name); fi >= 0 {
|
|
||||||
return []int{fi}
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, f := range t.field {
|
lookup = func(typ *itype) []int {
|
||||||
switch f.typ.cat {
|
if seen[typ] {
|
||||||
case ptrT, structT, interfaceT, aliasT:
|
return nil
|
||||||
if index2 := f.typ.lookupField(name); len(index2) > 0 {
|
}
|
||||||
return append([]int{i}, index2...)
|
seen[typ] = true
|
||||||
|
|
||||||
|
switch typ.cat {
|
||||||
|
case aliasT, ptrT:
|
||||||
|
return lookup(typ.val)
|
||||||
|
}
|
||||||
|
if fi := typ.fieldIndex(name); fi >= 0 {
|
||||||
|
return []int{fi}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, f := range typ.field {
|
||||||
|
switch f.typ.cat {
|
||||||
|
case ptrT, structT, interfaceT, aliasT:
|
||||||
|
if index2 := lookup(f.typ); len(index2) > 0 {
|
||||||
|
return append([]int{i}, index2...)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return lookup(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
// lookupBinField returns a structfield and a path to access an embedded binary field in a struct object.
|
// lookupBinField returns a structfield and a path to access an embedded binary field in a struct object.
|
||||||
@@ -1207,10 +1294,13 @@ func (t *itype) lookupBinField(name string) (s reflect.StructField, index []int,
|
|||||||
if !isStruct(t) {
|
if !isStruct(t) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
rt := t.rtype
|
rt := t.TypeOf()
|
||||||
if t.cat == valueT && rt.Kind() == reflect.Ptr {
|
for t.cat == valueT && rt.Kind() == reflect.Ptr {
|
||||||
rt = rt.Elem()
|
rt = rt.Elem()
|
||||||
}
|
}
|
||||||
|
if rt.Kind() != reflect.Struct {
|
||||||
|
return
|
||||||
|
}
|
||||||
s, ok = rt.FieldByName(name)
|
s, ok = rt.FieldByName(name)
|
||||||
if !ok {
|
if !ok {
|
||||||
for i, f := range t.field {
|
for i, f := range t.field {
|
||||||
@@ -1241,6 +1331,13 @@ func (t *itype) methodCallType() reflect.Type {
|
|||||||
return reflect.FuncOf(it, ot, t.rtype.IsVariadic())
|
return reflect.FuncOf(it, ot, t.rtype.IsVariadic())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *itype) resolveAlias() *itype {
|
||||||
|
for t.cat == aliasT {
|
||||||
|
t = t.val
|
||||||
|
}
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
// GetMethod returns a pointer to the method definition.
|
// GetMethod returns a pointer to the method definition.
|
||||||
func (t *itype) getMethod(name string) *node {
|
func (t *itype) getMethod(name string) *node {
|
||||||
for _, m := range t.method {
|
for _, m := range t.method {
|
||||||
@@ -1268,6 +1365,9 @@ func (t *itype) lookupMethod(name string) (*node, []int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if t.cat == aliasT || isInterfaceSrc(t) && t.val != nil {
|
||||||
|
return t.val.lookupMethod(name)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return m, index
|
return m, index
|
||||||
}
|
}
|
||||||
@@ -1386,12 +1486,10 @@ func (t *itype) refType(defined map[string]*itype, wrapRecursive bool) reflect.T
|
|||||||
switch t.cat {
|
switch t.cat {
|
||||||
case aliasT:
|
case aliasT:
|
||||||
t.rtype = t.val.refType(defined, wrapRecursive)
|
t.rtype = t.val.refType(defined, wrapRecursive)
|
||||||
case arrayT, variadicT:
|
case arrayT:
|
||||||
if t.sizedef {
|
t.rtype = reflect.ArrayOf(t.length, t.val.refType(defined, wrapRecursive))
|
||||||
t.rtype = reflect.ArrayOf(t.size, t.val.refType(defined, wrapRecursive))
|
case sliceT, variadicT:
|
||||||
} else {
|
t.rtype = reflect.SliceOf(t.val.refType(defined, wrapRecursive))
|
||||||
t.rtype = reflect.SliceOf(t.val.refType(defined, wrapRecursive))
|
|
||||||
}
|
|
||||||
case chanT:
|
case chanT:
|
||||||
t.rtype = reflect.ChanOf(reflect.BothDir, t.val.refType(defined, wrapRecursive))
|
t.rtype = reflect.ChanOf(reflect.BothDir, t.val.refType(defined, wrapRecursive))
|
||||||
case chanRecvT:
|
case chanRecvT:
|
||||||
@@ -1402,7 +1500,7 @@ func (t *itype) refType(defined map[string]*itype, wrapRecursive bool) reflect.T
|
|||||||
t.rtype = reflect.TypeOf(new(error)).Elem()
|
t.rtype = reflect.TypeOf(new(error)).Elem()
|
||||||
case funcT:
|
case funcT:
|
||||||
if t.name != "" {
|
if t.name != "" {
|
||||||
defined[name] = t
|
defined[name] = t // TODO(marc): make sure that key is name and not t.name.
|
||||||
}
|
}
|
||||||
variadic := false
|
variadic := false
|
||||||
in := make([]reflect.Type, len(t.arg))
|
in := make([]reflect.Type, len(t.arg))
|
||||||
@@ -1423,10 +1521,11 @@ func (t *itype) refType(defined map[string]*itype, wrapRecursive bool) reflect.T
|
|||||||
t.rtype = reflect.PtrTo(t.val.refType(defined, wrapRecursive))
|
t.rtype = reflect.PtrTo(t.val.refType(defined, wrapRecursive))
|
||||||
case structT:
|
case structT:
|
||||||
if t.name != "" {
|
if t.name != "" {
|
||||||
if defined[name] != nil {
|
// Check against local t.name and not name to catch recursive type definitions.
|
||||||
|
if defined[t.name] != nil {
|
||||||
recursive = true
|
recursive = true
|
||||||
}
|
}
|
||||||
defined[name] = t
|
defined[t.name] = t
|
||||||
}
|
}
|
||||||
var fields []reflect.StructField
|
var fields []reflect.StructField
|
||||||
// TODO(mpl): make Anonymous work for recursive types too. Maybe not worth the
|
// TODO(mpl): make Anonymous work for recursive types too. Maybe not worth the
|
||||||
@@ -1465,16 +1564,23 @@ func (t *itype) frameType() (r reflect.Type) {
|
|||||||
switch t.cat {
|
switch t.cat {
|
||||||
case aliasT:
|
case aliasT:
|
||||||
r = t.val.frameType()
|
r = t.val.frameType()
|
||||||
case arrayT, variadicT:
|
case arrayT:
|
||||||
if t.sizedef {
|
r = reflect.ArrayOf(t.length, t.val.frameType())
|
||||||
r = reflect.ArrayOf(t.size, t.val.frameType())
|
case sliceT, variadicT:
|
||||||
} else {
|
r = reflect.SliceOf(t.val.frameType())
|
||||||
r = reflect.SliceOf(t.val.frameType())
|
|
||||||
}
|
|
||||||
case funcT:
|
case funcT:
|
||||||
r = reflect.TypeOf((*node)(nil))
|
r = reflect.TypeOf((*node)(nil))
|
||||||
case interfaceT:
|
case interfaceT:
|
||||||
|
if len(t.field) == 0 {
|
||||||
|
// empty interface, do not wrap it
|
||||||
|
r = reflect.TypeOf((*interface{})(nil)).Elem()
|
||||||
|
break
|
||||||
|
}
|
||||||
r = reflect.TypeOf((*valueInterface)(nil)).Elem()
|
r = reflect.TypeOf((*valueInterface)(nil)).Elem()
|
||||||
|
case mapT:
|
||||||
|
r = reflect.MapOf(t.key.frameType(), t.val.frameType())
|
||||||
|
case ptrT:
|
||||||
|
r = reflect.PtrTo(t.val.frameType())
|
||||||
default:
|
default:
|
||||||
r = t.TypeOf()
|
r = t.TypeOf()
|
||||||
}
|
}
|
||||||
@@ -1482,17 +1588,32 @@ func (t *itype) frameType() (r reflect.Type) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *itype) implements(it *itype) bool {
|
func (t *itype) implements(it *itype) bool {
|
||||||
if t.cat == valueT {
|
if isBin(t) {
|
||||||
return t.TypeOf().Implements(it.TypeOf())
|
return t.TypeOf().Implements(it.TypeOf())
|
||||||
}
|
}
|
||||||
return t.methods().contains(it.methods())
|
return t.methods().contains(it.methods())
|
||||||
}
|
}
|
||||||
|
|
||||||
// defaultType returns the default type of an untyped type.
|
// defaultType returns the default type of an untyped type.
|
||||||
func (t *itype) defaultType() *itype {
|
func (t *itype) defaultType(v reflect.Value) *itype {
|
||||||
if !t.untyped {
|
if !t.untyped {
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
// The default type can also be derived from a constant value.
|
||||||
|
if v.IsValid() && t.TypeOf().Implements(constVal) {
|
||||||
|
switch v.Interface().(constant.Value).Kind() {
|
||||||
|
case constant.String:
|
||||||
|
t = untypedString()
|
||||||
|
case constant.Bool:
|
||||||
|
t = untypedBool()
|
||||||
|
case constant.Int:
|
||||||
|
t = untypedInt()
|
||||||
|
case constant.Float:
|
||||||
|
t = untypedFloat()
|
||||||
|
case constant.Complex:
|
||||||
|
t = untypedComplex()
|
||||||
|
}
|
||||||
|
}
|
||||||
typ := *t
|
typ := *t
|
||||||
typ.untyped = false
|
typ.untyped = false
|
||||||
return &typ
|
return &typ
|
||||||
@@ -1510,6 +1631,13 @@ func (t *itype) hasNil() bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *itype) elem() *itype {
|
||||||
|
if t.cat == valueT {
|
||||||
|
return &itype{cat: valueT, rtype: t.rtype.Elem()}
|
||||||
|
}
|
||||||
|
return t.val
|
||||||
|
}
|
||||||
|
|
||||||
func copyDefined(m map[string]*itype) map[string]*itype {
|
func copyDefined(m map[string]*itype) map[string]*itype {
|
||||||
n := make(map[string]*itype, len(m))
|
n := make(map[string]*itype, len(m))
|
||||||
for k, v := range m {
|
for k, v := range m {
|
||||||
@@ -1539,7 +1667,7 @@ func hasRecursiveStruct(t *itype, defined map[string]*itype) bool {
|
|||||||
defined[typ.path+"/"+typ.name] = typ
|
defined[typ.path+"/"+typ.name] = typ
|
||||||
|
|
||||||
for _, f := range typ.field {
|
for _, f := range typ.field {
|
||||||
if hasRecursiveStruct(f.typ, defined) {
|
if hasRecursiveStruct(f.typ, copyDefined(defined)) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1571,6 +1699,13 @@ func defRecvType(n *node) *itype {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func wrappedType(n *node) *itype {
|
||||||
|
if n.typ.cat != valueT {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return n.typ.val
|
||||||
|
}
|
||||||
|
|
||||||
func isShiftNode(n *node) bool {
|
func isShiftNode(n *node) bool {
|
||||||
switch n.action {
|
switch n.action {
|
||||||
case aShl, aShr, aShlAssign, aShrAssign:
|
case aShl, aShr, aShlAssign, aShrAssign:
|
||||||
@@ -1598,12 +1733,27 @@ func isFunc(t *itype) bool { return t.TypeOf().Kind() == reflect.Func }
|
|||||||
func isMap(t *itype) bool { return t.TypeOf().Kind() == reflect.Map }
|
func isMap(t *itype) bool { return t.TypeOf().Kind() == reflect.Map }
|
||||||
func isPtr(t *itype) bool { return t.TypeOf().Kind() == reflect.Ptr }
|
func isPtr(t *itype) bool { return t.TypeOf().Kind() == reflect.Ptr }
|
||||||
|
|
||||||
|
func isEmptyInterface(t *itype) bool {
|
||||||
|
return t.cat == interfaceT && len(t.field) == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func isFuncSrc(t *itype) bool {
|
||||||
|
return t.cat == funcT || (t.cat == aliasT && isFuncSrc(t.val))
|
||||||
|
}
|
||||||
|
|
||||||
|
func isPtrSrc(t *itype) bool {
|
||||||
|
return t.cat == ptrT || (t.cat == aliasT && isPtrSrc(t.val))
|
||||||
|
}
|
||||||
|
|
||||||
func isSendChan(t *itype) bool {
|
func isSendChan(t *itype) bool {
|
||||||
rt := t.TypeOf()
|
rt := t.TypeOf()
|
||||||
return rt.Kind() == reflect.Chan && rt.ChanDir() == reflect.SendDir
|
return rt.Kind() == reflect.Chan && rt.ChanDir() == reflect.SendDir
|
||||||
}
|
}
|
||||||
|
|
||||||
func isArray(t *itype) bool {
|
func isArray(t *itype) bool {
|
||||||
|
if t.cat == nilT {
|
||||||
|
return false
|
||||||
|
}
|
||||||
k := t.TypeOf().Kind()
|
k := t.TypeOf().Kind()
|
||||||
return k == reflect.Array || k == reflect.Slice
|
return k == reflect.Array || k == reflect.Slice
|
||||||
}
|
}
|
||||||
@@ -1612,10 +1762,25 @@ func isInterfaceSrc(t *itype) bool {
|
|||||||
return t.cat == interfaceT || (t.cat == aliasT && isInterfaceSrc(t.val))
|
return t.cat == interfaceT || (t.cat == aliasT && isInterfaceSrc(t.val))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isInterfaceBin(t *itype) bool {
|
||||||
|
return t.cat == valueT && t.rtype.Kind() == reflect.Interface || t.cat == errorT
|
||||||
|
}
|
||||||
|
|
||||||
func isInterface(t *itype) bool {
|
func isInterface(t *itype) bool {
|
||||||
return isInterfaceSrc(t) || t.TypeOf() != nil && t.TypeOf().Kind() == reflect.Interface
|
return isInterfaceSrc(t) || t.TypeOf() != nil && t.TypeOf().Kind() == reflect.Interface
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isBin(t *itype) bool {
|
||||||
|
switch t.cat {
|
||||||
|
case valueT:
|
||||||
|
return true
|
||||||
|
case aliasT, ptrT:
|
||||||
|
return isBin(t.val)
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func isStruct(t *itype) bool {
|
func isStruct(t *itype) bool {
|
||||||
// Test first for a struct category, because a recursive interpreter struct may be
|
// Test first for a struct category, because a recursive interpreter struct may be
|
||||||
// represented by an interface{} at reflect level.
|
// represented by an interface{} at reflect level.
|
||||||
|
|||||||
@@ -32,12 +32,15 @@ func (check typecheck) op(p opPredicates, a action, n, c *node, t reflect.Type)
|
|||||||
//
|
//
|
||||||
// Use typ == nil to indicate assignment to an untyped blank identifier.
|
// Use typ == nil to indicate assignment to an untyped blank identifier.
|
||||||
func (check typecheck) assignment(n *node, typ *itype, context string) error {
|
func (check typecheck) assignment(n *node, typ *itype, context string) error {
|
||||||
|
if n.typ == nil {
|
||||||
|
return n.cfgErrorf("invalid type in %s", context)
|
||||||
|
}
|
||||||
if n.typ.untyped {
|
if n.typ.untyped {
|
||||||
if typ == nil || isInterface(typ) {
|
if typ == nil || isInterface(typ) {
|
||||||
if typ == nil && n.typ.cat == nilT {
|
if typ == nil && n.typ.cat == nilT {
|
||||||
return n.cfgErrorf("use of untyped nil in %s", context)
|
return n.cfgErrorf("use of untyped nil in %s", context)
|
||||||
}
|
}
|
||||||
typ = n.typ.defaultType()
|
typ = n.typ.defaultType(n.rval)
|
||||||
}
|
}
|
||||||
if err := check.convertUntyped(n, typ); err != nil {
|
if err := check.convertUntyped(n, typ); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -48,6 +51,10 @@ func (check typecheck) assignment(n *node, typ *itype, context string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if typ.isIndirectRecursive() || n.typ.isIndirectRecursive() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
if !n.typ.assignableTo(typ) {
|
if !n.typ.assignableTo(typ) {
|
||||||
if context == "" {
|
if context == "" {
|
||||||
return n.cfgErrorf("cannot use type %s as type %s", n.typ.id(), typ.id())
|
return n.cfgErrorf("cannot use type %s as type %s", n.typ.id(), typ.id())
|
||||||
@@ -65,7 +72,7 @@ func (check typecheck) assignExpr(n, dest, src *node) error {
|
|||||||
isConst := n.anc.kind == constDecl
|
isConst := n.anc.kind == constDecl
|
||||||
if !isConst {
|
if !isConst {
|
||||||
// var operations must be typed
|
// var operations must be typed
|
||||||
dest.typ = dest.typ.defaultType()
|
dest.typ = dest.typ.defaultType(src.rval)
|
||||||
}
|
}
|
||||||
|
|
||||||
return check.assignment(src, dest.typ, "assignment")
|
return check.assignment(src, dest.typ, "assignment")
|
||||||
@@ -228,6 +235,15 @@ func (check typecheck) binaryExpr(n *node) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch n.action {
|
switch n.action {
|
||||||
|
case aAdd:
|
||||||
|
if n.typ == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// Catch mixing string and number for "+" operator use.
|
||||||
|
k, k0, k1 := isNumber(n.typ.TypeOf()), isNumber(c0.typ.TypeOf()), isNumber(c1.typ.TypeOf())
|
||||||
|
if k != k0 || k != k1 {
|
||||||
|
return n.cfgErrorf("cannot use type %s as type %s in assignment", c0.typ.id(), n.typ.id())
|
||||||
|
}
|
||||||
case aRem:
|
case aRem:
|
||||||
if zeroConst(c1) {
|
if zeroConst(c1) {
|
||||||
return n.cfgErrorf("invalid operation: division by zero")
|
return n.cfgErrorf("invalid operation: division by zero")
|
||||||
@@ -285,7 +301,10 @@ func (check typecheck) index(n *node, max int) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// arrayLitExpr type checks an array composite literal expression.
|
// arrayLitExpr type checks an array composite literal expression.
|
||||||
func (check typecheck) arrayLitExpr(child []*node, typ *itype, length int) error {
|
func (check typecheck) arrayLitExpr(child []*node, typ *itype) error {
|
||||||
|
cat := typ.cat
|
||||||
|
length := typ.length
|
||||||
|
typ = typ.val
|
||||||
visited := make(map[int]bool, len(child))
|
visited := make(map[int]bool, len(child))
|
||||||
index := 0
|
index := 0
|
||||||
for _, c := range child {
|
for _, c := range child {
|
||||||
@@ -297,7 +316,7 @@ func (check typecheck) arrayLitExpr(child []*node, typ *itype, length int) error
|
|||||||
}
|
}
|
||||||
n = c.child[1]
|
n = c.child[1]
|
||||||
index = int(vInt(c.child[0].rval))
|
index = int(vInt(c.child[0].rval))
|
||||||
case length > 0 && index >= length:
|
case cat == arrayT && index >= length:
|
||||||
return c.cfgErrorf("index %d is out of bounds (>= %d)", index, length)
|
return c.cfgErrorf("index %d is out of bounds (>= %d)", index, length)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -499,9 +518,7 @@ func (check typecheck) sliceExpr(n *node) error {
|
|||||||
case reflect.Array:
|
case reflect.Array:
|
||||||
valid = true
|
valid = true
|
||||||
l = t.Len()
|
l = t.Len()
|
||||||
if c.kind != indexExpr && c.kind != selectorExpr && (c.sym == nil || c.sym.kind != varSym) {
|
// TODO(marc): check addressable status of array object (i.e. composite arrays are not).
|
||||||
return c.cfgErrorf("cannot slice type %s", c.typ.id())
|
|
||||||
}
|
|
||||||
case reflect.Slice:
|
case reflect.Slice:
|
||||||
valid = true
|
valid = true
|
||||||
case reflect.Ptr:
|
case reflect.Ptr:
|
||||||
@@ -638,7 +655,7 @@ func (check typecheck) conversion(n *node, typ *itype) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if isInterface(typ) || !isConstType(typ) {
|
if isInterface(typ) || !isConstType(typ) {
|
||||||
typ = n.typ.defaultType()
|
typ = n.typ.defaultType(n.rval)
|
||||||
}
|
}
|
||||||
return check.convertUntyped(n, typ)
|
return check.convertUntyped(n, typ)
|
||||||
}
|
}
|
||||||
@@ -902,7 +919,7 @@ func arrayDeref(typ *itype) *itype {
|
|||||||
return typ
|
return typ
|
||||||
}
|
}
|
||||||
|
|
||||||
if typ.cat == ptrT && typ.val.cat == arrayT && typ.val.sizedef {
|
if typ.cat == ptrT && typ.val.cat == arrayT {
|
||||||
return typ.val
|
return typ.val
|
||||||
}
|
}
|
||||||
return typ
|
return typ
|
||||||
@@ -958,7 +975,7 @@ func (check typecheck) argument(p param, ftyp *itype, i, l int, ellipsis bool) e
|
|||||||
}
|
}
|
||||||
t := p.Type().TypeOf()
|
t := p.Type().TypeOf()
|
||||||
if t.Kind() != reflect.Slice || !(&itype{cat: valueT, rtype: t.Elem()}).assignableTo(atyp) {
|
if t.Kind() != reflect.Slice || !(&itype{cat: valueT, rtype: t.Elem()}).assignableTo(atyp) {
|
||||||
return p.nod.cfgErrorf("cannot use %s as type %s", p.nod.typ.id(), (&itype{cat: arrayT, val: atyp}).id())
|
return p.nod.cfgErrorf("cannot use %s as type %s", p.nod.typ.id(), (&itype{cat: sliceT, val: atyp}).id())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -980,6 +997,8 @@ func getArg(ftyp *itype, i int) *itype {
|
|||||||
return arg
|
return arg
|
||||||
case i < l:
|
case i < l:
|
||||||
return ftyp.in(i)
|
return ftyp.in(i)
|
||||||
|
case ftyp.cat == valueT && i < ftyp.rtype.NumIn():
|
||||||
|
return &itype{cat: valueT, rtype: ftyp.rtype.In(i)}
|
||||||
default:
|
default:
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -1039,9 +1058,8 @@ func (check typecheck) convertUntyped(n *node, typ *itype) error {
|
|||||||
if len(n.typ.methods()) > 0 { // untyped cannot be set to iface
|
if len(n.typ.methods()) > 0 { // untyped cannot be set to iface
|
||||||
return convErr
|
return convErr
|
||||||
}
|
}
|
||||||
ityp = n.typ.defaultType()
|
ityp = n.typ.defaultType(n.rval)
|
||||||
rtyp = ntyp
|
rtyp = ntyp
|
||||||
|
|
||||||
case isArray(typ) || isMap(typ) || isChan(typ) || isFunc(typ) || isPtr(typ):
|
case isArray(typ) || isMap(typ) || isChan(typ) || isFunc(typ) || isPtr(typ):
|
||||||
// TODO(nick): above we are acting on itype, but really it is an rtype check. This is not clear which type
|
// TODO(nick): above we are acting on itype, but really it is an rtype check. This is not clear which type
|
||||||
// plain we are in. Fix this later.
|
// plain we are in. Fix this later.
|
||||||
|
|||||||
190
interp/value.go
190
interp/value.go
@@ -5,8 +5,15 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
notInFrame = -1 // value of node.findex for literal values (not in frame)
|
||||||
|
globalFrame = -1 // value of node.level for global symbols
|
||||||
|
)
|
||||||
|
|
||||||
func valueGenerator(n *node, i int) func(*frame) reflect.Value {
|
func valueGenerator(n *node, i int) func(*frame) reflect.Value {
|
||||||
switch n.level {
|
switch n.level {
|
||||||
|
case globalFrame:
|
||||||
|
return func(f *frame) reflect.Value { return valueOf(f.root.data, i) }
|
||||||
case 0:
|
case 0:
|
||||||
return func(f *frame) reflect.Value { return valueOf(f.data, i) }
|
return func(f *frame) reflect.Value { return valueOf(f.data, i) }
|
||||||
case 1:
|
case 1:
|
||||||
@@ -33,6 +40,44 @@ func valueOf(data []reflect.Value, i int) reflect.Value {
|
|||||||
return reflect.Value{}
|
return reflect.Value{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func genValueBinMethodOnInterface(n *node, defaultGen func(*frame) reflect.Value) func(*frame) reflect.Value {
|
||||||
|
if n == nil || n.child == nil || n.child[0] == nil ||
|
||||||
|
n.child[0].child == nil || n.child[0].child[0] == nil {
|
||||||
|
return defaultGen
|
||||||
|
}
|
||||||
|
c0 := n.child[0]
|
||||||
|
if c0.child[1] == nil || c0.child[1].ident == "" {
|
||||||
|
return defaultGen
|
||||||
|
}
|
||||||
|
value0 := genValue(c0.child[0])
|
||||||
|
|
||||||
|
return func(f *frame) reflect.Value {
|
||||||
|
v := value0(f)
|
||||||
|
var nod *node
|
||||||
|
|
||||||
|
for v.IsValid() {
|
||||||
|
// Traverse interface indirections to find out concrete type.
|
||||||
|
vi, ok := v.Interface().(valueInterface)
|
||||||
|
if !ok {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
v = vi.value
|
||||||
|
nod = vi.node
|
||||||
|
}
|
||||||
|
|
||||||
|
if nod == nil {
|
||||||
|
return defaultGen(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
typ := nod.typ
|
||||||
|
if typ.node != nil || typ.cat != valueT {
|
||||||
|
return defaultGen(f)
|
||||||
|
}
|
||||||
|
meth, _ := typ.rtype.MethodByName(c0.child[1].ident)
|
||||||
|
return meth.Func
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func genValueRecvIndirect(n *node) func(*frame) reflect.Value {
|
func genValueRecvIndirect(n *node) func(*frame) reflect.Value {
|
||||||
v := genValueRecv(n)
|
v := genValueRecv(n)
|
||||||
return func(f *frame) reflect.Value { return v(f).Elem() }
|
return func(f *frame) reflect.Value { return v(f).Elem() }
|
||||||
@@ -55,6 +100,35 @@ func genValueRecv(n *node) func(*frame) reflect.Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func genValueBinRecv(n *node, recv *receiver) func(*frame) reflect.Value {
|
||||||
|
value := genValue(n)
|
||||||
|
binValue := genValue(recv.node)
|
||||||
|
|
||||||
|
v := func(f *frame) reflect.Value {
|
||||||
|
if def, ok := value(f).Interface().(*node); ok {
|
||||||
|
if def != nil && def.recv != nil && def.recv.val.IsValid() {
|
||||||
|
return def.recv.val
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ival, _ := binValue(f).Interface().(valueInterface)
|
||||||
|
return ival.value
|
||||||
|
}
|
||||||
|
|
||||||
|
fi := recv.index
|
||||||
|
if len(fi) == 0 {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
return func(f *frame) reflect.Value {
|
||||||
|
r := v(f)
|
||||||
|
if r.Kind() == reflect.Ptr {
|
||||||
|
r = r.Elem()
|
||||||
|
}
|
||||||
|
return r.FieldByIndex(fi)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func genValueRecvInterfacePtr(n *node) func(*frame) reflect.Value {
|
func genValueRecvInterfacePtr(n *node) func(*frame) reflect.Value {
|
||||||
v := genValue(n.recv.node)
|
v := genValue(n.recv.node)
|
||||||
fi := n.recv.index
|
fi := n.recv.index
|
||||||
@@ -83,14 +157,27 @@ func genValueAsFunctionWrapper(n *node) func(*frame) reflect.Value {
|
|||||||
if v.IsNil() {
|
if v.IsNil() {
|
||||||
return reflect.New(typ).Elem()
|
return reflect.New(typ).Elem()
|
||||||
}
|
}
|
||||||
return genFunctionWrapper(v.Interface().(*node))(f)
|
vn, ok := v.Interface().(*node)
|
||||||
|
if ok && vn.rval.IsValid() && vn.rval.Type().Kind() == reflect.Func {
|
||||||
|
// The node value is already a callable func, no need to wrap it.
|
||||||
|
return vn.rval
|
||||||
|
}
|
||||||
|
return genFunctionWrapper(vn)(f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func genValueAs(n *node, t reflect.Type) func(*frame) reflect.Value {
|
func genValueAs(n *node, t reflect.Type) func(*frame) reflect.Value {
|
||||||
v := genValue(n)
|
value := genValue(n)
|
||||||
|
|
||||||
return func(f *frame) reflect.Value {
|
return func(f *frame) reflect.Value {
|
||||||
return v(f).Convert(t)
|
v := value(f)
|
||||||
|
switch v.Type().Kind() {
|
||||||
|
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice, reflect.UnsafePointer:
|
||||||
|
if v.IsNil() {
|
||||||
|
return reflect.New(t).Elem()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return v.Convert(t)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,18 +205,16 @@ func genValue(n *node) func(*frame) reflect.Value {
|
|||||||
return func(f *frame) reflect.Value { return v }
|
return func(f *frame) reflect.Value { return v }
|
||||||
}
|
}
|
||||||
if n.sym != nil {
|
if n.sym != nil {
|
||||||
if n.sym.index < 0 {
|
i := n.sym.index
|
||||||
|
if i < 0 {
|
||||||
return genValue(n.sym.node)
|
return genValue(n.sym.node)
|
||||||
}
|
}
|
||||||
i := n.sym.index
|
|
||||||
if n.sym.global {
|
if n.sym.global {
|
||||||
return func(f *frame) reflect.Value {
|
return func(f *frame) reflect.Value { return f.root.data[i] }
|
||||||
return n.interp.frame.data[i]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return valueGenerator(n, i)
|
return valueGenerator(n, i)
|
||||||
}
|
}
|
||||||
if n.findex < 0 {
|
if n.findex == notInFrame {
|
||||||
var v reflect.Value
|
var v reflect.Value
|
||||||
if w, ok := n.val.(reflect.Value); ok {
|
if w, ok := n.val.(reflect.Value); ok {
|
||||||
v = w
|
v = w
|
||||||
@@ -142,6 +227,31 @@ func genValue(n *node) func(*frame) reflect.Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func genDestValue(typ *itype, n *node) func(*frame) reflect.Value {
|
||||||
|
convertLiteralValue(n, typ.TypeOf())
|
||||||
|
switch {
|
||||||
|
case isInterfaceSrc(typ) && !isEmptyInterface(typ):
|
||||||
|
return genValueInterface(n)
|
||||||
|
case isFuncSrc(typ) && n.typ.cat == valueT:
|
||||||
|
return genValueNode(n)
|
||||||
|
case typ.cat == valueT && isFuncSrc(n.typ):
|
||||||
|
return genFunctionWrapper(n)
|
||||||
|
case isInterfaceBin(typ):
|
||||||
|
return genInterfaceWrapper(n, typ.rtype)
|
||||||
|
case n.kind == basicLit && n.val == nil:
|
||||||
|
return func(*frame) reflect.Value { return reflect.New(typ.rtype).Elem() }
|
||||||
|
case isRecursiveType(typ, typ.rtype):
|
||||||
|
return genValueRecursiveInterface(n, typ.rtype)
|
||||||
|
case isRecursiveType(n.typ, n.typ.rtype):
|
||||||
|
return genValueRecursiveInterfacePtrValue(n)
|
||||||
|
case n.typ.untyped && isComplex(typ.TypeOf()):
|
||||||
|
return genValueComplex(n)
|
||||||
|
case n.typ.untyped && !typ.untyped:
|
||||||
|
return genValueAs(n, typ.TypeOf())
|
||||||
|
}
|
||||||
|
return genValue(n)
|
||||||
|
}
|
||||||
|
|
||||||
func genValueArray(n *node) func(*frame) reflect.Value {
|
func genValueArray(n *node) func(*frame) reflect.Value {
|
||||||
value := genValue(n)
|
value := genValue(n)
|
||||||
// dereference array pointer, to support array operations on array pointer
|
// dereference array pointer, to support array operations on array pointer
|
||||||
@@ -163,21 +273,25 @@ func genValueRangeArray(n *node) func(*frame) reflect.Value {
|
|||||||
return value(f).Elem()
|
return value(f).Elem()
|
||||||
}
|
}
|
||||||
case n.typ.val != nil && n.typ.val.cat == interfaceT:
|
case n.typ.val != nil && n.typ.val.cat == interfaceT:
|
||||||
return func(f *frame) reflect.Value {
|
if len(n.typ.val.field) > 0 {
|
||||||
val := value(f)
|
return func(f *frame) reflect.Value {
|
||||||
v := []valueInterface{}
|
val := value(f)
|
||||||
for i := 0; i < val.Len(); i++ {
|
v := []valueInterface{}
|
||||||
switch av := val.Index(i).Interface().(type) {
|
for i := 0; i < val.Len(); i++ {
|
||||||
case []valueInterface:
|
switch av := val.Index(i).Interface().(type) {
|
||||||
v = append(v, av...)
|
case []valueInterface:
|
||||||
case valueInterface:
|
v = append(v, av...)
|
||||||
v = append(v, av)
|
case valueInterface:
|
||||||
default:
|
v = append(v, av)
|
||||||
panic(n.cfgErrorf("invalid type %v", val.Index(i).Type()))
|
default:
|
||||||
|
panic(n.cfgErrorf("invalid type %v", val.Index(i).Type()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return reflect.ValueOf(v)
|
||||||
}
|
}
|
||||||
return reflect.ValueOf(v)
|
|
||||||
}
|
}
|
||||||
|
// empty interface, do not wrap.
|
||||||
|
fallthrough
|
||||||
default:
|
default:
|
||||||
return func(f *frame) reflect.Value {
|
return func(f *frame) reflect.Value {
|
||||||
// This is necessary to prevent changes in the returned
|
// This is necessary to prevent changes in the returned
|
||||||
@@ -207,6 +321,7 @@ func genValueInterface(n *node) func(*frame) reflect.Value {
|
|||||||
return func(f *frame) reflect.Value {
|
return func(f *frame) reflect.Value {
|
||||||
v := value(f)
|
v := value(f)
|
||||||
nod := n
|
nod := n
|
||||||
|
|
||||||
for v.IsValid() {
|
for v.IsValid() {
|
||||||
// traverse interface indirections to find out concrete type
|
// traverse interface indirections to find out concrete type
|
||||||
vi, ok := v.Interface().(valueInterface)
|
vi, ok := v.Interface().(valueInterface)
|
||||||
@@ -216,6 +331,12 @@ func genValueInterface(n *node) func(*frame) reflect.Value {
|
|||||||
v = vi.value
|
v = vi.value
|
||||||
nod = vi.node
|
nod = vi.node
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// empty interface, do not wrap.
|
||||||
|
if nod.typ.cat == interfaceT && len(nod.typ.field) == 0 {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
return reflect.ValueOf(valueInterface{nod, v})
|
return reflect.ValueOf(valueInterface{nod, v})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -226,12 +347,26 @@ func zeroInterfaceValue() reflect.Value {
|
|||||||
return reflect.ValueOf(valueInterface{n, v})
|
return reflect.ValueOf(valueInterface{n, v})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func wantEmptyInterface(n *node) bool {
|
||||||
|
return isEmptyInterface(n.typ) ||
|
||||||
|
n.anc.action == aAssign && n.anc.typ.cat == interfaceT && len(n.anc.typ.field) == 0 ||
|
||||||
|
n.anc.kind == returnStmt && n.anc.val.(*node).typ.ret[0].cat == interfaceT && len(n.anc.val.(*node).typ.ret[0].field) == 0
|
||||||
|
}
|
||||||
|
|
||||||
func genValueOutput(n *node, t reflect.Type) func(*frame) reflect.Value {
|
func genValueOutput(n *node, t reflect.Type) func(*frame) reflect.Value {
|
||||||
value := genValue(n)
|
value := genValue(n)
|
||||||
switch {
|
switch {
|
||||||
case n.anc.action == aAssign && n.anc.typ.cat == interfaceT:
|
case n.anc.action == aAssign && n.anc.typ.cat == interfaceT:
|
||||||
|
if len(n.anc.typ.field) == 0 {
|
||||||
|
// empty interface, do not wrap
|
||||||
|
return value
|
||||||
|
}
|
||||||
fallthrough
|
fallthrough
|
||||||
case n.anc.kind == returnStmt && n.anc.val.(*node).typ.ret[0].cat == interfaceT:
|
case n.anc.kind == returnStmt && n.anc.val.(*node).typ.ret[0].cat == interfaceT:
|
||||||
|
if nod, ok := n.anc.val.(*node); !ok || len(nod.typ.ret[0].field) == 0 {
|
||||||
|
// empty interface, do not wrap
|
||||||
|
return value
|
||||||
|
}
|
||||||
// The result of the builtin has to be returned as an interface type.
|
// The result of the builtin has to be returned as an interface type.
|
||||||
// Wrap it in a valueInterface and return the dereferenced value.
|
// Wrap it in a valueInterface and return the dereferenced value.
|
||||||
return func(f *frame) reflect.Value {
|
return func(f *frame) reflect.Value {
|
||||||
@@ -244,6 +379,17 @@ func genValueOutput(n *node, t reflect.Type) func(*frame) reflect.Value {
|
|||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func valueInterfaceValue(v reflect.Value) reflect.Value {
|
||||||
|
for {
|
||||||
|
vv, ok := v.Interface().(valueInterface)
|
||||||
|
if !ok {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
v = vv.value
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
func genValueInterfaceValue(n *node) func(*frame) reflect.Value {
|
func genValueInterfaceValue(n *node) func(*frame) reflect.Value {
|
||||||
value := genValue(n)
|
value := genValue(n)
|
||||||
|
|
||||||
@@ -254,7 +400,7 @@ func genValueInterfaceValue(n *node) func(*frame) reflect.Value {
|
|||||||
v.Set(zeroInterfaceValue())
|
v.Set(zeroInterfaceValue())
|
||||||
v = value(f)
|
v = value(f)
|
||||||
}
|
}
|
||||||
return v.Interface().(valueInterface).value
|
return valueInterfaceValue(v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,74 +0,0 @@
|
|||||||
// Code generated by 'yaegi extract debug/pe'. DO NOT EDIT.
|
|
||||||
|
|
||||||
// +build go1.14,!go1.15
|
|
||||||
|
|
||||||
package stdlib
|
|
||||||
|
|
||||||
import (
|
|
||||||
"debug/pe"
|
|
||||||
"go/constant"
|
|
||||||
"go/token"
|
|
||||||
"reflect"
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
Symbols["debug/pe"] = map[string]reflect.Value{
|
|
||||||
// function, constant and variable definitions
|
|
||||||
"COFFSymbolSize": reflect.ValueOf(constant.MakeFromLiteral("18", token.INT, 0)),
|
|
||||||
"IMAGE_DIRECTORY_ENTRY_ARCHITECTURE": reflect.ValueOf(constant.MakeFromLiteral("7", token.INT, 0)),
|
|
||||||
"IMAGE_DIRECTORY_ENTRY_BASERELOC": reflect.ValueOf(constant.MakeFromLiteral("5", token.INT, 0)),
|
|
||||||
"IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT": reflect.ValueOf(constant.MakeFromLiteral("11", token.INT, 0)),
|
|
||||||
"IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR": reflect.ValueOf(constant.MakeFromLiteral("14", token.INT, 0)),
|
|
||||||
"IMAGE_DIRECTORY_ENTRY_DEBUG": reflect.ValueOf(constant.MakeFromLiteral("6", token.INT, 0)),
|
|
||||||
"IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT": reflect.ValueOf(constant.MakeFromLiteral("13", token.INT, 0)),
|
|
||||||
"IMAGE_DIRECTORY_ENTRY_EXCEPTION": reflect.ValueOf(constant.MakeFromLiteral("3", token.INT, 0)),
|
|
||||||
"IMAGE_DIRECTORY_ENTRY_EXPORT": reflect.ValueOf(constant.MakeFromLiteral("0", token.INT, 0)),
|
|
||||||
"IMAGE_DIRECTORY_ENTRY_GLOBALPTR": reflect.ValueOf(constant.MakeFromLiteral("8", token.INT, 0)),
|
|
||||||
"IMAGE_DIRECTORY_ENTRY_IAT": reflect.ValueOf(constant.MakeFromLiteral("12", token.INT, 0)),
|
|
||||||
"IMAGE_DIRECTORY_ENTRY_IMPORT": reflect.ValueOf(constant.MakeFromLiteral("1", token.INT, 0)),
|
|
||||||
"IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG": reflect.ValueOf(constant.MakeFromLiteral("10", token.INT, 0)),
|
|
||||||
"IMAGE_DIRECTORY_ENTRY_RESOURCE": reflect.ValueOf(constant.MakeFromLiteral("2", token.INT, 0)),
|
|
||||||
"IMAGE_DIRECTORY_ENTRY_SECURITY": reflect.ValueOf(constant.MakeFromLiteral("4", token.INT, 0)),
|
|
||||||
"IMAGE_DIRECTORY_ENTRY_TLS": reflect.ValueOf(constant.MakeFromLiteral("9", token.INT, 0)),
|
|
||||||
"IMAGE_FILE_MACHINE_AM33": reflect.ValueOf(constant.MakeFromLiteral("467", token.INT, 0)),
|
|
||||||
"IMAGE_FILE_MACHINE_AMD64": reflect.ValueOf(constant.MakeFromLiteral("34404", token.INT, 0)),
|
|
||||||
"IMAGE_FILE_MACHINE_ARM": reflect.ValueOf(constant.MakeFromLiteral("448", token.INT, 0)),
|
|
||||||
"IMAGE_FILE_MACHINE_ARM64": reflect.ValueOf(constant.MakeFromLiteral("43620", token.INT, 0)),
|
|
||||||
"IMAGE_FILE_MACHINE_ARMNT": reflect.ValueOf(constant.MakeFromLiteral("452", token.INT, 0)),
|
|
||||||
"IMAGE_FILE_MACHINE_EBC": reflect.ValueOf(constant.MakeFromLiteral("3772", token.INT, 0)),
|
|
||||||
"IMAGE_FILE_MACHINE_I386": reflect.ValueOf(constant.MakeFromLiteral("332", token.INT, 0)),
|
|
||||||
"IMAGE_FILE_MACHINE_IA64": reflect.ValueOf(constant.MakeFromLiteral("512", token.INT, 0)),
|
|
||||||
"IMAGE_FILE_MACHINE_M32R": reflect.ValueOf(constant.MakeFromLiteral("36929", token.INT, 0)),
|
|
||||||
"IMAGE_FILE_MACHINE_MIPS16": reflect.ValueOf(constant.MakeFromLiteral("614", token.INT, 0)),
|
|
||||||
"IMAGE_FILE_MACHINE_MIPSFPU": reflect.ValueOf(constant.MakeFromLiteral("870", token.INT, 0)),
|
|
||||||
"IMAGE_FILE_MACHINE_MIPSFPU16": reflect.ValueOf(constant.MakeFromLiteral("1126", token.INT, 0)),
|
|
||||||
"IMAGE_FILE_MACHINE_POWERPC": reflect.ValueOf(constant.MakeFromLiteral("496", token.INT, 0)),
|
|
||||||
"IMAGE_FILE_MACHINE_POWERPCFP": reflect.ValueOf(constant.MakeFromLiteral("497", token.INT, 0)),
|
|
||||||
"IMAGE_FILE_MACHINE_R4000": reflect.ValueOf(constant.MakeFromLiteral("358", token.INT, 0)),
|
|
||||||
"IMAGE_FILE_MACHINE_SH3": reflect.ValueOf(constant.MakeFromLiteral("418", token.INT, 0)),
|
|
||||||
"IMAGE_FILE_MACHINE_SH3DSP": reflect.ValueOf(constant.MakeFromLiteral("419", token.INT, 0)),
|
|
||||||
"IMAGE_FILE_MACHINE_SH4": reflect.ValueOf(constant.MakeFromLiteral("422", token.INT, 0)),
|
|
||||||
"IMAGE_FILE_MACHINE_SH5": reflect.ValueOf(constant.MakeFromLiteral("424", token.INT, 0)),
|
|
||||||
"IMAGE_FILE_MACHINE_THUMB": reflect.ValueOf(constant.MakeFromLiteral("450", token.INT, 0)),
|
|
||||||
"IMAGE_FILE_MACHINE_UNKNOWN": reflect.ValueOf(constant.MakeFromLiteral("0", token.INT, 0)),
|
|
||||||
"IMAGE_FILE_MACHINE_WCEMIPSV2": reflect.ValueOf(constant.MakeFromLiteral("361", token.INT, 0)),
|
|
||||||
"NewFile": reflect.ValueOf(pe.NewFile),
|
|
||||||
"Open": reflect.ValueOf(pe.Open),
|
|
||||||
|
|
||||||
// type definitions
|
|
||||||
"COFFSymbol": reflect.ValueOf((*pe.COFFSymbol)(nil)),
|
|
||||||
"DataDirectory": reflect.ValueOf((*pe.DataDirectory)(nil)),
|
|
||||||
"File": reflect.ValueOf((*pe.File)(nil)),
|
|
||||||
"FileHeader": reflect.ValueOf((*pe.FileHeader)(nil)),
|
|
||||||
"FormatError": reflect.ValueOf((*pe.FormatError)(nil)),
|
|
||||||
"ImportDirectory": reflect.ValueOf((*pe.ImportDirectory)(nil)),
|
|
||||||
"OptionalHeader32": reflect.ValueOf((*pe.OptionalHeader32)(nil)),
|
|
||||||
"OptionalHeader64": reflect.ValueOf((*pe.OptionalHeader64)(nil)),
|
|
||||||
"Reloc": reflect.ValueOf((*pe.Reloc)(nil)),
|
|
||||||
"Section": reflect.ValueOf((*pe.Section)(nil)),
|
|
||||||
"SectionHeader": reflect.ValueOf((*pe.SectionHeader)(nil)),
|
|
||||||
"SectionHeader32": reflect.ValueOf((*pe.SectionHeader32)(nil)),
|
|
||||||
"StringTable": reflect.ValueOf((*pe.StringTable)(nil)),
|
|
||||||
"Symbol": reflect.ValueOf((*pe.Symbol)(nil)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user