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

250 Commits

Author SHA1 Message Date
Hyang-Ah Hana Kim
a54776c08a bind, internal/loader: fix errors reported by vet
Change-Id: Ia1c2e13cd093fec1c4d6c38828b057d1a38274e4
Reviewed-on: https://go-review.googlesource.com/16841
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2015-11-11 22:33:35 +00:00
Hyang-Ah (Hana) Kim
4eceb7296e bind: error on named basic types
error message example:

 gomobile: unsupported named type github.com/sridharv/bugreports/typealiasmissing.Alias

Objective-C and Java generation code currently panics, which need to be
fixed separately. (See TODO in bind/seq.go)

Fixes golang/go#13190

Change-Id: Ie46dc58ea800522b8ab7cb8ac662ad561e0ca82e
Reviewed-on: https://go-review.googlesource.com/16780
Reviewed-by: Burcu Dogan <jbd@google.com>
2015-11-11 22:33:09 +00:00
Hyang-Ah Hana Kim
9c6db08f1d bind/objc/testpkg: delete accidentally committed files.
Change-Id: I9a2a83ef64cb9ee877db952ff6f8e050cd52873d
Reviewed-on: https://go-review.googlesource.com/16840
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2015-11-11 22:32:48 +00:00
Hyang-Ah (Hana) Kim
9868458844 bind/objc: correct handling of empty string return value.
Fixes golang/go#13087

Change-Id: I9d7319d9ed3eee73e6479510911f796ab0607bd2
Reviewed-on: https://go-review.googlesource.com/16452
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-10-28 19:30:01 +00:00
Hyang-Ah (Hana) Kim
8a59fd3f45 bind: correctly lowercasing all caps names
Fixes golang/go#13031

Change-Id: I6cae8e303d0d929e26c44cbdf1c243e98dabb766
Reviewed-on: https://go-review.googlesource.com/16270
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-10-23 16:10:33 +00:00
Hyang-Ah (Hana) Kim
bf2ca7a93b x/mobile/bind: forward declare protocol/class for interface types
Fixes golang/go#13004

Change-Id: I114cf3d7d970485d0609b1d7b497e232010e64df
Reviewed-on: https://go-review.googlesource.com/16155
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-10-22 17:03:04 +00:00
Burcu Dogan
319b6ff544 bind: use the prefix in the include guards
Fixes golang/go#12984.

Change-Id: I485e6071ff136eb3f475d03ebebf8d306f24c931
Reviewed-on: https://go-review.googlesource.com/16027
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2015-10-19 16:13:27 +00:00
Hyang-Ah (Hana) Kim
51389df416 bind/objc: use lowercase for method names.
Fixes golang/go#12889.

Change-Id: I4b8f5e4b2c4fe53146fc351889664cbeb5a1860b
Reviewed-on: https://go-review.googlesource.com/15780
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-10-13 18:34:52 +00:00
Hyang-Ah (Hana) Kim
090457ac45 bind/objc: change exported var accessors to class methods.
for example,

package testpkg
var AnInt int64

will be mapped to

@interface GoTestpkg: NSObject
+ (int64_t) AnInt;
+ (void) setAnInt:(int64_t)v;
@end

Followup of cl/15340

Update golang/go#12475

Change-Id: Ie26c92af977fc3dd62dcad2b10c6a5c1c1b8941b
Reviewed-on: https://go-review.googlesource.com/15770
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-10-13 18:34:32 +00:00
Hyang-Ah (Hana) Kim
bb506cefe4 bind: error for packages with no exported names.
For error message improvement mentioned in golang/go#12842

Change-Id: I066ae9dc9415b34bf93128098199bbb935b7dedc
Reviewed-on: https://go-review.googlesource.com/15410
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-10-07 20:50:02 +00:00
Hyang-Ah (Hana) Kim
45ed283c80 bind: support exported variables
basic types, struct*, interface.

TODO: error, non-pointer struct, pointer basic types, slice of bytes.

Update golang/go#12475.

