mirror of https://github.com/goccy/go-json.git
Merge pull request #232 from goccy/feature/refactor-escape-string
Refactor Escape string
This commit is contained in:
commit
374d8261fa
62
encode.go
62
encode.go
|
@ -6,8 +6,6 @@ import (
|
|||
|
||||
"github.com/goccy/go-json/internal/encoder"
|
||||
"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"
|
||||
)
|
||||
|
||||
|
@ -172,8 +170,8 @@ func marshalIndent(v interface{}, prefix, indent string, optFuncs ...EncodeOptio
|
|||
func encode(ctx *encoder.RuntimeContext, v interface{}) ([]byte, error) {
|
||||
b := ctx.Buf[:0]
|
||||
if v == nil {
|
||||
b = encoder.AppendNull(b)
|
||||
b = encoder.AppendComma(b)
|
||||
b = encoder.AppendNull(ctx, b)
|
||||
b = encoder.AppendComma(ctx, b)
|
||||
return b, nil
|
||||
}
|
||||
header := (*emptyInterface)(unsafe.Pointer(&v))
|
||||
|
@ -186,13 +184,7 @@ func encode(ctx *encoder.RuntimeContext, v interface{}) ([]byte, error) {
|
|||
}
|
||||
|
||||
p := uintptr(header.ptr)
|
||||
var code *encoder.Opcode
|
||||
if ctx.Option.HTMLEscape {
|
||||
code = codeSet.EscapeKeyCode
|
||||
} else {
|
||||
code = codeSet.NoescapeKeyCode
|
||||
}
|
||||
ctx.Init(code, p, codeSet.CodeLength)
|
||||
ctx.Init(p, codeSet.CodeLength)
|
||||
ctx.KeepRefs = append(ctx.KeepRefs, header.ptr)
|
||||
|
||||
buf, err := encodeRunCode(ctx, b, codeSet)
|
||||
|
@ -206,8 +198,8 @@ func encode(ctx *encoder.RuntimeContext, v interface{}) ([]byte, error) {
|
|||
func encodeNoEscape(ctx *encoder.RuntimeContext, v interface{}) ([]byte, error) {
|
||||
b := ctx.Buf[:0]
|
||||
if v == nil {
|
||||
b = encoder.AppendNull(b)
|
||||
b = encoder.AppendComma(b)
|
||||
b = encoder.AppendNull(ctx, b)
|
||||
b = encoder.AppendComma(ctx, b)
|
||||
return b, nil
|
||||
}
|
||||
header := (*emptyInterface)(unsafe.Pointer(&v))
|
||||
|
@ -219,14 +211,8 @@ func encodeNoEscape(ctx *encoder.RuntimeContext, v interface{}) ([]byte, error)
|
|||
return nil, err
|
||||
}
|
||||
|
||||
var code *encoder.Opcode
|
||||
if ctx.Option.HTMLEscape {
|
||||
code = codeSet.EscapeKeyCode
|
||||
} else {
|
||||
code = codeSet.NoescapeKeyCode
|
||||
}
|
||||
p := uintptr(header.ptr)
|
||||
ctx.Init(code, p, codeSet.CodeLength)
|
||||
ctx.Init(p, codeSet.CodeLength)
|
||||
buf, err := encodeRunCode(ctx, b, codeSet)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -239,8 +225,8 @@ func encodeNoEscape(ctx *encoder.RuntimeContext, v interface{}) ([]byte, error)
|
|||
func encodeIndent(ctx *encoder.RuntimeContext, v interface{}, prefix, indent string) ([]byte, error) {
|
||||
b := ctx.Buf[:0]
|
||||
if v == nil {
|
||||
b = encoder.AppendNull(b)
|
||||
b = encoder.AppendCommaIndent(b)
|
||||
b = encoder.AppendNull(ctx, b)
|
||||
b = encoder.AppendCommaIndent(ctx, b)
|
||||
return b, nil
|
||||
}
|
||||
header := (*emptyInterface)(unsafe.Pointer(&v))
|
||||
|
@ -252,14 +238,8 @@ func encodeIndent(ctx *encoder.RuntimeContext, v interface{}, prefix, indent str
|
|||
return nil, err
|
||||
}
|
||||
|
||||
var code *encoder.Opcode
|
||||
if ctx.Option.HTMLEscape {
|
||||
code = codeSet.EscapeKeyCode
|
||||
} else {
|
||||
code = codeSet.NoescapeKeyCode
|
||||
}
|
||||
p := uintptr(header.ptr)
|
||||
ctx.Init(code, p, codeSet.CodeLength)
|
||||
ctx.Init(p, codeSet.CodeLength)
|
||||
buf, err := encodeRunIndentCode(ctx, b, codeSet, prefix, indent)
|
||||
|
||||
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) {
|
||||
if ctx.Option.Debug {
|
||||
return encodeDebugRunCode(ctx, b, codeSet)
|
||||
}
|
||||
if ctx.Option.HTMLEscape {
|
||||
return vm_escaped.Run(ctx, b, codeSet)
|
||||
return vm.DebugRun(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) {
|
||||
ctx.Prefix = []byte(prefix)
|
||||
ctx.IndentStr = []byte(indent)
|
||||
if ctx.Option.Debug {
|
||||
return encodeDebugRunIndentCode(ctx, b, codeSet)
|
||||
}
|
||||
if ctx.Option.HTMLEscape {
|
||||
return vm_escaped_indent.Run(ctx, b, codeSet)
|
||||
return vm_indent.DebugRun(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) {
|
||||
opt.HTMLEscape = false
|
||||
opt.Indent = false
|
||||
|
|
|
@ -286,7 +286,7 @@ func generateVM() error {
|
|||
if err != nil {
|
||||
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
|
||||
var buf bytes.Buffer
|
||||
printer.Fprint(&buf, fset, f)
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1456,7 +1456,8 @@ func compileStruct(ctx *compileContext, isPtr bool) (*Opcode, error) {
|
|||
}
|
||||
var key string
|
||||
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 {
|
||||
key = fmt.Sprintf(`"%s":`, tag.Key)
|
||||
}
|
||||
|
|
|
@ -112,11 +112,10 @@ type RuntimeContext struct {
|
|||
BaseIndent uint32
|
||||
Prefix []byte
|
||||
IndentStr []byte
|
||||
Code *Opcode
|
||||
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 {
|
||||
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.SeenPtr = c.SeenPtr[:0]
|
||||
c.BaseIndent = 0
|
||||
c.Code = code
|
||||
}
|
||||
|
||||
func (c *RuntimeContext) Ptr() uintptr {
|
||||
|
|
|
@ -269,7 +269,7 @@ func MapIterNext(it unsafe.Pointer)
|
|||
//go:noescape
|
||||
func MapLen(m unsafe.Pointer) int
|
||||
|
||||
func AppendByteSlice(b []byte, src []byte) []byte {
|
||||
func AppendByteSlice(_ *RuntimeContext, b []byte, src []byte) []byte {
|
||||
if src == nil {
|
||||
return append(b, `null`...)
|
||||
}
|
||||
|
@ -287,7 +287,7 @@ func AppendByteSlice(b []byte, src []byte) []byte {
|
|||
return append(append(b, buf...), '"')
|
||||
}
|
||||
|
||||
func AppendFloat32(b []byte, v float32) []byte {
|
||||
func AppendFloat32(_ *RuntimeContext, b []byte, v float32) []byte {
|
||||
f64 := float64(v)
|
||||
abs := math.Abs(f64)
|
||||
fmt := byte('f')
|
||||
|
@ -301,7 +301,7 @@ func AppendFloat32(b []byte, v float32) []byte {
|
|||
return strconv.AppendFloat(b, f64, fmt, -1, 32)
|
||||
}
|
||||
|
||||
func AppendFloat64(b []byte, v float64) []byte {
|
||||
func AppendFloat64(_ *RuntimeContext, b []byte, v float64) []byte {
|
||||
abs := math.Abs(v)
|
||||
fmt := byte('f')
|
||||
// Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right.
|
||||
|
@ -313,7 +313,7 @@ func AppendFloat64(b []byte, v float64) []byte {
|
|||
return strconv.AppendFloat(b, v, fmt, -1, 64)
|
||||
}
|
||||
|
||||
func AppendBool(b []byte, v bool) []byte {
|
||||
func AppendBool(_ *RuntimeContext, b []byte, v bool) []byte {
|
||||
if v {
|
||||
return append(b, "true"...)
|
||||
}
|
||||
|
@ -340,7 +340,7 @@ var (
|
|||
}
|
||||
)
|
||||
|
||||
func AppendNumber(b []byte, n json.Number) ([]byte, error) {
|
||||
func AppendNumber(_ *RuntimeContext, b []byte, n json.Number) ([]byte, error) {
|
||||
if len(n) == 0 {
|
||||
return append(b, '0'), nil
|
||||
}
|
||||
|
@ -353,7 +353,7 @@ func AppendNumber(b []byte, n json.Number) ([]byte, error) {
|
|||
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
|
||||
if (code.Flags & AddrForMarshalerFlags) != 0 {
|
||||
if rv.CanAddr() {
|
||||
|
@ -367,7 +367,7 @@ func AppendMarshalJSON(ctx *RuntimeContext, code *Opcode, b []byte, v interface{
|
|||
v = rv.Interface()
|
||||
marshaler, ok := v.(json.Marshaler)
|
||||
if !ok {
|
||||
return AppendNull(b), nil
|
||||
return AppendNull(ctx, b), nil
|
||||
}
|
||||
bb, err := marshaler.MarshalJSON()
|
||||
if err != nil {
|
||||
|
@ -375,7 +375,7 @@ func AppendMarshalJSON(ctx *RuntimeContext, code *Opcode, b []byte, v interface{
|
|||
}
|
||||
marshalBuf := ctx.MarshalBuf[:0]
|
||||
marshalBuf = append(append(marshalBuf, bb...), nul)
|
||||
compactedBuf, err := compact(b, marshalBuf, escape)
|
||||
compactedBuf, err := compact(b, marshalBuf, ctx.Option.HTMLEscape)
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
if (code.Flags & AddrForMarshalerFlags) != 0 {
|
||||
if rv.CanAddr() {
|
||||
|
@ -397,7 +397,7 @@ func AppendMarshalJSONIndent(ctx *RuntimeContext, code *Opcode, b []byte, v inte
|
|||
v = rv.Interface()
|
||||
marshaler, ok := v.(json.Marshaler)
|
||||
if !ok {
|
||||
return AppendNull(b), nil
|
||||
return AppendNull(ctx, b), nil
|
||||
}
|
||||
bb, err := marshaler.MarshalJSON()
|
||||
if err != nil {
|
||||
|
@ -410,7 +410,7 @@ func AppendMarshalJSONIndent(ctx *RuntimeContext, code *Opcode, b []byte, v inte
|
|||
marshalBuf,
|
||||
string(ctx.Prefix)+strings.Repeat(string(ctx.IndentStr), int(ctx.BaseIndent+code.Indent)),
|
||||
string(ctx.IndentStr),
|
||||
escape,
|
||||
ctx.Option.HTMLEscape,
|
||||
)
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
if (code.Flags & AddrForMarshalerFlags) != 0 {
|
||||
if rv.CanAddr() {
|
||||
|
@ -433,19 +433,16 @@ func AppendMarshalText(code *Opcode, b []byte, v interface{}, escape bool) ([]by
|
|||
v = rv.Interface()
|
||||
marshaler, ok := v.(encoding.TextMarshaler)
|
||||
if !ok {
|
||||
return AppendNull(b), nil
|
||||
return AppendNull(ctx, b), nil
|
||||
}
|
||||
bytes, err := marshaler.MarshalText()
|
||||
if err != nil {
|
||||
return nil, &errors.MarshalerError{Type: reflect.TypeOf(v), Err: err}
|
||||
}
|
||||
if escape {
|
||||
return AppendEscapedString(b, *(*string)(unsafe.Pointer(&bytes))), nil
|
||||
}
|
||||
return AppendString(b, *(*string)(unsafe.Pointer(&bytes))), nil
|
||||
return AppendString(ctx, 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
|
||||
if (code.Flags & AddrForMarshalerFlags) != 0 {
|
||||
if rv.CanAddr() {
|
||||
|
@ -459,31 +456,28 @@ func AppendMarshalTextIndent(code *Opcode, b []byte, v interface{}, escape bool)
|
|||
v = rv.Interface()
|
||||
marshaler, ok := v.(encoding.TextMarshaler)
|
||||
if !ok {
|
||||
return AppendNull(b), nil
|
||||
return AppendNull(ctx, b), nil
|
||||
}
|
||||
bytes, err := marshaler.MarshalText()
|
||||
if err != nil {
|
||||
return nil, &errors.MarshalerError{Type: reflect.TypeOf(v), Err: err}
|
||||
}
|
||||
if escape {
|
||||
return AppendEscapedString(b, *(*string)(unsafe.Pointer(&bytes))), nil
|
||||
}
|
||||
return AppendString(b, *(*string)(unsafe.Pointer(&bytes))), nil
|
||||
return AppendString(ctx, b, *(*string)(unsafe.Pointer(&bytes))), nil
|
||||
}
|
||||
|
||||
func AppendNull(b []byte) []byte {
|
||||
func AppendNull(_ *RuntimeContext, b []byte) []byte {
|
||||
return append(b, "null"...)
|
||||
}
|
||||
|
||||
func AppendComma(b []byte) []byte {
|
||||
func AppendComma(_ *RuntimeContext, b []byte) []byte {
|
||||
return append(b, ',')
|
||||
}
|
||||
|
||||
func AppendCommaIndent(b []byte) []byte {
|
||||
func AppendCommaIndent(_ *RuntimeContext, b []byte) []byte {
|
||||
return append(b, ',', '\n')
|
||||
}
|
||||
|
||||
func AppendStructEnd(b []byte) []byte {
|
||||
func AppendStructEnd(_ *RuntimeContext, b []byte) []byte {
|
||||
return append(b, '}', ',')
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
if valLen == 0 {
|
||||
return append(buf, `""`...)
|
||||
|
@ -531,7 +534,7 @@ ESCAPE_END:
|
|||
return append(append(buf, s[i:]...), '"')
|
||||
}
|
||||
|
||||
func AppendString(buf []byte, s string) []byte {
|
||||
func appendString(buf []byte, s string) []byte {
|
||||
valLen := len(s)
|
||||
if valLen == 0 {
|
||||
return append(buf, `""`...)
|
||||
|
|
|
@ -8,13 +8,20 @@ import (
|
|||
|
||||
func DebugRun(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]byte, error) {
|
||||
defer func() {
|
||||
var code *encoder.Opcode
|
||||
if ctx.Option.HTMLEscape {
|
||||
code = codeSet.EscapeKeyCode
|
||||
} else {
|
||||
code = codeSet.NoescapeKeyCode
|
||||
}
|
||||
|
||||
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.Println(code.Dump())
|
||||
fmt.Printf("\n")
|
||||
fmt.Println("* [CONTEXT]")
|
||||
fmt.Printf("%+v\n", ctx)
|
||||
|
|
|
@ -2,8 +2,8 @@ package vm
|
|||
|
||||
import (
|
||||
// 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.
|
||||
// dependency order: vm => vm_escaped => vm_indent => vm_escaped_indent
|
||||
_ "github.com/goccy/go-json/internal/encoder/vm_escaped"
|
||||
// dependency order: vm => vm_indent
|
||||
_ "github.com/goccy/go-json/internal/encoder/vm_indent"
|
||||
)
|
||||
|
|
|
@ -90,22 +90,22 @@ func ptrToInterface(code *encoder.Opcode, p uintptr) interface{} {
|
|||
}))
|
||||
}
|
||||
|
||||
func appendBool(b []byte, v bool) []byte {
|
||||
func appendBool(_ *encoder.RuntimeContext, b []byte, v bool) []byte {
|
||||
if v {
|
||||
return append(b, "true"...)
|
||||
}
|
||||
return append(b, "false"...)
|
||||
}
|
||||
|
||||
func appendNull(b []byte) []byte {
|
||||
func appendNull(_ *encoder.RuntimeContext, b []byte) []byte {
|
||||
return append(b, "null"...)
|
||||
}
|
||||
|
||||
func appendComma(b []byte) []byte {
|
||||
func appendComma(_ *encoder.RuntimeContext, b []byte) []byte {
|
||||
return append(b, ',')
|
||||
}
|
||||
|
||||
func appendColon(b []byte) []byte {
|
||||
func appendColon(_ *encoder.RuntimeContext, b []byte) []byte {
|
||||
last := len(b) - 1
|
||||
b[last] = ':'
|
||||
return b
|
||||
|
@ -146,7 +146,6 @@ func appendInterface(ctx *encoder.RuntimeContext, codeSet *encoder.OpcodeSet, _
|
|||
newPtrs[0] = uintptr(iface.ptr)
|
||||
|
||||
ctx.Ptrs = newPtrs
|
||||
ctx.Code = ifaceCodeSet.NoescapeKeyCode
|
||||
|
||||
bb, err := Run(ctx, b, ifaceCodeSet)
|
||||
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) {
|
||||
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) {
|
||||
return encoder.AppendMarshalText(code, b, v, false)
|
||||
func appendMarshalText(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) {
|
||||
return encoder.AppendMarshalText(ctx, code, b, v)
|
||||
}
|
||||
|
||||
func appendArrayHead(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte {
|
||||
|
@ -175,11 +174,11 @@ func appendArrayEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []by
|
|||
return append(b, ',')
|
||||
}
|
||||
|
||||
func appendEmptyArray(b []byte) []byte {
|
||||
func appendEmptyArray(_ *encoder.RuntimeContext, b []byte) []byte {
|
||||
return append(b, '[', ']', ',')
|
||||
}
|
||||
|
||||
func appendEmptyObject(b []byte) []byte {
|
||||
func appendEmptyObject(_ *encoder.RuntimeContext, b []byte) []byte {
|
||||
return append(b, '{', '}', ',')
|
||||
}
|
||||
|
||||
|
@ -189,7 +188,7 @@ func appendObjectEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []b
|
|||
return append(b, ',')
|
||||
}
|
||||
|
||||
func appendStructHead(b []byte) []byte {
|
||||
func appendStructHead(_ *encoder.RuntimeContext, b []byte) []byte {
|
||||
return append(b, '{')
|
||||
}
|
||||
|
||||
|
@ -205,7 +204,7 @@ func appendStructEndSkipLast(ctx *encoder.RuntimeContext, code *encoder.Opcode,
|
|||
last := len(b) - 1
|
||||
if b[last] == ',' {
|
||||
b[last] = '}'
|
||||
return appendComma(b)
|
||||
return appendComma(ctx, b)
|
||||
}
|
||||
return appendStructEnd(ctx, code, b)
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -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)
|
||||
}
|
|
@ -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"
|
||||
)
|
|
@ -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
|
@ -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)
|
||||
}
|
|
@ -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
|
|
@ -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
|
@ -7,6 +7,13 @@ import (
|
|||
)
|
||||
|
||||
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() {
|
||||
if err := recover(); err != nil {
|
||||
fmt.Println("=============[DEBUG]===============")
|
||||
|
@ -14,7 +21,7 @@ func DebugRun(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet)
|
|||
fmt.Println(codeSet.Type)
|
||||
fmt.Printf("\n")
|
||||
fmt.Println("* [ALL OPCODE]")
|
||||
fmt.Println(ctx.Code.Dump())
|
||||
fmt.Println(code.Dump())
|
||||
fmt.Printf("\n")
|
||||
fmt.Println("* [CONTEXT]")
|
||||
fmt.Printf("%+v\n", ctx)
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
package vm_indent
|
||||
|
||||
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_escaped_indent"
|
||||
)
|
||||
// HACK: compile order
|
||||
// `vm`, `vm_indent` packages uses a lot of memory to compile,
|
||||
// so forcibly make dependencies and avoid compiling in concurrent.
|
||||
// dependency order: vm => vm_indent
|
||||
|
|
|
@ -92,22 +92,22 @@ func ptrToInterface(code *encoder.Opcode, p uintptr) interface{} {
|
|||
}))
|
||||
}
|
||||
|
||||
func appendBool(b []byte, v bool) []byte {
|
||||
func appendBool(_ *encoder.RuntimeContext, b []byte, v bool) []byte {
|
||||
if v {
|
||||
return append(b, "true"...)
|
||||
}
|
||||
return append(b, "false"...)
|
||||
}
|
||||
|
||||
func appendNull(b []byte) []byte {
|
||||
func appendNull(_ *encoder.RuntimeContext, b []byte) []byte {
|
||||
return append(b, "null"...)
|
||||
}
|
||||
|
||||
func appendComma(b []byte) []byte {
|
||||
func appendComma(_ *encoder.RuntimeContext, b []byte) []byte {
|
||||
return append(b, ',', '\n')
|
||||
}
|
||||
|
||||
func appendColon(b []byte) []byte {
|
||||
func appendColon(_ *encoder.RuntimeContext, b []byte) []byte {
|
||||
return append(b, ':', ' ')
|
||||
}
|
||||
|
||||
|
@ -137,7 +137,6 @@ func appendInterface(ctx *encoder.RuntimeContext, codeSet *encoder.OpcodeSet, co
|
|||
|
||||
oldBaseIndent := ctx.BaseIndent
|
||||
ctx.BaseIndent = code.Indent
|
||||
ctx.Code = ifaceCodeSet.NoescapeKeyCode
|
||||
bb, err := Run(ctx, b, ifaceCodeSet)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -176,11 +175,11 @@ func appendArrayEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte)
|
|||
return append(b, ']', ',', '\n')
|
||||
}
|
||||
|
||||
func appendEmptyArray(b []byte) []byte {
|
||||
func appendEmptyArray(_ *encoder.RuntimeContext, b []byte) []byte {
|
||||
return append(b, '[', ']', ',', '\n')
|
||||
}
|
||||
|
||||
func appendEmptyObject(b []byte) []byte {
|
||||
func appendEmptyObject(_ *encoder.RuntimeContext, b []byte) []byte {
|
||||
return append(b, '{', '}', ',', '\n')
|
||||
}
|
||||
|
||||
|
@ -192,14 +191,14 @@ func appendObjectEnd(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte
|
|||
}
|
||||
|
||||
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) {
|
||||
return encoder.AppendMarshalTextIndent(code, b, v, false)
|
||||
func appendMarshalText(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) {
|
||||
return encoder.AppendMarshalTextIndent(ctx, code, b, v)
|
||||
}
|
||||
|
||||
func appendStructHead(b []byte) []byte {
|
||||
func appendStructHead(_ *encoder.RuntimeContext, b []byte) []byte {
|
||||
return append(b, '{', '\n')
|
||||
}
|
||||
|
||||
|
@ -222,7 +221,7 @@ func appendStructEndSkipLast(ctx *encoder.RuntimeContext, code *encoder.Opcode,
|
|||
b = appendIndent(ctx, b, code.Indent-1)
|
||||
b = append(b, '}')
|
||||
}
|
||||
return appendComma(b)
|
||||
return appendComma(ctx, b)
|
||||
}
|
||||
|
||||
func restoreIndent(ctx *encoder.RuntimeContext, code *encoder.Opcode, ctxptr uintptr) {
|
||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue