Skip to content

Commit

Permalink
fix: wrong behaviors on nested interface
Browse files Browse the repository at this point in the history
fix #359
  • Loading branch information
orisano committed Apr 21, 2022
1 parent 171d975 commit cfe4ac8
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 10 deletions.
13 changes: 13 additions & 0 deletions decode_test.go
Expand Up @@ -3918,3 +3918,16 @@ func TestIssue360(t *testing.T) {
t.Errorf("unexpected result: %v", uints)
}
}

func TestIssue359(t *testing.T) {
var a interface{} = 1
var b interface{} = &a
var c interface{} = &b
v, err := json.Marshal(c)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if string(v) != "1" {
t.Errorf("unexpected result: %v", string(v))
}
}
9 changes: 7 additions & 2 deletions internal/cmd/generator/vm.go.tmpl
Expand Up @@ -14,6 +14,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
recursiveLevel := 0
ptrOffset := uintptr(0)
ctxptr := ctx.Ptr()
codeLength := uintptr(codeSet.CodeLength)
var code *encoder.Opcode
if (ctx.Option.Flag & encoder.HTMLEscapeOption) != 0 {
code = codeSet.EscapeKeyCode
Expand Down Expand Up @@ -204,8 +205,8 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
return nil, err
}

totalLength := uintptr(code.Length) + 3
nextTotalLength := uintptr(ifaceCodeSet.CodeLength) + 3
totalLength := codeLength + 4
nextTotalLength := uintptr(ifaceCodeSet.CodeLength) + 4

var c *encoder.Opcode
if (ctx.Option.Flag & encoder.HTMLEscapeOption) != 0 {
Expand All @@ -219,6 +220,8 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
ptrOffset += totalLength * uintptrSize
oldBaseIndent := ctx.BaseIndent
ctx.BaseIndent += code.Indent
oldCodeLength := codeLength
codeLength = uintptr(ifaceCodeSet.CodeLength)

newLen := offsetNum + totalLength + nextTotalLength
if curlen < newLen {
Expand All @@ -231,6 +234,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
store(ctxptr, end.Idx, oldOffset)
store(ctxptr, end.ElemIdx, uintptr(unsafe.Pointer(code.Next)))
storeIndent(ctxptr, end, uintptr(oldBaseIndent))
store(ctxptr, end.Offset, oldCodeLength)
code = c
recursiveLevel++
case encoder.OpInterfaceEnd:
Expand All @@ -239,6 +243,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
// restore ctxptr
offset := load(ctxptr, code.Idx)
restoreIndent(ctx, code, ctxptr)
codeLength = load(ctxptr, code.Offset)
ctx.SeenPtr = ctx.SeenPtr[:len(ctx.SeenPtr)-1]

codePtr := load(ctxptr, code.ElemIdx)
Expand Down
1 change: 1 addition & 0 deletions internal/encoder/opcode.go
Expand Up @@ -385,6 +385,7 @@ func copyToInterfaceOpcode(code *Opcode) *Opcode {
c.Idx += uintptrSize
c.ElemIdx = c.Idx + uintptrSize
c.Length = c.Idx + 2*uintptrSize
c.Offset = c.Idx + 3*uintptrSize
c.Op = OpInterfaceEnd
return copied
}
Expand Down
9 changes: 7 additions & 2 deletions internal/encoder/vm/vm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions internal/encoder/vm_color/vm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions internal/encoder/vm_color_indent/vm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions internal/encoder/vm_indent/vm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cfe4ac8

Please sign in to comment.