Change-Id: I5ff91059b1f963b0cadb6f76cb0e12f7b6b98718
Reviewed-on: https://go-review.googlesource.com/15340
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-10-07 20:48:45 +00:00
Hyang-Ah (Hana) Kim
60728759f9 bind: add const type support
Update golang/go#12475

Change-Id: I7fdc22462b5925c84ebbeb54517032c2fbd0545b
Reviewed-on: https://go-review.googlesource.com/15120
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-09-30 20:56:48 +00:00
David Crawshaw
7a70f37cf6 bind/objc: remove a constructor and retain blocks
A framework generated with gomobile bind -target=ios has two global
constructors: one initializing a data structure and another using it.
These constructors are defined in different translation units, which
(I believe, reasoning from C++ global constructors) means their order
of initialization is undefined.

A capturing block is stack allocated. Its memory is invalid after the
function returns. Make a copy of the interface initializer blocks so
they can be saved to the heap.

Block implementation background:
http://www.cocoawithlove.com/2009/10/how-blocks-are-implemented-and.html

Updates golang/go#12590

Change-Id: Ia7ae9f4bbd8df6e6e79949de54b3e6c48148c700
Reviewed-on: https://go-review.googlesource.com/14549
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2015-09-15 17:38:26 +00:00
Meir Fischer
00865a642f bind: improve readability for float default values
To improve readability for default numeric types
represented as strings, use 0.0 for floating point types.
(No tests make use of this change so golden update
not necessary; not significant enough for new test/test mod.)
Fix spelling on error statement; this was the only
instance of that spelling error in the repository.

Change-Id: I373890725b33da11c6780ba93674d89541bf758c
Reviewed-on: https://go-review.googlesource.com/13645
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2015-09-01 18:16:37 +00:00
Hyang-Ah (Hana) Kim
671f57b233 bind: fix interface methods' multiple return values handling.
Fixes golang/go#12403

Change-Id: I9b8e6d69beb1ceb27e991348212acb5054497e47
Reviewed-on: https://go-review.googlesource.com/14077
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-08-31 16:32:47 +00:00
Hyang-Ah (Hana) Kim
c384607ef3 bind: support for custom java package name and objective-c prefix.
Introduce options -javapkg and -prefix for gobind command.

The following generates java class Testpkg with package name com.example.

gobind -lang=java -javapkg=com.example testpkg

The following generates objective-c files where function and type names
are prefixed with ExampleTestpkg.

gobind -lang=objc -prefix=Example testpkg

As discussed in golang/go#9660 and golang/go#12245.

Gomobile support is not yet implemented.

Change-Id: Ib9e39997ce915580a5a2e25643c0c28373f27ee1
Reviewed-on: https://go-review.googlesource.com/13969
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-08-28 13:57:06 +00:00
David Crawshaw
4628c38e0f bind: handle unimplementable interfaces
Fixes golang/go#12330

Change-Id: I1568d04b7e48242105a7028ca471e2138f684eeb
Reviewed-on: https://go-review.googlesource.com/13946
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2015-08-27 21:36:14 +00:00
David Crawshaw
11fe695b54 bind: support fields with type error
Fixes golang/go#12328

Change-Id: I42872d26acb1c44522a64405cfa2d0f10fb24485
Reviewed-on: https://go-review.googlesource.com/13919
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2015-08-26 20:25:55 +00:00
Hyang-Ah (Hana) Kim
da28dd9045 bind/objc: fix error return type handling
Fixes golang/go#12307.

Change-Id: I5560b616a3cd51c536d9e5529745681effe26e9a
Reviewed-on: https://go-review.googlesource.com/13913
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-08-25 15:22:55 +00:00
Hana Kim
49a82fefe3 all: support go get golang.org/x/mobile/... on Windows
Fixes golang/go#12212
Workaround for golang/go#12261 until golang/go#9306 is fixed.

Change-Id: I51c1bcfc92c1553fe2132586a0234b1c1af6aeb1
Reviewed-on: https://go-review.googlesource.com/13745
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-08-24 21:09:04 +00:00
David Crawshaw
48bd7cbbc7 bind: adjust invalid java package names
Updates golang/go#12273

