Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3ae01a2af3 | ||
|
|
c06f83f34a | ||
|
|
a6d9c84a30 |
6
Makefile
6
Makefile
@@ -20,4 +20,8 @@ tests:
|
||||
GO111MODULE=off go test -v ./...
|
||||
GO111MODULE=off go test -race ./interp
|
||||
|
||||
.PHONY: check gen_all_syscall gen_tests
|
||||
# https://github.com/goreleaser/godownloader
|
||||
generate_downloader:
|
||||
godownloader --repo=traefik/yaegi -o install.sh
|
||||
|
||||
.PHONY: check gen_all_syscall gen_tests generate_downloader
|
||||
|
||||
@@ -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),
|
||||
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
|
||||
|
||||
### As an embedded interpreter
|
||||
|
||||
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
|
||||
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
|
||||
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")
|
||||
}
|
||||
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
|
||||
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-19T15:21:58Z. 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 goexports" ;;
|
||||
darwin/arm64) BINARIES="yaegi goexports" ;;
|
||||
darwin/armv5) BINARIES="yaegi goexports" ;;
|
||||
darwin/armv6) BINARIES="yaegi goexports" ;;
|
||||
darwin/armv7) BINARIES="yaegi goexports" ;;
|
||||
freebsd/386) BINARIES="yaegi goexports" ;;
|
||||
freebsd/amd64) BINARIES="yaegi goexports" ;;
|
||||
freebsd/arm64) BINARIES="yaegi goexports" ;;
|
||||
freebsd/armv5) BINARIES="yaegi goexports" ;;
|
||||
freebsd/armv6) BINARIES="yaegi goexports" ;;
|
||||
freebsd/armv7) BINARIES="yaegi goexports" ;;
|
||||
linux/386) BINARIES="yaegi goexports" ;;
|
||||
linux/amd64) BINARIES="yaegi goexports" ;;
|
||||
linux/arm64) BINARIES="yaegi goexports" ;;
|
||||
linux/armv5) BINARIES="yaegi goexports" ;;
|
||||
linux/armv6) BINARIES="yaegi goexports" ;;
|
||||
linux/armv7) BINARIES="yaegi goexports" ;;
|
||||
openbsd/386) BINARIES="yaegi goexports" ;;
|
||||
openbsd/amd64) BINARIES="yaegi goexports" ;;
|
||||
openbsd/arm64) BINARIES="yaegi goexports" ;;
|
||||
openbsd/armv5) BINARIES="yaegi goexports" ;;
|
||||
openbsd/armv6) BINARIES="yaegi goexports" ;;
|
||||
openbsd/armv7) BINARIES="yaegi goexports" ;;
|
||||
solaris/386) BINARIES="yaegi goexports" ;;
|
||||
solaris/amd64) BINARIES="yaegi goexports" ;;
|
||||
solaris/arm64) BINARIES="yaegi goexports" ;;
|
||||
solaris/armv5) BINARIES="yaegi goexports" ;;
|
||||
solaris/armv6) BINARIES="yaegi goexports" ;;
|
||||
solaris/armv7) BINARIES="yaegi goexports" ;;
|
||||
*)
|
||||
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 -xzf "${tarball}" ;;
|
||||
*.tar) tar -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
|
||||
@@ -543,6 +543,12 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
|
||||
// which require and additional operation to set the value
|
||||
break
|
||||
}
|
||||
if dest.action == aGetIndex {
|
||||
// optimization does not work when assigning to a struct field. Maybe we're not
|
||||
// setting the right frame index or something, and we would end up not writing at
|
||||
// the right place. So disabling it for now.
|
||||
break
|
||||
}
|
||||
// Skip the assign operation entirely, the source frame index is set
|
||||
// to destination index, avoiding extra memory alloc and duplication.
|
||||
n.gen = nop
|
||||
@@ -2368,9 +2374,9 @@ func compositeGenerator(n *node, typ *itype) (gen bltnGenerator) {
|
||||
gen = compositeLitNotype
|
||||
case n.lastChild().kind == keyValueExpr:
|
||||
if n.nleft == 1 {
|
||||
gen = compositeSparse
|
||||
gen = compositeLitKeyed
|
||||
} else {
|
||||
gen = compositeSparseNotype
|
||||
gen = compositeLitKeyedNotype
|
||||
}
|
||||
default:
|
||||
if n.nleft == 1 {
|
||||
|
||||
@@ -60,7 +60,7 @@ type receiver struct {
|
||||
|
||||
// frame contains values for the current execution level (a function context).
|
||||
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.
|
||||
// Located at start of struct to ensure proper aligment.
|
||||
id uint64
|
||||
|
||||
138
interp/run.go
138
interp/run.go
@@ -1039,14 +1039,6 @@ func call(n *node) {
|
||||
}
|
||||
}
|
||||
|
||||
// pindex returns definition parameter index for function call.
|
||||
func pindex(i, variadic int) int {
|
||||
if variadic < 0 || i <= variadic {
|
||||
return i
|
||||
}
|
||||
return variadic
|
||||
}
|
||||
|
||||
func getFrame(f *frame, l int) *frame {
|
||||
switch l {
|
||||
case 0:
|
||||
@@ -1077,7 +1069,7 @@ func callBin(n *node) {
|
||||
// A method signature obtained from reflect.Type includes receiver as 1st arg, except for interface types.
|
||||
rcvrOffset := 0
|
||||
if recv := n.child[0].recv; recv != nil && !isInterface(recv.node.typ) {
|
||||
if funcType.NumIn() > len(child) {
|
||||
if variadic > 0 || funcType.NumIn() > len(child) {
|
||||
rcvrOffset = 1
|
||||
}
|
||||
}
|
||||
@@ -1089,7 +1081,13 @@ func callBin(n *node) {
|
||||
}
|
||||
|
||||
for i, c := range child {
|
||||
defType := funcType.In(rcvrOffset + pindex(i, variadic))
|
||||
var defType reflect.Type
|
||||
if variadic >= 0 && i >= variadic {
|
||||
defType = funcType.In(variadic)
|
||||
} else {
|
||||
defType = funcType.In(rcvrOffset + i)
|
||||
}
|
||||
|
||||
switch {
|
||||
case isBinCall(c):
|
||||
// Handle nested function calls: pass returned values as arguments
|
||||
@@ -2062,68 +2060,7 @@ func destType(n *node) *itype {
|
||||
}
|
||||
}
|
||||
|
||||
// doCompositeLit creates and populates a struct object.
|
||||
func doCompositeLit(n *node, hasType bool) {
|
||||
value := valueGenerator(n, n.findex)
|
||||
next := getExec(n.tnext)
|
||||
typ := n.typ
|
||||
if typ.cat == ptrT || typ.cat == aliasT {
|
||||
typ = typ.val
|
||||
}
|
||||
var mu sync.Mutex
|
||||
typ.mu = &mu
|
||||
child := n.child
|
||||
if hasType {
|
||||
child = n.child[1:]
|
||||
}
|
||||
destInterface := destType(n).cat == interfaceT
|
||||
|
||||
values := make([]func(*frame) reflect.Value, len(child))
|
||||
for i, c := range child {
|
||||
convertLiteralValue(c, typ.field[i].typ.TypeOf())
|
||||
switch {
|
||||
case c.typ.cat == funcT:
|
||||
values[i] = genFunctionWrapper(c)
|
||||
case isArray(c.typ) && c.typ.val != nil && c.typ.val.cat == interfaceT:
|
||||
values[i] = genValueInterfaceArray(c)
|
||||
case isRecursiveType(typ.field[i].typ, typ.field[i].typ.rtype):
|
||||
values[i] = genValueRecursiveInterface(c, typ.field[i].typ.rtype)
|
||||
case isInterface(typ.field[i].typ):
|
||||
values[i] = genInterfaceWrapper(c, typ.field[i].typ.rtype)
|
||||
default:
|
||||
values[i] = genValue(c)
|
||||
}
|
||||
}
|
||||
|
||||
i := n.findex
|
||||
l := n.level
|
||||
n.exec = func(f *frame) bltn {
|
||||
// TODO: it seems fishy that the typ might be modified post-compilation, and
|
||||
// hence that several goroutines might be using the same typ that they all modify.
|
||||
// We probably need to revisit that.
|
||||
typ.mu.Lock()
|
||||
a := reflect.New(typ.TypeOf()).Elem()
|
||||
typ.mu.Unlock()
|
||||
for i, v := range values {
|
||||
a.Field(i).Set(v(f))
|
||||
}
|
||||
switch d := value(f); {
|
||||
case d.Type().Kind() == reflect.Ptr:
|
||||
d.Set(a.Addr())
|
||||
case destInterface:
|
||||
d.Set(reflect.ValueOf(valueInterface{n, a}))
|
||||
default:
|
||||
getFrame(f, l).data[i] = a
|
||||
}
|
||||
return next
|
||||
}
|
||||
}
|
||||
|
||||
func compositeLit(n *node) { doCompositeLit(n, true) }
|
||||
func compositeLitNotype(n *node) { doCompositeLit(n, false) }
|
||||
|
||||
// doCompositeSparse creates a struct Object, filling fields from sparse key-values.
|
||||
func doCompositeSparse(n *node, hasType bool) {
|
||||
func doComposite(n *node, hasType bool, keyed bool) {
|
||||
value := valueGenerator(n, n.findex)
|
||||
next := getExec(n.tnext)
|
||||
typ := n.typ
|
||||
@@ -2139,27 +2076,37 @@ func doCompositeSparse(n *node, hasType bool) {
|
||||
destInterface := destType(n).cat == interfaceT
|
||||
|
||||
values := make(map[int]func(*frame) reflect.Value)
|
||||
for _, c := range child {
|
||||
c1 := c.child[1]
|
||||
field := typ.fieldIndex(c.child[0].ident)
|
||||
convertLiteralValue(c1, typ.field[field].typ.TypeOf())
|
||||
for i, c := range child {
|
||||
var val *node
|
||||
var fieldIndex int
|
||||
if keyed {
|
||||
val = c.child[1]
|
||||
fieldIndex = typ.fieldIndex(c.child[0].ident)
|
||||
} else {
|
||||
val = c
|
||||
fieldIndex = i
|
||||
}
|
||||
convertLiteralValue(val, typ.field[fieldIndex].typ.TypeOf())
|
||||
switch {
|
||||
case c1.typ.cat == funcT:
|
||||
values[field] = genFunctionWrapper(c1)
|
||||
case isArray(c1.typ) && c1.typ.val != nil && c1.typ.val.cat == interfaceT:
|
||||
values[field] = genValueInterfaceArray(c1)
|
||||
case isRecursiveType(typ.field[field].typ, typ.field[field].typ.rtype):
|
||||
values[field] = genValueRecursiveInterface(c1, typ.field[field].typ.rtype)
|
||||
case isInterface(typ.field[field].typ):
|
||||
values[field] = genInterfaceWrapper(c1, typ.field[field].typ.rtype)
|
||||
case val.typ.cat == funcT:
|
||||
values[fieldIndex] = genFunctionWrapper(val)
|
||||
case isArray(val.typ) && val.typ.val != nil && val.typ.val.cat == interfaceT:
|
||||
values[fieldIndex] = genValueInterfaceArray(val)
|
||||
case isRecursiveType(typ.field[fieldIndex].typ, typ.field[fieldIndex].typ.rtype):
|
||||
values[fieldIndex] = genValueRecursiveInterface(val, typ.field[fieldIndex].typ.rtype)
|
||||
case isInterface(typ.field[fieldIndex].typ):
|
||||
values[fieldIndex] = genInterfaceWrapper(val, typ.field[fieldIndex].typ.rtype)
|
||||
default:
|
||||
values[field] = genValue(c1)
|
||||
values[fieldIndex] = genValue(val)
|
||||
}
|
||||
}
|
||||
|
||||
frameIndex := n.findex
|
||||
l := n.level
|
||||
n.exec = func(f *frame) bltn {
|
||||
typ.mu.Lock()
|
||||
a, _ := typ.zero()
|
||||
// No need to call zero() as doComposite is only called for a structT
|
||||
a := reflect.New(typ.TypeOf()).Elem()
|
||||
typ.mu.Unlock()
|
||||
for i, v := range values {
|
||||
a.Field(i).Set(v(f))
|
||||
@@ -2171,14 +2118,27 @@ func doCompositeSparse(n *node, hasType bool) {
|
||||
case destInterface:
|
||||
d.Set(reflect.ValueOf(valueInterface{n, a}))
|
||||
default:
|
||||
d.Set(a)
|
||||
getFrame(f, l).data[frameIndex] = a
|
||||
}
|
||||
return next
|
||||
}
|
||||
}
|
||||
|
||||
func compositeSparse(n *node) { doCompositeSparse(n, true) }
|
||||
func compositeSparseNotype(n *node) { doCompositeSparse(n, false) }
|
||||
// doCompositeLit creates and populates a struct object.
|
||||
func doCompositeLit(n *node, hasType bool) {
|
||||
doComposite(n, hasType, false)
|
||||
}
|
||||
|
||||
func compositeLit(n *node) { doCompositeLit(n, true) }
|
||||
func compositeLitNotype(n *node) { doCompositeLit(n, false) }
|
||||
|
||||
// doCompositeLitKeyed creates a struct Object, filling fields from sparse key-values.
|
||||
func doCompositeLitKeyed(n *node, hasType bool) {
|
||||
doComposite(n, hasType, true)
|
||||
}
|
||||
|
||||
func compositeLitKeyed(n *node) { doCompositeLitKeyed(n, true) }
|
||||
func compositeLitKeyedNotype(n *node) { doCompositeLitKeyed(n, false) }
|
||||
|
||||
func empty(n *node) {}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user