Refactor escape string

This commit is contained in:
Masaaki Goshima 2021-06-01 01:25:05 +09:00
parent 8c93c749f9
commit 11437c8556
23 changed files with 171 additions and 10704 deletions

View File

@ -6,8 +6,6 @@ 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_escaped"
"github.com/goccy/go-json/internal/encoder/vm_escaped_indent"
"github.com/goccy/go-json/internal/encoder/vm_indent" "github.com/goccy/go-json/internal/encoder/vm_indent"
) )
@ -186,13 +184,7 @@ func encode(ctx *encoder.RuntimeContext, v interface{}) ([]byte, error) {
} }
p := uintptr(header.ptr) p := uintptr(header.ptr)
var code *encoder.Opcode ctx.Init(p, codeSet.CodeLength)
if ctx.Option.HTMLEscape {
code = codeSet.EscapeKeyCode
} else {
code = codeSet.NoescapeKeyCode
}
ctx.Init(code, p, codeSet.CodeLength)
ctx.KeepRefs = append(ctx.KeepRefs, header.ptr) ctx.KeepRefs = append(ctx.KeepRefs, header.ptr)
buf, err := encodeRunCode(ctx, b, codeSet) buf, err := encodeRunCode(ctx, b, codeSet)
@ -219,14 +211,8 @@ func encodeNoEscape(ctx *encoder.RuntimeContext, v interface{}) ([]byte, error)
return nil, err return nil, err
} }
var code *encoder.Opcode
if ctx.Option.HTMLEscape {
code = codeSet.EscapeKeyCode
} else {
code = codeSet.NoescapeKeyCode
}
p := uintptr(header.ptr) p := uintptr(header.ptr)
ctx.Init(code, p, codeSet.CodeLength) ctx.Init(p, codeSet.CodeLength)
buf, err := encodeRunCode(ctx, b, codeSet) buf, err := encodeRunCode(ctx, b, codeSet)
if err != nil { if err != nil {
return nil, err return nil, err
@ -252,14 +238,8 @@ func encodeIndent(ctx *encoder.RuntimeContext, v interface{}, prefix, indent str
return nil, err return nil, err
} }
var code *encoder.Opcode
if ctx.Option.HTMLEscape {
code = codeSet.EscapeKeyCode
} else {
code = codeSet.NoescapeKeyCode
}
p := uintptr(header.ptr) p := uintptr(header.ptr)
ctx.Init(code, p, codeSet.CodeLength) ctx.Init(p, codeSet.CodeLength)
buf, err := encodeRunIndentCode(ctx, b, codeSet, prefix, indent) buf, err := encodeRunIndentCode(ctx, b, codeSet, prefix, indent)
ctx.KeepRefs = append(ctx.KeepRefs, header.ptr) ctx.KeepRefs = append(ctx.KeepRefs, header.ptr)
@ -274,40 +254,20 @@ 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.Debug {
return encodeDebugRunCode(ctx, b, codeSet) return vm.DebugRun(ctx, b, codeSet)
}
if ctx.Option.HTMLEscape {
return vm_escaped.Run(ctx, b, codeSet)
} }
return vm.Run(ctx, b, codeSet) return vm.Run(ctx, b, codeSet)
} }
func encodeDebugRunCode(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]byte, error) {
if ctx.Option.HTMLEscape {
return vm_escaped.DebugRun(ctx, b, codeSet)
}
return vm.DebugRun(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.Debug {
return encodeDebugRunIndentCode(ctx, b, codeSet) return vm_indent.DebugRun(ctx, b, codeSet)
}
if ctx.Option.HTMLEscape {
return vm_escaped_indent.Run(ctx, b, codeSet)
} }
return vm_indent.Run(ctx, b, codeSet) return vm_indent.Run(ctx, b, codeSet)
} }
func encodeDebugRunIndentCode(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]byte, error) {
if ctx.Option.HTMLEscape {
return vm_escaped_indent.DebugRun(ctx, b, codeSet)
}
return vm_indent.DebugRun(ctx, b, codeSet)
}
func initOption(opt *EncodeOption) { func initOption(opt *EncodeOption) {
opt.HTMLEscape = false opt.HTMLEscape = false
opt.Indent = false opt.Indent = 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", "vm_escaped", "vm_escaped_indent"} { for _, pkg := range []string{"vm", "vm_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

@ -13,7 +13,12 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
recursiveLevel := 0 recursiveLevel := 0
ptrOffset := uintptr(0) ptrOffset := uintptr(0)
ctxptr := ctx.Ptr() ctxptr := ctx.Ptr()
code := ctx.Code var code *encoder.Opcode
if ctx.Option.HTMLEscape {
code = codeSet.EscapeKeyCode
} else {
code = codeSet.NoescapeKeyCode
}
for { for {
switch code.Op { switch code.Op {
@ -106,7 +111,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.OpString: case encoder.OpString:
b = appendString(b, ptrToString(load(ctxptr, code.Idx))) b = appendString(ctx, b, ptrToString(load(ctxptr, code.Idx)))
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
case encoder.OpBoolPtr: case encoder.OpBoolPtr:
@ -242,7 +247,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if (code.Flags&encoder.IsNilableTypeFlags) != 0 && (code.Flags&encoder.IndirectFlags) != 0 { if (code.Flags&encoder.IsNilableTypeFlags) != 0 && (code.Flags&encoder.IndirectFlags) != 0 {
p = ptrToPtr(p) p = ptrToPtr(p)
} }
bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) bb, err := appendMarshalText(ctx, code, b, ptrToInterface(code, p))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -1759,7 +1764,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendStructHead(b) b = appendStructHead(b)
} }
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendString(b, ptrToString(p+uintptr(code.Offset))) b = appendString(ctx, b, ptrToString(p+uintptr(code.Offset)))
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
case encoder.OpStructPtrHeadOmitEmptyString: case encoder.OpStructPtrHeadOmitEmptyString:
@ -1794,7 +1799,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 = appendString(b, v) b = appendString(ctx, b, v)
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
} }
@ -1826,7 +1831,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendStructHead(b) b = appendStructHead(b)
} }
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendString(b, string(appendString([]byte{}, ptrToString(p+uintptr(code.Offset))))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, ptrToString(p+uintptr(code.Offset)))))
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
case encoder.OpStructPtrHeadOmitEmptyStringString: case encoder.OpStructPtrHeadOmitEmptyStringString:
@ -1861,7 +1866,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 = appendString(b, string(appendString([]byte{}, v))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, v)))
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
} }
@ -1897,7 +1902,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
b = appendString(b, ptrToString(p)) b = appendString(ctx, b, ptrToString(p))
} }
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
@ -1931,7 +1936,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 = appendString(b, ptrToString(p)) b = appendString(ctx, b, ptrToString(p))
b = appendComma(b) b = appendComma(b)
} }
code = code.Next code = code.Next
@ -1967,7 +1972,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
b = appendString(b, string(appendString([]byte{}, ptrToString(p)))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, ptrToString(p))))
} }
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
@ -2001,7 +2006,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 = appendString(b, string(appendString([]byte{}, ptrToString(p)))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, ptrToString(p))))
b = appendComma(b) b = appendComma(b)
} }
code = code.Next code = code.Next
@ -3256,7 +3261,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 && (code.Flags&encoder.NilCheckFlags) != 0 { if p == 0 && (code.Flags&encoder.NilCheckFlags) != 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) bb, err := appendMarshalText(ctx, code, b, ptrToInterface(code, p))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3300,7 +3305,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)
bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) bb, err := appendMarshalText(ctx, code, b, ptrToInterface(code, p))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3340,7 +3345,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) bb, err := appendMarshalText(ctx, code, b, ptrToInterface(code, p))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3380,7 +3385,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)
bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) bb, err := appendMarshalText(ctx, code, b, ptrToInterface(code, p))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3756,7 +3761,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
case encoder.OpStructFieldString: case encoder.OpStructFieldString:
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendString(b, ptrToString(p+uintptr(code.Offset))) b = appendString(ctx, b, ptrToString(p+uintptr(code.Offset)))
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
case encoder.OpStructFieldOmitEmptyString: case encoder.OpStructFieldOmitEmptyString:
@ -3764,7 +3769,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
v := ptrToString(p + uintptr(code.Offset)) v := ptrToString(p + uintptr(code.Offset))
if v != "" { if v != "" {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendString(b, v) b = appendString(ctx, b, v)
b = appendComma(b) b = appendComma(b)
} }
code = code.Next code = code.Next
@ -3772,7 +3777,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
s := ptrToString(p + uintptr(code.Offset)) s := ptrToString(p + uintptr(code.Offset))
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendString(b, string(appendString([]byte{}, s))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, s)))
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
case encoder.OpStructFieldOmitEmptyStringString: case encoder.OpStructFieldOmitEmptyStringString:
@ -3780,7 +3785,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
v := ptrToString(p + uintptr(code.Offset)) v := ptrToString(p + uintptr(code.Offset))
if v != "" { if v != "" {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendString(b, string(appendString([]byte{}, v))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, v)))
b = appendComma(b) b = appendComma(b)
} }
code = code.Next code = code.Next
@ -3791,7 +3796,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
b = appendString(b, ptrToString(p)) b = appendString(ctx, b, ptrToString(p))
} }
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
@ -3800,7 +3805,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 = appendString(b, ptrToString(p)) b = appendString(ctx, b, ptrToString(p))
b = appendComma(b) b = appendComma(b)
} }
code = code.Next code = code.Next
@ -3811,7 +3816,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
b = appendString(b, string(appendString([]byte{}, ptrToString(p)))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, ptrToString(p))))
} }
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
@ -3820,7 +3825,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 = appendString(b, string(appendString([]byte{}, ptrToString(p)))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, ptrToString(p))))
b = appendComma(b) b = appendComma(b)
} }
code = code.Next code = code.Next
@ -4117,7 +4122,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 && (code.Flags&encoder.NilCheckFlags) != 0 { if p == 0 && (code.Flags&encoder.NilCheckFlags) != 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) bb, err := appendMarshalText(ctx, code, b, ptrToInterface(code, p))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -4136,7 +4141,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
break break
} }
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) bb, err := appendMarshalText(ctx, code, b, ptrToInterface(code, p))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -4149,7 +4154,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) bb, err := appendMarshalText(ctx, code, b, ptrToInterface(code, p))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -4162,7 +4167,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)
bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) bb, err := appendMarshalText(ctx, code, b, ptrToInterface(code, p))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -4672,7 +4677,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
case encoder.OpStructEndString: case encoder.OpStructEndString:
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendString(b, ptrToString(p+uintptr(code.Offset))) b = appendString(ctx, b, ptrToString(p+uintptr(code.Offset)))
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
case encoder.OpStructEndOmitEmptyString: case encoder.OpStructEndOmitEmptyString:
@ -4680,7 +4685,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
v := ptrToString(p + uintptr(code.Offset)) v := ptrToString(p + uintptr(code.Offset))
if v != "" { if v != "" {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendString(b, v) b = appendString(ctx, b, v)
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
b = appendStructEndSkipLast(ctx, code, b) b = appendStructEndSkipLast(ctx, code, b)
@ -4690,7 +4695,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)
s := ptrToString(p + uintptr(code.Offset)) s := ptrToString(p + uintptr(code.Offset))
b = appendString(b, string(appendString([]byte{}, s))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, s)))
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
case encoder.OpStructEndOmitEmptyStringString: case encoder.OpStructEndOmitEmptyStringString:
@ -4698,7 +4703,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
v := ptrToString(p + uintptr(code.Offset)) v := ptrToString(p + uintptr(code.Offset))
if v != "" { if v != "" {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendString(b, string(appendString([]byte{}, v))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, v)))
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
b = appendStructEndSkipLast(ctx, code, b) b = appendStructEndSkipLast(ctx, code, b)
@ -4711,7 +4716,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
b = appendString(b, ptrToString(p)) b = appendString(ctx, b, ptrToString(p))
} }
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
@ -4720,7 +4725,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 = appendString(b, ptrToString(p)) b = appendString(ctx, b, ptrToString(p))
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
b = appendStructEndSkipLast(ctx, code, b) b = appendStructEndSkipLast(ctx, code, b)
@ -4733,7 +4738,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
b = appendString(b, string(appendString([]byte{}, ptrToString(p)))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, ptrToString(p))))
} }
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
@ -4742,7 +4747,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 = appendString(b, string(appendString([]byte{}, ptrToString(p)))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, ptrToString(p))))
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
b = appendStructEndSkipLast(ctx, code, b) b = appendStructEndSkipLast(ctx, code, b)