Change-Id: I8eac3e84d5a473e9ffe45705cea88537573aef61
Reviewed-on: https://go-review.googlesource.com/13873
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2015-08-24 21:08:09 +00:00
David Crawshaw
da78c710f3 bind/testdata: update golden files
Tests now pass on OS X.

Change-Id: Id2e6c5bd9ddd4070962ff9bc121d7902e8c9a647
Reviewed-on: https://go-review.googlesource.com/13694
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2015-08-18 17:57:47 +00:00
Hyang-Ah (Hana) Kim
5c55c26446 bind/objc: don't expose internal class types mapped to Go interface.
For interface binding, we need only protocols.

Change-Id: I377ec9c8de0a3414d48272892b169fb94823e8ff
Reviewed-on: https://go-review.googlesource.com/13541
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-08-11 19:16:10 +00:00
Hyang-Ah (Hana) Kim
50f2c0f60e bind/objc: update test.bash to run gobind.
Change-Id: I38379d6f1a6bbaa13234060b52edc8206b750644
Reviewed-on: https://go-review.googlesource.com/13028
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-08-05 20:02:56 +00:00
Hyang-Ah (Hana) Kim
9865dd0755 bind: implement objective-C interface binding code generation.
Depends on cl/12389 progress.

Change-Id: I044b0cf7c4df1d84b9ee158a4ceb3fdb13bf7645
Reviewed-on: https://go-review.googlesource.com/12410
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-07-31 20:12:37 +00:00
Hyang-Ah (Hana) Kim
926122382b bind/objc: Interface binding support
Given a Go package defining an interface,

  package testpkg
  type I interface {
      Fn()
  }

I is mapped to an objective-c protocol and a proxy interface
that implements the routing of method calls from Go.

  @protocol GoTestpkgI
  -(void)Fn;
  @end

Users implement a class conforming the generated protocol. For example,

  @interface MyI <GoTestpkgI> {
  }
  @end

  @implementation MyI {
  }
  - (void)Fn { .... }
  @end

