Commit Graph

274 Commits

Author SHA1 Message Date
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
James Treanor 72d8d1135a bind: fix CLANG_WARN_STRICT_PROTOTYPES errors for generated ObjC code
Xcode 9 now enables CLANG_WARN_STRICT_PROTOTYPES by default.

This update ObjC function prototypes with no params from

FOUNDATION_EXPORT Something* DoSomething();

to

FOUNDATION_EXPORT Something* DoSomething(void);

Change-Id: I23b1d3e70a2ede2d2d77951ffe8a1a9598a1b7a0
Reviewed-on: https://go-review.googlesource.com/68970
Reviewed-by: Elias Naur <elias.naur@gmail.com>
2017-10-09 16:20:38 +00:00
Elias Naur 9bd992d065 bind,cmd/gomobile: fix follow ups to doc generator CL (52330)
Also, add tests for blank, anonymous and multiple struct fields.

Change-Id: I15e6fff8d1684f2a31e99e1adf023b92f1f3cb48
Reviewed-on: https://go-review.googlesource.com/59550
Reviewed-by: Alan Donovan <adonovan@google.com>
2017-09-05 18:43:28 +00:00
Elias Naur 05f1abc543 bind,cmd: generate ObjC and Java documentation
Convert Go documentation to JavaDoc tags (/** ... */).

Since the .aar file format doesn't support source files, gomobile
will create a package-sources.jar along with the main package.aar.

For Objective-C, JavaDoc-style comments seems to work as well,
judging by manual inspection of Xcode quick help.

Change-Id: I47fe5b6804681d459a873be37a44610d392166ef
Reviewed-on: https://go-review.googlesource.com/52330
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2017-08-25 16:30:26 +00:00
Elias Naur b2ece8c621 bind: declare implemented ObjC protocols
Declare all implemented interfaces in the protocol list of a bound
struct. This is the ObjC equivalent of similar functionality for
Java.

Change-Id: Ibaea881f57fecf8a0716d25ec925de43974fe0fe
Reviewed-on: https://go-review.googlesource.com/52010
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2017-08-22 15:31:44 +00:00
Elias Naur e66540375e bind: generate ObjC initializers
For functions on the form

New<T>... (...) *T

or

New<T>... (...) (*T, error)

generate corresponding initializers. The name of an initializer is
the function name where "New<T>" is replaced by "init".

If no functions match for a type *T, generate a default (empty)
initializer that returns new(T). The default initializer mirrors
the default constructor in Java.

Fixes golang/go#20254.

Change-Id: I3c317418fa517d3f2de3f67f400867285b11ea4f
Reviewed-on: https://go-review.googlesource.com/52012
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2017-08-11 20:43:43 +00:00
Elias Naur 202a52f8ee bind: remove erroneous class to name replacer
The name replacement is done in asMethod.

Change-Id: I9aedf1231c0563f53e3a34f29197e106c090ee6e
Reviewed-on: https://go-review.googlesource.com/51871
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2017-08-03 22:24:39 +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 0a555e528a bind/objc: fix benchmark
The generator recently changed the default ObjC prefix to the empty
string but the benchmark wasn't changed accordingly (the benchmark
doesn't run in -short mode).

Fixes golang/go#19048

Change-Id: Id8f63da787f399410ea86c13ee77eb3005d31f38
Reviewed-on: https://go-review.googlesource.com/37314
Reviewed-by: Andris Valums <eighttt@fastmail.com>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2017-02-21 16:13:43 +00:00
Elias Naur ab9391cb88 bind: handle ClassNotFoundExceptions on older Androids
If a wrapped Java class is missing at runtime, for example because
the app is running on an older Android version, the resulting class
not found exception would cause the app to crash. Fix it by ignoring
missing classes, allowing the app to avoid using them according to a
runtime version check.