View File

@ -1456,7 +1456,8 @@ func compileStruct(ctx *compileContext, isPtr bool) (*Opcode, error) {
} }
var key string var key string
if ctx.escapeKey { if ctx.escapeKey {
key = fmt.Sprintf(`%s:`, string(AppendEscapedString([]byte{}, tag.Key))) rctx := &RuntimeContext{Option: &Option{HTMLEscape: true}}
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

@ -112,11 +112,10 @@ type RuntimeContext struct {
BaseIndent uint32 BaseIndent uint32
Prefix []byte Prefix []byte
IndentStr []byte IndentStr []byte
Code *Opcode
Option *Option Option *Option
} }
func (c *RuntimeContext) Init(code *Opcode, p uintptr, codelen int) { func (c *RuntimeContext) Init(p uintptr, codelen int) {
if len(c.Ptrs) < codelen { if len(c.Ptrs) < codelen {
c.Ptrs = make([]uintptr, codelen) c.Ptrs = make([]uintptr, codelen)
} }
@ -124,7 +123,6 @@ func (c *RuntimeContext) Init(code *Opcode, p uintptr, codelen int) {
c.KeepRefs = c.KeepRefs[:0] c.KeepRefs = c.KeepRefs[:0]
c.SeenPtr = c.SeenPtr[:0] c.SeenPtr = c.SeenPtr[:0]
c.BaseIndent = 0 c.BaseIndent = 0
c.Code = code
} }
func (c *RuntimeContext) Ptr() uintptr { func (c *RuntimeContext) Ptr() uintptr {

View File

@ -353,7 +353,7 @@ func AppendNumber(b []byte, n json.Number) ([]byte, error) {
return b, nil return b, nil
} }
func AppendMarshalJSON(ctx *RuntimeContext, code *Opcode, b []byte, v interface{}, escape bool) ([]byte, error) { func AppendMarshalJSON(ctx *RuntimeContext, code *Opcode, b []byte, v interface{}) ([]byte, error) {
rv := reflect.ValueOf(v) // convert by dynamic interface type rv := reflect.ValueOf(v) // convert by dynamic interface type
if (code.Flags & AddrForMarshalerFlags) != 0 { if (code.Flags & AddrForMarshalerFlags) != 0 {
if rv.CanAddr() { if rv.CanAddr() {
@ -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, escape) compactedBuf, err := compact(b, marshalBuf, ctx.Option.HTMLEscape)
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}
} }
@ -383,7 +383,7 @@ func AppendMarshalJSON(ctx *RuntimeContext, code *Opcode, b []byte, v interface{
return compactedBuf, nil return compactedBuf, nil
} }
func AppendMarshalJSONIndent(ctx *RuntimeContext, code *Opcode, b []byte, v interface{}, escape bool) ([]byte, error) { func AppendMarshalJSONIndent(ctx *RuntimeContext, code *Opcode, b []byte, v interface{}) ([]byte, error) {
rv := reflect.ValueOf(v) // convert by dynamic interface type rv := reflect.ValueOf(v) // convert by dynamic interface type
if (code.Flags & AddrForMarshalerFlags) != 0 { if (code.Flags & AddrForMarshalerFlags) != 0 {
if rv.CanAddr() { if rv.CanAddr() {
@ -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),
escape, ctx.Option.HTMLEscape,
) )
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}
@ -419,7 +419,7 @@ func AppendMarshalJSONIndent(ctx *RuntimeContext, code *Opcode, b []byte, v inte
return indentedBuf, nil return indentedBuf, nil
} }
func AppendMarshalText(code *Opcode, b []byte, v interface{}, escape bool) ([]byte, error) { func AppendMarshalText(ctx *RuntimeContext, code *Opcode, b []byte, v interface{}) ([]byte, error) {
rv := reflect.ValueOf(v) // convert by dynamic interface type rv := reflect.ValueOf(v) // convert by dynamic interface type
if (code.Flags & AddrForMarshalerFlags) != 0 { if (code.Flags & AddrForMarshalerFlags) != 0 {
if rv.CanAddr() { if rv.CanAddr() {
@ -439,13 +439,10 @@ func AppendMarshalText(code *Opcode, b []byte, v interface{}, escape bool) ([]by
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}
} }
if escape { return AppendString(ctx, b, *(*string)(unsafe.Pointer(&bytes))), nil
return AppendEscapedString(b, *(*string)(unsafe.Pointer(&bytes))), nil
}
return AppendString(b, *(*string)(unsafe.Pointer(&bytes))), nil
} }
func AppendMarshalTextIndent(code *Opcode, b []byte, v interface{}, escape bool) ([]byte, error) { func AppendMarshalTextIndent(ctx *RuntimeContext, code *Opcode, b []byte, v interface{}) ([]byte, error) {
rv := reflect.ValueOf(v) // convert by dynamic interface type rv := reflect.ValueOf(v) // convert by dynamic interface type
if (code.Flags & AddrForMarshalerFlags) != 0 { if (code.Flags & AddrForMarshalerFlags) != 0 {
if rv.CanAddr() { if rv.CanAddr() {
@ -465,10 +462,7 @@ func AppendMarshalTextIndent(code *Opcode, b []byte, v interface{}, escape bool)
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}
} }
if escape { return AppendString(ctx, b, *(*string)(unsafe.Pointer(&bytes))), nil
return AppendEscapedString(b, *(*string)(unsafe.Pointer(&bytes))), nil
}
return AppendString(b, *(*string)(unsafe.Pointer(&bytes))), nil
} }
func AppendNull(b []byte) []byte { func AppendNull(b []byte) []byte {

View File

@ -405,7 +405,10 @@ func stringToUint64Slice(s string) []uint64 {
})) }))
} }
func AppendEscapedString(buf []byte, s string) []byte { func AppendString(ctx *RuntimeContext, buf []byte, s string) []byte {
if !ctx.Option.HTMLEscape {
return appendString(buf, s)
}
valLen := len(s) valLen := len(s)
if valLen == 0 { if valLen == 0 {
return append(buf, `""`...) return append(buf, `""`...)
@ -531,7 +534,7 @@ ESCAPE_END:
return append(append(buf, s[i:]...), '"') return append(append(buf, s[i:]...), '"')
} }
func AppendString(buf []byte, s string) []byte { func appendString(buf []byte, s string) []byte {
valLen := len(s) valLen := len(s)
if valLen == 0 { if valLen == 0 {
return append(buf, `""`...) return append(buf, `""`...)

View File

@ -8,13 +8,20 @@ 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
if ctx.Option.HTMLEscape {
code = codeSet.EscapeKeyCode
} else {
code = codeSet.NoescapeKeyCode
}
if err := recover(); err != nil { if err := recover(); err != nil {
fmt.Println("=============[DEBUG]===============") fmt.Println("=============[DEBUG]===============")
fmt.Println("* [TYPE]") fmt.Println("* [TYPE]")
fmt.Println(codeSet.Type) fmt.Println(codeSet.Type)
fmt.Printf("\n") fmt.Printf("\n")
fmt.Println("* [ALL OPCODE]") fmt.Println("* [ALL OPCODE]")
fmt.Println(ctx.Code.Dump()) fmt.Println(code.Dump())
fmt.Printf("\n") fmt.Printf("\n")
fmt.Println("* [CONTEXT]") fmt.Println("* [CONTEXT]")
fmt.Printf("%+v\n", ctx) fmt.Printf("%+v\n", ctx)

View File

@ -2,8 +2,8 @@ package vm
import ( import (
// HACK: compile order // HACK: compile order
// `vm`, `vm_escaped`, `vm_indent`, `vm_escaped_indent` packages uses a lot of memory to compile, // `vm`, `vm_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_escaped => vm_indent => vm_escaped_indent // dependency order: vm => vm_indent
_ "github.com/goccy/go-json/internal/encoder/vm_escaped" _ "github.com/goccy/go-json/internal/encoder/vm_indent"
) )

View File

@ -146,7 +146,6 @@ func appendInterface(ctx *encoder.RuntimeContext, codeSet *encoder.OpcodeSet, _
newPtrs[0] = uintptr(iface.ptr) newPtrs[0] = uintptr(iface.ptr)
ctx.Ptrs = newPtrs ctx.Ptrs = newPtrs
ctx.Code = ifaceCodeSet.NoescapeKeyCode
bb, err := Run(ctx, b, ifaceCodeSet) bb, err := Run(ctx, b, ifaceCodeSet)
if err != nil { if err != nil {
@ -158,11 +157,11 @@ func appendInterface(ctx *encoder.RuntimeContext, codeSet *encoder.OpcodeSet, _
} }
func appendMarshalJSON(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) { func appendMarshalJSON(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) {
return encoder.AppendMarshalJSON(ctx, code, b, v, false) return encoder.AppendMarshalJSON(ctx, code, b, v)
} }
func appendMarshalText(code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) { func appendMarshalText(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) {
return encoder.AppendMarshalText(code, b, v, false) return encoder.AppendMarshalText(ctx, code, b, v)
} }
func appendArrayHead(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { func appendArrayHead(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte {

View File

@ -13,7 +13,12 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
recursiveLevel := 0 recursiveLevel := 0
ptrOffset := uintptr(0) ptrOffset := uintptr(0)
ctxptr := ctx.Ptr() ctxptr := ctx.Ptr()
code := ctx.Code var code *encoder.Opcode
if ctx.Option.HTMLEscape {
code = codeSet.EscapeKeyCode
} else {
code = codeSet.NoescapeKeyCode
}
for { for {
switch code.Op { switch code.Op {
@ -106,7 +111,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.OpString: case encoder.OpString:
b = appendString(b, ptrToString(load(ctxptr, code.Idx))) b = appendString(ctx, b, ptrToString(load(ctxptr, code.Idx)))
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
case encoder.OpBoolPtr: case encoder.OpBoolPtr:
@ -242,7 +247,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if (code.Flags&encoder.IsNilableTypeFlags) != 0 && (code.Flags&encoder.IndirectFlags) != 0 { if (code.Flags&encoder.IsNilableTypeFlags) != 0 && (code.Flags&encoder.IndirectFlags) != 0 {
p = ptrToPtr(p) p = ptrToPtr(p)
} }
bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) bb, err := appendMarshalText(ctx, code, b, ptrToInterface(code, p))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -1759,7 +1764,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendStructHead(b) b = appendStructHead(b)
} }
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendString(b, ptrToString(p+uintptr(code.Offset))) b = appendString(ctx, b, ptrToString(p+uintptr(code.Offset)))
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
case encoder.OpStructPtrHeadOmitEmptyString: case encoder.OpStructPtrHeadOmitEmptyString:
@ -1794,7 +1799,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 = appendString(b, v) b = appendString(ctx, b, v)
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
} }
@ -1826,7 +1831,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendStructHead(b) b = appendStructHead(b)
} }
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendString(b, string(appendString([]byte{}, ptrToString(p+uintptr(code.Offset))))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, ptrToString(p+uintptr(code.Offset)))))
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
case encoder.OpStructPtrHeadOmitEmptyStringString: case encoder.OpStructPtrHeadOmitEmptyStringString:
@ -1861,7 +1866,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 = appendString(b, string(appendString([]byte{}, v))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, v)))
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
} }
@ -1897,7 +1902,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
b = appendString(b, ptrToString(p)) b = appendString(ctx, b, ptrToString(p))
} }
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
@ -1931,7 +1936,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 = appendString(b, ptrToString(p)) b = appendString(ctx, b, ptrToString(p))
b = appendComma(b) b = appendComma(b)
} }
code = code.Next code = code.Next
@ -1967,7 +1972,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
b = appendString(b, string(appendString([]byte{}, ptrToString(p)))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, ptrToString(p))))
} }
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
@ -2001,7 +2006,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 = appendString(b, string(appendString([]byte{}, ptrToString(p)))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, ptrToString(p))))
b = appendComma(b) b = appendComma(b)
} }
code = code.Next code = code.Next
@ -3256,7 +3261,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 && (code.Flags&encoder.NilCheckFlags) != 0 { if p == 0 && (code.Flags&encoder.NilCheckFlags) != 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) bb, err := appendMarshalText(ctx, code, b, ptrToInterface(code, p))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3300,7 +3305,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)
bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) bb, err := appendMarshalText(ctx, code, b, ptrToInterface(code, p))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3340,7 +3345,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) bb, err := appendMarshalText(ctx, code, b, ptrToInterface(code, p))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3380,7 +3385,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)
bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) bb, err := appendMarshalText(ctx, code, b, ptrToInterface(code, p))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3756,7 +3761,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
case encoder.OpStructFieldString: case encoder.OpStructFieldString:
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendString(b, ptrToString(p+uintptr(code.Offset))) b = appendString(ctx, b, ptrToString(p+uintptr(code.Offset)))
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
case encoder.OpStructFieldOmitEmptyString: case encoder.OpStructFieldOmitEmptyString:
@ -3764,7 +3769,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
v := ptrToString(p + uintptr(code.Offset)) v := ptrToString(p + uintptr(code.Offset))
if v != "" { if v != "" {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendString(b, v) b = appendString(ctx, b, v)
b = appendComma(b) b = appendComma(b)
} }
code = code.Next code = code.Next
@ -3772,7 +3777,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
s := ptrToString(p + uintptr(code.Offset)) s := ptrToString(p + uintptr(code.Offset))
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendString(b, string(appendString([]byte{}, s))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, s)))
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
case encoder.OpStructFieldOmitEmptyStringString: case encoder.OpStructFieldOmitEmptyStringString:
@ -3780,7 +3785,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
v := ptrToString(p + uintptr(code.Offset)) v := ptrToString(p + uintptr(code.Offset))
if v != "" { if v != "" {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendString(b, string(appendString([]byte{}, v))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, v)))
b = appendComma(b) b = appendComma(b)
} }
code = code.Next code = code.Next
@ -3791,7 +3796,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
b = appendString(b, ptrToString(p)) b = appendString(ctx, b, ptrToString(p))
} }
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
@ -3800,7 +3805,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 = appendString(b, ptrToString(p)) b = appendString(ctx, b, ptrToString(p))
b = appendComma(b) b = appendComma(b)
} }
code = code.Next code = code.Next
@ -3811,7 +3816,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
b = appendString(b, string(appendString([]byte{}, ptrToString(p)))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, ptrToString(p))))
} }
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
@ -3820,7 +3825,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 = appendString(b, string(appendString([]byte{}, ptrToString(p)))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, ptrToString(p))))
b = appendComma(b) b = appendComma(b)
} }
code = code.Next code = code.Next
@ -4117,7 +4122,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 && (code.Flags&encoder.NilCheckFlags) != 0 { if p == 0 && (code.Flags&encoder.NilCheckFlags) != 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) bb, err := appendMarshalText(ctx, code, b, ptrToInterface(code, p))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -4136,7 +4141,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
break break
} }
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) bb, err := appendMarshalText(ctx, code, b, ptrToInterface(code, p))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -4149,7 +4154,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) bb, err := appendMarshalText(ctx, code, b, ptrToInterface(code, p))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -4162,7 +4167,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)
bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) bb, err := appendMarshalText(ctx, code, b, ptrToInterface(code, p))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -4672,7 +4677,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
case encoder.OpStructEndString: case encoder.OpStructEndString:
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendString(b, ptrToString(p+uintptr(code.Offset))) b = appendString(ctx, b, ptrToString(p+uintptr(code.Offset)))
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
case encoder.OpStructEndOmitEmptyString: case encoder.OpStructEndOmitEmptyString:
@ -4680,7 +4685,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
v := ptrToString(p + uintptr(code.Offset)) v := ptrToString(p + uintptr(code.Offset))
if v != "" { if v != "" {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendString(b, v) b = appendString(ctx, b, v)
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
b = appendStructEndSkipLast(ctx, code, b) b = appendStructEndSkipLast(ctx, code, b)
@ -4690,7 +4695,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)
s := ptrToString(p + uintptr(code.Offset)) s := ptrToString(p + uintptr(code.Offset))
b = appendString(b, string(appendString([]byte{}, s))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, s)))
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
case encoder.OpStructEndOmitEmptyStringString: case encoder.OpStructEndOmitEmptyStringString:
@ -4698,7 +4703,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
v := ptrToString(p + uintptr(code.Offset)) v := ptrToString(p + uintptr(code.Offset))
if v != "" { if v != "" {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendString(b, string(appendString([]byte{}, v))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, v)))
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
b = appendStructEndSkipLast(ctx, code, b) b = appendStructEndSkipLast(ctx, code, b)
@ -4711,7 +4716,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
b = appendString(b, ptrToString(p)) b = appendString(ctx, b, ptrToString(p))
} }
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
@ -4720,7 +4725,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 = appendString(b, ptrToString(p)) b = appendString(ctx, b, ptrToString(p))
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
b = appendStructEndSkipLast(ctx, code, b) b = appendStructEndSkipLast(ctx, code, b)
@ -4733,7 +4738,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
b = appendString(b, string(appendString([]byte{}, ptrToString(p)))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, ptrToString(p))))
} }
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
@ -4742,7 +4747,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 = appendString(b, string(appendString([]byte{}, ptrToString(p)))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, ptrToString(p))))
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
b = appendStructEndSkipLast(ctx, code, b) b = appendStructEndSkipLast(ctx, code, b)

