2
0
mirror of synced 2025-02-23 14:58:12 +00:00

114 Commits

Author SHA1 Message Date
Elias Naur
f16143114e bind, cmd/gobind/test: remove GOOS build tags from test packages
The gobind command is about to get more powerful and able to generate
complete and standalone bindings. Platform specific build tags based
on GOOS or GOARCH are now meaningless to generate bindings from, so
remove them from the test packages.

The tags mattered to the reverse bound packages, since the go tool can't
build them without the Go wrappers for the imported Java packages.
Before this CL, the `android` tag was used to fool the go tool since
the host GOOS is unlikely to be android.

A fix is to check in the generated Go wrappers, but since the
packages are for testing we don't want that. Instead, move the test
packages to the testdata directory so the go tool ignores them.

Change-Id: I57178e930a400f690ebd7a65758bed894eeb10b0
Reviewed-on: https://go-review.googlesource.com/99315
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2018-03-14 22:25:02 +00:00
Elias Naur
4113bce085 bind/java: remove android support library dependency in test
Remove the android support library dependency.

The Android build system assumes at least one resource, so
add a dummy string.

Change-Id: I7223b75709f1aa5907e6d04bc656eea5503bb640
Reviewed-on: https://go-review.googlesource.com/94195
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2018-02-21 20:07:02 +00:00
Elias Naur
0879aa9afa bind: support types with the same title name as their packages
If a Go struct or interface has the same name as its package class,
append an underscore to the generated Java class name.

Fixes golang/go#23327.

Change-Id: Ib680af35c956801073a0effb510a3ed9bbb8b9d1
Reviewed-on: https://go-review.googlesource.com/87656
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2018-01-17 09:45:22 +00:00
Tad Fisher
50b7067763 bind/java: fix build on NDK r16
Import <string.h> to provide a memcpy declaration.

Fixes golang/go#22766

Change-Id: I0762a1bb9d8d30bb1ae6f1a98648795ea57b0913
Reviewed-on: https://go-review.googlesource.com/79499
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-12-17 15:46:28 +00:00
Péter Szilágyi
44a54e9b78 bind: accept null objects as nil pointers
Currently the generated bindings assume that any object
passed to Go as a method argument is actually a valid one
originating from Go. The `null` object is however a corner
case to this assumption, which should be accepted for Go
pointer types, since they can cleanly convert into `nil`.

This CL modifies the generated wrapper code so any `nil`
reference is permitted for Go pointer types, which until
now produced a nil pointer dereference error.

Fixes golang/go#20330

Change-Id: If1ab9cf9df7ac3808486d23ccf2db8d32fb89426
Reviewed-on: https://go-review.googlesource.com/43253
Reviewed-by: Elias Naur <elias.naur@gmail.com>
2017-05-24 10:27:38 +00:00
Elias Naur
c243211167 bind,internal/importers: add Unwrap methods to unwrap Java wrappers
For Java classes implemented in Go, it is useful to take a Java instance
and extract its wrapped Go instance. For example, consider the
java.lang.Runnable implementation wrapping a Go function:

package somepkg

type GoRunnable struct {
    lang.Runnable
    f func()
}

Java methods that take a java.lang.Runnable cannot directly take a
*GoRunnable, so this CL adds a Unwrap method:

import gorun "Java/somepkg/GoRunnable"

...

r := gorun.New()
r.Unwrap().(*GoRunnable).f = func() { ... }
javapkg.Run(r)

The extra interface conversion is unfortunately needed to avoid
import cycles.

Change-Id: Ib775a5712cd25aa75a19d364a55d76b1e11dce77
Reviewed-on: https://go-review.googlesource.com/35295
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-18 20:31:42 +00:00
Elias Naur
91f2c8983e bind/java: fix tests on Windows
Make the golang.org/x/mobile/bind/java buildable without CGO, and
replace the which tool with exec.LookPath. Both helps on Windows that
often don't have a C compiler available and no which command.

Found while testing external NDK use on Windows.

