Commit 033d03cb authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 4db15124
...@@ -206,25 +206,33 @@ type Address struct { ...@@ -206,25 +206,33 @@ type Address struct {
} }
// NOTE if Host == "" -> Port not added to wire (see py.PAddress): // NOTE if Host == "" -> Port not added to wire (see py.PAddress):
// func (a *Address) neoMsgEncode(b []byte) int { func (a *Address) neoEncodedLen() int {
// n := string_NEOEncode(a.Host, b[0:]) l := string_neoEncodedLen(a.Host)
// if a.Host != "" { if a.Host != "" {
// BigEndian.PutUint16(b[n:], a.Port) l += 2
// n += 2 }
// } return l
// return n }
// }
// func (a *Address) neoEncode(b []byte) int {
// func (a *Address) NEODecode(b []byte) int { n := string_neoEncode(a.Host, b[0:])
// n := string_NEODecode(&a.Host, b) if a.Host != "" {
// if a.Host != "" { BigEndian.PutUint16(b[n:], a.Port)
// a.Port = BigEndian.Uint16(b[n:]) n += 2
// n += 2 }
// } else { return n
// a.Port = 0 }
// }
// return n func (a *Address) neoDecode(b []byte) int {
// } n := string_neoDecode(&a.Host, b)
if a.Host != "" {
a.Port = BigEndian.Uint16(b[n:])
n += 2
} else {
a.Port = 0
}
return n
}
// A SHA1 hash // A SHA1 hash
type Checksum [20]byte type Checksum [20]byte
......
...@@ -438,6 +438,10 @@ type CodeGenerator interface { ...@@ -438,6 +438,10 @@ type CodeGenerator interface {
genArray1(path string, typ *types.Array) genArray1(path string, typ *types.Array)
genSlice1(path string, typ types.Type) genSlice1(path string, typ types.Type)
// generate code for a custom type which implements its own encoding/decodeing
// XXX review text
genCustom(path string, typ types.Type) // XXX + obj?
// get generated code. // get generated code.
generatedCode() string generatedCode() string
} }
...@@ -1063,11 +1067,38 @@ func (d *decoder) genMap(assignto string, typ *types.Map, obj types.Object) { ...@@ -1063,11 +1067,38 @@ func (d *decoder) genMap(assignto string, typ *types.Map, obj types.Object) {
d.emit("}") d.emit("}")
} }
// emit code to size/encode/decode custom type
// XXX typ not needed
func (s *sizer) genCustom(path string, typ *types.Type) {
s.size.AddExpr("%s.neoEncodedLen()", path)
}
// XXX typ unused
func (e *encoder) genCustom(path string, typ *types.Type) {
e.emit("{")
e.emit("n := %s.neoEncodedLen()", path)
e.emit("%s.neoEncode(data[%v:])", e.n)
e.emit("data = data[%v + n:]", e.n)
e.emit("}")
e.n = 0
}
func (e *decoder) genCustom(path string) {
d.emit("{")
d.emit("n := %s.neoDecode(data[%v ...") // XXX error!
d.emit("}")
}
// top-level driver for emitting size/encode/decode code for a type // top-level driver for emitting size/encode/decode code for a type
// //
// obj is object that uses this type in source program (so in case of an error // obj is object that uses this type in source program (so in case of an error
// we can point to source location for where it happened) // we can point to source location for where it happened)
func codegenType(path string, typ types.Type, obj types.Object, codegen CodeGenerator) { func codegenType(path string, typ types.Type, obj types.Object, codegen CodeGenerator) {
if types.Implements(typ, neoCustomXXX) {
codegen.genCustom(path, typ)
return
}
switch u := typ.Underlying().(type) { switch u := typ.Underlying().(type) {
case *types.Basic: case *types.Basic:
// go puts string into basic, but it is really slice1 // go puts string into basic, but it is really slice1
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment