From 334412418f5ccf83d373be3c554778ff2a06af7f Mon Sep 17 00:00:00 2001 From: "Hyang-Ah (Hana) Kim" Date: Mon, 6 Apr 2015 16:43:18 -0400 Subject: [PATCH] bind: replace {Read,Write}UTF16 with {Read,Write}String for string. This is to enable more flexible encoding/decoding of Go string. For Java, we use UTF16 to be compatible with java string. For other languages, we will want other way to represent a string. Change-Id: Iccd53e2eea18d37636c3c619d06cb473facef0cd Reviewed-on: https://go-review.googlesource.com/8628 Reviewed-by: David Crawshaw --- bind/gengo.go | 4 +-- bind/genjava.go | 6 ++-- bind/java/Seq.java | 2 ++ bind/java/seq_android.go | 12 +++++++- bind/java/testpkg/Testpkg.java | 32 ++++++++++---------- bind/java/testpkg/go_testpkg/go_testpkg.go | 34 +++++++++++----------- bind/seq.go | 4 +-- bind/seq/buffer.go | 8 +++++ bind/seq/seq.go | 6 ++++ bind/testdata/basictypes.go.golden | 8 ++--- bind/testdata/basictypes.java.golden | 4 +-- 11 files changed, 73 insertions(+), 47 deletions(-) diff --git a/bind/gengo.go b/bind/gengo.go index 6e9a088..6d2dca2 100644 --- a/bind/gengo.go +++ b/bind/gengo.go @@ -91,9 +91,9 @@ func (g *goGen) genFuncBody(o *types.Func, selectorLHS string) { func (g *goGen) genWrite(valName, seqName string, T types.Type) { if isErrorType(T) { g.Printf("if %s == nil {\n", valName) - g.Printf(" %s.WriteUTF16(\"\");\n", seqName) + g.Printf(" %s.WriteString(\"\");\n", seqName) g.Printf("} else {\n") - g.Printf(" %s.WriteUTF16(%s.Error());\n", seqName, valName) + g.Printf(" %s.WriteString(%s.Error());\n", seqName, valName) g.Printf("}\n") return } diff --git a/bind/genjava.go b/bind/genjava.go index 61575d9..c6b23a6 100644 --- a/bind/genjava.go +++ b/bind/genjava.go @@ -205,7 +205,7 @@ func (g *javaGen) genInterfaceStub(o *types.TypeName, m *types.Interface) { g.Printf("out.write%s;\n", seqWrite(res.At(0).Type(), "result")) } if returnsError { - g.Printf("out.writeUTF16(null);\n") + g.Printf("out.writeString(null);\n") g.Outdent() g.Printf("} catch (Exception e) {\n") g.Indent() @@ -214,7 +214,7 @@ func (g *javaGen) genInterfaceStub(o *types.TypeName, m *types.Interface) { g.Printf("%s result = %s;\n", g.javaType(resTyp), g.javaTypeDefault(resTyp)) g.Printf("out.write%s;\n", seqWrite(resTyp, "result")) } - g.Printf("out.writeUTF16(e.getMessage());\n") + g.Printf("out.writeString(e.getMessage());\n") g.Outdent() g.Printf("}\n") } @@ -483,7 +483,7 @@ func (g *javaGen) genFunc(o *types.Func, method bool) { g.genRead("_result", "_out", resultType) } if returnsError { - g.Printf(`String _err = _out.readUTF16(); + g.Printf(`String _err = _out.readString(); if (_err != null) { throw new Exception(_err); } diff --git a/bind/java/Seq.java b/bind/java/Seq.java index 516a875..417ab26 100644 --- a/bind/java/Seq.java +++ b/bind/java/Seq.java @@ -36,6 +36,7 @@ public class Seq { public native float readFloat32(); public native double readFloat64(); public native String readUTF16(); + public String readString() { return readUTF16(); } public native byte[] readByteArray(); public native void writeInt8(byte v); @@ -47,6 +48,7 @@ public class Seq { public native void writeFloat32(float v); public native void writeFloat64(double v); public native void writeUTF16(String v); + public void writeString(String v) { writeUTF16(v); } public native void writeByteArray(byte[] v); public void writeRef(Ref ref) { diff --git a/bind/java/seq_android.go b/bind/java/seq_android.go index 62f4f2d..d5a25b5 100644 --- a/bind/java/seq_android.go +++ b/bind/java/seq_android.go @@ -86,7 +86,7 @@ func initSeq() { } func seqToBuf(bufptr **C.uint8_t, lenptr *C.size_t, buf *seq.Buffer) { - if false { + if debug { fmt.Printf("seqToBuf tag 1, len(buf.Data)=%d, *lenptr=%d\n", len(buf.Data), *lenptr) } if len(buf.Data) == 0 { @@ -166,6 +166,14 @@ func transact(ref *seq.Ref, code int, in *seq.Buffer) *seq.Buffer { return out } +func encodeString(out *seq.Buffer, v string) { + out.WriteUTF16(v) +} + +func decodeString(in *seq.Buffer) string { + return in.ReadUTF16() +} + func init() { seq.FinalizeRef = func(ref *seq.Ref) { if ref.Num < 0 { @@ -175,4 +183,6 @@ func init() { } seq.Transact = transact + seq.EncString = encodeString + seq.DecString = decodeString } diff --git a/bind/java/testpkg/Testpkg.java b/bind/java/testpkg/Testpkg.java index d4fd2b9..6751826 100644 --- a/bind/java/testpkg/Testpkg.java +++ b/bind/java/testpkg/Testpkg.java @@ -24,7 +24,7 @@ public abstract class Testpkg { go.Seq _in = new go.Seq(); go.Seq _out = new go.Seq(); byte[] _result; - _in.writeUTF16(str); + _in.writeString(str); _in.writeByteArray(someBytes); Seq.send(DESCRIPTOR, CALL_AppendToString, _in, _out); _result = _out.readByteArray(); @@ -47,7 +47,7 @@ public abstract class Testpkg { go.Seq _out = new go.Seq(); _in.writeRef(i.ref()); Seq.send(DESCRIPTOR, CALL_CallE, _in, _out); - String _err = _out.readUTF16(); + String _err = _out.readString(); if (_err != null) { throw new Exception(_err); } @@ -97,7 +97,7 @@ public abstract class Testpkg { _in.writeRef(i.ref()); Seq.send(DESCRIPTOR, CALL_CallVE, _in, _out); _result = _out.readInt(); - String _err = _out.readUTF16(); + String _err = _out.readString(); if (_err != null) { throw new Exception(_err); } @@ -107,9 +107,9 @@ public abstract class Testpkg { public static void Err(String s) throws Exception { go.Seq _in = new go.Seq(); go.Seq _out = new go.Seq(); - _in.writeUTF16(s); + _in.writeString(s); Seq.send(DESCRIPTOR, CALL_Err, _in, _out); - String _err = _out.readUTF16(); + String _err = _out.readString(); if (_err != null) { throw new Exception(_err); } @@ -151,9 +151,9 @@ public abstract class Testpkg { case Proxy.CALL_E: { try { this.E(); - out.writeUTF16(null); + out.writeString(null); } catch (Exception e) { - out.writeUTF16(e.getMessage()); + out.writeString(e.getMessage()); } return; } @@ -173,7 +173,7 @@ public abstract class Testpkg { } case Proxy.CALL_String: { String result = this.String(); - out.writeUTF16(result); + out.writeString(result); return; } case Proxy.CALL_V: { @@ -185,11 +185,11 @@ public abstract class Testpkg { try { long result = this.VE(); out.writeInt(result); - out.writeUTF16(null); + out.writeString(null); } catch (Exception e) { long result = 0; out.writeInt(result); - out.writeUTF16(e.getMessage()); + out.writeString(e.getMessage()); } return; } @@ -217,7 +217,7 @@ public abstract class Testpkg { go.Seq _out = new go.Seq(); _in.writeRef(ref); Seq.send(DESCRIPTOR, CALL_E, _in, _out); - String _err = _out.readUTF16(); + String _err = _out.readString(); if (_err != null) { throw new Exception(_err); } @@ -256,7 +256,7 @@ public abstract class Testpkg { String _result; _in.writeRef(ref); Seq.send(DESCRIPTOR, CALL_String, _in, _out); - _result = _out.readUTF16(); + _result = _out.readString(); return _result; } @@ -277,7 +277,7 @@ public abstract class Testpkg { _in.writeRef(ref); Seq.send(DESCRIPTOR, CALL_VE, _in, _out); _result = _out.readInt(); - String _err = _out.readUTF16(); + String _err = _out.readString(); if (_err != null) { throw new Exception(_err); } @@ -348,7 +348,7 @@ public abstract class Testpkg { String _result; _in.writeRef(ref); Seq.send(DESCRIPTOR, CALL_String, _in, _out); - _result = _out.readUTF16(); + _result = _out.readString(); return _result; } @@ -376,9 +376,9 @@ public abstract class Testpkg { go.Seq _in = new go.Seq(); go.Seq _out = new go.Seq(); String _result; - _in.writeUTF16(s); + _in.writeString(s); Seq.send(DESCRIPTOR, CALL_StrDup, _in, _out); - _result = _out.readUTF16(); + _result = _out.readString(); return _result; } diff --git a/bind/java/testpkg/go_testpkg/go_testpkg.go b/bind/java/testpkg/go_testpkg/go_testpkg.go index 9762a42..be43ee9 100644 --- a/bind/java/testpkg/go_testpkg/go_testpkg.go +++ b/bind/java/testpkg/go_testpkg/go_testpkg.go @@ -17,7 +17,7 @@ func proxy_Add(out, in *seq.Buffer) { } func proxy_AppendToString(out, in *seq.Buffer) { - param_str := in.ReadUTF16() + param_str := in.ReadString() param_someBytes := in.ReadByteArray() res := testpkg.AppendToString(param_str, param_someBytes) out.WriteByteArray(res) @@ -40,9 +40,9 @@ func proxy_CallE(out, in *seq.Buffer) { } err := testpkg.CallE(param_i) if err == nil { - out.WriteUTF16("") + out.WriteString("") } else { - out.WriteUTF16(err.Error()) + out.WriteString(err.Error()) } } @@ -104,19 +104,19 @@ func proxy_CallVE(out, in *seq.Buffer) { res, err := testpkg.CallVE(param_i) out.WriteInt(res) if err == nil { - out.WriteUTF16("") + out.WriteString("") } else { - out.WriteUTF16(err.Error()) + out.WriteString(err.Error()) } } func proxy_Err(out, in *seq.Buffer) { - param_s := in.ReadUTF16() + param_s := in.ReadString() err := testpkg.Err(param_s) if err == nil { - out.WriteUTF16("") + out.WriteString("") } else { - out.WriteUTF16(err.Error()) + out.WriteString(err.Error()) } } @@ -140,9 +140,9 @@ func proxyIE(out, in *seq.Buffer) { v := ref.Get().(testpkg.I) err := v.E() if err == nil { - out.WriteUTF16("") + out.WriteString("") } else { - out.WriteUTF16(err.Error()) + out.WriteString(err.Error()) } } @@ -170,7 +170,7 @@ func proxyIString(out, in *seq.Buffer) { ref := in.ReadRef() v := ref.Get().(testpkg.I) res := v.String() - out.WriteUTF16(res) + out.WriteString(res) } func proxyIV(out, in *seq.Buffer) { @@ -186,9 +186,9 @@ func proxyIVE(out, in *seq.Buffer) { res, err := v.VE() out.WriteInt(res) if err == nil { - out.WriteUTF16("") + out.WriteString("") } else { - out.WriteUTF16(err.Error()) + out.WriteString(err.Error()) } } @@ -241,7 +241,7 @@ func (p *proxyI) S() *testpkg.S { func (p *proxyI) String() string { in := new(seq.Buffer) out := seq.Transact((*seq.Ref)(p), proxyIStringCode, in) - res_0 := out.ReadUTF16() + res_0 := out.ReadString() return res_0 } @@ -299,7 +299,7 @@ func proxySString(out, in *seq.Buffer) { ref := in.ReadRef() v := ref.Get().(*testpkg.S) res := v.String() - out.WriteUTF16(res) + out.WriteString(res) } func init() { @@ -308,9 +308,9 @@ func init() { } func proxy_StrDup(out, in *seq.Buffer) { - param_s := in.ReadUTF16() + param_s := in.ReadString() res := testpkg.StrDup(param_s) - out.WriteUTF16(res) + out.WriteString(res) } func init() { diff --git a/bind/seq.go b/bind/seq.go index f1aa3ac..fbcdf96 100644 --- a/bind/seq.go +++ b/bind/seq.go @@ -11,7 +11,7 @@ import ( // TODO(hyangah): avoid panic; gobind needs to output the problematic code location. func seqType(t types.Type) string { if isErrorType(t) { - return "UTF16" + return "String" } switch t := t.(type) { case *types.Basic: @@ -35,7 +35,7 @@ func seqType(t types.Type) string { case types.Float64: return "Float64" case types.String: - return "UTF16" + return "String" default: // Should be caught earlier in processing. panic(fmt.Sprintf("unsupported basic seqType: %s", t)) diff --git a/bind/seq/buffer.go b/bind/seq/buffer.go index 462ed77..d6c37f5 100644 --- a/bind/seq/buffer.go +++ b/bind/seq/buffer.go @@ -132,6 +132,10 @@ func (b *Buffer) ReadRef() *Ref { return ref } +func (b *Buffer) ReadString() string { + return DecString(b) +} + func (b *Buffer) WriteInt32(v int32) { offset := align(b.Offset, 4) if len(b.Data)-offset < 4 { @@ -185,6 +189,10 @@ func (b *Buffer) WriteByteArray(byt []byte) { return } +func (b *Buffer) WriteString(v string) { + EncString(b, v) +} + func (b *Buffer) WriteGoRef(obj interface{}) { refs.Lock() num := refs.refs[obj] diff --git a/bind/seq/seq.go b/bind/seq/seq.go index 2239bdf..b134a1b 100644 --- a/bind/seq/seq.go +++ b/bind/seq/seq.go @@ -44,3 +44,9 @@ func Register(descriptor string, code int, fn Func) { } m[code] = fn } + +// DecString decodes a string encoded in the Buffer. +var DecString func(in *Buffer) string + +// EncString encodes a Go string into the Buffer. +var EncString func(out *Buffer, v string) diff --git a/bind/testdata/basictypes.go.golden b/bind/testdata/basictypes.go.golden index ed35dda..ab3d26b 100644 --- a/bind/testdata/basictypes.go.golden +++ b/bind/testdata/basictypes.go.golden @@ -18,9 +18,9 @@ func proxy_ByteArrays(out, in *seq.Buffer) { func proxy_Error(out, in *seq.Buffer) { err := basictypes.Error() if err == nil { - out.WriteUTF16("") + out.WriteString("") } else { - out.WriteUTF16(err.Error()) + out.WriteString(err.Error()) } } @@ -28,9 +28,9 @@ func proxy_ErrorPair(out, in *seq.Buffer) { res, err := basictypes.ErrorPair() out.WriteInt(res) if err == nil { - out.WriteUTF16("") + out.WriteString("") } else { - out.WriteUTF16(err.Error()) + out.WriteString(err.Error()) } } diff --git a/bind/testdata/basictypes.java.golden b/bind/testdata/basictypes.java.golden index f44ad6e..d9426a2 100644 --- a/bind/testdata/basictypes.java.golden +++ b/bind/testdata/basictypes.java.golden @@ -23,7 +23,7 @@ public abstract class Basictypes { go.Seq _in = new go.Seq(); go.Seq _out = new go.Seq(); Seq.send(DESCRIPTOR, CALL_Error, _in, _out); - String _err = _out.readUTF16(); + String _err = _out.readString(); if (_err != null) { throw new Exception(_err); } @@ -35,7 +35,7 @@ public abstract class Basictypes { long _result; Seq.send(DESCRIPTOR, CALL_ErrorPair, _in, _out); _result = _out.readInt(); - String _err = _out.readUTF16(); + String _err = _out.readString(); if (_err != null) { throw new Exception(_err); }