go-json/encode_context.go

77 lines
1.3 KiB
Go

package json
import (
"reflect"
"unsafe"
)
type encodeCompileContext struct {
typ *rtype
withIndent bool
root bool
opcodeIndex int
ptrIndex int
indent int
parent *encodeCompileContext
}
func (c *encodeCompileContext) context() *encodeCompileContext {
return &encodeCompileContext{
typ: c.typ,
withIndent: c.withIndent,
root: c.root,
opcodeIndex: c.opcodeIndex,
ptrIndex: c.ptrIndex,
indent: c.indent,
parent: c,
}
}
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
}
func (c *encodeCompileContext) incOpcodeIndex() {
c.opcodeIndex++
c.ptrIndex++
if c.parent != nil {
c.parent.incOpcodeIndex()
}
}
func (c *encodeCompileContext) decOpcodeIndex() {
c.opcodeIndex--
c.ptrIndex--
if c.parent != nil {
c.parent.decOpcodeIndex()
}
}
type encodeRuntimeContext struct {
ptrs []uintptr
}
func (c *encodeRuntimeContext) init(p uintptr) {
c.ptrs[0] = p
}
func (c *encodeRuntimeContext) ptr() uintptr {
header := (*reflect.SliceHeader)(unsafe.Pointer(&c.ptrs))
return header.Data
}