Change-Id: I9138c4e2a905b180959306ecbb997695236ab273
Reviewed-on: https://go-review.googlesource.com/35853
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-31 20:25:05 +00:00
Elias Naur 959f80f9a9 bind: use runtime.KeepAlive to keep foreign references alive
With Go 1.8 to be released, runtime.KeepAlive is now necessary to
make sure a reference to a foreign object is not garbage collected
and finalized before we get a chance to increment its reference
count.

runtime.KeepAlive was introduced in Go 1.7 which is the minimum
version required by gomobile.

Change-Id: I32215f96e4f415ff9be7b979dd3677e067b8d201
Reviewed-on: https://go-review.googlesource.com/35954
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-30 19:33:00 +00:00
Elias Naur 44ced21510 bind,cmd/gomobile: require Go 1.7
Bump the minimum required version of Go to 1.7.

This removes version specific code and makes sure users have the
latest mobile related fixes to Go applied. Also, this change is
necessary when runtime.KeepAlive is introduced in a later CL.

Change-Id: I8441a28aef7f645379fbd8f00edabe3c3fb219de
Reviewed-on: https://go-review.googlesource.com/35953
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-30 19:32:33 +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 a0f998b2d8 bind: skip unsupported functions in function sets
Change-Id: Ibac8f11503ff088600e75c16edab7d20d8128157
Reviewed-on: https://go-review.googlesource.com/35332
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-18 02:44:14 +00:00
Elias Naur ab8d69d0ab internal,importers: find deeply nested prefixed class references
Before this CL, with code such as

import "Java/some/pkg/Class"

...

Class.StaticFunc().DoSomething()

the reverse generator wouldn't find the the Java/some/pkg.Class
reference.

Change-Id: I1def4b54589fd1c123767ff59438c647cbced0cd
Reviewed-on: https://go-review.googlesource.com/35331
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-18 02:43:28 +00:00
Elias Naur c4d780faeb bind,internal/importers: handle Java long constants correctly
Change-Id: Idff789038e7470ef5b12798273fc67165d57f4dc
Reviewed-on: https://go-review.googlesource.com/35293
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-17 15:58:41 +00:00
Elias Naur d8b1e1aab8 bind,misc: guard reverse generated import with mobile os tags
Running go get golang.org/x/mobile/... results in errors because the
go tool fails to find the reverse generated Java ("Java/...") and
Objective-C ("ObjC/...") packages. Work around the errors by adding
the android and ios tags, respectively, to files importing those
packages.

The gobind gradle plugin is updated to pass along GOOS=android to
ensure the gobind tool continues to build Android reverse packages.

Fixes golang/go#17750

Change-Id: Id66a3c6cdfe249c6ed494192eb12195d6509332f
Reviewed-on: https://go-review.googlesource.com/34956
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-17 14:22:42 +00:00
Elias Naur 2f5693b8d8 bind,internal/importers: always generate toString methods
When wrapping Java exceptions, their toString() methods are called from
the wrapper's Error() method to satisfy the Go error interface. Make
sure toString() is always included, even if it never directly referenced
from bound packages.

Change-Id: I5653f6ad82afbe4b061e02a69d60453000288a83
Reviewed-on: https://go-review.googlesource.com/35189
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-17 14:05:11 +00:00
Elias Naur 3884e8cb98 internal,bind: resolve overloaded methods at runtime
Before this CL, calling overloaded methods on reverse bound Java
classes and interfaces involved confusing and ugly name mangling.
If a set of methods with the same name differed only in argument count,
the mangling was simply adding the argument count to the name:

func F()
func F1(int32)

But if two or more methods had the same number of arguments, the type
had to be appended:

func (...) F() int32
func (...) F1(int32) (int32, error)
func (...) F__I(int32, int32)
func (...) F__JLjava_util_concurrent_TimeUnit_2(int64, concurrent.TimeUnit)

This CL sacrifices a bit of type safety and performance to regain the
convenience and simplicity of Go by resolving overloaded method dispatch
at runtime.

Overloaded Java methods are combined to one Go method that, when invoked,
determines the correct Java method variant at runtime.

