diff --git a/encode_compile.go b/encode_compile.go index 54819df..fcbb05d 100644 --- a/encode_compile.go +++ b/encode_compile.go @@ -954,7 +954,7 @@ func (e *Encoder) compileStruct(ctx *encodeCompileContext, isPtr bool) (*opcode, } structEndCode.displayIdx = ctx.opcodeIndex - structEndCode.idx = opcodeOffset(ctx.opcodeIndex) + structEndCode.idx = opcodeOffset(ctx.ptrIndex) ctx.incIndex() if ctx.withIndent { diff --git a/encode_opcode.go b/encode_opcode.go index 9ff35ba..a3d9e1a 100644 --- a/encode_opcode.go +++ b/encode_opcode.go @@ -95,6 +95,18 @@ func (c *opcode) decOpcodeIndex() { for code := c; code.op != opEnd; { code.displayIdx-- code.idx -= uintptrSize + if code.headIdx > 0 { + code.headIdx -= uintptrSize + } + if code.elemIdx > 0 { + code.elemIdx -= uintptrSize + } + if code.mapIter > 0 { + code.mapIter -= uintptrSize + } + if code.length > 0 && code.op.codeType() != codeArrayHead && code.op.codeType() != codeArrayElem { + code.length -= uintptrSize + } switch code.op.codeType() { case codeArrayElem, codeSliceElem, codeMapKey: code = code.end @@ -106,7 +118,7 @@ func (c *opcode) decOpcodeIndex() { func (c *opcode) dumpHead(code *opcode) string { var length uintptr - if code.op.codeType() == codeArrayElem { + if code.op.codeType() == codeArrayHead { length = code.length } else { length = code.length / uintptrSize @@ -382,12 +394,3 @@ func newRecursiveCode(ctx *encodeCompileContext, jmp *compiledCode) *opcode { jmp: jmp, } } - -//func newRecursiveCode(recursive *recursiveCode) *opcode { -//code := copyOpcode(recursive.jmp.code) -//head := (*structFieldCode)(unsafe.Pointer(code)) -//head.end.next = newEndOp(&encodeCompileContext{}) - -//code.op = code.op.ptrHeadToHead() -// return code -//}