Commit Graph

23 Commits

Author SHA1 Message Date
cuishuang 55113b94f0 mobile: fix some typos
Change-Id: Iaffde6f90b92b1f8b96c76d2bc3d8c312ebb764f
GitHub-Last-Rev: 7413e9537adc1626c76386cf2d47188eeed8342e
GitHub-Pull-Request: golang/mobile#76
Reviewed-on: https://go-review.googlesource.com/c/mobile/+/390014
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Trust: Hyang-Ah Hana Kim <hyangah@gmail.com>
2022-03-07 22:04:22 +00:00
Federico Bond feefccb6c1 bind: ignore type aliases to basic types
Fixes golang/go#29559

Change-Id: Iffaac239e5c9a9e53f4e292b4d9bf669d5084e1f
GitHub-Last-Rev: c5d4a4823d2bf60fa7bff09ab7bbdc9235c49d67
GitHub-Pull-Request: golang/mobile#25
Reviewed-on: https://go-review.googlesource.com/c/159417
Reviewed-by: Elias Naur <mail@eliasnaur.com>
2019-01-25 20:10:28 +00:00
Tobias Klauser ca3c58166e all: use "reports whether" in godoc comments
Go documentation style for boolean funcs is to say:

    // Foo reports whether ...
    func Foo() bool

rather than "returns true if" or "returns whether".

Change-Id: I5d01c45a8867b47462ed142a49ad51bf0a319ce1
Reviewed-on: https://go-review.googlesource.com/c/151957
Reviewed-by: Elias Naur <elias.naur@gmail.com>
2018-11-30 13:31:20 +00:00
Elias Naur 1f177cbe4d bind: ignore unsupported basic types
Before this change, binding unsupported basic types such as
uint failed with an error. Instead, add them to the list of
ignored types so that no error is generated and a comment is
generated explaining why the offending function, constant or
variable was skipped.

Unsigned integers are probably easy to support in ObjC, but
leave them unsupported for now.

While here, improve the printing of the ignored types in the
explaining comments.

Fixes golang/go#24762

Change-Id: I0d9ab471b2245728270f6ee588f554d4a105d500
Reviewed-on: https://go-review.googlesource.com/105377
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
2018-04-10 16:30:43 +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 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
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 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
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 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 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 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 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 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 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 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 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
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 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