The signature of the Go method  is compatible with every Java method with
that name. For the example above, the single Go method becomes the most
general

func (...) F(...interface{}) (interface{}, error)

The method is variadic to cover function with a varying number of
arguments, and it returns interface{} to cover int32, int64 and no
argument. Finally, it returns an error to cover the variant that returns
an error. The generator tries to be specific; for example

func G1(int32) int32
func G2(int32, int32) int32

becomes

func G(int32, ...int32) int32

Overriding Java methods in Go is changed to use the Go parameter types to
determine to correct Java method. To avoid name clashes when overriding
multiple overloaded methods, trailing underscores in the method name are
ignored when matching Java methods.  See the Get methods of GoFuture in
bind/testpkg/javapkg for an example.

Change-Id: I6ac3e024141daa8fc2c35187865c5d7a63368094
Reviewed-on: https://go-review.googlesource.com/35186
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-17 14:03:33 +00:00
Elias Naur 1aa9ad5c48 bind: generate reverse functions the same way as methods
This CL restructures function generation to match the way methods
are generated, to avoid two different code paths for a coming CL.

Change-Id: I5a4f15e51ea5df101f9aa419ed4170ab36506418
Reviewed-on: https://go-review.googlesource.com/35185
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-17 14:01:43 +00:00
Elias Naur 100a59ffc1 bind: fix reverse binding generation on Windows
The filepath package works on native paths, not package paths. Replace
it with the path package. Do it for Objective-C as well, for correctness.

No new tests; the existing reverse tests fails on Windows without
this CL.

Change-Id: I8963db992d4bed30e8828579fb83ecaf8c9d9ade
Reviewed-on: https://go-review.googlesource.com/35176
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2017-01-13 10:02:40 +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 2802a0168e bind: include all generated code in tests
The tests was missing the generated code for the universe package
and some parts of the reverse generated code. Include it.

Change-Id: Id5e2f215c8f6f717c30377965255c4b64f31e923
Reviewed-on: https://go-review.googlesource.com/34992
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-12 22:17:26 +00:00
Elias Naur 103834091a bind,internal: generate reverse bindings for implicit ObjC types
This is the Objective-C version of a similar change to the Java
generaters, CL 34777.

This CL makes sure the types only implicitly referenced through method
parameters or return values are also reverse generated. In doing so,
the anonymous interface{} is never used by the reverse generator,
gaining type safety, removing a special case and making sure passing
values of implicit types can be passed across the language barrier.

To support implicit types, the Objective-C importer is changed to
extract the module of a type from the clang AST dump instead of
relying on the module implied by the Go reference (e.g.
ObjC/Foundation.NSString).

Change-Id: Ie9305f4cd9a9802decbd93f81cec84dd05af11ab
Reviewed-on: https://go-review.googlesource.com/34991
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-12 22:11:22 +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 6f0c9f6df9 bind: generate wrappers for generated ObjC types
This is the Objective-C equivalent of CL 34776, generating reverse
wrappers for generated ObjC types. The implementation follows the same
strategy as the Java implementation: use the Go ast package to find
exported structs with embedded Objective-C types and synthesize their
types as if they were imported through clang.

In turn, the handling of the implicit "self" parameter changes in the
same way as well: the type of self parameters must be the wrapped type
for the generated type. For example:

func (d *GoNSDate) Description(self Foundation.NSDate) string

becomes

import gopkg "ObjC/Objcpkg"

func (d *GoNSDate) Description(self gopkg.GoNSDate) string

Change-Id: I26f838b06a622864be463f81dbb4dcae76f70f20
Reviewed-on: https://go-review.googlesource.com/34780
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-11 20:07:46 +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 ff6f6e8d8e bind,cmd,internal: generate reverse bindings for exported Go structs
Before this CL, the type of the implicit "this" parameter to Java methods
implemented in Go could only be a super class of the generated Java
class. For example, the following GoRunnable type is an implementation of
the Java interface java.lang.Runnable with a toString method:

package somepkg

import "Java/java/lang"

