2015-02-11 15:23:09 -05:00
|
|
|
// Copyright 2015 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-08-20 21:10:46 +02:00
|
|
|
"bytes"
|
2015-02-11 15:23:09 -05:00
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/build"
|
2015-11-13 13:57:15 -05:00
|
|
|
"go/importer"
|
2015-02-11 15:23:09 -05:00
|
|
|
"go/token"
|
2015-07-25 09:45:10 -04:00
|
|
|
"go/types"
|
2015-02-11 15:23:09 -05:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-11-20 11:26:00 -05:00
|
|
|
"path"
|
2015-02-11 15:23:09 -05:00
|
|
|
"path/filepath"
|
2015-07-14 18:09:25 -04:00
|
|
|
"strings"
|
2015-02-11 15:23:09 -05:00
|
|
|
|
|
|
|
"golang.org/x/mobile/bind"
|
2016-09-07 18:27:36 +02:00
|
|
|
"golang.org/x/mobile/internal/importers"
|
|
|
|
"golang.org/x/mobile/internal/importers/java"
|
2015-02-11 15:23:09 -05:00
|
|
|
)
|
|
|
|
|
2015-07-10 16:47:46 -06:00
|
|
|
// ctx, pkg, tmpdir in build.go
|
2015-02-11 15:23:09 -05:00
|
|
|
|
|
|
|
var cmdBind = &command{
|
|
|
|
run: runBind,
|
|
|
|
Name: "bind",
|
2015-06-19 12:27:15 -04:00
|
|
|
Usage: "[-target android|ios] [-o output] [build flags] [package]",
|
2015-08-29 16:27:39 -07:00
|
|
|
Short: "build a library for Android and iOS",
|
2015-02-11 15:23:09 -05:00
|
|
|
Long: `
|
2015-06-19 12:27:15 -04:00
|
|
|
Bind generates language bindings for the package named by the import
|
|
|
|
path, and compiles a library for the named target system.
|
|
|
|
|
|
|
|
The -target flag takes a target system name, either android (the
|
|
|
|
default) or ios.
|
|
|
|
|
|
|
|
For -target android, the bind command produces an AAR (Android ARchive)
|
|
|
|
file that archives the precompiled Java API stub classes, the compiled
|
|
|
|
shared libraries, and all asset files in the /assets subdirectory under
|
|
|
|
the package directory. The output is named '<package_name>.aar' by
|
|
|
|
default. This AAR file is commonly used for binary distribution of an
|
|
|
|
Android library project and most Android IDEs support AAR import. For
|
|
|
|
example, in Android Studio (1.2+), an AAR file can be imported using
|
|
|
|
the module import wizard (File > New > New Module > Import .JAR or
|
|
|
|
.AAR package), and setting it as a new dependency
|
|
|
|
(File > Project Structure > Dependencies). This requires 'javac'
|
2016-07-07 11:36:51 -04:00
|
|
|
(version 1.7+) and Android SDK (API level 15 or newer) to build the
|
2015-06-19 12:27:15 -04:00
|
|
|
library for Android. The environment variable ANDROID_HOME must be set
|
2015-08-28 13:39:30 -04:00
|
|
|
to the path to Android SDK. The generated Java class is in the java
|
|
|
|
package 'go.<package_name>' unless -javapkg flag is specified.
|
2015-06-19 12:27:15 -04:00
|
|
|
|
2016-06-14 10:43:25 -04:00
|
|
|
By default, -target=android builds shared libraries for all supported
|
|
|
|
instruction sets (arm, arm64, 386, amd64). A subset of instruction sets
|
|
|
|
can be selected by specifying target type with the architecture name. E.g.,
|
|
|
|
-target=android/arm,android/386.
|
|
|
|
|
2015-06-19 12:27:15 -04:00
|
|
|
For -target ios, gomobile must be run on an OS X machine with Xcode
|
2015-08-28 13:39:30 -04:00
|
|
|
installed. Support is not complete. The generated Objective-C types
|
|
|
|
are prefixed with 'Go' unless the -prefix flag is provided.
|
2015-02-11 15:23:09 -05:00
|
|
|
|
|
|
|
The -v flag provides verbose output, including the list of packages built.
|
|
|
|
|
2015-11-30 14:01:47 -05:00
|
|
|
The build flags -a, -n, -x, -gcflags, -ldflags, -tags, and -work
|
2015-07-27 16:33:08 -04:00
|
|
|
are shared with the build command. For documentation, see 'go help build'.
|
2015-02-11 15:23:09 -05:00
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
|
|
|
func runBind(cmd *command) error {
|
2015-07-16 13:32:51 -04:00
|
|
|
cleanup, err := buildEnvInit()
|
2015-02-11 15:23:09 -05:00
|
|
|
if err != nil {
|
2015-07-10 16:47:46 -06:00
|
|
|
return err
|
2015-02-11 15:23:09 -05:00
|
|
|
}
|
2015-07-10 16:47:46 -06:00
|
|
|
defer cleanup()
|
|
|
|
|
2015-02-11 15:23:09 -05:00
|
|
|
args := cmd.flag.Args()
|
|
|
|
|
2015-12-11 18:36:28 -05:00
|
|
|
targetOS, targetArchs, err := parseBuildTarget(buildTarget)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(`invalid -target=%q: %v`, buildTarget, err)
|
2015-07-24 07:04:40 -04:00
|
|
|
}
|
|
|
|
|
2015-12-11 18:36:28 -05:00
|
|
|
ctx.GOARCH = "arm"
|
|
|
|
ctx.GOOS = targetOS
|
|
|
|
|
2015-08-28 13:39:30 -04:00
|
|
|
if bindJavaPkg != "" && ctx.GOOS != "android" {
|
|
|
|
return fmt.Errorf("-javapkg is supported only for android target")
|
|
|
|
}
|
|
|
|
if bindPrefix != "" && ctx.GOOS != "darwin" {
|
|
|
|
return fmt.Errorf("-prefix is supported only for ios target")
|
|
|
|
}
|
|
|
|
|
2015-11-19 10:51:29 +05:30
|
|
|
var pkgs []*build.Package
|
2015-02-11 15:23:09 -05:00
|
|
|
switch len(args) {
|
|
|
|
case 0:
|
2015-11-19 10:51:29 +05:30
|
|
|
pkgs = make([]*build.Package, 1)
|
|
|
|
pkgs[0], err = ctx.ImportDir(cwd, build.ImportComment)
|
2015-02-11 15:23:09 -05:00
|
|
|
default:
|
2015-11-19 10:51:29 +05:30
|
|
|
pkgs, err = importPackages(args)
|
2015-02-11 15:23:09 -05:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-05-05 09:22:14 -07:00
|
|
|
// check if any of the package is main
|
|
|
|
for _, pkg := range pkgs {
|
|
|
|
if pkg.Name == "main" {
|
|
|
|
return fmt.Errorf("binding 'main' package (%s) is not supported", pkg.ImportComment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-18 12:04:02 -05:00
|
|
|
switch targetOS {
|
2015-06-19 12:27:15 -04:00
|
|
|
case "android":
|
2015-12-11 18:36:28 -05:00
|
|
|
return goAndroidBind(pkgs, targetArchs)
|
2015-12-18 12:04:02 -05:00
|
|
|
case "darwin":
|
2015-12-11 18:36:28 -05:00
|
|
|
// TODO: use targetArchs?
|
2015-11-19 10:51:29 +05:30
|
|
|
return goIOSBind(pkgs)
|
2015-07-24 16:02:39 -04:00
|
|
|
default:
|
2015-12-11 18:36:28 -05:00
|
|
|
return fmt.Errorf(`invalid -target=%q`, buildTarget)
|
2015-06-19 12:27:15 -04:00
|
|
|
}
|
2015-02-11 15:23:09 -05:00
|
|
|
}
|
|
|
|
|
2015-11-19 10:51:29 +05:30
|
|
|
func importPackages(args []string) ([]*build.Package, error) {
|
|
|
|
pkgs := make([]*build.Package, len(args))
|
|
|
|
for i, path := range args {
|
|
|
|
var err error
|
|
|
|
if pkgs[i], err = ctx.Import(path, cwd, build.ImportComment); err != nil {
|
|
|
|
return nil, fmt.Errorf("package %q: %v", path, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pkgs, nil
|
|
|
|
}
|
|
|
|
|
2015-08-28 13:39:30 -04:00
|
|
|
var (
|
|
|
|
bindPrefix string // -prefix
|
|
|
|
bindJavaPkg string // -javapkg
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// bind command specific commands.
|
|
|
|
cmdBind.flag.StringVar(&bindJavaPkg, "javapkg", "",
|
2016-08-20 21:10:46 +02:00
|
|
|
"specifies custom Java package path prefix used instead of the default 'go'. Valid only with -target=android.")
|
2015-08-28 13:39:30 -04:00
|
|
|
cmdBind.flag.StringVar(&bindPrefix, "prefix", "",
|
|
|
|
"custom Objective-C name prefix used instead of the default 'Go'. Valid only with -lang=ios.")
|
|
|
|
}
|
|
|
|
|
2015-02-11 15:23:09 -05:00
|
|
|
type binder struct {
|
|
|
|
files []*ast.File
|
|
|
|
fset *token.FileSet
|
2015-11-19 10:51:29 +05:30
|
|
|
pkgs []*types.Package
|
2015-02-11 15:23:09 -05: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
|
|
|
func (b *binder) GenGoSupport(outdir string) error {
|
|
|
|
bindPkg, err := ctx.Import("golang.org/x/mobile/bind", "", build.FindOnly)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return copyFile(filepath.Join(outdir, "seq.go"), filepath.Join(bindPkg.Dir, "seq.go.support"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *binder) GenObjcSupport(outdir string) error {
|
|
|
|
objcPkg, err := ctx.Import("golang.org/x/mobile/bind/objc", "", build.FindOnly)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := copyFile(filepath.Join(outdir, "seq_darwin.m"), filepath.Join(objcPkg.Dir, "seq_darwin.m.support")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := copyFile(filepath.Join(outdir, "seq_darwin.go"), filepath.Join(objcPkg.Dir, "seq_darwin.go.support")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return copyFile(filepath.Join(outdir, "seq.h"), filepath.Join(objcPkg.Dir, "seq.h"))
|
|
|
|
}
|
|
|
|
|
2016-03-11 11:52:33 +01:00
|
|
|
func (b *binder) GenObjc(pkg *types.Package, allPkg []*types.Package, outdir string) (string, error) {
|
2015-10-18 21:37:57 -07:00
|
|
|
const bindPrefixDefault = "Go"
|
mobile/bind: use objects to pass errors across the language barrier
Gobind uses strings for passing errors across the language barrier.
However, since Gobind doesn't have a concept of a nil string, it
can't separate an empty native string from a nil string.
In turn, that means that empty errors, exceptions or NSError * with
an empty description are treated as no error. With ObjC, empty errors
are replaced with a default string to workaround the issue, while
with Java empty errors are silently ignored.
Fix this by replacing strings with actual error objects, wrapping
the Go error, Java Throwable or ObjC NSError *, and letting the
existing bind machinery take care of passing the references across.
It's a large change for a small corner case, but I believe objects
are a better fit for exception that strings. Error objects also
naturally leads to future additions, for example accessing the
exception class name or chained exception.
Change-Id: Ie03b47cafcb231ad1e12a80195693fa7459c6265
Reviewed-on: https://go-review.googlesource.com/24100
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-06-14 11:58:31 +02:00
|
|
|
if bindPrefix == "" || pkg == nil {
|
2015-10-18 21:37:57 -07:00
|
|
|
bindPrefix = bindPrefixDefault
|
|
|
|
}
|
mobile/bind: use objects to pass errors across the language barrier
Gobind uses strings for passing errors across the language barrier.
However, since Gobind doesn't have a concept of a nil string, it
can't separate an empty native string from a nil string.
In turn, that means that empty errors, exceptions or NSError * with
an empty description are treated as no error. With ObjC, empty errors
are replaced with a default string to workaround the issue, while
with Java empty errors are silently ignored.
Fix this by replacing strings with actual error objects, wrapping
the Go error, Java Throwable or ObjC NSError *, and letting the
existing bind machinery take care of passing the references across.
It's a large change for a small corner case, but I believe objects
are a better fit for exception that strings. Error objects also
naturally leads to future additions, for example accessing the
exception class name or chained exception.
Change-Id: Ie03b47cafcb231ad1e12a80195693fa7459c6265
Reviewed-on: https://go-review.googlesource.com/24100
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-06-14 11:58:31 +02:00
|
|
|
pkgName := ""
|
|
|
|
pkgPath := ""
|
|
|
|
if pkg != nil {
|
|
|
|
pkgName = pkg.Name()
|
|
|
|
pkgPath = pkg.Path()
|
|
|
|
} else {
|
|
|
|
pkgName = "universe"
|
|
|
|
}
|
2015-08-28 13:39:30 -04:00
|
|
|
bindOption := "-lang=objc"
|
2015-10-18 21:37:57 -07:00
|
|
|
if bindPrefix != bindPrefixDefault {
|
2015-08-28 13:39:30 -04:00
|
|
|
bindOption += " -prefix=" + bindPrefix
|
2015-07-14 18:09:25 -04:00
|
|
|
}
|
|
|
|
|
mobile/bind: use objects to pass errors across the language barrier
Gobind uses strings for passing errors across the language barrier.
However, since Gobind doesn't have a concept of a nil string, it
can't separate an empty native string from a nil string.
In turn, that means that empty errors, exceptions or NSError * with
an empty description are treated as no error. With ObjC, empty errors
are replaced with a default string to workaround the issue, while
with Java empty errors are silently ignored.
Fix this by replacing strings with actual error objects, wrapping
the Go error, Java Throwable or ObjC NSError *, and letting the
existing bind machinery take care of passing the references across.
It's a large change for a small corner case, but I believe objects
are a better fit for exception that strings. Error objects also
naturally leads to future additions, for example accessing the
exception class name or chained exception.
Change-Id: Ie03b47cafcb231ad1e12a80195693fa7459c6265
Reviewed-on: https://go-review.googlesource.com/24100
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-06-14 11:58:31 +02:00
|
|
|
fileBase := bindPrefix + strings.Title(pkgName)
|
2015-12-01 09:37:56 +05:30
|
|
|
mfile := filepath.Join(outdir, fileBase+".m")
|
|
|
|
hfile := filepath.Join(outdir, fileBase+".h")
|
mobile/bind: use objects to pass errors across the language barrier
Gobind uses strings for passing errors across the language barrier.
However, since Gobind doesn't have a concept of a nil string, it
can't separate an empty native string from a nil string.
In turn, that means that empty errors, exceptions or NSError * with
an empty description are treated as no error. With ObjC, empty errors
are replaced with a default string to workaround the issue, while
with Java empty errors are silently ignored.
Fix this by replacing strings with actual error objects, wrapping
the Go error, Java Throwable or ObjC NSError *, and letting the
existing bind machinery take care of passing the references across.
It's a large change for a small corner case, but I believe objects
are a better fit for exception that strings. Error objects also
naturally leads to future additions, for example accessing the
exception class name or chained exception.
Change-Id: Ie03b47cafcb231ad1e12a80195693fa7459c6265
Reviewed-on: https://go-review.googlesource.com/24100
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-06-14 11:58:31 +02:00
|
|
|
gohfile := filepath.Join(outdir, pkgName+".h")
|
2015-08-27 12:19:47 -04:00
|
|
|
|
2016-03-11 11:52:33 +01:00
|
|
|
conf := &bind.GeneratorConfig{
|
|
|
|
Fset: b.fset,
|
|
|
|
Pkg: pkg,
|
|
|
|
AllPkg: allPkg,
|
|
|
|
}
|
2015-07-14 18:09:25 -04:00
|
|
|
generate := func(w io.Writer) error {
|
2015-08-28 13:39:30 -04:00
|
|
|
if buildX {
|
mobile/bind: use objects to pass errors across the language barrier
Gobind uses strings for passing errors across the language barrier.
However, since Gobind doesn't have a concept of a nil string, it
can't separate an empty native string from a nil string.
In turn, that means that empty errors, exceptions or NSError * with
an empty description are treated as no error. With ObjC, empty errors
are replaced with a default string to workaround the issue, while
with Java empty errors are silently ignored.
Fix this by replacing strings with actual error objects, wrapping
the Go error, Java Throwable or ObjC NSError *, and letting the
existing bind machinery take care of passing the references across.
It's a large change for a small corner case, but I believe objects
are a better fit for exception that strings. Error objects also
naturally leads to future additions, for example accessing the
exception class name or chained exception.
Change-Id: Ie03b47cafcb231ad1e12a80195693fa7459c6265
Reviewed-on: https://go-review.googlesource.com/24100
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-06-14 11:58:31 +02:00
|
|
|
printcmd("gobind %s -outdir=%s %s", bindOption, outdir, pkgPath)
|
2015-08-28 13:39:30 -04:00
|
|
|
}
|
2015-11-20 11:26:00 -05:00
|
|
|
if buildN {
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-11 11:52:33 +01:00
|
|
|
conf.Writer = w
|
|
|
|
return bind.GenObjc(conf, bindPrefix, bind.ObjcM)
|
2015-07-14 18:09:25 -04:00
|
|
|
}
|
|
|
|
if err := writeFile(mfile, generate); err != nil {
|
2015-12-01 09:37:56 +05:30
|
|
|
return "", err
|
2015-07-14 18:09:25 -04:00
|
|
|
}
|
|
|
|
generate = func(w io.Writer) error {
|
2015-11-20 11:26:00 -05:00
|
|
|
if buildN {
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-11 11:52:33 +01:00
|
|
|
conf.Writer = w
|
|
|
|
return bind.GenObjc(conf, bindPrefix, bind.ObjcH)
|
2015-07-14 18:09:25 -04:00
|
|
|
}
|
|
|
|
if err := writeFile(hfile, generate); err != nil {
|
2015-12-01 09:37:56 +05:30
|
|
|
return "", err
|
2015-07-14 18:09:25 -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
|
|
|
generate = func(w io.Writer) error {
|
|
|
|
if buildN {
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-11 11:52:33 +01:00
|
|
|
conf.Writer = w
|
|
|
|
return bind.GenObjc(conf, bindPrefix, bind.ObjcGoH)
|
2015-12-01 09:37:56 +05:30
|
|
|
}
|
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 err := writeFile(gohfile, generate); err != nil {
|
2015-12-01 09:37:56 +05:30
|
|
|
return "", err
|
2015-07-14 18:09:25 -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
|
|
|
|
2015-12-01 09:37:56 +05:30
|
|
|
return fileBase, nil
|
2015-07-14 18:09:25 -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
|
|
|
func (b *binder) GenJavaSupport(outdir string) error {
|
|
|
|
javaPkg, err := ctx.Import("golang.org/x/mobile/bind/java", "", build.FindOnly)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := copyFile(filepath.Join(outdir, "seq_android.go"), filepath.Join(javaPkg.Dir, "seq_android.go.support")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := copyFile(filepath.Join(outdir, "seq_android.c"), filepath.Join(javaPkg.Dir, "seq_android.c.support")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return copyFile(filepath.Join(outdir, "seq.h"), filepath.Join(javaPkg.Dir, "seq.h"))
|
|
|
|
}
|
|
|
|
|
2016-09-07 18:27:36 +02:00
|
|
|
func GenClasses(pkgs []*build.Package, srcDir, jpkgSrc string) ([]*java.Class, error) {
|
|
|
|
apiPath, err := androidAPIPath()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
refs, err := importers.AnalyzePackages(pkgs, "Java/")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
classes, err := java.Import(filepath.Join(apiPath, "android.jar"), refs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
g := &bind.ClassGen{
|
|
|
|
Printer: &bind.Printer{
|
|
|
|
IndentEach: []byte("\t"),
|
|
|
|
Buf: &buf,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
g.Init(classes)
|
|
|
|
for i, jpkg := range g.Packages() {
|
|
|
|
pkgDir := filepath.Join(jpkgSrc, "src", "Java", jpkg)
|
|
|
|
if err := os.MkdirAll(pkgDir, 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pkgFile := filepath.Join(pkgDir, "package.go")
|
|
|
|
generate := func(w io.Writer) error {
|
|
|
|
if buildN {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
buf.Reset()
|
|
|
|
g.GenPackage(i)
|
|
|
|
_, err := io.Copy(w, &buf)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := writeFile(pkgFile, generate); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create the Java wrapper package %s: %v", jpkg, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
generate := func(w io.Writer) error {
|
|
|
|
if buildN {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
buf.Reset()
|
|
|
|
g.GenGo()
|
|
|
|
_, err := io.Copy(w, &buf)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := writeFile(filepath.Join(srcDir, "classes.go"), generate); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create the Java classes Go file: %v", err)
|
|
|
|
}
|
|
|
|
generate = func(w io.Writer) error {
|
|
|
|
if buildN {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
buf.Reset()
|
|
|
|
g.GenH()
|
|
|
|
_, err := io.Copy(w, &buf)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := writeFile(filepath.Join(srcDir, "classes.h"), generate); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create the Java classes header file: %v", err)
|
|
|
|
}
|
|
|
|
generate = func(w io.Writer) error {
|
|
|
|
if buildN {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
buf.Reset()
|
|
|
|
g.GenC()
|
|
|
|
_, err := io.Copy(w, &buf)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := writeFile(filepath.Join(srcDir, "classes.c"), generate); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create the Java classes C file: %v", err)
|
|
|
|
}
|
|
|
|
generate = func(w io.Writer) error {
|
|
|
|
if buildN {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
buf.Reset()
|
|
|
|
g.GenInterfaces()
|
|
|
|
_, err := io.Copy(w, &buf)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := writeFile(filepath.Join(jpkgSrc, "src", "Java", "interfaces.go"), generate); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create the Java classes interfaces file: %v", err)
|
|
|
|
}
|
|
|
|
return classes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *binder) GenJava(pkg *types.Package, allPkg []*types.Package, classes []*java.Class, outdir, javadir string) error {
|
mobile/bind: use objects to pass errors across the language barrier
Gobind uses strings for passing errors across the language barrier.
However, since Gobind doesn't have a concept of a nil string, it
can't separate an empty native string from a nil string.
In turn, that means that empty errors, exceptions or NSError * with
an empty description are treated as no error. With ObjC, empty errors
are replaced with a default string to workaround the issue, while
with Java empty errors are silently ignored.
Fix this by replacing strings with actual error objects, wrapping
the Go error, Java Throwable or ObjC NSError *, and letting the
existing bind machinery take care of passing the references across.
It's a large change for a small corner case, but I believe objects
are a better fit for exception that strings. Error objects also
naturally leads to future additions, for example accessing the
exception class name or chained exception.
Change-Id: Ie03b47cafcb231ad1e12a80195693fa7459c6265
Reviewed-on: https://go-review.googlesource.com/24100
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-06-14 11:58:31 +02:00
|
|
|
var className string
|
|
|
|
pkgName := ""
|
|
|
|
pkgPath := ""
|
|
|
|
javaPkg := ""
|
|
|
|
if pkg != nil {
|
|
|
|
className = strings.Title(pkg.Name())
|
|
|
|
pkgName = pkg.Name()
|
|
|
|
pkgPath = pkg.Path()
|
|
|
|
javaPkg = bindJavaPkg
|
|
|
|
} else {
|
|
|
|
pkgName = "universe"
|
|
|
|
className = "Universe"
|
|
|
|
}
|
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
|
|
|
javaFile := filepath.Join(javadir, className+".java")
|
mobile/bind: use objects to pass errors across the language barrier
Gobind uses strings for passing errors across the language barrier.
However, since Gobind doesn't have a concept of a nil string, it
can't separate an empty native string from a nil string.
In turn, that means that empty errors, exceptions or NSError * with
an empty description are treated as no error. With ObjC, empty errors
are replaced with a default string to workaround the issue, while
with Java empty errors are silently ignored.
Fix this by replacing strings with actual error objects, wrapping
the Go error, Java Throwable or ObjC NSError *, and letting the
existing bind machinery take care of passing the references across.
It's a large change for a small corner case, but I believe objects
are a better fit for exception that strings. Error objects also
naturally leads to future additions, for example accessing the
exception class name or chained exception.
Change-Id: Ie03b47cafcb231ad1e12a80195693fa7459c6265
Reviewed-on: https://go-review.googlesource.com/24100
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-06-14 11:58:31 +02:00
|
|
|
cFile := filepath.Join(outdir, "java_"+pkgName+".c")
|
|
|
|
hFile := filepath.Join(outdir, pkgName+".h")
|
2015-08-28 13:39:30 -04:00
|
|
|
bindOption := "-lang=java"
|
mobile/bind: use objects to pass errors across the language barrier
Gobind uses strings for passing errors across the language barrier.
However, since Gobind doesn't have a concept of a nil string, it
can't separate an empty native string from a nil string.
In turn, that means that empty errors, exceptions or NSError * with
an empty description are treated as no error. With ObjC, empty errors
are replaced with a default string to workaround the issue, while
with Java empty errors are silently ignored.
Fix this by replacing strings with actual error objects, wrapping
the Go error, Java Throwable or ObjC NSError *, and letting the
existing bind machinery take care of passing the references across.
It's a large change for a small corner case, but I believe objects
are a better fit for exception that strings. Error objects also
naturally leads to future additions, for example accessing the
exception class name or chained exception.
Change-Id: Ie03b47cafcb231ad1e12a80195693fa7459c6265
Reviewed-on: https://go-review.googlesource.com/24100
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-06-14 11:58:31 +02:00
|
|
|
if javaPkg != "" {
|
|
|
|
bindOption += " -javapkg=" + javaPkg
|
2015-02-11 15:23:09 -05:00
|
|
|
}
|
|
|
|
|
2016-08-20 21:10:46 +02:00
|
|
|
var buf bytes.Buffer
|
|
|
|
g := &bind.JavaGen{
|
|
|
|
JavaPkg: javaPkg,
|
|
|
|
Generator: &bind.Generator{
|
|
|
|
Printer: &bind.Printer{Buf: &buf, IndentEach: []byte(" ")},
|
|
|
|
Fset: b.fset,
|
|
|
|
AllPkg: allPkg,
|
|
|
|
Pkg: pkg,
|
|
|
|
},
|
2016-03-11 11:52:33 +01:00
|
|
|
}
|
2016-09-16 19:19:01 +02:00
|
|
|
g.Init(classes)
|
2016-08-20 21:10:46 +02:00
|
|
|
|
2015-02-11 15:23:09 -05:00
|
|
|
generate := func(w io.Writer) error {
|
2015-08-28 13:39:30 -04:00
|
|
|
if buildX {
|
mobile/bind: use objects to pass errors across the language barrier
Gobind uses strings for passing errors across the language barrier.
However, since Gobind doesn't have a concept of a nil string, it
can't separate an empty native string from a nil string.
In turn, that means that empty errors, exceptions or NSError * with
an empty description are treated as no error. With ObjC, empty errors
are replaced with a default string to workaround the issue, while
with Java empty errors are silently ignored.
Fix this by replacing strings with actual error objects, wrapping
the Go error, Java Throwable or ObjC NSError *, and letting the
existing bind machinery take care of passing the references across.
It's a large change for a small corner case, but I believe objects
are a better fit for exception that strings. Error objects also
naturally leads to future additions, for example accessing the
exception class name or chained exception.
Change-Id: Ie03b47cafcb231ad1e12a80195693fa7459c6265
Reviewed-on: https://go-review.googlesource.com/24100
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-06-14 11:58:31 +02:00
|
|
|
printcmd("gobind %s -outdir=%s %s", bindOption, javadir, pkgPath)
|
2015-08-28 13:39:30 -04:00
|
|
|
}
|
2015-11-20 11:26:00 -05:00
|
|
|
if buildN {
|
|
|
|
return nil
|
|
|
|
}
|
2016-08-20 21:10:46 +02:00
|
|
|
buf.Reset()
|
|
|
|
if err := g.GenJava(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := io.Copy(w, &buf)
|
|
|
|
return err
|
2015-02-11 15:23:09 -05:00
|
|
|
}
|
|
|
|
if err := writeFile(javaFile, generate); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-20 21:10:46 +02:00
|
|
|
for i, name := range g.ClassNames() {
|
|
|
|
generate := func(w io.Writer) error {
|
|
|
|
if buildN {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
buf.Reset()
|
|
|
|
if err := g.GenClass(i); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := io.Copy(w, &buf)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
classFile := filepath.Join(javadir, name+".java")
|
|
|
|
if err := writeFile(classFile, generate); err != nil {
|
|
|
|
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
|
|
|
generate = func(w io.Writer) error {
|
|
|
|
if buildN {
|
|
|
|
return nil
|
|
|
|
}
|
2016-08-20 21:10:46 +02:00
|
|
|
buf.Reset()
|
|
|
|
if err := g.GenC(); err != nil {
|
|
|
|
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
|
|
|
}
|
|
|
|
if err := writeFile(cFile, generate); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
generate = func(w io.Writer) error {
|
|
|
|
if buildN {
|
|
|
|
return nil
|
|
|
|
}
|
2016-08-20 21:10:46 +02:00
|
|
|
buf.Reset()
|
|
|
|
if err := g.GenH(); err != nil {
|
|
|
|
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
|
|
|
}
|
|
|
|
return writeFile(hFile, generate)
|
2015-02-11 15:23:09 -05:00
|
|
|
}
|
|
|
|
|
2016-03-11 11:52:33 +01:00
|
|
|
func (b *binder) GenGo(pkg *types.Package, allPkg []*types.Package, outdir string) error {
|
mobile/bind: use objects to pass errors across the language barrier
Gobind uses strings for passing errors across the language barrier.
However, since Gobind doesn't have a concept of a nil string, it
can't separate an empty native string from a nil string.
In turn, that means that empty errors, exceptions or NSError * with
an empty description are treated as no error. With ObjC, empty errors
are replaced with a default string to workaround the issue, while
with Java empty errors are silently ignored.
Fix this by replacing strings with actual error objects, wrapping
the Go error, Java Throwable or ObjC NSError *, and letting the
existing bind machinery take care of passing the references across.
It's a large change for a small corner case, but I believe objects
are a better fit for exception that strings. Error objects also
naturally leads to future additions, for example accessing the
exception class name or chained exception.
Change-Id: Ie03b47cafcb231ad1e12a80195693fa7459c6265
Reviewed-on: https://go-review.googlesource.com/24100
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-06-14 11:58:31 +02:00
|
|
|
pkgName := "go_"
|
|
|
|
pkgPath := ""
|
|
|
|
if pkg != nil {
|
|
|
|
pkgName += pkg.Name()
|
|
|
|
pkgPath = pkg.Path()
|
|
|
|
}
|
2015-08-28 13:39:30 -04:00
|
|
|
goFile := filepath.Join(outdir, pkgName+"main.go")
|
2015-02-11 15:23:09 -05:00
|
|
|
|
|
|
|
generate := func(w io.Writer) error {
|
2015-08-28 13:39:30 -04:00
|
|
|
if buildX {
|
mobile/bind: use objects to pass errors across the language barrier
Gobind uses strings for passing errors across the language barrier.
However, since Gobind doesn't have a concept of a nil string, it
can't separate an empty native string from a nil string.
In turn, that means that empty errors, exceptions or NSError * with
an empty description are treated as no error. With ObjC, empty errors
are replaced with a default string to workaround the issue, while
with Java empty errors are silently ignored.
Fix this by replacing strings with actual error objects, wrapping
the Go error, Java Throwable or ObjC NSError *, and letting the
existing bind machinery take care of passing the references across.
It's a large change for a small corner case, but I believe objects
are a better fit for exception that strings. Error objects also
naturally leads to future additions, for example accessing the
exception class name or chained exception.
Change-Id: Ie03b47cafcb231ad1e12a80195693fa7459c6265
Reviewed-on: https://go-review.googlesource.com/24100
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-06-14 11:58:31 +02:00
|
|
|
printcmd("gobind -lang=go -outdir=%s %s", outdir, pkgPath)
|
2015-08-28 13:39:30 -04:00
|
|
|
}
|
2015-11-20 11:26:00 -05:00
|
|
|
if buildN {
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-11 11:52:33 +01:00
|
|
|
conf := &bind.GeneratorConfig{
|
|
|
|
Writer: w,
|
|
|
|
Fset: b.fset,
|
|
|
|
Pkg: pkg,
|
|
|
|
AllPkg: allPkg,
|
|
|
|
}
|
|
|
|
return bind.GenGo(conf)
|
2015-02-11 15:23:09 -05:00
|
|
|
}
|
|
|
|
if err := writeFile(goFile, generate); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-14 18:09:25 -04:00
|
|
|
func copyFile(dst, src string) error {
|
|
|
|
if buildX {
|
|
|
|
printcmd("cp %s %s", src, dst)
|
|
|
|
}
|
|
|
|
return writeFile(dst, func(w io.Writer) error {
|
2015-07-19 21:29:35 -04:00
|
|
|
if buildN {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
f, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if _, err := io.Copy(w, f); err != nil {
|
|
|
|
return fmt.Errorf("cp %s %s failed: %v", src, dst, err)
|
|
|
|
}
|
|
|
|
return nil
|
2015-07-14 18:09:25 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-02-11 15:23:09 -05:00
|
|
|
func writeFile(filename string, generate func(io.Writer) error) error {
|
|
|
|
if buildV {
|
|
|
|
fmt.Fprintf(os.Stderr, "write %s\n", filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := mkdir(filepath.Dir(filename))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if buildN {
|
|
|
|
return generate(ioutil.Discard)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if cerr := f.Close(); err == nil {
|
|
|
|
err = cerr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return generate(f)
|
|
|
|
}
|
|
|
|
|
2015-11-19 10:51:29 +05:30
|
|
|
func loadExportData(pkgs []*build.Package, env []string, args ...string) ([]*types.Package, error) {
|
2015-11-13 13:57:15 -05:00
|
|
|
// Compile the package. This will produce good errors if the package
|
|
|
|
// doesn't typecheck for some reason, and is a necessary step to
|
|
|
|
// building the final output anyway.
|
2015-11-19 10:51:29 +05:30
|
|
|
paths := make([]string, len(pkgs))
|
|
|
|
for i, p := range pkgs {
|
|
|
|
paths[i] = p.ImportPath
|
|
|
|
}
|
|
|
|
if err := goInstall(paths, env, args...); err != nil {
|
2015-11-13 13:57:15 -05:00
|
|
|
return nil, err
|
2015-02-11 15:23:09 -05:00
|
|
|
}
|
|
|
|
|
2015-12-11 17:19:23 -05:00
|
|
|
goos, goarch := getenv(env, "GOOS"), getenv(env, "GOARCH")
|
|
|
|
|
2015-11-13 13:57:15 -05:00
|
|
|
// Assemble a fake GOPATH and trick go/importer into using it.
|
|
|
|
// Ideally the importer package would let us provide this to
|
|
|
|
// it somehow, but this works with what's in Go 1.5 today and
|
|
|
|
// gives us access to the gcimporter package without us having
|
|
|
|
// to make a copy of it.
|
|
|
|
fakegopath := filepath.Join(tmpdir, "fakegopath")
|
|
|
|
if err := removeAll(fakegopath); err != nil {
|
|
|
|
return nil, err
|
2015-02-11 15:23:09 -05:00
|
|
|
}
|
2015-11-13 13:57:15 -05:00
|
|
|
if err := mkdir(filepath.Join(fakegopath, "pkg")); err != nil {
|
|
|
|
return nil, err
|
2015-02-11 15:23:09 -05:00
|
|
|
}
|
2015-11-19 10:51:29 +05:30
|
|
|
typePkgs := make([]*types.Package, len(pkgs))
|
2016-03-21 20:22:15 +01:00
|
|
|
imp := importer.Default()
|
2015-11-19 10:51:29 +05:30
|
|
|
for i, p := range pkgs {
|
|
|
|
importPath := p.ImportPath
|
|
|
|
src := filepath.Join(pkgdir(env), importPath+".a")
|
2015-12-11 17:19:23 -05:00
|
|
|
dst := filepath.Join(fakegopath, "pkg/"+goos+"_"+goarch+"/"+importPath+".a")
|
2015-11-19 10:51:29 +05:30
|
|
|
if err := copyFile(dst, src); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if buildN {
|
|
|
|
typePkgs[i] = types.NewPackage(importPath, path.Base(importPath))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
oldDefault := build.Default
|
|
|
|
build.Default = ctx // copy
|
2015-12-11 17:19:23 -05:00
|
|
|
build.Default.GOARCH = goarch
|
2015-11-19 10:51:29 +05:30
|
|
|
build.Default.GOPATH = fakegopath
|
2016-03-21 20:22:15 +01:00
|
|
|
p, err := imp.Import(importPath)
|
2015-11-19 10:51:29 +05:30
|
|
|
build.Default = oldDefault
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
typePkgs[i] = p
|
2015-02-11 15:23:09 -05:00
|
|
|
}
|
2015-11-19 10:51:29 +05:30
|
|
|
return typePkgs, nil
|
2015-11-13 13:57:15 -05:00
|
|
|
}
|
|
|
|
|
2015-11-19 10:51:29 +05:30
|
|
|
func newBinder(pkgs []*types.Package) (*binder, error) {
|
|
|
|
for _, pkg := range pkgs {
|
|
|
|
if pkg.Name() == "main" {
|
|
|
|
return nil, fmt.Errorf("package %q (%q): can only bind a library package", pkg.Name(), pkg.Path())
|
|
|
|
}
|
2015-11-13 13:57:15 -05:00
|
|
|
}
|
2015-02-11 15:23:09 -05:00
|
|
|
b := &binder{
|
2015-11-13 13:57:15 -05:00
|
|
|
fset: token.NewFileSet(),
|
2015-11-19 10:51:29 +05:30
|
|
|
pkgs: pkgs,
|
2015-02-11 15:23:09 -05:00
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|