From fc968c75ee0c3646a02e8ea37b3659f54eb40bc8 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Mon, 17 May 2021 21:08:45 +0900 Subject: [PATCH 1/3] Refactor encoding of marshaler and structEnd --- internal/encoder/encoder.go | 8 +- internal/encoder/vm/util.go | 14 +- internal/encoder/vm/vm.go | 220 ++++++++++----------- internal/encoder/vm_debug/util.go | 14 +- internal/encoder/vm_debug/vm.go | 220 ++++++++++----------- internal/encoder/vm_escaped/util.go | 14 +- internal/encoder/vm_escaped/vm.go | 220 ++++++++++----------- internal/encoder/vm_escaped_indent/util.go | 10 +- internal/encoder/vm_escaped_indent/vm.go | 220 ++++++++++----------- internal/encoder/vm_indent/util.go | 10 +- internal/encoder/vm_indent/vm.go | 218 ++++++++++---------- 11 files changed, 599 insertions(+), 569 deletions(-) diff --git a/internal/encoder/encoder.go b/internal/encoder/encoder.go index b0cab28..ef6b910 100644 --- a/internal/encoder/encoder.go +++ b/internal/encoder/encoder.go @@ -390,7 +390,7 @@ func AppendMarshalJSON(ctx *RuntimeContext, code *Opcode, b []byte, v interface{ return compactedBuf, nil } -func AppendMarshalJSONIndent(ctx *RuntimeContext, code *Opcode, b []byte, v interface{}, indent int, escape bool) ([]byte, error) { +func AppendMarshalJSONIndent(ctx *RuntimeContext, code *Opcode, b []byte, v interface{}, escape bool) ([]byte, error) { rv := reflect.ValueOf(v) // convert by dynamic interface type if code.AddrForMarshaler { if rv.CanAddr() { @@ -415,7 +415,7 @@ func AppendMarshalJSONIndent(ctx *RuntimeContext, code *Opcode, b []byte, v inte indentedBuf, err := doIndent( b, marshalBuf, - string(ctx.Prefix)+strings.Repeat(string(ctx.IndentStr), ctx.BaseIndent+indent), + string(ctx.Prefix)+strings.Repeat(string(ctx.IndentStr), ctx.BaseIndent+code.Indent), string(ctx.IndentStr), escape, ) @@ -494,10 +494,10 @@ func AppendStructEnd(b []byte) []byte { return append(b, '}', ',') } -func AppendStructEndIndent(ctx *RuntimeContext, b []byte, indent int) []byte { +func AppendStructEndIndent(ctx *RuntimeContext, code *Opcode, b []byte) []byte { b = append(b, '\n') b = append(b, ctx.Prefix...) - indentNum := ctx.BaseIndent + indent + indentNum := ctx.BaseIndent + code.Indent - 1 for i := 0; i < indentNum; i++ { b = append(b, ctx.IndentStr...) } diff --git a/internal/encoder/vm/util.go b/internal/encoder/vm/util.go index f8c9c07..9faccd7 100644 --- a/internal/encoder/vm/util.go +++ b/internal/encoder/vm/util.go @@ -76,6 +76,14 @@ func appendComma(b []byte) []byte { return append(b, ',') } +func appendMarshalJSON(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) { + return encoder.AppendMarshalJSON(ctx, code, b, v, false) +} + +func appendMarshalText(code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) { + return encoder.AppendMarshalText(code, b, v, false) +} + func appendStructKey(_ *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { return append(b, code.Key...) } @@ -84,15 +92,15 @@ func appendStructHead(b []byte) []byte { return append(b, '{') } -func appendStructEnd(b []byte) []byte { +func appendStructEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { return append(b, '}', ',') } -func appendStructEndSkipLast(b []byte) []byte { +func appendStructEndSkipLast(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { last := len(b) - 1 if b[last] == ',' { b[last] = '}' return appendComma(b) } - return appendStructEnd(b) + return appendStructEnd(ctx, code, b) } diff --git a/internal/encoder/vm/vm.go b/internal/encoder/vm/vm.go index 0214598..beab6ee 100644 --- a/internal/encoder/vm/vm.go +++ b/internal/encoder/vm/vm.go @@ -26,8 +26,6 @@ var ( appendString = encoder.AppendString appendByteSlice = encoder.AppendByteSlice appendNumber = encoder.AppendNumber - appendMarshalJSON = encoder.AppendMarshalJSON - appendMarshalText = encoder.AppendMarshalText errUnsupportedValue = encoder.ErrUnsupportedValue errUnsupportedFloat = encoder.ErrUnsupportedFloat mapiterinit = encoder.MapIterInit @@ -273,7 +271,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if code.IsNilableType && code.Indirect { p = ptrToPtr(p) } - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -300,7 +298,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if code.IsNilableType && code.Indirect { p = ptrToPtr(p) } - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3147,7 +3145,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 && code.Nilcheck { b = appendNull(b) } else { - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3192,7 +3190,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.NextField } else { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalJSON(ctx, code, b, iface, false) + bb, err := appendMarshalJSON(ctx, code, b, iface) if err != nil { return nil, err } @@ -3232,7 +3230,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 { b = appendNull(b) } else { - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3272,7 +3270,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.NextField } else { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3316,7 +3314,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 && code.Nilcheck { b = appendNull(b) } else { - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3360,7 +3358,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.NextField } else { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3400,7 +3398,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 { b = appendNull(b) } else { - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3440,7 +3438,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.NextField } else { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4110,7 +4108,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 && code.Nilcheck { b = appendNull(b) } else { - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4134,7 +4132,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt break } b = appendStructKey(ctx, code, b) - bb, err := appendMarshalJSON(ctx, code, b, iface, false) + bb, err := appendMarshalJSON(ctx, code, b, iface) if err != nil { return nil, err } @@ -4147,7 +4145,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 { b = appendNull(b) } else { - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4160,7 +4158,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p = ptrToNPtr(p+code.Offset, code.PtrNum) if p != 0 { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4177,7 +4175,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 && code.Nilcheck { b = appendNull(b) } else { - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4196,7 +4194,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt break } b = appendStructKey(ctx, code, b) - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4209,7 +4207,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 { b = appendNull(b) } else { - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4222,7 +4220,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p = ptrToNPtr(p+code.Offset, code.PtrNum) if p != 0 { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4349,7 +4347,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p := load(ctxptr, code.HeadIdx) b = appendStructKey(ctx, code, b) b = appendInt(b, ptrToUint64(p+code.Offset), code) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyInt: p := load(ctxptr, code.HeadIdx) @@ -4358,9 +4356,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v != 0 { b = appendStructKey(ctx, code, b) b = appendInt(b, u64, code) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndIntString: @@ -4369,7 +4367,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendInt(b, ptrToUint64(p+code.Offset), code) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyIntString: p := load(ctxptr, code.HeadIdx) @@ -4380,9 +4378,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendInt(b, u64, code) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndIntPtr: @@ -4394,7 +4392,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendInt(b, ptrToUint64(p), code) } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyIntPtr: p := load(ctxptr, code.HeadIdx) @@ -4402,9 +4400,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendInt(b, ptrToUint64(p), code) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndIntPtrString: @@ -4418,7 +4416,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendInt(b, ptrToUint64(p), code) b = append(b, '"') } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyIntPtrString: p := load(ctxptr, code.HeadIdx) @@ -4428,16 +4426,16 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendInt(b, ptrToUint64(p), code) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndUint: p := load(ctxptr, code.HeadIdx) b = appendStructKey(ctx, code, b) b = appendUint(b, ptrToUint64(p+code.Offset), code) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyUint: p := load(ctxptr, code.HeadIdx) @@ -4446,9 +4444,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v != 0 { b = appendStructKey(ctx, code, b) b = appendUint(b, u64, code) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndUintString: @@ -4457,7 +4455,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendUint(b, ptrToUint64(p+code.Offset), code) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyUintString: p := load(ctxptr, code.HeadIdx) @@ -4468,9 +4466,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendUint(b, u64, code) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndUintPtr: @@ -4482,7 +4480,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendUint(b, ptrToUint64(p), code) } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyUintPtr: p := load(ctxptr, code.HeadIdx) @@ -4490,9 +4488,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendUint(b, ptrToUint64(p), code) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndUintPtrString: @@ -4506,7 +4504,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendUint(b, ptrToUint64(p), code) b = append(b, '"') } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyUintPtrString: p := load(ctxptr, code.HeadIdx) @@ -4516,16 +4514,16 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendUint(b, ptrToUint64(p), code) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat32: p := load(ctxptr, code.HeadIdx) b = appendStructKey(ctx, code, b) b = appendFloat32(b, ptrToFloat32(p+code.Offset)) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat32: p := load(ctxptr, code.HeadIdx) @@ -4533,9 +4531,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v != 0 { b = appendStructKey(ctx, code, b) b = appendFloat32(b, v) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat32String: @@ -4544,7 +4542,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendFloat32(b, ptrToFloat32(p+code.Offset)) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat32String: p := load(ctxptr, code.HeadIdx) @@ -4554,9 +4552,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendFloat32(b, v) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat32Ptr: @@ -4568,7 +4566,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendFloat32(b, ptrToFloat32(p)) } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat32Ptr: p := load(ctxptr, code.HeadIdx) @@ -4576,9 +4574,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendFloat32(b, ptrToFloat32(p)) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat32PtrString: @@ -4592,7 +4590,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendFloat32(b, ptrToFloat32(p)) b = append(b, '"') } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat32PtrString: p := load(ctxptr, code.HeadIdx) @@ -4602,9 +4600,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendFloat32(b, ptrToFloat32(p)) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat64: @@ -4615,7 +4613,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, errUnsupportedFloat(v) } b = appendFloat64(b, v) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat64: p := load(ctxptr, code.HeadIdx) @@ -4626,9 +4624,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } b = appendStructKey(ctx, code, b) b = appendFloat64(b, v) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat64String: @@ -4641,7 +4639,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendFloat64(b, v) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat64String: p := load(ctxptr, code.HeadIdx) @@ -4654,9 +4652,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendFloat64(b, v) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat64Ptr: @@ -4665,7 +4663,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p = ptrToNPtr(p+code.Offset, code.PtrNum) if p == 0 { b = appendNull(b) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next break } @@ -4674,7 +4672,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, errUnsupportedFloat(v) } b = appendFloat64(b, v) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat64Ptr: p := load(ctxptr, code.HeadIdx) @@ -4686,9 +4684,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, errUnsupportedFloat(v) } b = appendFloat64(b, v) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat64PtrString: @@ -4706,7 +4704,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendFloat64(b, v) b = append(b, '"') } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat64PtrString: p := load(ctxptr, code.HeadIdx) @@ -4720,16 +4718,16 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendFloat64(b, v) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndString: p := load(ctxptr, code.HeadIdx) b = appendStructKey(ctx, code, b) b = appendString(b, ptrToString(p+code.Offset)) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyString: p := load(ctxptr, code.HeadIdx) @@ -4737,9 +4735,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v != "" { b = appendStructKey(ctx, code, b) b = appendString(b, v) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndStringString: @@ -4747,7 +4745,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendStructKey(ctx, code, b) s := ptrToString(p + code.Offset) b = appendString(b, string(appendString([]byte{}, s))) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyStringString: p := load(ctxptr, code.HeadIdx) @@ -4755,9 +4753,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v != "" { b = appendStructKey(ctx, code, b) b = appendString(b, string(appendString([]byte{}, v))) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndStringPtr: @@ -4769,7 +4767,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendString(b, ptrToString(p)) } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyStringPtr: p := load(ctxptr, code.HeadIdx) @@ -4777,9 +4775,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendString(b, ptrToString(p)) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndStringPtrString: @@ -4792,7 +4790,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt v := ptrToString(p) b = appendString(b, string(appendString([]byte{}, v))) } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyStringPtrString: p := load(ctxptr, code.HeadIdx) @@ -4801,16 +4799,16 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendStructKey(ctx, code, b) v := ptrToString(p) b = appendString(b, string(appendString([]byte{}, v))) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBool: p := load(ctxptr, code.HeadIdx) b = appendStructKey(ctx, code, b) b = appendBool(b, ptrToBool(p+code.Offset)) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBool: p := load(ctxptr, code.HeadIdx) @@ -4818,9 +4816,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v { b = appendStructKey(ctx, code, b) b = appendBool(b, v) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBoolString: @@ -4829,7 +4827,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendBool(b, ptrToBool(p+code.Offset)) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBoolString: p := load(ctxptr, code.HeadIdx) @@ -4839,9 +4837,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendBool(b, v) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBoolPtr: @@ -4853,7 +4851,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendBool(b, ptrToBool(p)) } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBoolPtr: p := load(ctxptr, code.HeadIdx) @@ -4861,9 +4859,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendBool(b, ptrToBool(p)) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBoolPtrString: @@ -4877,7 +4875,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendBool(b, ptrToBool(p)) b = append(b, '"') } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBoolPtrString: p := load(ctxptr, code.HeadIdx) @@ -4887,16 +4885,16 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendBool(b, ptrToBool(p)) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBytes: p := load(ctxptr, code.HeadIdx) b = appendStructKey(ctx, code, b) b = appendByteSlice(b, ptrToBytes(p+code.Offset)) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBytes: p := load(ctxptr, code.HeadIdx) @@ -4904,9 +4902,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if len(v) > 0 { b = appendStructKey(ctx, code, b) b = appendByteSlice(b, v) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBytesPtr: @@ -4918,7 +4916,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendByteSlice(b, ptrToBytes(p)) } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBytesPtr: p := load(ctxptr, code.HeadIdx) @@ -4926,9 +4924,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendByteSlice(b, ptrToBytes(p)) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndNumber: @@ -4938,7 +4936,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if err != nil { return nil, err } - b = appendStructEnd(bb) + b = appendStructEnd(ctx, code, bb) code = code.Next case encoder.OpStructEndOmitEmptyNumber: p := load(ctxptr, code.HeadIdx) @@ -4949,9 +4947,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if err != nil { return nil, err } - b = appendStructEnd(bb) + b = appendStructEnd(ctx, code, bb) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndNumberString: @@ -4963,7 +4961,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, err } b = append(bb, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyNumberString: p := load(ctxptr, code.HeadIdx) @@ -4976,9 +4974,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, err } b = append(bb, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndNumberPtr: @@ -4994,7 +4992,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } b = bb } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyNumberPtr: p := load(ctxptr, code.HeadIdx) @@ -5005,9 +5003,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if err != nil { return nil, err } - b = appendStructEnd(bb) + b = appendStructEnd(ctx, code, bb) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndNumberPtrString: @@ -5024,7 +5022,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } b = append(bb, '"') } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyNumberPtrString: p := load(ctxptr, code.HeadIdx) @@ -5037,9 +5035,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, err } b = append(bb, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpEnd: diff --git a/internal/encoder/vm_debug/util.go b/internal/encoder/vm_debug/util.go index 94f8521..8cda230 100644 --- a/internal/encoder/vm_debug/util.go +++ b/internal/encoder/vm_debug/util.go @@ -76,6 +76,14 @@ func appendComma(b []byte) []byte { return append(b, ',') } +func appendMarshalJSON(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) { + return encoder.AppendMarshalJSON(ctx, code, b, v, false) +} + +func appendMarshalText(code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) { + return encoder.AppendMarshalText(code, b, v, false) +} + func appendStructKey(_ *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { return append(b, code.Key...) } @@ -84,15 +92,15 @@ func appendStructHead(b []byte) []byte { return append(b, '{') } -func appendStructEnd(b []byte) []byte { +func appendStructEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { return append(b, '}', ',') } -func appendStructEndSkipLast(b []byte) []byte { +func appendStructEndSkipLast(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { last := len(b) - 1 if b[last] == ',' { b[last] = '}' return appendComma(b) } - return appendStructEnd(b) + return appendStructEnd(ctx, code, b) } diff --git a/internal/encoder/vm_debug/vm.go b/internal/encoder/vm_debug/vm.go index ce6ec9f..328f0ad 100644 --- a/internal/encoder/vm_debug/vm.go +++ b/internal/encoder/vm_debug/vm.go @@ -20,8 +20,6 @@ var ( appendString = encoder.AppendString appendByteSlice = encoder.AppendByteSlice appendNumber = encoder.AppendNumber - appendMarshalJSON = encoder.AppendMarshalJSON - appendMarshalText = encoder.AppendMarshalText errUnsupportedValue = encoder.ErrUnsupportedValue errUnsupportedFloat = encoder.ErrUnsupportedFloat mapiterinit = encoder.MapIterInit @@ -286,7 +284,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if code.IsNilableType && code.Indirect { p = ptrToPtr(p) } - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -313,7 +311,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if code.IsNilableType && code.Indirect { p = ptrToPtr(p) } - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3160,7 +3158,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 && code.Nilcheck { b = appendNull(b) } else { - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3205,7 +3203,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.NextField } else { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalJSON(ctx, code, b, iface, false) + bb, err := appendMarshalJSON(ctx, code, b, iface) if err != nil { return nil, err } @@ -3245,7 +3243,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 { b = appendNull(b) } else { - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3285,7 +3283,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.NextField } else { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3329,7 +3327,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 && code.Nilcheck { b = appendNull(b) } else { - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3373,7 +3371,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.NextField } else { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3413,7 +3411,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 { b = appendNull(b) } else { - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3453,7 +3451,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.NextField } else { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4123,7 +4121,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 && code.Nilcheck { b = appendNull(b) } else { - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4147,7 +4145,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt break } b = appendStructKey(ctx, code, b) - bb, err := appendMarshalJSON(ctx, code, b, iface, false) + bb, err := appendMarshalJSON(ctx, code, b, iface) if err != nil { return nil, err } @@ -4160,7 +4158,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 { b = appendNull(b) } else { - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4173,7 +4171,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p = ptrToNPtr(p+code.Offset, code.PtrNum) if p != 0 { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4190,7 +4188,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 && code.Nilcheck { b = appendNull(b) } else { - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4209,7 +4207,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt break } b = appendStructKey(ctx, code, b) - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4222,7 +4220,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 { b = appendNull(b) } else { - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4235,7 +4233,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p = ptrToNPtr(p+code.Offset, code.PtrNum) if p != 0 { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4362,7 +4360,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p := load(ctxptr, code.HeadIdx) b = appendStructKey(ctx, code, b) b = appendInt(b, ptrToUint64(p+code.Offset), code) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyInt: p := load(ctxptr, code.HeadIdx) @@ -4371,9 +4369,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v != 0 { b = appendStructKey(ctx, code, b) b = appendInt(b, u64, code) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndIntString: @@ -4382,7 +4380,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendInt(b, ptrToUint64(p+code.Offset), code) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyIntString: p := load(ctxptr, code.HeadIdx) @@ -4393,9 +4391,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendInt(b, u64, code) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndIntPtr: @@ -4407,7 +4405,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendInt(b, ptrToUint64(p), code) } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyIntPtr: p := load(ctxptr, code.HeadIdx) @@ -4415,9 +4413,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendInt(b, ptrToUint64(p), code) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndIntPtrString: @@ -4431,7 +4429,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendInt(b, ptrToUint64(p), code) b = append(b, '"') } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyIntPtrString: p := load(ctxptr, code.HeadIdx) @@ -4441,16 +4439,16 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendInt(b, ptrToUint64(p), code) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndUint: p := load(ctxptr, code.HeadIdx) b = appendStructKey(ctx, code, b) b = appendUint(b, ptrToUint64(p+code.Offset), code) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyUint: p := load(ctxptr, code.HeadIdx) @@ -4459,9 +4457,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v != 0 { b = appendStructKey(ctx, code, b) b = appendUint(b, u64, code) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndUintString: @@ -4470,7 +4468,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendUint(b, ptrToUint64(p+code.Offset), code) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyUintString: p := load(ctxptr, code.HeadIdx) @@ -4481,9 +4479,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendUint(b, u64, code) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndUintPtr: @@ -4495,7 +4493,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendUint(b, ptrToUint64(p), code) } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyUintPtr: p := load(ctxptr, code.HeadIdx) @@ -4503,9 +4501,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendUint(b, ptrToUint64(p), code) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndUintPtrString: @@ -4519,7 +4517,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendUint(b, ptrToUint64(p), code) b = append(b, '"') } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyUintPtrString: p := load(ctxptr, code.HeadIdx) @@ -4529,16 +4527,16 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendUint(b, ptrToUint64(p), code) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat32: p := load(ctxptr, code.HeadIdx) b = appendStructKey(ctx, code, b) b = appendFloat32(b, ptrToFloat32(p+code.Offset)) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat32: p := load(ctxptr, code.HeadIdx) @@ -4546,9 +4544,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v != 0 { b = appendStructKey(ctx, code, b) b = appendFloat32(b, v) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat32String: @@ -4557,7 +4555,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendFloat32(b, ptrToFloat32(p+code.Offset)) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat32String: p := load(ctxptr, code.HeadIdx) @@ -4567,9 +4565,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendFloat32(b, v) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat32Ptr: @@ -4581,7 +4579,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendFloat32(b, ptrToFloat32(p)) } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat32Ptr: p := load(ctxptr, code.HeadIdx) @@ -4589,9 +4587,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendFloat32(b, ptrToFloat32(p)) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat32PtrString: @@ -4605,7 +4603,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendFloat32(b, ptrToFloat32(p)) b = append(b, '"') } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat32PtrString: p := load(ctxptr, code.HeadIdx) @@ -4615,9 +4613,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendFloat32(b, ptrToFloat32(p)) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat64: @@ -4628,7 +4626,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, errUnsupportedFloat(v) } b = appendFloat64(b, v) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat64: p := load(ctxptr, code.HeadIdx) @@ -4639,9 +4637,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } b = appendStructKey(ctx, code, b) b = appendFloat64(b, v) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat64String: @@ -4654,7 +4652,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendFloat64(b, v) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat64String: p := load(ctxptr, code.HeadIdx) @@ -4667,9 +4665,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendFloat64(b, v) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat64Ptr: @@ -4678,7 +4676,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p = ptrToNPtr(p+code.Offset, code.PtrNum) if p == 0 { b = appendNull(b) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next break } @@ -4687,7 +4685,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, errUnsupportedFloat(v) } b = appendFloat64(b, v) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat64Ptr: p := load(ctxptr, code.HeadIdx) @@ -4699,9 +4697,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, errUnsupportedFloat(v) } b = appendFloat64(b, v) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat64PtrString: @@ -4719,7 +4717,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendFloat64(b, v) b = append(b, '"') } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat64PtrString: p := load(ctxptr, code.HeadIdx) @@ -4733,16 +4731,16 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendFloat64(b, v) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndString: p := load(ctxptr, code.HeadIdx) b = appendStructKey(ctx, code, b) b = appendString(b, ptrToString(p+code.Offset)) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyString: p := load(ctxptr, code.HeadIdx) @@ -4750,9 +4748,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v != "" { b = appendStructKey(ctx, code, b) b = appendString(b, v) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndStringString: @@ -4760,7 +4758,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendStructKey(ctx, code, b) s := ptrToString(p + code.Offset) b = appendString(b, string(appendString([]byte{}, s))) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyStringString: p := load(ctxptr, code.HeadIdx) @@ -4768,9 +4766,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v != "" { b = appendStructKey(ctx, code, b) b = appendString(b, string(appendString([]byte{}, v))) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndStringPtr: @@ -4782,7 +4780,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendString(b, ptrToString(p)) } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyStringPtr: p := load(ctxptr, code.HeadIdx) @@ -4790,9 +4788,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendString(b, ptrToString(p)) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndStringPtrString: @@ -4805,7 +4803,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt v := ptrToString(p) b = appendString(b, string(appendString([]byte{}, v))) } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyStringPtrString: p := load(ctxptr, code.HeadIdx) @@ -4814,16 +4812,16 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendStructKey(ctx, code, b) v := ptrToString(p) b = appendString(b, string(appendString([]byte{}, v))) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBool: p := load(ctxptr, code.HeadIdx) b = appendStructKey(ctx, code, b) b = appendBool(b, ptrToBool(p+code.Offset)) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBool: p := load(ctxptr, code.HeadIdx) @@ -4831,9 +4829,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v { b = appendStructKey(ctx, code, b) b = appendBool(b, v) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBoolString: @@ -4842,7 +4840,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendBool(b, ptrToBool(p+code.Offset)) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBoolString: p := load(ctxptr, code.HeadIdx) @@ -4852,9 +4850,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendBool(b, v) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBoolPtr: @@ -4866,7 +4864,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendBool(b, ptrToBool(p)) } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBoolPtr: p := load(ctxptr, code.HeadIdx) @@ -4874,9 +4872,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendBool(b, ptrToBool(p)) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBoolPtrString: @@ -4890,7 +4888,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendBool(b, ptrToBool(p)) b = append(b, '"') } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBoolPtrString: p := load(ctxptr, code.HeadIdx) @@ -4900,16 +4898,16 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendBool(b, ptrToBool(p)) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBytes: p := load(ctxptr, code.HeadIdx) b = appendStructKey(ctx, code, b) b = appendByteSlice(b, ptrToBytes(p+code.Offset)) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBytes: p := load(ctxptr, code.HeadIdx) @@ -4917,9 +4915,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if len(v) > 0 { b = appendStructKey(ctx, code, b) b = appendByteSlice(b, v) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBytesPtr: @@ -4931,7 +4929,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendByteSlice(b, ptrToBytes(p)) } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBytesPtr: p := load(ctxptr, code.HeadIdx) @@ -4939,9 +4937,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendByteSlice(b, ptrToBytes(p)) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndNumber: @@ -4951,7 +4949,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if err != nil { return nil, err } - b = appendStructEnd(bb) + b = appendStructEnd(ctx, code, bb) code = code.Next case encoder.OpStructEndOmitEmptyNumber: p := load(ctxptr, code.HeadIdx) @@ -4962,9 +4960,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if err != nil { return nil, err } - b = appendStructEnd(bb) + b = appendStructEnd(ctx, code, bb) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndNumberString: @@ -4976,7 +4974,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, err } b = append(bb, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyNumberString: p := load(ctxptr, code.HeadIdx) @@ -4989,9 +4987,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, err } b = append(bb, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndNumberPtr: @@ -5007,7 +5005,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } b = bb } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyNumberPtr: p := load(ctxptr, code.HeadIdx) @@ -5018,9 +5016,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if err != nil { return nil, err } - b = appendStructEnd(bb) + b = appendStructEnd(ctx, code, bb) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndNumberPtrString: @@ -5037,7 +5035,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } b = append(bb, '"') } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyNumberPtrString: p := load(ctxptr, code.HeadIdx) @@ -5050,9 +5048,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, err } b = append(bb, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpEnd: diff --git a/internal/encoder/vm_escaped/util.go b/internal/encoder/vm_escaped/util.go index 07765a7..a32d7bf 100644 --- a/internal/encoder/vm_escaped/util.go +++ b/internal/encoder/vm_escaped/util.go @@ -76,6 +76,14 @@ func appendComma(b []byte) []byte { return append(b, ',') } +func appendMarshalJSON(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) { + return encoder.AppendMarshalJSON(ctx, code, b, v, true) +} + +func appendMarshalText(code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) { + return encoder.AppendMarshalText(code, b, v, true) +} + func appendStructHead(b []byte) []byte { return append(b, '{') } @@ -84,15 +92,15 @@ func appendStructKey(_ *encoder.RuntimeContext, code *encoder.Opcode, b []byte) return append(b, code.EscapedKey...) } -func appendStructEnd(b []byte) []byte { +func appendStructEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { return append(b, '}', ',') } -func appendStructEndSkipLast(b []byte) []byte { +func appendStructEndSkipLast(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { last := len(b) - 1 if b[last] == ',' { b[last] = '}' return appendComma(b) } - return appendStructEnd(b) + return appendStructEnd(ctx, code, b) } diff --git a/internal/encoder/vm_escaped/vm.go b/internal/encoder/vm_escaped/vm.go index f65920f..f2851bb 100644 --- a/internal/encoder/vm_escaped/vm.go +++ b/internal/encoder/vm_escaped/vm.go @@ -26,8 +26,6 @@ var ( appendString = encoder.AppendEscapedString appendByteSlice = encoder.AppendByteSlice appendNumber = encoder.AppendNumber - appendMarshalJSON = encoder.AppendMarshalJSON - appendMarshalText = encoder.AppendMarshalText errUnsupportedValue = encoder.ErrUnsupportedValue errUnsupportedFloat = encoder.ErrUnsupportedFloat mapiterinit = encoder.MapIterInit @@ -273,7 +271,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if code.IsNilableType && code.Indirect { p = ptrToPtr(p) } - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), true) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -300,7 +298,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if code.IsNilableType && code.Indirect { p = ptrToPtr(p) } - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), true) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3147,7 +3145,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 && code.Nilcheck { b = appendNull(b) } else { - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), true) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3192,7 +3190,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.NextField } else { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalJSON(ctx, code, b, iface, true) + bb, err := appendMarshalJSON(ctx, code, b, iface) if err != nil { return nil, err } @@ -3232,7 +3230,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 { b = appendNull(b) } else { - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), true) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3272,7 +3270,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.NextField } else { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), true) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3316,7 +3314,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 && code.Nilcheck { b = appendNull(b) } else { - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), true) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3360,7 +3358,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.NextField } else { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), true) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3400,7 +3398,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 { b = appendNull(b) } else { - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), true) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3440,7 +3438,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.NextField } else { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), true) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4110,7 +4108,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 && code.Nilcheck { b = appendNull(b) } else { - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), true) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4134,7 +4132,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt break } b = appendStructKey(ctx, code, b) - bb, err := appendMarshalJSON(ctx, code, b, iface, true) + bb, err := appendMarshalJSON(ctx, code, b, iface) if err != nil { return nil, err } @@ -4147,7 +4145,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 { b = appendNull(b) } else { - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), true) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4160,7 +4158,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p = ptrToNPtr(p+code.Offset, code.PtrNum) if p != 0 { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), true) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4177,7 +4175,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 && code.Nilcheck { b = appendNull(b) } else { - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), true) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4196,7 +4194,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt break } b = appendStructKey(ctx, code, b) - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), true) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4209,7 +4207,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 { b = appendNull(b) } else { - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), true) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4222,7 +4220,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p = ptrToNPtr(p+code.Offset, code.PtrNum) if p != 0 { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), true) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4349,7 +4347,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p := load(ctxptr, code.HeadIdx) b = appendStructKey(ctx, code, b) b = appendInt(b, ptrToUint64(p+code.Offset), code) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyInt: p := load(ctxptr, code.HeadIdx) @@ -4358,9 +4356,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v != 0 { b = appendStructKey(ctx, code, b) b = appendInt(b, u64, code) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndIntString: @@ -4369,7 +4367,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendInt(b, ptrToUint64(p+code.Offset), code) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyIntString: p := load(ctxptr, code.HeadIdx) @@ -4380,9 +4378,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendInt(b, u64, code) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndIntPtr: @@ -4394,7 +4392,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendInt(b, ptrToUint64(p), code) } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyIntPtr: p := load(ctxptr, code.HeadIdx) @@ -4402,9 +4400,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendInt(b, ptrToUint64(p), code) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndIntPtrString: @@ -4418,7 +4416,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendInt(b, ptrToUint64(p), code) b = append(b, '"') } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyIntPtrString: p := load(ctxptr, code.HeadIdx) @@ -4428,16 +4426,16 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendInt(b, ptrToUint64(p), code) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndUint: p := load(ctxptr, code.HeadIdx) b = appendStructKey(ctx, code, b) b = appendUint(b, ptrToUint64(p+code.Offset), code) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyUint: p := load(ctxptr, code.HeadIdx) @@ -4446,9 +4444,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v != 0 { b = appendStructKey(ctx, code, b) b = appendUint(b, u64, code) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndUintString: @@ -4457,7 +4455,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendUint(b, ptrToUint64(p+code.Offset), code) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyUintString: p := load(ctxptr, code.HeadIdx) @@ -4468,9 +4466,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendUint(b, u64, code) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndUintPtr: @@ -4482,7 +4480,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendUint(b, ptrToUint64(p), code) } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyUintPtr: p := load(ctxptr, code.HeadIdx) @@ -4490,9 +4488,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendUint(b, ptrToUint64(p), code) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndUintPtrString: @@ -4506,7 +4504,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendUint(b, ptrToUint64(p), code) b = append(b, '"') } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyUintPtrString: p := load(ctxptr, code.HeadIdx) @@ -4516,16 +4514,16 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendUint(b, ptrToUint64(p), code) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat32: p := load(ctxptr, code.HeadIdx) b = appendStructKey(ctx, code, b) b = appendFloat32(b, ptrToFloat32(p+code.Offset)) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat32: p := load(ctxptr, code.HeadIdx) @@ -4533,9 +4531,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v != 0 { b = appendStructKey(ctx, code, b) b = appendFloat32(b, v) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat32String: @@ -4544,7 +4542,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendFloat32(b, ptrToFloat32(p+code.Offset)) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat32String: p := load(ctxptr, code.HeadIdx) @@ -4554,9 +4552,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendFloat32(b, v) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat32Ptr: @@ -4568,7 +4566,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendFloat32(b, ptrToFloat32(p)) } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat32Ptr: p := load(ctxptr, code.HeadIdx) @@ -4576,9 +4574,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendFloat32(b, ptrToFloat32(p)) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat32PtrString: @@ -4592,7 +4590,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendFloat32(b, ptrToFloat32(p)) b = append(b, '"') } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat32PtrString: p := load(ctxptr, code.HeadIdx) @@ -4602,9 +4600,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendFloat32(b, ptrToFloat32(p)) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat64: @@ -4615,7 +4613,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, errUnsupportedFloat(v) } b = appendFloat64(b, v) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat64: p := load(ctxptr, code.HeadIdx) @@ -4626,9 +4624,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } b = appendStructKey(ctx, code, b) b = appendFloat64(b, v) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat64String: @@ -4641,7 +4639,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendFloat64(b, v) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat64String: p := load(ctxptr, code.HeadIdx) @@ -4654,9 +4652,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendFloat64(b, v) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat64Ptr: @@ -4665,7 +4663,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p = ptrToNPtr(p+code.Offset, code.PtrNum) if p == 0 { b = appendNull(b) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next break } @@ -4674,7 +4672,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, errUnsupportedFloat(v) } b = appendFloat64(b, v) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat64Ptr: p := load(ctxptr, code.HeadIdx) @@ -4686,9 +4684,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, errUnsupportedFloat(v) } b = appendFloat64(b, v) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat64PtrString: @@ -4706,7 +4704,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendFloat64(b, v) b = append(b, '"') } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat64PtrString: p := load(ctxptr, code.HeadIdx) @@ -4720,16 +4718,16 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendFloat64(b, v) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndString: p := load(ctxptr, code.HeadIdx) b = appendStructKey(ctx, code, b) b = appendString(b, ptrToString(p+code.Offset)) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyString: p := load(ctxptr, code.HeadIdx) @@ -4737,9 +4735,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v != "" { b = appendStructKey(ctx, code, b) b = appendString(b, v) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndStringString: @@ -4747,7 +4745,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendStructKey(ctx, code, b) s := ptrToString(p + code.Offset) b = appendString(b, string(appendString([]byte{}, s))) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyStringString: p := load(ctxptr, code.HeadIdx) @@ -4755,9 +4753,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v != "" { b = appendStructKey(ctx, code, b) b = appendString(b, string(appendString([]byte{}, v))) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndStringPtr: @@ -4769,7 +4767,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendString(b, ptrToString(p)) } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyStringPtr: p := load(ctxptr, code.HeadIdx) @@ -4777,9 +4775,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendString(b, ptrToString(p)) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndStringPtrString: @@ -4792,7 +4790,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt v := ptrToString(p) b = appendString(b, string(appendString([]byte{}, v))) } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyStringPtrString: p := load(ctxptr, code.HeadIdx) @@ -4801,16 +4799,16 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendStructKey(ctx, code, b) v := ptrToString(p) b = appendString(b, string(appendString([]byte{}, v))) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBool: p := load(ctxptr, code.HeadIdx) b = appendStructKey(ctx, code, b) b = appendBool(b, ptrToBool(p+code.Offset)) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBool: p := load(ctxptr, code.HeadIdx) @@ -4818,9 +4816,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v { b = appendStructKey(ctx, code, b) b = appendBool(b, v) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBoolString: @@ -4829,7 +4827,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendBool(b, ptrToBool(p+code.Offset)) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBoolString: p := load(ctxptr, code.HeadIdx) @@ -4839,9 +4837,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendBool(b, v) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBoolPtr: @@ -4853,7 +4851,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendBool(b, ptrToBool(p)) } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBoolPtr: p := load(ctxptr, code.HeadIdx) @@ -4861,9 +4859,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendBool(b, ptrToBool(p)) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBoolPtrString: @@ -4877,7 +4875,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendBool(b, ptrToBool(p)) b = append(b, '"') } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBoolPtrString: p := load(ctxptr, code.HeadIdx) @@ -4887,16 +4885,16 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendBool(b, ptrToBool(p)) b = append(b, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBytes: p := load(ctxptr, code.HeadIdx) b = appendStructKey(ctx, code, b) b = appendByteSlice(b, ptrToBytes(p+code.Offset)) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBytes: p := load(ctxptr, code.HeadIdx) @@ -4904,9 +4902,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if len(v) > 0 { b = appendStructKey(ctx, code, b) b = appendByteSlice(b, v) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBytesPtr: @@ -4918,7 +4916,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendByteSlice(b, ptrToBytes(p)) } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBytesPtr: p := load(ctxptr, code.HeadIdx) @@ -4926,9 +4924,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendByteSlice(b, ptrToBytes(p)) - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndNumber: @@ -4938,7 +4936,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if err != nil { return nil, err } - b = appendStructEnd(bb) + b = appendStructEnd(ctx, code, bb) code = code.Next case encoder.OpStructEndOmitEmptyNumber: p := load(ctxptr, code.HeadIdx) @@ -4949,9 +4947,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if err != nil { return nil, err } - b = appendStructEnd(bb) + b = appendStructEnd(ctx, code, bb) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndNumberString: @@ -4963,7 +4961,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, err } b = append(bb, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyNumberString: p := load(ctxptr, code.HeadIdx) @@ -4976,9 +4974,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, err } b = append(bb, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndNumberPtr: @@ -4994,7 +4992,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } b = bb } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyNumberPtr: p := load(ctxptr, code.HeadIdx) @@ -5005,9 +5003,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if err != nil { return nil, err } - b = appendStructEnd(bb) + b = appendStructEnd(ctx, code, bb) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndNumberPtrString: @@ -5024,7 +5022,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } b = append(bb, '"') } - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyNumberPtrString: p := load(ctxptr, code.HeadIdx) @@ -5037,9 +5035,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, err } b = append(bb, '"') - b = appendStructEnd(b) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(b) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpEnd: diff --git a/internal/encoder/vm_escaped_indent/util.go b/internal/encoder/vm_escaped_indent/util.go index 6f427b1..70e008d 100644 --- a/internal/encoder/vm_escaped_indent/util.go +++ b/internal/encoder/vm_escaped_indent/util.go @@ -76,6 +76,14 @@ func appendComma(b []byte) []byte { return append(b, ',', '\n') } +func appendMarshalJSON(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) { + return encoder.AppendMarshalJSONIndent(ctx, code, b, v, true) +} + +func appendMarshalText(code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) { + return encoder.AppendMarshalTextIndent(code, b, v, true) +} + func appendStructHead(b []byte) []byte { return append(b, '{', '\n') } @@ -86,7 +94,7 @@ func appendStructKey(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte return append(b, ' ') } -func appendStructEndSkipLast(ctx *encoder.RuntimeContext, b []byte, code *encoder.Opcode) []byte { +func appendStructEndSkipLast(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { last := len(b) - 1 if b[last-1] == '{' { b[last] = '}' diff --git a/internal/encoder/vm_escaped_indent/vm.go b/internal/encoder/vm_escaped_indent/vm.go index 752b541..805994b 100644 --- a/internal/encoder/vm_escaped_indent/vm.go +++ b/internal/encoder/vm_escaped_indent/vm.go @@ -26,8 +26,6 @@ var ( appendString = encoder.AppendEscapedString appendByteSlice = encoder.AppendByteSlice appendNumber = encoder.AppendNumber - appendMarshalJSON = encoder.AppendMarshalJSONIndent - appendMarshalText = encoder.AppendMarshalTextIndent appendStructEnd = encoder.AppendStructEndIndent appendIndent = encoder.AppendIndent errUnsupportedValue = encoder.ErrUnsupportedValue @@ -278,7 +276,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if code.IsNilableType && code.Indirect { p = ptrToPtr(p) } - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), code.Indent, true) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -305,7 +303,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if code.IsNilableType && code.Indirect { p = ptrToPtr(p) } - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), true) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -612,7 +610,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if !code.AnonymousHead { b = appendStructHead(b) } - if !code.AnonymousKey && len(code.EscapedKey) > 0 { + if !code.AnonymousKey && len(code.Key) > 0 { b = appendStructKey(ctx, code, b) } p += code.Offset @@ -3170,7 +3168,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 && code.Nilcheck { b = appendNull(b) } else { - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), code.Indent, true) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3215,7 +3213,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.NextField } else { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalJSON(ctx, code, b, iface, code.Indent, true) + bb, err := appendMarshalJSON(ctx, code, b, iface) if err != nil { return nil, err } @@ -3255,7 +3253,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 { b = appendNull(b) } else { - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), code.Indent, true) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3295,7 +3293,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.NextField } else { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), code.Indent, true) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3339,7 +3337,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 && code.Nilcheck { b = appendNull(b) } else { - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), true) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3383,7 +3381,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.NextField } else { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), true) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3423,7 +3421,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 { b = appendNull(b) } else { - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), true) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3463,7 +3461,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.NextField } else { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), true) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4131,7 +4129,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 && code.Nilcheck { b = appendNull(b) } else { - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), code.Indent, true) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4155,7 +4153,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt break } b = appendStructKey(ctx, code, b) - bb, err := appendMarshalJSON(ctx, code, b, iface, code.Indent, true) + bb, err := appendMarshalJSON(ctx, code, b, iface) if err != nil { return nil, err } @@ -4168,7 +4166,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 { b = appendNull(b) } else { - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), code.Indent, true) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4181,7 +4179,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p = ptrToNPtr(p+code.Offset, code.PtrNum) if p != 0 { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), code.Indent, true) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4198,7 +4196,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 && code.Nilcheck { b = appendNull(b) } else { - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), true) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4217,7 +4215,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt break } b = appendStructKey(ctx, code, b) - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), true) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4230,7 +4228,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 { b = appendNull(b) } else { - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), true) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4243,7 +4241,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p = ptrToNPtr(p+code.Offset, code.PtrNum) if p != 0 { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), true) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4388,7 +4386,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) b = appendInt(b, ptrToUint64(p+code.Offset), code) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyInt: p := load(ctxptr, code.HeadIdx) @@ -4397,9 +4395,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v != 0 { b = appendStructKey(ctx, code, b) b = appendInt(b, u64, code) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndIntString: @@ -4408,7 +4406,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendInt(b, ptrToUint64(p+code.Offset), code) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyIntString: p := load(ctxptr, code.HeadIdx) @@ -4419,9 +4417,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendInt(b, u64, code) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndIntPtr: @@ -4433,7 +4431,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendInt(b, ptrToUint64(p), code) } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyIntPtr: p := load(ctxptr, code.HeadIdx) @@ -4441,9 +4439,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendInt(b, ptrToUint64(p), code) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndIntPtrString: @@ -4457,7 +4455,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendInt(b, ptrToUint64(p), code) b = append(b, '"') } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyIntPtrString: p := load(ctxptr, code.HeadIdx) @@ -4467,16 +4465,16 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendInt(b, ptrToUint64(p), code) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndUint: b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) b = appendUint(b, ptrToUint64(p+code.Offset), code) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyUint: p := load(ctxptr, code.HeadIdx) @@ -4485,9 +4483,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v != 0 { b = appendStructKey(ctx, code, b) b = appendUint(b, u64, code) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndUintString: @@ -4496,7 +4494,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendUint(b, ptrToUint64(p+code.Offset), code) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyUintString: p := load(ctxptr, code.HeadIdx) @@ -4507,9 +4505,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendUint(b, u64, code) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndUintPtr: @@ -4521,7 +4519,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendUint(b, ptrToUint64(p), code) } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyUintPtr: p := load(ctxptr, code.HeadIdx) @@ -4529,9 +4527,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendUint(b, ptrToUint64(p), code) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndUintPtrString: @@ -4545,7 +4543,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendUint(b, ptrToUint64(p), code) b = append(b, '"') } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyUintPtrString: p := load(ctxptr, code.HeadIdx) @@ -4555,16 +4553,16 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendUint(b, ptrToUint64(p), code) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat32: b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) b = appendFloat32(b, ptrToFloat32(p+code.Offset)) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat32: p := load(ctxptr, code.HeadIdx) @@ -4572,9 +4570,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v != 0 { b = appendStructKey(ctx, code, b) b = appendFloat32(b, v) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat32String: @@ -4583,7 +4581,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendFloat32(b, ptrToFloat32(p+code.Offset)) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat32String: p := load(ctxptr, code.HeadIdx) @@ -4593,9 +4591,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendFloat32(b, v) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat32Ptr: @@ -4607,7 +4605,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendFloat32(b, ptrToFloat32(p)) } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat32Ptr: p := load(ctxptr, code.HeadIdx) @@ -4615,9 +4613,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendFloat32(b, ptrToFloat32(p)) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat32PtrString: @@ -4631,7 +4629,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendFloat32(b, ptrToFloat32(p)) b = append(b, '"') } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat32PtrString: p := load(ctxptr, code.HeadIdx) @@ -4641,9 +4639,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendFloat32(b, ptrToFloat32(p)) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat64: @@ -4654,7 +4652,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, errUnsupportedFloat(v) } b = appendFloat64(b, v) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat64: p := load(ctxptr, code.HeadIdx) @@ -4665,9 +4663,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, errUnsupportedFloat(v) } b = appendFloat64(b, v) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat64String: @@ -4680,7 +4678,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendFloat64(b, v) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat64String: p := load(ctxptr, code.HeadIdx) @@ -4693,9 +4691,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } b = appendFloat64(b, v) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat64Ptr: @@ -4711,7 +4709,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } b = appendFloat64(b, v) } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat64Ptr: p := load(ctxptr, code.HeadIdx) @@ -4723,9 +4721,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, errUnsupportedFloat(v) } b = appendFloat64(b, v) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat64PtrString: @@ -4743,7 +4741,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendFloat64(b, v) b = append(b, '"') } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat64PtrString: p := load(ctxptr, code.HeadIdx) @@ -4757,16 +4755,16 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } b = appendFloat64(b, v) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndString: b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) b = appendString(b, ptrToString(p+code.Offset)) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyString: p := load(ctxptr, code.HeadIdx) @@ -4774,9 +4772,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v != "" { b = appendStructKey(ctx, code, b) b = appendString(b, v) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndStringString: @@ -4784,7 +4782,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendStructKey(ctx, code, b) s := ptrToString(p + code.Offset) b = appendString(b, string(appendString([]byte{}, s))) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyStringString: p := load(ctxptr, code.HeadIdx) @@ -4792,9 +4790,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v != "" { b = appendStructKey(ctx, code, b) b = appendString(b, string(appendString([]byte{}, v))) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndStringPtr: @@ -4806,7 +4804,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendString(b, ptrToString(p)) } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyStringPtr: p := load(ctxptr, code.HeadIdx) @@ -4814,9 +4812,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendString(b, ptrToString(p)) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndStringPtrString: @@ -4828,7 +4826,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendString(b, string(appendString([]byte{}, ptrToString(p)))) } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyStringPtrString: p := load(ctxptr, code.HeadIdx) @@ -4836,16 +4834,16 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendString(b, string(appendString([]byte{}, ptrToString(p)))) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBool: b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) b = appendBool(b, ptrToBool(p+code.Offset)) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBool: p := load(ctxptr, code.HeadIdx) @@ -4853,9 +4851,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v { b = appendStructKey(ctx, code, b) b = appendBool(b, v) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBoolString: @@ -4864,7 +4862,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendBool(b, ptrToBool(p+code.Offset)) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBoolString: p := load(ctxptr, code.HeadIdx) @@ -4874,9 +4872,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendBool(b, v) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBoolPtr: @@ -4888,7 +4886,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendBool(b, ptrToBool(p)) } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBoolPtr: p := load(ctxptr, code.HeadIdx) @@ -4896,9 +4894,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendBool(b, ptrToBool(p)) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBoolPtrString: @@ -4912,7 +4910,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendBool(b, ptrToBool(p)) b = append(b, '"') } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBoolPtrString: p := load(ctxptr, code.HeadIdx) @@ -4922,16 +4920,16 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendBool(b, ptrToBool(p)) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBytes: b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) b = appendByteSlice(b, ptrToBytes(p+code.Offset)) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBytes: p := load(ctxptr, code.HeadIdx) @@ -4939,9 +4937,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if len(v) > 0 { b = appendStructKey(ctx, code, b) b = appendByteSlice(b, v) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBytesPtr: @@ -4953,7 +4951,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendByteSlice(b, ptrToBytes(p)) } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBytesPtr: p := load(ctxptr, code.HeadIdx) @@ -4961,9 +4959,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendByteSlice(b, ptrToBytes(p)) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndNumber: @@ -4973,7 +4971,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if err != nil { return nil, err } - b = appendStructEnd(ctx, bb, code.Indent-1) + b = appendStructEnd(ctx, code, bb) code = code.Next case encoder.OpStructEndOmitEmptyNumber: p := load(ctxptr, code.HeadIdx) @@ -4984,9 +4982,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if err != nil { return nil, err } - b = appendStructEnd(ctx, bb, code.Indent-1) + b = appendStructEnd(ctx, code, bb) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndNumberString: @@ -4998,7 +4996,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, err } b = append(bb, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyNumberString: p := load(ctxptr, code.HeadIdx) @@ -5011,9 +5009,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, err } b = append(bb, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndNumberPtr: @@ -5029,7 +5027,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } b = bb } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyNumberPtr: p := load(ctxptr, code.HeadIdx) @@ -5040,9 +5038,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if err != nil { return nil, err } - b = appendStructEnd(ctx, bb, code.Indent-1) + b = appendStructEnd(ctx, code, bb) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndNumberPtrString: @@ -5059,7 +5057,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } b = append(bb, '"') } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyNumberPtrString: p := load(ctxptr, code.HeadIdx) @@ -5072,9 +5070,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, err } b = append(bb, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpEnd: diff --git a/internal/encoder/vm_indent/util.go b/internal/encoder/vm_indent/util.go index a20ca12..8a74c08 100644 --- a/internal/encoder/vm_indent/util.go +++ b/internal/encoder/vm_indent/util.go @@ -80,13 +80,21 @@ func appendStructHead(b []byte) []byte { return append(b, '{', '\n') } +func appendMarshalJSON(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) { + return encoder.AppendMarshalJSONIndent(ctx, code, b, v, false) +} + +func appendMarshalText(code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) { + return encoder.AppendMarshalTextIndent(code, b, v, false) +} + func appendStructKey(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { b = appendIndent(ctx, b, code.Indent) b = append(b, code.Key...) return append(b, ' ') } -func appendStructEndSkipLast(ctx *encoder.RuntimeContext, b []byte, code *encoder.Opcode) []byte { +func appendStructEndSkipLast(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { last := len(b) - 1 if b[last-1] == '{' { b[last] = '}' diff --git a/internal/encoder/vm_indent/vm.go b/internal/encoder/vm_indent/vm.go index 31ce1e4..dfc7a30 100644 --- a/internal/encoder/vm_indent/vm.go +++ b/internal/encoder/vm_indent/vm.go @@ -26,8 +26,6 @@ var ( appendString = encoder.AppendString appendByteSlice = encoder.AppendByteSlice appendNumber = encoder.AppendNumber - appendMarshalJSON = encoder.AppendMarshalJSONIndent - appendMarshalText = encoder.AppendMarshalTextIndent appendStructEnd = encoder.AppendStructEndIndent appendIndent = encoder.AppendIndent errUnsupportedValue = encoder.ErrUnsupportedValue @@ -278,7 +276,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if code.IsNilableType && code.Indirect { p = ptrToPtr(p) } - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), code.Indent, false) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -305,7 +303,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if code.IsNilableType && code.Indirect { p = ptrToPtr(p) } - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3170,7 +3168,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 && code.Nilcheck { b = appendNull(b) } else { - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), code.Indent, false) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3215,7 +3213,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.NextField } else { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalJSON(ctx, code, b, iface, code.Indent, false) + bb, err := appendMarshalJSON(ctx, code, b, iface) if err != nil { return nil, err } @@ -3255,7 +3253,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 { b = appendNull(b) } else { - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), code.Indent, false) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3295,7 +3293,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.NextField } else { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), code.Indent, false) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3339,7 +3337,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 && code.Nilcheck { b = appendNull(b) } else { - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3383,7 +3381,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.NextField } else { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3423,7 +3421,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 { b = appendNull(b) } else { - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -3463,7 +3461,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.NextField } else { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4131,7 +4129,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 && code.Nilcheck { b = appendNull(b) } else { - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), code.Indent, false) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4155,7 +4153,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt break } b = appendStructKey(ctx, code, b) - bb, err := appendMarshalJSON(ctx, code, b, iface, code.Indent, false) + bb, err := appendMarshalJSON(ctx, code, b, iface) if err != nil { return nil, err } @@ -4168,7 +4166,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 { b = appendNull(b) } else { - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), code.Indent, false) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4181,7 +4179,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p = ptrToNPtr(p+code.Offset, code.PtrNum) if p != 0 { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), code.Indent, false) + bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4198,7 +4196,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 && code.Nilcheck { b = appendNull(b) } else { - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4217,7 +4215,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt break } b = appendStructKey(ctx, code, b) - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4230,7 +4228,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 { b = appendNull(b) } else { - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4243,7 +4241,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p = ptrToNPtr(p+code.Offset, code.PtrNum) if p != 0 { b = appendStructKey(ctx, code, b) - bb, err := appendMarshalText(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) if err != nil { return nil, err } @@ -4388,7 +4386,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) b = appendInt(b, ptrToUint64(p+code.Offset), code) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyInt: p := load(ctxptr, code.HeadIdx) @@ -4397,9 +4395,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v != 0 { b = appendStructKey(ctx, code, b) b = appendInt(b, u64, code) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndIntString: @@ -4408,7 +4406,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendInt(b, ptrToUint64(p+code.Offset), code) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyIntString: p := load(ctxptr, code.HeadIdx) @@ -4419,9 +4417,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendInt(b, u64, code) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndIntPtr: @@ -4433,7 +4431,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendInt(b, ptrToUint64(p), code) } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyIntPtr: p := load(ctxptr, code.HeadIdx) @@ -4441,9 +4439,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendInt(b, ptrToUint64(p), code) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndIntPtrString: @@ -4457,7 +4455,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendInt(b, ptrToUint64(p), code) b = append(b, '"') } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyIntPtrString: p := load(ctxptr, code.HeadIdx) @@ -4467,16 +4465,16 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendInt(b, ptrToUint64(p), code) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndUint: b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) b = appendUint(b, ptrToUint64(p+code.Offset), code) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyUint: p := load(ctxptr, code.HeadIdx) @@ -4485,9 +4483,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v != 0 { b = appendStructKey(ctx, code, b) b = appendUint(b, u64, code) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndUintString: @@ -4496,7 +4494,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendUint(b, ptrToUint64(p+code.Offset), code) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyUintString: p := load(ctxptr, code.HeadIdx) @@ -4507,9 +4505,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendUint(b, u64, code) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndUintPtr: @@ -4521,7 +4519,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendUint(b, ptrToUint64(p), code) } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyUintPtr: p := load(ctxptr, code.HeadIdx) @@ -4529,9 +4527,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendUint(b, ptrToUint64(p), code) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndUintPtrString: @@ -4545,7 +4543,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendUint(b, ptrToUint64(p), code) b = append(b, '"') } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyUintPtrString: p := load(ctxptr, code.HeadIdx) @@ -4555,16 +4553,16 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendUint(b, ptrToUint64(p), code) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat32: b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) b = appendFloat32(b, ptrToFloat32(p+code.Offset)) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat32: p := load(ctxptr, code.HeadIdx) @@ -4572,9 +4570,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v != 0 { b = appendStructKey(ctx, code, b) b = appendFloat32(b, v) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat32String: @@ -4583,7 +4581,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendFloat32(b, ptrToFloat32(p+code.Offset)) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat32String: p := load(ctxptr, code.HeadIdx) @@ -4593,9 +4591,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendFloat32(b, v) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat32Ptr: @@ -4607,7 +4605,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendFloat32(b, ptrToFloat32(p)) } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat32Ptr: p := load(ctxptr, code.HeadIdx) @@ -4615,9 +4613,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendFloat32(b, ptrToFloat32(p)) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat32PtrString: @@ -4631,7 +4629,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendFloat32(b, ptrToFloat32(p)) b = append(b, '"') } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat32PtrString: p := load(ctxptr, code.HeadIdx) @@ -4641,9 +4639,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendFloat32(b, ptrToFloat32(p)) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat64: @@ -4654,7 +4652,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, errUnsupportedFloat(v) } b = appendFloat64(b, v) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat64: p := load(ctxptr, code.HeadIdx) @@ -4665,9 +4663,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, errUnsupportedFloat(v) } b = appendFloat64(b, v) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat64String: @@ -4680,7 +4678,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendFloat64(b, v) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat64String: p := load(ctxptr, code.HeadIdx) @@ -4693,9 +4691,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } b = appendFloat64(b, v) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat64Ptr: @@ -4711,7 +4709,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } b = appendFloat64(b, v) } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat64Ptr: p := load(ctxptr, code.HeadIdx) @@ -4723,9 +4721,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, errUnsupportedFloat(v) } b = appendFloat64(b, v) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndFloat64PtrString: @@ -4743,7 +4741,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendFloat64(b, v) b = append(b, '"') } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat64PtrString: p := load(ctxptr, code.HeadIdx) @@ -4757,16 +4755,16 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } b = appendFloat64(b, v) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndString: b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) b = appendString(b, ptrToString(p+code.Offset)) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyString: p := load(ctxptr, code.HeadIdx) @@ -4774,9 +4772,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v != "" { b = appendStructKey(ctx, code, b) b = appendString(b, v) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndStringString: @@ -4784,7 +4782,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendStructKey(ctx, code, b) s := ptrToString(p + code.Offset) b = appendString(b, string(appendString([]byte{}, s))) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyStringString: p := load(ctxptr, code.HeadIdx) @@ -4792,9 +4790,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v != "" { b = appendStructKey(ctx, code, b) b = appendString(b, string(appendString([]byte{}, v))) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndStringPtr: @@ -4806,7 +4804,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendString(b, ptrToString(p)) } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyStringPtr: p := load(ctxptr, code.HeadIdx) @@ -4814,9 +4812,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendString(b, ptrToString(p)) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndStringPtrString: @@ -4828,7 +4826,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendString(b, string(appendString([]byte{}, ptrToString(p)))) } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyStringPtrString: p := load(ctxptr, code.HeadIdx) @@ -4836,16 +4834,16 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendString(b, string(appendString([]byte{}, ptrToString(p)))) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBool: b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) b = appendBool(b, ptrToBool(p+code.Offset)) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBool: p := load(ctxptr, code.HeadIdx) @@ -4853,9 +4851,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if v { b = appendStructKey(ctx, code, b) b = appendBool(b, v) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBoolString: @@ -4864,7 +4862,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendBool(b, ptrToBool(p+code.Offset)) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBoolString: p := load(ctxptr, code.HeadIdx) @@ -4874,9 +4872,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendBool(b, v) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBoolPtr: @@ -4888,7 +4886,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendBool(b, ptrToBool(p)) } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBoolPtr: p := load(ctxptr, code.HeadIdx) @@ -4896,9 +4894,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendBool(b, ptrToBool(p)) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBoolPtrString: @@ -4912,7 +4910,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendBool(b, ptrToBool(p)) b = append(b, '"') } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBoolPtrString: p := load(ctxptr, code.HeadIdx) @@ -4922,16 +4920,16 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = append(b, '"') b = appendBool(b, ptrToBool(p)) b = append(b, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBytes: b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) b = appendByteSlice(b, ptrToBytes(p+code.Offset)) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBytes: p := load(ctxptr, code.HeadIdx) @@ -4939,9 +4937,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if len(v) > 0 { b = appendStructKey(ctx, code, b) b = appendByteSlice(b, v) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndBytesPtr: @@ -4953,7 +4951,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } else { b = appendByteSlice(b, ptrToBytes(p)) } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyBytesPtr: p := load(ctxptr, code.HeadIdx) @@ -4961,9 +4959,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p != 0 { b = appendStructKey(ctx, code, b) b = appendByteSlice(b, ptrToBytes(p)) - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndNumber: @@ -4973,7 +4971,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if err != nil { return nil, err } - b = appendStructEnd(ctx, bb, code.Indent-1) + b = appendStructEnd(ctx, code, bb) code = code.Next case encoder.OpStructEndOmitEmptyNumber: p := load(ctxptr, code.HeadIdx) @@ -4984,9 +4982,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if err != nil { return nil, err } - b = appendStructEnd(ctx, bb, code.Indent-1) + b = appendStructEnd(ctx, code, bb) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndNumberString: @@ -4998,7 +4996,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, err } b = append(bb, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyNumberString: p := load(ctxptr, code.HeadIdx) @@ -5011,9 +5009,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, err } b = append(bb, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndNumberPtr: @@ -5029,7 +5027,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } b = bb } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyNumberPtr: p := load(ctxptr, code.HeadIdx) @@ -5040,9 +5038,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if err != nil { return nil, err } - b = appendStructEnd(ctx, bb, code.Indent-1) + b = appendStructEnd(ctx, code, bb) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpStructEndNumberPtrString: @@ -5059,7 +5057,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } b = append(bb, '"') } - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyNumberPtrString: p := load(ctxptr, code.HeadIdx) @@ -5072,9 +5070,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt return nil, err } b = append(bb, '"') - b = appendStructEnd(ctx, b, code.Indent-1) + b = appendStructEnd(ctx, code, b) } else { - b = appendStructEndSkipLast(ctx, b, code) + b = appendStructEndSkipLast(ctx, code, b) } code = code.Next case encoder.OpEnd: From f696453c1bddb535220b9a43850b6e148e2777b2 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Tue, 18 May 2021 13:51:57 +0900 Subject: [PATCH 2/3] Refactor vm sources for encoding ( use same source ) --- internal/encoder/compiler.go | 3 +- internal/encoder/vm/hack.go | 9 + internal/encoder/vm/util.go | 113 ++++++++++- internal/encoder/vm/vm.go | 169 ++++++---------- internal/encoder/vm_debug/util.go | 113 ++++++++++- internal/encoder/vm_debug/vm.go | 162 ++++++--------- internal/encoder/vm_escaped/hack.go | 9 + internal/encoder/vm_escaped/util.go | 109 ++++++++++ internal/encoder/vm_escaped/vm.go | 169 ++++++---------- internal/encoder/vm_escaped_indent/hack.go | 9 + internal/encoder/vm_escaped_indent/util.go | 130 ++++++++++++ internal/encoder/vm_escaped_indent/vm.go | 220 ++++++--------------- internal/encoder/vm_indent/hack.go | 9 + internal/encoder/vm_indent/util.go | 134 ++++++++++++- internal/encoder/vm_indent/vm.go | 220 ++++++--------------- 15 files changed, 927 insertions(+), 651 deletions(-) create mode 100644 internal/encoder/vm/hack.go create mode 100644 internal/encoder/vm_escaped/hack.go create mode 100644 internal/encoder/vm_escaped_indent/hack.go create mode 100644 internal/encoder/vm_indent/hack.go diff --git a/internal/encoder/compiler.go b/internal/encoder/compiler.go index 128ec48..486c80f 100644 --- a/internal/encoder/compiler.go +++ b/internal/encoder/compiler.go @@ -1450,7 +1450,6 @@ func compileStruct(ctx *compileContext, isPtr bool) (*Opcode, error) { } fieldIdx++ } - ctx = ctx.decIndent() structEndCode := &Opcode{ Op: OpStructEnd, @@ -1459,6 +1458,8 @@ func compileStruct(ctx *compileContext, isPtr bool) (*Opcode, error) { Next: newEndOp(ctx), } + ctx = ctx.decIndent() + // no struct field if head == nil { head = &Opcode{ diff --git a/internal/encoder/vm/hack.go b/internal/encoder/vm/hack.go new file mode 100644 index 0000000..2c1918d --- /dev/null +++ b/internal/encoder/vm/hack.go @@ -0,0 +1,9 @@ +package vm + +import ( + // HACK: compile order + // `vm`, `vm_escaped`, `vm_indent`, `vm_escaped_indent`, `vm_debug` packages uses a lot of memory to compile, + // so forcibly make dependencies and avoid compiling in concurrent. + // dependency order: vm => vm_escaped => vm_indent => vm_escaped_indent => vm_debug + _ "github.com/goccy/go-json/internal/encoder/vm_escaped" +) diff --git a/internal/encoder/vm/util.go b/internal/encoder/vm/util.go index 9faccd7..7b8f331 100644 --- a/internal/encoder/vm/util.go +++ b/internal/encoder/vm/util.go @@ -2,12 +2,41 @@ package vm import ( "encoding/json" + "fmt" "unsafe" "github.com/goccy/go-json/internal/encoder" "github.com/goccy/go-json/internal/runtime" ) +const uintptrSize = 4 << (^uintptr(0) >> 63) + +var ( + appendInt = encoder.AppendInt + appendUint = encoder.AppendUint + appendFloat32 = encoder.AppendFloat32 + appendFloat64 = encoder.AppendFloat64 + appendString = encoder.AppendString + appendByteSlice = encoder.AppendByteSlice + appendNumber = encoder.AppendNumber + errUnsupportedValue = encoder.ErrUnsupportedValue + errUnsupportedFloat = encoder.ErrUnsupportedFloat + mapiterinit = encoder.MapIterInit + mapiterkey = encoder.MapIterKey + mapitervalue = encoder.MapIterValue + mapiternext = encoder.MapIterNext + maplen = encoder.MapLen +) + +type emptyInterface struct { + typ *runtime.Type + ptr unsafe.Pointer +} + +func errUnimplementedOp(op encoder.OpType) error { + return fmt.Errorf("encoder: opcode %s has not been implemented", op) +} + func load(base uintptr, idx uintptr) uintptr { addr := base + idx return **(**uintptr)(unsafe.Pointer(&addr)) @@ -76,6 +105,57 @@ func appendComma(b []byte) []byte { return append(b, ',') } +func appendColon(b []byte) []byte { + last := len(b) - 1 + b[last] = ':' + return b +} + +func appendMapKeyValue(ctx *encoder.RuntimeContext, code *encoder.Opcode, b, key, value []byte) []byte { + b = append(b, key...) + b[len(b)-1] = ':' + return append(b, value...) +} + +func appendMapEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { + b[len(b)-1] = '}' + b = append(b, ',') + return b +} + +func appendInterface(ctx *encoder.RuntimeContext, codeSet *encoder.OpcodeSet, opt encoder.Option, 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 + + bb, err := Run(ctx, b, ifaceCodeSet, opt) + 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, false) } @@ -84,14 +164,38 @@ func appendMarshalText(code *encoder.Opcode, b []byte, v interface{}) ([]byte, e return encoder.AppendMarshalText(code, b, v, false) } -func appendStructKey(_ *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { - return append(b, code.Key...) +func appendArrayHead(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { + return append(b, '[') +} + +func appendArrayEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { + last := len(b) - 1 + b[last] = ']' + return append(b, ',') +} + +func appendEmptyArray(b []byte) []byte { + return append(b, '[', ']', ',') +} + +func appendEmptyObject(b []byte) []byte { + return append(b, '{', '}', ',') +} + +func appendObjectEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { + last := len(b) - 1 + b[last] = '}' + return append(b, ',') } func appendStructHead(b []byte) []byte { return append(b, '{') } +func appendStructKey(_ *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { + return append(b, code.Key...) +} + func appendStructEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { return append(b, '}', ',') } @@ -104,3 +208,8 @@ func appendStructEndSkipLast(ctx *encoder.RuntimeContext, code *encoder.Opcode, } return appendStructEnd(ctx, code, b) } + +func restoreIndent(ctx *encoder.RuntimeContext, code *encoder.Opcode, ctxptr uintptr) {} +func storeIndent(ctxptr uintptr, code *encoder.Opcode, indent uintptr) {} +func appendMapKeyIndent(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { return b } +func appendArrayElemIndent(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { return b } diff --git a/internal/encoder/vm/vm.go b/internal/encoder/vm/vm.go index beab6ee..dca1a99 100644 --- a/internal/encoder/vm/vm.go +++ b/internal/encoder/vm/vm.go @@ -1,45 +1,13 @@ package vm import ( - "fmt" "math" "sort" "unsafe" "github.com/goccy/go-json/internal/encoder" - "github.com/goccy/go-json/internal/runtime" - - // HACK: compile order - // `vm`, `vm_escaped`, `vm_indent`, `vm_escaped_indent`, `vm_debug` packages uses a lot of memory to compile, - // so forcibly make dependencies and avoid compiling in concurrent. - // dependency order: vm => vm_escaped => vm_indent => vm_escaped_indent => vm_debug - _ "github.com/goccy/go-json/internal/encoder/vm_escaped" ) -const uintptrSize = 4 << (^uintptr(0) >> 63) - -var ( - appendInt = encoder.AppendInt - appendUint = encoder.AppendUint - appendFloat32 = encoder.AppendFloat32 - appendFloat64 = encoder.AppendFloat64 - appendString = encoder.AppendString - appendByteSlice = encoder.AppendByteSlice - appendNumber = encoder.AppendNumber - errUnsupportedValue = encoder.ErrUnsupportedValue - errUnsupportedFloat = encoder.ErrUnsupportedFloat - mapiterinit = encoder.MapIterInit - mapiterkey = encoder.MapIterKey - mapitervalue = encoder.MapIterValue - mapiternext = encoder.MapIterNext - maplen = encoder.MapLen -) - -type emptyInterface struct { - typ *runtime.Type - ptr unsafe.Pointer -} - func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt encoder.Option) ([]byte, error) { recursiveLevel := 0 ptrOffset := uintptr(0) @@ -49,7 +17,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt for { switch code.Op { default: - return nil, fmt.Errorf("encoder: opcode %s has not been implemented", code.Op) + return nil, errUnimplementedOp(code.Op) case encoder.OpPtr: p := load(ctxptr, code.Idx) code = code.Next @@ -121,7 +89,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt case encoder.OpFloat64: v := ptrToFloat64(load(ctxptr, code.Idx)) if math.IsInf(v, 0) || math.IsNaN(v) { - return nil, encoder.ErrUnsupportedFloat(v) + return nil, errUnsupportedFloat(v) } b = appendFloat64(b, v) b = appendComma(b) @@ -216,35 +184,10 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.Next break } - ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(iface)) - ifaceCodeSet, err := encoder.CompileToGetCodeSet(uintptr(unsafe.Pointer(iface.typ))) + bb, err := appendInterface(ctx, codeSet, opt, code, b, iface, ptrOffset) 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, opt) - if err != nil { - return nil, err - } - - ctx.Ptrs = oldPtrs ctxptr = ctx.Ptr() ctx.SeenPtr = ctx.SeenPtr[:len(ctx.SeenPtr)-1] @@ -327,11 +270,11 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt store(ctxptr, code.Length, uintptr(slice.Len)) store(ctxptr, code.Idx, uintptr(slice.Data)) if slice.Len > 0 { - b = append(b, '[') + b = appendArrayHead(ctx, code, b) code = code.Next store(ctxptr, code.Idx, uintptr(slice.Data)) } else { - b = append(b, '[', ']', ',') + b = appendEmptyArray(b) code = code.End.Next } case encoder.OpSliceElem: @@ -339,15 +282,14 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt length := load(ctxptr, code.Length) idx++ if idx < length { + b = appendArrayElemIndent(ctx, code, b) store(ctxptr, code.ElemIdx, idx) data := load(ctxptr, code.HeadIdx) size := code.Size code = code.Next store(ctxptr, code.Idx, data+idx*size) } else { - last := len(b) - 1 - b[last] = ']' - b = appendComma(b) + b = appendArrayEnd(ctx, code, b) code = code.End.Next } case encoder.OpArrayPtr: @@ -369,27 +311,26 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt break } if code.Length > 0 { - b = append(b, '[') + b = appendArrayHead(ctx, code, b) store(ctxptr, code.ElemIdx, 0) code = code.Next store(ctxptr, code.Idx, p) } else { - b = append(b, '[', ']', ',') + b = appendEmptyArray(b) code = code.End.Next } case encoder.OpArrayElem: idx := load(ctxptr, code.ElemIdx) idx++ if idx < code.Length { + b = appendArrayElemIndent(ctx, code, b) store(ctxptr, code.ElemIdx, idx) p := load(ctxptr, code.HeadIdx) size := code.Size code = code.Next store(ctxptr, code.Idx, p+idx*size) } else { - last := len(b) - 1 - b[last] = ']' - b = appendComma(b) + b = appendArrayEnd(ctx, code, b) code = code.End.Next } case encoder.OpMapPtr: @@ -413,11 +354,11 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt uptr := ptrToUnsafePtr(p) mlen := maplen(uptr) if mlen <= 0 { - b = append(b, '{', '}', ',') + b = appendEmptyObject(b) code = code.End.Next break } - b = append(b, '{') + b = appendStructHead(b) iter := mapiterinit(code.Type, uptr) ctx.KeepRefs = append(ctx.KeepRefs, iter) store(ctxptr, code.ElemIdx, 0) @@ -428,6 +369,8 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt mapCtx.Pos = append(mapCtx.Pos, len(b)) ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx)) store(ctxptr, code.End.MapPos, uintptr(unsafe.Pointer(mapCtx))) + } else { + b = appendMapKeyIndent(ctx, code.Next, b) } key := mapiterkey(iter) store(ctxptr, code.Next.Idx, uintptr(key)) @@ -438,16 +381,15 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt idx++ if (opt & encoder.UnorderedMapOption) != 0 { if idx < length { + b = appendMapKeyIndent(ctx, code, b) + store(ctxptr, code.ElemIdx, idx) ptr := load(ctxptr, code.MapIter) iter := ptrToUnsafePtr(ptr) - store(ctxptr, code.ElemIdx, idx) key := mapiterkey(iter) store(ctxptr, code.Next.Idx, uintptr(key)) code = code.Next } else { - last := len(b) - 1 - b[last] = '}' - b = appendComma(b) + b = appendObjectEnd(ctx, code, b) code = code.End.Next } } else { @@ -467,8 +409,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } case encoder.OpMapValue: if (opt & encoder.UnorderedMapOption) != 0 { - last := len(b) - 1 - b[last] = ':' + b = appendColon(b) } else { ptr := load(ctxptr, code.End.MapPos) mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(ptr)) @@ -503,12 +444,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt sort.Sort(mapCtx.Slice) buf := mapCtx.Buf for _, item := range mapCtx.Slice.Items { - buf = append(buf, item.Key...) - buf[len(buf)-1] = ':' - buf = append(buf, item.Value...) + buf = appendMapKeyValue(ctx, code, buf, item.Key, item.Value) } - buf[len(buf)-1] = '}' - buf = append(buf, ',') + buf = appendMapEnd(ctx, code, buf) b = b[:pos[0]] b = append(b, buf...) mapCtx.Buf = buf @@ -539,6 +477,8 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt offsetNum := ptrOffset / uintptrSize oldOffset := ptrOffset ptrOffset += code.Jmp.CurLen * uintptrSize + oldBaseIndent := ctx.BaseIndent + ctx.BaseIndent += code.Indent - 1 newLen := offsetNum + code.Jmp.CurLen + code.Jmp.NextLen if curlen < newLen { @@ -549,12 +489,14 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt 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)) code = c recursiveLevel++ case encoder.OpRecursiveEnd: recursiveLevel-- // restore ctxptr + restoreIndent(ctx, code, ctxptr) offset := load(ctxptr, code.Idx) ctx.SeenPtr = ctx.SeenPtr[:len(ctx.SeenPtr)-1] @@ -587,7 +529,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if !code.AnonymousHead { b = appendStructHead(b) } - if !code.AnonymousKey { + if !code.AnonymousKey && len(code.Key) > 0 { b = appendStructKey(ctx, code, b) } p += code.Offset @@ -1883,8 +1825,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendStructHead(b) } b = appendStructKey(ctx, code, b) - s := ptrToString(p + code.Offset) - b = appendString(b, string(appendString([]byte{}, s))) + b = appendString(b, string(appendString([]byte{}, ptrToString(p+code.Offset)))) b = appendComma(b) code = code.Next case encoder.OpStructPtrHeadOmitEmptyStringString: @@ -2404,11 +2345,11 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendStructHead(b) } v := ptrToBytes(p + code.Offset) - if v == nil { + if len(v) == 0 { code = code.NextField } else { b = appendStructKey(ctx, code, b) - b = appendByteSlice(b, ptrToBytes(p)) + b = appendByteSlice(b, v) b = appendComma(b) code = code.Next } @@ -3500,9 +3441,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldIntPtr: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -3580,9 +3521,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldUintPtr: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -3658,9 +3599,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldFloat32Ptr: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -3750,9 +3691,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldFloat64Ptr: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) b = appendComma(b) @@ -3828,8 +3769,8 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.Next case encoder.OpStructFieldStringString: p := load(ctxptr, code.HeadIdx) - b = appendStructKey(ctx, code, b) s := ptrToString(p + code.Offset) + b = appendStructKey(ctx, code, b) b = appendString(b, string(appendString([]byte{}, s))) b = appendComma(b) code = code.Next @@ -3843,9 +3784,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldStringPtr: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -3863,9 +3804,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldStringPtrString: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -3917,9 +3858,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldBoolPtr: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -3937,9 +3878,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldBoolPtrString: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -3976,9 +3917,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldBytesPtr: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -4042,9 +3983,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldNumberPtr: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -4069,9 +4010,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldNumberPtrString: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -4234,9 +4175,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.Next store(ctxptr, code.Idx, p) case encoder.OpStructFieldOmitEmptyArray: + b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p += code.Offset - b = appendStructKey(ctx, code, b) code = code.Next store(ctxptr, code.Idx, p) case encoder.OpStructFieldArrayPtr: @@ -4332,17 +4273,21 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p += code.Offset code = code.Next store(ctxptr, code.Idx, p) - case encoder.OpStructEnd: - last := len(b) - 1 - if b[last] == ',' { - b[last] = '}' + case encoder.OpStructFieldOmitEmptyStruct: + p := load(ctxptr, code.HeadIdx) + p += code.Offset + if ptrToPtr(p) == 0 && code.IsNextOpPtrType { + code = code.NextField } else { - b = append(b, '}') + b = appendStructKey(ctx, code, b) + code = code.Next + store(ctxptr, code.Idx, p) } - b = appendComma(b) - code = code.Next case encoder.OpStructAnonymousEnd: code = code.Next + case encoder.OpStructEnd: + b = appendStructEndSkipLast(ctx, code, b) + code = code.Next case encoder.OpStructEndInt: p := load(ctxptr, code.HeadIdx) b = appendStructKey(ctx, code, b) @@ -4607,11 +4552,11 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.Next case encoder.OpStructEndFloat64: p := load(ctxptr, code.HeadIdx) - b = appendStructKey(ctx, code, b) v := ptrToFloat64(p + code.Offset) if math.IsInf(v, 0) || math.IsNaN(v) { return nil, errUnsupportedFloat(v) } + b = appendStructKey(ctx, code, b) b = appendFloat64(b, v) b = appendStructEnd(ctx, code, b) code = code.Next @@ -4787,8 +4732,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 { b = appendNull(b) } else { - v := ptrToString(p) - b = appendString(b, string(appendString([]byte{}, v))) + b = appendString(b, string(appendString([]byte{}, ptrToString(p)))) } b = appendStructEnd(ctx, code, b) code = code.Next @@ -4797,8 +4741,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p = ptrToNPtr(p+code.Offset, code.PtrNum) if p != 0 { b = appendStructKey(ctx, code, b) - v := ptrToString(p) - b = appendString(b, string(appendString([]byte{}, v))) + b = appendString(b, string(appendString([]byte{}, ptrToString(p)))) b = appendStructEnd(ctx, code, b) } else { b = appendStructEndSkipLast(ctx, code, b) diff --git a/internal/encoder/vm_debug/util.go b/internal/encoder/vm_debug/util.go index 8cda230..2c6e74f 100644 --- a/internal/encoder/vm_debug/util.go +++ b/internal/encoder/vm_debug/util.go @@ -2,12 +2,41 @@ package vm_debug import ( "encoding/json" + "fmt" "unsafe" "github.com/goccy/go-json/internal/encoder" "github.com/goccy/go-json/internal/runtime" ) +const uintptrSize = 4 << (^uintptr(0) >> 63) + +var ( + appendInt = encoder.AppendInt + appendUint = encoder.AppendUint + appendFloat32 = encoder.AppendFloat32 + appendFloat64 = encoder.AppendFloat64 + appendString = encoder.AppendString + appendByteSlice = encoder.AppendByteSlice + appendNumber = encoder.AppendNumber + errUnsupportedValue = encoder.ErrUnsupportedValue + errUnsupportedFloat = encoder.ErrUnsupportedFloat + mapiterinit = encoder.MapIterInit + mapiterkey = encoder.MapIterKey + mapitervalue = encoder.MapIterValue + mapiternext = encoder.MapIterNext + maplen = encoder.MapLen +) + +type emptyInterface struct { + typ *runtime.Type + ptr unsafe.Pointer +} + +func errUnimplementedOp(op encoder.OpType) error { + return fmt.Errorf("encoder (debug): opcode %s has not been implemented", op) +} + func load(base uintptr, idx uintptr) uintptr { addr := base + idx return **(**uintptr)(unsafe.Pointer(&addr)) @@ -76,6 +105,57 @@ func appendComma(b []byte) []byte { return append(b, ',') } +func appendColon(b []byte) []byte { + last := len(b) - 1 + b[last] = ':' + return b +} + +func appendMapKeyValue(ctx *encoder.RuntimeContext, code *encoder.Opcode, b, key, value []byte) []byte { + b = append(b, key...) + b[len(b)-1] = ':' + return append(b, value...) +} + +func appendMapEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { + b[len(b)-1] = '}' + b = append(b, ',') + return b +} + +func appendInterface(ctx *encoder.RuntimeContext, codeSet *encoder.OpcodeSet, opt encoder.Option, 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 + + bb, err := Run(ctx, b, ifaceCodeSet, opt) + 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, false) } @@ -84,14 +164,38 @@ func appendMarshalText(code *encoder.Opcode, b []byte, v interface{}) ([]byte, e return encoder.AppendMarshalText(code, b, v, false) } -func appendStructKey(_ *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { - return append(b, code.Key...) +func appendArrayHead(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { + return append(b, '[') +} + +func appendArrayEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { + last := len(b) - 1 + b[last] = ']' + return append(b, ',') +} + +func appendEmptyArray(b []byte) []byte { + return append(b, '[', ']', ',') +} + +func appendEmptyObject(b []byte) []byte { + return append(b, '{', '}', ',') +} + +func appendObjectEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { + last := len(b) - 1 + b[last] = '}' + return append(b, ',') } func appendStructHead(b []byte) []byte { return append(b, '{') } +func appendStructKey(_ *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { + return append(b, code.Key...) +} + func appendStructEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { return append(b, '}', ',') } @@ -104,3 +208,8 @@ func appendStructEndSkipLast(ctx *encoder.RuntimeContext, code *encoder.Opcode, } return appendStructEnd(ctx, code, b) } + +func restoreIndent(ctx *encoder.RuntimeContext, code *encoder.Opcode, ctxptr uintptr) {} +func storeIndent(ctxptr uintptr, code *encoder.Opcode, indent uintptr) {} +func appendMapKeyIndent(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { return b } +func appendArrayElemIndent(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { return b } diff --git a/internal/encoder/vm_debug/vm.go b/internal/encoder/vm_debug/vm.go index 328f0ad..4f344df 100644 --- a/internal/encoder/vm_debug/vm.go +++ b/internal/encoder/vm_debug/vm.go @@ -7,33 +7,8 @@ import ( "unsafe" "github.com/goccy/go-json/internal/encoder" - "github.com/goccy/go-json/internal/runtime" ) -const uintptrSize = 4 << (^uintptr(0) >> 63) - -var ( - appendInt = encoder.AppendInt - appendUint = encoder.AppendUint - appendFloat32 = encoder.AppendFloat32 - appendFloat64 = encoder.AppendFloat64 - appendString = encoder.AppendString - appendByteSlice = encoder.AppendByteSlice - appendNumber = encoder.AppendNumber - errUnsupportedValue = encoder.ErrUnsupportedValue - errUnsupportedFloat = encoder.ErrUnsupportedFloat - mapiterinit = encoder.MapIterInit - mapiterkey = encoder.MapIterKey - mapitervalue = encoder.MapIterValue - mapiternext = encoder.MapIterNext - maplen = encoder.MapLen -) - -type emptyInterface struct { - typ *runtime.Type - ptr unsafe.Pointer -} - func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt encoder.Option) ([]byte, error) { recursiveLevel := 0 ptrOffset := uintptr(0) @@ -62,7 +37,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt for { switch code.Op { default: - return nil, fmt.Errorf("encoder: opcode %s has not been implemented", code.Op) + return nil, errUnimplementedOp(code.Op) case encoder.OpPtr: p := load(ctxptr, code.Idx) code = code.Next @@ -134,7 +109,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt case encoder.OpFloat64: v := ptrToFloat64(load(ctxptr, code.Idx)) if math.IsInf(v, 0) || math.IsNaN(v) { - return nil, encoder.ErrUnsupportedFloat(v) + return nil, errUnsupportedFloat(v) } b = appendFloat64(b, v) b = appendComma(b) @@ -229,35 +204,10 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.Next break } - ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(iface)) - ifaceCodeSet, err := encoder.CompileToGetCodeSet(uintptr(unsafe.Pointer(iface.typ))) + bb, err := appendInterface(ctx, codeSet, opt, code, b, iface, ptrOffset) 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, opt) - if err != nil { - return nil, err - } - - ctx.Ptrs = oldPtrs ctxptr = ctx.Ptr() ctx.SeenPtr = ctx.SeenPtr[:len(ctx.SeenPtr)-1] @@ -340,11 +290,11 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt store(ctxptr, code.Length, uintptr(slice.Len)) store(ctxptr, code.Idx, uintptr(slice.Data)) if slice.Len > 0 { - b = append(b, '[') + b = appendArrayHead(ctx, code, b) code = code.Next store(ctxptr, code.Idx, uintptr(slice.Data)) } else { - b = append(b, '[', ']', ',') + b = appendEmptyArray(b) code = code.End.Next } case encoder.OpSliceElem: @@ -352,15 +302,14 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt length := load(ctxptr, code.Length) idx++ if idx < length { + b = appendArrayElemIndent(ctx, code, b) store(ctxptr, code.ElemIdx, idx) data := load(ctxptr, code.HeadIdx) size := code.Size code = code.Next store(ctxptr, code.Idx, data+idx*size) } else { - last := len(b) - 1 - b[last] = ']' - b = appendComma(b) + b = appendArrayEnd(ctx, code, b) code = code.End.Next } case encoder.OpArrayPtr: @@ -382,27 +331,26 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt break } if code.Length > 0 { - b = append(b, '[') + b = appendArrayHead(ctx, code, b) store(ctxptr, code.ElemIdx, 0) code = code.Next store(ctxptr, code.Idx, p) } else { - b = append(b, '[', ']', ',') + b = appendEmptyArray(b) code = code.End.Next } case encoder.OpArrayElem: idx := load(ctxptr, code.ElemIdx) idx++ if idx < code.Length { + b = appendArrayElemIndent(ctx, code, b) store(ctxptr, code.ElemIdx, idx) p := load(ctxptr, code.HeadIdx) size := code.Size code = code.Next store(ctxptr, code.Idx, p+idx*size) } else { - last := len(b) - 1 - b[last] = ']' - b = appendComma(b) + b = appendArrayEnd(ctx, code, b) code = code.End.Next } case encoder.OpMapPtr: @@ -426,11 +374,11 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt uptr := ptrToUnsafePtr(p) mlen := maplen(uptr) if mlen <= 0 { - b = append(b, '{', '}', ',') + b = appendEmptyObject(b) code = code.End.Next break } - b = append(b, '{') + b = appendStructHead(b) iter := mapiterinit(code.Type, uptr) ctx.KeepRefs = append(ctx.KeepRefs, iter) store(ctxptr, code.ElemIdx, 0) @@ -441,6 +389,8 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt mapCtx.Pos = append(mapCtx.Pos, len(b)) ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx)) store(ctxptr, code.End.MapPos, uintptr(unsafe.Pointer(mapCtx))) + } else { + b = appendMapKeyIndent(ctx, code.Next, b) } key := mapiterkey(iter) store(ctxptr, code.Next.Idx, uintptr(key)) @@ -451,16 +401,15 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt idx++ if (opt & encoder.UnorderedMapOption) != 0 { if idx < length { + b = appendMapKeyIndent(ctx, code, b) + store(ctxptr, code.ElemIdx, idx) ptr := load(ctxptr, code.MapIter) iter := ptrToUnsafePtr(ptr) - store(ctxptr, code.ElemIdx, idx) key := mapiterkey(iter) store(ctxptr, code.Next.Idx, uintptr(key)) code = code.Next } else { - last := len(b) - 1 - b[last] = '}' - b = appendComma(b) + b = appendObjectEnd(ctx, code, b) code = code.End.Next } } else { @@ -480,8 +429,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } case encoder.OpMapValue: if (opt & encoder.UnorderedMapOption) != 0 { - last := len(b) - 1 - b[last] = ':' + b = appendColon(b) } else { ptr := load(ctxptr, code.End.MapPos) mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(ptr)) @@ -516,12 +464,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt sort.Sort(mapCtx.Slice) buf := mapCtx.Buf for _, item := range mapCtx.Slice.Items { - buf = append(buf, item.Key...) - buf[len(buf)-1] = ':' - buf = append(buf, item.Value...) + buf = appendMapKeyValue(ctx, code, buf, item.Key, item.Value) } - buf[len(buf)-1] = '}' - buf = append(buf, ',') + buf = appendMapEnd(ctx, code, buf) b = b[:pos[0]] b = append(b, buf...) mapCtx.Buf = buf @@ -552,6 +497,8 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt offsetNum := ptrOffset / uintptrSize oldOffset := ptrOffset ptrOffset += code.Jmp.CurLen * uintptrSize + oldBaseIndent := ctx.BaseIndent + ctx.BaseIndent += code.Indent - 1 newLen := offsetNum + code.Jmp.CurLen + code.Jmp.NextLen if curlen < newLen { @@ -562,12 +509,14 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt 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)) code = c recursiveLevel++ case encoder.OpRecursiveEnd: recursiveLevel-- // restore ctxptr + restoreIndent(ctx, code, ctxptr) offset := load(ctxptr, code.Idx) ctx.SeenPtr = ctx.SeenPtr[:len(ctx.SeenPtr)-1] @@ -600,7 +549,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if !code.AnonymousHead { b = appendStructHead(b) } - if !code.AnonymousKey { + if !code.AnonymousKey && len(code.Key) > 0 { b = appendStructKey(ctx, code, b) } p += code.Offset @@ -1896,8 +1845,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendStructHead(b) } b = appendStructKey(ctx, code, b) - s := ptrToString(p + code.Offset) - b = appendString(b, string(appendString([]byte{}, s))) + b = appendString(b, string(appendString([]byte{}, ptrToString(p+code.Offset)))) b = appendComma(b) code = code.Next case encoder.OpStructPtrHeadOmitEmptyStringString: @@ -2417,11 +2365,11 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendStructHead(b) } v := ptrToBytes(p + code.Offset) - if v == nil { + if len(v) == 0 { code = code.NextField } else { b = appendStructKey(ctx, code, b) - b = appendByteSlice(b, ptrToBytes(p)) + b = appendByteSlice(b, v) b = appendComma(b) code = code.Next } @@ -3513,9 +3461,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldIntPtr: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -3593,9 +3541,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldUintPtr: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -3671,9 +3619,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldFloat32Ptr: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -3763,9 +3711,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldFloat64Ptr: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) b = appendComma(b) @@ -3841,8 +3789,8 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.Next case encoder.OpStructFieldStringString: p := load(ctxptr, code.HeadIdx) - b = appendStructKey(ctx, code, b) s := ptrToString(p + code.Offset) + b = appendStructKey(ctx, code, b) b = appendString(b, string(appendString([]byte{}, s))) b = appendComma(b) code = code.Next @@ -3856,9 +3804,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldStringPtr: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -3876,9 +3824,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldStringPtrString: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -3930,9 +3878,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldBoolPtr: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -3950,9 +3898,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldBoolPtrString: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -3989,9 +3937,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldBytesPtr: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -4055,9 +4003,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldNumberPtr: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -4082,9 +4030,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldNumberPtrString: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -4247,9 +4195,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.Next store(ctxptr, code.Idx, p) case encoder.OpStructFieldOmitEmptyArray: + b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p += code.Offset - b = appendStructKey(ctx, code, b) code = code.Next store(ctxptr, code.Idx, p) case encoder.OpStructFieldArrayPtr: @@ -4345,17 +4293,21 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p += code.Offset code = code.Next store(ctxptr, code.Idx, p) - case encoder.OpStructEnd: - last := len(b) - 1 - if b[last] == ',' { - b[last] = '}' + case encoder.OpStructFieldOmitEmptyStruct: + p := load(ctxptr, code.HeadIdx) + p += code.Offset + if ptrToPtr(p) == 0 && code.IsNextOpPtrType { + code = code.NextField } else { - b = append(b, '}') + b = appendStructKey(ctx, code, b) + code = code.Next + store(ctxptr, code.Idx, p) } - b = appendComma(b) - code = code.Next case encoder.OpStructAnonymousEnd: code = code.Next + case encoder.OpStructEnd: + b = appendStructEndSkipLast(ctx, code, b) + code = code.Next case encoder.OpStructEndInt: p := load(ctxptr, code.HeadIdx) b = appendStructKey(ctx, code, b) @@ -4620,11 +4572,11 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.Next case encoder.OpStructEndFloat64: p := load(ctxptr, code.HeadIdx) - b = appendStructKey(ctx, code, b) v := ptrToFloat64(p + code.Offset) if math.IsInf(v, 0) || math.IsNaN(v) { return nil, errUnsupportedFloat(v) } + b = appendStructKey(ctx, code, b) b = appendFloat64(b, v) b = appendStructEnd(ctx, code, b) code = code.Next @@ -4800,8 +4752,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 { b = appendNull(b) } else { - v := ptrToString(p) - b = appendString(b, string(appendString([]byte{}, v))) + b = appendString(b, string(appendString([]byte{}, ptrToString(p)))) } b = appendStructEnd(ctx, code, b) code = code.Next @@ -4810,8 +4761,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p = ptrToNPtr(p+code.Offset, code.PtrNum) if p != 0 { b = appendStructKey(ctx, code, b) - v := ptrToString(p) - b = appendString(b, string(appendString([]byte{}, v))) + b = appendString(b, string(appendString([]byte{}, ptrToString(p)))) b = appendStructEnd(ctx, code, b) } else { b = appendStructEndSkipLast(ctx, code, b) diff --git a/internal/encoder/vm_escaped/hack.go b/internal/encoder/vm_escaped/hack.go new file mode 100644 index 0000000..d6c1997 --- /dev/null +++ b/internal/encoder/vm_escaped/hack.go @@ -0,0 +1,9 @@ +package vm_escaped + +import ( + // HACK: compile order + // `vm`, `vm_escaped`, `vm_indent`, `vm_escaped_indent`, `vm_debug` packages uses a lot of memory to compile, + // so forcibly make dependencies and avoid compiling in concurrent. + // dependency order: vm => vm_escaped => vm_indent => vm_escaped_indent => vm_debug + _ "github.com/goccy/go-json/internal/encoder/vm_indent" +) diff --git a/internal/encoder/vm_escaped/util.go b/internal/encoder/vm_escaped/util.go index a32d7bf..a09dbb7 100644 --- a/internal/encoder/vm_escaped/util.go +++ b/internal/encoder/vm_escaped/util.go @@ -2,12 +2,41 @@ package vm_escaped import ( "encoding/json" + "fmt" "unsafe" "github.com/goccy/go-json/internal/encoder" "github.com/goccy/go-json/internal/runtime" ) +const uintptrSize = 4 << (^uintptr(0) >> 63) + +var ( + appendInt = encoder.AppendInt + appendUint = encoder.AppendUint + appendFloat32 = encoder.AppendFloat32 + appendFloat64 = encoder.AppendFloat64 + appendString = encoder.AppendEscapedString + appendByteSlice = encoder.AppendByteSlice + appendNumber = encoder.AppendNumber + errUnsupportedValue = encoder.ErrUnsupportedValue + errUnsupportedFloat = encoder.ErrUnsupportedFloat + mapiterinit = encoder.MapIterInit + mapiterkey = encoder.MapIterKey + mapitervalue = encoder.MapIterValue + mapiternext = encoder.MapIterNext + maplen = encoder.MapLen +) + +type emptyInterface struct { + typ *runtime.Type + ptr unsafe.Pointer +} + +func errUnimplementedOp(op encoder.OpType) error { + return fmt.Errorf("encoder (escaped): opcode %s has not been implemented", op) +} + func load(base uintptr, idx uintptr) uintptr { addr := base + idx return **(**uintptr)(unsafe.Pointer(&addr)) @@ -76,6 +105,57 @@ func appendComma(b []byte) []byte { return append(b, ',') } +func appendColon(b []byte) []byte { + last := len(b) - 1 + b[last] = ':' + return b +} + +func appendMapKeyValue(ctx *encoder.RuntimeContext, code *encoder.Opcode, b, key, value []byte) []byte { + b = append(b, key...) + b[len(b)-1] = ':' + return append(b, value...) +} + +func appendMapEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { + b[len(b)-1] = '}' + b = append(b, ',') + return b +} + +func appendInterface(ctx *encoder.RuntimeContext, codeSet *encoder.OpcodeSet, opt encoder.Option, 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 + + bb, err := Run(ctx, b, ifaceCodeSet, opt) + 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, true) } @@ -84,6 +164,30 @@ func appendMarshalText(code *encoder.Opcode, b []byte, v interface{}) ([]byte, e return encoder.AppendMarshalText(code, b, v, true) } +func appendArrayHead(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { + return append(b, '[') +} + +func appendArrayEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { + last := len(b) - 1 + b[last] = ']' + return append(b, ',') +} + +func appendEmptyArray(b []byte) []byte { + return append(b, '[', ']', ',') +} + +func appendEmptyObject(b []byte) []byte { + return append(b, '{', '}', ',') +} + +func appendObjectEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { + last := len(b) - 1 + b[last] = '}' + return append(b, ',') +} + func appendStructHead(b []byte) []byte { return append(b, '{') } @@ -104,3 +208,8 @@ func appendStructEndSkipLast(ctx *encoder.RuntimeContext, code *encoder.Opcode, } return appendStructEnd(ctx, code, b) } + +func restoreIndent(ctx *encoder.RuntimeContext, code *encoder.Opcode, ctxptr uintptr) {} +func storeIndent(ctxptr uintptr, code *encoder.Opcode, indent uintptr) {} +func appendMapKeyIndent(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { return b } +func appendArrayElemIndent(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { return b } diff --git a/internal/encoder/vm_escaped/vm.go b/internal/encoder/vm_escaped/vm.go index f2851bb..891fa59 100644 --- a/internal/encoder/vm_escaped/vm.go +++ b/internal/encoder/vm_escaped/vm.go @@ -1,45 +1,13 @@ package vm_escaped import ( - "fmt" "math" "sort" "unsafe" "github.com/goccy/go-json/internal/encoder" - "github.com/goccy/go-json/internal/runtime" - - // HACK: compile order - // `vm`, `vm_escaped`, `vm_indent`, `vm_escaped_indent`, `vm_debug` packages uses a lot of memory to compile, - // so forcibly make dependencies and avoid compiling in concurrent. - // dependency order: vm => vm_escaped => vm_indent => vm_escaped_indent => vm_debug - _ "github.com/goccy/go-json/internal/encoder/vm_indent" ) -const uintptrSize = 4 << (^uintptr(0) >> 63) - -var ( - appendInt = encoder.AppendInt - appendUint = encoder.AppendUint - appendFloat32 = encoder.AppendFloat32 - appendFloat64 = encoder.AppendFloat64 - appendString = encoder.AppendEscapedString - appendByteSlice = encoder.AppendByteSlice - appendNumber = encoder.AppendNumber - errUnsupportedValue = encoder.ErrUnsupportedValue - errUnsupportedFloat = encoder.ErrUnsupportedFloat - mapiterinit = encoder.MapIterInit - mapiterkey = encoder.MapIterKey - mapitervalue = encoder.MapIterValue - mapiternext = encoder.MapIterNext - maplen = encoder.MapLen -) - -type emptyInterface struct { - typ *runtime.Type - ptr unsafe.Pointer -} - func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt encoder.Option) ([]byte, error) { recursiveLevel := 0 ptrOffset := uintptr(0) @@ -49,7 +17,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt for { switch code.Op { default: - return nil, fmt.Errorf("encoder: opcode %s has not been implemented", code.Op) + return nil, errUnimplementedOp(code.Op) case encoder.OpPtr: p := load(ctxptr, code.Idx) code = code.Next @@ -121,7 +89,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt case encoder.OpFloat64: v := ptrToFloat64(load(ctxptr, code.Idx)) if math.IsInf(v, 0) || math.IsNaN(v) { - return nil, encoder.ErrUnsupportedFloat(v) + return nil, errUnsupportedFloat(v) } b = appendFloat64(b, v) b = appendComma(b) @@ -216,35 +184,10 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.Next break } - ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(iface)) - ifaceCodeSet, err := encoder.CompileToGetCodeSet(uintptr(unsafe.Pointer(iface.typ))) + bb, err := appendInterface(ctx, codeSet, opt, code, b, iface, ptrOffset) 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, opt) - if err != nil { - return nil, err - } - - ctx.Ptrs = oldPtrs ctxptr = ctx.Ptr() ctx.SeenPtr = ctx.SeenPtr[:len(ctx.SeenPtr)-1] @@ -327,11 +270,11 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt store(ctxptr, code.Length, uintptr(slice.Len)) store(ctxptr, code.Idx, uintptr(slice.Data)) if slice.Len > 0 { - b = append(b, '[') + b = appendArrayHead(ctx, code, b) code = code.Next store(ctxptr, code.Idx, uintptr(slice.Data)) } else { - b = append(b, '[', ']', ',') + b = appendEmptyArray(b) code = code.End.Next } case encoder.OpSliceElem: @@ -339,15 +282,14 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt length := load(ctxptr, code.Length) idx++ if idx < length { + b = appendArrayElemIndent(ctx, code, b) store(ctxptr, code.ElemIdx, idx) data := load(ctxptr, code.HeadIdx) size := code.Size code = code.Next store(ctxptr, code.Idx, data+idx*size) } else { - last := len(b) - 1 - b[last] = ']' - b = appendComma(b) + b = appendArrayEnd(ctx, code, b) code = code.End.Next } case encoder.OpArrayPtr: @@ -369,27 +311,26 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt break } if code.Length > 0 { - b = append(b, '[') + b = appendArrayHead(ctx, code, b) store(ctxptr, code.ElemIdx, 0) code = code.Next store(ctxptr, code.Idx, p) } else { - b = append(b, '[', ']', ',') + b = appendEmptyArray(b) code = code.End.Next } case encoder.OpArrayElem: idx := load(ctxptr, code.ElemIdx) idx++ if idx < code.Length { + b = appendArrayElemIndent(ctx, code, b) store(ctxptr, code.ElemIdx, idx) p := load(ctxptr, code.HeadIdx) size := code.Size code = code.Next store(ctxptr, code.Idx, p+idx*size) } else { - last := len(b) - 1 - b[last] = ']' - b = appendComma(b) + b = appendArrayEnd(ctx, code, b) code = code.End.Next } case encoder.OpMapPtr: @@ -413,11 +354,11 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt uptr := ptrToUnsafePtr(p) mlen := maplen(uptr) if mlen <= 0 { - b = append(b, '{', '}', ',') + b = appendEmptyObject(b) code = code.End.Next break } - b = append(b, '{') + b = appendStructHead(b) iter := mapiterinit(code.Type, uptr) ctx.KeepRefs = append(ctx.KeepRefs, iter) store(ctxptr, code.ElemIdx, 0) @@ -428,6 +369,8 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt mapCtx.Pos = append(mapCtx.Pos, len(b)) ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx)) store(ctxptr, code.End.MapPos, uintptr(unsafe.Pointer(mapCtx))) + } else { + b = appendMapKeyIndent(ctx, code.Next, b) } key := mapiterkey(iter) store(ctxptr, code.Next.Idx, uintptr(key)) @@ -438,16 +381,15 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt idx++ if (opt & encoder.UnorderedMapOption) != 0 { if idx < length { + b = appendMapKeyIndent(ctx, code, b) + store(ctxptr, code.ElemIdx, idx) ptr := load(ctxptr, code.MapIter) iter := ptrToUnsafePtr(ptr) - store(ctxptr, code.ElemIdx, idx) key := mapiterkey(iter) store(ctxptr, code.Next.Idx, uintptr(key)) code = code.Next } else { - last := len(b) - 1 - b[last] = '}' - b = appendComma(b) + b = appendObjectEnd(ctx, code, b) code = code.End.Next } } else { @@ -467,8 +409,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } case encoder.OpMapValue: if (opt & encoder.UnorderedMapOption) != 0 { - last := len(b) - 1 - b[last] = ':' + b = appendColon(b) } else { ptr := load(ctxptr, code.End.MapPos) mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(ptr)) @@ -503,12 +444,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt sort.Sort(mapCtx.Slice) buf := mapCtx.Buf for _, item := range mapCtx.Slice.Items { - buf = append(buf, item.Key...) - buf[len(buf)-1] = ':' - buf = append(buf, item.Value...) + buf = appendMapKeyValue(ctx, code, buf, item.Key, item.Value) } - buf[len(buf)-1] = '}' - buf = append(buf, ',') + buf = appendMapEnd(ctx, code, buf) b = b[:pos[0]] b = append(b, buf...) mapCtx.Buf = buf @@ -539,6 +477,8 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt offsetNum := ptrOffset / uintptrSize oldOffset := ptrOffset ptrOffset += code.Jmp.CurLen * uintptrSize + oldBaseIndent := ctx.BaseIndent + ctx.BaseIndent += code.Indent - 1 newLen := offsetNum + code.Jmp.CurLen + code.Jmp.NextLen if curlen < newLen { @@ -549,12 +489,14 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt 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)) code = c recursiveLevel++ case encoder.OpRecursiveEnd: recursiveLevel-- // restore ctxptr + restoreIndent(ctx, code, ctxptr) offset := load(ctxptr, code.Idx) ctx.SeenPtr = ctx.SeenPtr[:len(ctx.SeenPtr)-1] @@ -587,7 +529,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if !code.AnonymousHead { b = appendStructHead(b) } - if !code.AnonymousKey { + if !code.AnonymousKey && len(code.Key) > 0 { b = appendStructKey(ctx, code, b) } p += code.Offset @@ -1883,8 +1825,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendStructHead(b) } b = appendStructKey(ctx, code, b) - s := ptrToString(p + code.Offset) - b = appendString(b, string(appendString([]byte{}, s))) + b = appendString(b, string(appendString([]byte{}, ptrToString(p+code.Offset)))) b = appendComma(b) code = code.Next case encoder.OpStructPtrHeadOmitEmptyStringString: @@ -2404,11 +2345,11 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendStructHead(b) } v := ptrToBytes(p + code.Offset) - if v == nil { + if len(v) == 0 { code = code.NextField } else { b = appendStructKey(ctx, code, b) - b = appendByteSlice(b, ptrToBytes(p)) + b = appendByteSlice(b, v) b = appendComma(b) code = code.Next } @@ -3500,9 +3441,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldIntPtr: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -3580,9 +3521,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldUintPtr: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -3658,9 +3599,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldFloat32Ptr: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -3750,9 +3691,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldFloat64Ptr: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) b = appendComma(b) @@ -3828,8 +3769,8 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.Next case encoder.OpStructFieldStringString: p := load(ctxptr, code.HeadIdx) - b = appendStructKey(ctx, code, b) s := ptrToString(p + code.Offset) + b = appendStructKey(ctx, code, b) b = appendString(b, string(appendString([]byte{}, s))) b = appendComma(b) code = code.Next @@ -3843,9 +3784,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldStringPtr: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -3863,9 +3804,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldStringPtrString: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -3917,9 +3858,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldBoolPtr: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -3937,9 +3878,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldBoolPtrString: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -3976,9 +3917,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldBytesPtr: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -4042,9 +3983,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldNumberPtr: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -4069,9 +4010,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructFieldNumberPtrString: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p = ptrToNPtr(p+code.Offset, code.PtrNum) + b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) } else { @@ -4234,9 +4175,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.Next store(ctxptr, code.Idx, p) case encoder.OpStructFieldOmitEmptyArray: + b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p += code.Offset - b = appendStructKey(ctx, code, b) code = code.Next store(ctxptr, code.Idx, p) case encoder.OpStructFieldArrayPtr: @@ -4332,17 +4273,21 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p += code.Offset code = code.Next store(ctxptr, code.Idx, p) - case encoder.OpStructEnd: - last := len(b) - 1 - if b[last] == ',' { - b[last] = '}' + case encoder.OpStructFieldOmitEmptyStruct: + p := load(ctxptr, code.HeadIdx) + p += code.Offset + if ptrToPtr(p) == 0 && code.IsNextOpPtrType { + code = code.NextField } else { - b = append(b, '}') + b = appendStructKey(ctx, code, b) + code = code.Next + store(ctxptr, code.Idx, p) } - b = appendComma(b) - code = code.Next case encoder.OpStructAnonymousEnd: code = code.Next + case encoder.OpStructEnd: + b = appendStructEndSkipLast(ctx, code, b) + code = code.Next case encoder.OpStructEndInt: p := load(ctxptr, code.HeadIdx) b = appendStructKey(ctx, code, b) @@ -4607,11 +4552,11 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.Next case encoder.OpStructEndFloat64: p := load(ctxptr, code.HeadIdx) - b = appendStructKey(ctx, code, b) v := ptrToFloat64(p + code.Offset) if math.IsInf(v, 0) || math.IsNaN(v) { return nil, errUnsupportedFloat(v) } + b = appendStructKey(ctx, code, b) b = appendFloat64(b, v) b = appendStructEnd(ctx, code, b) code = code.Next @@ -4787,8 +4732,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 { b = appendNull(b) } else { - v := ptrToString(p) - b = appendString(b, string(appendString([]byte{}, v))) + b = appendString(b, string(appendString([]byte{}, ptrToString(p)))) } b = appendStructEnd(ctx, code, b) code = code.Next @@ -4797,8 +4741,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p = ptrToNPtr(p+code.Offset, code.PtrNum) if p != 0 { b = appendStructKey(ctx, code, b) - v := ptrToString(p) - b = appendString(b, string(appendString([]byte{}, v))) + b = appendString(b, string(appendString([]byte{}, ptrToString(p)))) b = appendStructEnd(ctx, code, b) } else { b = appendStructEndSkipLast(ctx, code, b) diff --git a/internal/encoder/vm_escaped_indent/hack.go b/internal/encoder/vm_escaped_indent/hack.go new file mode 100644 index 0000000..a3672d9 --- /dev/null +++ b/internal/encoder/vm_escaped_indent/hack.go @@ -0,0 +1,9 @@ +package vm_escaped_indent + +import ( + // HACK: compile order + // `vm`, `vm_escaped`, `vm_indent`, `vm_escaped_indent`, `vm_debug` packages uses a lot of memory to compile, + // so forcibly make dependencies and avoid compiling in concurrent. + // dependency order: vm => vm_escaped => vm_indent => vm_escaped_indent => vm_debug + _ "github.com/goccy/go-json/internal/encoder/vm_debug" +) diff --git a/internal/encoder/vm_escaped_indent/util.go b/internal/encoder/vm_escaped_indent/util.go index 70e008d..60d73e0 100644 --- a/internal/encoder/vm_escaped_indent/util.go +++ b/internal/encoder/vm_escaped_indent/util.go @@ -2,12 +2,43 @@ package vm_escaped_indent import ( "encoding/json" + "fmt" "unsafe" "github.com/goccy/go-json/internal/encoder" "github.com/goccy/go-json/internal/runtime" ) +const uintptrSize = 4 << (^uintptr(0) >> 63) + +var ( + appendInt = encoder.AppendInt + appendUint = encoder.AppendUint + appendFloat32 = encoder.AppendFloat32 + appendFloat64 = encoder.AppendFloat64 + appendString = encoder.AppendString + appendByteSlice = encoder.AppendByteSlice + appendNumber = encoder.AppendNumber + appendStructEnd = encoder.AppendStructEndIndent + appendIndent = encoder.AppendIndent + errUnsupportedValue = encoder.ErrUnsupportedValue + errUnsupportedFloat = encoder.ErrUnsupportedFloat + mapiterinit = encoder.MapIterInit + mapiterkey = encoder.MapIterKey + mapitervalue = encoder.MapIterValue + mapiternext = encoder.MapIterNext + maplen = encoder.MapLen +) + +type emptyInterface struct { + typ *runtime.Type + ptr unsafe.Pointer +} + +func errUnimplementedOp(op encoder.OpType) error { + return fmt.Errorf("encoder (indent+escaped): opcode %s has not been implemented", op) +} + func load(base uintptr, idx uintptr) uintptr { addr := base + idx return **(**uintptr)(unsafe.Pointer(&addr)) @@ -76,6 +107,89 @@ func appendComma(b []byte) []byte { return append(b, ',', '\n') } +func appendColon(b []byte) []byte { + return append(b, ':', ' ') +} + +func appendInterface(ctx *encoder.RuntimeContext, codeSet *encoder.OpcodeSet, opt encoder.Option, 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, opt) + 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...) + b[len(b)-2] = ':' + b[len(b)-1] = ' ' + return append(b, value...) +} + +func appendMapEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { + b = b[:len(b)-2] + b = append(b, '\n') + b = appendIndent(ctx, b, code.Indent) + return append(b, '}', ',', '\n') +} + +func appendArrayHead(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { + b = append(b, '[', '\n') + return appendIndent(ctx, b, code.Indent+1) +} + +func appendArrayEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { + b = b[:len(b)-2] + b = append(b, '\n') + b = appendIndent(ctx, b, code.Indent) + return append(b, ']', ',', '\n') +} + +func appendEmptyArray(b []byte) []byte { + return append(b, '[', ']', ',', '\n') +} + +func appendEmptyObject(b []byte) []byte { + return append(b, '{', '}', ',', '\n') +} + +func appendObjectEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { + last := len(b) - 1 + b[last] = '\n' + b = appendIndent(ctx, b, code.Indent-1) + return append(b, '}', ',', '\n') +} + func appendMarshalJSON(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) { return encoder.AppendMarshalJSONIndent(ctx, code, b, v, true) } @@ -109,3 +223,19 @@ func appendStructEndSkipLast(ctx *encoder.RuntimeContext, code *encoder.Opcode, } return appendComma(b) } + +func restoreIndent(ctx *encoder.RuntimeContext, code *encoder.Opcode, ctxptr uintptr) { + ctx.BaseIndent = int(load(ctxptr, code.Length)) +} + +func storeIndent(ctxptr uintptr, code *encoder.Opcode, indent uintptr) { + store(ctxptr, code.End.Next.Length, indent) +} + +func appendArrayElemIndent(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { + return appendIndent(ctx, b, code.Indent+1) +} + +func appendMapKeyIndent(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { + return appendIndent(ctx, b, code.Indent) +} diff --git a/internal/encoder/vm_escaped_indent/vm.go b/internal/encoder/vm_escaped_indent/vm.go index 805994b..75aa3cb 100644 --- a/internal/encoder/vm_escaped_indent/vm.go +++ b/internal/encoder/vm_escaped_indent/vm.go @@ -1,47 +1,13 @@ package vm_escaped_indent import ( - "fmt" "math" "sort" "unsafe" "github.com/goccy/go-json/internal/encoder" - "github.com/goccy/go-json/internal/runtime" - - // HACK: compile order - // `vm`, `vm_escaped`, `vm_indent`, `vm_escaped_indent`, `vm_debug` packages uses a lot of memory to compile, - // so forcibly make dependencies and avoid compiling in concurrent. - // dependency order: vm => vm_escaped => vm_indent => vm_escaped_indent => vm_debug - _ "github.com/goccy/go-json/internal/encoder/vm_debug" ) -const uintptrSize = 4 << (^uintptr(0) >> 63) - -var ( - appendInt = encoder.AppendInt - appendUint = encoder.AppendUint - appendFloat32 = encoder.AppendFloat32 - appendFloat64 = encoder.AppendFloat64 - appendString = encoder.AppendEscapedString - appendByteSlice = encoder.AppendByteSlice - appendNumber = encoder.AppendNumber - appendStructEnd = encoder.AppendStructEndIndent - appendIndent = encoder.AppendIndent - errUnsupportedValue = encoder.ErrUnsupportedValue - errUnsupportedFloat = encoder.ErrUnsupportedFloat - mapiterinit = encoder.MapIterInit - mapiterkey = encoder.MapIterKey - mapitervalue = encoder.MapIterValue - mapiternext = encoder.MapIterNext - maplen = encoder.MapLen -) - -type emptyInterface struct { - typ *runtime.Type - ptr unsafe.Pointer -} - func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt encoder.Option) ([]byte, error) { recursiveLevel := 0 ptrOffset := uintptr(0) @@ -51,7 +17,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt for { switch code.Op { default: - return nil, fmt.Errorf("encoder (indent): opcode %s has not been implemented", code.Op) + return nil, errUnimplementedOp(code.Op) case encoder.OpPtr: p := load(ctxptr, code.Idx) code = code.Next @@ -218,38 +184,10 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.Next break } - ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(iface)) - ifaceCodeSet, err := encoder.CompileToGetCodeSet(uintptr(unsafe.Pointer(iface.typ))) + bb, err := appendInterface(ctx, codeSet, opt, code, b, iface, ptrOffset) 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, opt) - if err != nil { - return nil, err - } - ctx.BaseIndent = oldBaseIndent - - ctx.Ptrs = oldPtrs ctxptr = ctx.Ptr() ctx.SeenPtr = ctx.SeenPtr[:len(ctx.SeenPtr)-1] @@ -332,12 +270,11 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt store(ctxptr, code.Length, uintptr(slice.Len)) store(ctxptr, code.Idx, uintptr(slice.Data)) if slice.Len > 0 { - b = append(b, '[', '\n') - b = appendIndent(ctx, b, code.Indent+1) + b = appendArrayHead(ctx, code, b) code = code.Next store(ctxptr, code.Idx, uintptr(slice.Data)) } else { - b = append(b, '[', ']', ',', '\n') + b = appendEmptyArray(b) code = code.End.Next } case encoder.OpSliceElem: @@ -345,17 +282,14 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt length := load(ctxptr, code.Length) idx++ if idx < length { - b = appendIndent(ctx, b, code.Indent+1) + b = appendArrayElemIndent(ctx, code, b) store(ctxptr, code.ElemIdx, idx) data := load(ctxptr, code.HeadIdx) size := code.Size code = code.Next store(ctxptr, code.Idx, data+idx*size) } else { - b = b[:len(b)-2] - b = append(b, '\n') - b = appendIndent(ctx, b, code.Indent) - b = append(b, ']', ',', '\n') + b = appendArrayEnd(ctx, code, b) code = code.End.Next } case encoder.OpArrayPtr: @@ -377,30 +311,26 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt break } if code.Length > 0 { - b = append(b, '[', '\n') - b = appendIndent(ctx, b, code.Indent+1) + b = appendArrayHead(ctx, code, b) store(ctxptr, code.ElemIdx, 0) code = code.Next store(ctxptr, code.Idx, p) } else { - b = append(b, '[', ']', ',', '\n') + b = appendEmptyArray(b) code = code.End.Next } case encoder.OpArrayElem: idx := load(ctxptr, code.ElemIdx) idx++ if idx < code.Length { - b = appendIndent(ctx, b, code.Indent+1) + b = appendArrayElemIndent(ctx, code, b) store(ctxptr, code.ElemIdx, idx) p := load(ctxptr, code.HeadIdx) size := code.Size code = code.Next store(ctxptr, code.Idx, p+idx*size) } else { - b = b[:len(b)-2] - b = append(b, '\n') - b = appendIndent(ctx, b, code.Indent) - b = append(b, ']', ',', '\n') + b = appendArrayEnd(ctx, code, b) code = code.End.Next } case encoder.OpMapPtr: @@ -424,7 +354,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt uptr := ptrToUnsafePtr(p) mlen := maplen(uptr) if mlen <= 0 { - b = append(b, '{', '}', ',', '\n') + b = appendEmptyObject(b) code = code.End.Next break } @@ -440,7 +370,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx)) store(ctxptr, code.End.MapPos, uintptr(unsafe.Pointer(mapCtx))) } else { - b = appendIndent(ctx, b, code.Next.Indent) + b = appendMapKeyIndent(ctx, code.Next, b) } key := mapiterkey(iter) store(ctxptr, code.Next.Idx, uintptr(key)) @@ -451,7 +381,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt idx++ if (opt & encoder.UnorderedMapOption) != 0 { if idx < length { - b = appendIndent(ctx, b, code.Indent) + b = appendMapKeyIndent(ctx, code, b) store(ctxptr, code.ElemIdx, idx) ptr := load(ctxptr, code.MapIter) iter := ptrToUnsafePtr(ptr) @@ -459,10 +389,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt store(ctxptr, code.Next.Idx, uintptr(key)) code = code.Next } else { - last := len(b) - 1 - b[last] = '\n' - b = appendIndent(ctx, b, code.Indent-1) - b = append(b, '}', ',', '\n') + b = appendObjectEnd(ctx, code, b) code = code.End.Next } } else { @@ -482,7 +409,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } case encoder.OpMapValue: if (opt & encoder.UnorderedMapOption) != 0 { - b = append(b, ':', ' ') + b = appendColon(b) } else { ptr := load(ctxptr, code.End.MapPos) mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(ptr)) @@ -495,7 +422,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt mapiternext(iter) code = code.Next case encoder.OpMapEnd: - // this operation only used by sorted map + // this operation only used by sorted map. length := int(load(ctxptr, code.Length)) ptr := load(ctxptr, code.MapPos) mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(ptr)) @@ -517,17 +444,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt sort.Sort(mapCtx.Slice) buf := mapCtx.Buf for _, item := range mapCtx.Slice.Items { - buf = appendIndent(ctx, buf, code.Indent+1) - buf = append(buf, item.Key...) - buf[len(buf)-2] = ':' - buf[len(buf)-1] = ' ' - buf = append(buf, item.Value...) + buf = appendMapKeyValue(ctx, code, buf, item.Key, item.Value) } - buf = buf[:len(buf)-2] - buf = append(buf, '\n') - buf = appendIndent(ctx, buf, code.Indent) - buf = append(buf, '}', ',', '\n') - + buf = appendMapEnd(ctx, code, buf) b = b[:pos[0]] b = append(b, buf...) mapCtx.Buf = buf @@ -570,14 +489,14 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt store(ctxptr, c.Idx, ptr) store(ctxptr, c.End.Next.Idx, oldOffset) store(ctxptr, c.End.Next.ElemIdx, uintptr(unsafe.Pointer(code.Next))) - store(ctxptr, c.End.Next.Length, uintptr(oldBaseIndent)) + storeIndent(ctxptr, c, uintptr(oldBaseIndent)) code = c recursiveLevel++ case encoder.OpRecursiveEnd: recursiveLevel-- // restore ctxptr - ctx.BaseIndent = int(load(ctxptr, code.Length)) + restoreIndent(ctx, code, ctxptr) offset := load(ctxptr, code.Idx) ctx.SeenPtr = ctx.SeenPtr[:len(ctx.SeenPtr)-1] @@ -1382,7 +1301,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } b = appendStructKey(ctx, code, b) if code.Indirect { - p = ptrToNPtr(p, code.PtrNum) + p = ptrToNPtr(p+code.Offset, code.PtrNum) } if p == 0 { b = appendNull(b) @@ -1562,12 +1481,12 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendStructHead(b) } v := ptrToFloat64(p + code.Offset) - if math.IsInf(v, 0) || math.IsNaN(v) { - return nil, errUnsupportedFloat(v) - } if v == 0 { code = code.NextField } else { + if math.IsInf(v, 0) || math.IsNaN(v) { + return nil, errUnsupportedFloat(v) + } b = appendStructKey(ctx, code, b) b = appendFloat64(b, v) b = appendComma(b) @@ -1600,12 +1519,12 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if !code.AnonymousHead { b = appendStructHead(b) } - b = appendStructKey(ctx, code, b) - b = append(b, '"') v := ptrToFloat64(p + code.Offset) if math.IsInf(v, 0) || math.IsNaN(v) { return nil, errUnsupportedFloat(v) } + b = appendStructKey(ctx, code, b) + b = append(b, '"') b = appendFloat64(b, v) b = append(b, '"') b = appendComma(b) @@ -1638,12 +1557,12 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendStructHead(b) } v := ptrToFloat64(p + code.Offset) - if math.IsInf(v, 0) || math.IsNaN(v) { - return nil, errUnsupportedFloat(v) - } if v == 0 { code = code.NextField } else { + if math.IsInf(v, 0) || math.IsNaN(v) { + return nil, errUnsupportedFloat(v) + } b = appendStructKey(ctx, code, b) b = append(b, '"') b = appendFloat64(b, v) @@ -1906,8 +1825,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendStructHead(b) } b = appendStructKey(ctx, code, b) - v := ptrToString(p + code.Offset) - b = appendString(b, string(appendString([]byte{}, v))) + b = appendString(b, string(appendString([]byte{}, ptrToString(p+code.Offset)))) b = appendComma(b) code = code.Next case encoder.OpStructPtrHeadOmitEmptyStringString: @@ -2251,12 +2169,12 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } b = appendStructKey(ctx, code, b) if code.Indirect { - p = ptrToNPtr(p, code.PtrNum) + p = ptrToNPtr(p+code.Offset, code.PtrNum) } if p == 0 { b = appendNull(b) } else { - b = appendBool(b, ptrToBool(p+code.Offset)) + b = appendBool(b, ptrToBool(p)) } b = appendComma(b) code = code.Next @@ -3778,13 +3696,15 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) - } else { - v := ptrToFloat64(p) - if math.IsInf(v, 0) || math.IsNaN(v) { - return nil, errUnsupportedFloat(v) - } - b = appendFloat64(b, v) + b = appendComma(b) + code = code.Next + break } + v := ptrToFloat64(p) + if math.IsInf(v, 0) || math.IsNaN(v) { + return nil, errUnsupportedFloat(v) + } + b = appendFloat64(b, v) b = appendComma(b) code = code.Next case encoder.OpStructFieldOmitEmptyFloat64Ptr: @@ -3807,11 +3727,11 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 { b = appendNull(b) } else { - b = append(b, '"') v := ptrToFloat64(p) if math.IsInf(v, 0) || math.IsNaN(v) { return nil, errUnsupportedFloat(v) } + b = append(b, '"') b = appendFloat64(b, v) b = append(b, '"') } @@ -4348,9 +4268,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.NextField } case encoder.OpStructFieldStruct: + b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p += code.Offset - b = appendStructKey(ctx, code, b) code = code.Next store(ctxptr, code.Idx, p) case encoder.OpStructFieldOmitEmptyStruct: @@ -4366,25 +4286,11 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt case encoder.OpStructAnonymousEnd: code = code.Next case encoder.OpStructEnd: - last := len(b) - 1 - if b[last-1] == '{' { - b[last] = '}' - b = appendComma(b) - code = code.Next - break - } - if b[last] == '\n' { - // to remove ',' and '\n' characters - b = b[:len(b)-2] - } - b = append(b, '\n') - b = appendIndent(ctx, b, code.Indent) - b = append(b, '}') - b = appendComma(b) + b = appendStructEndSkipLast(ctx, code, b) code = code.Next case encoder.OpStructEndInt: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) + b = appendStructKey(ctx, code, b) b = appendInt(b, ptrToUint64(p+code.Offset), code) b = appendStructEnd(ctx, code, b) code = code.Next @@ -4471,8 +4377,8 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructEndUint: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) + b = appendStructKey(ctx, code, b) b = appendUint(b, ptrToUint64(p+code.Offset), code) b = appendStructEnd(ctx, code, b) code = code.Next @@ -4559,8 +4465,8 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructEndFloat32: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) + b = appendStructKey(ctx, code, b) b = appendFloat32(b, ptrToFloat32(p+code.Offset)) b = appendStructEnd(ctx, code, b) code = code.Next @@ -4645,12 +4551,12 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructEndFloat64: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) v := ptrToFloat64(p + code.Offset) if math.IsInf(v, 0) || math.IsNaN(v) { return nil, errUnsupportedFloat(v) } + b = appendStructKey(ctx, code, b) b = appendFloat64(b, v) b = appendStructEnd(ctx, code, b) code = code.Next @@ -4658,10 +4564,10 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p := load(ctxptr, code.HeadIdx) v := ptrToFloat64(p + code.Offset) if v != 0 { - b = appendStructKey(ctx, code, b) if math.IsInf(v, 0) || math.IsNaN(v) { return nil, errUnsupportedFloat(v) } + b = appendStructKey(ctx, code, b) b = appendFloat64(b, v) b = appendStructEnd(ctx, code, b) } else { @@ -4684,11 +4590,11 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p := load(ctxptr, code.HeadIdx) v := ptrToFloat64(p + code.Offset) if v != 0 { - b = appendStructKey(ctx, code, b) - b = append(b, '"') if math.IsInf(v, 0) || math.IsNaN(v) { return nil, errUnsupportedFloat(v) } + b = appendStructKey(ctx, code, b) + b = append(b, '"') b = appendFloat64(b, v) b = append(b, '"') b = appendStructEnd(ctx, code, b) @@ -4702,13 +4608,15 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p = ptrToNPtr(p+code.Offset, code.PtrNum) if p == 0 { b = appendNull(b) - } else { - v := ptrToFloat64(p) - if math.IsInf(v, 0) || math.IsNaN(v) { - return nil, errUnsupportedFloat(v) - } - b = appendFloat64(b, v) + b = appendStructEnd(ctx, code, b) + code = code.Next + break } + v := ptrToFloat64(p) + if math.IsInf(v, 0) || math.IsNaN(v) { + return nil, errUnsupportedFloat(v) + } + b = appendFloat64(b, v) b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat64Ptr: @@ -4748,11 +4656,11 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p = ptrToNPtr(p+code.Offset, code.PtrNum) if p != 0 { b = appendStructKey(ctx, code, b) - b = append(b, '"') v := ptrToFloat64(p) if math.IsInf(v, 0) || math.IsNaN(v) { return nil, errUnsupportedFloat(v) } + b = append(b, '"') b = appendFloat64(b, v) b = append(b, '"') b = appendStructEnd(ctx, code, b) @@ -4761,8 +4669,8 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructEndString: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) + b = appendStructKey(ctx, code, b) b = appendString(b, ptrToString(p+code.Offset)) b = appendStructEnd(ctx, code, b) code = code.Next @@ -4840,8 +4748,8 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructEndBool: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) + b = appendStructKey(ctx, code, b) b = appendBool(b, ptrToBool(p+code.Offset)) b = appendStructEnd(ctx, code, b) code = code.Next @@ -4926,8 +4834,8 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructEndBytes: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) + b = appendStructKey(ctx, code, b) b = appendByteSlice(b, ptrToBytes(p+code.Offset)) b = appendStructEnd(ctx, code, b) code = code.Next @@ -4965,8 +4873,8 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructEndNumber: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) + b = appendStructKey(ctx, code, b) bb, err := appendNumber(b, ptrToNumber(p+code.Offset)) if err != nil { return nil, err @@ -4988,9 +4896,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructEndNumberString: + p := load(ctxptr, code.HeadIdx) b = appendStructKey(ctx, code, b) b = append(b, '"') - p := load(ctxptr, code.HeadIdx) bb, err := appendNumber(b, ptrToNumber(p+code.Offset)) if err != nil { return nil, err diff --git a/internal/encoder/vm_indent/hack.go b/internal/encoder/vm_indent/hack.go new file mode 100644 index 0000000..7838010 --- /dev/null +++ b/internal/encoder/vm_indent/hack.go @@ -0,0 +1,9 @@ +package vm_indent + +import ( + // HACK: compile order + // `vm`, `vm_escaped`, `vm_indent`, `vm_escaped_indent`, `vm_debug` packages uses a lot of memory to compile, + // so forcibly make dependencies and avoid compiling in concurrent. + // dependency order: vm => vm_escaped => vm_indent => vm_escaped_indent => vm_debug + _ "github.com/goccy/go-json/internal/encoder/vm_escaped_indent" +) diff --git a/internal/encoder/vm_indent/util.go b/internal/encoder/vm_indent/util.go index 8a74c08..d73fb7b 100644 --- a/internal/encoder/vm_indent/util.go +++ b/internal/encoder/vm_indent/util.go @@ -2,12 +2,43 @@ package vm_indent import ( "encoding/json" + "fmt" "unsafe" "github.com/goccy/go-json/internal/encoder" "github.com/goccy/go-json/internal/runtime" ) +const uintptrSize = 4 << (^uintptr(0) >> 63) + +var ( + appendInt = encoder.AppendInt + appendUint = encoder.AppendUint + appendFloat32 = encoder.AppendFloat32 + appendFloat64 = encoder.AppendFloat64 + appendString = encoder.AppendString + appendByteSlice = encoder.AppendByteSlice + appendNumber = encoder.AppendNumber + appendStructEnd = encoder.AppendStructEndIndent + appendIndent = encoder.AppendIndent + errUnsupportedValue = encoder.ErrUnsupportedValue + errUnsupportedFloat = encoder.ErrUnsupportedFloat + mapiterinit = encoder.MapIterInit + mapiterkey = encoder.MapIterKey + mapitervalue = encoder.MapIterValue + mapiternext = encoder.MapIterNext + maplen = encoder.MapLen +) + +type emptyInterface struct { + typ *runtime.Type + ptr unsafe.Pointer +} + +func errUnimplementedOp(op encoder.OpType) error { + return fmt.Errorf("encoder (indent): opcode %s has not been implemented", op) +} + func load(base uintptr, idx uintptr) uintptr { addr := base + idx return **(**uintptr)(unsafe.Pointer(&addr)) @@ -76,8 +107,87 @@ func appendComma(b []byte) []byte { return append(b, ',', '\n') } -func appendStructHead(b []byte) []byte { - return append(b, '{', '\n') +func appendColon(b []byte) []byte { + return append(b, ':', ' ') +} + +func appendInterface(ctx *encoder.RuntimeContext, codeSet *encoder.OpcodeSet, opt encoder.Option, 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, opt) + 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...) + b[len(b)-2] = ':' + b[len(b)-1] = ' ' + return append(b, value...) +} + +func appendMapEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { + b = b[:len(b)-2] + b = append(b, '\n') + b = appendIndent(ctx, b, code.Indent) + return append(b, '}', ',', '\n') +} + +func appendArrayHead(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { + b = append(b, '[', '\n') + return appendIndent(ctx, b, code.Indent+1) +} + +func appendArrayEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { + b = b[:len(b)-2] + b = append(b, '\n') + b = appendIndent(ctx, b, code.Indent) + return append(b, ']', ',', '\n') +} + +func appendEmptyArray(b []byte) []byte { + return append(b, '[', ']', ',', '\n') +} + +func appendEmptyObject(b []byte) []byte { + return append(b, '{', '}', ',', '\n') +} + +func appendObjectEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { + last := len(b) - 1 + b[last] = '\n' + b = appendIndent(ctx, b, code.Indent-1) + return append(b, '}', ',', '\n') } func appendMarshalJSON(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) { @@ -88,6 +198,10 @@ func appendMarshalText(code *encoder.Opcode, b []byte, v interface{}) ([]byte, e return encoder.AppendMarshalTextIndent(code, b, v, false) } +func appendStructHead(b []byte) []byte { + return append(b, '{', '\n') +} + func appendStructKey(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { b = appendIndent(ctx, b, code.Indent) b = append(b, code.Key...) @@ -109,3 +223,19 @@ func appendStructEndSkipLast(ctx *encoder.RuntimeContext, code *encoder.Opcode, } return appendComma(b) } + +func restoreIndent(ctx *encoder.RuntimeContext, code *encoder.Opcode, ctxptr uintptr) { + ctx.BaseIndent = int(load(ctxptr, code.Length)) +} + +func storeIndent(ctxptr uintptr, code *encoder.Opcode, indent uintptr) { + store(ctxptr, code.End.Next.Length, indent) +} + +func appendArrayElemIndent(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { + return appendIndent(ctx, b, code.Indent+1) +} + +func appendMapKeyIndent(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { + return appendIndent(ctx, b, code.Indent) +} diff --git a/internal/encoder/vm_indent/vm.go b/internal/encoder/vm_indent/vm.go index dfc7a30..4b47457 100644 --- a/internal/encoder/vm_indent/vm.go +++ b/internal/encoder/vm_indent/vm.go @@ -1,47 +1,13 @@ package vm_indent import ( - "fmt" "math" "sort" "unsafe" "github.com/goccy/go-json/internal/encoder" - "github.com/goccy/go-json/internal/runtime" - - // HACK: compile order - // `vm`, `vm_escaped`, `vm_indent`, `vm_escaped_indent`, `vm_debug` packages uses a lot of memory to compile, - // so forcibly make dependencies and avoid compiling in concurrent. - // dependency order: vm => vm_escaped => vm_indent => vm_escaped_indent => vm_debug - _ "github.com/goccy/go-json/internal/encoder/vm_escaped_indent" ) -const uintptrSize = 4 << (^uintptr(0) >> 63) - -var ( - appendInt = encoder.AppendInt - appendUint = encoder.AppendUint - appendFloat32 = encoder.AppendFloat32 - appendFloat64 = encoder.AppendFloat64 - appendString = encoder.AppendString - appendByteSlice = encoder.AppendByteSlice - appendNumber = encoder.AppendNumber - appendStructEnd = encoder.AppendStructEndIndent - appendIndent = encoder.AppendIndent - errUnsupportedValue = encoder.ErrUnsupportedValue - errUnsupportedFloat = encoder.ErrUnsupportedFloat - mapiterinit = encoder.MapIterInit - mapiterkey = encoder.MapIterKey - mapitervalue = encoder.MapIterValue - mapiternext = encoder.MapIterNext - maplen = encoder.MapLen -) - -type emptyInterface struct { - typ *runtime.Type - ptr unsafe.Pointer -} - func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt encoder.Option) ([]byte, error) { recursiveLevel := 0 ptrOffset := uintptr(0) @@ -51,7 +17,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt for { switch code.Op { default: - return nil, fmt.Errorf("encoder (indent): opcode %s has not been implemented", code.Op) + return nil, errUnimplementedOp(code.Op) case encoder.OpPtr: p := load(ctxptr, code.Idx) code = code.Next @@ -218,38 +184,10 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.Next break } - ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(iface)) - ifaceCodeSet, err := encoder.CompileToGetCodeSet(uintptr(unsafe.Pointer(iface.typ))) + bb, err := appendInterface(ctx, codeSet, opt, code, b, iface, ptrOffset) 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, opt) - if err != nil { - return nil, err - } - ctx.BaseIndent = oldBaseIndent - - ctx.Ptrs = oldPtrs ctxptr = ctx.Ptr() ctx.SeenPtr = ctx.SeenPtr[:len(ctx.SeenPtr)-1] @@ -332,12 +270,11 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt store(ctxptr, code.Length, uintptr(slice.Len)) store(ctxptr, code.Idx, uintptr(slice.Data)) if slice.Len > 0 { - b = append(b, '[', '\n') - b = appendIndent(ctx, b, code.Indent+1) + b = appendArrayHead(ctx, code, b) code = code.Next store(ctxptr, code.Idx, uintptr(slice.Data)) } else { - b = append(b, '[', ']', ',', '\n') + b = appendEmptyArray(b) code = code.End.Next } case encoder.OpSliceElem: @@ -345,17 +282,14 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt length := load(ctxptr, code.Length) idx++ if idx < length { - b = appendIndent(ctx, b, code.Indent+1) + b = appendArrayElemIndent(ctx, code, b) store(ctxptr, code.ElemIdx, idx) data := load(ctxptr, code.HeadIdx) size := code.Size code = code.Next store(ctxptr, code.Idx, data+idx*size) } else { - b = b[:len(b)-2] - b = append(b, '\n') - b = appendIndent(ctx, b, code.Indent) - b = append(b, ']', ',', '\n') + b = appendArrayEnd(ctx, code, b) code = code.End.Next } case encoder.OpArrayPtr: @@ -377,30 +311,26 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt break } if code.Length > 0 { - b = append(b, '[', '\n') - b = appendIndent(ctx, b, code.Indent+1) + b = appendArrayHead(ctx, code, b) store(ctxptr, code.ElemIdx, 0) code = code.Next store(ctxptr, code.Idx, p) } else { - b = append(b, '[', ']', ',', '\n') + b = appendEmptyArray(b) code = code.End.Next } case encoder.OpArrayElem: idx := load(ctxptr, code.ElemIdx) idx++ if idx < code.Length { - b = appendIndent(ctx, b, code.Indent+1) + b = appendArrayElemIndent(ctx, code, b) store(ctxptr, code.ElemIdx, idx) p := load(ctxptr, code.HeadIdx) size := code.Size code = code.Next store(ctxptr, code.Idx, p+idx*size) } else { - b = b[:len(b)-2] - b = append(b, '\n') - b = appendIndent(ctx, b, code.Indent) - b = append(b, ']', ',', '\n') + b = appendArrayEnd(ctx, code, b) code = code.End.Next } case encoder.OpMapPtr: @@ -424,7 +354,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt uptr := ptrToUnsafePtr(p) mlen := maplen(uptr) if mlen <= 0 { - b = append(b, '{', '}', ',', '\n') + b = appendEmptyObject(b) code = code.End.Next break } @@ -440,7 +370,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx)) store(ctxptr, code.End.MapPos, uintptr(unsafe.Pointer(mapCtx))) } else { - b = appendIndent(ctx, b, code.Next.Indent) + b = appendMapKeyIndent(ctx, code.Next, b) } key := mapiterkey(iter) store(ctxptr, code.Next.Idx, uintptr(key)) @@ -451,7 +381,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt idx++ if (opt & encoder.UnorderedMapOption) != 0 { if idx < length { - b = appendIndent(ctx, b, code.Indent) + b = appendMapKeyIndent(ctx, code, b) store(ctxptr, code.ElemIdx, idx) ptr := load(ctxptr, code.MapIter) iter := ptrToUnsafePtr(ptr) @@ -459,10 +389,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt store(ctxptr, code.Next.Idx, uintptr(key)) code = code.Next } else { - last := len(b) - 1 - b[last] = '\n' - b = appendIndent(ctx, b, code.Indent-1) - b = append(b, '}', ',', '\n') + b = appendObjectEnd(ctx, code, b) code = code.End.Next } } else { @@ -482,7 +409,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } case encoder.OpMapValue: if (opt & encoder.UnorderedMapOption) != 0 { - b = append(b, ':', ' ') + b = appendColon(b) } else { ptr := load(ctxptr, code.End.MapPos) mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(ptr)) @@ -495,7 +422,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt mapiternext(iter) code = code.Next case encoder.OpMapEnd: - // this operation only used by sorted map + // this operation only used by sorted map. length := int(load(ctxptr, code.Length)) ptr := load(ctxptr, code.MapPos) mapCtx := (*encoder.MapContext)(ptrToUnsafePtr(ptr)) @@ -517,17 +444,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt sort.Sort(mapCtx.Slice) buf := mapCtx.Buf for _, item := range mapCtx.Slice.Items { - buf = appendIndent(ctx, buf, code.Indent+1) - buf = append(buf, item.Key...) - buf[len(buf)-2] = ':' - buf[len(buf)-1] = ' ' - buf = append(buf, item.Value...) + buf = appendMapKeyValue(ctx, code, buf, item.Key, item.Value) } - buf = buf[:len(buf)-2] - buf = append(buf, '\n') - buf = appendIndent(ctx, buf, code.Indent) - buf = append(buf, '}', ',', '\n') - + buf = appendMapEnd(ctx, code, buf) b = b[:pos[0]] b = append(b, buf...) mapCtx.Buf = buf @@ -570,14 +489,14 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt store(ctxptr, c.Idx, ptr) store(ctxptr, c.End.Next.Idx, oldOffset) store(ctxptr, c.End.Next.ElemIdx, uintptr(unsafe.Pointer(code.Next))) - store(ctxptr, c.End.Next.Length, uintptr(oldBaseIndent)) + storeIndent(ctxptr, c, uintptr(oldBaseIndent)) code = c recursiveLevel++ case encoder.OpRecursiveEnd: recursiveLevel-- // restore ctxptr - ctx.BaseIndent = int(load(ctxptr, code.Length)) + restoreIndent(ctx, code, ctxptr) offset := load(ctxptr, code.Idx) ctx.SeenPtr = ctx.SeenPtr[:len(ctx.SeenPtr)-1] @@ -1382,7 +1301,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } b = appendStructKey(ctx, code, b) if code.Indirect { - p = ptrToNPtr(p, code.PtrNum) + p = ptrToNPtr(p+code.Offset, code.PtrNum) } if p == 0 { b = appendNull(b) @@ -1562,12 +1481,12 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendStructHead(b) } v := ptrToFloat64(p + code.Offset) - if math.IsInf(v, 0) || math.IsNaN(v) { - return nil, errUnsupportedFloat(v) - } if v == 0 { code = code.NextField } else { + if math.IsInf(v, 0) || math.IsNaN(v) { + return nil, errUnsupportedFloat(v) + } b = appendStructKey(ctx, code, b) b = appendFloat64(b, v) b = appendComma(b) @@ -1600,12 +1519,12 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if !code.AnonymousHead { b = appendStructHead(b) } - b = appendStructKey(ctx, code, b) - b = append(b, '"') v := ptrToFloat64(p + code.Offset) if math.IsInf(v, 0) || math.IsNaN(v) { return nil, errUnsupportedFloat(v) } + b = appendStructKey(ctx, code, b) + b = append(b, '"') b = appendFloat64(b, v) b = append(b, '"') b = appendComma(b) @@ -1638,12 +1557,12 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendStructHead(b) } v := ptrToFloat64(p + code.Offset) - if math.IsInf(v, 0) || math.IsNaN(v) { - return nil, errUnsupportedFloat(v) - } if v == 0 { code = code.NextField } else { + if math.IsInf(v, 0) || math.IsNaN(v) { + return nil, errUnsupportedFloat(v) + } b = appendStructKey(ctx, code, b) b = append(b, '"') b = appendFloat64(b, v) @@ -1906,8 +1825,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendStructHead(b) } b = appendStructKey(ctx, code, b) - v := ptrToString(p + code.Offset) - b = appendString(b, string(appendString([]byte{}, v))) + b = appendString(b, string(appendString([]byte{}, ptrToString(p+code.Offset)))) b = appendComma(b) code = code.Next case encoder.OpStructPtrHeadOmitEmptyStringString: @@ -2251,12 +2169,12 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } b = appendStructKey(ctx, code, b) if code.Indirect { - p = ptrToNPtr(p, code.PtrNum) + p = ptrToNPtr(p+code.Offset, code.PtrNum) } if p == 0 { b = appendNull(b) } else { - b = appendBool(b, ptrToBool(p+code.Offset)) + b = appendBool(b, ptrToBool(p)) } b = appendComma(b) code = code.Next @@ -3778,13 +3696,15 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt b = appendStructKey(ctx, code, b) if p == 0 { b = appendNull(b) - } else { - v := ptrToFloat64(p) - if math.IsInf(v, 0) || math.IsNaN(v) { - return nil, errUnsupportedFloat(v) - } - b = appendFloat64(b, v) + b = appendComma(b) + code = code.Next + break } + v := ptrToFloat64(p) + if math.IsInf(v, 0) || math.IsNaN(v) { + return nil, errUnsupportedFloat(v) + } + b = appendFloat64(b, v) b = appendComma(b) code = code.Next case encoder.OpStructFieldOmitEmptyFloat64Ptr: @@ -3807,11 +3727,11 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt if p == 0 { b = appendNull(b) } else { - b = append(b, '"') v := ptrToFloat64(p) if math.IsInf(v, 0) || math.IsNaN(v) { return nil, errUnsupportedFloat(v) } + b = append(b, '"') b = appendFloat64(b, v) b = append(b, '"') } @@ -4348,9 +4268,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.NextField } case encoder.OpStructFieldStruct: + b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) p += code.Offset - b = appendStructKey(ctx, code, b) code = code.Next store(ctxptr, code.Idx, p) case encoder.OpStructFieldOmitEmptyStruct: @@ -4366,25 +4286,11 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt case encoder.OpStructAnonymousEnd: code = code.Next case encoder.OpStructEnd: - last := len(b) - 1 - if b[last-1] == '{' { - b[last] = '}' - b = appendComma(b) - code = code.Next - break - } - if b[last] == '\n' { - // to remove ',' and '\n' characters - b = b[:len(b)-2] - } - b = append(b, '\n') - b = appendIndent(ctx, b, code.Indent) - b = append(b, '}') - b = appendComma(b) + b = appendStructEndSkipLast(ctx, code, b) code = code.Next case encoder.OpStructEndInt: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) + b = appendStructKey(ctx, code, b) b = appendInt(b, ptrToUint64(p+code.Offset), code) b = appendStructEnd(ctx, code, b) code = code.Next @@ -4471,8 +4377,8 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructEndUint: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) + b = appendStructKey(ctx, code, b) b = appendUint(b, ptrToUint64(p+code.Offset), code) b = appendStructEnd(ctx, code, b) code = code.Next @@ -4559,8 +4465,8 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructEndFloat32: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) + b = appendStructKey(ctx, code, b) b = appendFloat32(b, ptrToFloat32(p+code.Offset)) b = appendStructEnd(ctx, code, b) code = code.Next @@ -4645,12 +4551,12 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructEndFloat64: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) v := ptrToFloat64(p + code.Offset) if math.IsInf(v, 0) || math.IsNaN(v) { return nil, errUnsupportedFloat(v) } + b = appendStructKey(ctx, code, b) b = appendFloat64(b, v) b = appendStructEnd(ctx, code, b) code = code.Next @@ -4658,10 +4564,10 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p := load(ctxptr, code.HeadIdx) v := ptrToFloat64(p + code.Offset) if v != 0 { - b = appendStructKey(ctx, code, b) if math.IsInf(v, 0) || math.IsNaN(v) { return nil, errUnsupportedFloat(v) } + b = appendStructKey(ctx, code, b) b = appendFloat64(b, v) b = appendStructEnd(ctx, code, b) } else { @@ -4684,11 +4590,11 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p := load(ctxptr, code.HeadIdx) v := ptrToFloat64(p + code.Offset) if v != 0 { - b = appendStructKey(ctx, code, b) - b = append(b, '"') if math.IsInf(v, 0) || math.IsNaN(v) { return nil, errUnsupportedFloat(v) } + b = appendStructKey(ctx, code, b) + b = append(b, '"') b = appendFloat64(b, v) b = append(b, '"') b = appendStructEnd(ctx, code, b) @@ -4702,13 +4608,15 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p = ptrToNPtr(p+code.Offset, code.PtrNum) if p == 0 { b = appendNull(b) - } else { - v := ptrToFloat64(p) - if math.IsInf(v, 0) || math.IsNaN(v) { - return nil, errUnsupportedFloat(v) - } - b = appendFloat64(b, v) + b = appendStructEnd(ctx, code, b) + code = code.Next + break } + v := ptrToFloat64(p) + if math.IsInf(v, 0) || math.IsNaN(v) { + return nil, errUnsupportedFloat(v) + } + b = appendFloat64(b, v) b = appendStructEnd(ctx, code, b) code = code.Next case encoder.OpStructEndOmitEmptyFloat64Ptr: @@ -4748,11 +4656,11 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt p = ptrToNPtr(p+code.Offset, code.PtrNum) if p != 0 { b = appendStructKey(ctx, code, b) - b = append(b, '"') v := ptrToFloat64(p) if math.IsInf(v, 0) || math.IsNaN(v) { return nil, errUnsupportedFloat(v) } + b = append(b, '"') b = appendFloat64(b, v) b = append(b, '"') b = appendStructEnd(ctx, code, b) @@ -4761,8 +4669,8 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructEndString: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) + b = appendStructKey(ctx, code, b) b = appendString(b, ptrToString(p+code.Offset)) b = appendStructEnd(ctx, code, b) code = code.Next @@ -4840,8 +4748,8 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructEndBool: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) + b = appendStructKey(ctx, code, b) b = appendBool(b, ptrToBool(p+code.Offset)) b = appendStructEnd(ctx, code, b) code = code.Next @@ -4926,8 +4834,8 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructEndBytes: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) + b = appendStructKey(ctx, code, b) b = appendByteSlice(b, ptrToBytes(p+code.Offset)) b = appendStructEnd(ctx, code, b) code = code.Next @@ -4965,8 +4873,8 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructEndNumber: - b = appendStructKey(ctx, code, b) p := load(ctxptr, code.HeadIdx) + b = appendStructKey(ctx, code, b) bb, err := appendNumber(b, ptrToNumber(p+code.Offset)) if err != nil { return nil, err @@ -4988,9 +4896,9 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt } code = code.Next case encoder.OpStructEndNumberString: + p := load(ctxptr, code.HeadIdx) b = appendStructKey(ctx, code, b) b = append(b, '"') - p := load(ctxptr, code.HeadIdx) bb, err := appendNumber(b, ptrToNumber(p+code.Offset)) if err != nil { return nil, err From f65ffc044940aa937fe9ab5253f3ebded28b1c5d Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Tue, 18 May 2021 14:03:14 +0900 Subject: [PATCH 3/3] Fix error by linter --- internal/encoder/vm/util.go | 12 ++++++------ internal/encoder/vm_debug/util.go | 12 ++++++------ internal/encoder/vm_escaped/util.go | 12 ++++++------ 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/internal/encoder/vm/util.go b/internal/encoder/vm/util.go index 7b8f331..01e608f 100644 --- a/internal/encoder/vm/util.go +++ b/internal/encoder/vm/util.go @@ -111,19 +111,19 @@ func appendColon(b []byte) []byte { return b } -func appendMapKeyValue(ctx *encoder.RuntimeContext, code *encoder.Opcode, b, key, value []byte) []byte { +func appendMapKeyValue(_ *encoder.RuntimeContext, _ *encoder.Opcode, b, key, value []byte) []byte { b = append(b, key...) b[len(b)-1] = ':' return append(b, value...) } -func appendMapEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { +func appendMapEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { b[len(b)-1] = '}' b = append(b, ',') return b } -func appendInterface(ctx *encoder.RuntimeContext, codeSet *encoder.OpcodeSet, opt encoder.Option, code *encoder.Opcode, b []byte, iface *emptyInterface, ptrOffset uintptr) ([]byte, error) { +func appendInterface(ctx *encoder.RuntimeContext, codeSet *encoder.OpcodeSet, opt encoder.Option, _ *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 { @@ -168,7 +168,7 @@ func appendArrayHead(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []b return append(b, '[') } -func appendArrayEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { +func appendArrayEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { last := len(b) - 1 b[last] = ']' return append(b, ',') @@ -209,7 +209,7 @@ func appendStructEndSkipLast(ctx *encoder.RuntimeContext, code *encoder.Opcode, return appendStructEnd(ctx, code, b) } -func restoreIndent(ctx *encoder.RuntimeContext, code *encoder.Opcode, ctxptr uintptr) {} -func storeIndent(ctxptr uintptr, code *encoder.Opcode, indent uintptr) {} +func restoreIndent(_ *encoder.RuntimeContext, _ *encoder.Opcode, _ uintptr) {} +func storeIndent(_ uintptr, _ *encoder.Opcode, _ uintptr) {} func appendMapKeyIndent(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { return b } func appendArrayElemIndent(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { return b } diff --git a/internal/encoder/vm_debug/util.go b/internal/encoder/vm_debug/util.go index 2c6e74f..2fe1a9a 100644 --- a/internal/encoder/vm_debug/util.go +++ b/internal/encoder/vm_debug/util.go @@ -111,19 +111,19 @@ func appendColon(b []byte) []byte { return b } -func appendMapKeyValue(ctx *encoder.RuntimeContext, code *encoder.Opcode, b, key, value []byte) []byte { +func appendMapKeyValue(_ *encoder.RuntimeContext, _ *encoder.Opcode, b, key, value []byte) []byte { b = append(b, key...) b[len(b)-1] = ':' return append(b, value...) } -func appendMapEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { +func appendMapEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { b[len(b)-1] = '}' b = append(b, ',') return b } -func appendInterface(ctx *encoder.RuntimeContext, codeSet *encoder.OpcodeSet, opt encoder.Option, code *encoder.Opcode, b []byte, iface *emptyInterface, ptrOffset uintptr) ([]byte, error) { +func appendInterface(ctx *encoder.RuntimeContext, codeSet *encoder.OpcodeSet, opt encoder.Option, _ *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 { @@ -168,7 +168,7 @@ func appendArrayHead(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []b return append(b, '[') } -func appendArrayEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { +func appendArrayEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { last := len(b) - 1 b[last] = ']' return append(b, ',') @@ -209,7 +209,7 @@ func appendStructEndSkipLast(ctx *encoder.RuntimeContext, code *encoder.Opcode, return appendStructEnd(ctx, code, b) } -func restoreIndent(ctx *encoder.RuntimeContext, code *encoder.Opcode, ctxptr uintptr) {} -func storeIndent(ctxptr uintptr, code *encoder.Opcode, indent uintptr) {} +func restoreIndent(_ *encoder.RuntimeContext, _ *encoder.Opcode, _ uintptr) {} +func storeIndent(_ uintptr, _ *encoder.Opcode, _ uintptr) {} func appendMapKeyIndent(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { return b } func appendArrayElemIndent(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { return b } diff --git a/internal/encoder/vm_escaped/util.go b/internal/encoder/vm_escaped/util.go index a09dbb7..129eb2c 100644 --- a/internal/encoder/vm_escaped/util.go +++ b/internal/encoder/vm_escaped/util.go @@ -111,19 +111,19 @@ func appendColon(b []byte) []byte { return b } -func appendMapKeyValue(ctx *encoder.RuntimeContext, code *encoder.Opcode, b, key, value []byte) []byte { +func appendMapKeyValue(_ *encoder.RuntimeContext, _ *encoder.Opcode, b, key, value []byte) []byte { b = append(b, key...) b[len(b)-1] = ':' return append(b, value...) } -func appendMapEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { +func appendMapEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { b[len(b)-1] = '}' b = append(b, ',') return b } -func appendInterface(ctx *encoder.RuntimeContext, codeSet *encoder.OpcodeSet, opt encoder.Option, code *encoder.Opcode, b []byte, iface *emptyInterface, ptrOffset uintptr) ([]byte, error) { +func appendInterface(ctx *encoder.RuntimeContext, codeSet *encoder.OpcodeSet, opt encoder.Option, _ *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 { @@ -168,7 +168,7 @@ func appendArrayHead(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []b return append(b, '[') } -func appendArrayEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { +func appendArrayEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { last := len(b) - 1 b[last] = ']' return append(b, ',') @@ -209,7 +209,7 @@ func appendStructEndSkipLast(ctx *encoder.RuntimeContext, code *encoder.Opcode, return appendStructEnd(ctx, code, b) } -func restoreIndent(ctx *encoder.RuntimeContext, code *encoder.Opcode, ctxptr uintptr) {} -func storeIndent(ctxptr uintptr, code *encoder.Opcode, indent uintptr) {} +func restoreIndent(_ *encoder.RuntimeContext, _ *encoder.Opcode, _ uintptr) {} +func storeIndent(_ uintptr, _ *encoder.Opcode, _ uintptr) {} func appendMapKeyIndent(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { return b } func appendArrayElemIndent(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { return b }