Gobind will also create a proxy interface to handle Go objects
implementing the interface and passed to Objective-C, but that will
be hidden from users.

  @interface GoTestpkgI : NSObject <GoTestpkgI> {
  ...
  -(void)Fn;
  @end

The gobind code to generate the objective-c binding is in a separate CL.

Change-Id: I6a72d34fe3a5b8d2774d2d53913229c1e71f2d60
Reviewed-on: https://go-review.googlesource.com/12389
Reviewed-by: David Crawshaw <crawshaw@golang.org>
Reviewed-by: Damien Neil <dneil@google.com>
2015-07-31 20:12:03 +00:00
David Crawshaw
b780379135 bind: vendor x/tools/go/loader
This breaks our dependency on the x/tools repository, which has a
tendency to change in unexpected ways. It also means we can use the
version of go/types that ships with Go 1.5.

Along the way, it appears that cgo processing has changed slightly.
The old check for cgo files apparently wasn't working, so I removed
it.

Change-Id: I14378e9df9cd65c5ab61b47728ba0d56f31cdf76
Reviewed-on: https://go-review.googlesource.com/12680
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2015-07-28 18:02:47 +00:00
Hyang-Ah (Hana) Kim
c261c465a9 bind: seq.Transact requires interface descriptor.
seq.Transact is called when Go calls a method of a foreign object
that implements a Go interface. Currently, we assume that the foreign
object has an instance method that can conduct the message routing,
so the object id and the method code is sufficient for transact.

Passing the interface descriptor (e.g.  go.testpkg.I) however allows
the bind internal to use non-instance methods to implement the routing.

Change-Id: I1f61a04f919fbd09117ea332d678cd50e4861e46
Reviewed-on: https://go-review.googlesource.com/12685
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-07-27 18:08:41 +00:00
Hyang-Ah (Hana) Kim
4755f4ad8f bind/objc: fixes miscalculation of string length.
Also, fixes the memory allocation bug - misuse of mem_ensure that caused
to allocate 72 bytes of memory to carry 16 bytes of data for instance.

Fixes golang/go#11842.

Change-Id: I21798be2ec7adfb68cc2897bb46a924f05f3478c
Reviewed-on: https://go-review.googlesource.com/12577
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-07-23 22:35:14 +00:00
Hyang-Ah (Hana) Kim
c039090cf1 bind/java: fix misuse of mem_ensure.
mem_ensure is to ensure the remaining space in buffer
is large enough to hold extra size bytes. We've been passing
the target capacity instead of the number of extra bytes we
need.

Change-Id: Ic6f6ddb4ad22cbcdbc44eb4a58e6a415ae771fb2
Reviewed-on: https://go-review.googlesource.com/12578
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-07-23 22:34:55 +00:00
Hyang-Ah (Hana) Kim
103c0611a8 bind/java: suppress compiler warning
non-varargs call of varargs method with inexact argument type for last
parameter

Change-Id: I7623951c365b8cf899a17ee784c8d4f3b4bdb198
Reviewed-on: https://go-review.googlesource.com/12528
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-07-22 21:01:20 +00:00
Hyang-Ah (Hana) Kim
136fa9bbbb bind/java: reenable asset access.
This is done by moving app.Context to internal/mobileinit,
introducing mobileinit.SetCurrentContext and,
making bind/java depend on it.

TODO: check gomobile bind's proguard rule - context lookup
was implemented through reflection on android.app.AppGlobals class.

Change-Id: Ieb6ad503eeef8c2c1c5836a21c667938c5a701a2
Reviewed-on: https://go-review.googlesource.com/12279
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-07-16 21:15:37 +00:00
Hyang-Ah (Hana) Kim
6fb2670129 bind/objc: Remove reference of seq.h from generated header file.
This allows gomobile bind to stop copying seq.h to the destination.

Change-Id: I4a23613fe0407500ad483ae9d8f6bb823d82f082
Reviewed-on: https://go-review.googlesource.com/12300
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-07-16 14:02:16 +00:00
David Crawshaw
e5193c59b3 cmd/gomobile: reorganize build logic
The goal here is to remove several inconsistencies between
-target=android and -target=ios support, along with making the flow
of the command follow the path you might expect given a certain set
of flags, and preparing for `gomobile bind` support of ios. In
particular, building non-main packages now works with both targets
and the initialization of global build state is clearer.

The reorg also is designed around an nm trick I thought of
yesterday to do better package import scanning without a slow
all-file scan. This will give better detection of x/mobile/app and
x/mobile/exp/audio/al packages. There's a TODO about it, and I'll do
it in a future CL.

Tested with:

	go test golang.org/x/mobile/cmd/gomobile
	gomobile init
	gomobile bind golang.org/x/mobile/asset
	go test golang.org/x/mobile/bind/java
	gomobile build -target=ios golang.org/x/mobile/example/basic
	gomobile build -target=ios golang.org/x/mobile/gl
	gomobile build -target=android golang.org/x/mobile/gl
	gomobile build -target=android golang.org/x/mobile/example/basic
	(Along with manual testing of basic on an android device.)

That might make a pretty good _test.go.

Change-Id: I41230008c3c15db25a11c33b9eaca4abada9f411
Reviewed-on: https://go-review.googlesource.com/12051
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2015-07-13 18:54:16 +00:00
Elias Naur
5aee943213 x/mobile/bind: remove debug print
Android support has advanced to the point where this logging statement
adds very little information.

Change-Id: I3c8c9d60be8c0b52a519f9e44711bd211b5bfe67
Reviewed-on: https://go-review.googlesource.com/11990
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2015-07-10 17:08:45 +00:00
David Crawshaw
960fedd178 internal/mobileinit: new package for shared logic
Package app contains some logic that we want to share with
gobind-based libraries. So move it to a new internal package
that both can import.

Long term the log changes should be in the standard library,
but the Go tree is currently frozen.

Fixes golang/go#11630.

Change-Id: I9ff622fc499bf255bce18df23cb68b03f667947f
Reviewed-on: https://go-review.googlesource.com/11981
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2015-07-08 21:29:11 +00:00
Hyang-Ah (Hana) Kim
a922dd97b1 bind/objc: remove '_' after Go<pkgname> prefix from names.
Before this change, func Hello() of mypkg package generated a function
GoMypkg_Hello to distinguish the prefix from the function name.
It seems the use of '_' is very rare in Objective-C though.
After this change, it generates GoMypkgHello.

Change-Id: Ic7aa2b667363a447a5ff3500262502d3137d6853
Reviewed-on: https://go-review.googlesource.com/11893
Reviewed-by: Damien Neil <dneil@google.com>
2015-07-07 03:29:17 +00:00
David Crawshaw
5ced61ace7 cmd/gomobile: call System.loadLibrary for .aar
Change-Id: I3fd099910cfb1fe112ded4386905d370acfea002
Reviewed-on: https://go-review.googlesource.com/11816
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2015-07-06 22:21:32 +00:00
Alan Donovan
616f82b347 bind: fix build broken by API change to golang.org/x/tools/go/types
Fixes golang/go#11509.

Change-Id: Ia54febfa3ef9b589befb09b8f5d79cb6d7f6c4da
Reviewed-on: https://go-review.googlesource.com/11841
Reviewed-by: Robert Griesemer <gri@golang.org>
2015-07-02 12:24:20 +00:00
David Crawshaw
8144405762 app, bind: separate the app package from gobind
Historically, the app package implemented Go runtime initialization.
This was convoluted, so the package was used both by all-Go apps
(currently based on Android's NativeActivity) and bind-based apps.

With Go 1.5 we have -buildmode=c-shared, which does a lot of the work
of the old app package. That code was removed a while back, but both
all-Go and gobind-based apps still used package app. The intermingled
initialization processes led to some strange states.

This CL separates gobind-based apps completely from the app package.
As part of that users are now expected to use System.loadLibrary
themselves. (A future CL may want to make the loadLibrary call part
of the .aar generated by gomobile bind.)

Delete the libhello example, which has been replaced by gomobile bind,
which could do with its own example at some point. Also delete the
libhellojni example, which now has nothing to do with the x/mobile
repository.

Change-Id: I444397f246dbafe81e5c53532eb482c197d26f70
Reviewed-on: https://go-review.googlesource.com/11654
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2015-06-29 11:40:40 +00:00
James Schofield
5878957524 mobile/bind: Fix objc bindings for multiple return vals.
The existing implementation generates uncompilable objc code for
functions returning two results.

Change-Id: I13f7329596d8fa6103c4b7827c26c5461e16a925
Reviewed-on: https://go-review.googlesource.com/11364
Reviewed-by: David Crawshaw <crawshaw@golang.org>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2015-06-24 18:25:43 +00:00
Hyang-Ah (Hana) Kim
3a9b5b50ac bind: correct methods' return arg type mapping in obj-c binding.
Change-Id: Ieda05982c7f894c5603103b8890658e682e97423
Reviewed-on: https://go-review.googlesource.com/11307
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-06-23 13:13:31 +00:00
Hyang-Ah (Hana) Kim
d7fb1ce757 bind: fix a bug in handling func returning a struct.
Change-Id: I63963c20c283638cfdc86b037f6aff70a115a78c
Reviewed-on: https://go-review.googlesource.com/10893
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-06-11 17:32:32 +00:00
Hyang-Ah (Hana) Kim
9c7482eec9 bind/objc: use gobind -lang=objc,go to generate code.
Removed checked-in version of go, objective-c proxy code.

Change-Id: Iac2c6ba3156def680746b5ae1fe02d6e712532a7
Reviewed-on: https://go-review.googlesource.com/10842
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-06-10 00:17:26 +00:00
Hyang-Ah (Hana) Kim
8f4eb9eb49 bind: add objective-c code generator.
Translate error type to NSError.
Interface type is not supported yet.

Change-Id: I54abba2360cff41ef8ca08063b0120e7edd65a47
Reviewed-on: https://go-review.googlesource.com/10793
Reviewed-by: David Crawshaw <crawshaw@golang.org>
Reviewed-by: Damien Neil <dneil@google.com>
2015-06-09 20:58:21 +00:00
Hyang-Ah (Hana) Kim
343eae7dd2 bind/objc: simplify reference counting of Go objects.
With https://go-review.googlesource.com/10638, the reference counting
of Go objects can be simplified. Everytime a Go object is passed into
Objective-C side, a new proxy object with GoSeqRef is created.
Deallocation of a GoSeqRef decrements the reference counter of the
corresponding Go object.

Test the Go object is collected.

Note: It's possible to reduce the number of GoSeqRef
allocations by maintaining a map of weak references to the existing
GoSeqRef, but for now, we allocate a new GoSeqRef instance.

Change-Id: I57a5a4b249c5800d9eba95d128a2cdf74bbef566
Reviewed-on: https://go-review.googlesource.com/10639
Reviewed-by: David Crawshaw <crawshaw@golang.org>
Reviewed-by: Damien Neil <dneil@google.com>
2015-06-04 23:02:01 +00:00
Hyang-Ah (Hana) Kim
b24fa8f869 bind/objc: add read/write Int and BOOL values.
Change-Id: I9e1d0c62acfa43cfc1b0dd79ff25b89b43a891cb
Reviewed-on: https://go-review.googlesource.com/10710
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-06-04 20:31:20 +00:00
David Crawshaw
f373bd373c bind: rename parameter p
Fixes golang/go#11064.

Change-Id: Idb4376f0143b0d1c8e87e7ccc54aae343b504e4a
Reviewed-on: https://go-review.googlesource.com/10689
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2015-06-04 17:29:43 +00:00
Hyang-Ah (Hana) Kim
c533dca65f bind/java: manage Java object lifetime based on reference count.
The gobind framework is supposed to use reference counting to
keep track of objects (e.g. pointer to a Go struct, interface
values) crossing the language boundary. This change fixes two bugs:

1) no reference counting on Java object: Previously, the lifetime
of a Java object was manages in the following way.

 a. The Java object is pinned in an internal map (javaObjs) when it's
constructed.
 b. When Go receives the reference to the Java object, it creates a
proxy object and sets a finalizer on it. The finalizer signals Java
to unpin the Java object (remove from the javaObjs map).
 c. The javaObjs map is also used to identify the Java object when
Go asks to invoke a method on it later.

When the same Java object is sent to Java more than once, and the
finalizer (b) runs after the first use, the second use of the Java
object can cause the crash described in golang/go#10933.

This change fixes the bug by reference counting the Java object.
Java side pins the Java object and increments the refcount whenever it
sees the object sent to Go (in Seq.writeRef). When the Go proxy
object's finalizer runs, the refcount is decremented. When the refcount
becomes 0, the object gets unpined.

2) race in Go object lifetime management: Pinning on a Go object
has been done when the Go object is sent to Java but the Go object
is not in the pinned object map yet. (bind/seq.WriteGoRef).
Unpinning the object occurs when Java finds there are no proxy objects
on its side. For this, Java maintains a reference count map (goObjs).
When the refcount becomes zero, Java notifies Go so the object is
unpinned. Here is a race case:

 a. Java has a proxy object for a Go object.
 b. Go is preparing for sending the same Go object. seq.WriteGoRef
notices the corresponding entry in the pinned object map already,
and returns. The remaining work for sending the object continues.
 c. The proxy object in Java finalizes and triggers deletion of the
object from the pinned object map.
 d. The remaining work for (b) completes and Java creates a new proxy
object. When a method is called for the Go object, the Go object is
already removed from the object map on Go side and maybe already GC'd.

This change fixes it by converting the pinned object map to reference
counter map maintained in Go. The counter increments for each
seq.WriteGoRef call. The finalizer of the proxy object in Java causes
a decrement of the counter.

Fixes golang/go#10933.

Renables the skipped testJavaRefGC.

Change-Id: I0992e002b1050b6183689e5ab821e058adbb420f
Reviewed-on: https://go-review.googlesource.com/10638
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-06-04 16:53:15 +00:00
Hyang-Ah (Hana) Kim
d8725953a1 bind: remove an unused field from javaGen.
Change-Id: If1eab27d334c38288fb0c7cec3e4bf2143ddaf91
Reviewed-on: https://go-review.googlesource.com/10637
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-06-03 14:31:32 +00:00