Change-Id: I6d3311aae3fa97acb61b5ab9bed334e4a608c386
Reviewed-on: https://go-review.googlesource.com/35174
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2017-01-13 01:37:47 +00:00
Elias Naur
9ec2d17db2 cmd: fix handling of (multiple) tags
The gomobile tool mishandled build tags in two ways, first by
ignoring tags for iOS, second by passing multiple tags along to
the go tool incorrectly. This CL fixes both.

Fixes golang/go#18523
Fixes golang/go#18515

Change-Id: I28a49c1e23670adb085617d9f5fb5cd5e22a4b65
Reviewed-on: https://go-review.googlesource.com/34955
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-12 22:00:16 +00:00
Elias Naur
c90c4f7c8a bind,internal: change the default Java package to the empty string
The Objective-C bindings was recently changed to support the empty
name prefix and to use that as the default. This CLs changed the Java
generators in the same way, supporting the empty Java package and using
it as the default.

Change-Id: I857affce686c67638a2b6c4e1da5d6a88d7ba560
Reviewed-on: https://go-review.googlesource.com/34778
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-11 19:15:09 +00:00
Elias Naur
fe0977739a bind: generate reverse bindings for implicit Java types
Before this CL, Java types that were only implicitly referenced
were represented as interface{}. However, if a value of such an
implicit type were passed to Java, a runtime crash would occur
because there would be no wrapper class to unwrap.

Fix this by generating implicit types, fixing the crashes,
gaining type safety, and removing the interface{} special case in
the generator.

While we're here, remove a redundant insert to the clsMap map in
java.go.

Change-Id: Ic50125da3d7cd6075899bf628d419b084c630490
Reviewed-on: https://go-review.googlesource.com/34777
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-11 19:09:27 +00:00
Elias Naur
72eef9d093 bind: avoid crashes from SIGPIPE
The Go runtime doesn't handle SIGIPE signals from writing to closed
sockets or pipes in c-archive and c-shared mode (issue 17393).
Work around it in gomobile by simply ignoring all SIGPIPE signals;
they're not useful in mobile apps anyway.

Change-Id: Ibd7ee41058856c5eddb4a519345a3851a29e9b44
Reviewed-on: https://go-review.googlesource.com/33771
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-12-01 16:06:28 +00:00
Elias Naur
27a7c7e186 bind: add -Werror to gomobile bind CFLAGS
C compiler warnings are hidden in the gomobile build process. Expose
them through -Werror.

Change-Id: I2d87e5985f9ff874dc6feb836ba35c2bd848bfa6
Reviewed-on: https://go-review.googlesource.com/31517
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-10-21 19:49:24 +00:00
Elias Naur
23d9c7b685 bind: skip benchmarks in short mode
The bind benchmarks are implemented as Go tests, but take quite a
while to run. Skip them in short mode.

Change-Id: I49ede84863b047aa8307bc0ef752fc7aa21e2d65
Reviewed-on: https://go-review.googlesource.com/31637
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-10-21 19:42:06 +00:00
Péter Szilágyi
2ea7e2cc92 bind, bind/java: support generating exceptional constructors
In Go type constructors can often return an error type too
beside an instance of the type being created. This is useful
in cases where the parameters might be wrong and instantiaion
cannot succeed. This CL extends the Java generator so that
these methods are also converted into class constructors that
also can throw.

Change-Id: I5e531eec126904a026767a8503968255b9fd833b
Reviewed-on: https://go-review.googlesource.com/31184
Reviewed-by: Elias Naur <elias.naur@gmail.com>
2016-10-17 14:21:52 +00:00
Elias Naur
63993740dd bind: fix field accessors for Go implemented Java classes
The go_seq_to_refnum function handles its argument object as a Java
object if it doesn't implement Seq.Proxy. Java classes implemented in
Go does not implement Seq.Proxy, but when passing "this" references to
Go, instances must be treated as a Go object.

Use go_seq_to_refnum_go everywhere "this" is passed to Go, which fixes
field accessors and simplifies the method call case.

