diff --git a/internal/cmd/generator/vm.go.tmpl b/internal/cmd/generator/vm.go.tmpl index cf1ff39..17374ba 100644 --- a/internal/cmd/generator/vm.go.tmpl +++ b/internal/cmd/generator/vm.go.tmpl @@ -224,7 +224,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b store(ctxptr, c.Idx, uintptr(iface.ptr)) store(ctxptr, end.Idx, oldOffset) store(ctxptr, end.ElemIdx, uintptr(unsafe.Pointer(code.Next))) - storeIndent(ctxptr, c, uintptr(oldBaseIndent)) + storeIndent(ctxptr, end, uintptr(oldBaseIndent)) code = c case encoder.OpInterfaceEnd: offset := load(ctxptr, code.Idx) @@ -533,7 +533,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b store(ctxptr, c.Idx, ptr) store(ctxptr, c.End.Next.Idx, oldOffset) store(ctxptr, c.End.Next.ElemIdx, uintptr(unsafe.Pointer(code.Next))) - storeIndent(ctxptr, c, uintptr(oldBaseIndent)) + storeIndent(ctxptr, c.End.Next, uintptr(oldBaseIndent)) code = c recursiveLevel++ case encoder.OpRecursiveEnd: diff --git a/internal/encoder/vm/util.go b/internal/encoder/vm/util.go index 7e358e2..c559447 100644 --- a/internal/encoder/vm/util.go +++ b/internal/encoder/vm/util.go @@ -123,39 +123,6 @@ func appendMapEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte return b } -func appendInterface(ctx *encoder.RuntimeContext, codeSet *encoder.OpcodeSet, _ *encoder.Opcode, b []byte, iface *emptyInterface, ptrOffset uintptr) ([]byte, error) { - ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(iface)) - ifaceCodeSet, err := encoder.CompileToGetCodeSet(uintptr(unsafe.Pointer(iface.typ))) - if err != nil { - return nil, err - } - - totalLength := uintptr(codeSet.CodeLength) - nextTotalLength := uintptr(ifaceCodeSet.CodeLength) - - curlen := uintptr(len(ctx.Ptrs)) - offsetNum := ptrOffset / uintptrSize - - newLen := offsetNum + totalLength + nextTotalLength - if curlen < newLen { - ctx.Ptrs = append(ctx.Ptrs, make([]uintptr, newLen-curlen)...) - } - oldPtrs := ctx.Ptrs - - newPtrs := ctx.Ptrs[(ptrOffset+totalLength*uintptrSize)/uintptrSize:] - newPtrs[0] = uintptr(iface.ptr) - - ctx.Ptrs = newPtrs - - bb, err := Run(ctx, b, ifaceCodeSet) - if err != nil { - return nil, err - } - - ctx.Ptrs = oldPtrs - return bb, nil -} - func appendMarshalJSON(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) { return encoder.AppendMarshalJSON(ctx, code, b, v) } diff --git a/internal/encoder/vm/vm.go b/internal/encoder/vm/vm.go index cf1ff39..17374ba 100644 --- a/internal/encoder/vm/vm.go +++ b/internal/encoder/vm/vm.go @@ -224,7 +224,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b store(ctxptr, c.Idx, uintptr(iface.ptr)) store(ctxptr, end.Idx, oldOffset) store(ctxptr, end.ElemIdx, uintptr(unsafe.Pointer(code.Next))) - storeIndent(ctxptr, c, uintptr(oldBaseIndent)) + storeIndent(ctxptr, end, uintptr(oldBaseIndent)) code = c case encoder.OpInterfaceEnd: offset := load(ctxptr, code.Idx) @@ -533,7 +533,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b store(ctxptr, c.Idx, ptr) store(ctxptr, c.End.Next.Idx, oldOffset) store(ctxptr, c.End.Next.ElemIdx, uintptr(unsafe.Pointer(code.Next))) - storeIndent(ctxptr, c, uintptr(oldBaseIndent)) + storeIndent(ctxptr, c.End.Next, uintptr(oldBaseIndent)) code = c recursiveLevel++ case encoder.OpRecursiveEnd: diff --git a/internal/encoder/vm_color/util.go b/internal/encoder/vm_color/util.go index 58dd9eb..516536d 100644 --- a/internal/encoder/vm_color/util.go +++ b/internal/encoder/vm_color/util.go @@ -176,39 +176,6 @@ func appendMapEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte return b } -func appendInterface(ctx *encoder.RuntimeContext, codeSet *encoder.OpcodeSet, _ *encoder.Opcode, b []byte, iface *emptyInterface, ptrOffset uintptr) ([]byte, error) { - ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(iface)) - ifaceCodeSet, err := encoder.CompileToGetCodeSet(uintptr(unsafe.Pointer(iface.typ))) - if err != nil { - return nil, err - } - - totalLength := uintptr(codeSet.CodeLength) - nextTotalLength := uintptr(ifaceCodeSet.CodeLength) - - curlen := uintptr(len(ctx.Ptrs)) - offsetNum := ptrOffset / uintptrSize - - newLen := offsetNum + totalLength + nextTotalLength - if curlen < newLen { - ctx.Ptrs = append(ctx.Ptrs, make([]uintptr, newLen-curlen)...) - } - oldPtrs := ctx.Ptrs - - newPtrs := ctx.Ptrs[(ptrOffset+totalLength*uintptrSize)/uintptrSize:] - newPtrs[0] = uintptr(iface.ptr) - - ctx.Ptrs = newPtrs - - bb, err := Run(ctx, b, ifaceCodeSet) - if err != nil { - return nil, err - } - - ctx.Ptrs = oldPtrs - return bb, nil -} - func appendMarshalJSON(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) { return encoder.AppendMarshalJSON(ctx, code, b, v) } diff --git a/internal/encoder/vm_color/vm.go b/internal/encoder/vm_color/vm.go index d8b01b4..33bea71 100644 --- a/internal/encoder/vm_color/vm.go +++ b/internal/encoder/vm_color/vm.go @@ -224,7 +224,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b store(ctxptr, c.Idx, uintptr(iface.ptr)) store(ctxptr, end.Idx, oldOffset) store(ctxptr, end.ElemIdx, uintptr(unsafe.Pointer(code.Next))) - storeIndent(ctxptr, c, uintptr(oldBaseIndent)) + storeIndent(ctxptr, end, uintptr(oldBaseIndent)) code = c case encoder.OpInterfaceEnd: offset := load(ctxptr, code.Idx) @@ -533,7 +533,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b store(ctxptr, c.Idx, ptr) store(ctxptr, c.End.Next.Idx, oldOffset) store(ctxptr, c.End.Next.ElemIdx, uintptr(unsafe.Pointer(code.Next))) - storeIndent(ctxptr, c, uintptr(oldBaseIndent)) + storeIndent(ctxptr, c.End.Next, uintptr(oldBaseIndent)) code = c recursiveLevel++ case encoder.OpRecursiveEnd: diff --git a/internal/encoder/vm_color_indent/util.go b/internal/encoder/vm_color_indent/util.go index 58f88e5..9f98781 100644 --- a/internal/encoder/vm_color_indent/util.go +++ b/internal/encoder/vm_color_indent/util.go @@ -163,43 +163,6 @@ func appendColon(_ *encoder.RuntimeContext, b []byte) []byte { return append(b, ':', ' ') } -func appendInterface(ctx *encoder.RuntimeContext, codeSet *encoder.OpcodeSet, code *encoder.Opcode, b []byte, iface *emptyInterface, ptrOffset uintptr) ([]byte, error) { - ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(iface)) - ifaceCodeSet, err := encoder.CompileToGetCodeSet(uintptr(unsafe.Pointer(iface.typ))) - if err != nil { - return nil, err - } - - totalLength := uintptr(codeSet.CodeLength) - nextTotalLength := uintptr(ifaceCodeSet.CodeLength) - - curlen := uintptr(len(ctx.Ptrs)) - offsetNum := ptrOffset / uintptrSize - - newLen := offsetNum + totalLength + nextTotalLength - if curlen < newLen { - ctx.Ptrs = append(ctx.Ptrs, make([]uintptr, newLen-curlen)...) - } - oldPtrs := ctx.Ptrs - - newPtrs := ctx.Ptrs[(ptrOffset+totalLength*uintptrSize)/uintptrSize:] - newPtrs[0] = uintptr(iface.ptr) - - ctx.Ptrs = newPtrs - - oldBaseIndent := ctx.BaseIndent - ctx.BaseIndent = code.Indent - bb, err := Run(ctx, b, ifaceCodeSet) - if err != nil { - return nil, err - } - ctx.BaseIndent = oldBaseIndent - - ctx.Ptrs = oldPtrs - - return bb, nil -} - func appendMapKeyValue(ctx *encoder.RuntimeContext, code *encoder.Opcode, b, key, value []byte) []byte { b = appendIndent(ctx, b, code.Indent+1) b = append(b, key...) @@ -292,7 +255,7 @@ func restoreIndent(ctx *encoder.RuntimeContext, code *encoder.Opcode, ctxptr uin } func storeIndent(ctxptr uintptr, code *encoder.Opcode, indent uintptr) { - store(ctxptr, code.End.Next.Length, indent) + store(ctxptr, code.Length, indent) } func appendArrayElemIndent(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { diff --git a/internal/encoder/vm_color_indent/vm.go b/internal/encoder/vm_color_indent/vm.go index f6260cc..523cb45 100644 --- a/internal/encoder/vm_color_indent/vm.go +++ b/internal/encoder/vm_color_indent/vm.go @@ -224,7 +224,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b store(ctxptr, c.Idx, uintptr(iface.ptr)) store(ctxptr, end.Idx, oldOffset) store(ctxptr, end.ElemIdx, uintptr(unsafe.Pointer(code.Next))) - storeIndent(ctxptr, c, uintptr(oldBaseIndent)) + storeIndent(ctxptr, end, uintptr(oldBaseIndent)) code = c case encoder.OpInterfaceEnd: offset := load(ctxptr, code.Idx) @@ -533,7 +533,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b store(ctxptr, c.Idx, ptr) store(ctxptr, c.End.Next.Idx, oldOffset) store(ctxptr, c.End.Next.ElemIdx, uintptr(unsafe.Pointer(code.Next))) - storeIndent(ctxptr, c, uintptr(oldBaseIndent)) + storeIndent(ctxptr, c.End.Next, uintptr(oldBaseIndent)) code = c recursiveLevel++ case encoder.OpRecursiveEnd: diff --git a/internal/encoder/vm_indent/util.go b/internal/encoder/vm_indent/util.go index 2e057aa..5f5d8a5 100644 --- a/internal/encoder/vm_indent/util.go +++ b/internal/encoder/vm_indent/util.go @@ -111,43 +111,6 @@ func appendColon(_ *encoder.RuntimeContext, b []byte) []byte { return append(b, ':', ' ') } -func appendInterface(ctx *encoder.RuntimeContext, codeSet *encoder.OpcodeSet, code *encoder.Opcode, b []byte, iface *emptyInterface, ptrOffset uintptr) ([]byte, error) { - ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(iface)) - ifaceCodeSet, err := encoder.CompileToGetCodeSet(uintptr(unsafe.Pointer(iface.typ))) - if err != nil { - return nil, err - } - - totalLength := uintptr(codeSet.CodeLength) - nextTotalLength := uintptr(ifaceCodeSet.CodeLength) - - curlen := uintptr(len(ctx.Ptrs)) - offsetNum := ptrOffset / uintptrSize - - newLen := offsetNum + totalLength + nextTotalLength - if curlen < newLen { - ctx.Ptrs = append(ctx.Ptrs, make([]uintptr, newLen-curlen)...) - } - oldPtrs := ctx.Ptrs - - newPtrs := ctx.Ptrs[(ptrOffset+totalLength*uintptrSize)/uintptrSize:] - newPtrs[0] = uintptr(iface.ptr) - - ctx.Ptrs = newPtrs - - oldBaseIndent := ctx.BaseIndent - ctx.BaseIndent = code.Indent - bb, err := Run(ctx, b, ifaceCodeSet) - if err != nil { - return nil, err - } - ctx.BaseIndent = oldBaseIndent - - ctx.Ptrs = oldPtrs - - return bb, nil -} - func appendMapKeyValue(ctx *encoder.RuntimeContext, code *encoder.Opcode, b, key, value []byte) []byte { b = appendIndent(ctx, b, code.Indent+1) b = append(b, key...) @@ -229,7 +192,7 @@ func restoreIndent(ctx *encoder.RuntimeContext, code *encoder.Opcode, ctxptr uin } func storeIndent(ctxptr uintptr, code *encoder.Opcode, indent uintptr) { - store(ctxptr, code.End.Next.Length, indent) + store(ctxptr, code.Length, indent) } func appendArrayElemIndent(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { diff --git a/internal/encoder/vm_indent/vm.go b/internal/encoder/vm_indent/vm.go index 55bc116..2d2dcb5 100644 --- a/internal/encoder/vm_indent/vm.go +++ b/internal/encoder/vm_indent/vm.go @@ -224,7 +224,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b store(ctxptr, c.Idx, uintptr(iface.ptr)) store(ctxptr, end.Idx, oldOffset) store(ctxptr, end.ElemIdx, uintptr(unsafe.Pointer(code.Next))) - storeIndent(ctxptr, c, uintptr(oldBaseIndent)) + storeIndent(ctxptr, end, uintptr(oldBaseIndent)) code = c case encoder.OpInterfaceEnd: offset := load(ctxptr, code.Idx) @@ -533,7 +533,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b store(ctxptr, c.Idx, ptr) store(ctxptr, c.End.Next.Idx, oldOffset) store(ctxptr, c.End.Next.ElemIdx, uintptr(unsafe.Pointer(code.Next))) - storeIndent(ctxptr, c, uintptr(oldBaseIndent)) + storeIndent(ctxptr, c.End.Next, uintptr(oldBaseIndent)) code = c recursiveLevel++ case encoder.OpRecursiveEnd: