2
0
mirror of synced 2025-02-23 06:48:15 +00:00

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>
This commit is contained in:
Elias Naur 2016-03-14 17:28:24 +01:00
parent 51fe33c7bf
commit 3e830506b0
10 changed files with 49 additions and 2 deletions

View File

@ -275,6 +275,8 @@ func (g *goGen) genInterface(obj *types.TypeName) {
}
g.Printf("type proxy%s_%s _seq.Ref\n\n", g.pkgPrefix, obj.Name())
g.Printf("func (p *proxy%s_%s) Bind_proxy_refnum__() int32 { return p.Num }\n\n", g.pkgPrefix, obj.Name())
for _, m := range summary.callable {
sig := m.Type().(*types.Signature)
params := sig.Params()

View File

@ -497,4 +497,9 @@ public class SeqTest extends InstrumentationTestCase {
Testpkg.WithImportedI(i);
Testpkg.WithImportedS(s);
}
public void testIDup() {
Testpkg.I want = new AnI();
assertTrue("java object passed through Go should not be wrapped", want == Testpkg.IDup(want));
}
}

View File

@ -367,4 +367,10 @@ static int numI = 0;
[fields setS:s];
}
- (void)testIDup {
Number *want = [[Number alloc] init];
Number *got = GoTestpkgIDup(want);
XCTAssertEqual(got, want, @"ObjC object passed through Go should not be wrapped");
}
@end

View File

@ -45,9 +45,23 @@ type Ref struct {
Num int32
}
// ToRefNum increments the reference count for a Go object and
// return its refnum.
type proxy interface {
// Use a strange name and hope that user code does not implement it
Bind_proxy_refnum__() int32
}
// ToRefNum increments the reference count for an object and
// returns its refnum.
func ToRefNum(obj interface{}) int32 {
// We don't track foreign objects, so if obj is a proxy
// return its refnum.
if r, ok := obj.(proxy); ok {
refnum := r.Bind_proxy_refnum__()
if refnum <= 0 {
panic(fmt.Errorf("seq: proxy contained invalid Go refnum: %d", refnum))
}
return refnum
}
refs.Lock()
num := refs.refs[obj]
if num != 0 {

View File

@ -38,6 +38,8 @@ func proxyinterfaces_Error_Err(refnum C.int32_t) C.nstring {
type proxyinterfaces_Error _seq.Ref
func (p *proxyinterfaces_Error) Bind_proxy_refnum__() int32 { return p.Num }
func (p *proxyinterfaces_Error) Err() error {
res := C.cproxyinterfaces_Error_Err(C.int32_t(p.Num))
_res_str := decodeString(res)
@ -56,6 +58,8 @@ func proxyinterfaces_I_Rand(refnum C.int32_t) C.int32_t {
type proxyinterfaces_I _seq.Ref
func (p *proxyinterfaces_I) Bind_proxy_refnum__() int32 { return p.Num }
func (p *proxyinterfaces_I) Rand() int32 {
res := C.cproxyinterfaces_I_Rand(C.int32_t(p.Num))
_res := int32(res)
@ -90,6 +94,8 @@ func proxyinterfaces_I3_F(refnum C.int32_t) C.int32_t {
type proxyinterfaces_I3 _seq.Ref
func (p *proxyinterfaces_I3) Bind_proxy_refnum__() int32 { return p.Num }
func (p *proxyinterfaces_I3) F() interfaces.I1 {
res := C.cproxyinterfaces_I3_F(C.int32_t(p.Num))
var _res interfaces.I1
@ -112,6 +118,8 @@ func proxyinterfaces_WithParam_HasParam(refnum C.int32_t, param_p0 C.char) {
type proxyinterfaces_WithParam _seq.Ref
func (p *proxyinterfaces_WithParam) Bind_proxy_refnum__() int32 { return p.Num }
func (p *proxyinterfaces_WithParam) HasParam(param_p0 bool) {
var _param_p0 C.char = 0
if param_p0 {

View File

@ -58,6 +58,8 @@ func proxyissue10788_TestInterface_MultipleUnnamedParams(refnum C.int32_t, param
type proxyissue10788_TestInterface _seq.Ref
func (p *proxyissue10788_TestInterface) Bind_proxy_refnum__() int32 { return p.Num }
func (p *proxyissue10788_TestInterface) DoSomeWork(param_s *issue10788.TestStruct) {
var _param_s C.int32_t = _seq.NullRefNum
if param_s != nil {

View File

@ -49,6 +49,8 @@ func proxyissue12403_Parsable_ToJSON(refnum C.int32_t) (C.nstring, C.nstring) {
type proxyissue12403_Parsable _seq.Ref
func (p *proxyissue12403_Parsable) Bind_proxy_refnum__() int32 { return p.Num }
func (p *proxyissue12403_Parsable) FromJSON(param_jstr string) string {
_param_jstr := encodeString(param_jstr)
res := C.cproxyissue12403_Parsable_FromJSON(C.int32_t(p.Num), _param_jstr)

View File

@ -104,6 +104,8 @@ func proxystructs_I_M(refnum C.int32_t) {
type proxystructs_I _seq.Ref
func (p *proxystructs_I) Bind_proxy_refnum__() int32 { return p.Num }
func (p *proxystructs_I) M() {
C.cproxystructs_I_M(C.int32_t(p.Num))
}

View File

@ -23,6 +23,8 @@ var _ = _seq.FromRefNum
type proxyvars_I _seq.Ref
func (p *proxyvars_I) Bind_proxy_refnum__() int32 { return p.Num }
//export var_setvars_ABool
func var_setvars_ABool(v C.char) {
_v := v != 0

View File

@ -138,6 +138,10 @@ func NumSCollected() int {
return numSCollected
}
func IDup(i I) I {
return i
}
func StrDup(s string) string {
return s
}