go-json/encode_context.go

105 lines
2.1 KiB
Go
Raw Normal View History

2020-08-29 09:35:03 +03:00
package json
2020-08-30 11:52:59 +03:00
import (
"unsafe"
)
2020-08-29 09:35:03 +03:00
type encodeCompileContext struct {
2020-12-24 21:53:48 +03:00
typ *rtype
withIndent bool
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,
withIndent: c.withIndent,
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()
}
}
type encodeRuntimeContext struct {
2020-09-04 14:28:27 +03:00
ptrs []uintptr
keepRefs []unsafe.Pointer
2020-08-30 18:14:32 +03:00
}
func (c *encodeRuntimeContext) init(p uintptr, codelen int) {
if len(c.ptrs) < codelen {
c.ptrs = make([]uintptr, codelen)
}
c.ptrs[0] = p
2020-09-04 14:28:27 +03:00
c.keepRefs = c.keepRefs[:0]
2020-08-30 11:52:59 +03:00
}
func (c *encodeRuntimeContext) ptr() uintptr {
header := (*sliceHeader)(unsafe.Pointer(&c.ptrs))
return uintptr(header.data)
2020-08-30 11:52:59 +03:00
}