Change-Id: I3d01c63d7b2081e6344ece431f5e5021a9dd7662
Reviewed-on: https://go-review.googlesource.com/31171
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-10-17 10:26:22 +00:00
Elias Naur
e76ec53021 bind: support casting of Java objects
Generate Cast functions that take a proxy for a Java class or interface,
and return a new proxy with the same reference. The Cast functions
panic if the underlying Java object is not an instance of the expected
type.

Change-Id: I08a5bf9a79139f0fac5dd102c7b028c8c989fc6d
Reviewed-on: https://go-review.googlesource.com/30095
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-10-05 08:40:06 +00:00
Elias Naur
3f7b83ffd4 bind: generate Java constructors for every exported struct
A recent CL added Java constructors to generated classes that extends
or implements other Java classes and interfaces. Constructors for a
struct S are Go functions on the form

func NewS...(...) *S

If no such constructors exists, a default empty constructor is
generated.

Expand that to cover every exported Go struct.

Fixes golang/go#17086

Change-Id: I910aba13d5884c3f67c946c62a8ac4a3db8e2ea7
Reviewed-on: https://go-review.googlesource.com/29710
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-10-05 07:47:24 +00:00
Elias Naur
89b8360218 bind: remove error wrappers to preserve error instance identity
CL 24800 changed the error representation from strings to objects.
However, since native errors types are not immediately compatible
across languages, wrapper types were introduced to bridge the gap.

This CL remove those wrappers and instead special case the error
proxy types to conform to their language error protocol.

Specifically:

 - The ObjC proxy for Go errors now extends NSError and calls
   initWithDomain to store the error message.
 - The Go proxy for ObjC NSError return the localizedDescription
    property for calls to Error.
 - The Java proxy for Go errors ow extends Exception and
   overrides getMessage() to return the error message.
 - The Go proxy for Java Exceptions returns getMessage whenever
   Error is called.

The end result is that error values behave more like normal objects
across the language boundary. In particular, instance identity is
now preserved: an error passed across the boundary and back will
result in the same instance.

There are two semantic changes that followed this change:

 - The domain for wrapped Go errors is now always "go".
   The domain wasn't useful before this CL: the domains were set to
   the package name of function or method where the error happened
   to cross the language boundary.
 - If a Go method that returns an error is implemented in ObjC, the
   implementation must now both return NO _and_ set the error result
   for the calling Go code to receive a non-nil error.
   Before this CL, because errors were always wrapped, a nil ObjC
   could be represented with a non-nil wrapper.

Change-Id: Idb415b6b13ecf79ccceb60f675059942bfc48fec
Reviewed-on: https://go-review.googlesource.com/29298
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-10-04 09:11:42 +00:00
Elias Naur
84d710de20 bind: don't throw away result values with unknown type
The Java API wrapper generator use interface{} for Java classes
that no Go code references. Return values of unknown types are thrown
away, since they're effectively useless. Since the return values can
be used for nil checks and since casting of Java instances are
supported in CL 30095, this CL returns the naked *seq.Ref results
values instead.

Change-Id: I821b1c344a4c68c57fd34e2b655404e449de4c03
Reviewed-on: https://go-review.googlesource.com/30097
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-10-04 08:12:22 +00:00
Elias Naur
16fd47fa04 bind: don't inherit java.lang.Object methods to Java interfaces
Before, the Java generator let Java interfaces inherit java.lang.Object
methods. However, interfaces strictly doesn't inherit Object and since
the JNI GetMethodID returns NULL for Object methods on interface classes,
stop making Object a super class to interfaces.

Change-Id: I3757c1ed02c07ccffab74a30132d5197742c6513
Reviewed-on: https://go-review.googlesource.com/30096
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-10-01 18:55:04 +00:00
Elias Naur
bdf873ed8f bind,cmd: accept Java API in bound packages
Accept Java API interface types as arguments and return values from
bound Go package functions and methods. Also, allow Go structs
to extend Java classes and implement Java interfaces as well as override
and implement methods.

This is the third and final part of the implementation of the golang/go#16876
proposal.

Fixes golang/go#16876