type GoRunnable struct {
    lang.Runnable
}

func (r *GoRunnable) ToString(this lang.Runnable) string {
    ...
}

The "this" parameter is implicit in the sense that the reverse generator
automatically fills it with a reference to the Java instance of
GoRunnable.

Note that "this" has the type Java/java/lang.Runnable, not
Java/go/somepkg.GoRunnable, which renders it impossible to call Java
methods and functions that expect GoRunnable. The most practical example
of this is the Android databinding libraries.

This CL changes the implicit this parameter to always match the exact
type. In the example, the toString implementation becomes:

import gopkg "Java/go/somepkg"

func (r *GoRunnable) ToString(this gopkg.GoRunnable) string {
    ...
}

One strategy would be to simply treat the generated Java classes
(GoRunnable in our example) as any other Java class and import it
through javap. However, since the Java classes are generated after
importing, this present a chicken-and-egg problem.

Instead, use the newly added support for structs with embedded prefixed types
and synthesize class descriptors for every exported Go struct type.

Change-Id: Ic5ce4a151312bd89f91798ed4088c9959225b448
Reviewed-on: https://go-review.googlesource.com/34776
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-11 19:06:53 +00:00
Elias Naur f5d8ba6777 bind: support Java packages with the same last component
Before this CL, using the reverse bindings to access two Java packages
with the same last component would fail with a duplicate package import
error. This CL renames generated import statements to use unique
aliases.

Change-Id: I94696281e58f011f45811445cf81aea02af69c4f
Reviewed-on: https://go-review.googlesource.com/34774
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-05 11:50:37 +00:00
Elias Naur fa10d8888b bind: fix inner class argument and return types
Change-Id: I1e74a7475ee3af491605e289685cda001ec80bb6
Reviewed-on: https://go-review.googlesource.com/34647
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-04 17:11:03 +00:00
Elias Naur e0bb90a948 bind: clear JNI return values when an exception was raised
The return value of a JNI call is undefined when an exception was
raised during the call. To make sure comparisons with NULL works,
clear the value when an exception is raised.

No new tests; some devices, like the Samsung S2, crashes with the
existing tests without this CL.

Change-Id: I85eb983e9444fff1f05e0f83a0640d106280e54d
Reviewed-on: https://go-review.googlesource.com/34631
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-12-27 15:54:01 +00:00
Elias Naur 6a96d4c7cf bind: make the default name prefix empty on ObjC
Since generated names now have their package names prefixed, the
extra prefix, "Go", is both confusing and counter-productive to
making the generated ObjC code look like any other native code.

Change the default to the empty prefix, while preserving support
for an explicit prefix if needed.

This is a backwards incompatible change; to keep the old behaviour,
specify "-prefix Go" to the gobind or gomobile command.

While we're here, fix the Ivy example for the recent change in
error returns.

Change-Id: I7fef4a92a18ddadee972ccf359652e3b31624f33
Reviewed-on: https://go-review.googlesource.com/34643
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-12-24 13:37:27 +00:00
Elias Naur 60feeb77f4 bind,cmd: support the empty ObjC prefix
Since the Go package name is already prefixed to generated ObjC
names, the empty extra prefix is useful. Support that by not reverting
to the default extra prefix, "Go", if -prefix "" is specified.

To avoid file name clashes with the Go header files, add ".objc" to
the ObjC-facing header names.

Change-Id: I559fe60d7474521617f23894af247c6019ff2a21
Reviewed-on: https://go-review.googlesource.com/33954
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-12-23 09:44:24 +00:00
Péter Szilágyi e1ac2f46b8 bind: don't use output arg for T in (T, error) returns if T is nullable
The current iOS binding generator only generates returns if the
function being bound does not return an error. If a second error
return type is also present, the binder always generates both the
primary as well as the error as an output parameter.

