From 5c527ab46388f481cb5af1ee4c5b5d807c6da39d Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Mon, 30 Aug 2021 13:02:43 +0900 Subject: [PATCH 1/3] Add test case --- encode_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/encode_test.go b/encode_test.go index 83075c5..358e51f 100644 --- a/encode_test.go +++ b/encode_test.go @@ -2108,3 +2108,29 @@ func TestEmbeddedNotFirstField(t *testing.T) { t.Fatalf("failed to encode embedded structure. expected = %q but got %q", expected, got) } } + +type implementedMethodIface interface { + M() +} + +type implementedIfaceType struct { + A int + B string +} + +func (implementedIfaceType) M() {} + +func TestImplementedMethodInterfaceType(t *testing.T) { + data := []implementedIfaceType{implementedIfaceType{}} + expected, err := stdjson.Marshal(data) + if err != nil { + t.Fatal(err) + } + got, err := json.Marshal(data) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(expected, got) { + t.Fatalf("failed to encode implemented method interface type. expected:[%q] but got:[%q]", expected, got) + } +} From 92d8dcd13bf09576c41fd304e63a7f3cb924485d Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Mon, 30 Aug 2021 13:03:14 +0900 Subject: [PATCH 2/3] Fix encoding of not empty interface type --- internal/cmd/generator/vm.go.tmpl | 24 ++++++++++++++++++------ internal/encoder/opcode.go | 24 +++++++++++++++--------- internal/encoder/vm/util.go | 9 +++++++++ internal/encoder/vm/vm.go | 24 ++++++++++++++++++------ internal/encoder/vm_color/util.go | 9 +++++++++ internal/encoder/vm_color/vm.go | 24 ++++++++++++++++++------ internal/encoder/vm_color_indent/util.go | 9 +++++++++ internal/encoder/vm_color_indent/vm.go | 24 ++++++++++++++++++------ internal/encoder/vm_indent/util.go | 9 +++++++++ internal/encoder/vm_indent/vm.go | 24 ++++++++++++++++++------ 10 files changed, 141 insertions(+), 39 deletions(-) diff --git a/internal/cmd/generator/vm.go.tmpl b/internal/cmd/generator/vm.go.tmpl index 5c62354..ccbbfdd 100644 --- a/internal/cmd/generator/vm.go.tmpl +++ b/internal/cmd/generator/vm.go.tmpl @@ -7,6 +7,7 @@ import ( "unsafe" "github.com/goccy/go-json/internal/encoder" + "github.com/goccy/go-json/internal/runtime" ) func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]byte, error) { @@ -185,16 +186,27 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b } } ctx.SeenPtr = append(ctx.SeenPtr, p) - iface := (*emptyInterface)(ptrToUnsafePtr(p)) - if iface.ptr == nil { + var ( + typ *runtime.Type + ifacePtr unsafe.Pointer + ) + if code.Flags&encoder.NonEmptyInterfaceFlags != 0 { + iface := (*nonEmptyInterface)(ptrToUnsafePtr(p)) + ifacePtr = iface.ptr + typ = iface.itab.typ + } else { + iface := (*emptyInterface)(ptrToUnsafePtr(p)) + ifacePtr = iface.ptr + typ = iface.typ + } + if ifacePtr == nil { b = appendNull(ctx, b) b = appendComma(ctx, b) code = code.Next break } - - ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(iface)) - ifaceCodeSet, err := encoder.CompileToGetCodeSet(uintptr(unsafe.Pointer(iface.typ))) + ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(p)) + ifaceCodeSet, err := encoder.CompileToGetCodeSet(uintptr(unsafe.Pointer(typ))) if err != nil { return nil, err } @@ -223,7 +235,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b ctxptr = ctx.Ptr() + ptrOffset // assign new ctxptr end := ifaceCodeSet.EndCode - store(ctxptr, c.Idx, uintptr(iface.ptr)) + store(ctxptr, c.Idx, uintptr(ifacePtr)) store(ctxptr, end.Idx, oldOffset) store(ctxptr, end.ElemIdx, uintptr(unsafe.Pointer(code.Next))) storeIndent(ctxptr, end, uintptr(oldBaseIndent)) diff --git a/internal/encoder/opcode.go b/internal/encoder/opcode.go index c23a90b..7c50eef 100644 --- a/internal/encoder/opcode.go +++ b/internal/encoder/opcode.go @@ -13,15 +13,16 @@ const uintptrSize = 4 << (^uintptr(0) >> 63) type OpFlags uint16 const ( - AnonymousHeadFlags OpFlags = 1 << 0 - AnonymousKeyFlags OpFlags = 1 << 1 - IndirectFlags OpFlags = 1 << 2 - IsTaggedKeyFlags OpFlags = 1 << 3 - NilCheckFlags OpFlags = 1 << 4 - AddrForMarshalerFlags OpFlags = 1 << 5 - IsNextOpPtrTypeFlags OpFlags = 1 << 6 - IsNilableTypeFlags OpFlags = 1 << 7 - MarshalerContextFlags OpFlags = 1 << 8 + AnonymousHeadFlags OpFlags = 1 << 0 + AnonymousKeyFlags OpFlags = 1 << 1 + IndirectFlags OpFlags = 1 << 2 + IsTaggedKeyFlags OpFlags = 1 << 3 + NilCheckFlags OpFlags = 1 << 4 + AddrForMarshalerFlags OpFlags = 1 << 5 + IsNextOpPtrTypeFlags OpFlags = 1 << 6 + IsNilableTypeFlags OpFlags = 1 << 7 + MarshalerContextFlags OpFlags = 1 << 8 + NonEmptyInterfaceFlags OpFlags = 1 << 9 ) type Opcode struct { @@ -743,6 +744,10 @@ func newMapEndCode(ctx *compileContext, head *Opcode) *Opcode { } func newInterfaceCode(ctx *compileContext) *Opcode { + var flag OpFlags + if ctx.typ.NumMethod() > 0 { + flag |= NonEmptyInterfaceFlags + } return &Opcode{ Op: OpInterface, Idx: opcodeOffset(ctx.ptrIndex), @@ -750,6 +755,7 @@ func newInterfaceCode(ctx *compileContext) *Opcode { Type: ctx.typ, DisplayIdx: ctx.opcodeIndex, Indent: ctx.indent, + Flags: flag, } } diff --git a/internal/encoder/vm/util.go b/internal/encoder/vm/util.go index c559447..f06f9c8 100644 --- a/internal/encoder/vm/util.go +++ b/internal/encoder/vm/util.go @@ -33,6 +33,15 @@ type emptyInterface struct { ptr unsafe.Pointer } +type nonEmptyInterface struct { + itab *struct { + ityp *runtime.Type // static interface type + typ *runtime.Type // dynamic concrete type + // unused fields... + } + ptr unsafe.Pointer +} + func errUnimplementedOp(op encoder.OpType) error { return fmt.Errorf("encoder: opcode %s has not been implemented", op) } diff --git a/internal/encoder/vm/vm.go b/internal/encoder/vm/vm.go index 5c62354..ccbbfdd 100644 --- a/internal/encoder/vm/vm.go +++ b/internal/encoder/vm/vm.go @@ -7,6 +7,7 @@ import ( "unsafe" "github.com/goccy/go-json/internal/encoder" + "github.com/goccy/go-json/internal/runtime" ) func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]byte, error) { @@ -185,16 +186,27 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b } } ctx.SeenPtr = append(ctx.SeenPtr, p) - iface := (*emptyInterface)(ptrToUnsafePtr(p)) - if iface.ptr == nil { + var ( + typ *runtime.Type + ifacePtr unsafe.Pointer + ) + if code.Flags&encoder.NonEmptyInterfaceFlags != 0 { + iface := (*nonEmptyInterface)(ptrToUnsafePtr(p)) + ifacePtr = iface.ptr + typ = iface.itab.typ + } else { + iface := (*emptyInterface)(ptrToUnsafePtr(p)) + ifacePtr = iface.ptr + typ = iface.typ + } + if ifacePtr == nil { b = appendNull(ctx, b) b = appendComma(ctx, b) code = code.Next break } - - ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(iface)) - ifaceCodeSet, err := encoder.CompileToGetCodeSet(uintptr(unsafe.Pointer(iface.typ))) + ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(p)) + ifaceCodeSet, err := encoder.CompileToGetCodeSet(uintptr(unsafe.Pointer(typ))) if err != nil { return nil, err } @@ -223,7 +235,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b ctxptr = ctx.Ptr() + ptrOffset // assign new ctxptr end := ifaceCodeSet.EndCode - store(ctxptr, c.Idx, uintptr(iface.ptr)) + store(ctxptr, c.Idx, uintptr(ifacePtr)) store(ctxptr, end.Idx, oldOffset) store(ctxptr, end.ElemIdx, uintptr(unsafe.Pointer(code.Next))) storeIndent(ctxptr, end, uintptr(oldBaseIndent)) diff --git a/internal/encoder/vm_color/util.go b/internal/encoder/vm_color/util.go index 516536d..710087f 100644 --- a/internal/encoder/vm_color/util.go +++ b/internal/encoder/vm_color/util.go @@ -26,6 +26,15 @@ type emptyInterface struct { ptr unsafe.Pointer } +type nonEmptyInterface struct { + itab *struct { + ityp *runtime.Type // static interface type + typ *runtime.Type // dynamic concrete type + // unused fields... + } + ptr unsafe.Pointer +} + func errUnimplementedOp(op encoder.OpType) error { return fmt.Errorf("encoder: opcode %s has not been implemented", op) } diff --git a/internal/encoder/vm_color/vm.go b/internal/encoder/vm_color/vm.go index 71aa3f7..b7f6560 100644 --- a/internal/encoder/vm_color/vm.go +++ b/internal/encoder/vm_color/vm.go @@ -7,6 +7,7 @@ import ( "unsafe" "github.com/goccy/go-json/internal/encoder" + "github.com/goccy/go-json/internal/runtime" ) func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]byte, error) { @@ -185,16 +186,27 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b } } ctx.SeenPtr = append(ctx.SeenPtr, p) - iface := (*emptyInterface)(ptrToUnsafePtr(p)) - if iface.ptr == nil { + var ( + typ *runtime.Type + ifacePtr unsafe.Pointer + ) + if code.Flags&encoder.NonEmptyInterfaceFlags != 0 { + iface := (*nonEmptyInterface)(ptrToUnsafePtr(p)) + ifacePtr = iface.ptr + typ = iface.itab.typ + } else { + iface := (*emptyInterface)(ptrToUnsafePtr(p)) + ifacePtr = iface.ptr + typ = iface.typ + } + if ifacePtr == nil { b = appendNull(ctx, b) b = appendComma(ctx, b) code = code.Next break } - - ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(iface)) - ifaceCodeSet, err := encoder.CompileToGetCodeSet(uintptr(unsafe.Pointer(iface.typ))) + ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(p)) + ifaceCodeSet, err := encoder.CompileToGetCodeSet(uintptr(unsafe.Pointer(typ))) if err != nil { return nil, err } @@ -223,7 +235,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b ctxptr = ctx.Ptr() + ptrOffset // assign new ctxptr end := ifaceCodeSet.EndCode - store(ctxptr, c.Idx, uintptr(iface.ptr)) + store(ctxptr, c.Idx, uintptr(ifacePtr)) store(ctxptr, end.Idx, oldOffset) store(ctxptr, end.ElemIdx, uintptr(unsafe.Pointer(code.Next))) storeIndent(ctxptr, end, uintptr(oldBaseIndent)) diff --git a/internal/encoder/vm_color_indent/util.go b/internal/encoder/vm_color_indent/util.go index 9f98781..1399a25 100644 --- a/internal/encoder/vm_color_indent/util.go +++ b/internal/encoder/vm_color_indent/util.go @@ -28,6 +28,15 @@ type emptyInterface struct { ptr unsafe.Pointer } +type nonEmptyInterface struct { + itab *struct { + ityp *runtime.Type // static interface type + typ *runtime.Type // dynamic concrete type + // unused fields... + } + ptr unsafe.Pointer +} + func errUnimplementedOp(op encoder.OpType) error { return fmt.Errorf("encoder (indent): opcode %s has not been implemented", op) } diff --git a/internal/encoder/vm_color_indent/vm.go b/internal/encoder/vm_color_indent/vm.go index 60d0083..9e17654 100644 --- a/internal/encoder/vm_color_indent/vm.go +++ b/internal/encoder/vm_color_indent/vm.go @@ -7,6 +7,7 @@ import ( "unsafe" "github.com/goccy/go-json/internal/encoder" + "github.com/goccy/go-json/internal/runtime" ) func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]byte, error) { @@ -185,16 +186,27 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b } } ctx.SeenPtr = append(ctx.SeenPtr, p) - iface := (*emptyInterface)(ptrToUnsafePtr(p)) - if iface.ptr == nil { + var ( + typ *runtime.Type + ifacePtr unsafe.Pointer + ) + if code.Flags&encoder.NonEmptyInterfaceFlags != 0 { + iface := (*nonEmptyInterface)(ptrToUnsafePtr(p)) + ifacePtr = iface.ptr + typ = iface.itab.typ + } else { + iface := (*emptyInterface)(ptrToUnsafePtr(p)) + ifacePtr = iface.ptr + typ = iface.typ + } + if ifacePtr == nil { b = appendNull(ctx, b) b = appendComma(ctx, b) code = code.Next break } - - ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(iface)) - ifaceCodeSet, err := encoder.CompileToGetCodeSet(uintptr(unsafe.Pointer(iface.typ))) + ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(p)) + ifaceCodeSet, err := encoder.CompileToGetCodeSet(uintptr(unsafe.Pointer(typ))) if err != nil { return nil, err } @@ -223,7 +235,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b ctxptr = ctx.Ptr() + ptrOffset // assign new ctxptr end := ifaceCodeSet.EndCode - store(ctxptr, c.Idx, uintptr(iface.ptr)) + store(ctxptr, c.Idx, uintptr(ifacePtr)) store(ctxptr, end.Idx, oldOffset) store(ctxptr, end.ElemIdx, uintptr(unsafe.Pointer(code.Next))) storeIndent(ctxptr, end, uintptr(oldBaseIndent)) diff --git a/internal/encoder/vm_indent/util.go b/internal/encoder/vm_indent/util.go index 5f5d8a5..2e3c35f 100644 --- a/internal/encoder/vm_indent/util.go +++ b/internal/encoder/vm_indent/util.go @@ -35,6 +35,15 @@ type emptyInterface struct { ptr unsafe.Pointer } +type nonEmptyInterface struct { + itab *struct { + ityp *runtime.Type // static interface type + typ *runtime.Type // dynamic concrete type + // unused fields... + } + ptr unsafe.Pointer +} + func errUnimplementedOp(op encoder.OpType) error { return fmt.Errorf("encoder (indent): opcode %s has not been implemented", op) } diff --git a/internal/encoder/vm_indent/vm.go b/internal/encoder/vm_indent/vm.go index e817484..ac1285a 100644 --- a/internal/encoder/vm_indent/vm.go +++ b/internal/encoder/vm_indent/vm.go @@ -7,6 +7,7 @@ import ( "unsafe" "github.com/goccy/go-json/internal/encoder" + "github.com/goccy/go-json/internal/runtime" ) func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]byte, error) { @@ -185,16 +186,27 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b } } ctx.SeenPtr = append(ctx.SeenPtr, p) - iface := (*emptyInterface)(ptrToUnsafePtr(p)) - if iface.ptr == nil { + var ( + typ *runtime.Type + ifacePtr unsafe.Pointer + ) + if code.Flags&encoder.NonEmptyInterfaceFlags != 0 { + iface := (*nonEmptyInterface)(ptrToUnsafePtr(p)) + ifacePtr = iface.ptr + typ = iface.itab.typ + } else { + iface := (*emptyInterface)(ptrToUnsafePtr(p)) + ifacePtr = iface.ptr + typ = iface.typ + } + if ifacePtr == nil { b = appendNull(ctx, b) b = appendComma(ctx, b) code = code.Next break } - - ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(iface)) - ifaceCodeSet, err := encoder.CompileToGetCodeSet(uintptr(unsafe.Pointer(iface.typ))) + ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(p)) + ifaceCodeSet, err := encoder.CompileToGetCodeSet(uintptr(unsafe.Pointer(typ))) if err != nil { return nil, err } @@ -223,7 +235,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b ctxptr = ctx.Ptr() + ptrOffset // assign new ctxptr end := ifaceCodeSet.EndCode - store(ctxptr, c.Idx, uintptr(iface.ptr)) + store(ctxptr, c.Idx, uintptr(ifacePtr)) store(ctxptr, end.Idx, oldOffset) store(ctxptr, end.ElemIdx, uintptr(unsafe.Pointer(code.Next))) storeIndent(ctxptr, end, uintptr(oldBaseIndent)) From 97c3cf6c55286ae5a34f8c04ed77bac72d399658 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Mon, 30 Aug 2021 13:11:44 +0900 Subject: [PATCH 3/3] Fix test --- internal/cmd/generator/vm.go.tmpl | 7 ++++--- internal/encoder/vm/vm.go | 7 ++++--- internal/encoder/vm_color/vm.go | 7 ++++--- internal/encoder/vm_color_indent/vm.go | 7 ++++--- internal/encoder/vm_indent/vm.go | 7 ++++--- 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/internal/cmd/generator/vm.go.tmpl b/internal/cmd/generator/vm.go.tmpl index ccbbfdd..5857249 100644 --- a/internal/cmd/generator/vm.go.tmpl +++ b/internal/cmd/generator/vm.go.tmpl @@ -190,12 +190,13 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b typ *runtime.Type ifacePtr unsafe.Pointer ) + up := ptrToUnsafePtr(p) if code.Flags&encoder.NonEmptyInterfaceFlags != 0 { - iface := (*nonEmptyInterface)(ptrToUnsafePtr(p)) + iface := (*nonEmptyInterface)(up) ifacePtr = iface.ptr typ = iface.itab.typ } else { - iface := (*emptyInterface)(ptrToUnsafePtr(p)) + iface := (*emptyInterface)(up) ifacePtr = iface.ptr typ = iface.typ } @@ -205,7 +206,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b code = code.Next break } - ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(p)) + ctx.KeepRefs = append(ctx.KeepRefs, up) ifaceCodeSet, err := encoder.CompileToGetCodeSet(uintptr(unsafe.Pointer(typ))) if err != nil { return nil, err diff --git a/internal/encoder/vm/vm.go b/internal/encoder/vm/vm.go index ccbbfdd..5857249 100644 --- a/internal/encoder/vm/vm.go +++ b/internal/encoder/vm/vm.go @@ -190,12 +190,13 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b typ *runtime.Type ifacePtr unsafe.Pointer ) + up := ptrToUnsafePtr(p) if code.Flags&encoder.NonEmptyInterfaceFlags != 0 { - iface := (*nonEmptyInterface)(ptrToUnsafePtr(p)) + iface := (*nonEmptyInterface)(up) ifacePtr = iface.ptr typ = iface.itab.typ } else { - iface := (*emptyInterface)(ptrToUnsafePtr(p)) + iface := (*emptyInterface)(up) ifacePtr = iface.ptr typ = iface.typ } @@ -205,7 +206,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b code = code.Next break } - ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(p)) + ctx.KeepRefs = append(ctx.KeepRefs, up) ifaceCodeSet, err := encoder.CompileToGetCodeSet(uintptr(unsafe.Pointer(typ))) if err != nil { return nil, err diff --git a/internal/encoder/vm_color/vm.go b/internal/encoder/vm_color/vm.go index b7f6560..73af884 100644 --- a/internal/encoder/vm_color/vm.go +++ b/internal/encoder/vm_color/vm.go @@ -190,12 +190,13 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b typ *runtime.Type ifacePtr unsafe.Pointer ) + up := ptrToUnsafePtr(p) if code.Flags&encoder.NonEmptyInterfaceFlags != 0 { - iface := (*nonEmptyInterface)(ptrToUnsafePtr(p)) + iface := (*nonEmptyInterface)(up) ifacePtr = iface.ptr typ = iface.itab.typ } else { - iface := (*emptyInterface)(ptrToUnsafePtr(p)) + iface := (*emptyInterface)(up) ifacePtr = iface.ptr typ = iface.typ } @@ -205,7 +206,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b code = code.Next break } - ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(p)) + ctx.KeepRefs = append(ctx.KeepRefs, up) ifaceCodeSet, err := encoder.CompileToGetCodeSet(uintptr(unsafe.Pointer(typ))) if err != nil { return nil, err diff --git a/internal/encoder/vm_color_indent/vm.go b/internal/encoder/vm_color_indent/vm.go index 9e17654..7b7844e 100644 --- a/internal/encoder/vm_color_indent/vm.go +++ b/internal/encoder/vm_color_indent/vm.go @@ -190,12 +190,13 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b typ *runtime.Type ifacePtr unsafe.Pointer ) + up := ptrToUnsafePtr(p) if code.Flags&encoder.NonEmptyInterfaceFlags != 0 { - iface := (*nonEmptyInterface)(ptrToUnsafePtr(p)) + iface := (*nonEmptyInterface)(up) ifacePtr = iface.ptr typ = iface.itab.typ } else { - iface := (*emptyInterface)(ptrToUnsafePtr(p)) + iface := (*emptyInterface)(up) ifacePtr = iface.ptr typ = iface.typ } @@ -205,7 +206,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b code = code.Next break } - ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(p)) + ctx.KeepRefs = append(ctx.KeepRefs, up) ifaceCodeSet, err := encoder.CompileToGetCodeSet(uintptr(unsafe.Pointer(typ))) if err != nil { return nil, err diff --git a/internal/encoder/vm_indent/vm.go b/internal/encoder/vm_indent/vm.go index ac1285a..6e05155 100644 --- a/internal/encoder/vm_indent/vm.go +++ b/internal/encoder/vm_indent/vm.go @@ -190,12 +190,13 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b typ *runtime.Type ifacePtr unsafe.Pointer ) + up := ptrToUnsafePtr(p) if code.Flags&encoder.NonEmptyInterfaceFlags != 0 { - iface := (*nonEmptyInterface)(ptrToUnsafePtr(p)) + iface := (*nonEmptyInterface)(up) ifacePtr = iface.ptr typ = iface.itab.typ } else { - iface := (*emptyInterface)(ptrToUnsafePtr(p)) + iface := (*emptyInterface)(up) ifacePtr = iface.ptr typ = iface.typ } @@ -205,7 +206,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b code = code.Next break } - ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(p)) + ctx.KeepRefs = append(ctx.KeepRefs, up) ifaceCodeSet, err := encoder.CompileToGetCodeSet(uintptr(unsafe.Pointer(typ))) if err != nil { return nil, err