2014-07-31 15:09:13 -04:00
|
|
|
package bind
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"flag"
|
|
|
|
"go/ast"
|
2016-09-07 18:27:36 +02:00
|
|
|
"go/build"
|
|
|
|
"go/importer"
|
2014-07-31 15:09:13 -04:00
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
2015-07-25 09:45:10 -04:00
|
|
|
"go/types"
|
2015-08-27 12:19:47 -04:00
|
|
|
"io"
|
2014-07-31 15:09:13 -04:00
|
|
|
"io/ioutil"
|
2015-06-05 15:24:08 -04:00
|
|
|
"log"
|
2015-01-06 01:01:15 -05:00
|
|
|
"os"
|
2014-07-31 15:09:13 -04:00
|
|
|
"os/exec"
|
2017-01-17 02:26:29 +01:00
|
|
|
"path"
|
2014-07-31 15:09:13 -04:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2016-09-07 18:27:36 +02:00
|
|
|
|
|
|
|
"golang.org/x/mobile/internal/importers"
|
|
|
|
"golang.org/x/mobile/internal/importers/java"
|
2016-09-13 06:05:52 +02:00
|
|
|
"golang.org/x/mobile/internal/importers/objc"
|
2014-07-31 15:09:13 -04:00
|
|
|
)
|
|
|
|
|
2015-06-05 15:24:08 -04:00
|
|
|
func init() {
|
|
|
|
log.SetFlags(log.Lshortfile)
|
|
|
|
}
|
|
|
|
|
2014-07-31 15:09:13 -04:00
|
|
|
var updateFlag = flag.Bool("update", false, "Update the golden files.")
|
|
|
|
|
|
|
|
var tests = []string{
|
2017-01-09 22:35:14 +01:00
|
|
|
"", // The universe package with the error type.
|
2014-07-31 15:09:13 -04:00
|
|
|
"testdata/basictypes.go",
|
|
|
|
"testdata/structs.go",
|
|
|
|
"testdata/interfaces.go",
|
2015-05-12 09:44:08 -04:00
|
|
|
"testdata/issue10788.go",
|
2015-08-25 16:39:49 -04:00
|
|
|
"testdata/issue12328.go",
|
2015-08-31 01:06:30 -04:00
|
|
|
"testdata/issue12403.go",
|
2019-01-25 19:23:34 +00:00
|
|
|
"testdata/issue29559.go",
|
2016-09-04 23:37:26 +03:00
|
|
|
"testdata/keywords.go",
|
2015-08-24 15:49:09 -04:00
|
|
|
"testdata/try.go",
|
2015-10-01 22:04:03 -04:00
|
|
|
"testdata/vars.go",
|
2016-03-13 15:15:56 +01:00
|
|
|
"testdata/ignore.go",
|
2017-07-31 16:13:01 +02:00
|
|
|
"testdata/doc.go",
|
2018-03-16 13:14:11 +01:00
|
|
|
"testdata/underscores.go",
|
2014-07-31 15:09:13 -04:00
|
|
|
}
|
|
|
|
|
2016-09-07 18:27:36 +02:00
|
|
|
var javaTests = []string{
|
|
|
|
"testdata/java.go",
|
2016-09-16 19:19:01 +02:00
|
|
|
"testdata/classes.go",
|
2016-09-07 18:27:36 +02:00
|
|
|
}
|
|
|
|
|
2016-09-13 06:05:52 +02:00
|
|
|
var objcTests = []string{
|
|
|
|
"testdata/objc.go",
|
2016-09-13 06:11:15 +02:00
|
|
|
"testdata/objcw.go",
|
2016-09-13 06:05:52 +02:00
|
|
|
}
|
|
|
|
|
2014-07-31 15:09:13 -04:00
|
|
|
var fset = token.NewFileSet()
|
|
|
|
|
2016-09-13 06:05:52 +02:00
|
|
|
func fileRefs(t *testing.T, filename string, pkgPrefix string) *importers.References {
|
2016-09-07 18:27:36 +02:00
|
|
|
f, err := parser.ParseFile(fset, filename, nil, parser.AllErrors)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s: %v", filename, err)
|
|
|
|
}
|
2016-09-13 06:05:52 +02:00
|
|
|
refs, err := importers.AnalyzeFile(f, pkgPrefix)
|
2016-09-07 18:27:36 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s: %v", filename, err)
|
|
|
|
}
|
2017-01-17 02:26:29 +01:00
|
|
|
fakePath := path.Dir(filename)
|
|
|
|
for i := range refs.Embedders {
|
|
|
|
refs.Embedders[i].PkgPath = fakePath
|
|
|
|
}
|
2016-09-07 18:27:36 +02:00
|
|
|
return refs
|
|
|
|
}
|
|
|
|
|
2017-08-28 19:53:40 +02:00
|
|
|
func typeCheck(t *testing.T, filename string, gopath string) (*types.Package, *ast.File) {
|
2017-07-31 16:13:01 +02:00
|
|
|
f, err := parser.ParseFile(fset, filename, nil, parser.AllErrors|parser.ParseComments)
|
2014-07-31 15:09:13 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s: %v", filename, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pkgName := filepath.Base(filename)
|
|
|
|
pkgName = strings.TrimSuffix(pkgName, ".go")
|
|
|
|
|
|
|
|
// typecheck and collect typechecker errors
|
|
|
|
var conf types.Config
|
|
|
|
conf.Error = func(err error) {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2016-09-07 18:27:36 +02:00
|
|
|
if gopath != "" {
|
|
|
|
conf.Importer = importer.Default()
|
|
|
|
oldDefault := build.Default
|
|
|
|
defer func() { build.Default = oldDefault }()
|
|
|
|
build.Default.GOPATH = gopath
|
|
|
|
}
|
2014-07-31 15:09:13 -04:00
|
|
|
pkg, err := conf.Check(pkgName, fset, []*ast.File{f}, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-08-28 19:53:40 +02:00
|
|
|
return pkg, f
|
2014-07-31 15:09:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// diff runs the command "diff a b" and returns its output
|
|
|
|
func diff(a, b string) string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
var cmd *exec.Cmd
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "plan9":
|
|
|
|
cmd = exec.Command("/bin/diff", "-c", a, b)
|
|
|
|
default:
|
|
|
|
cmd = exec.Command("/usr/bin/diff", "-u", a, b)
|
|
|
|
}
|
|
|
|
cmd.Stdout = &buf
|
|
|
|
cmd.Stderr = &buf
|
|
|
|
cmd.Run()
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeTempFile(t *testing.T, name string, contents []byte) string {
|
|
|
|
f, err := ioutil.TempFile("", name)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := f.Write(contents); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return f.Name()
|
|
|
|
}
|
|
|
|
|
2015-06-05 15:24:08 -04:00
|
|
|
func TestGenObjc(t *testing.T) {
|
|
|
|
for _, filename := range tests {
|
2017-01-09 22:35:14 +01:00
|
|
|
var pkg *types.Package
|
2017-08-28 19:53:40 +02:00
|
|
|
var file *ast.File
|
2017-01-09 22:35:14 +01:00
|
|
|
if filename != "" {
|
2017-08-28 19:53:40 +02:00
|
|
|
pkg, file = typeCheck(t, filename, "")
|
2017-01-09 22:35:14 +01:00
|
|
|
}
|
2015-06-05 15:24:08 -04:00
|
|
|
|
2016-09-13 06:05:52 +02:00
|
|
|
var buf bytes.Buffer
|
|
|
|
g := &ObjcGen{
|
|
|
|
Generator: &Generator{
|
|
|
|
Printer: &Printer{Buf: &buf, IndentEach: []byte("\t")},
|
|
|
|
Fset: fset,
|
2017-08-28 19:53:40 +02:00
|
|
|
Files: []*ast.File{file},
|
2016-09-13 06:05:52 +02:00
|
|
|
Pkg: pkg,
|
|
|
|
},
|
|
|
|
}
|
2017-01-09 22:35:14 +01:00
|
|
|
if pkg != nil {
|
|
|
|
g.AllPkg = []*types.Package{pkg}
|
|
|
|
}
|
2016-09-13 06:11:15 +02:00
|
|
|
g.Init(nil)
|
2016-09-13 06:05:52 +02:00
|
|
|
|
|
|
|
testcases := []struct {
|
|
|
|
suffix string
|
|
|
|
gen func() error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
".objc.h.golden",
|
|
|
|
g.GenH,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
".objc.m.golden",
|
|
|
|
g.GenM,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
".objc.go.h.golden",
|
|
|
|
g.GenGoH,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testcases {
|
|
|
|
buf.Reset()
|
|
|
|
if err := tc.gen(); err != nil {
|
2015-06-05 15:24:08 -04:00
|
|
|
t.Errorf("%s: %v", filename, err)
|
|
|
|
continue
|
|
|
|
}
|
2016-09-13 06:05:52 +02:00
|
|
|
out := writeTempFile(t, "generated"+tc.suffix, buf.Bytes())
|
2015-06-05 15:24:08 -04:00
|
|
|
defer os.Remove(out)
|
2017-01-09 22:35:14 +01:00
|
|
|
var golden string
|
|
|
|
if filename != "" {
|
|
|
|
golden = filename[:len(filename)-len(".go")]
|
|
|
|
} else {
|
|
|
|
golden = "testdata/universe"
|
|
|
|
}
|
|
|
|
golden += tc.suffix
|
2015-06-05 15:24:08 -04:00
|
|
|
if diffstr := diff(golden, out); diffstr != "" {
|
|
|
|
t.Errorf("%s: does not match Objective-C golden:\n%s", filename, diffstr)
|
|
|
|
if *updateFlag {
|
|
|
|
t.Logf("Updating %s...", golden)
|
|
|
|
err := exec.Command("/bin/cp", out, golden).Run()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Update failed: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-02 21:51:27 +01:00
|
|
|
func genObjcPackages(t *testing.T, dir string, cg *ObjcWrapper) {
|
2016-09-13 06:05:52 +02:00
|
|
|
pkgBase := filepath.Join(dir, "src", "ObjC")
|
|
|
|
if err := os.MkdirAll(pkgBase, 0700); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for i, jpkg := range cg.Packages() {
|
|
|
|
pkgDir := filepath.Join(pkgBase, jpkg)
|
|
|
|
if err := os.MkdirAll(pkgDir, 0700); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
pkgFile := filepath.Join(pkgDir, "package.go")
|
2017-01-02 21:51:27 +01:00
|
|
|
cg.Buf.Reset()
|
2016-09-13 06:05:52 +02:00
|
|
|
cg.GenPackage(i)
|
2017-01-02 21:51:27 +01:00
|
|
|
if err := ioutil.WriteFile(pkgFile, cg.Buf.Bytes(), 0600); err != nil {
|
2016-09-13 06:05:52 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2017-01-02 21:51:27 +01:00
|
|
|
cg.Buf.Reset()
|
2016-09-13 06:05:52 +02:00
|
|
|
cg.GenInterfaces()
|
|
|
|
clsFile := filepath.Join(pkgBase, "interfaces.go")
|
2017-01-02 21:51:27 +01:00
|
|
|
if err := ioutil.WriteFile(clsFile, cg.Buf.Bytes(), 0600); err != nil {
|
2016-09-13 06:05:52 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-08-23 18:54:58 +09:00
|
|
|
gocmd := filepath.Join(runtime.GOROOT(), "bin", "go")
|
2016-09-13 06:05:52 +02:00
|
|
|
cmd := exec.Command(
|
2019-08-23 18:54:58 +09:00
|
|
|
gocmd,
|
2016-09-13 06:05:52 +02:00
|
|
|
"install",
|
|
|
|
"-pkgdir="+filepath.Join(dir, "pkg", build.Default.GOOS+"_"+build.Default.GOARCH),
|
|
|
|
"ObjC/...",
|
|
|
|
)
|
2019-03-12 12:48:10 -04:00
|
|
|
cmd.Env = append(os.Environ(), "GOPATH="+dir, "GO111MODULE=off")
|
2016-09-13 06:05:52 +02:00
|
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
|
|
t.Fatalf("failed to go install the generated ObjC wrappers: %v: %s", err, string(out))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
bind,cmd,internal: generate reverse bindings for exported Go structs
Before this CL, the type of the implicit "this" parameter to Java methods
implemented in Go could only be a super class of the generated Java
class. For example, the following GoRunnable type is an implementation of
the Java interface java.lang.Runnable with a toString method:
package somepkg
import "Java/java/lang"
type GoRunnable struct {
lang.Runnable
}
func (r *GoRunnable) ToString(this lang.Runnable) string {
...
}
The "this" parameter is implicit in the sense that the reverse generator
automatically fills it with a reference to the Java instance of
GoRunnable.
Note that "this" has the type Java/java/lang.Runnable, not
Java/go/somepkg.GoRunnable, which renders it impossible to call Java
methods and functions that expect GoRunnable. The most practical example
of this is the Android databinding libraries.
This CL changes the implicit this parameter to always match the exact
type. In the example, the toString implementation becomes:
import gopkg "Java/go/somepkg"
func (r *GoRunnable) ToString(this gopkg.GoRunnable) string {
...
}
One strategy would be to simply treat the generated Java classes
(GoRunnable in our example) as any other Java class and import it
through javap. However, since the Java classes are generated after
importing, this present a chicken-and-egg problem.
Instead, use the newly added support for structs with embedded prefixed types
and synthesize class descriptors for every exported Go struct type.
Change-Id: Ic5ce4a151312bd89f91798ed4088c9959225b448
Reviewed-on: https://go-review.googlesource.com/34776
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-12-31 16:41:36 +01:00
|
|
|
func genJavaPackages(t *testing.T, dir string, cg *ClassGen) {
|
2017-01-09 22:35:14 +01:00
|
|
|
buf := cg.Buf
|
|
|
|
cg.Buf = new(bytes.Buffer)
|
2016-09-07 18:27:36 +02:00
|
|
|
pkgBase := filepath.Join(dir, "src", "Java")
|
|
|
|
if err := os.MkdirAll(pkgBase, 0700); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for i, jpkg := range cg.Packages() {
|
|
|
|
pkgDir := filepath.Join(pkgBase, jpkg)
|
|
|
|
if err := os.MkdirAll(pkgDir, 0700); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
pkgFile := filepath.Join(pkgDir, "package.go")
|
bind,cmd,internal: generate reverse bindings for exported Go structs
Before this CL, the type of the implicit "this" parameter to Java methods
implemented in Go could only be a super class of the generated Java
class. For example, the following GoRunnable type is an implementation of
the Java interface java.lang.Runnable with a toString method:
package somepkg
import "Java/java/lang"
type GoRunnable struct {
lang.Runnable
}
func (r *GoRunnable) ToString(this lang.Runnable) string {
...
}
The "this" parameter is implicit in the sense that the reverse generator
automatically fills it with a reference to the Java instance of
GoRunnable.
Note that "this" has the type Java/java/lang.Runnable, not
Java/go/somepkg.GoRunnable, which renders it impossible to call Java
methods and functions that expect GoRunnable. The most practical example
of this is the Android databinding libraries.
This CL changes the implicit this parameter to always match the exact
type. In the example, the toString implementation becomes:
import gopkg "Java/go/somepkg"
func (r *GoRunnable) ToString(this gopkg.GoRunnable) string {
...
}
One strategy would be to simply treat the generated Java classes
(GoRunnable in our example) as any other Java class and import it
through javap. However, since the Java classes are generated after
importing, this present a chicken-and-egg problem.
Instead, use the newly added support for structs with embedded prefixed types
and synthesize class descriptors for every exported Go struct type.
Change-Id: Ic5ce4a151312bd89f91798ed4088c9959225b448
Reviewed-on: https://go-review.googlesource.com/34776
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-12-31 16:41:36 +01:00
|
|
|
cg.Buf.Reset()
|
2016-09-07 18:27:36 +02:00
|
|
|
cg.GenPackage(i)
|
bind,cmd,internal: generate reverse bindings for exported Go structs
Before this CL, the type of the implicit "this" parameter to Java methods
implemented in Go could only be a super class of the generated Java
class. For example, the following GoRunnable type is an implementation of
the Java interface java.lang.Runnable with a toString method:
package somepkg
import "Java/java/lang"
type GoRunnable struct {
lang.Runnable
}
func (r *GoRunnable) ToString(this lang.Runnable) string {
...
}
The "this" parameter is implicit in the sense that the reverse generator
automatically fills it with a reference to the Java instance of
GoRunnable.
Note that "this" has the type Java/java/lang.Runnable, not
Java/go/somepkg.GoRunnable, which renders it impossible to call Java
methods and functions that expect GoRunnable. The most practical example
of this is the Android databinding libraries.
This CL changes the implicit this parameter to always match the exact
type. In the example, the toString implementation becomes:
import gopkg "Java/go/somepkg"
func (r *GoRunnable) ToString(this gopkg.GoRunnable) string {
...
}
One strategy would be to simply treat the generated Java classes
(GoRunnable in our example) as any other Java class and import it
through javap. However, since the Java classes are generated after
importing, this present a chicken-and-egg problem.
Instead, use the newly added support for structs with embedded prefixed types
and synthesize class descriptors for every exported Go struct type.
Change-Id: Ic5ce4a151312bd89f91798ed4088c9959225b448
Reviewed-on: https://go-review.googlesource.com/34776
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-12-31 16:41:36 +01:00
|
|
|
if err := ioutil.WriteFile(pkgFile, cg.Buf.Bytes(), 0600); err != nil {
|
2016-09-07 18:27:36 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-01-09 22:35:14 +01:00
|
|
|
io.Copy(buf, cg.Buf)
|
2016-09-07 18:27:36 +02:00
|
|
|
}
|
bind,cmd,internal: generate reverse bindings for exported Go structs
Before this CL, the type of the implicit "this" parameter to Java methods
implemented in Go could only be a super class of the generated Java
class. For example, the following GoRunnable type is an implementation of
the Java interface java.lang.Runnable with a toString method:
package somepkg
import "Java/java/lang"
type GoRunnable struct {
lang.Runnable
}
func (r *GoRunnable) ToString(this lang.Runnable) string {
...
}
The "this" parameter is implicit in the sense that the reverse generator
automatically fills it with a reference to the Java instance of
GoRunnable.
Note that "this" has the type Java/java/lang.Runnable, not
Java/go/somepkg.GoRunnable, which renders it impossible to call Java
methods and functions that expect GoRunnable. The most practical example
of this is the Android databinding libraries.
This CL changes the implicit this parameter to always match the exact
type. In the example, the toString implementation becomes:
import gopkg "Java/go/somepkg"
func (r *GoRunnable) ToString(this gopkg.GoRunnable) string {
...
}
One strategy would be to simply treat the generated Java classes
(GoRunnable in our example) as any other Java class and import it
through javap. However, since the Java classes are generated after
importing, this present a chicken-and-egg problem.
Instead, use the newly added support for structs with embedded prefixed types
and synthesize class descriptors for every exported Go struct type.
Change-Id: Ic5ce4a151312bd89f91798ed4088c9959225b448
Reviewed-on: https://go-review.googlesource.com/34776
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-12-31 16:41:36 +01:00
|
|
|
cg.Buf.Reset()
|
2016-09-07 18:27:36 +02:00
|
|
|
cg.GenInterfaces()
|
|
|
|
clsFile := filepath.Join(pkgBase, "interfaces.go")
|
bind,cmd,internal: generate reverse bindings for exported Go structs
Before this CL, the type of the implicit "this" parameter to Java methods
implemented in Go could only be a super class of the generated Java
class. For example, the following GoRunnable type is an implementation of
the Java interface java.lang.Runnable with a toString method:
package somepkg
import "Java/java/lang"
type GoRunnable struct {
lang.Runnable
}
func (r *GoRunnable) ToString(this lang.Runnable) string {
...
}
The "this" parameter is implicit in the sense that the reverse generator
automatically fills it with a reference to the Java instance of
GoRunnable.
Note that "this" has the type Java/java/lang.Runnable, not
Java/go/somepkg.GoRunnable, which renders it impossible to call Java
methods and functions that expect GoRunnable. The most practical example
of this is the Android databinding libraries.
This CL changes the implicit this parameter to always match the exact
type. In the example, the toString implementation becomes:
import gopkg "Java/go/somepkg"
func (r *GoRunnable) ToString(this gopkg.GoRunnable) string {
...
}
One strategy would be to simply treat the generated Java classes
(GoRunnable in our example) as any other Java class and import it
through javap. However, since the Java classes are generated after
importing, this present a chicken-and-egg problem.
Instead, use the newly added support for structs with embedded prefixed types
and synthesize class descriptors for every exported Go struct type.
Change-Id: Ic5ce4a151312bd89f91798ed4088c9959225b448
Reviewed-on: https://go-review.googlesource.com/34776
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-12-31 16:41:36 +01:00
|
|
|
if err := ioutil.WriteFile(clsFile, cg.Buf.Bytes(), 0600); err != nil {
|
2016-09-07 18:27:36 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-01-09 22:35:14 +01:00
|
|
|
io.Copy(buf, cg.Buf)
|
|
|
|
cg.Buf = buf
|
2016-09-07 18:27:36 +02:00
|
|
|
|
2019-08-23 18:54:58 +09:00
|
|
|
gocmd := filepath.Join(runtime.GOROOT(), "bin", "go")
|
2016-09-07 18:27:36 +02:00
|
|
|
cmd := exec.Command(
|
2019-08-23 18:54:58 +09:00
|
|
|
gocmd,
|
2016-09-07 18:27:36 +02:00
|
|
|
"install",
|
|
|
|
"-pkgdir="+filepath.Join(dir, "pkg", build.Default.GOOS+"_"+build.Default.GOARCH),
|
|
|
|
"Java/...",
|
|
|
|
)
|
2019-03-12 12:48:10 -04:00
|
|
|
cmd.Env = append(os.Environ(), "GOPATH="+dir, "GO111MODULE=off")
|
2016-09-07 18:27:36 +02:00
|
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
|
|
t.Fatalf("failed to go install the generated Java wrappers: %v: %s", err, string(out))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-31 15:09:13 -04:00
|
|
|
func TestGenJava(t *testing.T) {
|
2016-09-07 18:27:36 +02:00
|
|
|
allTests := tests
|
|
|
|
if java.IsAvailable() {
|
|
|
|
allTests = append(append([]string{}, allTests...), javaTests...)
|
|
|
|
}
|
|
|
|
for _, filename := range allTests {
|
2017-01-09 22:35:14 +01:00
|
|
|
var pkg *types.Package
|
2017-08-28 19:53:40 +02:00
|
|
|
var file *ast.File
|
2016-08-20 21:10:46 +02:00
|
|
|
var buf bytes.Buffer
|
2017-01-09 22:35:14 +01:00
|
|
|
var cg *ClassGen
|
|
|
|
var classes []*java.Class
|
|
|
|
if filename != "" {
|
|
|
|
refs := fileRefs(t, filename, "Java/")
|
|
|
|
imp := &java.Importer{}
|
|
|
|
var err error
|
|
|
|
classes, err = imp.Import(refs)
|
2016-09-07 18:27:36 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-01-09 22:35:14 +01:00
|
|
|
tmpGopath := ""
|
|
|
|
if len(classes) > 0 {
|
|
|
|
tmpGopath, err = ioutil.TempDir(os.TempDir(), "gomobile-bind-test-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpGopath)
|
|
|
|
cg = &ClassGen{
|
|
|
|
Printer: &Printer{
|
|
|
|
IndentEach: []byte("\t"),
|
|
|
|
Buf: new(bytes.Buffer),
|
|
|
|
},
|
|
|
|
}
|
2017-01-17 02:26:29 +01:00
|
|
|
cg.Init(classes, refs.Embedders)
|
2017-01-09 22:35:14 +01:00
|
|
|
genJavaPackages(t, tmpGopath, cg)
|
|
|
|
cg.Buf = &buf
|
bind,cmd,internal: generate reverse bindings for exported Go structs
Before this CL, the type of the implicit "this" parameter to Java methods
implemented in Go could only be a super class of the generated Java
class. For example, the following GoRunnable type is an implementation of
the Java interface java.lang.Runnable with a toString method:
package somepkg
import "Java/java/lang"
type GoRunnable struct {
lang.Runnable
}
func (r *GoRunnable) ToString(this lang.Runnable) string {
...
}
The "this" parameter is implicit in the sense that the reverse generator
automatically fills it with a reference to the Java instance of
GoRunnable.
Note that "this" has the type Java/java/lang.Runnable, not
Java/go/somepkg.GoRunnable, which renders it impossible to call Java
methods and functions that expect GoRunnable. The most practical example
of this is the Android databinding libraries.
This CL changes the implicit this parameter to always match the exact
type. In the example, the toString implementation becomes:
import gopkg "Java/go/somepkg"
func (r *GoRunnable) ToString(this gopkg.GoRunnable) string {
...
}
One strategy would be to simply treat the generated Java classes
(GoRunnable in our example) as any other Java class and import it
through javap. However, since the Java classes are generated after
importing, this present a chicken-and-egg problem.
Instead, use the newly added support for structs with embedded prefixed types
and synthesize class descriptors for every exported Go struct type.
Change-Id: Ic5ce4a151312bd89f91798ed4088c9959225b448
Reviewed-on: https://go-review.googlesource.com/34776
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-12-31 16:41:36 +01:00
|
|
|
}
|
2017-08-28 19:53:40 +02:00
|
|
|
pkg, file = typeCheck(t, filename, tmpGopath)
|
2016-09-07 18:27:36 +02:00
|
|
|
}
|
2016-08-20 21:10:46 +02:00
|
|
|
g := &JavaGen{
|
|
|
|
Generator: &Generator{
|
|
|
|
Printer: &Printer{Buf: &buf, IndentEach: []byte(" ")},
|
|
|
|
Fset: fset,
|
2017-08-28 19:53:40 +02:00
|
|
|
Files: []*ast.File{file},
|
2016-08-20 21:10:46 +02:00
|
|
|
Pkg: pkg,
|
|
|
|
},
|
|
|
|
}
|
2017-01-09 22:35:14 +01:00
|
|
|
if pkg != nil {
|
|
|
|
g.AllPkg = []*types.Package{pkg}
|
|
|
|
}
|
2016-09-16 19:19:01 +02:00
|
|
|
g.Init(classes)
|
2016-08-20 21:10:46 +02:00
|
|
|
testCases := []struct {
|
|
|
|
suffix string
|
|
|
|
gen func() error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
".java.golden",
|
|
|
|
func() error {
|
|
|
|
for i := range g.ClassNames() {
|
|
|
|
if err := g.GenClass(i); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return g.GenJava()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
".java.c.golden",
|
2016-09-07 18:27:36 +02:00
|
|
|
func() error {
|
|
|
|
if cg != nil {
|
|
|
|
cg.GenC()
|
|
|
|
}
|
|
|
|
return g.GenC()
|
|
|
|
},
|
2016-08-20 21:10:46 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
".java.h.golden",
|
2016-09-07 18:27:36 +02:00
|
|
|
func() error {
|
|
|
|
if cg != nil {
|
|
|
|
cg.GenH()
|
|
|
|
}
|
|
|
|
return g.GenH()
|
|
|
|
},
|
2016-08-20 21:10:46 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
buf.Reset()
|
|
|
|
if err := tc.gen(); err != nil {
|
mobile/bind: replace seq serialization with direct calls
The seq serialization machinery is a historic artifact from when Go
mobile code had to run in a separate process. Now that Go code is running
in-process, replace the explicit serialization with direct calls and pass
arguments on the stack.
The benefits are a much smaller bind runtime, much less garbage (and, in
Java, fewer objects with finalizers), less argument copying, and faster
cross-language calls.
The cost is a more complex generator, because some of the work from the
bind runtime is moved to generated code. Generated code now handles
conversion between Go and Java/ObjC types, multiple return values and memory
management of byte slice and string arguments.
To overcome the lack of calling C code between Go packages, all bound
packages now end up in the same (fake) package, "gomobile_bind", instead of
separate packages (go_<pkgname>). To avoid name clashes, the package name is
added as a prefix to generated functions and types.
Also, don't copy byte arrays passed to Go, saving call time and
allowing read([]byte)-style interfaces to foreign callers (#12113).
Finally, add support for nil interfaces and struct pointers to objc.
This is a large CL, but most of the changes stem from changing testdata.
The full benchcmp output on the CL/20095 benchmarks on my Nexus 5 is
reproduced below. Note that the savings for the JavaSlice* benchmarks are
skewed because byte slices are no longer copied before passing them to Go.
benchmark old ns/op new ns/op delta
BenchmarkJavaEmpty 26.0 19.0 -26.92%
BenchmarkJavaEmptyDirect 23.0 22.0 -4.35%
BenchmarkJavaNoargs 7685 2339 -69.56%
BenchmarkJavaNoargsDirect 17405 8041 -53.80%
BenchmarkJavaOnearg 26887 2366 -91.20%
BenchmarkJavaOneargDirect 34266 7910 -76.92%
BenchmarkJavaOneret 38325 2245 -94.14%
BenchmarkJavaOneretDirect 46265 7708 -83.34%
BenchmarkJavaManyargs 41720 2535 -93.92%
BenchmarkJavaManyargsDirect 51026 8373 -83.59%
BenchmarkJavaRefjava 38139 21260 -44.26%
BenchmarkJavaRefjavaDirect 42706 28150 -34.08%
BenchmarkJavaRefgo 34403 6843 -80.11%
BenchmarkJavaRefgoDirect 40193 16582 -58.74%
BenchmarkJavaStringShort 32366 9323 -71.20%
BenchmarkJavaStringShortDirect 41973 19118 -54.45%
BenchmarkJavaStringLong 127879 94420 -26.16%
BenchmarkJavaStringLongDirect 133776 114760 -14.21%
BenchmarkJavaStringShortUnicode 32562 9221 -71.68%
BenchmarkJavaStringShortUnicodeDirect 41464 19094 -53.95%
BenchmarkJavaStringLongUnicode 131015 89401 -31.76%
BenchmarkJavaStringLongUnicodeDirect 134130 90786 -32.31%
BenchmarkJavaSliceShort 42462 7538 -82.25%
BenchmarkJavaSliceShortDirect 52940 17017 -67.86%
BenchmarkJavaSliceLong 138391 8466 -93.88%
BenchmarkJavaSliceLongDirect 205804 15666 -92.39%
BenchmarkGoEmpty 3.00 3.00 +0.00%
BenchmarkGoEmptyDirect 3.00 3.00 +0.00%
BenchmarkGoNoarg 40342 13716 -66.00%
BenchmarkGoNoargDirect 46691 13569 -70.94%
BenchmarkGoOnearg 43529 13757 -68.40%
BenchmarkGoOneargDirect 44867 14078 -68.62%
BenchmarkGoOneret 45456 13559 -70.17%
BenchmarkGoOneretDirect 44694 13442 -69.92%
BenchmarkGoRefjava 55111 28071 -49.06%
BenchmarkGoRefjavaDirect 60883 26872 -55.86%
BenchmarkGoRefgo 57038 29223 -48.77%
BenchmarkGoRefgoDirect 56153 27812 -50.47%
BenchmarkGoManyargs 67967 17398 -74.40%
BenchmarkGoManyargsDirect 60617 16998 -71.96%
BenchmarkGoStringShort 57538 22600 -60.72%
BenchmarkGoStringShortDirect 52627 22704 -56.86%
BenchmarkGoStringLong 128485 52530 -59.12%
BenchmarkGoStringLongDirect 138377 52079 -62.36%
BenchmarkGoStringShortUnicode 57062 22994 -59.70%
BenchmarkGoStringShortUnicodeDirect 62563 22938 -63.34%
BenchmarkGoStringLongUnicode 139913 55553 -60.29%
BenchmarkGoStringLongUnicodeDirect 150863 57791 -61.69%
BenchmarkGoSliceShort 59279 20215 -65.90%
BenchmarkGoSliceShortDirect 60160 21136 -64.87%
BenchmarkGoSliceLong 411225 301870 -26.59%
BenchmarkGoSliceLongDirect 399029 298915 -25.09%
Fixes golang/go#12619
Fixes golang/go#12113
Fixes golang/go#13033
Change-Id: I2b45e9e98a1248e3c23a5137f775f7364908bec7
Reviewed-on: https://go-review.googlesource.com/19821
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-02-12 18:50:33 +01:00
|
|
|
t.Errorf("%s: %v", filename, err)
|
|
|
|
continue
|
|
|
|
}
|
2016-08-20 21:10:46 +02:00
|
|
|
out := writeTempFile(t, "generated"+tc.suffix, buf.Bytes())
|
mobile/bind: replace seq serialization with direct calls
The seq serialization machinery is a historic artifact from when Go
mobile code had to run in a separate process. Now that Go code is running
in-process, replace the explicit serialization with direct calls and pass
arguments on the stack.
The benefits are a much smaller bind runtime, much less garbage (and, in
Java, fewer objects with finalizers), less argument copying, and faster
cross-language calls.
The cost is a more complex generator, because some of the work from the
bind runtime is moved to generated code. Generated code now handles
conversion between Go and Java/ObjC types, multiple return values and memory
management of byte slice and string arguments.
To overcome the lack of calling C code between Go packages, all bound
packages now end up in the same (fake) package, "gomobile_bind", instead of
separate packages (go_<pkgname>). To avoid name clashes, the package name is
added as a prefix to generated functions and types.
Also, don't copy byte arrays passed to Go, saving call time and
allowing read([]byte)-style interfaces to foreign callers (#12113).
Finally, add support for nil interfaces and struct pointers to objc.
This is a large CL, but most of the changes stem from changing testdata.
The full benchcmp output on the CL/20095 benchmarks on my Nexus 5 is
reproduced below. Note that the savings for the JavaSlice* benchmarks are
skewed because byte slices are no longer copied before passing them to Go.
benchmark old ns/op new ns/op delta
BenchmarkJavaEmpty 26.0 19.0 -26.92%
BenchmarkJavaEmptyDirect 23.0 22.0 -4.35%
BenchmarkJavaNoargs 7685 2339 -69.56%
BenchmarkJavaNoargsDirect 17405 8041 -53.80%
BenchmarkJavaOnearg 26887 2366 -91.20%
BenchmarkJavaOneargDirect 34266 7910 -76.92%
BenchmarkJavaOneret 38325 2245 -94.14%
BenchmarkJavaOneretDirect 46265 7708 -83.34%
BenchmarkJavaManyargs 41720 2535 -93.92%
BenchmarkJavaManyargsDirect 51026 8373 -83.59%
BenchmarkJavaRefjava 38139 21260 -44.26%
BenchmarkJavaRefjavaDirect 42706 28150 -34.08%
BenchmarkJavaRefgo 34403 6843 -80.11%
BenchmarkJavaRefgoDirect 40193 16582 -58.74%
BenchmarkJavaStringShort 32366 9323 -71.20%
BenchmarkJavaStringShortDirect 41973 19118 -54.45%
BenchmarkJavaStringLong 127879 94420 -26.16%
BenchmarkJavaStringLongDirect 133776 114760 -14.21%
BenchmarkJavaStringShortUnicode 32562 9221 -71.68%
BenchmarkJavaStringShortUnicodeDirect 41464 19094 -53.95%
BenchmarkJavaStringLongUnicode 131015 89401 -31.76%
BenchmarkJavaStringLongUnicodeDirect 134130 90786 -32.31%
BenchmarkJavaSliceShort 42462 7538 -82.25%
BenchmarkJavaSliceShortDirect 52940 17017 -67.86%
BenchmarkJavaSliceLong 138391 8466 -93.88%
BenchmarkJavaSliceLongDirect 205804 15666 -92.39%
BenchmarkGoEmpty 3.00 3.00 +0.00%
BenchmarkGoEmptyDirect 3.00 3.00 +0.00%
BenchmarkGoNoarg 40342 13716 -66.00%
BenchmarkGoNoargDirect 46691 13569 -70.94%
BenchmarkGoOnearg 43529 13757 -68.40%
BenchmarkGoOneargDirect 44867 14078 -68.62%
BenchmarkGoOneret 45456 13559 -70.17%
BenchmarkGoOneretDirect 44694 13442 -69.92%
BenchmarkGoRefjava 55111 28071 -49.06%
BenchmarkGoRefjavaDirect 60883 26872 -55.86%
BenchmarkGoRefgo 57038 29223 -48.77%
BenchmarkGoRefgoDirect 56153 27812 -50.47%
BenchmarkGoManyargs 67967 17398 -74.40%
BenchmarkGoManyargsDirect 60617 16998 -71.96%
BenchmarkGoStringShort 57538 22600 -60.72%
BenchmarkGoStringShortDirect 52627 22704 -56.86%
BenchmarkGoStringLong 128485 52530 -59.12%
BenchmarkGoStringLongDirect 138377 52079 -62.36%
BenchmarkGoStringShortUnicode 57062 22994 -59.70%
BenchmarkGoStringShortUnicodeDirect 62563 22938 -63.34%
BenchmarkGoStringLongUnicode 139913 55553 -60.29%
BenchmarkGoStringLongUnicodeDirect 150863 57791 -61.69%
BenchmarkGoSliceShort 59279 20215 -65.90%
BenchmarkGoSliceShortDirect 60160 21136 -64.87%
BenchmarkGoSliceLong 411225 301870 -26.59%
BenchmarkGoSliceLongDirect 399029 298915 -25.09%
Fixes golang/go#12619
Fixes golang/go#12113
Fixes golang/go#13033
Change-Id: I2b45e9e98a1248e3c23a5137f775f7364908bec7
Reviewed-on: https://go-review.googlesource.com/19821
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-02-12 18:50:33 +01:00
|
|
|
defer os.Remove(out)
|
2017-01-09 22:35:14 +01:00
|
|
|
var golden string
|
|
|
|
if filename != "" {
|
|
|
|
golden = filename[:len(filename)-len(".go")]
|
|
|
|
} else {
|
|
|
|
golden = "testdata/universe"
|
|
|
|
}
|
|
|
|
golden += tc.suffix
|
mobile/bind: replace seq serialization with direct calls
The seq serialization machinery is a historic artifact from when Go
mobile code had to run in a separate process. Now that Go code is running
in-process, replace the explicit serialization with direct calls and pass
arguments on the stack.
The benefits are a much smaller bind runtime, much less garbage (and, in
Java, fewer objects with finalizers), less argument copying, and faster
cross-language calls.
The cost is a more complex generator, because some of the work from the
bind runtime is moved to generated code. Generated code now handles
conversion between Go and Java/ObjC types, multiple return values and memory
management of byte slice and string arguments.
To overcome the lack of calling C code between Go packages, all bound
packages now end up in the same (fake) package, "gomobile_bind", instead of
separate packages (go_<pkgname>). To avoid name clashes, the package name is
added as a prefix to generated functions and types.
Also, don't copy byte arrays passed to Go, saving call time and
allowing read([]byte)-style interfaces to foreign callers (#12113).
Finally, add support for nil interfaces and struct pointers to objc.
This is a large CL, but most of the changes stem from changing testdata.
The full benchcmp output on the CL/20095 benchmarks on my Nexus 5 is
reproduced below. Note that the savings for the JavaSlice* benchmarks are
skewed because byte slices are no longer copied before passing them to Go.
benchmark old ns/op new ns/op delta
BenchmarkJavaEmpty 26.0 19.0 -26.92%
BenchmarkJavaEmptyDirect 23.0 22.0 -4.35%
BenchmarkJavaNoargs 7685 2339 -69.56%
BenchmarkJavaNoargsDirect 17405 8041 -53.80%
BenchmarkJavaOnearg 26887 2366 -91.20%
BenchmarkJavaOneargDirect 34266 7910 -76.92%
BenchmarkJavaOneret 38325 2245 -94.14%
BenchmarkJavaOneretDirect 46265 7708 -83.34%
BenchmarkJavaManyargs 41720 2535 -93.92%
BenchmarkJavaManyargsDirect 51026 8373 -83.59%
BenchmarkJavaRefjava 38139 21260 -44.26%
BenchmarkJavaRefjavaDirect 42706 28150 -34.08%
BenchmarkJavaRefgo 34403 6843 -80.11%
BenchmarkJavaRefgoDirect 40193 16582 -58.74%
BenchmarkJavaStringShort 32366 9323 -71.20%
BenchmarkJavaStringShortDirect 41973 19118 -54.45%
BenchmarkJavaStringLong 127879 94420 -26.16%
BenchmarkJavaStringLongDirect 133776 114760 -14.21%
BenchmarkJavaStringShortUnicode 32562 9221 -71.68%
BenchmarkJavaStringShortUnicodeDirect 41464 19094 -53.95%
BenchmarkJavaStringLongUnicode 131015 89401 -31.76%
BenchmarkJavaStringLongUnicodeDirect 134130 90786 -32.31%
BenchmarkJavaSliceShort 42462 7538 -82.25%
BenchmarkJavaSliceShortDirect 52940 17017 -67.86%
BenchmarkJavaSliceLong 138391 8466 -93.88%
BenchmarkJavaSliceLongDirect 205804 15666 -92.39%
BenchmarkGoEmpty 3.00 3.00 +0.00%
BenchmarkGoEmptyDirect 3.00 3.00 +0.00%
BenchmarkGoNoarg 40342 13716 -66.00%
BenchmarkGoNoargDirect 46691 13569 -70.94%
BenchmarkGoOnearg 43529 13757 -68.40%
BenchmarkGoOneargDirect 44867 14078 -68.62%
BenchmarkGoOneret 45456 13559 -70.17%
BenchmarkGoOneretDirect 44694 13442 -69.92%
BenchmarkGoRefjava 55111 28071 -49.06%
BenchmarkGoRefjavaDirect 60883 26872 -55.86%
BenchmarkGoRefgo 57038 29223 -48.77%
BenchmarkGoRefgoDirect 56153 27812 -50.47%
BenchmarkGoManyargs 67967 17398 -74.40%
BenchmarkGoManyargsDirect 60617 16998 -71.96%
BenchmarkGoStringShort 57538 22600 -60.72%
BenchmarkGoStringShortDirect 52627 22704 -56.86%
BenchmarkGoStringLong 128485 52530 -59.12%
BenchmarkGoStringLongDirect 138377 52079 -62.36%
BenchmarkGoStringShortUnicode 57062 22994 -59.70%
BenchmarkGoStringShortUnicodeDirect 62563 22938 -63.34%
BenchmarkGoStringLongUnicode 139913 55553 -60.29%
BenchmarkGoStringLongUnicodeDirect 150863 57791 -61.69%
BenchmarkGoSliceShort 59279 20215 -65.90%
BenchmarkGoSliceShortDirect 60160 21136 -64.87%
BenchmarkGoSliceLong 411225 301870 -26.59%
BenchmarkGoSliceLongDirect 399029 298915 -25.09%
Fixes golang/go#12619
Fixes golang/go#12113
Fixes golang/go#13033
Change-Id: I2b45e9e98a1248e3c23a5137f775f7364908bec7
Reviewed-on: https://go-review.googlesource.com/19821
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-02-12 18:50:33 +01:00
|
|
|
if diffstr := diff(golden, out); diffstr != "" {
|
|
|
|
t.Errorf("%s: does not match Java golden:\n%s", filename, diffstr)
|
2014-07-31 15:09:13 -04:00
|
|
|
|
mobile/bind: replace seq serialization with direct calls
The seq serialization machinery is a historic artifact from when Go
mobile code had to run in a separate process. Now that Go code is running
in-process, replace the explicit serialization with direct calls and pass
arguments on the stack.
The benefits are a much smaller bind runtime, much less garbage (and, in
Java, fewer objects with finalizers), less argument copying, and faster
cross-language calls.
The cost is a more complex generator, because some of the work from the
bind runtime is moved to generated code. Generated code now handles
conversion between Go and Java/ObjC types, multiple return values and memory
management of byte slice and string arguments.
To overcome the lack of calling C code between Go packages, all bound
packages now end up in the same (fake) package, "gomobile_bind", instead of
separate packages (go_<pkgname>). To avoid name clashes, the package name is
added as a prefix to generated functions and types.
Also, don't copy byte arrays passed to Go, saving call time and
allowing read([]byte)-style interfaces to foreign callers (#12113).
Finally, add support for nil interfaces and struct pointers to objc.
This is a large CL, but most of the changes stem from changing testdata.
The full benchcmp output on the CL/20095 benchmarks on my Nexus 5 is
reproduced below. Note that the savings for the JavaSlice* benchmarks are
skewed because byte slices are no longer copied before passing them to Go.
benchmark old ns/op new ns/op delta
BenchmarkJavaEmpty 26.0 19.0 -26.92%
BenchmarkJavaEmptyDirect 23.0 22.0 -4.35%
BenchmarkJavaNoargs 7685 2339 -69.56%
BenchmarkJavaNoargsDirect 17405 8041 -53.80%
BenchmarkJavaOnearg 26887 2366 -91.20%
BenchmarkJavaOneargDirect 34266 7910 -76.92%
BenchmarkJavaOneret 38325 2245 -94.14%
BenchmarkJavaOneretDirect 46265 7708 -83.34%
BenchmarkJavaManyargs 41720 2535 -93.92%
BenchmarkJavaManyargsDirect 51026 8373 -83.59%
BenchmarkJavaRefjava 38139 21260 -44.26%
BenchmarkJavaRefjavaDirect 42706 28150 -34.08%
BenchmarkJavaRefgo 34403 6843 -80.11%
BenchmarkJavaRefgoDirect 40193 16582 -58.74%
BenchmarkJavaStringShort 32366 9323 -71.20%
BenchmarkJavaStringShortDirect 41973 19118 -54.45%
BenchmarkJavaStringLong 127879 94420 -26.16%
BenchmarkJavaStringLongDirect 133776 114760 -14.21%
BenchmarkJavaStringShortUnicode 32562 9221 -71.68%
BenchmarkJavaStringShortUnicodeDirect 41464 19094 -53.95%
BenchmarkJavaStringLongUnicode 131015 89401 -31.76%
BenchmarkJavaStringLongUnicodeDirect 134130 90786 -32.31%
BenchmarkJavaSliceShort 42462 7538 -82.25%
BenchmarkJavaSliceShortDirect 52940 17017 -67.86%
BenchmarkJavaSliceLong 138391 8466 -93.88%
BenchmarkJavaSliceLongDirect 205804 15666 -92.39%
BenchmarkGoEmpty 3.00 3.00 +0.00%
BenchmarkGoEmptyDirect 3.00 3.00 +0.00%
BenchmarkGoNoarg 40342 13716 -66.00%
BenchmarkGoNoargDirect 46691 13569 -70.94%
BenchmarkGoOnearg 43529 13757 -68.40%
BenchmarkGoOneargDirect 44867 14078 -68.62%
BenchmarkGoOneret 45456 13559 -70.17%
BenchmarkGoOneretDirect 44694 13442 -69.92%
BenchmarkGoRefjava 55111 28071 -49.06%
BenchmarkGoRefjavaDirect 60883 26872 -55.86%
BenchmarkGoRefgo 57038 29223 -48.77%
BenchmarkGoRefgoDirect 56153 27812 -50.47%
BenchmarkGoManyargs 67967 17398 -74.40%
BenchmarkGoManyargsDirect 60617 16998 -71.96%
BenchmarkGoStringShort 57538 22600 -60.72%
BenchmarkGoStringShortDirect 52627 22704 -56.86%
BenchmarkGoStringLong 128485 52530 -59.12%
BenchmarkGoStringLongDirect 138377 52079 -62.36%
BenchmarkGoStringShortUnicode 57062 22994 -59.70%
BenchmarkGoStringShortUnicodeDirect 62563 22938 -63.34%
BenchmarkGoStringLongUnicode 139913 55553 -60.29%
BenchmarkGoStringLongUnicodeDirect 150863 57791 -61.69%
BenchmarkGoSliceShort 59279 20215 -65.90%
BenchmarkGoSliceShortDirect 60160 21136 -64.87%
BenchmarkGoSliceLong 411225 301870 -26.59%
BenchmarkGoSliceLongDirect 399029 298915 -25.09%
Fixes golang/go#12619
Fixes golang/go#12113
Fixes golang/go#13033
Change-Id: I2b45e9e98a1248e3c23a5137f775f7364908bec7
Reviewed-on: https://go-review.googlesource.com/19821
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-02-12 18:50:33 +01:00
|
|
|
if *updateFlag {
|
|
|
|
t.Logf("Updating %s...", golden)
|
|
|
|
if err := exec.Command("/bin/cp", out, golden).Run(); err != nil {
|
|
|
|
t.Errorf("Update failed: %s", err)
|
|
|
|
}
|
2014-07-31 15:09:13 -04:00
|
|
|
}
|
|
|
|
|
mobile/bind: replace seq serialization with direct calls
The seq serialization machinery is a historic artifact from when Go
mobile code had to run in a separate process. Now that Go code is running
in-process, replace the explicit serialization with direct calls and pass
arguments on the stack.
The benefits are a much smaller bind runtime, much less garbage (and, in
Java, fewer objects with finalizers), less argument copying, and faster
cross-language calls.
The cost is a more complex generator, because some of the work from the
bind runtime is moved to generated code. Generated code now handles
conversion between Go and Java/ObjC types, multiple return values and memory
management of byte slice and string arguments.
To overcome the lack of calling C code between Go packages, all bound
packages now end up in the same (fake) package, "gomobile_bind", instead of
separate packages (go_<pkgname>). To avoid name clashes, the package name is
added as a prefix to generated functions and types.
Also, don't copy byte arrays passed to Go, saving call time and
allowing read([]byte)-style interfaces to foreign callers (#12113).
Finally, add support for nil interfaces and struct pointers to objc.
This is a large CL, but most of the changes stem from changing testdata.
The full benchcmp output on the CL/20095 benchmarks on my Nexus 5 is
reproduced below. Note that the savings for the JavaSlice* benchmarks are
skewed because byte slices are no longer copied before passing them to Go.
benchmark old ns/op new ns/op delta
BenchmarkJavaEmpty 26.0 19.0 -26.92%
BenchmarkJavaEmptyDirect 23.0 22.0 -4.35%
BenchmarkJavaNoargs 7685 2339 -69.56%
BenchmarkJavaNoargsDirect 17405 8041 -53.80%
BenchmarkJavaOnearg 26887 2366 -91.20%
BenchmarkJavaOneargDirect 34266 7910 -76.92%
BenchmarkJavaOneret 38325 2245 -94.14%
BenchmarkJavaOneretDirect 46265 7708 -83.34%
BenchmarkJavaManyargs 41720 2535 -93.92%
BenchmarkJavaManyargsDirect 51026 8373 -83.59%
BenchmarkJavaRefjava 38139 21260 -44.26%
BenchmarkJavaRefjavaDirect 42706 28150 -34.08%
BenchmarkJavaRefgo 34403 6843 -80.11%
BenchmarkJavaRefgoDirect 40193 16582 -58.74%
BenchmarkJavaStringShort 32366 9323 -71.20%
BenchmarkJavaStringShortDirect 41973 19118 -54.45%
BenchmarkJavaStringLong 127879 94420 -26.16%
BenchmarkJavaStringLongDirect 133776 114760 -14.21%
BenchmarkJavaStringShortUnicode 32562 9221 -71.68%
BenchmarkJavaStringShortUnicodeDirect 41464 19094 -53.95%
BenchmarkJavaStringLongUnicode 131015 89401 -31.76%
BenchmarkJavaStringLongUnicodeDirect 134130 90786 -32.31%
BenchmarkJavaSliceShort 42462 7538 -82.25%
BenchmarkJavaSliceShortDirect 52940 17017 -67.86%
BenchmarkJavaSliceLong 138391 8466 -93.88%
BenchmarkJavaSliceLongDirect 205804 15666 -92.39%
BenchmarkGoEmpty 3.00 3.00 +0.00%
BenchmarkGoEmptyDirect 3.00 3.00 +0.00%
BenchmarkGoNoarg 40342 13716 -66.00%
BenchmarkGoNoargDirect 46691 13569 -70.94%
BenchmarkGoOnearg 43529 13757 -68.40%
BenchmarkGoOneargDirect 44867 14078 -68.62%
BenchmarkGoOneret 45456 13559 -70.17%
BenchmarkGoOneretDirect 44694 13442 -69.92%
BenchmarkGoRefjava 55111 28071 -49.06%
BenchmarkGoRefjavaDirect 60883 26872 -55.86%
BenchmarkGoRefgo 57038 29223 -48.77%
BenchmarkGoRefgoDirect 56153 27812 -50.47%
BenchmarkGoManyargs 67967 17398 -74.40%
BenchmarkGoManyargsDirect 60617 16998 -71.96%
BenchmarkGoStringShort 57538 22600 -60.72%
BenchmarkGoStringShortDirect 52627 22704 -56.86%
BenchmarkGoStringLong 128485 52530 -59.12%
BenchmarkGoStringLongDirect 138377 52079 -62.36%
BenchmarkGoStringShortUnicode 57062 22994 -59.70%
BenchmarkGoStringShortUnicodeDirect 62563 22938 -63.34%
BenchmarkGoStringLongUnicode 139913 55553 -60.29%
BenchmarkGoStringLongUnicodeDirect 150863 57791 -61.69%
BenchmarkGoSliceShort 59279 20215 -65.90%
BenchmarkGoSliceShortDirect 60160 21136 -64.87%
BenchmarkGoSliceLong 411225 301870 -26.59%
BenchmarkGoSliceLongDirect 399029 298915 -25.09%
Fixes golang/go#12619
Fixes golang/go#12113
Fixes golang/go#13033
Change-Id: I2b45e9e98a1248e3c23a5137f775f7364908bec7
Reviewed-on: https://go-review.googlesource.com/19821
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-02-12 18:50:33 +01:00
|
|
|
}
|
2014-07-31 15:09:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenGo(t *testing.T) {
|
2016-09-13 06:05:52 +02:00
|
|
|
for _, filename := range tests {
|
|
|
|
var buf bytes.Buffer
|
2017-01-09 22:35:14 +01:00
|
|
|
var pkg *types.Package
|
|
|
|
if filename != "" {
|
2017-07-31 16:13:01 +02:00
|
|
|
pkg, _ = typeCheck(t, filename, "")
|
2017-01-09 22:35:14 +01:00
|
|
|
}
|
2016-09-13 06:05:52 +02:00
|
|
|
testGenGo(t, filename, &buf, pkg)
|
2016-09-07 18:27:36 +02:00
|
|
|
}
|
2016-09-13 06:05:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenGoJavaWrappers(t *testing.T) {
|
|
|
|
if !java.IsAvailable() {
|
|
|
|
t.Skipf("java is not available")
|
|
|
|
}
|
|
|
|
for _, filename := range javaTests {
|
2014-07-31 15:09:13 -04:00
|
|
|
var buf bytes.Buffer
|
2016-09-13 06:05:52 +02:00
|
|
|
refs := fileRefs(t, filename, "Java/")
|
2017-01-01 22:43:46 +01:00
|
|
|
imp := &java.Importer{}
|
bind,cmd,internal: generate reverse bindings for exported Go structs
Before this CL, the type of the implicit "this" parameter to Java methods
implemented in Go could only be a super class of the generated Java
class. For example, the following GoRunnable type is an implementation of
the Java interface java.lang.Runnable with a toString method:
package somepkg
import "Java/java/lang"
type GoRunnable struct {
lang.Runnable
}
func (r *GoRunnable) ToString(this lang.Runnable) string {
...
}
The "this" parameter is implicit in the sense that the reverse generator
automatically fills it with a reference to the Java instance of
GoRunnable.
Note that "this" has the type Java/java/lang.Runnable, not
Java/go/somepkg.GoRunnable, which renders it impossible to call Java
methods and functions that expect GoRunnable. The most practical example
of this is the Android databinding libraries.
This CL changes the implicit this parameter to always match the exact
type. In the example, the toString implementation becomes:
import gopkg "Java/go/somepkg"
func (r *GoRunnable) ToString(this gopkg.GoRunnable) string {
...
}
One strategy would be to simply treat the generated Java classes
(GoRunnable in our example) as any other Java class and import it
through javap. However, since the Java classes are generated after
importing, this present a chicken-and-egg problem.
Instead, use the newly added support for structs with embedded prefixed types
and synthesize class descriptors for every exported Go struct type.
Change-Id: Ic5ce4a151312bd89f91798ed4088c9959225b448
Reviewed-on: https://go-review.googlesource.com/34776
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-12-31 16:41:36 +01:00
|
|
|
classes, err := imp.Import(refs)
|
2016-09-07 18:27:36 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-13 06:05:52 +02:00
|
|
|
tmpGopath, err := ioutil.TempDir(os.TempDir(), "gomobile-bind-test-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2016-09-07 18:27:36 +02:00
|
|
|
}
|
2016-09-13 06:05:52 +02:00
|
|
|
defer os.RemoveAll(tmpGopath)
|
bind,cmd,internal: generate reverse bindings for exported Go structs
Before this CL, the type of the implicit "this" parameter to Java methods
implemented in Go could only be a super class of the generated Java
class. For example, the following GoRunnable type is an implementation of
the Java interface java.lang.Runnable with a toString method:
package somepkg
import "Java/java/lang"
type GoRunnable struct {
lang.Runnable
}
func (r *GoRunnable) ToString(this lang.Runnable) string {
...
}
The "this" parameter is implicit in the sense that the reverse generator
automatically fills it with a reference to the Java instance of
GoRunnable.
Note that "this" has the type Java/java/lang.Runnable, not
Java/go/somepkg.GoRunnable, which renders it impossible to call Java
methods and functions that expect GoRunnable. The most practical example
of this is the Android databinding libraries.
This CL changes the implicit this parameter to always match the exact
type. In the example, the toString implementation becomes:
import gopkg "Java/go/somepkg"
func (r *GoRunnable) ToString(this gopkg.GoRunnable) string {
...
}
One strategy would be to simply treat the generated Java classes
(GoRunnable in our example) as any other Java class and import it
through javap. However, since the Java classes are generated after
importing, this present a chicken-and-egg problem.
Instead, use the newly added support for structs with embedded prefixed types
and synthesize class descriptors for every exported Go struct type.
Change-Id: Ic5ce4a151312bd89f91798ed4088c9959225b448
Reviewed-on: https://go-review.googlesource.com/34776
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-12-31 16:41:36 +01:00
|
|
|
cg := &ClassGen{
|
|
|
|
Printer: &Printer{
|
|
|
|
IndentEach: []byte("\t"),
|
|
|
|
Buf: &buf,
|
|
|
|
},
|
|
|
|
}
|
2017-01-17 02:26:29 +01:00
|
|
|
cg.Init(classes, refs.Embedders)
|
bind,cmd,internal: generate reverse bindings for exported Go structs
Before this CL, the type of the implicit "this" parameter to Java methods
implemented in Go could only be a super class of the generated Java
class. For example, the following GoRunnable type is an implementation of
the Java interface java.lang.Runnable with a toString method:
package somepkg
import "Java/java/lang"
type GoRunnable struct {
lang.Runnable
}
func (r *GoRunnable) ToString(this lang.Runnable) string {
...
}
The "this" parameter is implicit in the sense that the reverse generator
automatically fills it with a reference to the Java instance of
GoRunnable.
Note that "this" has the type Java/java/lang.Runnable, not
Java/go/somepkg.GoRunnable, which renders it impossible to call Java
methods and functions that expect GoRunnable. The most practical example
of this is the Android databinding libraries.
This CL changes the implicit this parameter to always match the exact
type. In the example, the toString implementation becomes:
import gopkg "Java/go/somepkg"
func (r *GoRunnable) ToString(this gopkg.GoRunnable) string {
...
}
One strategy would be to simply treat the generated Java classes
(GoRunnable in our example) as any other Java class and import it
through javap. However, since the Java classes are generated after
importing, this present a chicken-and-egg problem.
Instead, use the newly added support for structs with embedded prefixed types
and synthesize class descriptors for every exported Go struct type.
Change-Id: Ic5ce4a151312bd89f91798ed4088c9959225b448
Reviewed-on: https://go-review.googlesource.com/34776
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-12-31 16:41:36 +01:00
|
|
|
genJavaPackages(t, tmpGopath, cg)
|
2017-07-31 16:13:01 +02:00
|
|
|
pkg, _ := typeCheck(t, filename, tmpGopath)
|
2016-09-13 06:05:52 +02:00
|
|
|
cg.GenGo()
|
|
|
|
testGenGo(t, filename, &buf, pkg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenGoObjcWrappers(t *testing.T) {
|
|
|
|
if runtime.GOOS != "darwin" {
|
|
|
|
t.Skipf("can only generate objc wrappers on darwin")
|
|
|
|
}
|
|
|
|
for _, filename := range objcTests {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
refs := fileRefs(t, filename, "ObjC/")
|
|
|
|
types, err := objc.Import(refs)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2016-09-07 18:27:36 +02:00
|
|
|
}
|
2016-09-13 06:05:52 +02:00
|
|
|
tmpGopath, err := ioutil.TempDir(os.TempDir(), "gomobile-bind-test-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2014-07-31 15:09:13 -04:00
|
|
|
}
|
2016-09-13 06:05:52 +02:00
|
|
|
defer os.RemoveAll(tmpGopath)
|
2017-01-02 21:51:27 +01:00
|
|
|
cg := &ObjcWrapper{
|
|
|
|
Printer: &Printer{
|
|
|
|
IndentEach: []byte("\t"),
|
|
|
|
Buf: &buf,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var genNames []string
|
|
|
|
for _, emb := range refs.Embedders {
|
|
|
|
genNames = append(genNames, emb.Name)
|
|
|
|
}
|
|
|
|
cg.Init(types, genNames)
|
|
|
|
genObjcPackages(t, tmpGopath, cg)
|
2017-07-31 16:13:01 +02:00
|
|
|
pkg, _ := typeCheck(t, filename, tmpGopath)
|
2016-09-13 06:05:52 +02:00
|
|
|
cg.GenGo()
|
|
|
|
testGenGo(t, filename, &buf, pkg)
|
|
|
|
}
|
|
|
|
}
|
2014-07-31 15:09:13 -04:00
|
|
|
|
2016-09-13 06:05:52 +02:00
|
|
|
func testGenGo(t *testing.T, filename string, buf *bytes.Buffer, pkg *types.Package) {
|
|
|
|
conf := &GeneratorConfig{
|
|
|
|
Writer: buf,
|
|
|
|
Fset: fset,
|
|
|
|
Pkg: pkg,
|
2017-01-09 22:35:14 +01:00
|
|
|
}
|
|
|
|
if pkg != nil {
|
|
|
|
conf.AllPkg = []*types.Package{pkg}
|
2016-09-13 06:05:52 +02:00
|
|
|
}
|
|
|
|
if err := GenGo(conf); err != nil {
|
|
|
|
t.Errorf("%s: %v", filename, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
out := writeTempFile(t, "go", buf.Bytes())
|
|
|
|
defer os.Remove(out)
|
2017-01-09 22:35:14 +01:00
|
|
|
golden := filename
|
|
|
|
if golden == "" {
|
|
|
|
golden = "testdata/universe"
|
|
|
|
}
|
|
|
|
golden += ".golden"
|
2016-09-13 06:05:52 +02:00
|
|
|
if diffstr := diff(golden, out); diffstr != "" {
|
|
|
|
t.Errorf("%s: does not match Go golden:\n%s", filename, diffstr)
|
|
|
|
|
|
|
|
if *updateFlag {
|
|
|
|
t.Logf("Updating %s...", golden)
|
|
|
|
if err := exec.Command("/bin/cp", out, golden).Run(); err != nil {
|
|
|
|
t.Errorf("Update failed: %s", err)
|
2014-07-31 15:09:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-27 12:19:47 -04:00
|
|
|
|
|
|
|
func TestCustomPrefix(t *testing.T) {
|
|
|
|
const datafile = "testdata/customprefix.go"
|
2017-08-28 19:53:40 +02:00
|
|
|
pkg, file := typeCheck(t, datafile, "")
|
2015-08-27 12:19:47 -04:00
|
|
|
|
2016-11-24 16:53:21 +01:00
|
|
|
type testCase struct {
|
|
|
|
golden string
|
|
|
|
gen func(w io.Writer) error
|
|
|
|
}
|
2016-08-20 21:10:46 +02:00
|
|
|
var buf bytes.Buffer
|
2016-09-13 06:05:52 +02:00
|
|
|
jg := &JavaGen{
|
2016-08-20 21:10:46 +02:00
|
|
|
JavaPkg: "com.example",
|
|
|
|
Generator: &Generator{
|
|
|
|
Printer: &Printer{Buf: &buf, IndentEach: []byte(" ")},
|
|
|
|
Fset: fset,
|
|
|
|
AllPkg: []*types.Package{pkg},
|
2017-08-28 19:53:40 +02:00
|
|
|
Files: []*ast.File{file},
|
2016-08-20 21:10:46 +02:00
|
|
|
Pkg: pkg,
|
|
|
|
},
|
|
|
|
}
|
2016-09-13 06:05:52 +02:00
|
|
|
jg.Init(nil)
|
2016-11-24 16:53:21 +01:00
|
|
|
testCases := []testCase{
|
2015-08-27 12:19:47 -04:00
|
|
|
{
|
|
|
|
"testdata/customprefix.java.golden",
|
2016-08-20 21:10:46 +02:00
|
|
|
func(w io.Writer) error {
|
|
|
|
buf.Reset()
|
2016-09-13 06:05:52 +02:00
|
|
|
for i := range jg.ClassNames() {
|
|
|
|
if err := jg.GenClass(i); err != nil {
|
2016-08-20 21:10:46 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-09-13 06:05:52 +02:00
|
|
|
if err := jg.GenJava(); err != nil {
|
2016-08-20 21:10:46 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := io.Copy(w, &buf)
|
|
|
|
return err
|
|
|
|
},
|
mobile/bind: replace seq serialization with direct calls
The seq serialization machinery is a historic artifact from when Go
mobile code had to run in a separate process. Now that Go code is running
in-process, replace the explicit serialization with direct calls and pass
arguments on the stack.
The benefits are a much smaller bind runtime, much less garbage (and, in
Java, fewer objects with finalizers), less argument copying, and faster
cross-language calls.
The cost is a more complex generator, because some of the work from the
bind runtime is moved to generated code. Generated code now handles
conversion between Go and Java/ObjC types, multiple return values and memory
management of byte slice and string arguments.
To overcome the lack of calling C code between Go packages, all bound
packages now end up in the same (fake) package, "gomobile_bind", instead of
separate packages (go_<pkgname>). To avoid name clashes, the package name is
added as a prefix to generated functions and types.
Also, don't copy byte arrays passed to Go, saving call time and
allowing read([]byte)-style interfaces to foreign callers (#12113).
Finally, add support for nil interfaces and struct pointers to objc.
This is a large CL, but most of the changes stem from changing testdata.
The full benchcmp output on the CL/20095 benchmarks on my Nexus 5 is
reproduced below. Note that the savings for the JavaSlice* benchmarks are
skewed because byte slices are no longer copied before passing them to Go.
benchmark old ns/op new ns/op delta
BenchmarkJavaEmpty 26.0 19.0 -26.92%
BenchmarkJavaEmptyDirect 23.0 22.0 -4.35%
BenchmarkJavaNoargs 7685 2339 -69.56%
BenchmarkJavaNoargsDirect 17405 8041 -53.80%
BenchmarkJavaOnearg 26887 2366 -91.20%
BenchmarkJavaOneargDirect 34266 7910 -76.92%
BenchmarkJavaOneret 38325 2245 -94.14%
BenchmarkJavaOneretDirect 46265 7708 -83.34%
BenchmarkJavaManyargs 41720 2535 -93.92%
BenchmarkJavaManyargsDirect 51026 8373 -83.59%
BenchmarkJavaRefjava 38139 21260 -44.26%
BenchmarkJavaRefjavaDirect 42706 28150 -34.08%
BenchmarkJavaRefgo 34403 6843 -80.11%
BenchmarkJavaRefgoDirect 40193 16582 -58.74%
BenchmarkJavaStringShort 32366 9323 -71.20%
BenchmarkJavaStringShortDirect 41973 19118 -54.45%
BenchmarkJavaStringLong 127879 94420 -26.16%
BenchmarkJavaStringLongDirect 133776 114760 -14.21%
BenchmarkJavaStringShortUnicode 32562 9221 -71.68%
BenchmarkJavaStringShortUnicodeDirect 41464 19094 -53.95%
BenchmarkJavaStringLongUnicode 131015 89401 -31.76%
BenchmarkJavaStringLongUnicodeDirect 134130 90786 -32.31%
BenchmarkJavaSliceShort 42462 7538 -82.25%
BenchmarkJavaSliceShortDirect 52940 17017 -67.86%
BenchmarkJavaSliceLong 138391 8466 -93.88%
BenchmarkJavaSliceLongDirect 205804 15666 -92.39%
BenchmarkGoEmpty 3.00 3.00 +0.00%
BenchmarkGoEmptyDirect 3.00 3.00 +0.00%
BenchmarkGoNoarg 40342 13716 -66.00%
BenchmarkGoNoargDirect 46691 13569 -70.94%
BenchmarkGoOnearg 43529 13757 -68.40%
BenchmarkGoOneargDirect 44867 14078 -68.62%
BenchmarkGoOneret 45456 13559 -70.17%
BenchmarkGoOneretDirect 44694 13442 -69.92%
BenchmarkGoRefjava 55111 28071 -49.06%
BenchmarkGoRefjavaDirect 60883 26872 -55.86%
BenchmarkGoRefgo 57038 29223 -48.77%
BenchmarkGoRefgoDirect 56153 27812 -50.47%
BenchmarkGoManyargs 67967 17398 -74.40%
BenchmarkGoManyargsDirect 60617 16998 -71.96%
BenchmarkGoStringShort 57538 22600 -60.72%
BenchmarkGoStringShortDirect 52627 22704 -56.86%
BenchmarkGoStringLong 128485 52530 -59.12%
BenchmarkGoStringLongDirect 138377 52079 -62.36%
BenchmarkGoStringShortUnicode 57062 22994 -59.70%
BenchmarkGoStringShortUnicodeDirect 62563 22938 -63.34%
BenchmarkGoStringLongUnicode 139913 55553 -60.29%
BenchmarkGoStringLongUnicodeDirect 150863 57791 -61.69%
BenchmarkGoSliceShort 59279 20215 -65.90%
BenchmarkGoSliceShortDirect 60160 21136 -64.87%
BenchmarkGoSliceLong 411225 301870 -26.59%
BenchmarkGoSliceLongDirect 399029 298915 -25.09%
Fixes golang/go#12619
Fixes golang/go#12113
Fixes golang/go#13033
Change-Id: I2b45e9e98a1248e3c23a5137f775f7364908bec7
Reviewed-on: https://go-review.googlesource.com/19821
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-02-12 18:50:33 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"testdata/customprefix.java.h.golden",
|
2016-08-20 21:10:46 +02:00
|
|
|
func(w io.Writer) error {
|
|
|
|
buf.Reset()
|
2016-09-13 06:05:52 +02:00
|
|
|
if err := jg.GenH(); err != nil {
|
2016-08-20 21:10:46 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := io.Copy(w, &buf)
|
|
|
|
return err
|
|
|
|
},
|
mobile/bind: replace seq serialization with direct calls
The seq serialization machinery is a historic artifact from when Go
mobile code had to run in a separate process. Now that Go code is running
in-process, replace the explicit serialization with direct calls and pass
arguments on the stack.
The benefits are a much smaller bind runtime, much less garbage (and, in
Java, fewer objects with finalizers), less argument copying, and faster
cross-language calls.
The cost is a more complex generator, because some of the work from the
bind runtime is moved to generated code. Generated code now handles
conversion between Go and Java/ObjC types, multiple return values and memory
management of byte slice and string arguments.
To overcome the lack of calling C code between Go packages, all bound
packages now end up in the same (fake) package, "gomobile_bind", instead of
separate packages (go_<pkgname>). To avoid name clashes, the package name is
added as a prefix to generated functions and types.
Also, don't copy byte arrays passed to Go, saving call time and
allowing read([]byte)-style interfaces to foreign callers (#12113).
Finally, add support for nil interfaces and struct pointers to objc.
This is a large CL, but most of the changes stem from changing testdata.
The full benchcmp output on the CL/20095 benchmarks on my Nexus 5 is
reproduced below. Note that the savings for the JavaSlice* benchmarks are
skewed because byte slices are no longer copied before passing them to Go.
benchmark old ns/op new ns/op delta
BenchmarkJavaEmpty 26.0 19.0 -26.92%
BenchmarkJavaEmptyDirect 23.0 22.0 -4.35%
BenchmarkJavaNoargs 7685 2339 -69.56%
BenchmarkJavaNoargsDirect 17405 8041 -53.80%
BenchmarkJavaOnearg 26887 2366 -91.20%
BenchmarkJavaOneargDirect 34266 7910 -76.92%
BenchmarkJavaOneret 38325 2245 -94.14%
BenchmarkJavaOneretDirect 46265 7708 -83.34%
BenchmarkJavaManyargs 41720 2535 -93.92%
BenchmarkJavaManyargsDirect 51026 8373 -83.59%
BenchmarkJavaRefjava 38139 21260 -44.26%
BenchmarkJavaRefjavaDirect 42706 28150 -34.08%
BenchmarkJavaRefgo 34403 6843 -80.11%
BenchmarkJavaRefgoDirect 40193 16582 -58.74%
BenchmarkJavaStringShort 32366 9323 -71.20%
BenchmarkJavaStringShortDirect 41973 19118 -54.45%
BenchmarkJavaStringLong 127879 94420 -26.16%
BenchmarkJavaStringLongDirect 133776 114760 -14.21%
BenchmarkJavaStringShortUnicode 32562 9221 -71.68%
BenchmarkJavaStringShortUnicodeDirect 41464 19094 -53.95%
BenchmarkJavaStringLongUnicode 131015 89401 -31.76%
BenchmarkJavaStringLongUnicodeDirect 134130 90786 -32.31%
BenchmarkJavaSliceShort 42462 7538 -82.25%
BenchmarkJavaSliceShortDirect 52940 17017 -67.86%
BenchmarkJavaSliceLong 138391 8466 -93.88%
BenchmarkJavaSliceLongDirect 205804 15666 -92.39%
BenchmarkGoEmpty 3.00 3.00 +0.00%
BenchmarkGoEmptyDirect 3.00 3.00 +0.00%
BenchmarkGoNoarg 40342 13716 -66.00%
BenchmarkGoNoargDirect 46691 13569 -70.94%
BenchmarkGoOnearg 43529 13757 -68.40%
BenchmarkGoOneargDirect 44867 14078 -68.62%
BenchmarkGoOneret 45456 13559 -70.17%
BenchmarkGoOneretDirect 44694 13442 -69.92%
BenchmarkGoRefjava 55111 28071 -49.06%
BenchmarkGoRefjavaDirect 60883 26872 -55.86%
BenchmarkGoRefgo 57038 29223 -48.77%
BenchmarkGoRefgoDirect 56153 27812 -50.47%
BenchmarkGoManyargs 67967 17398 -74.40%
BenchmarkGoManyargsDirect 60617 16998 -71.96%
BenchmarkGoStringShort 57538 22600 -60.72%
BenchmarkGoStringShortDirect 52627 22704 -56.86%
BenchmarkGoStringLong 128485 52530 -59.12%
BenchmarkGoStringLongDirect 138377 52079 -62.36%
BenchmarkGoStringShortUnicode 57062 22994 -59.70%
BenchmarkGoStringShortUnicodeDirect 62563 22938 -63.34%
BenchmarkGoStringLongUnicode 139913 55553 -60.29%
BenchmarkGoStringLongUnicodeDirect 150863 57791 -61.69%
BenchmarkGoSliceShort 59279 20215 -65.90%
BenchmarkGoSliceShortDirect 60160 21136 -64.87%
BenchmarkGoSliceLong 411225 301870 -26.59%
BenchmarkGoSliceLongDirect 399029 298915 -25.09%
Fixes golang/go#12619
Fixes golang/go#12113
Fixes golang/go#13033
Change-Id: I2b45e9e98a1248e3c23a5137f775f7364908bec7
Reviewed-on: https://go-review.googlesource.com/19821
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-02-12 18:50:33 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"testdata/customprefix.java.c.golden",
|
2016-08-20 21:10:46 +02:00
|
|
|
func(w io.Writer) error {
|
|
|
|
buf.Reset()
|
2016-09-13 06:05:52 +02:00
|
|
|
if err := jg.GenC(); err != nil {
|
2016-08-20 21:10:46 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := io.Copy(w, &buf)
|
|
|
|
return err
|
|
|
|
},
|
mobile/bind: replace seq serialization with direct calls
The seq serialization machinery is a historic artifact from when Go
mobile code had to run in a separate process. Now that Go code is running
in-process, replace the explicit serialization with direct calls and pass
arguments on the stack.
The benefits are a much smaller bind runtime, much less garbage (and, in
Java, fewer objects with finalizers), less argument copying, and faster
cross-language calls.
The cost is a more complex generator, because some of the work from the
bind runtime is moved to generated code. Generated code now handles
conversion between Go and Java/ObjC types, multiple return values and memory
management of byte slice and string arguments.
To overcome the lack of calling C code between Go packages, all bound
packages now end up in the same (fake) package, "gomobile_bind", instead of
separate packages (go_<pkgname>). To avoid name clashes, the package name is
added as a prefix to generated functions and types.
Also, don't copy byte arrays passed to Go, saving call time and
allowing read([]byte)-style interfaces to foreign callers (#12113).
Finally, add support for nil interfaces and struct pointers to objc.
This is a large CL, but most of the changes stem from changing testdata.
The full benchcmp output on the CL/20095 benchmarks on my Nexus 5 is
reproduced below. Note that the savings for the JavaSlice* benchmarks are
skewed because byte slices are no longer copied before passing them to Go.
benchmark old ns/op new ns/op delta
BenchmarkJavaEmpty 26.0 19.0 -26.92%
BenchmarkJavaEmptyDirect 23.0 22.0 -4.35%
BenchmarkJavaNoargs 7685 2339 -69.56%
BenchmarkJavaNoargsDirect 17405 8041 -53.80%
BenchmarkJavaOnearg 26887 2366 -91.20%
BenchmarkJavaOneargDirect 34266 7910 -76.92%
BenchmarkJavaOneret 38325 2245 -94.14%
BenchmarkJavaOneretDirect 46265 7708 -83.34%
BenchmarkJavaManyargs 41720 2535 -93.92%
BenchmarkJavaManyargsDirect 51026 8373 -83.59%
BenchmarkJavaRefjava 38139 21260 -44.26%
BenchmarkJavaRefjavaDirect 42706 28150 -34.08%
BenchmarkJavaRefgo 34403 6843 -80.11%
BenchmarkJavaRefgoDirect 40193 16582 -58.74%
BenchmarkJavaStringShort 32366 9323 -71.20%
BenchmarkJavaStringShortDirect 41973 19118 -54.45%
BenchmarkJavaStringLong 127879 94420 -26.16%
BenchmarkJavaStringLongDirect 133776 114760 -14.21%
BenchmarkJavaStringShortUnicode 32562 9221 -71.68%
BenchmarkJavaStringShortUnicodeDirect 41464 19094 -53.95%
BenchmarkJavaStringLongUnicode 131015 89401 -31.76%
BenchmarkJavaStringLongUnicodeDirect 134130 90786 -32.31%
BenchmarkJavaSliceShort 42462 7538 -82.25%
BenchmarkJavaSliceShortDirect 52940 17017 -67.86%
BenchmarkJavaSliceLong 138391 8466 -93.88%
BenchmarkJavaSliceLongDirect 205804 15666 -92.39%
BenchmarkGoEmpty 3.00 3.00 +0.00%
BenchmarkGoEmptyDirect 3.00 3.00 +0.00%
BenchmarkGoNoarg 40342 13716 -66.00%
BenchmarkGoNoargDirect 46691 13569 -70.94%
BenchmarkGoOnearg 43529 13757 -68.40%
BenchmarkGoOneargDirect 44867 14078 -68.62%
BenchmarkGoOneret 45456 13559 -70.17%
BenchmarkGoOneretDirect 44694 13442 -69.92%
BenchmarkGoRefjava 55111 28071 -49.06%
BenchmarkGoRefjavaDirect 60883 26872 -55.86%
BenchmarkGoRefgo 57038 29223 -48.77%
BenchmarkGoRefgoDirect 56153 27812 -50.47%
BenchmarkGoManyargs 67967 17398 -74.40%
BenchmarkGoManyargsDirect 60617 16998 -71.96%
BenchmarkGoStringShort 57538 22600 -60.72%
BenchmarkGoStringShortDirect 52627 22704 -56.86%
BenchmarkGoStringLong 128485 52530 -59.12%
BenchmarkGoStringLongDirect 138377 52079 -62.36%
BenchmarkGoStringShortUnicode 57062 22994 -59.70%
BenchmarkGoStringShortUnicodeDirect 62563 22938 -63.34%
BenchmarkGoStringLongUnicode 139913 55553 -60.29%
BenchmarkGoStringLongUnicodeDirect 150863 57791 -61.69%
BenchmarkGoSliceShort 59279 20215 -65.90%
BenchmarkGoSliceShortDirect 60160 21136 -64.87%
BenchmarkGoSliceLong 411225 301870 -26.59%
BenchmarkGoSliceLongDirect 399029 298915 -25.09%
Fixes golang/go#12619
Fixes golang/go#12113
Fixes golang/go#13033
Change-Id: I2b45e9e98a1248e3c23a5137f775f7364908bec7
Reviewed-on: https://go-review.googlesource.com/19821
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-02-12 18:50:33 +01:00
|
|
|
},
|
2016-11-24 16:53:21 +01:00
|
|
|
}
|
|
|
|
for _, pref := range []string{"EX", ""} {
|
|
|
|
og := &ObjcGen{
|
|
|
|
Prefix: pref,
|
|
|
|
Generator: &Generator{
|
|
|
|
Printer: &Printer{Buf: &buf, IndentEach: []byte(" ")},
|
|
|
|
Fset: fset,
|
|
|
|
AllPkg: []*types.Package{pkg},
|
|
|
|
Pkg: pkg,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
og.Init(nil)
|
|
|
|
testCases = append(testCases, []testCase{
|
|
|
|
{
|
|
|
|
"testdata/customprefix" + pref + ".objc.go.h.golden",
|
|
|
|
func(w io.Writer) error {
|
|
|
|
buf.Reset()
|
|
|
|
if err := og.GenGoH(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := io.Copy(w, &buf)
|
2016-09-13 06:05:52 +02:00
|
|
|
return err
|
2016-11-24 16:53:21 +01:00
|
|
|
},
|
2016-09-13 06:05:52 +02:00
|
|
|
},
|
2016-11-24 16:53:21 +01:00
|
|
|
{
|
|
|
|
"testdata/customprefix" + pref + ".objc.h.golden",
|
|
|
|
func(w io.Writer) error {
|
|
|
|
buf.Reset()
|
|
|
|
if err := og.GenH(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := io.Copy(w, &buf)
|
2016-09-13 06:05:52 +02:00
|
|
|
return err
|
2016-11-24 16:53:21 +01:00
|
|
|
},
|
2016-09-13 06:05:52 +02:00
|
|
|
},
|
2016-11-24 16:53:21 +01:00
|
|
|
{
|
|
|
|
"testdata/customprefix" + pref + ".objc.m.golden",
|
|
|
|
func(w io.Writer) error {
|
|
|
|
buf.Reset()
|
|
|
|
if err := og.GenM(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := io.Copy(w, &buf)
|
2016-09-13 06:05:52 +02:00
|
|
|
return err
|
2016-11-24 16:53:21 +01:00
|
|
|
},
|
2016-09-13 06:05:52 +02:00
|
|
|
},
|
2016-11-24 16:53:21 +01:00
|
|
|
}...)
|
2015-08-27 12:19:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := tc.gen(&buf); err != nil {
|
|
|
|
t.Errorf("generating %s: %v", tc.golden, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
out := writeTempFile(t, "generated", buf.Bytes())
|
|
|
|
defer os.Remove(out)
|
|
|
|
if diffstr := diff(tc.golden, out); diffstr != "" {
|
|
|
|
t.Errorf("%s: generated file does not match:\b%s", tc.golden, diffstr)
|
|
|
|
if *updateFlag {
|
|
|
|
t.Logf("Updating %s...", tc.golden)
|
|
|
|
err := exec.Command("/bin/cp", out, tc.golden).Run()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Update failed: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-23 12:01:49 -04:00
|
|
|
|
|
|
|
func TestLowerFirst(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
in, want string
|
|
|
|
}{
|
|
|
|
{"", ""},
|
|
|
|
{"Hello", "hello"},
|
|
|
|
{"HelloGopher", "helloGopher"},
|
|
|
|
{"hello", "hello"},
|
|
|
|
{"ID", "id"},
|
|
|
|
{"IDOrName", "idOrName"},
|
|
|
|
{"ΓειαΣας", "γειαΣας"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
if got := lowerFirst(tc.in); got != tc.want {
|
|
|
|
t.Errorf("lowerFirst(%q) = %q; want %q", tc.in, got, tc.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-28 19:53:40 +02:00
|
|
|
|
|
|
|
// Test that typeName work for anonymous qualified fields.
|
|
|
|
func TestSelectorExprTypeName(t *testing.T) {
|
|
|
|
e, err := parser.ParseExprFrom(fset, "", "struct { bytes.Buffer }", 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ft := e.(*ast.StructType).Fields.List[0].Type
|
|
|
|
if got, want := typeName(ft), "Buffer"; got != want {
|
|
|
|
t.Errorf("got: %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
}
|