This is undersirable because most decent functions in Go will
also return errors, so all of those get converted to plain methods
iOS side, each of them requiring allocating the return variable
first and only then execute the call. This gets even more annoying
with the Swift error wrapping protocol which converts errors to
throw statements automatically, but which still needs the ugly pre-
allocs caused by the genrated bindings not returning the result,
just placing it in an output argument.

This CL changes that so that if a nullable result is being returned
by a bound method from Go, then it is generated as a proper return
and not an output argument. This allows erroring functions to still
be called as a function in ObjC, and even more elegantly drop even
the error part in Swift.

Change-Id: I35152d7d2fd2a132eba836fa23be8fd4f317f097
Reviewed-on: https://go-review.googlesource.com/34072
Reviewed-by: Elias Naur <elias.naur@gmail.com>
2016-12-08 14:50:39 +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
Elias Naur e107ad8ed2 bind: sanitize parameter names
Package names, type names and method names are already sanitized.
Extend that to parameter names as well.

Change-Id: I408024c5e2f9561c37e8059a2b53199ee1afaef6
Reviewed-on: https://go-review.googlesource.com/31519
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-10-21 09:25:03 +00:00
Elias Naur 7f64fd241f bind: fix compiler warnings
CL 29298 got rid of error type wrappers, but failed to update a
variable type accordingly.

Exposed by CL 31517 that enables -Werror.

Change-Id: I2c2b75dcd43b89ffa7fb008150b1aee09ec25229
Reviewed-on: https://go-review.googlesource.com/31518
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-10-21 09:24:50 +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 4cb3e7f634 bind: accept implicit `self` parameters in all methods
This is the ObjC equiivalent to CL 30276. It expands support for
implicit `self` parameters to every exported method.

Change-Id: Iff8a956b38448213866a93dc02ca59cac592feef
Reviewed-on: https://go-review.googlesource.com/30277
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-10-17 10:37:36 +00:00
Elias Naur 1c49d29d1c bind,cmd: accept ObjC wrapper types in bound packages
Accept ObjC API wrapper types as arguments and return values from
bound Go package functions and methods. Also, allow Go structs
to extend ObjC classes and implement ObjC protocols as well as override
and implement methods.

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

Fixes golang/go#17102

Change-Id: I601d90fb6d22b8d6f8b7d5fe0130daa1a4dd4734
Reviewed-on: https://go-review.googlesource.com/29175
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-10-17 10:37:16 +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 6ecf8eedb8 bind,cmd: add generator for ObjC API wrappers
Using the new ObjC type analyzer API, scan the bound packages for
references to ObjC classes and protocols and generate Go wrappers for them.

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

For golang/go#17102

Change-Id: I773db7b0362a7ff526d0a0fd6da5b2fa33301144
Reviewed-on: https://go-review.googlesource.com/29174
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-10-16 10:25:40 +00:00
Elias Naur a82bb3c8ba cmd/gobind,bind: generate complete Java interface with gobind
Output every Java class, including the support classes, from gobind
-lang=java. In addition, replace Go package export data parsing with
converting from go/ast to go/types. That way, gobind can tolerate
unknown imports as long as the exported Go API doesn't use them.

In a follow-up CL, the gobind gradle plugin will use gobind for a first
pass to expose the generated Java classes to the android plugin.

Change-Id: I8134899ec818c7fee79e4d9df8afcae9dd679add
Reviewed-on: https://go-review.googlesource.com/30093
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-10-14 13:14:45 +00:00
Russ Cox a4f0a77f2f bind: param name replacement for invalid unicode names
The are three generators that currently call this method:
    A  -> B
(1) go -> java
(2) go -> objective-c
(3) go -> go

As discussed below, we only substitute for invalid unicode characters
in case (1).

**Case 1**
Go:
From golang.org/ref/spec:
Identifiers name program entities such as variables and types.
An identifier is a sequence of one or more letters and digits(unicode_digit).
The first character in an identifier must be a letter(unicode_letter | "_" ).

