Compare commits
80 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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 | ||
|
|
d494f9e420 | ||
|
|
6da1107c39 | ||
|
|
38a7331bf9 | ||
|
|
13783889cb | ||
|
|
ed626f3fb9 | ||
|
|
d0a34d467b | ||
|
|
83676577ac | ||
|
|
f0fc907269 | ||
|
|
61f4704925 | ||
|
|
b1ccfbf47f | ||
|
|
0ed4b362dc | ||
|
|
98807387a4 | ||
|
|
c817823ba1 | ||
|
|
3cb8bca81a | ||
|
|
a38d19288f | ||
|
|
7f8ffa6719 | ||
|
|
513f5e37aa | ||
|
|
9520a92241 | ||
|
|
d47821bfaa | ||
|
|
d7ede8ed5c | ||
|
|
22c63b225c | ||
|
|
c0eaab0891 | ||
|
|
c74d050c5a | ||
|
|
804664c631 | ||
|
|
f6d8261a8a | ||
|
|
68c02ce054 | ||
|
|
4b3e9ee231 | ||
|
|
8916618a81 | ||
|
|
57b49f40d6 | ||
|
|
190dade469 | ||
|
|
6b652ea485 | ||
|
|
473bc63588 | ||
|
|
e32da38ad0 | ||
|
|
b2b519c2fd | ||
|
|
9491e58920 | ||
|
|
f362237ac5 | ||
|
|
a83ec1f925 | ||
|
|
155ca4e6ad | ||
|
|
ca196a5768 | ||
|
|
b78d55c66b | ||
|
|
16f5586a11 | ||
|
|
c3cf301c60 | ||
|
|
0dde990d0b | ||
|
|
4cfeb1946e | ||
|
|
ec64b006cf | ||
|
|
f36d4e01eb | ||
|
|
5dfc3b86dc | ||
|
|
3ae01a2af3 | ||
|
|
c06f83f34a | ||
|
|
a6d9c84a30 |
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.14, 1.15 ]
|
||||||
|
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.15
|
||||||
|
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.14, 1.15 ]
|
||||||
|
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.14, 1.15 ]
|
||||||
|
|
||||||
|
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.15
|
||||||
|
|
||||||
|
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 }}
|
||||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -3,7 +3,7 @@
|
|||||||
*.dot
|
*.dot
|
||||||
.idea/
|
.idea/
|
||||||
/yaegi
|
/yaegi
|
||||||
cmd/goexports/goexports
|
internal/cmd/extract/extract
|
||||||
example/inception/inception
|
example/inception/inception
|
||||||
_test/tmp/
|
_test/tmp/
|
||||||
/dist
|
/dist
|
||||||
|
|||||||
@@ -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"]
|
||||||
|
|||||||
@@ -26,31 +26,6 @@ builds:
|
|||||||
- goos: darwin
|
- goos: darwin
|
||||||
goarch: 386
|
goarch: 386
|
||||||
|
|
||||||
- id: goexports
|
|
||||||
binary: goexports
|
|
||||||
main: ./cmd/goexports/
|
|
||||||
|
|
||||||
goos:
|
|
||||||
- darwin
|
|
||||||
- linux
|
|
||||||
# - windows
|
|
||||||
- freebsd
|
|
||||||
- openbsd
|
|
||||||
- solaris
|
|
||||||
goarch:
|
|
||||||
- amd64
|
|
||||||
- 386
|
|
||||||
- arm
|
|
||||||
- arm64
|
|
||||||
goarm:
|
|
||||||
- 7
|
|
||||||
- 6
|
|
||||||
- 5
|
|
||||||
|
|
||||||
ignore:
|
|
||||||
- goos: darwin
|
|
||||||
goarch: 386
|
|
||||||
|
|
||||||
changelog:
|
changelog:
|
||||||
sort: asc
|
sort: asc
|
||||||
filters:
|
filters:
|
||||||
@@ -70,3 +45,19 @@ archives:
|
|||||||
format: zip
|
format: zip
|
||||||
files:
|
files:
|
||||||
- LICENSE
|
- LICENSE
|
||||||
|
|
||||||
|
brews:
|
||||||
|
- tap:
|
||||||
|
owner: traefik
|
||||||
|
name: homebrew-tap
|
||||||
|
commit_author:
|
||||||
|
name: traefiker
|
||||||
|
email: 30906710+traefiker@users.noreply.github.com
|
||||||
|
folder: Formula
|
||||||
|
homepage: https://github.com/traefik/yaegi
|
||||||
|
description: |
|
||||||
|
Yaegi is Another Elegant Go Interpreter.
|
||||||
|
It powers executable Go scripts and plugins, in embedded interpreters
|
||||||
|
or interactive shells, on top of the Go runtime.
|
||||||
|
test: |
|
||||||
|
system "#{bin}/yaegi version"
|
||||||
|
|||||||
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
|
|
||||||
27
Makefile
27
Makefile
@@ -3,21 +3,28 @@ check:
|
|||||||
golangci-lint run
|
golangci-lint run
|
||||||
|
|
||||||
# Generate stdlib/syscall/syscall_GOOS_GOARCH.go for all platforms
|
# Generate stdlib/syscall/syscall_GOOS_GOARCH.go for all platforms
|
||||||
gen_all_syscall: cmd/goexports/goexports
|
gen_all_syscall: internal/cmd/extract/extract
|
||||||
@cd stdlib/syscall && \
|
@for v in $$(go tool dist list); do \
|
||||||
for v in $$(go tool dist list); do \
|
|
||||||
echo syscall_$${v%/*}_$${v#*/}.go; \
|
echo syscall_$${v%/*}_$${v#*/}.go; \
|
||||||
GOOS=$${v%/*} GOARCH=$${v#*/} go generate; \
|
GOOS=$${v%/*} GOARCH=$${v#*/} go generate ./stdlib/syscall ./stdlib/unrestricted; \
|
||||||
done
|
done
|
||||||
|
|
||||||
cmd/goexports/goexports: cmd/goexports/goexports.go
|
internal/cmd/extract/extract:
|
||||||
go generate cmd/goexports/goexports.go
|
rm -f internal/cmd/extract/extract
|
||||||
|
go generate ./internal/cmd/extract
|
||||||
|
|
||||||
generate: gen_all_syscall
|
generate: gen_all_syscall
|
||||||
go generate
|
go generate
|
||||||
|
|
||||||
tests:
|
install:
|
||||||
GO111MODULE=off go test -v ./...
|
GOFLAGS=-ldflags=-X=main.version=$$(git describe --tags) go install ./...
|
||||||
GO111MODULE=off go test -race ./interp
|
|
||||||
|
|
||||||
.PHONY: check gen_all_syscall gen_tests
|
tests:
|
||||||
|
go test -v ./...
|
||||||
|
go test -race ./interp
|
||||||
|
|
||||||
|
# https://github.com/goreleaser/godownloader
|
||||||
|
install.sh: .goreleaser.yml
|
||||||
|
godownloader --repo=traefik/yaegi -o install.sh .goreleaser.yml
|
||||||
|
|
||||||
|
.PHONY: check gen_all_syscall gen_tests generate_downloader internal/cmd/extract/extract install
|
||||||
|
|||||||
24
README.md
24
README.md
@@ -37,6 +37,12 @@ go get -u github.com/traefik/yaegi/cmd/yaegi
|
|||||||
Note that you can use [rlwrap](https://github.com/hanslub42/rlwrap) (install with your favorite package manager),
|
Note that you can use [rlwrap](https://github.com/hanslub42/rlwrap) (install with your favorite package manager),
|
||||||
and alias the `yaegi` command in `alias yaegi='rlwrap yaegi'` in your `~/.bashrc`, to have history and command line edition.
|
and alias the `yaegi` command in `alias yaegi='rlwrap yaegi'` in your `~/.bashrc`, to have history and command line edition.
|
||||||
|
|
||||||
|
### CI Integration
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -sfL https://raw.githubusercontent.com/traefik/yaegi/master/install.sh | bash -s -- -b $GOPATH/bin v0.9.0
|
||||||
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
### As an embedded interpreter
|
### As an embedded interpreter
|
||||||
@@ -68,7 +74,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
[Go Playground](https://play.golang.org/p/zzvw4VlerLP)
|
[Go Playground](https://play.golang.org/p/2n-EpZbMYI9)
|
||||||
|
|
||||||
### As a dynamic extension framework
|
### As a dynamic extension framework
|
||||||
|
|
||||||
@@ -106,7 +112,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
[Go Playground](https://play.golang.org/p/6SEAoaO7n0U)
|
[Go Playground](https://play.golang.org/p/WvwH4JqrU-p)
|
||||||
|
|
||||||
### As a command-line interpreter
|
### As a command-line interpreter
|
||||||
|
|
||||||
@@ -122,10 +128,20 @@ Hello World
|
|||||||
>
|
>
|
||||||
```
|
```
|
||||||
|
|
||||||
Or interpret Go files:
|
Note that in interactive mode, all stdlib package are pre-imported,
|
||||||
|
you can use them directly:
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$ yaegi cmd/yaegi/yaegi.go
|
$ yaegi
|
||||||
|
> reflect.TypeOf(time.Date)
|
||||||
|
: func(int, time.Month, int, int, int, int, int, *time.Location) time.Time
|
||||||
|
>
|
||||||
|
```
|
||||||
|
|
||||||
|
Or interpret Go packages, directories or files, including itself:
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ yaegi -syscall -unsafe -unrestricted github.com/traefik/yaegi/cmd/yaegi
|
||||||
>
|
>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
17
_test/a43.go
Normal file
17
_test/a43.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type T [l1 + l2]int
|
||||||
|
|
||||||
|
const (
|
||||||
|
l1 = 2
|
||||||
|
l2 = 3
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(T{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [0 0 0 0 0]
|
||||||
12
_test/a44.go
Normal file
12
_test/a44.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
var a = [max]int{}
|
||||||
|
|
||||||
|
const max = 32
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
println(len(a))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 32
|
||||||
@@ -1,10 +1,33 @@
|
|||||||
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))
|
||||||
|
|
||||||
|
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))
|
println(a.(int))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 7
|
// 7
|
||||||
|
// 8
|
||||||
|
// 9
|
||||||
|
// 10
|
||||||
|
// 11
|
||||||
|
// 12
|
||||||
|
|||||||
22
_test/addr1.go
Normal file
22
_test/addr1.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type T struct {
|
||||||
|
A int
|
||||||
|
B int
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
a := &[]T{
|
||||||
|
{1, 2},
|
||||||
|
{3, 4},
|
||||||
|
}
|
||||||
|
fmt.Println("a:", a)
|
||||||
|
x := &(*a)[1:][0]
|
||||||
|
fmt.Println("x:", x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// a: &[{1 2} {3 4}]
|
||||||
|
// x: &{3 4}
|
||||||
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ouput:
|
||||||
|
// <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()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ouput:
|
||||||
|
// 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"]
|
||||||
15
_test/append1.go
Normal file
15
_test/append1.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
s := bufio.NewScanner(bytes.NewReader([]byte("Hello\nTest\nLine3")))
|
||||||
|
s.Scan()
|
||||||
|
println(string(append(s.Bytes(), []byte(" World")...)))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// Hello World
|
||||||
15
_test/append2.go
Normal file
15
_test/append2.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
s := bufio.NewScanner(bytes.NewReader([]byte("Hello\nTest\nLine3")))
|
||||||
|
s.Scan()
|
||||||
|
println(string(append(s.Bytes(), " World"...)))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// Hello World
|
||||||
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
|
||||||
15
_test/assign16.go
Normal file
15
_test/assign16.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type H struct {
|
||||||
|
bits uint
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
h := &H{8}
|
||||||
|
var x uint = (1 << h.bits) >> 6
|
||||||
|
|
||||||
|
println(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 4
|
||||||
17
_test/binstruct_ptr_map0.go
Normal file
17
_test/binstruct_ptr_map0.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"image"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
v := map[string]*image.Point{
|
||||||
|
"foo": {X: 3, Y: 2},
|
||||||
|
"bar": {X: 4, Y: 5},
|
||||||
|
}
|
||||||
|
fmt.Println(v["foo"], v["bar"])
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// (3,2) (4,5)
|
||||||
17
_test/binstruct_ptr_slice0.go
Normal file
17
_test/binstruct_ptr_slice0.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"image"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
v := []*image.Point{
|
||||||
|
{X: 3, Y: 2},
|
||||||
|
{X: 4, Y: 5},
|
||||||
|
}
|
||||||
|
fmt.Println(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [(3,2) (4,5)]
|
||||||
16
_test/binstruct_slice0.go
Normal file
16
_test/binstruct_slice0.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"image"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
v := []image.Point{
|
||||||
|
{X: 3, Y: 2},
|
||||||
|
}
|
||||||
|
fmt.Println(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [(3,2)]
|
||||||
@@ -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
|
||||||
|
|||||||
19
_test/composite12.go
Normal file
19
_test/composite12.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type A struct {
|
||||||
|
C D
|
||||||
|
}
|
||||||
|
|
||||||
|
type D struct {
|
||||||
|
E string
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
a := A{}
|
||||||
|
a.C = D{"bb"}
|
||||||
|
|
||||||
|
println(a.C.E)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// bb
|
||||||
19
_test/composite13.go
Normal file
19
_test/composite13.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type A struct {
|
||||||
|
C D
|
||||||
|
}
|
||||||
|
|
||||||
|
type D struct {
|
||||||
|
E string
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
a := A{}
|
||||||
|
a.C = D{E: "bb"}
|
||||||
|
|
||||||
|
println(a.C.E)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// bb
|
||||||
15
_test/composite14.go
Normal file
15
_test/composite14.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type T struct {
|
||||||
|
b []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
t := T{nil}
|
||||||
|
fmt.Println(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// {[]}
|
||||||
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
|
||||||
16
_test/composite8bis.go
Normal file
16
_test/composite8bis.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type T struct{ I int }
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
t := []*T{}
|
||||||
|
s := []int{1, 2}
|
||||||
|
for _, e := range s {
|
||||||
|
x := &T{I: e}
|
||||||
|
t = append(t, x)
|
||||||
|
}
|
||||||
|
println(t[0].I, t[1].I)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1 2
|
||||||
12
_test/const16.go
Normal file
12
_test/const16.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(7 / 3)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2
|
||||||
14
_test/const17.go
Normal file
14
_test/const17.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
var t [7/3]int
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
t[0] = 3/2
|
||||||
|
t[1] = 5/2
|
||||||
|
fmt.Println(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [1 2]
|
||||||
11
_test/const18.go
Normal file
11
_test/const18.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
a := int64(time.Second)
|
||||||
|
println(a)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1000000000
|
||||||
17
_test/const19.go
Normal file
17
_test/const19.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func get10Hours() time.Duration {
|
||||||
|
return 10 * time.Hour
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(get10Hours().String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 10h0m0s
|
||||||
12
_test/const20.go
Normal file
12
_test/const20.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
const maxLen = int64(int(^uint(0) >> 1))
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(maxLen)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 9223372036854775807
|
||||||
12
_test/const21.go
Normal file
12
_test/const21.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
const a = 64
|
||||||
|
|
||||||
|
var b uint = a * a / 2
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
println(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2048
|
||||||
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
|
||||||
21
_test/convert0.go
Normal file
21
_test/convert0.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type T struct {
|
||||||
|
v int
|
||||||
|
}
|
||||||
|
|
||||||
|
type comparator func(T, T) bool
|
||||||
|
|
||||||
|
func sort(items []T, comp comparator) {
|
||||||
|
println("in sort")
|
||||||
|
}
|
||||||
|
|
||||||
|
func compT(t0, t1 T) bool { return t0.v < t1.v }
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
a := []T{}
|
||||||
|
sort(a, comparator(compT))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// in sort
|
||||||
17
_test/convert1.go
Normal file
17
_test/convert1.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "strconv"
|
||||||
|
|
||||||
|
type atoidef func(s string) (int, error)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
stdatoi := atoidef(strconv.Atoi)
|
||||||
|
n, err := stdatoi("7")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
println(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 7
|
||||||
19
_test/convert2.go
Normal file
19
_test/convert2.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "bufio"
|
||||||
|
|
||||||
|
func fakeSplitFunc(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
||||||
|
return 7, nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
splitfunc := bufio.SplitFunc(fakeSplitFunc)
|
||||||
|
n, _, err := splitfunc(nil, true)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
println(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 7
|
||||||
16
_test/for15.go
Normal file
16
_test/for15.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func f() int { println("in f"); return 1 }
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
for i := f(); ; {
|
||||||
|
println("in loop")
|
||||||
|
if i > 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// in f
|
||||||
|
// in loop
|
||||||
16
_test/for16.go
Normal file
16
_test/for16.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
max := 1
|
||||||
|
for ; ; max-- {
|
||||||
|
if max == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
println("in for")
|
||||||
|
}
|
||||||
|
println("bye")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// in for
|
||||||
|
// bye
|
||||||
@@ -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)
|
||||||
|
|||||||
10
_test/fun23.go
Normal file
10
_test/fun23.go
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func f(x int) { return x }
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
print("hello")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error:
|
||||||
|
// 3:17: too many arguments to return
|
||||||
10
_test/fun24.go
Normal file
10
_test/fun24.go
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func f(x int) (int, int) { return x, "foo" }
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
print("hello")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error:
|
||||||
|
// cannot use "foo" (type stringT) as type intT in return argument
|
||||||
10
_test/fun25.go
Normal file
10
_test/fun25.go
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func f(x string) (a int, b int) { return x, 5 }
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
print("hello")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error:
|
||||||
|
// cannot use x (type stringT) as type intT in return argument
|
||||||
17
_test/fun26.go
Normal file
17
_test/fun26.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type F func() (int, error)
|
||||||
|
|
||||||
|
func f1() (int, error) { return 3, nil }
|
||||||
|
|
||||||
|
func f2(a string, f F) {
|
||||||
|
c, _ := f()
|
||||||
|
println(a, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
f2("hello", F(f1))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello 3
|
||||||
12
_test/goto1.go
Normal file
12
_test/goto1.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if true {
|
||||||
|
goto here
|
||||||
|
}
|
||||||
|
here:
|
||||||
|
println("ok")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// ok
|
||||||
32
_test/interface47.go
Normal file
32
_test/interface47.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type Doer interface {
|
||||||
|
Do() error
|
||||||
|
}
|
||||||
|
|
||||||
|
type T struct {
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *T) Do() error { println("in do"); return nil }
|
||||||
|
|
||||||
|
func f() (Doer, error) { return &T{"truc"}, nil }
|
||||||
|
|
||||||
|
type Ev struct {
|
||||||
|
doer func() (Doer, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Ev) do() {
|
||||||
|
d, _ := e.doer()
|
||||||
|
d.Do()
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
e := &Ev{f}
|
||||||
|
println(e != nil)
|
||||||
|
e.do()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// in do
|
||||||
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
|
||||||
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-880.go
Normal file
23
_test/issue-880.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var buf1 = make([]byte, 1024)
|
||||||
|
var buf2 []byte
|
||||||
|
buf1 = []byte("Hallo\nTest\nLine3")
|
||||||
|
|
||||||
|
s := bufio.NewScanner(bytes.NewReader(buf1))
|
||||||
|
for s.Scan() {
|
||||||
|
buf2 = append(buf2, append(s.Bytes(), []byte("\n")...)...)
|
||||||
|
}
|
||||||
|
print(string(buf2))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// Hallo
|
||||||
|
// Test
|
||||||
|
// Line3
|
||||||
15
_test/issue-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
|
||||||
7
_test/m2/m2_test.go
Normal file
7
_test/m2/m2_test.go
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package m2
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestM2(t *testing.T) {
|
||||||
|
t.Errorf("got %s, want %s", "AAA", "BBB")
|
||||||
|
}
|
||||||
@@ -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())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
28
_test/nil3.go
Normal file
28
_test/nil3.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type I interface {
|
||||||
|
Hello()
|
||||||
|
}
|
||||||
|
|
||||||
|
type T struct {
|
||||||
|
h I
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *T) Hello() { println("Hello") }
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
t := &T{}
|
||||||
|
println(t.h != nil)
|
||||||
|
println(t.h == nil)
|
||||||
|
t.h = t
|
||||||
|
println(t.h != nil)
|
||||||
|
println(t.h == nil)
|
||||||
|
t.h.Hello()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// false
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// Hello
|
||||||
@@ -1,18 +1,20 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
import "fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
c1 := make(chan string)
|
c1 := make(chan string)
|
||||||
c2 := make(chan string)
|
c2 := make(chan string)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
time.Sleep(1e9)
|
time.Sleep(1e7)
|
||||||
c1 <- "one"
|
c1 <- "one"
|
||||||
}()
|
}()
|
||||||
go func() {
|
go func() {
|
||||||
time.Sleep(2e9)
|
time.Sleep(2e7)
|
||||||
c2 <- "two"
|
c2 <- "two"
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|||||||
44
_test/select14.go
Normal file
44
_test/select14.go
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
period = 100 * time.Millisecond
|
||||||
|
precision = 5 * time.Millisecond
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
counter := 0
|
||||||
|
p := time.Now()
|
||||||
|
ticker := time.NewTicker(period)
|
||||||
|
ch := make(chan int)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
select {
|
||||||
|
case t := <-ticker.C:
|
||||||
|
counter = counter + 1
|
||||||
|
ch <- counter
|
||||||
|
if d := t.Sub(p) - period; d < -precision || d > precision {
|
||||||
|
fmt.Println("wrong delay", d)
|
||||||
|
}
|
||||||
|
p = t
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ch <- 0
|
||||||
|
}()
|
||||||
|
for c := range ch {
|
||||||
|
if c == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
println(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
|
// 2
|
||||||
|
// 3
|
||||||
23
_test/select15.go
Normal file
23
_test/select15.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type T struct {
|
||||||
|
c1 chan string
|
||||||
|
c2 chan string
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
t := &T{}
|
||||||
|
t.c2 = make(chan string)
|
||||||
|
|
||||||
|
go func(c chan string) { c <- "done" }(t.c2)
|
||||||
|
|
||||||
|
select {
|
||||||
|
case msg := <-t.c1:
|
||||||
|
println("received from c1:", msg)
|
||||||
|
case <-t.c2:
|
||||||
|
}
|
||||||
|
println("Bye")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// Bye
|
||||||
11
_test/slice.go
Normal file
11
_test/slice.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
a := [2][2]int{{0, 1}, {2, 3}}
|
||||||
|
fmt.Println(a[0][0:])
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [0 1]
|
||||||
24
_test/struct58.go
Normal file
24
_test/struct58.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
|
type A struct {
|
||||||
|
Test string `tag:"test"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
a := A{}
|
||||||
|
t := reflect.TypeOf(a)
|
||||||
|
f, ok := t.FieldByName("Test")
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(f.Tag.Get("tag"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// test
|
||||||
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
|
||||||
|
|||||||
20
_test/time14.go
Normal file
20
_test/time14.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var t time.Time
|
||||||
|
|
||||||
|
func f() time.Time {
|
||||||
|
time := t
|
||||||
|
return time
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(f())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 0001-01-01 00:00:00 +0000 UTC
|
||||||
15
_test/time15.go
Normal file
15
_test/time15.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type TimeValue time.Time
|
||||||
|
|
||||||
|
func (v *TimeValue) decode() { println("in decode") }
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var tv TimeValue
|
||||||
|
tv.decode()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// in decode
|
||||||
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
|
||||||
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
|
||||||
16
_test/variadic10.go
Normal file
16
_test/variadic10.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
logger := log.New(os.Stdout, "test ", log.Lmsgprefix)
|
||||||
|
logger.Printf("args: %v %v", 1, "truc")
|
||||||
|
logger.Printf("args: %v %v %v", 1, "truc", 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// test args: 1 truc
|
||||||
|
// test args: 1 truc 2
|
||||||
@@ -1,137 +0,0 @@
|
|||||||
//go:generate go build
|
|
||||||
|
|
||||||
/*
|
|
||||||
Goexports generates wrappers of package exported symbols.
|
|
||||||
|
|
||||||
Output files are written in the current directory, and prefixed with the go version.
|
|
||||||
|
|
||||||
Usage:
|
|
||||||
|
|
||||||
goexports package...
|
|
||||||
|
|
||||||
Example:
|
|
||||||
|
|
||||||
goexports github.com/traefik/yaegi/interp
|
|
||||||
|
|
||||||
The same goexport program is used for all target operating systems and architectures.
|
|
||||||
The GOOS and GOARCH environment variables set the desired target.
|
|
||||||
*/
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"bytes"
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"path"
|
|
||||||
"runtime"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/traefik/yaegi/extract"
|
|
||||||
)
|
|
||||||
|
|
||||||
// genLicense generates the correct LICENSE header text from the provided
|
|
||||||
// path to a LICENSE file.
|
|
||||||
func genLicense(fname string) (string, error) {
|
|
||||||
if fname == "" {
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
f, err := os.Open(fname)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("could not open LICENSE file: %v", err)
|
|
||||||
}
|
|
||||||
defer func() { _ = f.Close() }()
|
|
||||||
|
|
||||||
license := new(strings.Builder)
|
|
||||||
sc := bufio.NewScanner(f)
|
|
||||||
for sc.Scan() {
|
|
||||||
txt := sc.Text()
|
|
||||||
if txt != "" {
|
|
||||||
txt = " " + txt
|
|
||||||
}
|
|
||||||
license.WriteString("//" + txt + "\n")
|
|
||||||
}
|
|
||||||
if sc.Err() != nil {
|
|
||||||
return "", fmt.Errorf("could not scan LICENSE file: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return license.String(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
licenseFlag = flag.String("license", "", "path to a LICENSE file")
|
|
||||||
// TODO: deal with a module that has several packages (so there's only one go.mod file at the root of the project).
|
|
||||||
importPathFlag = flag.String("import_path", "", "the namespace for the symbols extracted from the argument. Not needed if the argument is from the stdlib, or if the name can be found in a go.mod")
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
if flag.NArg() == 0 {
|
|
||||||
flag.Usage()
|
|
||||||
log.Fatalf("missing package path")
|
|
||||||
}
|
|
||||||
|
|
||||||
license, err := genLicense(*licenseFlag)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
wd, err := os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
ext := extract.Extractor{
|
|
||||||
Dest: path.Base(wd),
|
|
||||||
License: license,
|
|
||||||
}
|
|
||||||
goos, goarch := os.Getenv("GOOS"), os.Getenv("GOARCH")
|
|
||||||
skip := map[string]bool{}
|
|
||||||
if goos == "solaris" {
|
|
||||||
skip["syscall.RawSyscall6"] = true
|
|
||||||
skip["syscall.Syscall6"] = true
|
|
||||||
}
|
|
||||||
ext.Skip = skip
|
|
||||||
|
|
||||||
for _, pkgIdent := range flag.Args() {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
importPath, err := ext.Extract(pkgIdent, *importPathFlag, &buf)
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
var oFile string
|
|
||||||
if pkgIdent == "syscall" {
|
|
||||||
oFile = strings.ReplaceAll(importPath, "/", "_") + "_" + goos + "_" + goarch + ".go"
|
|
||||||
} else {
|
|
||||||
oFile = strings.ReplaceAll(importPath, "/", "_") + ".go"
|
|
||||||
}
|
|
||||||
|
|
||||||
prefix := runtime.Version()
|
|
||||||
if runtime.Version() != "devel" {
|
|
||||||
parts := strings.Split(runtime.Version(), ".")
|
|
||||||
|
|
||||||
prefix = parts[0] + "_" + extract.GetMinor(parts[1])
|
|
||||||
}
|
|
||||||
|
|
||||||
f, err := os.Create(prefix + "_" + oFile)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := io.Copy(f, &buf); err != nil {
|
|
||||||
_ = f.Close()
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := f.Close(); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -15,11 +15,15 @@ import (
|
|||||||
|
|
||||||
func extractCmd(arg []string) error {
|
func extractCmd(arg []string) error {
|
||||||
var licensePath string
|
var licensePath string
|
||||||
var importPath string
|
var name string
|
||||||
|
var exclude string
|
||||||
|
var include string
|
||||||
|
|
||||||
eflag := flag.NewFlagSet("run", flag.ContinueOnError)
|
eflag := flag.NewFlagSet("run", flag.ContinueOnError)
|
||||||
eflag.StringVar(&licensePath, "license", "", "path to a LICENSE file")
|
eflag.StringVar(&licensePath, "license", "", "path to a LICENSE file")
|
||||||
eflag.StringVar(&importPath, "import_path", "", "the namespace for the extracted symbols")
|
eflag.StringVar(&name, "name", "", "the namespace for the extracted symbols")
|
||||||
|
eflag.StringVar(&exclude, "exclude", "", "comma separated list of regexp matching symbols to exclude")
|
||||||
|
eflag.StringVar(&include, "include", "", "comma separated list of regexp matching symbols to include")
|
||||||
eflag.Usage = func() {
|
eflag.Usage = func() {
|
||||||
fmt.Println("Usage: yaegi extract [options] packages...")
|
fmt.Println("Usage: yaegi extract [options] packages...")
|
||||||
fmt.Println("Options:")
|
fmt.Println("Options:")
|
||||||
@@ -45,20 +49,32 @@ func extractCmd(arg []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if name == "" {
|
||||||
|
name = path.Base(wd)
|
||||||
|
}
|
||||||
ext := extract.Extractor{
|
ext := extract.Extractor{
|
||||||
Dest: path.Base(wd),
|
Dest: name,
|
||||||
License: license,
|
License: license,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if exclude != "" {
|
||||||
|
ext.Exclude = strings.Split(exclude, ",")
|
||||||
|
}
|
||||||
|
if include != "" {
|
||||||
|
ext.Include = strings.Split(include, ",")
|
||||||
|
}
|
||||||
|
|
||||||
|
r := strings.NewReplacer("/", "-", ".", "_")
|
||||||
|
|
||||||
for _, pkgIdent := range args {
|
for _, pkgIdent := range args {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
importPath, err := ext.Extract(pkgIdent, importPath, &buf)
|
importPath, err := ext.Extract(pkgIdent, name, &buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
oFile := strings.ReplaceAll(importPath, "/", "_") + ".go"
|
oFile := r.Replace(importPath) + ".go"
|
||||||
f, err := os.Create(oFile)
|
f, err := os.Create(oFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -86,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() }()
|
||||||
|
|
||||||
@@ -100,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
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ The commands are:
|
|||||||
help print usage information
|
help print usage information
|
||||||
run execute a Go program from source
|
run execute a Go program from source
|
||||||
test execute test functions in a Go package
|
test execute test functions in a Go package
|
||||||
|
version print version
|
||||||
|
|
||||||
Use "yaegi help <command>" for more information about a command.
|
Use "yaegi help <command>" for more information about a command.
|
||||||
|
|
||||||
@@ -37,6 +38,9 @@ func help(arg []string) error {
|
|||||||
return run([]string{"-h"})
|
return run([]string{"-h"})
|
||||||
case Test:
|
case Test:
|
||||||
return test([]string{"-h"})
|
return test([]string{"-h"})
|
||||||
|
case Version:
|
||||||
|
fmt.Println("Usage: yaegi version")
|
||||||
|
return nil
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("help: invalid yaegi command: %v", cmd)
|
return fmt.Errorf("help: invalid yaegi command: %v", cmd)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"go/build"
|
"go/build"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/traefik/yaegi/interp"
|
"github.com/traefik/yaegi/interp"
|
||||||
@@ -17,19 +18,21 @@ import (
|
|||||||
|
|
||||||
func run(arg []string) error {
|
func run(arg []string) error {
|
||||||
var interactive bool
|
var interactive bool
|
||||||
var useSyscall bool
|
|
||||||
var useUnrestricted bool
|
|
||||||
var useUnsafe bool
|
|
||||||
var tags string
|
var tags string
|
||||||
var cmd string
|
var cmd string
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
// The following flags are initialized from environment.
|
||||||
|
useSyscall, _ := strconv.ParseBool(os.Getenv("YAEGI_SYSCALL"))
|
||||||
|
useUnrestricted, _ := strconv.ParseBool(os.Getenv("YAEGI_UNRESTRICTED"))
|
||||||
|
useUnsafe, _ := strconv.ParseBool(os.Getenv("YAEGI_UNSAFE"))
|
||||||
|
|
||||||
rflag := flag.NewFlagSet("run", flag.ContinueOnError)
|
rflag := flag.NewFlagSet("run", flag.ContinueOnError)
|
||||||
rflag.BoolVar(&interactive, "i", false, "start an interactive REPL")
|
rflag.BoolVar(&interactive, "i", false, "start an interactive REPL")
|
||||||
rflag.BoolVar(&useSyscall, "syscall", false, "include syscall symbols")
|
rflag.BoolVar(&useSyscall, "syscall", useSyscall, "include syscall symbols")
|
||||||
rflag.BoolVar(&useUnrestricted, "unrestricted", false, "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", false, "include usafe symbols")
|
rflag.BoolVar(&useUnsafe, "unsafe", useUnsafe, "include unsafe symbols")
|
||||||
rflag.StringVar(&cmd, "e", "", "set the command to be executed (instead of script or/and shell)")
|
rflag.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]")
|
||||||
@@ -46,31 +49,40 @@ func run(arg []string) error {
|
|||||||
i.Use(interp.Symbols)
|
i.Use(interp.Symbols)
|
||||||
if useSyscall {
|
if useSyscall {
|
||||||
i.Use(syscall.Symbols)
|
i.Use(syscall.Symbols)
|
||||||
|
// Using a environment var allows a nested interpreter to import the syscall package.
|
||||||
|
if err := os.Setenv("YAEGI_SYSCALL", "1"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if useUnsafe {
|
if useUnsafe {
|
||||||
i.Use(unsafe.Symbols)
|
i.Use(unsafe.Symbols)
|
||||||
|
if err := os.Setenv("YAEGI_UNSAFE", "1"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if useUnrestricted {
|
if useUnrestricted {
|
||||||
// Use of unrestricted symbols should always follow use of stdlib symbols, to update them.
|
// Use of unrestricted symbols should always follow stdlib and syscall symbols, to update them.
|
||||||
i.Use(unrestricted.Symbols)
|
i.Use(unrestricted.Symbols)
|
||||||
|
if err := os.Setenv("YAEGI_UNRESTRICTED", "1"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmd != "" {
|
if cmd != "" {
|
||||||
_, err = i.Eval(cmd)
|
_, err = i.Eval(cmd)
|
||||||
showError(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
if interactive || cmd == "" {
|
if interactive || cmd == "" {
|
||||||
_, err = i.REPL()
|
|
||||||
showError(err)
|
showError(err)
|
||||||
|
_, 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[1:]
|
os.Args = arg
|
||||||
flag.CommandLine = flag.NewFlagSet(path, flag.ExitOnError)
|
flag.CommandLine = flag.NewFlagSet(path, flag.ExitOnError)
|
||||||
|
|
||||||
if isFile(path) {
|
if isFile(path) {
|
||||||
@@ -78,7 +90,6 @@ func run(arg []string) error {
|
|||||||
} else {
|
} else {
|
||||||
_, err = i.EvalPath(path)
|
_, err = i.EvalPath(path)
|
||||||
}
|
}
|
||||||
showError(err)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -86,7 +97,6 @@ func run(arg []string) error {
|
|||||||
|
|
||||||
if interactive {
|
if interactive {
|
||||||
_, err = i.REPL()
|
_, err = i.REPL()
|
||||||
showError(err)
|
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/build"
|
"go/build"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -18,22 +21,24 @@ import (
|
|||||||
|
|
||||||
func test(arg []string) (err error) {
|
func test(arg []string) (err error) {
|
||||||
var (
|
var (
|
||||||
bench string
|
bench string
|
||||||
benchmem bool
|
benchmem bool
|
||||||
benchtime string
|
benchtime string
|
||||||
count string
|
count string
|
||||||
cpu string
|
cpu string
|
||||||
failfast bool
|
failfast bool
|
||||||
run string
|
run string
|
||||||
short bool
|
short bool
|
||||||
tags string
|
tags string
|
||||||
useUnrestricted bool
|
timeout string
|
||||||
useUnsafe bool
|
verbose bool
|
||||||
useSyscall bool
|
|
||||||
timeout string
|
|
||||||
verbose bool
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// The following flags are initialized from environment.
|
||||||
|
useSyscall, _ := strconv.ParseBool(os.Getenv("YAEGI_SYSCALL"))
|
||||||
|
useUnrestricted, _ := strconv.ParseBool(os.Getenv("YAEGI_UNRESTRICTED"))
|
||||||
|
useUnsafe, _ := strconv.ParseBool(os.Getenv("YAEGI_UNSAFE"))
|
||||||
|
|
||||||
tflag := flag.NewFlagSet("test", flag.ContinueOnError)
|
tflag := flag.NewFlagSet("test", flag.ContinueOnError)
|
||||||
tflag.StringVar(&bench, "bench", "", "Run only those benchmarks matching a regular expression.")
|
tflag.StringVar(&bench, "bench", "", "Run only those benchmarks matching a regular expression.")
|
||||||
tflag.BoolVar(&benchmem, "benchmem", false, "Print memory allocation statistics for benchmarks.")
|
tflag.BoolVar(&benchmem, "benchmem", false, "Print memory allocation statistics for benchmarks.")
|
||||||
@@ -45,9 +50,9 @@ func test(arg []string) (err error) {
|
|||||||
tflag.BoolVar(&short, "short", false, "Tell long-running tests to shorten their run time.")
|
tflag.BoolVar(&short, "short", false, "Tell long-running tests to shorten their run time.")
|
||||||
tflag.StringVar(&tags, "tags", "", "Set a list of build tags.")
|
tflag.StringVar(&tags, "tags", "", "Set a list of build tags.")
|
||||||
tflag.StringVar(&timeout, "timeout", "", "If a test binary runs longer than duration d, panic.")
|
tflag.StringVar(&timeout, "timeout", "", "If a test binary runs longer than duration d, panic.")
|
||||||
tflag.BoolVar(&useUnrestricted, "unrestricted", false, "Include unrestricted symbols.")
|
tflag.BoolVar(&useUnrestricted, "unrestricted", useUnrestricted, "Include unrestricted symbols.")
|
||||||
tflag.BoolVar(&useUnsafe, "unsafe", false, "Include usafe symbols.")
|
tflag.BoolVar(&useUnsafe, "unsafe", useUnsafe, "Include usafe symbols.")
|
||||||
tflag.BoolVar(&useSyscall, "syscall", false, "Include syscall symbols.")
|
tflag.BoolVar(&useSyscall, "syscall", useSyscall, "Include syscall symbols.")
|
||||||
tflag.BoolVar(&verbose, "v", false, "Verbose output: log all tests as they are run.")
|
tflag.BoolVar(&verbose, "v", false, "Verbose output: log all tests as they are run.")
|
||||||
tflag.Usage = func() {
|
tflag.Usage = func() {
|
||||||
fmt.Println("Usage: yaegi test [options] [path]")
|
fmt.Println("Usage: yaegi test [options] [path]")
|
||||||
@@ -98,18 +103,40 @@ 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)
|
i.Use(stdlib.Symbols)
|
||||||
i.Use(interp.Symbols)
|
i.Use(interp.Symbols)
|
||||||
if useSyscall {
|
if useSyscall {
|
||||||
i.Use(syscall.Symbols)
|
i.Use(syscall.Symbols)
|
||||||
|
// Using a environment var allows a nested interpreter to import the syscall package.
|
||||||
|
if err := os.Setenv("YAEGI_SYSCALL", "1"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if useUnrestricted {
|
if useUnrestricted {
|
||||||
i.Use(unrestricted.Symbols)
|
i.Use(unrestricted.Symbols)
|
||||||
|
if err := os.Setenv("YAEGI_UNRESTRICTED", "1"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if useUnsafe {
|
if useUnsafe {
|
||||||
i.Use(unsafe.Symbols)
|
i.Use(unsafe.Symbols)
|
||||||
|
if err := os.Setenv("YAEGI_UNSAFE", "1"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if err = i.EvalTest(path); err != nil {
|
if err = i.EvalTest(path); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -117,7 +144,11 @@ func test(arg []string) (err error) {
|
|||||||
|
|
||||||
benchmarks := []testing.InternalBenchmark{}
|
benchmarks := []testing.InternalBenchmark{}
|
||||||
tests := []testing.InternalTest{}
|
tests := []testing.InternalTest{}
|
||||||
for name, sym := range i.Symbols(path) {
|
syms, ok := i.Symbols(path)[path]
|
||||||
|
if !ok {
|
||||||
|
return errors.New("No tests found")
|
||||||
|
}
|
||||||
|
for name, sym := range syms {
|
||||||
switch fun := sym.Interface().(type) {
|
switch fun := sym.Interface().(type) {
|
||||||
case func(*testing.B):
|
case func(*testing.B):
|
||||||
benchmarks = append(benchmarks, testing.InternalBenchmark{name, fun})
|
benchmarks = append(benchmarks, testing.InternalBenchmark{name, fun})
|
||||||
|
|||||||
@@ -71,7 +71,16 @@ Options:
|
|||||||
-unsafe
|
-unsafe
|
||||||
include unsafe symbols.
|
include unsafe symbols.
|
||||||
|
|
||||||
Debugging support (may be removed at any time):
|
Environment variables:
|
||||||
|
YAEGI_SYSCALL=1
|
||||||
|
Include syscall symbols (same as -syscall flag).
|
||||||
|
YAEGI_UNRESTRICTED=1
|
||||||
|
Include unrestricted symbols (same as -unrestricted flag).
|
||||||
|
YAEGI_UNSAFE=1
|
||||||
|
Include unsafe symbols (same as -unsafe flag).
|
||||||
|
YAEGI_PROMPT=1
|
||||||
|
Force enable the printing of the REPL prompt and the result of last instruction,
|
||||||
|
even if stdin is not a terminal.
|
||||||
YAEGI_AST_DOT=1
|
YAEGI_AST_DOT=1
|
||||||
Generate and display graphviz dot of AST with dotty(1)
|
Generate and display graphviz dot of AST with dotty(1)
|
||||||
YAEGI_CFG_DOT=1
|
YAEGI_CFG_DOT=1
|
||||||
@@ -90,6 +99,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/traefik/yaegi/interp"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -97,8 +108,11 @@ const (
|
|||||||
Help = "help"
|
Help = "help"
|
||||||
Run = "run"
|
Run = "run"
|
||||||
Test = "test"
|
Test = "test"
|
||||||
|
Version = "version"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var version = "devel" // This may be overwritten at build time.
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var cmd string
|
var cmd string
|
||||||
var err error
|
var err error
|
||||||
@@ -119,6 +133,8 @@ func main() {
|
|||||||
err = run(os.Args[2:])
|
err = run(os.Args[2:])
|
||||||
case Test:
|
case Test:
|
||||||
err = test(os.Args[2:])
|
err = test(os.Args[2:])
|
||||||
|
case Version:
|
||||||
|
fmt.Println(version)
|
||||||
default:
|
default:
|
||||||
// If no command is given, fallback to default "run" command.
|
// If no command is given, fallback to default "run" command.
|
||||||
// This allows scripts starting with "#!/usr/bin/env yaegi",
|
// This allows scripts starting with "#!/usr/bin/env yaegi",
|
||||||
@@ -129,8 +145,10 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err != nil && !errors.Is(err, flag.ErrHelp) {
|
if err != nil && !errors.Is(err, flag.ErrHelp) {
|
||||||
err = fmt.Errorf("%s: %w", cmd, err)
|
fmt.Fprintln(os.Stderr, fmt.Errorf("%s: %w", cmd, err))
|
||||||
fmt.Fprintln(os.Stderr, err)
|
if p, ok := err.(interp.Panic); ok {
|
||||||
|
fmt.Fprintln(os.Stderr, string(p.Stack))
|
||||||
|
}
|
||||||
exitCode = 1
|
exitCode = 1
|
||||||
}
|
}
|
||||||
os.Exit(exitCode)
|
os.Exit(exitCode)
|
||||||
|
|||||||
@@ -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.
|
||||||
|
|||||||
13
example/pkg/_pkg13/src/guthib.com/foo/bar/main.go
Normal file
13
example/pkg/_pkg13/src/guthib.com/foo/bar/main.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"guthib.com/bat/baz"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
t := baz.NewT()
|
||||||
|
|
||||||
|
fmt.Printf("%s", t.A3)
|
||||||
|
}
|
||||||
22
example/pkg/_pkg13/src/guthib.com/foo/bar/vendor/guthib.com/bat/baz/baz.go
generated
vendored
Normal file
22
example/pkg/_pkg13/src/guthib.com/foo/bar/vendor/guthib.com/bat/baz/baz.go
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package baz
|
||||||
|
|
||||||
|
func NewT() *T {
|
||||||
|
return &T{
|
||||||
|
A1: make([]U, 0),
|
||||||
|
A3: "foobar",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type T struct {
|
||||||
|
A1 []U
|
||||||
|
A3 string
|
||||||
|
}
|
||||||
|
|
||||||
|
type U struct {
|
||||||
|
B1 V
|
||||||
|
B2 V
|
||||||
|
}
|
||||||
|
|
||||||
|
type V struct {
|
||||||
|
C1 string
|
||||||
|
}
|
||||||
@@ -93,12 +93,18 @@ func TestPackages(t *testing.T) {
|
|||||||
expected: "Yo hello",
|
expected: "Yo hello",
|
||||||
evalFile: "./_pkg12/src/guthib.com/foo/main.go",
|
evalFile: "./_pkg12/src/guthib.com/foo/main.go",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
desc: "eval main with vendor",
|
||||||
|
goPath: "./_pkg13/",
|
||||||
|
expected: "foobar",
|
||||||
|
evalFile: "./_pkg13/src/guthib.com/foo/bar/main.go",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range testCases {
|
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)
|
||||||
}
|
}
|
||||||
@@ -109,7 +115,7 @@ func TestPackages(t *testing.T) {
|
|||||||
|
|
||||||
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()
|
||||||
@@ -140,6 +146,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()
|
||||||
}
|
}
|
||||||
@@ -153,7 +161,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"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,13 +18,14 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
const model = `// Code generated by 'github.com/traefik/yaegi/extract {{.PkgName}}'. DO NOT EDIT.
|
const model = `// Code generated by 'yaegi extract {{.PkgName}}'. DO NOT EDIT.
|
||||||
|
|
||||||
{{.License}}
|
{{.License}}
|
||||||
|
|
||||||
@@ -111,7 +112,17 @@ var restricted = map[string]bool{
|
|||||||
"logNew": true,
|
"logNew": true,
|
||||||
}
|
}
|
||||||
|
|
||||||
func genContent(dest, importPath, license string, p *types.Package, skip map[string]bool) ([]byte, error) {
|
func matchList(name string, list []string) (match bool, err error) {
|
||||||
|
for _, re := range list {
|
||||||
|
match, err = regexp.MatchString(re, name)
|
||||||
|
if err != nil || match {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Extractor) genContent(importPath string, p *types.Package) ([]byte, error) {
|
||||||
prefix := "_" + importPath + "_"
|
prefix := "_" + importPath + "_"
|
||||||
prefix = strings.NewReplacer("/", "_", "-", "_", ".", "_").Replace(prefix)
|
prefix = strings.NewReplacer("/", "_", "-", "_", ".", "_").Replace(prefix)
|
||||||
|
|
||||||
@@ -137,11 +148,26 @@ func genContent(dest, importPath, license string, p *types.Package, skip map[str
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
pname := path.Base(importPath) + "." + name
|
if len(e.Include) > 0 {
|
||||||
if skip[pname] {
|
match, err := matchList(name, e.Include)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !match {
|
||||||
|
// Explicitly defined include expressions force non matching symbols to be skipped.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
match, err := matchList(name, e.Exclude)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if match {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pname := path.Base(importPath) + "." + name
|
||||||
if rname := path.Base(importPath) + name; restricted[rname] {
|
if rname := path.Base(importPath) + name; restricted[rname] {
|
||||||
// Restricted symbol, locally provided by stdlib wrapper.
|
// Restricted symbol, locally provided by stdlib wrapper.
|
||||||
pname = rname
|
pname = rname
|
||||||
@@ -150,7 +176,7 @@ func genContent(dest, importPath, license string, p *types.Package, skip map[str
|
|||||||
switch o := o.(type) {
|
switch o := o.(type) {
|
||||||
case *types.Const:
|
case *types.Const:
|
||||||
if b, ok := o.Type().(*types.Basic); ok && (b.Info()&types.IsUntyped) != 0 {
|
if b, ok := o.Type().(*types.Basic); ok && (b.Info()&types.IsUntyped) != 0 {
|
||||||
// convert untyped constant to right type to avoid overflow
|
// Convert untyped constant to right type to avoid overflow.
|
||||||
val[name] = Val{fixConst(pname, o.Val(), imports), false}
|
val[name] = Val{fixConst(pname, o.Val(), imports), false}
|
||||||
} else {
|
} else {
|
||||||
val[name] = Val{pname, false}
|
val[name] = Val{pname, false}
|
||||||
@@ -201,15 +227,21 @@ func genContent(dest, importPath, license string, p *types.Package, skip map[str
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
buildTags, err := buildTags()
|
// Generate buildTags with Go version only for stdlib packages.
|
||||||
if err != nil {
|
// Third party packages do not depend on Go compiler version by default.
|
||||||
return nil, err
|
var buildTags string
|
||||||
|
if isInStdlib(importPath) {
|
||||||
|
var err error
|
||||||
|
buildTags, err = genBuildTags()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
base := template.New("goexports")
|
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" {
|
||||||
@@ -231,24 +263,24 @@ func genContent(dest, importPath, license string, p *types.Package, skip map[str
|
|||||||
|
|
||||||
b := new(bytes.Buffer)
|
b := new(bytes.Buffer)
|
||||||
data := map[string]interface{}{
|
data := map[string]interface{}{
|
||||||
"Dest": dest,
|
"Dest": e.Dest,
|
||||||
"Imports": imports,
|
"Imports": imports,
|
||||||
"PkgName": importPath,
|
"PkgName": importPath,
|
||||||
"Val": val,
|
"Val": val,
|
||||||
"Typ": typ,
|
"Typ": typ,
|
||||||
"Wrap": wrap,
|
"Wrap": wrap,
|
||||||
"BuildTags": buildTags,
|
"BuildTags": buildTags,
|
||||||
"License": license,
|
"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
|
||||||
}
|
}
|
||||||
@@ -288,12 +320,20 @@ func fixConst(name string, val constant.Value, imports map[string]bool) string {
|
|||||||
return fmt.Sprintf("constant.MakeFromLiteral(%q, token.%s, 0)", str, tok)
|
return fmt.Sprintf("constant.MakeFromLiteral(%q, token.%s, 0)", str, tok)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Extractor creates a package with all the symbols from a dependency package.
|
||||||
|
type Extractor struct {
|
||||||
|
Dest string // The name of the created package.
|
||||||
|
License string // License text to be included in the created package, optional.
|
||||||
|
Exclude []string // Comma separated list of regexp matching symbols to exclude.
|
||||||
|
Include []string // Comma separated list of regexp matching symbols to include.
|
||||||
|
}
|
||||||
|
|
||||||
// importPath checks whether pkgIdent is an existing directory relative to
|
// importPath checks whether pkgIdent is an existing directory relative to
|
||||||
// e.WorkingDir. If yes, it returns the actual import path of the Go package
|
// e.WorkingDir. If yes, it returns the actual import path of the Go package
|
||||||
// located in the directory. If it is definitely a relative path, but it does not
|
// located in the directory. If it is definitely a relative path, but it does not
|
||||||
// exist, an error is returned. Otherwise, it is assumed to be an import path, and
|
// exist, an error is returned. Otherwise, it is assumed to be an import path, and
|
||||||
// pkgIdent is returned.
|
// pkgIdent is returned.
|
||||||
func (e Extractor) importPath(pkgIdent, importPath string) (string, error) {
|
func (e *Extractor) importPath(pkgIdent, importPath string) (string, error) {
|
||||||
wd, err := os.Getwd()
|
wd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@@ -353,20 +393,13 @@ func (e Extractor) importPath(pkgIdent, importPath string) (string, error) {
|
|||||||
return parts[1], nil
|
return parts[1], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extractor creates a package with all the symbols from a dependency package.
|
|
||||||
type Extractor struct {
|
|
||||||
Dest string // the name of the created package.
|
|
||||||
License string // license text to be included in the created package, optional.
|
|
||||||
Skip map[string]bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract writes to rw a Go package with all the symbols found at pkgIdent.
|
// Extract writes to rw a Go package with all the symbols found at pkgIdent.
|
||||||
// pkgIdent can be an import path, or a local path, relative to e.WorkingDir. In
|
// pkgIdent can be an import path, or a local path, relative to e.WorkingDir. In
|
||||||
// the latter case, Extract returns the actual import path of the package found at
|
// the latter case, Extract returns the actual import path of the package found at
|
||||||
// pkgIdent, otherwise it just returns pkgIdent.
|
// pkgIdent, otherwise it just returns pkgIdent.
|
||||||
// If pkgIdent is an import path, it is looked up in GOPATH. Vendoring is not
|
// If pkgIdent is an import path, it is looked up in GOPATH. Vendoring is not
|
||||||
// supported yet, and the behavior is only defined for GO111MODULE=off.
|
// supported yet, and the behavior is only defined for GO111MODULE=off.
|
||||||
func (e Extractor) Extract(pkgIdent, importPath string, rw io.Writer) (string, error) {
|
func (e *Extractor) Extract(pkgIdent, importPath string, rw io.Writer) (string, error) {
|
||||||
ipp, err := e.importPath(pkgIdent, importPath)
|
ipp, err := e.importPath(pkgIdent, importPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@@ -377,7 +410,7 @@ func (e Extractor) Extract(pkgIdent, importPath string, rw io.Writer) (string, e
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
content, err := genContent(e.Dest, ipp, e.License, pkg, e.Skip)
|
content, err := e.genContent(ipp, pkg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -403,10 +436,12 @@ func GetMinor(part string) string {
|
|||||||
return minor
|
return minor
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildTags() (string, error) {
|
const defaultMinorVersion = 15
|
||||||
|
|
||||||
|
func genBuildTags() (string, error) {
|
||||||
version := runtime.Version()
|
version := runtime.Version()
|
||||||
if version == "devel" {
|
if strings.HasPrefix(version, "devel") {
|
||||||
return "", nil
|
return "", fmt.Errorf("extracting only supported with stable releases of Go, not %v", version)
|
||||||
}
|
}
|
||||||
parts := strings.Split(version, ".")
|
parts := strings.Split(version, ".")
|
||||||
|
|
||||||
@@ -416,10 +451,17 @@ func buildTags() (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
|
||||||
|
if minor >= defaultMinorVersion {
|
||||||
|
return currentGoVersion, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
nextGoVersion := parts[0] + "." + strconv.Itoa(minor+1)
|
nextGoVersion := parts[0] + "." + strconv.Itoa(minor+1)
|
||||||
|
|
||||||
return currentGoVersion + ",!" + nextGoVersion, nil
|
return currentGoVersion + ",!" + nextGoVersion, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isInStdlib(path string) bool { return !strings.Contains(path, ".") }
|
||||||
|
|||||||
@@ -8,9 +8,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
var expectedOutput = `// Code generated by 'github.com/traefik/yaegi/extract guthib.com/baz'. DO NOT EDIT.
|
var expectedOutput = `// Code generated by 'yaegi extract guthib.com/baz'. DO NOT EDIT.
|
||||||
|
|
||||||
// +build BUILD_TAGS
|
|
||||||
|
|
||||||
package bar
|
package bar
|
||||||
|
|
||||||
@@ -27,14 +25,6 @@ func init() {
|
|||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
func init() {
|
|
||||||
buildTags, err := buildTags()
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
expectedOutput = strings.Replace(expectedOutput, "BUILD_TAGS", buildTags, 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPackages(t *testing.T) {
|
func TestPackages(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
desc string
|
desc string
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package yaegi
|
package yaegi
|
||||||
|
|
||||||
|
//go:generate go generate github.com/traefik/yaegi/internal/cmd/extract
|
||||||
//go:generate go generate github.com/traefik/yaegi/interp
|
//go:generate go generate github.com/traefik/yaegi/interp
|
||||||
//go:generate go generate github.com/traefik/yaegi/cmd/goexports
|
|
||||||
//go:generate go generate github.com/traefik/yaegi/stdlib
|
//go:generate go generate github.com/traefik/yaegi/stdlib
|
||||||
//go:generate go generate github.com/traefik/yaegi/stdlib/syscall
|
//go:generate go generate github.com/traefik/yaegi/stdlib/syscall
|
||||||
//go:generate go generate github.com/traefik/yaegi/stdlib/unsafe
|
//go:generate go generate github.com/traefik/yaegi/stdlib/unsafe
|
||||||
|
|||||||
401
install.sh
Normal file
401
install.sh
Normal file
@@ -0,0 +1,401 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -e
|
||||||
|
# Code generated by godownloader on 2020-09-27T12:53:27Z. DO NOT EDIT.
|
||||||
|
#
|
||||||
|
|
||||||
|
usage() {
|
||||||
|
this=$1
|
||||||
|
cat <<EOF
|
||||||
|
$this: download go binaries for traefik/yaegi
|
||||||
|
|
||||||
|
Usage: $this [-b] bindir [-d] [tag]
|
||||||
|
-b sets bindir or installation directory, Defaults to ./bin
|
||||||
|
-d turns on debug logging
|
||||||
|
[tag] is a tag from
|
||||||
|
https://github.com/traefik/yaegi/releases
|
||||||
|
If tag is missing, then the latest will be used.
|
||||||
|
|
||||||
|
Generated by godownloader
|
||||||
|
https://github.com/goreleaser/godownloader
|
||||||
|
|
||||||
|
EOF
|
||||||
|
exit 2
|
||||||
|
}
|
||||||
|
|
||||||
|
parse_args() {
|
||||||
|
#BINDIR is ./bin unless set be ENV
|
||||||
|
# over-ridden by flag below
|
||||||
|
|
||||||
|
BINDIR=${BINDIR:-./bin}
|
||||||
|
while getopts "b:dh?x" arg; do
|
||||||
|
case "$arg" in
|
||||||
|
b) BINDIR="$OPTARG" ;;
|
||||||
|
d) log_set_priority 10 ;;
|
||||||
|
h | \?) usage "$0" ;;
|
||||||
|
x) set -x ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
shift $((OPTIND - 1))
|
||||||
|
TAG=$1
|
||||||
|
}
|
||||||
|
# this function wraps all the destructive operations
|
||||||
|
# if a curl|bash cuts off the end of the script due to
|
||||||
|
# network, either nothing will happen or will syntax error
|
||||||
|
# out preventing half-done work
|
||||||
|
execute() {
|
||||||
|
tmpdir=$(mktemp -d)
|
||||||
|
log_debug "downloading files into ${tmpdir}"
|
||||||
|
http_download "${tmpdir}/${TARBALL}" "${TARBALL_URL}"
|
||||||
|
http_download "${tmpdir}/${CHECKSUM}" "${CHECKSUM_URL}"
|
||||||
|
hash_sha256_verify "${tmpdir}/${TARBALL}" "${tmpdir}/${CHECKSUM}"
|
||||||
|
srcdir="${tmpdir}"
|
||||||
|
(cd "${tmpdir}" && untar "${TARBALL}")
|
||||||
|
test ! -d "${BINDIR}" && install -d "${BINDIR}"
|
||||||
|
for binexe in $BINARIES; do
|
||||||
|
if [ "$OS" = "windows" ]; then
|
||||||
|
binexe="${binexe}.exe"
|
||||||
|
fi
|
||||||
|
install "${srcdir}/${binexe}" "${BINDIR}/"
|
||||||
|
log_info "installed ${BINDIR}/${binexe}"
|
||||||
|
done
|
||||||
|
rm -rf "${tmpdir}"
|
||||||
|
}
|
||||||
|
get_binaries() {
|
||||||
|
case "$PLATFORM" in
|
||||||
|
darwin/amd64) BINARIES="yaegi" ;;
|
||||||
|
darwin/arm64) BINARIES="yaegi" ;;
|
||||||
|
darwin/armv5) BINARIES="yaegi" ;;
|
||||||
|
darwin/armv6) BINARIES="yaegi" ;;
|
||||||
|
darwin/armv7) BINARIES="yaegi" ;;
|
||||||
|
freebsd/386) BINARIES="yaegi" ;;
|
||||||
|
freebsd/amd64) BINARIES="yaegi" ;;
|
||||||
|
freebsd/arm64) BINARIES="yaegi" ;;
|
||||||
|
freebsd/armv5) BINARIES="yaegi" ;;
|
||||||
|
freebsd/armv6) BINARIES="yaegi" ;;
|
||||||
|
freebsd/armv7) BINARIES="yaegi" ;;
|
||||||
|
linux/386) BINARIES="yaegi" ;;
|
||||||
|
linux/amd64) BINARIES="yaegi" ;;
|
||||||
|
linux/arm64) BINARIES="yaegi" ;;
|
||||||
|
linux/armv5) BINARIES="yaegi" ;;
|
||||||
|
linux/armv6) BINARIES="yaegi" ;;
|
||||||
|
linux/armv7) BINARIES="yaegi" ;;
|
||||||
|
openbsd/386) BINARIES="yaegi" ;;
|
||||||
|
openbsd/amd64) BINARIES="yaegi" ;;
|
||||||
|
openbsd/arm64) BINARIES="yaegi" ;;
|
||||||
|
openbsd/armv5) BINARIES="yaegi" ;;
|
||||||
|
openbsd/armv6) BINARIES="yaegi" ;;
|
||||||
|
openbsd/armv7) BINARIES="yaegi" ;;
|
||||||
|
solaris/386) BINARIES="yaegi" ;;
|
||||||
|
solaris/amd64) BINARIES="yaegi" ;;
|
||||||
|
solaris/arm64) BINARIES="yaegi" ;;
|
||||||
|
solaris/armv5) BINARIES="yaegi" ;;
|
||||||
|
solaris/armv6) BINARIES="yaegi" ;;
|
||||||
|
solaris/armv7) BINARIES="yaegi" ;;
|
||||||
|
*)
|
||||||
|
log_crit "platform $PLATFORM is not supported. Make sure this script is up-to-date and file request at https://github.com/${PREFIX}/issues/new"
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
tag_to_version() {
|
||||||
|
if [ -z "${TAG}" ]; then
|
||||||
|
log_info "checking GitHub for latest tag"
|
||||||
|
else
|
||||||
|
log_info "checking GitHub for tag '${TAG}'"
|
||||||
|
fi
|
||||||
|
REALTAG=$(github_release "$OWNER/$REPO" "${TAG}") && true
|
||||||
|
if test -z "$REALTAG"; then
|
||||||
|
log_crit "unable to find '${TAG}' - use 'latest' or see https://github.com/${PREFIX}/releases for details"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
# if version starts with 'v', remove it
|
||||||
|
TAG="$REALTAG"
|
||||||
|
VERSION=${TAG#v}
|
||||||
|
}
|
||||||
|
adjust_format() {
|
||||||
|
# change format (tar.gz or zip) based on OS
|
||||||
|
case ${OS} in
|
||||||
|
windows) FORMAT=zip ;;
|
||||||
|
esac
|
||||||
|
true
|
||||||
|
}
|
||||||
|
adjust_os() {
|
||||||
|
# adjust archive name based on OS
|
||||||
|
true
|
||||||
|
}
|
||||||
|
adjust_arch() {
|
||||||
|
# adjust archive name based on ARCH
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
cat /dev/null <<EOF
|
||||||
|
------------------------------------------------------------------------
|
||||||
|
https://github.com/client9/shlib - portable posix shell functions
|
||||||
|
Public domain - http://unlicense.org
|
||||||
|
https://github.com/client9/shlib/blob/master/LICENSE.md
|
||||||
|
but credit (and pull requests) appreciated.
|
||||||
|
------------------------------------------------------------------------
|
||||||
|
EOF
|
||||||
|
is_command() {
|
||||||
|
command -v "$1" >/dev/null
|
||||||
|
}
|
||||||
|
echoerr() {
|
||||||
|
echo "$@" 1>&2
|
||||||
|
}
|
||||||
|
log_prefix() {
|
||||||
|
echo "$0"
|
||||||
|
}
|
||||||
|
_logp=6
|
||||||
|
log_set_priority() {
|
||||||
|
_logp="$1"
|
||||||
|
}
|
||||||
|
log_priority() {
|
||||||
|
if test -z "$1"; then
|
||||||
|
echo "$_logp"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
[ "$1" -le "$_logp" ]
|
||||||
|
}
|
||||||
|
log_tag() {
|
||||||
|
case $1 in
|
||||||
|
0) echo "emerg" ;;
|
||||||
|
1) echo "alert" ;;
|
||||||
|
2) echo "crit" ;;
|
||||||
|
3) echo "err" ;;
|
||||||
|
4) echo "warning" ;;
|
||||||
|
5) echo "notice" ;;
|
||||||
|
6) echo "info" ;;
|
||||||
|
7) echo "debug" ;;
|
||||||
|
*) echo "$1" ;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
log_debug() {
|
||||||
|
log_priority 7 || return 0
|
||||||
|
echoerr "$(log_prefix)" "$(log_tag 7)" "$@"
|
||||||
|
}
|
||||||
|
log_info() {
|
||||||
|
log_priority 6 || return 0
|
||||||
|
echoerr "$(log_prefix)" "$(log_tag 6)" "$@"
|
||||||
|
}
|
||||||
|
log_err() {
|
||||||
|
log_priority 3 || return 0
|
||||||
|
echoerr "$(log_prefix)" "$(log_tag 3)" "$@"
|
||||||
|
}
|
||||||
|
log_crit() {
|
||||||
|
log_priority 2 || return 0
|
||||||
|
echoerr "$(log_prefix)" "$(log_tag 2)" "$@"
|
||||||
|
}
|
||||||
|
uname_os() {
|
||||||
|
os=$(uname -s | tr '[:upper:]' '[:lower:]')
|
||||||
|
case "$os" in
|
||||||
|
cygwin_nt*) os="windows" ;;
|
||||||
|
mingw*) os="windows" ;;
|
||||||
|
msys_nt*) os="windows" ;;
|
||||||
|
esac
|
||||||
|
echo "$os"
|
||||||
|
}
|
||||||
|
uname_arch() {
|
||||||
|
arch=$(uname -m)
|
||||||
|
case $arch in
|
||||||
|
x86_64) arch="amd64" ;;
|
||||||
|
x86) arch="386" ;;
|
||||||
|
i686) arch="386" ;;
|
||||||
|
i386) arch="386" ;;
|
||||||
|
aarch64) arch="arm64" ;;
|
||||||
|
armv5*) arch="armv5" ;;
|
||||||
|
armv6*) arch="armv6" ;;
|
||||||
|
armv7*) arch="armv7" ;;
|
||||||
|
esac
|
||||||
|
echo ${arch}
|
||||||
|
}
|
||||||
|
uname_os_check() {
|
||||||
|
os=$(uname_os)
|
||||||
|
case "$os" in
|
||||||
|
darwin) return 0 ;;
|
||||||
|
dragonfly) return 0 ;;
|
||||||
|
freebsd) return 0 ;;
|
||||||
|
linux) return 0 ;;
|
||||||
|
android) return 0 ;;
|
||||||
|
nacl) return 0 ;;
|
||||||
|
netbsd) return 0 ;;
|
||||||
|
openbsd) return 0 ;;
|
||||||
|
plan9) return 0 ;;
|
||||||
|
solaris) return 0 ;;
|
||||||
|
windows) return 0 ;;
|
||||||
|
esac
|
||||||
|
log_crit "uname_os_check '$(uname -s)' got converted to '$os' which is not a GOOS value. Please file bug at https://github.com/client9/shlib"
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
uname_arch_check() {
|
||||||
|
arch=$(uname_arch)
|
||||||
|
case "$arch" in
|
||||||
|
386) return 0 ;;
|
||||||
|
amd64) return 0 ;;
|
||||||
|
arm64) return 0 ;;
|
||||||
|
armv5) return 0 ;;
|
||||||
|
armv6) return 0 ;;
|
||||||
|
armv7) return 0 ;;
|
||||||
|
ppc64) return 0 ;;
|
||||||
|
ppc64le) return 0 ;;
|
||||||
|
mips) return 0 ;;
|
||||||
|
mipsle) return 0 ;;
|
||||||
|
mips64) return 0 ;;
|
||||||
|
mips64le) return 0 ;;
|
||||||
|
s390x) return 0 ;;
|
||||||
|
amd64p32) return 0 ;;
|
||||||
|
esac
|
||||||
|
log_crit "uname_arch_check '$(uname -m)' got converted to '$arch' which is not a GOARCH value. Please file bug report at https://github.com/client9/shlib"
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
untar() {
|
||||||
|
tarball=$1
|
||||||
|
case "${tarball}" in
|
||||||
|
*.tar.gz | *.tgz) tar --no-same-owner -xzf "${tarball}" ;;
|
||||||
|
*.tar) tar --no-same-owner -xf "${tarball}" ;;
|
||||||
|
*.zip) unzip "${tarball}" ;;
|
||||||
|
*)
|
||||||
|
log_err "untar unknown archive format for ${tarball}"
|
||||||
|
return 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
http_download_curl() {
|
||||||
|
local_file=$1
|
||||||
|
source_url=$2
|
||||||
|
header=$3
|
||||||
|
if [ -z "$header" ]; then
|
||||||
|
code=$(curl -w '%{http_code}' -sL -o "$local_file" "$source_url")
|
||||||
|
else
|
||||||
|
code=$(curl -w '%{http_code}' -sL -H "$header" -o "$local_file" "$source_url")
|
||||||
|
fi
|
||||||
|
if [ "$code" != "200" ]; then
|
||||||
|
log_debug "http_download_curl received HTTP status $code"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
http_download_wget() {
|
||||||
|
local_file=$1
|
||||||
|
source_url=$2
|
||||||
|
header=$3
|
||||||
|
if [ -z "$header" ]; then
|
||||||
|
wget -q -O "$local_file" "$source_url"
|
||||||
|
else
|
||||||
|
wget -q --header "$header" -O "$local_file" "$source_url"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
http_download() {
|
||||||
|
log_debug "http_download $2"
|
||||||
|
if is_command curl; then
|
||||||
|
http_download_curl "$@"
|
||||||
|
return
|
||||||
|
elif is_command wget; then
|
||||||
|
http_download_wget "$@"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
log_crit "http_download unable to find wget or curl"
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
http_copy() {
|
||||||
|
tmp=$(mktemp)
|
||||||
|
http_download "${tmp}" "$1" "$2" || return 1
|
||||||
|
body=$(cat "$tmp")
|
||||||
|
rm -f "${tmp}"
|
||||||
|
echo "$body"
|
||||||
|
}
|
||||||
|
github_release() {
|
||||||
|
owner_repo=$1
|
||||||
|
version=$2
|
||||||
|
test -z "$version" && version="latest"
|
||||||
|
giturl="https://github.com/${owner_repo}/releases/${version}"
|
||||||
|
json=$(http_copy "$giturl" "Accept:application/json")
|
||||||
|
test -z "$json" && return 1
|
||||||
|
version=$(echo "$json" | tr -s '\n' ' ' | sed 's/.*"tag_name":"//' | sed 's/".*//')
|
||||||
|
test -z "$version" && return 1
|
||||||
|
echo "$version"
|
||||||
|
}
|
||||||
|
hash_sha256() {
|
||||||
|
TARGET=${1:-/dev/stdin}
|
||||||
|
if is_command gsha256sum; then
|
||||||
|
hash=$(gsha256sum "$TARGET") || return 1
|
||||||
|
echo "$hash" | cut -d ' ' -f 1
|
||||||
|
elif is_command sha256sum; then
|
||||||
|
hash=$(sha256sum "$TARGET") || return 1
|
||||||
|
echo "$hash" | cut -d ' ' -f 1
|
||||||
|
elif is_command shasum; then
|
||||||
|
hash=$(shasum -a 256 "$TARGET" 2>/dev/null) || return 1
|
||||||
|
echo "$hash" | cut -d ' ' -f 1
|
||||||
|
elif is_command openssl; then
|
||||||
|
hash=$(openssl -dst openssl dgst -sha256 "$TARGET") || return 1
|
||||||
|
echo "$hash" | cut -d ' ' -f a
|
||||||
|
else
|
||||||
|
log_crit "hash_sha256 unable to find command to compute sha-256 hash"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
hash_sha256_verify() {
|
||||||
|
TARGET=$1
|
||||||
|
checksums=$2
|
||||||
|
if [ -z "$checksums" ]; then
|
||||||
|
log_err "hash_sha256_verify checksum file not specified in arg2"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
BASENAME=${TARGET##*/}
|
||||||
|
want=$(grep "${BASENAME}" "${checksums}" 2>/dev/null | tr '\t' ' ' | cut -d ' ' -f 1)
|
||||||
|
if [ -z "$want" ]; then
|
||||||
|
log_err "hash_sha256_verify unable to find checksum for '${TARGET}' in '${checksums}'"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
got=$(hash_sha256 "$TARGET")
|
||||||
|
if [ "$want" != "$got" ]; then
|
||||||
|
log_err "hash_sha256_verify checksum for '$TARGET' did not verify ${want} vs $got"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
cat /dev/null <<EOF
|
||||||
|
------------------------------------------------------------------------
|
||||||
|
End of functions from https://github.com/client9/shlib
|
||||||
|
------------------------------------------------------------------------
|
||||||
|
EOF
|
||||||
|
|
||||||
|
PROJECT_NAME="yaegi"
|
||||||
|
OWNER=traefik
|
||||||
|
REPO="yaegi"
|
||||||
|
BINARY=yaegi
|
||||||
|
FORMAT=tar.gz
|
||||||
|
OS=$(uname_os)
|
||||||
|
ARCH=$(uname_arch)
|
||||||
|
PREFIX="$OWNER/$REPO"
|
||||||
|
|
||||||
|
# use in logging routines
|
||||||
|
log_prefix() {
|
||||||
|
echo "$PREFIX"
|
||||||
|
}
|
||||||
|
PLATFORM="${OS}/${ARCH}"
|
||||||
|
GITHUB_DOWNLOAD=https://github.com/${OWNER}/${REPO}/releases/download
|
||||||
|
|
||||||
|
uname_os_check "$OS"
|
||||||
|
uname_arch_check "$ARCH"
|
||||||
|
|
||||||
|
parse_args "$@"
|
||||||
|
|
||||||
|
get_binaries
|
||||||
|
|
||||||
|
tag_to_version
|
||||||
|
|
||||||
|
adjust_format
|
||||||
|
|
||||||
|
adjust_os
|
||||||
|
|
||||||
|
adjust_arch
|
||||||
|
|
||||||
|
log_info "found version: ${VERSION} for ${TAG}/${OS}/${ARCH}"
|
||||||
|
|
||||||
|
NAME=${PROJECT_NAME}_v${VERSION}_${OS}_${ARCH}
|
||||||
|
TARBALL=${NAME}.${FORMAT}
|
||||||
|
TARBALL_URL=${GITHUB_DOWNLOAD}/${TAG}/${TARBALL}
|
||||||
|
CHECKSUM=${PROJECT_NAME}_${VERSION}_checksums.txt
|
||||||
|
CHECKSUM_URL=${GITHUB_DOWNLOAD}/${TAG}/${CHECKSUM}
|
||||||
|
|
||||||
|
|
||||||
|
execute
|
||||||
108
internal/cmd/extract/extract.go
Normal file
108
internal/cmd/extract/extract.go
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
//go:generate go build
|
||||||
|
|
||||||
|
/*
|
||||||
|
extract generates wrappers of stdlib package exported symbols. This command
|
||||||
|
is reserved for internal use in yaegi project.
|
||||||
|
|
||||||
|
For a similar purpose with third party packages, see the yaegi extract subcommand,
|
||||||
|
based on the same code.
|
||||||
|
|
||||||
|
Output files are written in the current directory, and prefixed with the go version.
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
|
||||||
|
extract package...
|
||||||
|
|
||||||
|
The same program is used for all target operating systems and architectures.
|
||||||
|
The GOOS and GOARCH environment variables set the desired target.
|
||||||
|
*/
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"flag"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/traefik/yaegi/extract"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
exclude = flag.String("exclude", "", "comma separated list of regexp matching symbols to exclude")
|
||||||
|
include = flag.String("include", "", "comma separated list of regexp matching symbols to include")
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
if flag.NArg() == 0 {
|
||||||
|
flag.Usage()
|
||||||
|
log.Fatalf("missing package path")
|
||||||
|
}
|
||||||
|
|
||||||
|
wd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ext := extract.Extractor{
|
||||||
|
Dest: path.Base(wd),
|
||||||
|
}
|
||||||
|
|
||||||
|
goos, goarch := os.Getenv("GOOS"), os.Getenv("GOARCH")
|
||||||
|
|
||||||
|
if *exclude != "" {
|
||||||
|
ext.Exclude = strings.Split(*exclude, ",")
|
||||||
|
}
|
||||||
|
|
||||||
|
if *include != "" {
|
||||||
|
ext.Include = strings.Split(*include, ",")
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, pkgIdent := range flag.Args() {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
|
||||||
|
if pkgIdent == "syscall" && goos == "solaris" {
|
||||||
|
// Syscall6 is broken on solaris (https://github.com/golang/go/issues/24357),
|
||||||
|
// it breaks build, skip related symbols.
|
||||||
|
ext.Exclude = append(ext.Exclude, "Syscall6")
|
||||||
|
}
|
||||||
|
|
||||||
|
importPath, err := ext.Extract(pkgIdent, "", &buf)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var oFile string
|
||||||
|
if pkgIdent == "syscall" {
|
||||||
|
oFile = strings.ReplaceAll(importPath, "/", "_") + "_" + goos + "_" + goarch + ".go"
|
||||||
|
} else {
|
||||||
|
oFile = strings.ReplaceAll(importPath, "/", "_") + ".go"
|
||||||
|
}
|
||||||
|
|
||||||
|
version := runtime.Version()
|
||||||
|
if strings.HasPrefix(version, "devel") {
|
||||||
|
log.Fatalf("extracting only supported with stable releases of Go, not %v", version)
|
||||||
|
}
|
||||||
|
parts := strings.Split(version, ".")
|
||||||
|
prefix := parts[0] + "_" + extract.GetMinor(parts[1])
|
||||||
|
|
||||||
|
f, err := os.Create(prefix + "_" + oFile)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := io.Copy(f, &buf); err != nil {
|
||||||
|
_ = f.Close()
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := f.Close(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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"
|
||||||
@@ -63,11 +63,38 @@ func {{$name}}(n *node) {
|
|||||||
{{else}}
|
{{else}}
|
||||||
v1 := genValueInt(c1)
|
v1 := genValueInt(c1)
|
||||||
{{end -}}
|
{{end -}}
|
||||||
|
{{- if (eq $name "add")}}
|
||||||
|
if n.typ.cat != interfaceT || len(n.typ.field) > 0 {
|
||||||
|
n.exec = func(f *frame) bltn {
|
||||||
|
_, j := v1(f)
|
||||||
|
dest(f).SetInt(i + j)
|
||||||
|
return next
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var valf func(sum int64) reflect.Value
|
||||||
|
// TODO: cover other int types, and actually other numbers, and even all
|
||||||
|
// relevant operations, not just add.
|
||||||
|
switch typ.Kind() {
|
||||||
|
case reflect.Int:
|
||||||
|
valf = func(sum int64) reflect.Value { return reflect.ValueOf(int(sum)) }
|
||||||
|
case reflect.Int32:
|
||||||
|
valf = func(sum int64) reflect.Value { return reflect.ValueOf(int32(sum)) }
|
||||||
|
default: // int64
|
||||||
|
valf = func(sum int64) reflect.Value { return reflect.ValueOf(sum) }
|
||||||
|
}
|
||||||
|
n.exec = func(f *frame) bltn {
|
||||||
|
_, j := v1(f)
|
||||||
|
dest(f).Set(valf(i + j))
|
||||||
|
return next
|
||||||
|
}
|
||||||
|
{{else -}}
|
||||||
n.exec = func(f *frame) bltn {
|
n.exec = func(f *frame) bltn {
|
||||||
_, j := v1(f)
|
_, j := v1(f)
|
||||||
dest(f).SetInt(i {{$op.Name}} j)
|
dest(f).SetInt(i {{$op.Name}} j)
|
||||||
return next
|
return next
|
||||||
}
|
}
|
||||||
|
{{end -}}
|
||||||
case c1.rval.IsValid():
|
case c1.rval.IsValid():
|
||||||
v0 := genValueInt(c0)
|
v0 := genValueInt(c0)
|
||||||
{{- if $op.Shift}}
|
{{- if $op.Shift}}
|
||||||
@@ -75,11 +102,38 @@ func {{$name}}(n *node) {
|
|||||||
{{else}}
|
{{else}}
|
||||||
j := vInt(c1.rval)
|
j := vInt(c1.rval)
|
||||||
{{end -}}
|
{{end -}}
|
||||||
|
{{- if (eq $name "add")}}
|
||||||
|
var valf func(sum int64) reflect.Value
|
||||||
|
// TODO: cover other int types, and actually other numbers, and even all
|
||||||
|
// relevant operations, not just add.
|
||||||
|
switch typ.Kind() {
|
||||||
|
case reflect.Int:
|
||||||
|
valf = func(sum int64) reflect.Value { return reflect.ValueOf(int(sum)) }
|
||||||
|
case reflect.Int32:
|
||||||
|
valf = func(sum int64) reflect.Value { return reflect.ValueOf(int32(sum)) }
|
||||||
|
default: // int64
|
||||||
|
valf = func(sum int64) reflect.Value { return reflect.ValueOf(sum) }
|
||||||
|
}
|
||||||
|
if wantEmptyInterface(n) {
|
||||||
|
n.exec = func(f *frame) bltn {
|
||||||
|
_, i := v0(f)
|
||||||
|
dest(f).Set(valf(i + j))
|
||||||
|
return next
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
n.exec = func(f *frame) bltn {
|
||||||
|
_, i := v0(f)
|
||||||
|
dest(f).SetInt(i + j)
|
||||||
|
return next
|
||||||
|
}
|
||||||
|
{{else -}}
|
||||||
n.exec = func(f *frame) bltn {
|
n.exec = func(f *frame) bltn {
|
||||||
_, i := v0(f)
|
_, i := v0(f)
|
||||||
dest(f).SetInt(i {{$op.Name}} j)
|
dest(f).SetInt(i {{$op.Name}} j)
|
||||||
return next
|
return next
|
||||||
}
|
}
|
||||||
|
{{end -}}
|
||||||
default:
|
default:
|
||||||
v0 := genValueInt(c0)
|
v0 := genValueInt(c0)
|
||||||
{{- if $op.Shift}}
|
{{- if $op.Shift}}
|
||||||
@@ -196,8 +250,24 @@ func {{$name}}Const(n *node) {
|
|||||||
{{- if $op.Shift}}
|
{{- if $op.Shift}}
|
||||||
v := constant.Shift(vConstantValue(v0), token.{{tokenFromName $name}}, uint(vUint(v1)))
|
v := constant.Shift(vConstantValue(v0), token.{{tokenFromName $name}}, uint(vUint(v1)))
|
||||||
n.rval.Set(reflect.ValueOf(v))
|
n.rval.Set(reflect.ValueOf(v))
|
||||||
|
{{- else if (eq $op.Name "/")}}
|
||||||
|
var operator token.Token
|
||||||
|
// When the result of the operation is expected to be an int (because both
|
||||||
|
// operands are ints), we want to force the type of the whole expression to be an
|
||||||
|
// int (and not a float), which is achieved by using the QUO_ASSIGN operator.
|
||||||
|
if n.typ.untyped && isInt(n.typ.rtype) {
|
||||||
|
operator = token.QUO_ASSIGN
|
||||||
|
} else {
|
||||||
|
operator = token.QUO
|
||||||
|
}
|
||||||
|
v := constant.BinaryOp(vConstantValue(v0), operator, vConstantValue(v1))
|
||||||
|
n.rval.Set(reflect.ValueOf(v))
|
||||||
|
{{- else}}
|
||||||
|
{{- if $op.Int}}
|
||||||
|
v := constant.BinaryOp(constant.ToInt(vConstantValue(v0)), token.{{tokenFromName $name}}, constant.ToInt(vConstantValue(v1)))
|
||||||
{{- else}}
|
{{- 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}}
|
||||||
@@ -395,10 +465,10 @@ func {{$name}}Const(n *node) {
|
|||||||
case isConst:
|
case isConst:
|
||||||
v := constant.UnaryOp(token.{{tokenFromName $name}}, vConstantValue(v0), 0)
|
v := constant.UnaryOp(token.{{tokenFromName $name}}, vConstantValue(v0), 0)
|
||||||
n.rval.Set(reflect.ValueOf(v))
|
n.rval.Set(reflect.ValueOf(v))
|
||||||
case isInt(t):
|
|
||||||
n.rval.SetInt({{$op.Name}} v0.Int())
|
|
||||||
case isUint(t):
|
case isUint(t):
|
||||||
n.rval.SetUint({{$op.Name}} v0.Uint())
|
n.rval.SetUint({{$op.Name}} v0.Uint())
|
||||||
|
case isInt(t):
|
||||||
|
n.rval.SetInt({{$op.Name}} v0.Int())
|
||||||
{{- if $op.Float}}
|
{{- if $op.Float}}
|
||||||
case isFloat(t):
|
case isFloat(t):
|
||||||
n.rval.SetFloat({{$op.Name}} v0.Float())
|
n.rval.SetFloat({{$op.Name}} v0.Float())
|
||||||
@@ -928,10 +998,11 @@ 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() {
|
||||||
base := template.New("goexports")
|
base := template.New("genop")
|
||||||
base.Funcs(template.FuncMap{
|
base.Funcs(template.FuncMap{
|
||||||
"tokenFromName": func(name string) string {
|
"tokenFromName": func(name string) string {
|
||||||
switch name {
|
switch name {
|
||||||
@@ -956,17 +1027,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: "+"},
|
||||||
@@ -984,7 +1055,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 {
|
||||||
@@ -51,12 +51,14 @@ const (
|
|||||||
fieldList
|
fieldList
|
||||||
fileStmt
|
fileStmt
|
||||||
forStmt0 // for {}
|
forStmt0 // for {}
|
||||||
forStmt1 // for cond {}
|
forStmt1 // for init; ; {}
|
||||||
forStmt2 // for init; cond; {}
|
forStmt2 // for cond {}
|
||||||
forStmt3 // for ; cond; post {}
|
forStmt3 // for init; cond; {}
|
||||||
forStmt3a // for init; ; post {}
|
forStmt4 // for ; ; post {}
|
||||||
forStmt4 // for init; cond; post {}
|
forStmt5 // for ; cond; post {}
|
||||||
forRangeStmt // for range
|
forStmt6 // for init; ; post {}
|
||||||
|
forStmt7 // for init; cond; post {}
|
||||||
|
forRangeStmt // for range {}
|
||||||
funcDecl
|
funcDecl
|
||||||
funcLit
|
funcLit
|
||||||
funcType
|
funcType
|
||||||
@@ -134,8 +136,10 @@ var kinds = [...]string{
|
|||||||
forStmt1: "forStmt1",
|
forStmt1: "forStmt1",
|
||||||
forStmt2: "forStmt2",
|
forStmt2: "forStmt2",
|
||||||
forStmt3: "forStmt3",
|
forStmt3: "forStmt3",
|
||||||
forStmt3a: "forStmt3a",
|
|
||||||
forStmt4: "forStmt4",
|
forStmt4: "forStmt4",
|
||||||
|
forStmt5: "forStmt5",
|
||||||
|
forStmt6: "forStmt6",
|
||||||
|
forStmt7: "forStmt7",
|
||||||
forRangeStmt: "forRangeStmt",
|
forRangeStmt: "forRangeStmt",
|
||||||
funcDecl: "funcDecl",
|
funcDecl: "funcDecl",
|
||||||
funcType: "funcType",
|
funcType: "funcType",
|
||||||
@@ -654,23 +658,23 @@ func (interp *Interpreter) ast(src, name string, inc bool) (string, *node, error
|
|||||||
case *ast.ForStmt:
|
case *ast.ForStmt:
|
||||||
// Disambiguate variants of FOR statements with a node kind per variant
|
// Disambiguate variants of FOR statements with a node kind per variant
|
||||||
var kind nkind
|
var kind nkind
|
||||||
if a.Cond == nil {
|
switch {
|
||||||
if a.Init != nil && a.Post != nil {
|
case a.Cond == nil && a.Init == nil && a.Post == nil:
|
||||||
kind = forStmt3a
|
kind = forStmt0
|
||||||
} else {
|
case a.Cond == nil && a.Init != nil && a.Post == nil:
|
||||||
kind = forStmt0
|
kind = forStmt1
|
||||||
}
|
case a.Cond != nil && a.Init == nil && a.Post == nil:
|
||||||
} else {
|
kind = forStmt2
|
||||||
switch {
|
case a.Cond != nil && a.Init != nil && a.Post == nil:
|
||||||
case a.Init == nil && a.Post == nil:
|
kind = forStmt3
|
||||||
kind = forStmt1
|
case a.Cond == nil && a.Init == nil && a.Post != nil:
|
||||||
case a.Init != nil && a.Post == nil:
|
kind = forStmt4
|
||||||
kind = forStmt2
|
case a.Cond != nil && a.Init == nil && a.Post != nil:
|
||||||
case a.Init == nil && a.Post != nil:
|
kind = forStmt5
|
||||||
kind = forStmt3
|
case a.Cond == nil && a.Init != nil && a.Post != nil:
|
||||||
default:
|
kind = forStmt6
|
||||||
kind = forStmt4
|
case a.Cond != nil && a.Init != nil && a.Post != nil:
|
||||||
}
|
kind = forStmt7
|
||||||
}
|
}
|
||||||
st.push(addChild(&root, anc, pos, kind, aNop), nod)
|
st.push(addChild(&root, anc, pos, kind, aNop), nod)
|
||||||
|
|
||||||
|
|||||||
331
interp/cfg.go
331
interp/cfg.go
@@ -66,6 +66,44 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
switch n.kind {
|
switch n.kind {
|
||||||
|
case binaryExpr, unaryExpr, parenExpr:
|
||||||
|
if isBoolAction(n) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// Gather assigned type if set, to give context for type propagation at post-order.
|
||||||
|
switch n.anc.kind {
|
||||||
|
case assignStmt, defineStmt:
|
||||||
|
a := n.anc
|
||||||
|
i := childPos(n) - a.nright
|
||||||
|
if i < 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if len(a.child) > a.nright+a.nleft {
|
||||||
|
i--
|
||||||
|
}
|
||||||
|
dest := a.child[i]
|
||||||
|
if dest.typ != nil && !isInterface(dest.typ) {
|
||||||
|
// Interface type are not propagated, and will be resolved at post-order.
|
||||||
|
n.typ = dest.typ
|
||||||
|
}
|
||||||
|
case binaryExpr, unaryExpr, parenExpr:
|
||||||
|
n.typ = n.anc.typ
|
||||||
|
}
|
||||||
|
|
||||||
|
case defineStmt:
|
||||||
|
// Determine type of variables initialized at declaration, so it can be propagated.
|
||||||
|
if n.nleft+n.nright == len(n.child) {
|
||||||
|
// No type was specified on the left hand side, it will resolved at post-order.
|
||||||
|
break
|
||||||
|
}
|
||||||
|
n.typ, err = nodeType(interp, sc, n.child[n.nleft])
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
for i := 0; i < n.nleft; i++ {
|
||||||
|
n.child[i].typ = n.typ
|
||||||
|
}
|
||||||
|
|
||||||
case blockStmt:
|
case blockStmt:
|
||||||
if n.anc != nil && n.anc.kind == rangeStmt {
|
if n.anc != nil && n.anc.kind == rangeStmt {
|
||||||
// For range block: ensure that array or map type is propagated to iterators
|
// For range block: ensure that array or map type is propagated to iterators
|
||||||
@@ -159,7 +197,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
|
|
||||||
case breakStmt, continueStmt, gotoStmt:
|
case breakStmt, continueStmt, gotoStmt:
|
||||||
if len(n.child) > 0 {
|
if len(n.child) > 0 {
|
||||||
// Handle labeled statements
|
// Handle labeled statements.
|
||||||
label := n.child[0].ident
|
label := n.child[0].ident
|
||||||
if sym, _, ok := sc.lookup(label); ok {
|
if sym, _, ok := sc.lookup(label); ok {
|
||||||
if sym.kind != labelSym {
|
if sym.kind != labelSym {
|
||||||
@@ -176,25 +214,23 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
|
|
||||||
case labeledStmt:
|
case labeledStmt:
|
||||||
label := n.child[0].ident
|
label := n.child[0].ident
|
||||||
if sym, _, ok := sc.lookup(label); ok {
|
// TODO(marc): labels must be stored outside of symbols to avoid collisions
|
||||||
if sym.kind != labelSym {
|
// Used labels are searched in current and sub scopes, not upper ones.
|
||||||
err = n.child[0].cfgErrorf("label %s not defined", label)
|
if sym, ok := sc.lookdown(label); ok {
|
||||||
break
|
|
||||||
}
|
|
||||||
sym.node = n
|
sym.node = n
|
||||||
n.sym = sym
|
n.sym = sym
|
||||||
} else {
|
} else {
|
||||||
n.sym = &symbol{kind: labelSym, node: n, index: -1}
|
n.sym = &symbol{kind: labelSym, node: n, index: -1}
|
||||||
sc.sym[label] = n.sym
|
|
||||||
}
|
}
|
||||||
|
sc.sym[label] = n.sym
|
||||||
|
|
||||||
case caseClause:
|
case caseClause:
|
||||||
sc = sc.pushBloc()
|
sc = sc.pushBloc()
|
||||||
if sn := n.anc.anc; sn.kind == typeSwitch && sn.child[1].action == aAssign {
|
if sn := n.anc.anc; sn.kind == typeSwitch && sn.child[1].action == aAssign {
|
||||||
// Type switch clause with a var defined in switch guard
|
// Type switch clause with a var defined in switch guard.
|
||||||
var typ *itype
|
var typ *itype
|
||||||
if len(n.child) == 2 {
|
if len(n.child) == 2 {
|
||||||
// 1 type in clause: define the var with this type in the case clause scope
|
// 1 type in clause: define the var with this type in the case clause scope.
|
||||||
switch {
|
switch {
|
||||||
case n.child[0].ident == nilIdent:
|
case n.child[0].ident == nilIdent:
|
||||||
typ = sc.getType("interface{}")
|
typ = sc.getType("interface{}")
|
||||||
@@ -204,7 +240,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
typ, err = nodeType(interp, sc, n.child[0])
|
typ, err = nodeType(interp, sc, n.child[0])
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// define the var with the type in the switch guard expression
|
// Define the var with the type in the switch guard expression.
|
||||||
typ = sn.child[1].child[1].child[0].typ
|
typ = sn.child[1].child[1].child[0].typ
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -252,8 +288,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")
|
||||||
@@ -285,11 +325,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case forStmt0, forRangeStmt:
|
case forStmt0, forStmt1, forStmt2, forStmt3, forStmt4, forStmt5, forStmt6, forStmt7, forRangeStmt:
|
||||||
sc = sc.pushBloc()
|
|
||||||
sc.loop, sc.loopRestart = n, n.child[0]
|
|
||||||
|
|
||||||
case forStmt1, forStmt2, forStmt3, forStmt3a, forStmt4:
|
|
||||||
sc = sc.pushBloc()
|
sc = sc.pushBloc()
|
||||||
sc.loop, sc.loopRestart = n, n.lastChild()
|
sc.loop, sc.loopRestart = n, n.lastChild()
|
||||||
|
|
||||||
@@ -386,9 +422,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
|
||||||
}
|
}
|
||||||
@@ -443,14 +480,11 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
n.gen = nop
|
n.gen = nop
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if n.anc.kind == commClause {
|
|
||||||
n.gen = nop
|
|
||||||
break
|
|
||||||
}
|
|
||||||
var atyp *itype
|
var atyp *itype
|
||||||
if n.nleft+n.nright < len(n.child) {
|
if n.nleft+n.nright < len(n.child) {
|
||||||
if atyp, err = nodeType(interp, sc, n.child[n.nleft]); err != nil {
|
if atyp, err = nodeType(interp, sc, n.child[n.nleft]); err != nil {
|
||||||
return
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -521,10 +555,23 @@ 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
|
// Propagate type.
|
||||||
// TODO: Check that existing destination type matches source type
|
// TODO: Check that existing destination type matches source type.
|
||||||
|
|
||||||
|
// In the following, we attempt to optimize by skipping the assign
|
||||||
|
// 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 isCall(src) && dest.typ.cat != interfaceT && !isRecursiveField(dest):
|
||||||
// Call action may perform the assignment directly.
|
// Call action may perform the assignment directly.
|
||||||
n.gen = nop
|
n.gen = nop
|
||||||
src.level = level
|
src.level = level
|
||||||
@@ -532,38 +579,45 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
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
|
||||||
|
}
|
||||||
|
if dest.action == aGetIndex {
|
||||||
|
// Skip optimization, as it does not work when assigning to a struct field.
|
||||||
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 src.kind == basicLit && !src.rval.IsValid():
|
case len(n.child) < 4 && isArithmeticAction(src):
|
||||||
|
// Optimize single assignments from some arithmetic operations.
|
||||||
|
src.typ = dest.typ
|
||||||
|
src.findex = dest.findex
|
||||||
|
src.level = level
|
||||||
|
n.gen = nop
|
||||||
|
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()
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
@@ -604,7 +658,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:
|
||||||
@@ -633,7 +687,12 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch n.action {
|
switch n.action {
|
||||||
case aRem, aShl, aShr:
|
case aRem:
|
||||||
|
n.typ = c0.typ
|
||||||
|
case aShl, aShr:
|
||||||
|
if c0.typ.untyped {
|
||||||
|
break
|
||||||
|
}
|
||||||
n.typ = c0.typ
|
n.typ = c0.typ
|
||||||
case aEqual, aNotEqual:
|
case aEqual, aNotEqual:
|
||||||
n.typ = sc.getType("bool")
|
n.typ = sc.getType("bool")
|
||||||
@@ -655,7 +714,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.
|
||||||
}
|
}
|
||||||
@@ -664,7 +723,7 @@ 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:
|
||||||
// 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.
|
||||||
@@ -687,7 +746,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()}
|
||||||
@@ -793,27 +858,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):
|
||||||
@@ -847,9 +932,14 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
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
|
||||||
n.rval = c1.rval
|
if c, ok := c1.rval.Interface().(constant.Value); ok {
|
||||||
|
i, _ := constant.Int64Val(constant.ToInt(c))
|
||||||
|
n.rval = reflect.ValueOf(i).Convert(c0.typ.rtype)
|
||||||
|
} else {
|
||||||
|
n.rval = c1.rval.Convert(c0.typ.rtype)
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
n.gen = convert
|
n.gen = convert
|
||||||
n.typ = c0.typ
|
n.typ = c0.typ
|
||||||
@@ -905,7 +995,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
n.findex = -1
|
n.findex = notInFrame
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -976,7 +1066,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
|
|
||||||
n.findex = sc.add(n.typ)
|
n.findex = sc.add(n.typ)
|
||||||
// TODO: Check that composite literal expr matches corresponding type
|
// TODO: Check that composite literal expr matches corresponding type
|
||||||
n.gen = compositeGenerator(n, n.typ)
|
n.gen = compositeGenerator(n, n.typ, nil)
|
||||||
|
|
||||||
case fallthroughtStmt:
|
case fallthroughtStmt:
|
||||||
if n.anc.kind != caseBody {
|
if n.anc.kind != caseBody {
|
||||||
@@ -986,7 +1076,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]
|
||||||
@@ -994,7 +1084,14 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
body.tnext = n.start
|
body.tnext = n.start
|
||||||
sc = sc.pop()
|
sc = sc.pop()
|
||||||
|
|
||||||
case forStmt1: // for cond {}
|
case forStmt1: // for init; ; {}
|
||||||
|
init, body := n.child[0], n.child[1]
|
||||||
|
n.start = init.start
|
||||||
|
init.tnext = body.start
|
||||||
|
body.tnext = n.start
|
||||||
|
sc = sc.pop()
|
||||||
|
|
||||||
|
case forStmt2: // for cond {}
|
||||||
cond, body := n.child[0], n.child[1]
|
cond, body := n.child[0], n.child[1]
|
||||||
if !isBool(cond.typ) {
|
if !isBool(cond.typ) {
|
||||||
err = cond.cfgErrorf("non-bool used as for condition")
|
err = cond.cfgErrorf("non-bool used as for condition")
|
||||||
@@ -1013,7 +1110,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
setFNext(cond, n)
|
setFNext(cond, n)
|
||||||
sc = sc.pop()
|
sc = sc.pop()
|
||||||
|
|
||||||
case forStmt2: // for init; cond; {}
|
case forStmt3: // for init; cond; {}
|
||||||
init, cond, body := n.child[0], n.child[1], n.child[2]
|
init, cond, body := n.child[0], n.child[1], n.child[2]
|
||||||
if !isBool(cond.typ) {
|
if !isBool(cond.typ) {
|
||||||
err = cond.cfgErrorf("non-bool used as for condition")
|
err = cond.cfgErrorf("non-bool used as for condition")
|
||||||
@@ -1035,7 +1132,14 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
setFNext(cond, n)
|
setFNext(cond, n)
|
||||||
sc = sc.pop()
|
sc = sc.pop()
|
||||||
|
|
||||||
case forStmt3: // for ; cond; post {}
|
case forStmt4: // for ; ; post {}
|
||||||
|
post, body := n.child[0], n.child[1]
|
||||||
|
n.start = body.start
|
||||||
|
post.tnext = body.start
|
||||||
|
body.tnext = post.start
|
||||||
|
sc = sc.pop()
|
||||||
|
|
||||||
|
case forStmt5: // for ; cond; post {}
|
||||||
cond, post, body := n.child[0], n.child[1], n.child[2]
|
cond, post, body := n.child[0], n.child[1], n.child[2]
|
||||||
if !isBool(cond.typ) {
|
if !isBool(cond.typ) {
|
||||||
err = cond.cfgErrorf("non-bool used as for condition")
|
err = cond.cfgErrorf("non-bool used as for condition")
|
||||||
@@ -1055,7 +1159,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
body.tnext = post.start
|
body.tnext = post.start
|
||||||
sc = sc.pop()
|
sc = sc.pop()
|
||||||
|
|
||||||
case forStmt3a: // for init; ; post {}
|
case forStmt6: // for init; ; post {}
|
||||||
init, post, body := n.child[0], n.child[1], n.child[2]
|
init, post, body := n.child[0], n.child[1], n.child[2]
|
||||||
n.start = init.start
|
n.start = init.start
|
||||||
init.tnext = body.start
|
init.tnext = body.start
|
||||||
@@ -1063,7 +1167,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
post.tnext = body.start
|
post.tnext = body.start
|
||||||
sc = sc.pop()
|
sc = sc.pop()
|
||||||
|
|
||||||
case forStmt4: // for init; cond; post {}
|
case forStmt7: // for init; cond; post {}
|
||||||
init, cond, post, body := n.child[0], n.child[1], n.child[2], n.child[3]
|
init, cond, post, body := n.child[0], n.child[1], n.child[2], n.child[3]
|
||||||
if !isBool(cond.typ) {
|
if !isBool(cond.typ) {
|
||||||
err = cond.cfgErrorf("non-bool used as for condition")
|
err = cond.cfgErrorf("non-bool used as for condition")
|
||||||
@@ -1123,7 +1227,6 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
sym, level, found := sc.lookup(n.ident)
|
sym, level, found := sc.lookup(n.ident)
|
||||||
if !found {
|
if !found {
|
||||||
// retry with the filename, in case ident is a package name.
|
// retry with the filename, in case ident is a package name.
|
||||||
// TODO(mpl): maybe we improve lookup itself so it can deal with that.
|
|
||||||
sym, level, found = sc.lookup(filepath.Join(n.ident, baseName))
|
sym, level, found = sc.lookup(filepath.Join(n.ident, baseName))
|
||||||
if !found {
|
if !found {
|
||||||
err = n.cfgErrorf("undefined: %s", n.ident)
|
err = n.cfgErrorf("undefined: %s", n.ident)
|
||||||
@@ -1296,7 +1399,12 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case returnStmt:
|
case returnStmt:
|
||||||
if mustReturnValue(sc.def.child[2]) {
|
if len(n.child) > sc.def.typ.numOut() {
|
||||||
|
err = n.cfgErrorf("too many arguments to return")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
returnSig := sc.def.child[2]
|
||||||
|
if mustReturnValue(returnSig) {
|
||||||
nret := len(n.child)
|
nret := len(n.child)
|
||||||
if nret == 1 && isCall(n.child[0]) {
|
if nret == 1 && isCall(n.child[0]) {
|
||||||
nret = n.child[0].child[0].typ.numOut()
|
nret = n.child[0].child[0].typ.numOut()
|
||||||
@@ -1310,13 +1418,19 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
n.tnext = nil
|
n.tnext = nil
|
||||||
n.val = sc.def
|
n.val = sc.def
|
||||||
for i, c := range n.child {
|
for i, c := range n.child {
|
||||||
|
var typ *itype
|
||||||
|
typ, err = nodeType(interp, sc.upperLevel(), returnSig.child[1].fieldType(i))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// TODO(mpl): move any of that code to typecheck?
|
||||||
|
c.typ.node = c
|
||||||
|
if !c.typ.assignableTo(typ) {
|
||||||
|
err = fmt.Errorf("cannot use %v (type %v) as type %v in return argument", c.ident, c.typ.cat, typ.cat)
|
||||||
|
return
|
||||||
|
}
|
||||||
if c.typ.cat == nilT {
|
if c.typ.cat == nilT {
|
||||||
// nil: Set node value to zero of return type
|
// nil: Set node value to zero of return type
|
||||||
f := sc.def
|
|
||||||
var typ *itype
|
|
||||||
if typ, err = nodeType(interp, sc, f.child[2].child[1].fieldType(i)); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if typ.cat == funcT {
|
if typ.cat == funcT {
|
||||||
// Wrap the typed nil value in a node, as per other interpreter functions
|
// Wrap the typed nil value in a node, as per other interpreter functions
|
||||||
c.rval = reflect.ValueOf(&node{kind: basicLit, rval: reflect.New(typ.TypeOf()).Elem()})
|
c.rval = reflect.ValueOf(&node{kind: basicLit, rval: reflect.New(typ.TypeOf()).Elem()})
|
||||||
@@ -1420,6 +1534,9 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
// 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
|
||||||
@@ -1434,7 +1551,7 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
|||||||
if n.child[0].isType(sc) {
|
if n.child[0].isType(sc) {
|
||||||
// Handle method as a function with receiver in 1st argument
|
// Handle method as a function with receiver in 1st argument
|
||||||
n.val = m
|
n.val = m
|
||||||
n.findex = -1
|
n.findex = notInFrame
|
||||||
n.gen = nop
|
n.gen = nop
|
||||||
n.typ = &itype{}
|
n.typ = &itype{}
|
||||||
*n.typ = *m.typ
|
*n.typ = *m.typ
|
||||||
@@ -1747,7 +1864,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
|
||||||
@@ -1769,11 +1886,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}
|
||||||
@@ -1800,8 +1919,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)})
|
||||||
}
|
}
|
||||||
@@ -1825,7 +1951,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
|
||||||
@@ -2224,6 +2350,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 == "_" {
|
||||||
@@ -2257,7 +2397,7 @@ func isCall(n *node) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func isBinCall(n *node) bool {
|
func isBinCall(n *node) bool {
|
||||||
return n.kind == callExpr && n.child[0].typ.cat == valueT && n.child[0].typ.rtype.Kind() == reflect.Func
|
return isCall(n) && n.child[0].typ.cat == valueT && n.child[0].typ.rtype.Kind() == reflect.Func
|
||||||
}
|
}
|
||||||
|
|
||||||
func mustReturnValue(n *node) bool {
|
func mustReturnValue(n *node) bool {
|
||||||
@@ -2273,7 +2413,7 @@ func mustReturnValue(n *node) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func isRegularCall(n *node) bool {
|
func isRegularCall(n *node) bool {
|
||||||
return n.kind == callExpr && n.child[0].typ.cat == funcT
|
return isCall(n) && n.child[0].typ.cat == funcT
|
||||||
}
|
}
|
||||||
|
|
||||||
func variadicPos(n *node) int {
|
func variadicPos(n *node) int {
|
||||||
@@ -2354,10 +2494,10 @@ func gotoLabel(s *symbol) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func compositeGenerator(n *node, typ *itype) (gen bltnGenerator) {
|
func compositeGenerator(n *node, typ *itype, rtyp reflect.Type) (gen bltnGenerator) {
|
||||||
switch typ.cat {
|
switch typ.cat {
|
||||||
case aliasT, ptrT:
|
case aliasT, ptrT:
|
||||||
gen = compositeGenerator(n, n.typ.val)
|
gen = compositeGenerator(n, n.typ.val, rtyp)
|
||||||
case arrayT:
|
case arrayT:
|
||||||
gen = arrayLit
|
gen = arrayLit
|
||||||
case mapT:
|
case mapT:
|
||||||
@@ -2368,9 +2508,9 @@ func compositeGenerator(n *node, typ *itype) (gen bltnGenerator) {
|
|||||||
gen = compositeLitNotype
|
gen = compositeLitNotype
|
||||||
case n.lastChild().kind == keyValueExpr:
|
case n.lastChild().kind == keyValueExpr:
|
||||||
if n.nleft == 1 {
|
if n.nleft == 1 {
|
||||||
gen = compositeSparse
|
gen = compositeLitKeyed
|
||||||
} else {
|
} else {
|
||||||
gen = compositeSparseNotype
|
gen = compositeLitKeyedNotype
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
if n.nleft == 1 {
|
if n.nleft == 1 {
|
||||||
@@ -2380,11 +2520,27 @@ func compositeGenerator(n *node, typ *itype) (gen bltnGenerator) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
case valueT:
|
case valueT:
|
||||||
switch k := n.typ.rtype.Kind(); k {
|
if rtyp == nil {
|
||||||
|
rtyp = n.typ.rtype
|
||||||
|
}
|
||||||
|
// TODO(mpl): I do not understand where this side-effect is coming from, and why it happens. quickfix for now.
|
||||||
|
if rtyp == nil {
|
||||||
|
rtyp = n.typ.val.rtype
|
||||||
|
}
|
||||||
|
switch k := rtyp.Kind(); k {
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
gen = compositeBinStruct
|
if n.nleft == 1 {
|
||||||
|
gen = compositeBinStruct
|
||||||
|
} else {
|
||||||
|
gen = compositeBinStructNotype
|
||||||
|
}
|
||||||
case reflect.Map:
|
case reflect.Map:
|
||||||
|
// TODO(mpl): maybe needs a NoType version too
|
||||||
gen = compositeBinMap
|
gen = compositeBinMap
|
||||||
|
case reflect.Ptr:
|
||||||
|
gen = compositeGenerator(n, typ, n.typ.val.rtype)
|
||||||
|
case reflect.Slice:
|
||||||
|
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))
|
||||||
}
|
}
|
||||||
@@ -2420,9 +2576,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) {
|
}
|
||||||
|
|
||||||
|
// isArithmeticAction returns true if the node action is an arithmetic operator.
|
||||||
|
func isArithmeticAction(n *node) bool {
|
||||||
|
switch n.action {
|
||||||
|
case aAdd, aAnd, aAndNot, aBitNot, aMul, aNeg, aOr, aPos, aQuo, aRem, aShl, aShr, aSub, aXor:
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return t.String() == t.Kind().String()
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func isBoolAction(n *node) bool {
|
||||||
|
switch n.action {
|
||||||
|
case aEqual, aGreater, aGreaterEqual, aLand, aLor, aLower, aLowerEqual, aNot, aNotEqual:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ found in vendor, sources modules will be searched in GOPATH. Go modules
|
|||||||
are not supported yet by yaegi.
|
are not supported yet by yaegi.
|
||||||
|
|
||||||
Binary form packages are compiled and linked with the interpreter
|
Binary form packages are compiled and linked with the interpreter
|
||||||
executable, and exposed to scripts with the Use method. The goexports
|
executable, and exposed to scripts with the Use method. The extract
|
||||||
command can be used to generate package wrappers.
|
subcommand of yaegi can be used to generate package wrappers.
|
||||||
|
|
||||||
Custom build tags
|
Custom build tags
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,6 +116,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 {
|
||||||
@@ -145,7 +147,7 @@ func (interp *Interpreter) gta(root *node, rpath, importPath string) ([]*node, e
|
|||||||
elementType := sc.getType(typeName)
|
elementType := sc.getType(typeName)
|
||||||
if elementType == nil {
|
if elementType == nil {
|
||||||
// Add type if necessary, so method can be registered
|
// Add type if necessary, so method can be registered
|
||||||
sc.sym[typeName] = &symbol{kind: typeSym, typ: &itype{name: typeName, path: rpath, incomplete: true, node: rtn.child[0], scope: sc}}
|
sc.sym[typeName] = &symbol{kind: typeSym, typ: &itype{name: typeName, path: importPath, incomplete: true, node: rtn.child[0], scope: sc}}
|
||||||
elementType = sc.sym[typeName].typ
|
elementType = sc.sym[typeName].typ
|
||||||
}
|
}
|
||||||
rcvrtype = &itype{cat: ptrT, val: elementType, incomplete: elementType.incomplete, node: rtn, scope: sc}
|
rcvrtype = &itype{cat: ptrT, val: elementType, incomplete: elementType.incomplete, node: rtn, scope: sc}
|
||||||
@@ -154,7 +156,7 @@ func (interp *Interpreter) gta(root *node, rpath, importPath string) ([]*node, e
|
|||||||
rcvrtype = sc.getType(typeName)
|
rcvrtype = sc.getType(typeName)
|
||||||
if rcvrtype == nil {
|
if rcvrtype == nil {
|
||||||
// Add type if necessary, so method can be registered
|
// Add type if necessary, so method can be registered
|
||||||
sc.sym[typeName] = &symbol{kind: typeSym, typ: &itype{name: typeName, path: rpath, incomplete: true, node: rtn, scope: sc}}
|
sc.sym[typeName] = &symbol{kind: typeSym, typ: &itype{name: typeName, path: importPath, incomplete: true, node: rtn, scope: sc}}
|
||||||
rcvrtype = sc.sym[typeName].typ
|
rcvrtype = sc.sym[typeName].typ
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -248,16 +250,19 @@ func (interp *Interpreter) gta(root *node, rpath, importPath string) ([]*node, e
|
|||||||
typeName := n.child[0].ident
|
typeName := n.child[0].ident
|
||||||
var typ *itype
|
var typ *itype
|
||||||
if typ, err = nodeType(interp, sc, n.child[1]); err != nil {
|
if typ, err = nodeType(interp, sc, n.child[1]); err != nil {
|
||||||
|
err = nil
|
||||||
|
revisit = append(revisit, n)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if n.child[1].kind == identExpr {
|
switch n.child[1].kind {
|
||||||
n.typ = &itype{cat: aliasT, val: typ, name: typeName, path: rpath, field: typ.field, incomplete: typ.incomplete, scope: sc, node: n.child[0]}
|
case identExpr, selectorExpr:
|
||||||
|
n.typ = &itype{cat: aliasT, val: typ, name: typeName, path: importPath, field: typ.field, incomplete: typ.incomplete, scope: sc, node: n.child[0]}
|
||||||
copy(n.typ.method, typ.method)
|
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 = rpath
|
n.typ.path = importPath
|
||||||
}
|
}
|
||||||
|
|
||||||
asImportName := filepath.Join(typeName, baseName)
|
asImportName := filepath.Join(typeName, baseName)
|
||||||
@@ -315,7 +320,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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,6 @@ package interp
|
|||||||
|
|
||||||
import "reflect"
|
import "reflect"
|
||||||
|
|
||||||
const hooksPath = "github.com/traefik/yaegi"
|
|
||||||
|
|
||||||
// convertFn is the signature of a symbol converter.
|
// convertFn is the signature of a symbol converter.
|
||||||
type convertFn func(from, to reflect.Type) func(src, dest reflect.Value)
|
type convertFn func(from, to reflect.Type) func(src, dest reflect.Value)
|
||||||
|
|
||||||
|
|||||||
112
interp/interp.go
112
interp/interp.go
@@ -60,12 +60,13 @@ type receiver struct {
|
|||||||
|
|
||||||
// frame contains values for the current execution level (a function context).
|
// frame contains values for the current execution level (a function context).
|
||||||
type frame struct {
|
type frame struct {
|
||||||
// id is an atomic counter used for cancellation, only access
|
// id is an atomic counter used for cancellation, only accessed
|
||||||
// via newFrame/runid/setrunid/clone.
|
// via newFrame/runid/setrunid/clone.
|
||||||
// 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,14 +75,17 @@ 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
|
||||||
}
|
}
|
||||||
@@ -93,6 +97,7 @@ func (f *frame) clone() *frame {
|
|||||||
defer f.mutex.RUnlock()
|
defer f.mutex.RUnlock()
|
||||||
return &frame{
|
return &frame{
|
||||||
anc: f.anc,
|
anc: f.anc,
|
||||||
|
root: f.root,
|
||||||
data: f.data,
|
data: f.data,
|
||||||
deferred: f.deferred,
|
deferred: f.deferred,
|
||||||
recovered: f.recovered,
|
recovered: f.recovered,
|
||||||
@@ -155,8 +160,9 @@ type Interpreter struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
mainID = "main"
|
mainID = "main"
|
||||||
selfPath = "github.com/traefik/yaegi/interp"
|
selfPrefix = "github.com/traefik/yaegi"
|
||||||
|
selfPath = selfPrefix + "/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?
|
||||||
@@ -175,6 +181,7 @@ var Symbols = Exports{
|
|||||||
|
|
||||||
"Interpreter": reflect.ValueOf((*Interpreter)(nil)),
|
"Interpreter": reflect.ValueOf((*Interpreter)(nil)),
|
||||||
"Options": reflect.ValueOf((*Options)(nil)),
|
"Options": reflect.ValueOf((*Options)(nil)),
|
||||||
|
"Panic": reflect.ValueOf((*Panic)(nil)),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,7 +244,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{},
|
||||||
@@ -401,32 +408,59 @@ func (interp *Interpreter) EvalTest(path string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Symbols returns a map of interpreter exported symbol values for the given path.
|
// Symbols returns a map of interpreter exported symbol values for the given
|
||||||
func (interp *Interpreter) Symbols(path string) map[string]reflect.Value {
|
// import path. If the argument is the empty string, all known symbols are
|
||||||
m := map[string]reflect.Value{}
|
// returned.
|
||||||
|
func (interp *Interpreter) Symbols(importPath string) Exports {
|
||||||
|
m := map[string]map[string]reflect.Value{}
|
||||||
interp.mutex.RLock()
|
interp.mutex.RLock()
|
||||||
if interp.scopes[path] == nil {
|
defer interp.mutex.RUnlock()
|
||||||
interp.mutex.RUnlock()
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
sym := interp.scopes[path].sym
|
|
||||||
interp.mutex.RUnlock()
|
|
||||||
|
|
||||||
for n, s := range sym {
|
for k, v := range interp.srcPkg {
|
||||||
if !canExport(n) {
|
if importPath != "" && k != importPath {
|
||||||
// Skip private non-exported symbols.
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
switch s.kind {
|
syms := map[string]reflect.Value{}
|
||||||
case constSym:
|
for n, s := range v {
|
||||||
m[n] = s.rval
|
if !canExport(n) {
|
||||||
case funcSym:
|
// Skip private non-exported symbols.
|
||||||
m[n] = genFunctionWrapper(s.node)(interp.frame)
|
continue
|
||||||
case varSym:
|
}
|
||||||
m[n] = interp.frame.data[s.index]
|
switch s.kind {
|
||||||
|
case constSym:
|
||||||
|
syms[n] = s.rval
|
||||||
|
case funcSym:
|
||||||
|
syms[n] = genFunctionWrapper(s.node)(interp.frame)
|
||||||
|
case varSym:
|
||||||
|
syms[n] = interp.frame.data[s.index]
|
||||||
|
case typeSym:
|
||||||
|
syms[n] = reflect.New(s.typ.TypeOf())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(syms) > 0 {
|
||||||
|
m[k] = syms
|
||||||
|
}
|
||||||
|
|
||||||
|
if importPath != "" {
|
||||||
|
return m
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if importPath != "" && len(m) > 0 {
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range interp.binPkg {
|
||||||
|
if importPath != "" && k != importPath {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
m[k] = v
|
||||||
|
if importPath != "" {
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -603,7 +637,7 @@ func (interp *Interpreter) getWrapper(t reflect.Type) reflect.Type {
|
|||||||
// 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) {
|
||||||
for k, v := range values {
|
for k, v := range values {
|
||||||
if k == hooksPath {
|
if k == selfPrefix {
|
||||||
interp.hooks.Parse(v)
|
interp.hooks.Parse(v)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -785,20 +819,28 @@ func (interp *Interpreter) REPL() (reflect.Value, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func doPrompt(out io.Writer) func(v reflect.Value) {
|
||||||
|
return func(v reflect.Value) {
|
||||||
|
if v.IsValid() {
|
||||||
|
fmt.Fprintln(out, ":", v)
|
||||||
|
}
|
||||||
|
fmt.Fprint(out, "> ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// getPrompt returns a function which prints a prompt only if input is a terminal.
|
// getPrompt returns a function which prints a prompt only if input is a terminal.
|
||||||
func getPrompt(in io.Reader, out io.Writer) func(reflect.Value) {
|
func getPrompt(in io.Reader, out io.Writer) func(reflect.Value) {
|
||||||
|
forcePrompt, _ := strconv.ParseBool(os.Getenv("YAEGI_PROMPT"))
|
||||||
|
if forcePrompt {
|
||||||
|
return doPrompt(out)
|
||||||
|
}
|
||||||
s, ok := in.(interface{ Stat() (os.FileInfo, error) })
|
s, ok := in.(interface{ Stat() (os.FileInfo, error) })
|
||||||
if !ok {
|
if !ok {
|
||||||
return func(reflect.Value) {}
|
return func(reflect.Value) {}
|
||||||
}
|
}
|
||||||
stat, err := s.Stat()
|
stat, err := s.Stat()
|
||||||
if err == nil && stat.Mode()&os.ModeCharDevice != 0 {
|
if err == nil && stat.Mode()&os.ModeCharDevice != 0 {
|
||||||
return func(v reflect.Value) {
|
return doPrompt(out)
|
||||||
if v.IsValid() {
|
|
||||||
fmt.Fprintln(out, ":", v)
|
|
||||||
}
|
|
||||||
fmt.Fprint(out, "> ")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return func(reflect.Value) {}
|
return func(reflect.Value) {}
|
||||||
}
|
}
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user