From 6d80e5a85f2385fb9a65e1d3303ed61b3e8943c8 Mon Sep 17 00:00:00 2001 From: "Hyang-Ah (Hana) Kim" Date: Thu, 14 May 2015 15:46:09 -0400 Subject: [PATCH] bind: support Bool types. Also add missing int8/int16 handling in bind/seq. Fixes golang/go#10855. Change-Id: I326ada44df10fc3c22628bdd2ae4ee2c3dc7902e Reviewed-on: https://go-review.googlesource.com/10046 Reviewed-by: Alan Donovan --- bind/java/Seq.java | 2 ++ bind/java/SeqTest.java | 5 +++ bind/java/seq_android.c | 14 ++++++++ bind/java/testpkg/testpkg.go | 4 +++ bind/seq.go | 2 ++ bind/seq/buffer.go | 50 +++++++++++++++++++++++++++- bind/testdata/basictypes.go | 2 ++ bind/testdata/basictypes.go.golden | 15 ++++++--- bind/testdata/basictypes.java.golden | 19 ++++++++--- 9 files changed, 104 insertions(+), 9 deletions(-) diff --git a/bind/java/Seq.java b/bind/java/Seq.java index d86bca6..028b2f2 100644 --- a/bind/java/Seq.java +++ b/bind/java/Seq.java @@ -31,6 +31,7 @@ public class Seq { public native void log(String label); + public native boolean readBool(); public native byte readInt8(); public native short readInt16(); public native int readInt32(); @@ -43,6 +44,7 @@ public class Seq { public String readString() { return readUTF16(); } public native byte[] readByteArray(); + public native void writeBool(boolean v); public native void writeInt8(byte v); public native void writeInt16(short v); public native void writeInt32(int v); diff --git a/bind/java/SeqTest.java b/bind/java/SeqTest.java index 0aee7e7..1ed7fae 100644 --- a/bind/java/SeqTest.java +++ b/bind/java/SeqTest.java @@ -22,6 +22,11 @@ public class SeqTest extends AndroidTestCase { assertEquals("Unexpected arithmetic failure", 7, res); } + public void testBool() { + assertTrue(Testpkg.Negate(false)); + assertFalse(Testpkg.Negate(true)); + } + public void testShortString() { String want = "a short string"; String got = Testpkg.StrDup(want); diff --git a/bind/java/seq_android.c b/bind/java/seq_android.c index c94526a..29cefc9 100644 --- a/bind/java/seq_android.c +++ b/bind/java/seq_android.c @@ -250,6 +250,15 @@ Java_go_Seq_free(JNIEnv *env, jobject obj) { #define MEM_READ(obj, ty) ((ty*)mem_read(env, obj, sizeof(ty), sizeof(ty))) +JNIEXPORT jboolean JNICALL +Java_go_Seq_readBool(JNIEnv *env, jobject obj) { + int8_t *v = MEM_READ(obj, int8_t); + if (v == NULL) { + return 0; + } + return *v != 0 ? 1 : 0; +} + JNIEXPORT jbyte JNICALL Java_go_Seq_readInt8(JNIEnv *env, jobject obj) { uint8_t *v = MEM_READ(obj, uint8_t); @@ -314,6 +323,11 @@ Java_go_Seq_readByteArray(JNIEnv *env, jobject obj) { #define MEM_WRITE(ty) (*(ty*)mem_write(env, obj, sizeof(ty), sizeof(ty))) +JNIEXPORT void JNICALL +Java_go_Seq_writeBool(JNIEnv *env, jobject obj, jboolean v) { + MEM_WRITE(int8_t) = v ? 1 : 0; +} + JNIEXPORT void JNICALL Java_go_Seq_writeInt8(JNIEnv *env, jobject obj, jbyte v) { MEM_WRITE(int8_t) = v; diff --git a/bind/java/testpkg/testpkg.go b/bind/java/testpkg/testpkg.go index 06b1536..ea291b9 100644 --- a/bind/java/testpkg/testpkg.go +++ b/bind/java/testpkg/testpkg.go @@ -104,6 +104,10 @@ func StrDup(s string) string { return s } +func Negate(x bool) bool { + return !x +} + func Err(s string) error { if s != "" { return errors.New(s) diff --git a/bind/seq.go b/bind/seq.go index fbcdf96..1e41848 100644 --- a/bind/seq.go +++ b/bind/seq.go @@ -16,6 +16,8 @@ func seqType(t types.Type) string { switch t := t.(type) { case *types.Basic: switch t.Kind() { + case types.Bool: + return "Bool" case types.Int: return "Int" case types.Int8: diff --git a/bind/seq/buffer.go b/bind/seq/buffer.go index d6c37f5..372ecf2 100644 --- a/bind/seq/buffer.go +++ b/bind/seq/buffer.go @@ -60,6 +60,26 @@ func align(offset, alignment int) int { return pad + offset } +func (b *Buffer) ReadInt8() int8 { + offset := b.Offset + if len(b.Data)-offset < 1 { + b.panic(1) + } + v := *(*int8)(unsafe.Pointer(&b.Data[offset])) + b.Offset++ + return v +} + +func (b *Buffer) ReadInt16() int16 { + offset := align(b.Offset, 2) + if len(b.Data)-offset < 2 { + b.panic(2) + } + v := *(*int16)(unsafe.Pointer(&b.Data[offset])) + b.Offset = offset + 2 + return v +} + func (b *Buffer) ReadInt32() int32 { offset := align(b.Offset, 4) if len(b.Data)-offset < 4 { @@ -80,7 +100,9 @@ func (b *Buffer) ReadInt64() int64 { return v } -// TODO(hyangah): int8, int16? +func (b *Buffer) ReadBool() bool { + return b.ReadInt8() != 0 +} func (b *Buffer) ReadInt() int { return int(b.ReadInt64()) @@ -136,6 +158,24 @@ func (b *Buffer) ReadString() string { return DecString(b) } +func (b *Buffer) WriteInt8(v int8) { + offset := b.Offset + if len(b.Data)-offset < 1 { + b.grow(offset + 1 - len(b.Data)) + } + *(*int8)(unsafe.Pointer(&b.Data[offset])) = v + b.Offset++ +} + +func (b *Buffer) WriteInt16(v int16) { + offset := align(b.Offset, 2) + if len(b.Data)-offset < 2 { + b.grow(offset + 2 - len(b.Data)) + } + *(*int16)(unsafe.Pointer(&b.Data[offset])) = v + b.Offset = offset + 2 +} + func (b *Buffer) WriteInt32(v int32) { offset := align(b.Offset, 4) if len(b.Data)-offset < 4 { @@ -154,6 +194,14 @@ func (b *Buffer) WriteInt64(v int64) { b.Offset = offset + 8 } +func (b *Buffer) WriteBool(v bool) { + if v { + b.WriteInt8(1) + } else { + b.WriteInt8(0) + } +} + func (b *Buffer) WriteInt(v int) { b.WriteInt64(int64(v)) } diff --git a/bind/testdata/basictypes.go b/bind/testdata/basictypes.go index bc0bffb..579fbf7 100644 --- a/bind/testdata/basictypes.go +++ b/bind/testdata/basictypes.go @@ -11,3 +11,5 @@ func Error() error { return nil } func ErrorPair() (int, error) { return 0, nil } func ByteArrays(x []byte) []byte { return nil } + +func Bool(bool) bool { return true } diff --git a/bind/testdata/basictypes.go.golden b/bind/testdata/basictypes.go.golden index ab3d26b..f741dc3 100644 --- a/bind/testdata/basictypes.go.golden +++ b/bind/testdata/basictypes.go.golden @@ -9,6 +9,12 @@ import ( "golang.org/x/mobile/bind/seq" ) +func proxy_Bool(out, in *seq.Buffer) { + param_p0 := in.ReadBool() + res := basictypes.Bool(param_p0) + out.WriteBool(res) +} + func proxy_ByteArrays(out, in *seq.Buffer) { param_x := in.ReadByteArray() res := basictypes.ByteArrays(param_x) @@ -44,8 +50,9 @@ func proxy_Ints(out, in *seq.Buffer) { } func init() { - seq.Register("basictypes", 1, proxy_ByteArrays) - seq.Register("basictypes", 2, proxy_Error) - seq.Register("basictypes", 3, proxy_ErrorPair) - seq.Register("basictypes", 4, proxy_Ints) + seq.Register("basictypes", 1, proxy_Bool) + seq.Register("basictypes", 2, proxy_ByteArrays) + seq.Register("basictypes", 3, proxy_Error) + seq.Register("basictypes", 4, proxy_ErrorPair) + seq.Register("basictypes", 5, proxy_Ints) } diff --git a/bind/testdata/basictypes.java.golden b/bind/testdata/basictypes.java.golden index d9426a2..8255242 100644 --- a/bind/testdata/basictypes.java.golden +++ b/bind/testdata/basictypes.java.golden @@ -9,6 +9,16 @@ import go.Seq; public abstract class Basictypes { private Basictypes() {} // uninstantiable + public static boolean Bool(boolean p0) { + go.Seq _in = new go.Seq(); + go.Seq _out = new go.Seq(); + boolean _result; + _in.writeBool(p0); + Seq.send(DESCRIPTOR, CALL_Bool, _in, _out); + _result = _out.readBool(); + return _result; + } + public static byte[] ByteArrays(byte[] x) { go.Seq _in = new go.Seq(); go.Seq _out = new go.Seq(); @@ -53,9 +63,10 @@ public abstract class Basictypes { Seq.send(DESCRIPTOR, CALL_Ints, _in, _out); } - private static final int CALL_ByteArrays = 1; - private static final int CALL_Error = 2; - private static final int CALL_ErrorPair = 3; - private static final int CALL_Ints = 4; + private static final int CALL_Bool = 1; + private static final int CALL_ByteArrays = 2; + private static final int CALL_Error = 3; + private static final int CALL_ErrorPair = 4; + private static final int CALL_Ints = 5; private static final String DESCRIPTOR = "basictypes"; }