Java:
From https://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.8
`The "Java letters" include uppercase and lowercase ASCII Latin letters
A-Z (\u0041-\u005a), and a-z (\u0061-\u007a), and, for historical reasons,
the ASCII underscore (_, or \u005f) and dollar sign ($, or \u0024). The $
character should be used only in mechanically generated source code or,
rarely, to access pre-existing names on legacy systems.`

Therefore, Go's identifiers are checked in case they break these Java rules.

**Case 2**
There is no objective-c standard specification for valid identifiers.
From some testing it seems that Go and objective-c have identical
valid identifier rules.

**Case 3**
Requires no checking.

Change-Id: I881810eb9355af6a418727ace32cb6ce4266b2a0
Reviewed-on: https://go-review.googlesource.com/14044
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-10-12 18:37:41 +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 69dc8e1c38 bind: allow implicit `this` argument on every exported method
Before this CL, the implicit `this` arguments to methods on Java classes
implemented in Go was only supported on overriding methods, because
their parameter count are known. This CL expands support for the `this`
parameter to every exported method. It only recognizes parameters named
`this` declared with a Java wrapper type.

Change-Id: I8a9d3417d259bdfcc28512a72f07d6a05f483adc
Reviewed-on: https://go-review.googlesource.com/30276
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-10-05 07:49:55 +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 e04a76eb64 cmd/gomobile,internal: add bootclasspath and classpath flags to gomobile
Add -bootclasspath and -classpath flags to the gomobile tool. In a
follow-up CL, the gobind gradle plugin will use them to support R and
databinding classes from Go.

Change-Id: Id33acf0c3fe1ec3908740b2a736ed241fa6391c2
Reviewed-on: https://go-review.googlesource.com/30092
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-09-30 20:31:20 +00:00
Elias Naur 9d5f7955ff bind: correctly generate methods with implicit this and parameters
Change-Id: I885a21876d9f639bc0996c9279fd0afefa93cef6
Reviewed-on: https://go-review.googlesource.com/29877
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-09-30 14:05:34 +00:00
Elias Naur 2dcaa053a0 bind: preserve no-arg Java constructors
When the Java class parser began culling unused constructors, the
logic for determining whether a given Java class has a no-arg
constructor broke when the no-arg constructor is culled. Add
an explicit field for tracking the no-arg constructor property.

Change-Id: Ib68929ae1108bd6fa1fd23de1d134332eb0d97a2
Reviewed-on: https://go-review.googlesource.com/29875
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-09-30 14:00:14 +00:00
Péter Szilágyi 1663ffa95c bind: initialize JNI library on any class load
With the introduction of constructors Java side, all types
become entry points into the library. However the library
was only initialized by the main class until now, resulting
in all other constructors hitting linker errors until an
interaction with the main library class.

This CL fixes that by changing each generated type to touch
the main library class, ensuring that the underlying native
library is loaded.

Change-Id: I640d1dc329e072f8d0753f74ccce87cd9e5aaea8
Reviewed-on: https://go-review.googlesource.com/29994
Reviewed-by: Elias Naur <elias.naur@gmail.com>
2016-09-29 11:31:45 +00:00
Elias Naur 9640137a86 bind: add missing unsafe import in generated code
The ClassGen.genGo function always uses unsafe, contrary to what
the check in GenGo says. With this CL, generated code always
imports unsafe if there are any classes to be generated.

Change-Id: Ic807111a26e494b4941790830b1950bb8b1f73d5
Reviewed-on: https://go-review.googlesource.com/29873
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-09-27 16:37:51 +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 08c3b2f4a5 bind: fix generated declaration of GoUniverseerror
GoUniverseerror is a (generated) protocol type, and variables of
protocol types use id<> notation.

Change-Id: I3d36b3ba634c10f0e59424faf71809c94df52cc6
Reviewed-on: https://go-review.googlesource.com/29052
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-09-21 06:52:53 +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
Elias Naur e99a906c3a bind: avoid ObjC reserved names
The new tests in CL 28494 exposed a bug: the ObjC generator does
not avoid reserved names and names with special meaning ("init").
Generalize the name sanitizer from the Java generator and use that.

