Commit 4a177116 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 732db7ce
...@@ -230,6 +230,8 @@ type CodecCodeGen interface { ...@@ -230,6 +230,8 @@ type CodecCodeGen interface {
type sizer struct { type sizer struct {
Buffer // XXX Buffer // XXX
n int n int
symLenv []string // symbolic lengths TODO
sizeVarUsed bool // whether size var was used
} }
type encoder struct { type encoder struct {
...@@ -242,6 +244,7 @@ type decoder struct { ...@@ -242,6 +244,7 @@ type decoder struct {
n int // current decode position in data n int // current decode position in data
} }
var _ CodecCodeGen = (*sizer)(nil)
var _ CodecCodeGen = (*encoder)(nil) var _ CodecCodeGen = (*encoder)(nil)
var _ CodecCodeGen = (*decoder)(nil) var _ CodecCodeGen = (*decoder)(nil)
...@@ -259,7 +262,9 @@ func (d *decoder) generatedCode() string { ...@@ -259,7 +262,9 @@ func (d *decoder) generatedCode() string {
func (s *sizer) genPrologue(recvName, typeName string) { func (s *sizer) genPrologue(recvName, typeName string) {
s.emit("func (%s *%s) NEOEncodedLen() int {", recvName, typeName) s.emit("func (%s *%s) NEOEncodedLen() int {", recvName, typeName)
if s.sizeVarUsed {
s.emit("var size int") s.emit("var size int")
}
} }
func (e *encoder) genPrologue(recvName, typeName string) { func (e *encoder) genPrologue(recvName, typeName string) {
...@@ -271,9 +276,16 @@ func (d *decoder) genPrologue(recvName, typeName string) { ...@@ -271,9 +276,16 @@ func (d *decoder) genPrologue(recvName, typeName string) {
d.emit("var nread uint32") d.emit("var nread uint32")
} }
func (e *sizer) genEpilogue() { func (s *sizer) genEpilogue() {
e.emit("return size + %v", e.n) size := fmt.Sprintf("%v", s.n)
e.emit("}\n") if len(s.symLenv) > 0 {
size += " + " + strings.Join(s.synLenv, " + ")
}
if s.sizeVarUsed {
size += " + size")
}
s.emit("return %v", size)
s.emit("}\n")
} }
func (e *encoder) genEpilogue() { func (e *encoder) genEpilogue() {
...@@ -329,8 +341,8 @@ func (d *decoder) genBasic(assignto string, typ *types.Basic, userType types.Typ ...@@ -329,8 +341,8 @@ func (d *decoder) genBasic(assignto string, typ *types.Basic, userType types.Typ
// TODO []byte support // TODO []byte support
func (s *sizer) genStrBytes(path string) { func (s *sizer) genStrBytes(path string) {
s.n += 4 s.n += 4
s.emit("size += %v + len(%s)", s.n, path) s.symLenv = append(s.symLenv, fmt.Sprintf("len(%s)", path))
s.n = 0 //s.n = 0
} }
func (e *encoder) genStrBytes(path string) { func (e *encoder) genStrBytes(path string) {
......
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