2020-08-29 09:35:03 +03:00
|
|
|
package json
|
|
|
|
|
2020-08-30 11:52:59 +03:00
|
|
|
import (
|
2021-01-23 21:02:26 +03:00
|
|
|
"bytes"
|
|
|
|
"sync"
|
2020-08-30 11:52:59 +03:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2021-01-23 21:02:26 +03:00
|
|
|
type mapItem struct {
|
|
|
|
key []byte
|
|
|
|
value []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type mapslice struct {
|
|
|
|
items []mapItem
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mapslice) Len() int {
|
|
|
|
return len(m.items)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mapslice) Less(i, j int) bool {
|
|
|
|
return bytes.Compare(m.items[i].key, m.items[j].key) < 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mapslice) Swap(i, j int) {
|
|
|
|
m.items[i], m.items[j] = m.items[j], m.items[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
type encodeMapContext struct {
|
|
|
|
iter unsafe.Pointer
|
|
|
|
pos []int
|
|
|
|
slice *mapslice
|
|
|
|
buf []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
var mapContextPool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return &encodeMapContext{}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMapContext(mapLen int) *encodeMapContext {
|
|
|
|
ctx := mapContextPool.Get().(*encodeMapContext)
|
|
|
|
if ctx.slice == nil {
|
|
|
|
ctx.slice = &mapslice{
|
|
|
|
items: make([]mapItem, 0, mapLen),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cap(ctx.pos) < mapLen*2 {
|
|
|
|
ctx.pos = make([]int, 0, mapLen*2)
|
|
|
|
ctx.slice.items = make([]mapItem, 0, mapLen)
|
|
|
|
} else {
|
|
|
|
ctx.pos = ctx.pos[:0]
|
|
|
|
ctx.slice.items = ctx.slice.items[:0]
|
|
|
|
}
|
|
|
|
ctx.buf = ctx.buf[:0]
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func releaseMapContext(c *encodeMapContext) {
|
|
|
|
mapContextPool.Put(c)
|
|
|
|
}
|
|
|
|
|
2020-08-29 09:35:03 +03:00
|
|
|
type encodeCompileContext struct {
|
2020-12-24 21:53:48 +03:00
|
|
|
typ *rtype
|
|
|
|
root bool
|
|
|
|
opcodeIndex int
|
|
|
|
ptrIndex int
|
|
|
|
indent int
|
|
|
|
structTypeToCompiledCode map[uintptr]*compiledCode
|
2020-08-30 11:32:26 +03:00
|
|
|
|
|
|
|
parent *encodeCompileContext
|
2020-08-29 09:35:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *encodeCompileContext) context() *encodeCompileContext {
|
|
|
|
return &encodeCompileContext{
|
2020-12-24 21:53:48 +03:00
|
|
|
typ: c.typ,
|
|
|
|
root: c.root,
|
|
|
|
opcodeIndex: c.opcodeIndex,
|
|
|
|
ptrIndex: c.ptrIndex,
|
|
|
|
indent: c.indent,
|
|
|
|
structTypeToCompiledCode: c.structTypeToCompiledCode,
|
|
|
|
parent: c,
|
2020-08-29 09:35:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *encodeCompileContext) withType(typ *rtype) *encodeCompileContext {
|
|
|
|
ctx := c.context()
|
|
|
|
ctx.typ = typ
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *encodeCompileContext) incIndent() *encodeCompileContext {
|
|
|
|
ctx := c.context()
|
|
|
|
ctx.indent++
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *encodeCompileContext) decIndent() *encodeCompileContext {
|
|
|
|
ctx := c.context()
|
|
|
|
ctx.indent--
|
|
|
|
return ctx
|
|
|
|
}
|
2020-08-30 11:32:26 +03:00
|
|
|
|
2020-09-01 16:26:26 +03:00
|
|
|
func (c *encodeCompileContext) incIndex() {
|
|
|
|
c.incOpcodeIndex()
|
|
|
|
c.incPtrIndex()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *encodeCompileContext) decIndex() {
|
|
|
|
c.decOpcodeIndex()
|
|
|
|
c.decPtrIndex()
|
|
|
|
}
|
|
|
|
|
2020-08-30 11:32:26 +03:00
|
|
|
func (c *encodeCompileContext) incOpcodeIndex() {
|
|
|
|
c.opcodeIndex++
|
|
|
|
if c.parent != nil {
|
|
|
|
c.parent.incOpcodeIndex()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *encodeCompileContext) decOpcodeIndex() {
|
|
|
|
c.opcodeIndex--
|
|
|
|
if c.parent != nil {
|
|
|
|
c.parent.decOpcodeIndex()
|
|
|
|
}
|
|
|
|
}
|
2020-08-30 11:52:59 +03:00
|
|
|
|
2020-09-01 16:26:26 +03:00
|
|
|
func (c *encodeCompileContext) incPtrIndex() {
|
|
|
|
c.ptrIndex++
|
|
|
|
if c.parent != nil {
|
|
|
|
c.parent.incPtrIndex()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *encodeCompileContext) decPtrIndex() {
|
|
|
|
c.ptrIndex--
|
|
|
|
if c.parent != nil {
|
|
|
|
c.parent.decPtrIndex()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-30 17:58:58 +03:00
|
|
|
type encodeRuntimeContext struct {
|
2020-09-04 14:28:27 +03:00
|
|
|
ptrs []uintptr
|
|
|
|
keepRefs []unsafe.Pointer
|
2021-01-15 10:25:00 +03:00
|
|
|
seenPtr []uintptr
|
2020-08-30 18:14:32 +03:00
|
|
|
}
|
|
|
|
|
2020-12-20 14:09:41 +03:00
|
|
|
func (c *encodeRuntimeContext) init(p uintptr, codelen int) {
|
|
|
|
if len(c.ptrs) < codelen {
|
|
|
|
c.ptrs = make([]uintptr, codelen)
|
|
|
|
}
|
2020-08-30 17:58:58 +03:00
|
|
|
c.ptrs[0] = p
|
2020-09-04 14:28:27 +03:00
|
|
|
c.keepRefs = c.keepRefs[:0]
|
2021-01-15 10:25:00 +03:00
|
|
|
c.seenPtr = c.seenPtr[:0]
|
2020-08-30 11:52:59 +03:00
|
|
|
}
|
|
|
|
|
2020-08-30 17:58:58 +03:00
|
|
|
func (c *encodeRuntimeContext) ptr() uintptr {
|
2020-11-14 21:24:34 +03:00
|
|
|
header := (*sliceHeader)(unsafe.Pointer(&c.ptrs))
|
|
|
|
return uintptr(header.data)
|
2020-08-30 11:52:59 +03:00
|
|
|
}
|