Also, move the lowerFirst function to gen.go since it is now used
by both generators.

Change-Id: I25b7af2594b2ea136f05d2bab1cfdc66ba169859
Reviewed-on: https://go-review.googlesource.com/28592
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-09-07 14:13:50 +00:00
Elias Naur 7f59993615 bind/objc: fix tests
CL 24792 changed Go's int type to be represented in ObjC as long.
Change SeqTest.m accordingly.

Change-Id: Ifd34787db713444fc729b497ed72b62688384bc8
Reviewed-on: https://go-review.googlesource.com/28591
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-09-07 12:53:58 +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 ed036a869f mobile/bind: fix comment
Change-Id: I100d1a32da7ec4f6b29f3590f23ca0a9ddbf230a
Reviewed-on: https://go-review.googlesource.com/27442
Reviewed-by: David Crawshaw <crawshaw@golang.org>
Reviewed-by: Sebastien Binet <seb.binet@gmail.com>
2016-08-22 14:31:51 +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
Hana Kim d38f5ba53d bind: map Go's int type to Objective-C's long type
https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaTouch64BitGuide/Major64-BitChanges/Major64-BitChanges.html

Fixes golang/go#16182

Change-Id: I312749e8a7113bf9231eb3896f801e450f46a410
Reviewed-on: https://go-review.googlesource.com/24792
Reviewed-by: Elias Naur <elias.naur@gmail.com>
2016-07-07 16:20:08 +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 d53576ecbc bind: replace an incorrect return with continue
Change-Id: Ie929e60614b7a42ab7f65b500725168015946f87
Reviewed-on: https://go-review.googlesource.com/24080
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-06-14 16:25:23 +00:00
Elias Naur e1840f9c11 mobile/bind: make Java proxy classes private
Proxies are an implementation detail, so make them private to hide
them from the public API.

Also, move the proxy classes out of its interface scope; classes
declared inside Java interfaces are always public and cannot be
declared otherwise. Use the lowercase "proxy" class prefix to
avoid name clashes with exported Go interfaces and structs.

Change-Id: Iae6a53ed4885b7899f2fa770b73c135f54ffb263
Reviewed-on: https://go-review.googlesource.com/21370
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-04-02 13:41:50 +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 e8a257577d mobile/bind/objc: treat warnings as errors in SeqTest.m
Warnings used to be invisible when running SeqTest.m through
go test. Treat warnings as errors and fix a bug that surfaced.

Change-Id: I81e7291635824cdb4a898c91db740f7aa10f3611
Reviewed-on: https://go-review.googlesource.com/21133
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-03-25 14:34:30 +00:00
Ernest Chiang d181647601 bind/objc: fix incorrect objc reference handling in RefTracker
The objc reference handling has bug on assigning different refnum
to the same ios object. The reason for this is that the
RefTracker _refs is not properly initialized thus incorrect
reference check in assignRefnumAndIncRefcount.

Change-Id: Id86423dcf378d11e9056bf7c7ecb646333a94a04
Reviewed-on: https://go-review.googlesource.com/21120
Reviewed-by: Elias Naur <elias.naur@gmail.com>
2016-03-25 10:50:18 +00:00
Elias Naur e2bc210c9a bind/objc: fix compile warning
An extra struct initializer was left over from an earlier version
of the direct call conversion. Remove it.

Change-Id: I19c3eb3be7bf78378af47ea182931b0c24cdd34d
Reviewed-on: https://go-review.googlesource.com/21104
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-03-25 00:13:16 +00:00
Elias Naur eb6cb84428 mobile/bind: ignore unbound types
If a method or function refers to a type from an unbound package,
ignore it instead of reporting an error.

Change-Id: I689da63c1a0d1a3aa09220311d871c1f6f66208f
Reviewed-on: https://go-review.googlesource.com/20985
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-03-21 23:28:22 +00:00
Elias Naur 7cb2b86de7 mobile/bind: allow Num function in bound interfaces
Rename the refnum field, Num,  to something much less likely to clash
with an interface method set.

Change-Id: If334966b2430f38118baded44461bd39298bafb0
Reviewed-on: https://go-review.googlesource.com/20983
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-03-21 23:26:23 +00:00
Elias Naur 390f7b3813 mobile/bind: skip unsupported functions, vars, fields and methods
Bind attempts to generate bindings for everything a package exports,
generating an error for what it cannot handle.
For multiple bound packages, unexporting what should not be bound
is sometimes awkward or outright impossible.

Lacking the equivalent of Cgo's //export directory, this CL change
the behaviour of bind to simply ignore everything it can't generate
bindings for, even if otherwise exported. For every declaration it
ignores, a comment is generated instead, to help any confusion as
to why a particular export was not included.

Change-Id: I2c7a5bee0f19a58009293b4e5ac2c95687e62e80
Reviewed-on: https://go-review.googlesource.com/20651
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-03-15 16:00:42 +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 51fe33c7bf mobile/bind: don't generate goSeqRefInterface
It was generated once per bound package before, but since it is
constant it belongs in seq.h.

Change-Id: I7d920e8e87ce11cc9ae5e5e410dd935bc6e53480
Reviewed-on: https://go-review.googlesource.com/20657
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-03-14 13:45:40 +00:00
Elias Naur 7d65c03f0a mobile/bind: only generate callable jmethodID declarations
Change-Id: I11b901be718d730f50fdc3877cbc2cf173aa6c66
Reviewed-on: https://go-review.googlesource.com/20658
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-03-14 13:44:20 +00:00
Elias Naur 1a0b242e59 mobile/bind: let Java toString call String method if available
Bind generates a default toString for Go structs. If the struct has
a String() string method, use that instead.

Change-Id: If83a6f5c9ad03abbd0b939b9120ff8dd2135f713
Reviewed-on: https://go-review.googlesource.com/20656
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-03-14 13:30:01 +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 462ddb163e mobile/bind: use the correct package for JNI type descriptors
Before, bind always used the currently bound package for all JNI type
descriptors. Now, the type's package is used, which is important
when referencing other bound packages.

Change-Id: If36a45785f7333072803f1e7a8ff28b39fa57d0f
Reviewed-on: https://go-review.googlesource.com/20650
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-03-13 18:10:01 +00:00
Elias Naur 7e17db74dc mobile/bind: fix tests
Fix the tests that CL 20575 broke.

Change-Id: Id4059547c289c693ed4cfda6f748209d3e7f8658
Reviewed-on: https://go-review.googlesource.com/20620
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2016-03-12 10:35:00 +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 0d3fdd1e30 mobile/bind: replace panics with errors
cgoType panics on types not yet supported by bind. Replace the panics
with more appropriate error messages.

Change-Id: I0b8609b50de07ca93db13c50654f62ffbd9f25c2
Reviewed-on: https://go-review.googlesource.com/20472
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-03-09 14:40:50 +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 4da9347475 mobile/bind: convert iOS tests to XCTestCase
Replace test.bash with go test that builds and run the bind tests
through the XCode testing framework.

Running on the iOS emulator unmasked a bug where autorelease pools
were not in place for Go calls into ObjC, leaking autoreleased
objects. Fix that by adding autoreleasepool blocks to the tracker
finalizer callback and to every generated ObjC proxy.

Will not run on the emulator without CL 19206.

Change-Id: I6a775f9995f3b8ea50272982069d033e41ddcb7b
Reviewed-on: https://go-review.googlesource.com/20255
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2016-03-07 14:35:49 +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
Hyang-Ah Hana Kim c2b8429cd1 bind: fallback to const.Value.String for pre go1.6
Fixes golang/go#14615

Change-Id: I75e130e5b7b2534660098907fa1f044390c01d01
Reviewed-on: https://go-review.googlesource.com/20164
Reviewed-by: Elias Naur <elias.naur@gmail.com>
2016-03-04 12:00: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