View File

@ -1,27 +0,0 @@
package vm_escaped
import (
"fmt"
"github.com/goccy/go-json/internal/encoder"
)
func DebugRun(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]byte, error) {
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(ctx.Code.Dump())
fmt.Printf("\n")
fmt.Println("* [CONTEXT]")
fmt.Printf("%+v\n", ctx)
fmt.Println("===================================")
panic(err)
}
}()
return Run(ctx, b, codeSet)
}

View File

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

View File

@ -1,216 +0,0 @@
package vm_escaped
import (
"encoding/json"
"fmt"
"unsafe"
"github.com/goccy/go-json/internal/encoder"
"github.com/goccy/go-json/internal/runtime"
)
const uintptrSize = 4 << (^uintptr(0) >> 63)
var (
appendInt = encoder.AppendInt
appendUint = encoder.AppendUint
appendFloat32 = encoder.AppendFloat32
appendFloat64 = encoder.AppendFloat64
appendString = encoder.AppendEscapedString
appendByteSlice = encoder.AppendByteSlice
appendNumber = encoder.AppendNumber
errUnsupportedValue = encoder.ErrUnsupportedValue
errUnsupportedFloat = encoder.ErrUnsupportedFloat
mapiterinit = encoder.MapIterInit
mapiterkey = encoder.MapIterKey
mapitervalue = encoder.MapIterValue
mapiternext = encoder.MapIterNext
maplen = encoder.MapLen
)
type emptyInterface struct {
typ *runtime.Type
ptr unsafe.Pointer
}
func errUnimplementedOp(op encoder.OpType) error {
return fmt.Errorf("encoder (escaped): opcode %s has not been implemented", op)
}
func load(base uintptr, idx 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 appendBool(b []byte, v bool) []byte {
if v {
return append(b, "true"...)
}
return append(b, "false"...)
}
func appendNull(b []byte) []byte {
return append(b, "null"...)
}
func appendComma(b []byte) []byte {
return append(b, ',')
}
func appendColon(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...)
b[len(b)-1] = ':'
return append(b, value...)
}
func appendMapEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte {
b[len(b)-1] = '}'
b = append(b, ',')
return b
}
func appendInterface(ctx *encoder.RuntimeContext, codeSet *encoder.OpcodeSet, _ *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
ctx.Code = ifaceCodeSet.EscapeKeyCode
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, true)
}
func appendMarshalText(code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) {
return encoder.AppendMarshalText(code, b, v, true)
}
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(b []byte) []byte {
return append(b, '[', ']', ',')
}
func appendEmptyObject(b []byte) []byte {
return append(b, '{', '}', ',')
}
func appendObjectEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte {
last := len(b) - 1
b[last] = '}'
return append(b, ',')
}
func appendStructHead(b []byte) []byte {
return append(b, '{')
}
func appendStructKey(_ *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte {
return append(b, code.Key...)
}
func appendStructEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte {
return append(b, '}', ',')
}
func appendStructEndSkipLast(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte {
last := len(b) - 1
if b[last] == ',' {
b[last] = '}'
return appendComma(b)
}
return appendStructEnd(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

@ -1,27 +0,0 @@
package vm_escaped_indent
import (
"fmt"
"github.com/goccy/go-json/internal/encoder"
)
func DebugRun(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]byte, error) {
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(ctx.Code.Dump())
fmt.Printf("\n")
fmt.Println("* [CONTEXT]")
fmt.Printf("%+v\n", ctx)
fmt.Println("===================================")
panic(err)
}
}()
return Run(ctx, b, codeSet)
}

View File

@ -1,6 +0,0 @@
package vm_escaped_indent
// HACK: compile order
// `vm`, `vm_escaped`, `vm_indent`, `vm_escaped_indent` packages uses a lot of memory to compile,
// so forcibly make dependencies and avoid compiling in concurrent.
// dependency order: vm => vm_escaped => vm_indent => vm_escaped_indent

View File

@ -1,242 +0,0 @@
package vm_escaped_indent
import (
"encoding/json"
"fmt"
"unsafe"
"github.com/goccy/go-json/internal/encoder"
"github.com/goccy/go-json/internal/runtime"
)
const uintptrSize = 4 << (^uintptr(0) >> 63)
var (
appendInt = encoder.AppendInt
appendUint = encoder.AppendUint
appendFloat32 = encoder.AppendFloat32
appendFloat64 = encoder.AppendFloat64
appendString = encoder.AppendString
appendByteSlice = encoder.AppendByteSlice
appendNumber = encoder.AppendNumber
appendStructEnd = encoder.AppendStructEndIndent
appendIndent = encoder.AppendIndent
errUnsupportedValue = encoder.ErrUnsupportedValue
errUnsupportedFloat = encoder.ErrUnsupportedFloat
mapiterinit = encoder.MapIterInit
mapiterkey = encoder.MapIterKey
mapitervalue = encoder.MapIterValue
mapiternext = encoder.MapIterNext
maplen = encoder.MapLen
)
type emptyInterface struct {
typ *runtime.Type
ptr unsafe.Pointer
}
func errUnimplementedOp(op encoder.OpType) error {
return fmt.Errorf("encoder (indent+escaped): opcode %s has not been implemented", op)
}
func load(base uintptr, idx 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 appendBool(b []byte, v bool) []byte {
if v {
return append(b, "true"...)
}
return append(b, "false"...)
}
func appendNull(b []byte) []byte {
return append(b, "null"...)
}
func appendComma(b []byte) []byte {
return append(b, ',', '\n')
}
func appendColon(b []byte) []byte {
return append(b, ':', ' ')
}
func appendInterface(ctx *encoder.RuntimeContext, codeSet *encoder.OpcodeSet, 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
ctx.Code = ifaceCodeSet.EscapeKeyCode
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(b []byte) []byte {
return append(b, '[', ']', ',', '\n')
}
func appendEmptyObject(b []byte) []byte {
return append(b, '{', '}', ',', '\n')
}
func appendObjectEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte {
last := len(b) - 1
b[last] = '\n'
b = appendIndent(ctx, b, code.Indent-1)
return append(b, '}', ',', '\n')
}
func appendMarshalJSON(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) {
return encoder.AppendMarshalJSONIndent(ctx, code, b, v, true)
}
func appendMarshalText(code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) {
return encoder.AppendMarshalTextIndent(code, b, v, true)
}
func appendStructHead(b []byte) []byte {
return append(b, '{', '\n')
}
func appendStructKey(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte {
b = appendIndent(ctx, b, code.Indent)
b = append(b, code.Key...)
return append(b, ' ')
}
func appendStructEndSkipLast(ctx *encoder.RuntimeContext, 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(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

@ -7,6 +7,13 @@ 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
if ctx.Option.HTMLEscape {
code = codeSet.EscapeKeyCode
} else {
code = codeSet.NoescapeKeyCode
}
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
fmt.Println("=============[DEBUG]===============") fmt.Println("=============[DEBUG]===============")
@ -14,7 +21,7 @@ func DebugRun(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet)
fmt.Println(codeSet.Type) fmt.Println(codeSet.Type)
fmt.Printf("\n") fmt.Printf("\n")
fmt.Println("* [ALL OPCODE]") fmt.Println("* [ALL OPCODE]")
fmt.Println(ctx.Code.Dump()) fmt.Println(code.Dump())
fmt.Printf("\n") fmt.Printf("\n")
fmt.Println("* [CONTEXT]") fmt.Println("* [CONTEXT]")
fmt.Printf("%+v\n", ctx) fmt.Printf("%+v\n", ctx)

View File

@ -1,9 +1,6 @@
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_escaped`, `vm_indent`, `vm_escaped_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_escaped => vm_indent => vm_escaped_indent
_ "github.com/goccy/go-json/internal/encoder/vm_escaped_indent"
)

View File

@ -137,7 +137,6 @@ func appendInterface(ctx *encoder.RuntimeContext, codeSet *encoder.OpcodeSet, co
oldBaseIndent := ctx.BaseIndent oldBaseIndent := ctx.BaseIndent
ctx.BaseIndent = code.Indent ctx.BaseIndent = code.Indent
ctx.Code = ifaceCodeSet.NoescapeKeyCode
bb, err := Run(ctx, b, ifaceCodeSet) bb, err := Run(ctx, b, ifaceCodeSet)
if err != nil { if err != nil {
return nil, err return nil, err
@ -192,11 +191,11 @@ func appendObjectEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte
} }
func appendMarshalJSON(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) { func appendMarshalJSON(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) {
return encoder.AppendMarshalJSONIndent(ctx, code, b, v, false) return encoder.AppendMarshalJSONIndent(ctx, code, b, v)
} }
func appendMarshalText(code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) { func appendMarshalText(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) {
return encoder.AppendMarshalTextIndent(code, b, v, false) return encoder.AppendMarshalTextIndent(ctx, code, b, v)
} }
func appendStructHead(b []byte) []byte { func appendStructHead(b []byte) []byte {

View File

@ -13,7 +13,12 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
recursiveLevel := 0 recursiveLevel := 0
ptrOffset := uintptr(0) ptrOffset := uintptr(0)
ctxptr := ctx.Ptr() ctxptr := ctx.Ptr()
code := ctx.Code var code *encoder.Opcode
if ctx.Option.HTMLEscape {
code = codeSet.EscapeKeyCode
} else {
code = codeSet.NoescapeKeyCode
}
for { for {
switch code.Op { switch code.Op {
@ -106,7 +111,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.OpString: case encoder.OpString:
b = appendString(b, ptrToString(load(ctxptr, code.Idx))) b = appendString(ctx, b, ptrToString(load(ctxptr, code.Idx)))
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
case encoder.OpBoolPtr: case encoder.OpBoolPtr:
@ -242,7 +247,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if (code.Flags&encoder.IsNilableTypeFlags) != 0 && (code.Flags&encoder.IndirectFlags) != 0 { if (code.Flags&encoder.IsNilableTypeFlags) != 0 && (code.Flags&encoder.IndirectFlags) != 0 {
p = ptrToPtr(p) p = ptrToPtr(p)
} }
bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) bb, err := appendMarshalText(ctx, code, b, ptrToInterface(code, p))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -1759,7 +1764,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendStructHead(b) b = appendStructHead(b)
} }
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendString(b, ptrToString(p+uintptr(code.Offset))) b = appendString(ctx, b, ptrToString(p+uintptr(code.Offset)))
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
case encoder.OpStructPtrHeadOmitEmptyString: case encoder.OpStructPtrHeadOmitEmptyString:
@ -1794,7 +1799,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 = appendString(b, v) b = appendString(ctx, b, v)
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
} }
@ -1826,7 +1831,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
b = appendStructHead(b) b = appendStructHead(b)
} }
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendString(b, string(appendString([]byte{}, ptrToString(p+uintptr(code.Offset))))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, ptrToString(p+uintptr(code.Offset)))))
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
case encoder.OpStructPtrHeadOmitEmptyStringString: case encoder.OpStructPtrHeadOmitEmptyStringString:
@ -1861,7 +1866,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 = appendString(b, string(appendString([]byte{}, v))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, v)))
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
} }
@ -1897,7 +1902,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
b = appendString(b, ptrToString(p)) b = appendString(ctx, b, ptrToString(p))
} }
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
@ -1931,7 +1936,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 = appendString(b, ptrToString(p)) b = appendString(ctx, b, ptrToString(p))
b = appendComma(b) b = appendComma(b)
} }
code = code.Next code = code.Next
@ -1967,7 +1972,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
b = appendString(b, string(appendString([]byte{}, ptrToString(p)))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, ptrToString(p))))
} }
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
@ -2001,7 +2006,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 = appendString(b, string(appendString([]byte{}, ptrToString(p)))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, ptrToString(p))))
b = appendComma(b) b = appendComma(b)
} }
code = code.Next code = code.Next
@ -3256,7 +3261,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 && (code.Flags&encoder.NilCheckFlags) != 0 { if p == 0 && (code.Flags&encoder.NilCheckFlags) != 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) bb, err := appendMarshalText(ctx, code, b, ptrToInterface(code, p))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3300,7 +3305,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)
bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) bb, err := appendMarshalText(ctx, code, b, ptrToInterface(code, p))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3340,7 +3345,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) bb, err := appendMarshalText(ctx, code, b, ptrToInterface(code, p))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3380,7 +3385,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)
bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) bb, err := appendMarshalText(ctx, code, b, ptrToInterface(code, p))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3756,7 +3761,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
case encoder.OpStructFieldString: case encoder.OpStructFieldString:
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendString(b, ptrToString(p+uintptr(code.Offset))) b = appendString(ctx, b, ptrToString(p+uintptr(code.Offset)))
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
case encoder.OpStructFieldOmitEmptyString: case encoder.OpStructFieldOmitEmptyString:
@ -3764,7 +3769,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
v := ptrToString(p + uintptr(code.Offset)) v := ptrToString(p + uintptr(code.Offset))
if v != "" { if v != "" {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendString(b, v) b = appendString(ctx, b, v)
b = appendComma(b) b = appendComma(b)
} }
code = code.Next code = code.Next
@ -3772,7 +3777,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
s := ptrToString(p + uintptr(code.Offset)) s := ptrToString(p + uintptr(code.Offset))
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendString(b, string(appendString([]byte{}, s))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, s)))
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
case encoder.OpStructFieldOmitEmptyStringString: case encoder.OpStructFieldOmitEmptyStringString:
@ -3780,7 +3785,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
v := ptrToString(p + uintptr(code.Offset)) v := ptrToString(p + uintptr(code.Offset))
if v != "" { if v != "" {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendString(b, string(appendString([]byte{}, v))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, v)))
b = appendComma(b) b = appendComma(b)
} }
code = code.Next code = code.Next
@ -3791,7 +3796,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
b = appendString(b, ptrToString(p)) b = appendString(ctx, b, ptrToString(p))
} }
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
@ -3800,7 +3805,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 = appendString(b, ptrToString(p)) b = appendString(ctx, b, ptrToString(p))
b = appendComma(b) b = appendComma(b)
} }
code = code.Next code = code.Next
@ -3811,7 +3816,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
b = appendString(b, string(appendString([]byte{}, ptrToString(p)))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, ptrToString(p))))
} }
b = appendComma(b) b = appendComma(b)
code = code.Next code = code.Next
@ -3820,7 +3825,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 = appendString(b, string(appendString([]byte{}, ptrToString(p)))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, ptrToString(p))))
b = appendComma(b) b = appendComma(b)
} }
code = code.Next code = code.Next
@ -4117,7 +4122,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 && (code.Flags&encoder.NilCheckFlags) != 0 { if p == 0 && (code.Flags&encoder.NilCheckFlags) != 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) bb, err := appendMarshalText(ctx, code, b, ptrToInterface(code, p))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -4136,7 +4141,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
break break
} }
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) bb, err := appendMarshalText(ctx, code, b, ptrToInterface(code, p))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -4149,7 +4154,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) bb, err := appendMarshalText(ctx, code, b, ptrToInterface(code, p))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -4162,7 +4167,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)
bb, err := appendMarshalText(code, b, ptrToInterface(code, p)) bb, err := appendMarshalText(ctx, code, b, ptrToInterface(code, p))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -4672,7 +4677,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
case encoder.OpStructEndString: case encoder.OpStructEndString:
p := load(ctxptr, code.Idx) p := load(ctxptr, code.Idx)
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendString(b, ptrToString(p+uintptr(code.Offset))) b = appendString(ctx, b, ptrToString(p+uintptr(code.Offset)))
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
case encoder.OpStructEndOmitEmptyString: case encoder.OpStructEndOmitEmptyString:
@ -4680,7 +4685,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
v := ptrToString(p + uintptr(code.Offset)) v := ptrToString(p + uintptr(code.Offset))
if v != "" { if v != "" {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendString(b, v) b = appendString(ctx, b, v)
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
b = appendStructEndSkipLast(ctx, code, b) b = appendStructEndSkipLast(ctx, code, b)
@ -4690,7 +4695,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)
s := ptrToString(p + uintptr(code.Offset)) s := ptrToString(p + uintptr(code.Offset))
b = appendString(b, string(appendString([]byte{}, s))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, s)))
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
case encoder.OpStructEndOmitEmptyStringString: case encoder.OpStructEndOmitEmptyStringString:
@ -4698,7 +4703,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
v := ptrToString(p + uintptr(code.Offset)) v := ptrToString(p + uintptr(code.Offset))
if v != "" { if v != "" {
b = appendStructKey(ctx, code, b) b = appendStructKey(ctx, code, b)
b = appendString(b, string(appendString([]byte{}, v))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, v)))
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
b = appendStructEndSkipLast(ctx, code, b) b = appendStructEndSkipLast(ctx, code, b)
@ -4711,7 +4716,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
b = appendString(b, ptrToString(p)) b = appendString(ctx, b, ptrToString(p))
} }
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
@ -4720,7 +4725,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 = appendString(b, ptrToString(p)) b = appendString(ctx, b, ptrToString(p))
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
b = appendStructEndSkipLast(ctx, code, b) b = appendStructEndSkipLast(ctx, code, b)
@ -4733,7 +4738,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
b = appendString(b, string(appendString([]byte{}, ptrToString(p)))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, ptrToString(p))))
} }
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
code = code.Next code = code.Next
@ -4742,7 +4747,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 = appendString(b, string(appendString([]byte{}, ptrToString(p)))) b = appendString(ctx, b, string(appendString(ctx, []byte{}, ptrToString(p))))
b = appendStructEnd(ctx, code, b) b = appendStructEnd(ctx, code, b)
} else { } else {
b = appendStructEndSkipLast(ctx, code, b) b = appendStructEndSkipLast(ctx, code, b)