Change-Id: I6951dd87235553ce09abe5117a39a503466163c0
Reviewed-on: https://go-review.googlesource.com/28597
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-09-22 10:16:33 +00:00
Elias Naur
bf31dd1a5e bind,cmd/gomobile: add a new generator for Java API wrappers
Using the new Java class analyzer API, scan the bound packages
for references to Java classes and interfaces and generate Go
wrappers for them.

This is the second part of the implementation of proposal golang/go#16876.

For golang/go#16876

Change-Id: I59ec0ebdae0081a615dc34d450f344c20c03f871
Reviewed-on: https://go-review.googlesource.com/28596
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-09-16 17:25:25 +00:00
Elias Naur
61011ba584 bind: fix error type
Errors was recently converted to use objects as representation instead
of strings. Issue golang/go#17073 exposed a few places that wasn't properly
updated. Fix them and add the test case from the the issue.

Fixes golang/go#17073

Change-Id: I0191993a8427d930540716407fc09032f282fc66
Reviewed-on: https://go-review.googlesource.com/29176
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-09-16 13:52:19 +00:00
Péter Szilágyi
2f75be449f bind: use lowercase method names for Java binds
There was a discussion a year ago about making methods and types
lowercase in ObjC (https://github.com/golang/go/issues/12889),
which was done (https://go-review.googlesource.com/#/c/15780/),
alas the suggested Java lower casing was never addressed.

This CL converts all generated Java methods to lower case.

Change-Id: Ia2f28519bc59362877881636109ddfc651b24960
Reviewed-on: https://go-review.googlesource.com/28494
Reviewed-by: Elias Naur <elias.naur@gmail.com>
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-09-07 12:52:06 +00:00
Elias Naur
80e11ad074 mobile/bind: move generated Java classes to package level
Before this CL, generated Java classes or interfaces were inner
classes to the top package class. That is both unnecessary and creates
ugly class names. Instead, move every generated class and interface to its
own package level class.

NOTE: This is a backwards incompatible change and requires every client
of gomobile APIs to be updated to leave out the package class in the
type names. For example, the Go type

package pkg

type S struct {
}

now generates (with the default java package name go) a Java class named
go.pkg.S. The name before this CL was go.pkg.Pkg.S.

Also, change the custom java package to specify the package prefix and
not the full package as before. This is an unfortunate change needed
to avoid name clashes between two bound packages. On the plus side,
the change brings the custom package case closer to the default behaviour,
which is a commen prefix, "go.", and a distinct java package for every
Go package bound.

Change-Id: Iadfaad56e101d1caf7e2a05006f4d384859a20fe
Reviewed-on: https://go-review.googlesource.com/27436
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-08-22 07:41:35 +00:00
Elias Naur
44ce26ee94 mobile/bind: fix gomobile bind with custom Java package
The change from using strings to objects for passing errors across
the language barrier broke the custom java package mode of gombile
bind. Fix it and add a runtime test to make sure it won't happen
again.

Fixes golang/go#16262

Change-Id: Ia7f8afb79556798056f0755758052190081a2dbb
Reviewed-on: https://go-review.googlesource.com/24800
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-07-07 16:08:17 +00:00
Elias Naur
d5b8fb1623 mobile/bind/java: fix compiler warnings
Add a missing #include to declare the exported Go function
setContext, and replace old GNU-style struct initializers.

Change-Id: Id1660559236c39505a47368a700c8e0ad834cf6c
Reviewed-on: https://go-review.googlesource.com/24491
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-06-28 17:54:14 +00:00
Elias Naur
a3e0621280 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-23 18:55:48 +00:00
Elias Naur
5e11c20fc0 mobile/bind: don't force Java classes to extend stub classes
Requiring user code to extend Go interface Stubs to be able to pass
Java objects to Go is clumsy and use up the single extend slot.
Instead, support (and enforce) java classes to implement translated
Go interface directly. This is similar to how ObjC works.

The stub classes are now gone, and users of gobind Java APIs need
to update their code to implement interfaces directly.

Change-Id: I880bb7c8e89d3c21210b2ab2c85ced8d7859ff48
Reviewed-on: https://go-review.googlesource.com/21313
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-03-31 07:59:45 +00:00
Elias Naur
07a529f836 mobile/bind: fix a reference count race with the garbage collectors
Each side of the language barrier maintains a map of reference numbers
to objects. Each entry has a reference count that exactly matches
the number of active proxy objects on the other side. When a reference
crosses the barrier, the count is incremented and when a proxy finalizer
is run, the count is decremented. If the count reaches 0, the reference
number and its object are removed from the map.

There is a possibility that a reference number is passed to the other
side, and the last proxy is then immediately garbage collected and
finalized. The reference counter then reaches 0 before the other side has
converted the reference number to its object, crashing the program.

This is possible in both Go/Java/ObjC but is most likely to happen in
ObjC because its own automatic reference count runtime frees objects
as soon as they are statically never referenced again.

Fix the race by always incrementing the reference count before sending
a reference across the barrier. When converting the reference back into
an object on the other side, decrement the counter again.

Only the new ObjC test fails without this fix, but I left the Java
counterpart in for good measure.

Change-Id: I92743aabec275b4a5b82b952052e7e284872ce02
Reviewed-on: https://go-review.googlesource.com/21311
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-03-30 18:24:55 +00:00
Elias Naur
5b96314b59 mobile/bind: implement interfaces from imported bound packages
Java classes must explicitly declare implemented interfaces. Bind
already declares all such interfaces within each package. Expand
the set of interfaces to include all bound packages.

In addition, let Java interfaces extend all possible interfaces in
the same way as Java classes. To avoid circular references, only
let interfaces extend compatible interfaces with fewer methods.

Before, each package was imported in its own importer, breaking the
assumption of types.AssignableTo that identical packages have
identical *types.Package. Fix that by using one importer for all
bound packages, replacing package path equality checks with direct
equality checks.

While we're here, add missing arguments to a few error messages.

Change-Id: I5eb58972a3abe918862ca99d5a203809699a3433
Reviewed-on: https://go-review.googlesource.com/20987
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-03-29 08:36:18 +00:00
Elias Naur
3e830506b0 mobile/bind: stop tracking foreign objects in the Go reference tracker
ToRefNum only handles Go objects, but it can be passed foreign object
proxies as well. Add a check whether the object is a proxy, and if so,
simply return its refnum and don't track it.

Change-Id: Ib17bd11b48e472c3bec0e5fb06661b201c3dfa97
Reviewed-on: https://go-review.googlesource.com/20681
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-03-14 17:50:23 +00:00
Elias Naur
d841a034b5 mobile/bind: initialize imported bound packages
Make sure that a bound package's imported and also bound packages
are initialized before referencing methods and constructors in the
imported packages.

Change-Id: If158aac83c245a33695d3b1648d0dfc37a7313ac
Reviewed-on: https://go-review.googlesource.com/20652
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-03-13 18:10:58 +00:00
Elias Naur
7df33f4a5c mobile/bind: allow bound packages to refer to imported bound packages
Multiple packages are already supported, but only as if each packages
were bound in isolation. This CL lets a bound package refer to other
bound packages in its exported functions, types and fields.

In Java, the JNI class jclass and constructor jmethodID are exported
so other packages can construct proxies of other packages' interfaces.

In ObjC, the class @interface declarations are moved from the package
.m file to its .h file to allow other packages to constructs its
interface proxies.

Add a supporting test package, secondpkg, and add Java and ObjC tests
for the new cross package functionality. Also add simplepkg for
testing corner cases where the generated Go file must not include its
bound package.

While we're here, stop generating Go proxy types for struct types;
only Go interfaces can be implemented in the foreign language.

Change-Id: Icbfa739c893703867d38a9100ed0928fbd7a660d
Reviewed-on: https://go-review.googlesource.com/20575
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-03-12 06:23:01 +00:00
Hyang-Ah Hana Kim
5b73745053 bind/java,objc: run gomobile init after installing gomobile
Also, upgrade the android plugin version used for java/seq_test
from 1.2.3 to 1.5.0

For golang/go#9603

Change-Id: I7b465ff0e607319a08150c4405675832d91edc1e
Reviewed-on: https://go-review.googlesource.com/20411
Reviewed-by: Elias Naur <elias.naur@gmail.com>
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-03-10 21:34:06 +00:00
Elias Naur
976d0710d0 mobile/bind: merge iOS and Android Go test packages
Currently there is a Go test package for each platform, iOS and
Android. This CL merges them into a single, shared  package. Apart
from the reduced code duplication, the merger stops the tests
diverging further. Most importantly, one shared package clarifies
that the intent of gobind is that the same Go package can be
reused across platforms.

This CL only merges the obvious test duplicates. The rest have been
copied from the ObjC package into the Android test under different
names.

While we're here, demote the long string test to the basictypes
bind test; the test never had a runtime part.

Change-Id: I7838b16999968fae7b012016a5b5f6bb80f94023
Reviewed-on: https://go-review.googlesource.com/20300
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-03-07 16:10:51 +00:00
Elias Naur
5604bcf91f mobile/bind: implement iOS benchmarks
Add a XCTestCase based ObjC driver, SeqTest.m, to run the benchmarks
package on iOS.

While we're here, replace "Java" with "Foreign" in test names to
reflect that benchmarks run on both platforms now.

Change-Id: I38a38f3093b4b97961107b5ea66f03cff8e395c3
Reviewed-on: https://go-review.googlesource.com/20259
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-03-07 16:08:55 +00:00
Elias Naur
75a1c3da13 mobile/bind: make LOG_FATAL abort() on Android
LOG_FATAL already throws an exception on iOS. Make it abort() on
Android, so that any fatal error will hopefully end up with a useful
log instead of an easily missed message in logcat.

Also, remove return statements after LOG_FATAL on both platforms.
They're unnecessary and confusing and they weren't used consistently
anyway.

Change-Id: I2a8e2e0ac064e95f52ca130de17265c9741cefe4
Reviewed-on: https://go-review.googlesource.com/20257
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-03-06 14:57:14 +00:00
Elias Naur
ba0a725146 mobile/bind: avoid intermediate []rune copy converting Java string to Go
Converting a Go string to a string suitable use a specialized function,
UTF16Encode, that can encode the string directly to a malloc'ed buffer. That
way, only two copies are made when strings are passed from Go to Java; once
for UTF-8 to UTF-16 encoding and once for the creation of the Java String.

This CL implements the same optimization in the other direction, with a
UTF-16 to UTF-8 decoder implemented in C. Unfortunately, while calling into a
Go decoder also saves the extra copy, the Cgo overhead makes the calls much
slower for short strings.

To alleviate the risk of introducing decoding bugs, I've added the tests from
the encoding/utf16 package to SeqTest.

As a sideeffect, both Java and ObjC now always copy strings, regardless of
the argument mode. The cpy argument can therefore be removed from the string
conversion functions. Furthermore, the modeRetained and modeReturned modes
can be collapsed into just one.

While we're here, delete a leftover function from seq/strings.go that
wasn't removed when the old seq buffers went away.

Benchmarks, as compared with benchstat over 5 runs:

name                          old time/op  new time/op  delta
JavaStringShort               11.4µs ±13%  11.6µs ± 4%     ~     (p=0.859 n=10+5)
JavaStringShortDirect         19.5µs ± 9%  20.3µs ± 2%   +3.68%   (p=0.019 n=9+5)
JavaStringLong                 103µs ± 8%    24µs ± 4%  -77.13%   (p=0.001 n=9+5)
JavaStringLongDirect           113µs ± 9%    32µs ± 7%  -71.63%   (p=0.001 n=9+5)
JavaStringShortUnicode        11.1µs ±16%  10.7µs ± 5%     ~      (p=0.190 n=9+5)
JavaStringShortUnicodeDirect  19.6µs ± 7%  20.2µs ± 1%   +2.78%   (p=0.029 n=9+5)
JavaStringLongUnicode         97.1µs ± 9%  28.0µs ± 5%  -71.17%   (p=0.001 n=9+5)
JavaStringLongUnicodeDirect    105µs ±10%    34µs ± 5%  -67.23%   (p=0.002 n=8+5)
JavaStringRetShort            14.2µs ± 2%  13.9µs ± 1%   -2.15%   (p=0.006 n=8+5)
JavaStringRetShortDirect      20.8µs ± 2%  20.4µs ± 2%     ~      (p=0.065 n=8+5)
JavaStringRetLong             42.2µs ± 9%  42.4µs ± 3%     ~      (p=0.190 n=9+5)
JavaStringRetLongDirect       51.2µs ±21%  50.8µs ± 8%     ~      (p=0.518 n=9+5)
GoStringShort                 23.4µs ± 7%  22.5µs ± 3%   -3.55%   (p=0.019 n=9+5)
GoStringLong                  51.9µs ± 9%  53.1µs ± 3%     ~      (p=0.240 n=9+5)
GoStringShortUnicode          24.2µs ± 6%  22.8µs ± 1%   -5.54%   (p=0.002 n=9+5)
GoStringLongUnicode           58.6µs ± 8%  57.6µs ± 3%     ~      (p=0.518 n=9+5)
GoStringRetShort              27.6µs ± 1%  23.2µs ± 2%  -15.87%   (p=0.003 n=7+5)
GoStringRetLong                129µs ±12%    33µs ± 2%  -74.03%  (p=0.001 n=10+5)

Change-Id: Icb9481981493ffca8defed9fb80a9433d6048937
Reviewed-on: https://go-review.googlesource.com/20250
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-03-05 10:02:05 +00:00
Elias Naur
6fca37c69e 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-03-03 15:03:45 +00:00
Elias Naur
988d17d203 mobile/bind: update benchmarks to address post-submit review comments
Address comment from CL/20095.

Change-Id: I2b3b3230106ad27128440609472003c69bd97825
Reviewed-on: https://go-review.googlesource.com/20173
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-03-03 15:02:39 +00:00
Elias Naur
45143d8b25 mobile/bind: add Android benchmarks
Since the normal Go benchmark machinery cannot readily be used for
Android apps, a new test, TestJavaSeqBench, is added that builds and
runs the new benchmarkpkg package along with its support Java class
SeqBench. Benchmarkpkg mimics Go benchmarking, in particular it
produces benchcmp compatible output.

Excerpts of the output from a Nexus 5:

BenchmarkJavaEmpty	65536000	26 ns/op
BenchmarkJavaNoargs	256000	7685 ns/op
BenchmarkJavaNoargsDirect	64000	17405 ns/op
BenchmarkJavaOnearg	64000	26887 ns/op
BenchmarkJavaOneret	32000	38325 ns/op
BenchmarkJavaManyargs	32000	41720 ns/op
BenchmarkJavaRefjava	32000	38139 ns/op
BenchmarkJavaRefgo	32000	34403 ns/op
BenchmarkJavaStringShort	32000	32366 ns/op
BenchmarkJavaStringLong	8000	127879 ns/op
BenchmarkJavaSliceShort	32000	42462 ns/op
BenchmarkJavaSliceLong	8000	138391 ns/op
BenchmarkGoEmpty	524288000	3 ns/op
BenchmarkGoNoarg	32000	40342 ns/op
BenchmarkGoOnearg	32000	43529 ns/op
BenchmarkGoOneret	32000	45456 ns/op
BenchmarkGoRefjava	32000	55111 ns/op
BenchmarkGoRefgo	32000	57038 ns/op
BenchmarkGoManyargs	16000	67967 ns/op
BenchmarkGoStringShort	32000	57538 ns/op
BenchmarkGoStringLong	8000	128485 ns/op
BenchmarkGoSliceShort	32000	59279 ns/op
BenchmarkGoSliceLong	4000	411225 ns/op

Benchmarks prefixed with "BenchmarkJava" are for calls from Java into
Go. Benchmarks prefixed with "BenchmarksGo" are the other way around.
Note that all Go benchmarks run against a Java interface implementation
while the Java benchmarks calls Go functions directly. In other words,
every Go call serializes an implicit Java reference, explaining the
higher call times. The JavaRefgo and JavaRefjava tests attempt to
quantify the overhead equivalent for Java.

The "Direct" suffix are for variants that runs the benchmarks from a
new thread or goroutine. For Go it makes little difference, but there
is a noticable speedup when calling Go from Java when there is already
a JNI call context earlier in the stack.

The benchmarks are for Android only for now, but the benchmarkpkg
has been added to the common golang.org/x/mobile/bind package in
anticipation of future iOS support.

Change-Id: I3c948dc710b65bc348e7635416324095060a5beb
Reviewed-on: https://go-review.googlesource.com/20095
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-03-02 13:27:40 +00:00
Elias Naur
c432672682 mobile/bind: fix long strings
Change-Id: Ia7c4523804b2588efb3ea6cc14b34a951faa298c
Reviewed-on: https://go-review.googlesource.com/20092
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-03-01 16:03:40 +00:00
Elias Naur
320ec40f63 mobile/bind: fix RefMap invariant
RefMap tracks its number of live Ref instances in the 'live' member.
However, when a reference was removed and later added, 'live' wasn't
updated accordingly. Fix and add a test.

Change-Id: I806e17ea0319d76db4d07b5f8d9107b146ee80db
Reviewed-on: https://go-review.googlesource.com/19975
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-02-29 09:47:09 +00:00
Elias Naur
9ea846d4a9 mobile/bind: make sure the Java SeqTest has a valid context
On some Android devices (my HTC One S running Android 4.1.1),
SeqTest failed the testAssets test because the LoadJNI hack to
locate a valid context fails.

Instead, make testAssets set up a valid context acquired from
InstrumentTestCase.

Change-Id: If6e11173dbacff45eb6cb0f409f56cbd88186e30
Reviewed-on: https://go-review.googlesource.com/19896
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-02-26 14:44:39 +00:00
Hyang-Ah (Hana) Kim
876458415e bind: rename seq package with name _seq in generated Go code
Avoid taking a good name (seq) away from users.

Fixes golang/go#14168

Change-Id: I88e90cb74b479e348c642a1caa27096ed4a6d68e
Reviewed-on: https://go-review.googlesource.com/19601
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-02-21 08:48:00 +00:00
ttyh061
5a8964bd48 mobile/bind: fix JNI local reference table overflow
The existing implementation has  memory leaks on two local variables
that are not deleted after use. Android app will crash after this issue.

add UnitTest for this issue.

Fixes golang/go#14346

Change-Id: Ic233d15556ac97b35e00e13279a572c48a03049f
Reviewed-on: https://go-review.googlesource.com/19532
Reviewed-by: Elias Naur <elias.naur@gmail.com>
2016-02-17 14:34:31 +00:00
Elias Naur
060f2570e9 mobile/bind: add integration test for CL 19417
Change-Id: I6a2cab4ca9b5bd61db51d08966da19fdfb07c344
Reviewed-on: https://go-review.googlesource.com/19463
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-02-12 15:13:48 +00:00
Elias Naur
d1c2f486a3 mobile/bind: handle null references from Java in Go
The Seq Java class has a special case for null references. Expand
the special case to Go so that null references from Java are properly
translated to nil.

Fixes golang/go#14228

Change-Id: I915d1f843c9db299d6910480f6d10dae0121a3b4
Reviewed-on: https://go-review.googlesource.com/19460
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-02-12 14:54:21 +00:00
Elias Naur
46f9e01d1e mobile/bind: ensure that Java->Go->Java calls stay on same thread
Java methods from Go are run on a thread pool managed on the Java side,
to avoid the complexities of getting Go threads to play along with the
Android JVM. However, for call stacks that contain a Java->Go->Java
chain, this behaviour confuses Java code sensitive to specific threads
if the Go->Java call is executed on an arbitrary thread from the pool.
For example, most Android UI changes must happen on the single UI
thread.

Replace the thread pool with direct calls to mimic ObjC<->Go and
Java->Go calls. Threads not already attached to the JVM are attached.
Introduce a thread local variable to detach such threads at thread exit.

Change-Id: I8cb65803c9278666ae77a0c7a65dc2d9c7e739e1
Reviewed-on: https://go-review.googlesource.com/19334
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-02-11 21:58:20 +00:00