Merge pull request #233 from goccy/feature/colorize

Support Colorize option for encoding
This commit is contained in:
Masaaki Goshima 2021-06-01 18:36:22 +09:00 committed by GitHub
commit 75a8c1e3f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 11097 additions and 208 deletions

View File

@ -28,4 +28,5 @@ comment:
require_changes: no require_changes: no
ignore: ignore:
- internal/encoder/vm_debug - internal/encoder/vm_color
- internal/encoder/vm_color_indent

View File

@ -30,6 +30,13 @@ We are accepting requests for features that will be implemented between v0.6.0 a
If you have the API you need, please submit your issue [here](https://github.com/goccy/go-json/issues). If you have the API you need, please submit your issue [here](https://github.com/goccy/go-json/issues).
For example, I'm thinking of supporting `context.Context` of `json.Marshaler` and decoding using JSON Path. For example, I'm thinking of supporting `context.Context` of `json.Marshaler` and decoding using JSON Path.
# Features
- Drop-in replacement of `encoding/json`
- Fast ( See [Benchmark section](https://github.com/goccy/go-json#benchmarks) )
- Flexible customization with options
- Coloring the encoded string
# Installation # Installation
``` ```

View File

@ -67,6 +67,16 @@ func Benchmark_Encode_SmallStruct_SegmentioJson(b *testing.B) {
} }
} }
func Benchmark_Encode_SmallStruct_GoJsonColored(b *testing.B) {
colorOpt := gojson.Colorize(gojson.DefaultColorScheme)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.MarshalWithOption(NewSmallPayload(), colorOpt); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_SmallStruct_GoJson(b *testing.B) { func Benchmark_Encode_SmallStruct_GoJson(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
@ -146,6 +156,17 @@ func Benchmark_Encode_SmallStructCached_SegmentioJson(b *testing.B) {
} }
} }
func Benchmark_Encode_SmallStructCached_GoJsonColored(b *testing.B) {
cached := NewSmallPayload()
colorOpt := gojson.Colorize(gojson.DefaultColorScheme)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.MarshalWithOption(cached, colorOpt); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_SmallStructCached_GoJson(b *testing.B) { func Benchmark_Encode_SmallStructCached_GoJson(b *testing.B) {
cached := NewSmallPayload() cached := NewSmallPayload()
b.ReportAllocs() b.ReportAllocs()
@ -221,6 +242,16 @@ func Benchmark_Encode_MediumStruct_SegmentioJson(b *testing.B) {
} }
} }
func Benchmark_Encode_MediumStruct_GoJsonColored(b *testing.B) {
colorOpt := gojson.Colorize(gojson.DefaultColorScheme)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.MarshalWithOption(NewMediumPayload(), colorOpt); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_MediumStruct_GoJson(b *testing.B) { func Benchmark_Encode_MediumStruct_GoJson(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
@ -300,6 +331,17 @@ func Benchmark_Encode_MediumStructCached_SegmentioJson(b *testing.B) {
} }
} }
func Benchmark_Encode_MediumStructCached_GoJsonColored(b *testing.B) {
cached := NewMediumPayload()
colorOpt := gojson.Colorize(gojson.DefaultColorScheme)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.MarshalWithOption(cached, colorOpt); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_MediumStructCached_GoJson(b *testing.B) { func Benchmark_Encode_MediumStructCached_GoJson(b *testing.B) {
cached := NewMediumPayload() cached := NewMediumPayload()
b.ReportAllocs() b.ReportAllocs()
@ -375,6 +417,16 @@ func Benchmark_Encode_LargeStruct_SegmentioJson(b *testing.B) {
} }
} }
func Benchmark_Encode_LargeStruct_GoJsonColored(b *testing.B) {
colorOpt := gojson.Colorize(gojson.DefaultColorScheme)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.MarshalWithOption(NewLargePayload(), colorOpt); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_LargeStruct_GoJson(b *testing.B) { func Benchmark_Encode_LargeStruct_GoJson(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
@ -454,6 +506,17 @@ func Benchmark_Encode_LargeStructCached_SegmentioJson(b *testing.B) {
} }
} }
func Benchmark_Encode_LargeStructCached_GoJsonColored(b *testing.B) {
cached := NewLargePayload()
colorOpt := gojson.Colorize(gojson.DefaultColorScheme)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.MarshalWithOption(cached, colorOpt); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_LargeStructCached_GoJson(b *testing.B) { func Benchmark_Encode_LargeStructCached_GoJson(b *testing.B) {
cached := NewLargePayload() cached := NewLargePayload()
b.ReportAllocs() b.ReportAllocs()

68
color.go Normal file
View File

@ -0,0 +1,68 @@
package json
import (
"fmt"
"github.com/goccy/go-json/internal/encoder"
)
type (
ColorFormat = encoder.ColorFormat
ColorScheme = encoder.ColorScheme
)
const escape = "\x1b"
type colorAttr int
//nolint:deadcode,varcheck
const (
fgBlackColor colorAttr = iota + 30
fgRedColor
fgGreenColor
fgYellowColor
fgBlueColor
fgMagentaColor
fgCyanColor
fgWhiteColor
)
//nolint:deadcode,varcheck
const (
fgHiBlackColor colorAttr = iota + 90
fgHiRedColor
fgHiGreenColor
fgHiYellowColor
fgHiBlueColor
fgHiMagentaColor
fgHiCyanColor
fgHiWhiteColor
)
func createColorFormat(attr colorAttr) ColorFormat {
return ColorFormat{
Header: wrapColor(attr),
Footer: resetColor(),
}
}
func wrapColor(attr colorAttr) string {
return fmt.Sprintf("%s[%dm", escape, attr)
}
func resetColor() string {
return wrapColor(colorAttr(0))
}
var (
DefaultColorScheme = &ColorScheme{
Int: createColorFormat(fgHiMagentaColor),
Uint: createColorFormat(fgHiMagentaColor),
Float: createColorFormat(fgHiMagentaColor),
Bool: createColorFormat(fgHiYellowColor),
String: createColorFormat(fgHiGreenColor),
Binary: createColorFormat(fgHiRedColor),
ObjectKey: createColorFormat(fgHiCyanColor),
Null: createColorFormat(fgBlueColor),
}
)

49
color_test.go Normal file
View File

@ -0,0 +1,49 @@
package json_test
import (
"testing"
"github.com/goccy/go-json"
)
func TestColorize(t *testing.T) {
v := struct {
A int
B uint
C float32
D string
E bool
F []byte
G []int
H *struct{}
I map[string]interface{}
}{
A: 123,
B: 456,
C: 3.14,
D: "hello",
E: true,
F: []byte("binary"),
G: []int{1, 2, 3, 4},
H: nil,
I: map[string]interface{}{
"mapA": -10,
"mapB": 10,
"mapC": nil,
},
}
t.Run("marshal with color", func(t *testing.T) {
b, err := json.MarshalWithOption(v, json.Colorize(json.DefaultColorScheme))
if err != nil {
t.Fatal(err)
}
t.Log(string(b))
})
t.Run("marshal indent with color", func(t *testing.T) {
b, err := json.MarshalIndentWithOption(v, "", "\t", json.Colorize(json.DefaultColorScheme))
if err != nil {
t.Fatal(err)
}
t.Log("\n" + string(b))
})
}

View File

@ -6,6 +6,8 @@ import (
"github.com/goccy/go-json/internal/encoder" "github.com/goccy/go-json/internal/encoder"
"github.com/goccy/go-json/internal/encoder/vm" "github.com/goccy/go-json/internal/encoder/vm"
"github.com/goccy/go-json/internal/encoder/vm_color"
"github.com/goccy/go-json/internal/encoder/vm_color_indent"
"github.com/goccy/go-json/internal/encoder/vm_indent" "github.com/goccy/go-json/internal/encoder/vm_indent"
) )
@ -41,9 +43,9 @@ func (e *Encoder) EncodeWithOption(v interface{}, optFuncs ...EncodeOptionFunc)
} }
func (e *Encoder) encodeWithOption(ctx *encoder.RuntimeContext, v interface{}, optFuncs ...EncodeOptionFunc) error { func (e *Encoder) encodeWithOption(ctx *encoder.RuntimeContext, v interface{}, optFuncs ...EncodeOptionFunc) error {
initOption(ctx.Option) ctx.Option.Flag = 0
if e.enabledHTMLEscape { if e.enabledHTMLEscape {
ctx.Option.HTMLEscape = true ctx.Option.Flag |= encoder.HTMLEscapeOption
} }
for _, optFunc := range optFuncs { for _, optFunc := range optFuncs {
optFunc(ctx.Option) optFunc(ctx.Option)
@ -95,8 +97,8 @@ func (e *Encoder) SetIndent(prefix, indent string) {
func marshal(v interface{}, optFuncs ...EncodeOptionFunc) ([]byte, error) { func marshal(v interface{}, optFuncs ...EncodeOptionFunc) ([]byte, error) {
ctx := encoder.TakeRuntimeContext() ctx := encoder.TakeRuntimeContext()
initOption(ctx.Option) ctx.Option.Flag = 0
ctx.Option.HTMLEscape = true ctx.Option.Flag |= encoder.HTMLEscapeOption
for _, optFunc := range optFuncs { for _, optFunc := range optFuncs {
optFunc(ctx.Option) optFunc(ctx.Option)
} }
@ -122,8 +124,8 @@ func marshal(v interface{}, optFuncs ...EncodeOptionFunc) ([]byte, error) {
func marshalNoEscape(v interface{}) ([]byte, error) { func marshalNoEscape(v interface{}) ([]byte, error) {
ctx := encoder.TakeRuntimeContext() ctx := encoder.TakeRuntimeContext()
initOption(ctx.Option) ctx.Option.Flag = 0
ctx.Option.HTMLEscape = true ctx.Option.Flag |= encoder.HTMLEscapeOption
buf, err := encodeNoEscape(ctx, v) buf, err := encodeNoEscape(ctx, v)
if err != nil { if err != nil {
@ -146,9 +148,8 @@ func marshalNoEscape(v interface{}) ([]byte, error) {
func marshalIndent(v interface{}, prefix, indent string, optFuncs ...EncodeOptionFunc) ([]byte, error) { func marshalIndent(v interface{}, prefix, indent string, optFuncs ...EncodeOptionFunc) ([]byte, error) {
ctx := encoder.TakeRuntimeContext() ctx := encoder.TakeRuntimeContext()
initOption(ctx.Option) ctx.Option.Flag = 0
ctx.Option.HTMLEscape = true ctx.Option.Flag |= (encoder.HTMLEscapeOption | encoder.IndentOption)
ctx.Option.Indent = true
for _, optFunc := range optFuncs { for _, optFunc := range optFuncs {
optFunc(ctx.Option) optFunc(ctx.Option)
} }
@ -253,24 +254,29 @@ func encodeIndent(ctx *encoder.RuntimeContext, v interface{}, prefix, indent str
} }
func encodeRunCode(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]byte, error) { func encodeRunCode(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]byte, error) {
if ctx.Option.Debug { if (ctx.Option.Flag & encoder.DebugOption) != 0 {
if (ctx.Option.Flag & encoder.ColorizeOption) != 0 {
return vm_color.DebugRun(ctx, b, codeSet)
}
return vm.DebugRun(ctx, b, codeSet) return vm.DebugRun(ctx, b, codeSet)
} }
if (ctx.Option.Flag & encoder.ColorizeOption) != 0 {
return vm_color.Run(ctx, b, codeSet)
}
return vm.Run(ctx, b, codeSet) return vm.Run(ctx, b, codeSet)
} }
func encodeRunIndentCode(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, prefix, indent string) ([]byte, error) { func encodeRunIndentCode(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, prefix, indent string) ([]byte, error) {
ctx.Prefix = []byte(prefix) ctx.Prefix = []byte(prefix)
ctx.IndentStr = []byte(indent) ctx.IndentStr = []byte(indent)
if ctx.Option.Debug { if (ctx.Option.Flag & encoder.DebugOption) != 0 {
if (ctx.Option.Flag & encoder.ColorizeOption) != 0 {
return vm_color_indent.DebugRun(ctx, b, codeSet)
}
return vm_indent.DebugRun(ctx, b, codeSet) return vm_indent.DebugRun(ctx, b, codeSet)
} }
if (ctx.Option.Flag & encoder.ColorizeOption) != 0 {
return vm_color_indent.Run(ctx, b, codeSet)
}
return vm_indent.Run(ctx, b, codeSet) return vm_indent.Run(ctx, b, codeSet)
} }
func initOption(opt *EncodeOption) {
opt.HTMLEscape = false
opt.Indent = false
opt.UnorderedMap = false
opt.Debug = false
}

View File

@ -286,7 +286,7 @@ func generateVM() error {
if err != nil { if err != nil {
return err return err
} }
for _, pkg := range []string{"vm", "vm_indent"} { for _, pkg := range []string{"vm", "vm_indent", "vm_color", "vm_color_indent"} {
f.Name.Name = pkg f.Name.Name = pkg
var buf bytes.Buffer var buf bytes.Buffer
printer.Fprint(&buf, fset, f) printer.Fprint(&buf, fset, f)

View File

@ -14,7 +14,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
ptrOffset := uintptr(0) ptrOffset := uintptr(0)
ctxptr := ctx.Ptr() ctxptr := ctx.Ptr()
var code *encoder.Opcode var code *encoder.Opcode
if ctx.Option.HTMLEscape { if (ctx.Option.Flag & encoder.HTMLEscapeOption) != 0 {
code = codeSet.EscapeKeyCode code = codeSet.EscapeKeyCode
} else { } else {
code = codeSet.NoescapeKeyCode code = codeSet.NoescapeKeyCode
@ -39,7 +39,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
store(ctxptr, code.Idx, p) store(ctxptr, code.Idx, p)
fallthrough fallthrough
case encoder.OpInt: case encoder.OpInt:
b = appendInt(b, ptrToUint64(load(ctxptr, code.Idx)), code) b = appendInt(ctx, b, ptrToUint64(load(ctxptr, code.Idx)), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
case encoder.OpUintPtr: case encoder.OpUintPtr:
@ -53,18 +53,18 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
store(ctxptr, code.Idx, p) store(ctxptr, code.Idx, p)
fallthrough fallthrough
case encoder.OpUint: case encoder.OpUint:
b = appendUint(b, ptrToUint64(load(ctxptr, code.Idx)), code) b = appendUint(ctx, b, ptrToUint64(load(ctxptr, code.Idx)), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
case encoder.OpIntString: case encoder.OpIntString:
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(load(ctxptr, code.Idx)), code) b = appendInt(ctx, b, ptrToUint64(load(ctxptr, code.Idx)), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
case encoder.OpUintString: case encoder.OpUintString:
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(load(ctxptr, code.Idx)), code) b = appendUint(ctx, b, ptrToUint64(load(ctxptr, code.Idx)), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -370,7 +370,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
store(ctxptr, code.ElemIdx, 0) store(ctxptr, code.ElemIdx, 0)
store(ctxptr, code.Length, uintptr(mlen)) store(ctxptr, code.Length, uintptr(mlen))
store(ctxptr, code.MapIter, uintptr(iter)) store(ctxptr, code.MapIter, uintptr(iter))
if ctx.Option.UnorderedMap { if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
b = appendMapKeyIndent(ctx, code.Next, b) b = appendMapKeyIndent(ctx, code.Next, b)
} else { } else {
mapCtx := encoder.NewMapContext(mlen) mapCtx := encoder.NewMapContext(mlen)
@ -385,7 +385,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
idx := load(ctxptr, code.ElemIdx) idx := load(ctxptr, code.ElemIdx)
length := load(ctxptr, code.Length) length := load(ctxptr, code.Length)
idx++ idx++
if ctx.Option.UnorderedMap { if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
if idx < length { if idx < length {
b = appendMapKeyIndent(ctx, code, b) b = appendMapKeyIndent(ctx, code, b)
store(ctxptr, code.ElemIdx, idx) store(ctxptr, code.ElemIdx, idx)
@ -414,7 +414,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
} }
} }
case encoder.OpMapValue: case encoder.OpMapValue:
if ctx.Option.UnorderedMap { if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
b = appendColon(ctx, b) b = appendColon(ctx, b)
} else { } else {
ptr := load(ctxptr, code.End.MapPos) ptr := load(ctxptr, code.End.MapPos)
@ -602,7 +602,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendStructHead(ctx, b) b = appendStructHead(ctx, b)
} }
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendInt(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendInt(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
case encoder.OpStructPtrHeadOmitEmptyInt: case encoder.OpStructPtrHeadOmitEmptyInt:
@ -638,7 +638,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
code = code.NextField code = code.NextField
} else { } else {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendInt(b, u64, code) b = appendInt(ctx, b, u64, code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
} }
@ -671,7 +671,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
} }
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendInt(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -709,7 +709,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
} else { } else {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendInt(b, u64, code) b = appendInt(ctx, b, u64, code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -746,7 +746,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
} }
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -780,7 +780,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
} }
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
code = code.Next code = code.Next
@ -817,7 +817,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
} }
b = appendComma(ctx, b) b = appendComma(ctx, b)
@ -853,7 +853,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
@ -886,7 +886,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendStructHead(ctx, b) b = appendStructHead(ctx, b)
} }
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendUint(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendUint(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
case encoder.OpStructPtrHeadOmitEmptyUint: case encoder.OpStructPtrHeadOmitEmptyUint:
@ -922,7 +922,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
code = code.NextField code = code.NextField
} else { } else {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendUint(b, u64, code) b = appendUint(ctx, b, u64, code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
} }
@ -955,7 +955,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
} }
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendUint(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -993,7 +993,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
} else { } else {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendUint(b, u64, code) b = appendUint(ctx, b, u64, code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -1030,7 +1030,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
} }
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -1064,7 +1064,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
} }
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
code = code.Next code = code.Next
@ -1101,7 +1101,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
} }
b = appendComma(ctx, b) b = appendComma(ctx, b)
@ -1137,7 +1137,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
@ -3413,7 +3413,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
case encoder.OpStructFieldInt: case encoder.OpStructFieldInt:
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendInt(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendInt(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
case encoder.OpStructFieldOmitEmptyInt: case encoder.OpStructFieldOmitEmptyInt:
@ -3422,7 +3422,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
v := u64 & ((1 << code.NumBitSize) - 1) v := u64 & ((1 << code.NumBitSize) - 1)
if v != 0 { if v != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendInt(b, u64, code) b = appendInt(ctx, b, u64, code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
code = code.Next code = code.Next
@ -3430,7 +3430,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendInt(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -3441,7 +3441,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if v != 0 { if v != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendInt(b, u64, code) b = appendInt(ctx, b, u64, code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
@ -3453,7 +3453,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
} }
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -3462,7 +3462,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
p = ptrToNPtr(p+uintptr(code.Offset), code.PtrNum) p = ptrToNPtr(p+uintptr(code.Offset), code.PtrNum)
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
code = code.Next code = code.Next
@ -3474,7 +3474,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
} }
b = appendComma(ctx, b) b = appendComma(ctx, b)
@ -3485,7 +3485,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
@ -3493,7 +3493,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
case encoder.OpStructFieldUint: case encoder.OpStructFieldUint:
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendUint(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendUint(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
case encoder.OpStructFieldOmitEmptyUint: case encoder.OpStructFieldOmitEmptyUint:
@ -3502,7 +3502,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
v := u64 & ((1 << code.NumBitSize) - 1) v := u64 & ((1 << code.NumBitSize) - 1)
if v != 0 { if v != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendUint(b, u64, code) b = appendUint(ctx, b, u64, code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
code = code.Next code = code.Next
@ -3510,7 +3510,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendUint(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -3521,7 +3521,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if v != 0 { if v != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendUint(b, u64, code) b = appendUint(ctx, b, u64, code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
@ -3533,7 +3533,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
} }
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -3542,7 +3542,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
p = ptrToNPtr(p+uintptr(code.Offset), code.PtrNum) p = ptrToNPtr(p+uintptr(code.Offset), code.PtrNum)
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
code = code.Next code = code.Next
@ -3554,7 +3554,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
} }
b = appendComma(ctx, b) b = appendComma(ctx, b)
@ -3565,7 +3565,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
@ -4297,7 +4297,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
case encoder.OpStructEndInt: case encoder.OpStructEndInt:
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendInt(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendInt(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
case encoder.OpStructEndOmitEmptyInt: case encoder.OpStructEndOmitEmptyInt:
@ -4306,7 +4306,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
v := u64 & ((1 << code.NumBitSize) - 1) v := u64 & ((1 << code.NumBitSize) - 1)
if v != 0 { if v != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendInt(b, u64, code) b = appendInt(ctx, b, u64, code)
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
b = appendStructEndSkipLast(ctx, code, b) b = appendStructEndSkipLast(ctx, code, b)
@ -4316,7 +4316,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendInt(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = append(b, '"') b = append(b, '"')
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
@ -4327,7 +4327,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if v != 0 { if v != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendInt(b, u64, code) b = appendInt(ctx, b, u64, code)
b = append(b, '"') b = append(b, '"')
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
@ -4341,7 +4341,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
} }
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
@ -4350,7 +4350,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
p = ptrToNPtr(p+uintptr(code.Offset), code.PtrNum) p = ptrToNPtr(p+uintptr(code.Offset), code.PtrNum)
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
b = appendStructEndSkipLast(ctx, code, b) b = appendStructEndSkipLast(ctx, code, b)
@ -4364,7 +4364,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
} }
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
@ -4375,7 +4375,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
@ -4385,7 +4385,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
case encoder.OpStructEndUint: case encoder.OpStructEndUint:
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendUint(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendUint(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
case encoder.OpStructEndOmitEmptyUint: case encoder.OpStructEndOmitEmptyUint:
@ -4394,7 +4394,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
v := u64 & ((1 << code.NumBitSize) - 1) v := u64 & ((1 << code.NumBitSize) - 1)
if v != 0 { if v != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendUint(b, u64, code) b = appendUint(ctx, b, u64, code)
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
b = appendStructEndSkipLast(ctx, code, b) b = appendStructEndSkipLast(ctx, code, b)
@ -4404,7 +4404,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendUint(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = append(b, '"') b = append(b, '"')
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
@ -4415,7 +4415,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if v != 0 { if v != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendUint(b, u64, code) b = appendUint(ctx, b, u64, code)
b = append(b, '"') b = append(b, '"')
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
@ -4429,7 +4429,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
} }
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
@ -4438,7 +4438,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
p = ptrToNPtr(p+uintptr(code.Offset), code.PtrNum) p = ptrToNPtr(p+uintptr(code.Offset), code.PtrNum)
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
b = appendStructEndSkipLast(ctx, code, b) b = appendStructEndSkipLast(ctx, code, b)
@ -4452,7 +4452,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
} }
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
@ -4463,7 +4463,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {

View File

@ -1456,7 +1456,7 @@ func compileStruct(ctx *compileContext, isPtr bool) (*Opcode, error) {
} }
var key string var key string
if ctx.escapeKey { if ctx.escapeKey {
rctx := &RuntimeContext{Option: &Option{HTMLEscape: true}} rctx := &RuntimeContext{Option: &Option{Flag: HTMLEscapeOption}}
key = fmt.Sprintf(`%s:`, string(AppendString(rctx, []byte{}, tag.Key))) key = fmt.Sprintf(`%s:`, string(AppendString(rctx, []byte{}, tag.Key)))
} else { } else {
key = fmt.Sprintf(`"%s":`, tag.Key) key = fmt.Sprintf(`"%s":`, tag.Key)

View File

@ -375,7 +375,7 @@ func AppendMarshalJSON(ctx *RuntimeContext, code *Opcode, b []byte, v interface{
} }
marshalBuf := ctx.MarshalBuf[:0] marshalBuf := ctx.MarshalBuf[:0]
marshalBuf = append(append(marshalBuf, bb...), nul) marshalBuf = append(append(marshalBuf, bb...), nul)
compactedBuf, err := compact(b, marshalBuf, ctx.Option.HTMLEscape) compactedBuf, err := compact(b, marshalBuf, (ctx.Option.Flag&HTMLEscapeOption) != 0)
if err != nil { if err != nil {
return nil, &errors.MarshalerError{Type: reflect.TypeOf(v), Err: err} return nil, &errors.MarshalerError{Type: reflect.TypeOf(v), Err: err}
} }
@ -410,7 +410,7 @@ func AppendMarshalJSONIndent(ctx *RuntimeContext, code *Opcode, b []byte, v inte
marshalBuf, marshalBuf,
string(ctx.Prefix)+strings.Repeat(string(ctx.IndentStr), int(ctx.BaseIndent+code.Indent)), string(ctx.Prefix)+strings.Repeat(string(ctx.IndentStr), int(ctx.BaseIndent+code.Indent)),
string(ctx.IndentStr), string(ctx.IndentStr),
ctx.Option.HTMLEscape, (ctx.Option.Flag&HTMLEscapeOption) != 0,
) )
if err != nil { if err != nil {
return nil, &errors.MarshalerError{Type: reflect.TypeOf(v), Err: err} return nil, &errors.MarshalerError{Type: reflect.TypeOf(v), Err: err}

View File

@ -53,7 +53,7 @@ func numMask(numBitSize uint8) uint64 {
return 1<<numBitSize - 1 return 1<<numBitSize - 1
} }
func AppendInt(out []byte, u64 uint64, code *Opcode) []byte { func AppendInt(_ *RuntimeContext, out []byte, u64 uint64, code *Opcode) []byte {
mask := numMask(code.NumBitSize) mask := numMask(code.NumBitSize)
n := u64 & mask n := u64 & mask
negative := (u64>>(code.NumBitSize-1))&1 == 1 negative := (u64>>(code.NumBitSize-1))&1 == 1
@ -96,7 +96,7 @@ func AppendInt(out []byte, u64 uint64, code *Opcode) []byte {
return append(out, b[i:]...) return append(out, b[i:]...)
} }
func AppendUint(out []byte, u64 uint64, code *Opcode) []byte { func AppendUint(_ *RuntimeContext, out []byte, u64 uint64, code *Opcode) []byte {
mask := numMask(code.NumBitSize) mask := numMask(code.NumBitSize)
n := u64 & mask n := u64 & mask
if n < 10 { if n < 10 {

View File

@ -1,8 +1,37 @@
package encoder package encoder
type OptionFlag uint8
const (
HTMLEscapeOption OptionFlag = 1 << iota
IndentOption
UnorderedMapOption
DebugOption
ColorizeOption
)
type Option struct { type Option struct {
HTMLEscape bool Flag OptionFlag
Indent bool ColorScheme *ColorScheme
UnorderedMap bool
Debug bool
} }
type EncodeFormat struct {
Header string
Footer string
}
type EncodeFormatScheme struct {
Int EncodeFormat
Uint EncodeFormat
Float EncodeFormat
Bool EncodeFormat
String EncodeFormat
Binary EncodeFormat
ObjectKey EncodeFormat
Null EncodeFormat
}
type (
ColorScheme = EncodeFormatScheme
ColorFormat = EncodeFormat
)

View File

@ -406,7 +406,7 @@ func stringToUint64Slice(s string) []uint64 {
} }
func AppendString(ctx *RuntimeContext, buf []byte, s string) []byte { func AppendString(ctx *RuntimeContext, buf []byte, s string) []byte {
if !ctx.Option.HTMLEscape { if ctx.Option.Flag&HTMLEscapeOption == 0 {
return appendString(buf, s) return appendString(buf, s)
} }
valLen := len(s) valLen := len(s)

View File

@ -9,7 +9,7 @@ import (
func DebugRun(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]byte, error) { func DebugRun(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]byte, error) {
defer func() { defer func() {
var code *encoder.Opcode var code *encoder.Opcode
if ctx.Option.HTMLEscape { if (ctx.Option.Flag & encoder.HTMLEscapeOption) != 0 {
code = codeSet.EscapeKeyCode code = codeSet.EscapeKeyCode
} else { } else {
code = codeSet.NoescapeKeyCode code = codeSet.NoescapeKeyCode

View File

@ -2,8 +2,8 @@ package vm
import ( import (
// HACK: compile order // HACK: compile order
// `vm`, `vm_indent` packages uses a lot of memory to compile, // `vm`, `vm_indent`, `vm_color`, `vm_color_indent` packages uses a lot of memory to compile,
// so forcibly make dependencies and avoid compiling in concurrent. // so forcibly make dependencies and avoid compiling in concurrent.
// dependency order: vm => vm_indent // dependency order: vm => vm_indent => vm_color => vm_color_indent
_ "github.com/goccy/go-json/internal/encoder/vm_indent" _ "github.com/goccy/go-json/internal/encoder/vm_indent"
) )

View File

@ -14,7 +14,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
ptrOffset := uintptr(0) ptrOffset := uintptr(0)
ctxptr := ctx.Ptr() ctxptr := ctx.Ptr()
var code *encoder.Opcode var code *encoder.Opcode
if ctx.Option.HTMLEscape { if (ctx.Option.Flag & encoder.HTMLEscapeOption) != 0 {
code = codeSet.EscapeKeyCode code = codeSet.EscapeKeyCode
} else { } else {
code = codeSet.NoescapeKeyCode code = codeSet.NoescapeKeyCode
@ -39,7 +39,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
store(ctxptr, code.Idx, p) store(ctxptr, code.Idx, p)
fallthrough fallthrough
case encoder.OpInt: case encoder.OpInt:
b = appendInt(b, ptrToUint64(load(ctxptr, code.Idx)), code) b = appendInt(ctx, b, ptrToUint64(load(ctxptr, code.Idx)), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
case encoder.OpUintPtr: case encoder.OpUintPtr:
@ -53,18 +53,18 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
store(ctxptr, code.Idx, p) store(ctxptr, code.Idx, p)
fallthrough fallthrough
case encoder.OpUint: case encoder.OpUint:
b = appendUint(b, ptrToUint64(load(ctxptr, code.Idx)), code) b = appendUint(ctx, b, ptrToUint64(load(ctxptr, code.Idx)), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
case encoder.OpIntString: case encoder.OpIntString:
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(load(ctxptr, code.Idx)), code) b = appendInt(ctx, b, ptrToUint64(load(ctxptr, code.Idx)), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
case encoder.OpUintString: case encoder.OpUintString:
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(load(ctxptr, code.Idx)), code) b = appendUint(ctx, b, ptrToUint64(load(ctxptr, code.Idx)), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -370,7 +370,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
store(ctxptr, code.ElemIdx, 0) store(ctxptr, code.ElemIdx, 0)
store(ctxptr, code.Length, uintptr(mlen)) store(ctxptr, code.Length, uintptr(mlen))
store(ctxptr, code.MapIter, uintptr(iter)) store(ctxptr, code.MapIter, uintptr(iter))
if ctx.Option.UnorderedMap { if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
b = appendMapKeyIndent(ctx, code.Next, b) b = appendMapKeyIndent(ctx, code.Next, b)
} else { } else {
mapCtx := encoder.NewMapContext(mlen) mapCtx := encoder.NewMapContext(mlen)
@ -385,7 +385,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
idx := load(ctxptr, code.ElemIdx) idx := load(ctxptr, code.ElemIdx)
length := load(ctxptr, code.Length) length := load(ctxptr, code.Length)
idx++ idx++
if ctx.Option.UnorderedMap { if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
if idx < length { if idx < length {
b = appendMapKeyIndent(ctx, code, b) b = appendMapKeyIndent(ctx, code, b)
store(ctxptr, code.ElemIdx, idx) store(ctxptr, code.ElemIdx, idx)
@ -414,7 +414,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
} }
} }
case encoder.OpMapValue: case encoder.OpMapValue:
if ctx.Option.UnorderedMap { if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
b = appendColon(ctx, b) b = appendColon(ctx, b)
} else { } else {
ptr := load(ctxptr, code.End.MapPos) ptr := load(ctxptr, code.End.MapPos)
@ -602,7 +602,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendStructHead(ctx, b) b = appendStructHead(ctx, b)
} }
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendInt(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendInt(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
case encoder.OpStructPtrHeadOmitEmptyInt: case encoder.OpStructPtrHeadOmitEmptyInt:
@ -638,7 +638,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
code = code.NextField code = code.NextField
} else { } else {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendInt(b, u64, code) b = appendInt(ctx, b, u64, code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
} }
@ -671,7 +671,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
} }
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendInt(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -709,7 +709,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
} else { } else {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendInt(b, u64, code) b = appendInt(ctx, b, u64, code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -746,7 +746,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
} }
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -780,7 +780,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
} }
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
code = code.Next code = code.Next
@ -817,7 +817,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
} }
b = appendComma(ctx, b) b = appendComma(ctx, b)
@ -853,7 +853,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
@ -886,7 +886,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendStructHead(ctx, b) b = appendStructHead(ctx, b)
} }
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendUint(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendUint(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
case encoder.OpStructPtrHeadOmitEmptyUint: case encoder.OpStructPtrHeadOmitEmptyUint:
@ -922,7 +922,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
code = code.NextField code = code.NextField
} else { } else {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendUint(b, u64, code) b = appendUint(ctx, b, u64, code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
} }
@ -955,7 +955,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
} }
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendUint(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -993,7 +993,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
} else { } else {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendUint(b, u64, code) b = appendUint(ctx, b, u64, code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -1030,7 +1030,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
} }
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -1064,7 +1064,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
} }
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
code = code.Next code = code.Next
@ -1101,7 +1101,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
} }
b = appendComma(ctx, b) b = appendComma(ctx, b)
@ -1137,7 +1137,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
@ -3413,7 +3413,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
case encoder.OpStructFieldInt: case encoder.OpStructFieldInt:
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendInt(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendInt(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
case encoder.OpStructFieldOmitEmptyInt: case encoder.OpStructFieldOmitEmptyInt:
@ -3422,7 +3422,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
v := u64 & ((1 << code.NumBitSize) - 1) v := u64 & ((1 << code.NumBitSize) - 1)
if v != 0 { if v != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendInt(b, u64, code) b = appendInt(ctx, b, u64, code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
code = code.Next code = code.Next
@ -3430,7 +3430,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendInt(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -3441,7 +3441,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if v != 0 { if v != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendInt(b, u64, code) b = appendInt(ctx, b, u64, code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
@ -3453,7 +3453,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
} }
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -3462,7 +3462,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
p = ptrToNPtr(p+uintptr(code.Offset), code.PtrNum) p = ptrToNPtr(p+uintptr(code.Offset), code.PtrNum)
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
code = code.Next code = code.Next
@ -3474,7 +3474,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
} }
b = appendComma(ctx, b) b = appendComma(ctx, b)
@ -3485,7 +3485,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
@ -3493,7 +3493,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
case encoder.OpStructFieldUint: case encoder.OpStructFieldUint:
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendUint(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendUint(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
case encoder.OpStructFieldOmitEmptyUint: case encoder.OpStructFieldOmitEmptyUint:
@ -3502,7 +3502,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
v := u64 & ((1 << code.NumBitSize) - 1) v := u64 & ((1 << code.NumBitSize) - 1)
if v != 0 { if v != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendUint(b, u64, code) b = appendUint(ctx, b, u64, code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
code = code.Next code = code.Next
@ -3510,7 +3510,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendUint(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -3521,7 +3521,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if v != 0 { if v != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendUint(b, u64, code) b = appendUint(ctx, b, u64, code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
@ -3533,7 +3533,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
} }
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -3542,7 +3542,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
p = ptrToNPtr(p+uintptr(code.Offset), code.PtrNum) p = ptrToNPtr(p+uintptr(code.Offset), code.PtrNum)
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
code = code.Next code = code.Next
@ -3554,7 +3554,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
} }
b = appendComma(ctx, b) b = appendComma(ctx, b)
@ -3565,7 +3565,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
@ -4297,7 +4297,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
case encoder.OpStructEndInt: case encoder.OpStructEndInt:
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendInt(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendInt(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
case encoder.OpStructEndOmitEmptyInt: case encoder.OpStructEndOmitEmptyInt:
@ -4306,7 +4306,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
v := u64 & ((1 << code.NumBitSize) - 1) v := u64 & ((1 << code.NumBitSize) - 1)
if v != 0 { if v != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendInt(b, u64, code) b = appendInt(ctx, b, u64, code)
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
b = appendStructEndSkipLast(ctx, code, b) b = appendStructEndSkipLast(ctx, code, b)
@ -4316,7 +4316,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendInt(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = append(b, '"') b = append(b, '"')
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
@ -4327,7 +4327,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if v != 0 { if v != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendInt(b, u64, code) b = appendInt(ctx, b, u64, code)
b = append(b, '"') b = append(b, '"')
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
@ -4341,7 +4341,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
} }
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
@ -4350,7 +4350,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
p = ptrToNPtr(p+uintptr(code.Offset), code.PtrNum) p = ptrToNPtr(p+uintptr(code.Offset), code.PtrNum)
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
b = appendStructEndSkipLast(ctx, code, b) b = appendStructEndSkipLast(ctx, code, b)
@ -4364,7 +4364,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
} }
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
@ -4375,7 +4375,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
@ -4385,7 +4385,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
case encoder.OpStructEndUint: case encoder.OpStructEndUint:
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendUint(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendUint(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
case encoder.OpStructEndOmitEmptyUint: case encoder.OpStructEndOmitEmptyUint:
@ -4394,7 +4394,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
v := u64 & ((1 << code.NumBitSize) - 1) v := u64 & ((1 << code.NumBitSize) - 1)
if v != 0 { if v != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendUint(b, u64, code) b = appendUint(ctx, b, u64, code)
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
b = appendStructEndSkipLast(ctx, code, b) b = appendStructEndSkipLast(ctx, code, b)
@ -4404,7 +4404,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendUint(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = append(b, '"') b = append(b, '"')
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
@ -4415,7 +4415,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if v != 0 { if v != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendUint(b, u64, code) b = appendUint(ctx, b, u64, code)
b = append(b, '"') b = append(b, '"')
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
@ -4429,7 +4429,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
} }
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
@ -4438,7 +4438,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
p = ptrToNPtr(p+uintptr(code.Offset), code.PtrNum) p = ptrToNPtr(p+uintptr(code.Offset), code.PtrNum)
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
b = appendStructEndSkipLast(ctx, code, b) b = appendStructEndSkipLast(ctx, code, b)
@ -4452,7 +4452,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
} }
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
@ -4463,7 +4463,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {

View File

@ -0,0 +1,34 @@
package vm_color
import (
"fmt"
"github.com/goccy/go-json/internal/encoder"
)
func DebugRun(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]byte, error) {
var code *encoder.Opcode
if (ctx.Option.Flag & encoder.HTMLEscapeOption) != 0 {
code = codeSet.EscapeKeyCode
} else {
code = codeSet.NoescapeKeyCode
}
defer func() {
if err := recover(); err != nil {
fmt.Println("=============[DEBUG]===============")
fmt.Println("* [TYPE]")
fmt.Println(codeSet.Type)
fmt.Printf("\n")
fmt.Println("* [ALL OPCODE]")
fmt.Println(code.Dump())
fmt.Printf("\n")
fmt.Println("* [CONTEXT]")
fmt.Printf("%+v\n", ctx)
fmt.Println("===================================")
panic(err)
}
}()
return Run(ctx, b, codeSet)
}

View File

@ -0,0 +1,9 @@
package vm_color
import (
// HACK: compile order
// `vm`, `vm_indent`, `vm_color`, `vm_color_indent` packages uses a lot of memory to compile,
// so forcibly make dependencies and avoid compiling in concurrent.
// dependency order: vm => vm_indent => vm_color => vm_color_indent
_ "github.com/goccy/go-json/internal/encoder/vm_color_indent"
)

View File

@ -0,0 +1,279 @@
package vm_color
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 (
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 uint32) uintptr {
addr := base + uintptr(idx)
return **(**uintptr)(unsafe.Pointer(&addr))
}
func store(base uintptr, idx uint32, p uintptr) {
addr := base + uintptr(idx)
**(**uintptr)(unsafe.Pointer(&addr)) = p
}
func loadNPtr(base uintptr, idx uint32, ptrNum uint8) uintptr {
addr := base + uintptr(idx)
p := **(**uintptr)(unsafe.Pointer(&addr))
for i := uint8(0); i < ptrNum; i++ {
if p == 0 {
return 0
}
p = ptrToPtr(p)
}
return p
}
func ptrToUint64(p uintptr) uint64 { return **(**uint64)(unsafe.Pointer(&p)) }
func ptrToFloat32(p uintptr) float32 { return **(**float32)(unsafe.Pointer(&p)) }
func ptrToFloat64(p uintptr) float64 { return **(**float64)(unsafe.Pointer(&p)) }
func ptrToBool(p uintptr) bool { return **(**bool)(unsafe.Pointer(&p)) }
func ptrToBytes(p uintptr) []byte { return **(**[]byte)(unsafe.Pointer(&p)) }
func ptrToNumber(p uintptr) json.Number { return **(**json.Number)(unsafe.Pointer(&p)) }
func ptrToString(p uintptr) string { return **(**string)(unsafe.Pointer(&p)) }
func ptrToSlice(p uintptr) *runtime.SliceHeader { return *(**runtime.SliceHeader)(unsafe.Pointer(&p)) }
func ptrToPtr(p uintptr) uintptr {
return uintptr(**(**unsafe.Pointer)(unsafe.Pointer(&p)))
}
func ptrToNPtr(p uintptr, ptrNum uint8) uintptr {
for i := uint8(0); i < ptrNum; i++ {
if p == 0 {
return 0
}
p = ptrToPtr(p)
}
return p
}
func ptrToUnsafePtr(p uintptr) unsafe.Pointer {
return *(*unsafe.Pointer)(unsafe.Pointer(&p))
}
func ptrToInterface(code *encoder.Opcode, p uintptr) interface{} {
return *(*interface{})(unsafe.Pointer(&emptyInterface{
typ: code.Type,
ptr: *(*unsafe.Pointer)(unsafe.Pointer(&p)),
}))
}
func appendInt(ctx *encoder.RuntimeContext, b []byte, v uint64, code *encoder.Opcode) []byte {
format := ctx.Option.ColorScheme.Int
b = append(b, format.Header...)
b = encoder.AppendInt(ctx, b, v, code)
return append(b, format.Footer...)
}
func appendUint(ctx *encoder.RuntimeContext, b []byte, v uint64, code *encoder.Opcode) []byte {
format := ctx.Option.ColorScheme.Uint
b = append(b, format.Header...)
b = encoder.AppendUint(ctx, b, v, code)
return append(b, format.Footer...)
}
func appendFloat32(ctx *encoder.RuntimeContext, b []byte, v float32) []byte {
format := ctx.Option.ColorScheme.Float
b = append(b, format.Header...)
b = encoder.AppendFloat32(ctx, b, v)
return append(b, format.Footer...)
}
func appendFloat64(ctx *encoder.RuntimeContext, b []byte, v float64) []byte {
format := ctx.Option.ColorScheme.Float
b = append(b, format.Header...)
b = encoder.AppendFloat64(ctx, b, v)
return append(b, format.Footer...)
}
func appendString(ctx *encoder.RuntimeContext, b []byte, v string) []byte {
format := ctx.Option.ColorScheme.String
b = append(b, format.Header...)
b = encoder.AppendString(ctx, b, v)
return append(b, format.Footer...)
}
func appendByteSlice(ctx *encoder.RuntimeContext, b []byte, src []byte) []byte {
format := ctx.Option.ColorScheme.Binary
b = append(b, format.Header...)
b = encoder.AppendByteSlice(ctx, b, src)
return append(b, format.Footer...)
}
func appendNumber(ctx *encoder.RuntimeContext, b []byte, n json.Number) ([]byte, error) {
format := ctx.Option.ColorScheme.Int
b = append(b, format.Header...)
bb, err := encoder.AppendNumber(ctx, b, n)
if err != nil {
return nil, err
}
return append(bb, format.Footer...), nil
}
func appendBool(ctx *encoder.RuntimeContext, b []byte, v bool) []byte {
format := ctx.Option.ColorScheme.Bool
b = append(b, format.Header...)
if v {
b = append(b, "true"...)
} else {
b = append(b, "false"...)
}
return append(b, format.Footer...)
}
func appendNull(ctx *encoder.RuntimeContext, b []byte) []byte {
format := ctx.Option.ColorScheme.Null
b = append(b, format.Header...)
b = append(b, "null"...)
return append(b, format.Footer...)
}
func appendComma(_ *encoder.RuntimeContext, b []byte) []byte {
return append(b, ',')
}
func appendColon(_ *encoder.RuntimeContext, b []byte) []byte {
last := len(b) - 1
b[last] = ':'
return b
}
func appendMapKeyValue(_ *encoder.RuntimeContext, _ *encoder.Opcode, b, key, value []byte) []byte {
b = append(b, key[:len(key)-1]...)
b = append(b, ':')
return append(b, value...)
}
func appendMapEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte {
last := len(b) - 1
b[last] = '}'
b = append(b, ',')
return b
}
func appendInterface(ctx *encoder.RuntimeContext, codeSet *encoder.OpcodeSet, _ *encoder.Opcode, b []byte, iface *emptyInterface, ptrOffset uintptr) ([]byte, error) {
ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(iface))
ifaceCodeSet, err := encoder.CompileToGetCodeSet(uintptr(unsafe.Pointer(iface.typ)))
if err != nil {
return nil, err
}
totalLength := uintptr(codeSet.CodeLength)
nextTotalLength := uintptr(ifaceCodeSet.CodeLength)
curlen := uintptr(len(ctx.Ptrs))
offsetNum := ptrOffset / uintptrSize
newLen := offsetNum + totalLength + nextTotalLength
if curlen < newLen {
ctx.Ptrs = append(ctx.Ptrs, make([]uintptr, newLen-curlen)...)
}
oldPtrs := ctx.Ptrs
newPtrs := ctx.Ptrs[(ptrOffset+totalLength*uintptrSize)/uintptrSize:]
newPtrs[0] = uintptr(iface.ptr)
ctx.Ptrs = newPtrs
bb, err := Run(ctx, b, ifaceCodeSet)
if err != nil {
return nil, err
}
ctx.Ptrs = oldPtrs
return bb, nil
}
func appendMarshalJSON(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) {
return encoder.AppendMarshalJSON(ctx, code, b, v)
}
func appendMarshalText(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) {
format := ctx.Option.ColorScheme.String
b = append(b, format.Header...)
bb, err := encoder.AppendMarshalText(ctx, code, b, v)
if err != nil {
return nil, err
}
return append(bb, format.Footer...), nil
}
func appendArrayHead(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte {
return append(b, '[')
}
func appendArrayEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte {
last := len(b) - 1
b[last] = ']'
return append(b, ',')
}
func appendEmptyArray(_ *encoder.RuntimeContext, b []byte) []byte {
return append(b, '[', ']', ',')
}
func appendEmptyObject(_ *encoder.RuntimeContext, 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(_ *encoder.RuntimeContext, b []byte) []byte {
return append(b, '{')
}
func appendStructKey(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte {
format := ctx.Option.ColorScheme.ObjectKey
b = append(b, format.Header...)
b = append(b, code.Key[:len(code.Key)-1]...)
b = append(b, format.Footer...)
return append(b, ':')
}
func appendStructEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte {
return append(b, '}', ',')
}
func appendStructEndSkipLast(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte {
last := len(b) - 1
if b[last] == ',' {
b[last] = '}'
return appendComma(ctx, b)
}
return appendStructEnd(ctx, code, b)
}
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 }

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,34 @@
package vm_color_indent
import (
"fmt"
"github.com/goccy/go-json/internal/encoder"
)
func DebugRun(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]byte, error) {
var code *encoder.Opcode
if (ctx.Option.Flag & encoder.HTMLEscapeOption) != 0 {
code = codeSet.EscapeKeyCode
} else {
code = codeSet.NoescapeKeyCode
}
defer func() {
if err := recover(); err != nil {
fmt.Println("=============[DEBUG]===============")
fmt.Println("* [TYPE]")
fmt.Println(codeSet.Type)
fmt.Printf("\n")
fmt.Println("* [ALL OPCODE]")
fmt.Println(code.Dump())
fmt.Printf("\n")
fmt.Println("* [CONTEXT]")
fmt.Printf("%+v\n", ctx)
fmt.Println("===================================")
panic(err)
}
}()
return Run(ctx, b, codeSet)
}

View File

@ -0,0 +1,304 @@
package vm_color_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 (
appendIndent = encoder.AppendIndent
appendStructEnd = encoder.AppendStructEndIndent
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 uint32) uintptr {
addr := base + uintptr(idx)
return **(**uintptr)(unsafe.Pointer(&addr))
}
func store(base uintptr, idx uint32, p uintptr) {
addr := base + uintptr(idx)
**(**uintptr)(unsafe.Pointer(&addr)) = p
}
func loadNPtr(base uintptr, idx uint32, ptrNum uint8) uintptr {
addr := base + uintptr(idx)
p := **(**uintptr)(unsafe.Pointer(&addr))
for i := uint8(0); i < ptrNum; i++ {
if p == 0 {
return 0
}
p = ptrToPtr(p)
}
return p
}
func ptrToUint64(p uintptr) uint64 { return **(**uint64)(unsafe.Pointer(&p)) }
func ptrToFloat32(p uintptr) float32 { return **(**float32)(unsafe.Pointer(&p)) }
func ptrToFloat64(p uintptr) float64 { return **(**float64)(unsafe.Pointer(&p)) }
func ptrToBool(p uintptr) bool { return **(**bool)(unsafe.Pointer(&p)) }
func ptrToBytes(p uintptr) []byte { return **(**[]byte)(unsafe.Pointer(&p)) }
func ptrToNumber(p uintptr) json.Number { return **(**json.Number)(unsafe.Pointer(&p)) }
func ptrToString(p uintptr) string { return **(**string)(unsafe.Pointer(&p)) }
func ptrToSlice(p uintptr) *runtime.SliceHeader { return *(**runtime.SliceHeader)(unsafe.Pointer(&p)) }
func ptrToPtr(p uintptr) uintptr {
return uintptr(**(**unsafe.Pointer)(unsafe.Pointer(&p)))
}
func ptrToNPtr(p uintptr, ptrNum uint8) uintptr {
for i := uint8(0); i < ptrNum; i++ {
if p == 0 {
return 0
}
p = ptrToPtr(p)
}
return p
}
func ptrToUnsafePtr(p uintptr) unsafe.Pointer {
return *(*unsafe.Pointer)(unsafe.Pointer(&p))
}
func ptrToInterface(code *encoder.Opcode, p uintptr) interface{} {
return *(*interface{})(unsafe.Pointer(&emptyInterface{
typ: code.Type,
ptr: *(*unsafe.Pointer)(unsafe.Pointer(&p)),
}))
}
func appendInt(ctx *encoder.RuntimeContext, b []byte, v uint64, code *encoder.Opcode) []byte {
format := ctx.Option.ColorScheme.Int
b = append(b, format.Header...)
b = encoder.AppendInt(ctx, b, v, code)
return append(b, format.Footer...)
}
func appendUint(ctx *encoder.RuntimeContext, b []byte, v uint64, code *encoder.Opcode) []byte {
format := ctx.Option.ColorScheme.Uint
b = append(b, format.Header...)
b = encoder.AppendUint(ctx, b, v, code)
return append(b, format.Footer...)
}
func appendFloat32(ctx *encoder.RuntimeContext, b []byte, v float32) []byte {
format := ctx.Option.ColorScheme.Float
b = append(b, format.Header...)
b = encoder.AppendFloat32(ctx, b, v)
return append(b, format.Footer...)
}
func appendFloat64(ctx *encoder.RuntimeContext, b []byte, v float64) []byte {
format := ctx.Option.ColorScheme.Float
b = append(b, format.Header...)
b = encoder.AppendFloat64(ctx, b, v)
return append(b, format.Footer...)
}
func appendString(ctx *encoder.RuntimeContext, b []byte, v string) []byte {
format := ctx.Option.ColorScheme.String
b = append(b, format.Header...)
b = encoder.AppendString(ctx, b, v)
return append(b, format.Footer...)
}
func appendByteSlice(ctx *encoder.RuntimeContext, b []byte, src []byte) []byte {
format := ctx.Option.ColorScheme.Binary
b = append(b, format.Header...)
b = encoder.AppendByteSlice(ctx, b, src)
return append(b, format.Footer...)
}
func appendNumber(ctx *encoder.RuntimeContext, b []byte, n json.Number) ([]byte, error) {
format := ctx.Option.ColorScheme.Int
b = append(b, format.Header...)
bb, err := encoder.AppendNumber(ctx, b, n)
if err != nil {
return nil, err
}
return append(bb, format.Footer...), nil
}
func appendBool(ctx *encoder.RuntimeContext, b []byte, v bool) []byte {
format := ctx.Option.ColorScheme.Bool
b = append(b, format.Header...)
if v {
b = append(b, "true"...)
} else {
b = append(b, "false"...)
}
return append(b, format.Footer...)
}
func appendNull(ctx *encoder.RuntimeContext, b []byte) []byte {
format := ctx.Option.ColorScheme.Null
b = append(b, format.Header...)
b = append(b, "null"...)
return append(b, format.Footer...)
}
func appendComma(_ *encoder.RuntimeContext, b []byte) []byte {
return append(b, ',', '\n')
}
func appendColon(_ *encoder.RuntimeContext, b []byte) []byte {
return append(b, ':', ' ')
}
func appendInterface(ctx *encoder.RuntimeContext, codeSet *encoder.OpcodeSet, code *encoder.Opcode, b []byte, iface *emptyInterface, ptrOffset uintptr) ([]byte, error) {
ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(iface))
ifaceCodeSet, err := encoder.CompileToGetCodeSet(uintptr(unsafe.Pointer(iface.typ)))
if err != nil {
return nil, err
}
totalLength := uintptr(codeSet.CodeLength)
nextTotalLength := uintptr(ifaceCodeSet.CodeLength)
curlen := uintptr(len(ctx.Ptrs))
offsetNum := ptrOffset / uintptrSize
newLen := offsetNum + totalLength + nextTotalLength
if curlen < newLen {
ctx.Ptrs = append(ctx.Ptrs, make([]uintptr, newLen-curlen)...)
}
oldPtrs := ctx.Ptrs
newPtrs := ctx.Ptrs[(ptrOffset+totalLength*uintptrSize)/uintptrSize:]
newPtrs[0] = uintptr(iface.ptr)
ctx.Ptrs = newPtrs
oldBaseIndent := ctx.BaseIndent
ctx.BaseIndent = code.Indent
bb, err := Run(ctx, b, ifaceCodeSet)
if err != nil {
return nil, err
}
ctx.BaseIndent = oldBaseIndent
ctx.Ptrs = oldPtrs
return bb, nil
}
func appendMapKeyValue(ctx *encoder.RuntimeContext, code *encoder.Opcode, b, key, value []byte) []byte {
b = appendIndent(ctx, b, code.Indent+1)
b = append(b, key...)
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(_ *encoder.RuntimeContext, b []byte) []byte {
return append(b, '[', ']', ',', '\n')
}
func appendEmptyObject(_ *encoder.RuntimeContext, 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)
}
func appendMarshalText(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) {
format := ctx.Option.ColorScheme.String
b = append(b, format.Header...)
bb, err := encoder.AppendMarshalTextIndent(ctx, code, b, v)
if err != nil {
return nil, err
}
return append(bb, format.Footer...), nil
}
func appendStructHead(_ *encoder.RuntimeContext, b []byte) []byte {
return append(b, '{', '\n')
}
func appendStructKey(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte {
b = appendIndent(ctx, b, code.Indent)
format := ctx.Option.ColorScheme.ObjectKey
b = append(b, format.Header...)
b = append(b, code.Key[:len(code.Key)-1]...)
b = append(b, format.Footer...)
return append(b, ':', ' ')
}
func appendStructEndSkipLast(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte {
last := len(b) - 1
if b[last-1] == '{' {
b[last] = '}'
} else {
if b[last] == '\n' {
// to remove ',' and '\n' characters
b = b[:len(b)-2]
}
b = append(b, '\n')
b = appendIndent(ctx, b, code.Indent-1)
b = append(b, '}')
}
return appendComma(ctx, b)
}
func restoreIndent(ctx *encoder.RuntimeContext, code *encoder.Opcode, ctxptr uintptr) {
ctx.BaseIndent = uint32(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)
}

File diff suppressed because it is too large Load Diff

View File

@ -8,7 +8,7 @@ import (
func DebugRun(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]byte, error) { func DebugRun(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]byte, error) {
var code *encoder.Opcode var code *encoder.Opcode
if ctx.Option.HTMLEscape { if (ctx.Option.Flag & encoder.HTMLEscapeOption) != 0 {
code = codeSet.EscapeKeyCode code = codeSet.EscapeKeyCode
} else { } else {
code = codeSet.NoescapeKeyCode code = codeSet.NoescapeKeyCode

View File

@ -1,6 +1,9 @@
package vm_indent package vm_indent
import (
// HACK: compile order // HACK: compile order
// `vm`, `vm_indent` packages uses a lot of memory to compile, // `vm`, `vm_indent`, `vm_color`, `vm_color_indent` packages uses a lot of memory to compile,
// so forcibly make dependencies and avoid compiling in concurrent. // so forcibly make dependencies and avoid compiling in concurrent.
// dependency order: vm => vm_indent // dependency order: vm => vm_indent => vm_color => vm_color_indent
_ "github.com/goccy/go-json/internal/encoder/vm_color"
)

View File

@ -14,7 +14,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
ptrOffset := uintptr(0) ptrOffset := uintptr(0)
ctxptr := ctx.Ptr() ctxptr := ctx.Ptr()
var code *encoder.Opcode var code *encoder.Opcode
if ctx.Option.HTMLEscape { if (ctx.Option.Flag & encoder.HTMLEscapeOption) != 0 {
code = codeSet.EscapeKeyCode code = codeSet.EscapeKeyCode
} else { } else {
code = codeSet.NoescapeKeyCode code = codeSet.NoescapeKeyCode
@ -39,7 +39,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
store(ctxptr, code.Idx, p) store(ctxptr, code.Idx, p)
fallthrough fallthrough
case encoder.OpInt: case encoder.OpInt:
b = appendInt(b, ptrToUint64(load(ctxptr, code.Idx)), code) b = appendInt(ctx, b, ptrToUint64(load(ctxptr, code.Idx)), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
case encoder.OpUintPtr: case encoder.OpUintPtr:
@ -53,18 +53,18 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
store(ctxptr, code.Idx, p) store(ctxptr, code.Idx, p)
fallthrough fallthrough
case encoder.OpUint: case encoder.OpUint:
b = appendUint(b, ptrToUint64(load(ctxptr, code.Idx)), code) b = appendUint(ctx, b, ptrToUint64(load(ctxptr, code.Idx)), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
case encoder.OpIntString: case encoder.OpIntString:
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(load(ctxptr, code.Idx)), code) b = appendInt(ctx, b, ptrToUint64(load(ctxptr, code.Idx)), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
case encoder.OpUintString: case encoder.OpUintString:
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(load(ctxptr, code.Idx)), code) b = appendUint(ctx, b, ptrToUint64(load(ctxptr, code.Idx)), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -370,7 +370,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
store(ctxptr, code.ElemIdx, 0) store(ctxptr, code.ElemIdx, 0)
store(ctxptr, code.Length, uintptr(mlen)) store(ctxptr, code.Length, uintptr(mlen))
store(ctxptr, code.MapIter, uintptr(iter)) store(ctxptr, code.MapIter, uintptr(iter))
if ctx.Option.UnorderedMap { if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
b = appendMapKeyIndent(ctx, code.Next, b) b = appendMapKeyIndent(ctx, code.Next, b)
} else { } else {
mapCtx := encoder.NewMapContext(mlen) mapCtx := encoder.NewMapContext(mlen)
@ -385,7 +385,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
idx := load(ctxptr, code.ElemIdx) idx := load(ctxptr, code.ElemIdx)
length := load(ctxptr, code.Length) length := load(ctxptr, code.Length)
idx++ idx++
if ctx.Option.UnorderedMap { if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
if idx < length { if idx < length {
b = appendMapKeyIndent(ctx, code, b) b = appendMapKeyIndent(ctx, code, b)
store(ctxptr, code.ElemIdx, idx) store(ctxptr, code.ElemIdx, idx)
@ -414,7 +414,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
} }
} }
case encoder.OpMapValue: case encoder.OpMapValue:
if ctx.Option.UnorderedMap { if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
b = appendColon(ctx, b) b = appendColon(ctx, b)
} else { } else {
ptr := load(ctxptr, code.End.MapPos) ptr := load(ctxptr, code.End.MapPos)
@ -602,7 +602,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendStructHead(ctx, b) b = appendStructHead(ctx, b)
} }
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendInt(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendInt(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
case encoder.OpStructPtrHeadOmitEmptyInt: case encoder.OpStructPtrHeadOmitEmptyInt:
@ -638,7 +638,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
code = code.NextField code = code.NextField
} else { } else {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendInt(b, u64, code) b = appendInt(ctx, b, u64, code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
} }
@ -671,7 +671,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
} }
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendInt(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -709,7 +709,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
} else { } else {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendInt(b, u64, code) b = appendInt(ctx, b, u64, code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -746,7 +746,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
} }
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -780,7 +780,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
} }
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
code = code.Next code = code.Next
@ -817,7 +817,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
} }
b = appendComma(ctx, b) b = appendComma(ctx, b)
@ -853,7 +853,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
@ -886,7 +886,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendStructHead(ctx, b) b = appendStructHead(ctx, b)
} }
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendUint(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendUint(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
case encoder.OpStructPtrHeadOmitEmptyUint: case encoder.OpStructPtrHeadOmitEmptyUint:
@ -922,7 +922,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
code = code.NextField code = code.NextField
} else { } else {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendUint(b, u64, code) b = appendUint(ctx, b, u64, code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
} }
@ -955,7 +955,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
} }
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendUint(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -993,7 +993,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
} else { } else {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendUint(b, u64, code) b = appendUint(ctx, b, u64, code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -1030,7 +1030,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
} }
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -1064,7 +1064,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
} }
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
code = code.Next code = code.Next
@ -1101,7 +1101,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
} }
b = appendComma(ctx, b) b = appendComma(ctx, b)
@ -1137,7 +1137,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
@ -3413,7 +3413,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
case encoder.OpStructFieldInt: case encoder.OpStructFieldInt:
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendInt(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendInt(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
case encoder.OpStructFieldOmitEmptyInt: case encoder.OpStructFieldOmitEmptyInt:
@ -3422,7 +3422,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
v := u64 & ((1 << code.NumBitSize) - 1) v := u64 & ((1 << code.NumBitSize) - 1)
if v != 0 { if v != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendInt(b, u64, code) b = appendInt(ctx, b, u64, code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
code = code.Next code = code.Next
@ -3430,7 +3430,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendInt(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -3441,7 +3441,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if v != 0 { if v != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendInt(b, u64, code) b = appendInt(ctx, b, u64, code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
@ -3453,7 +3453,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
} }
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -3462,7 +3462,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
p = ptrToNPtr(p+uintptr(code.Offset), code.PtrNum) p = ptrToNPtr(p+uintptr(code.Offset), code.PtrNum)
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
code = code.Next code = code.Next
@ -3474,7 +3474,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
} }
b = appendComma(ctx, b) b = appendComma(ctx, b)
@ -3485,7 +3485,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
@ -3493,7 +3493,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
case encoder.OpStructFieldUint: case encoder.OpStructFieldUint:
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendUint(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendUint(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
case encoder.OpStructFieldOmitEmptyUint: case encoder.OpStructFieldOmitEmptyUint:
@ -3502,7 +3502,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
v := u64 & ((1 << code.NumBitSize) - 1) v := u64 & ((1 << code.NumBitSize) - 1)
if v != 0 { if v != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendUint(b, u64, code) b = appendUint(ctx, b, u64, code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
code = code.Next code = code.Next
@ -3510,7 +3510,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendUint(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -3521,7 +3521,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if v != 0 { if v != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendUint(b, u64, code) b = appendUint(ctx, b, u64, code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
@ -3533,7 +3533,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
} }
b = appendComma(ctx, b) b = appendComma(ctx, b)
code = code.Next code = code.Next
@ -3542,7 +3542,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
p = ptrToNPtr(p+uintptr(code.Offset), code.PtrNum) p = ptrToNPtr(p+uintptr(code.Offset), code.PtrNum)
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
code = code.Next code = code.Next
@ -3554,7 +3554,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
} }
b = appendComma(ctx, b) b = appendComma(ctx, b)
@ -3565,7 +3565,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
b = appendComma(ctx, b) b = appendComma(ctx, b)
} }
@ -4297,7 +4297,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
case encoder.OpStructEndInt: case encoder.OpStructEndInt:
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendInt(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendInt(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
case encoder.OpStructEndOmitEmptyInt: case encoder.OpStructEndOmitEmptyInt:
@ -4306,7 +4306,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
v := u64 & ((1 << code.NumBitSize) - 1) v := u64 & ((1 << code.NumBitSize) - 1)
if v != 0 { if v != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendInt(b, u64, code) b = appendInt(ctx, b, u64, code)
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
b = appendStructEndSkipLast(ctx, code, b) b = appendStructEndSkipLast(ctx, code, b)
@ -4316,7 +4316,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendInt(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = append(b, '"') b = append(b, '"')
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
@ -4327,7 +4327,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if v != 0 { if v != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendInt(b, u64, code) b = appendInt(ctx, b, u64, code)
b = append(b, '"') b = append(b, '"')
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
@ -4341,7 +4341,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
} }
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
@ -4350,7 +4350,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
p = ptrToNPtr(p+uintptr(code.Offset), code.PtrNum) p = ptrToNPtr(p+uintptr(code.Offset), code.PtrNum)
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
b = appendStructEndSkipLast(ctx, code, b) b = appendStructEndSkipLast(ctx, code, b)
@ -4364,7 +4364,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
} }
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
@ -4375,7 +4375,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendInt(b, ptrToUint64(p), code) b = appendInt(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
@ -4385,7 +4385,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
case encoder.OpStructEndUint: case encoder.OpStructEndUint:
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendUint(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendUint(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
case encoder.OpStructEndOmitEmptyUint: case encoder.OpStructEndOmitEmptyUint:
@ -4394,7 +4394,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
v := u64 & ((1 << code.NumBitSize) - 1) v := u64 & ((1 << code.NumBitSize) - 1)
if v != 0 { if v != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendUint(b, u64, code) b = appendUint(ctx, b, u64, code)
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
b = appendStructEndSkipLast(ctx, code, b) b = appendStructEndSkipLast(ctx, code, b)
@ -4404,7 +4404,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(p+uintptr(code.Offset)), code) b = appendUint(ctx, b, ptrToUint64(p+uintptr(code.Offset)), code)
b = append(b, '"') b = append(b, '"')
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
@ -4415,7 +4415,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if v != 0 { if v != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendUint(b, u64, code) b = appendUint(ctx, b, u64, code)
b = append(b, '"') b = append(b, '"')
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
@ -4429,7 +4429,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
} }
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
@ -4438,7 +4438,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
p = ptrToNPtr(p+uintptr(code.Offset), code.PtrNum) p = ptrToNPtr(p+uintptr(code.Offset), code.PtrNum)
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
b = appendStructEndSkipLast(ctx, code, b) b = appendStructEndSkipLast(ctx, code, b)
@ -4452,7 +4452,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendNull(ctx, b) b = appendNull(ctx, b)
} else { } else {
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
} }
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
@ -4463,7 +4463,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p != 0 { if p != 0 {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = append(b, '"') b = append(b, '"')
b = appendUint(b, ptrToUint64(p), code) b = appendUint(ctx, b, ptrToUint64(p), code)
b = append(b, '"') b = append(b, '"')
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {

View File

@ -10,12 +10,19 @@ type EncodeOptionFunc func(*EncodeOption)
func UnorderedMap() EncodeOptionFunc { func UnorderedMap() EncodeOptionFunc {
return func(opt *EncodeOption) { return func(opt *EncodeOption) {
opt.UnorderedMap = true opt.Flag |= encoder.UnorderedMapOption
} }
} }
func Debug() EncodeOptionFunc { func Debug() EncodeOptionFunc {
return func(opt *EncodeOption) { return func(opt *EncodeOption) {
opt.Debug = true opt.Flag |= encoder.DebugOption
}
}
func Colorize(scheme *ColorScheme) EncodeOptionFunc {
return func(opt *EncodeOption) {
opt.Flag |= encoder.ColorizeOption
opt.ColorScheme = scheme
} }
} }