2021-03-16 13:44:32 +03:00
|
|
|
// +build !race
|
|
|
|
|
|
|
|
package encoder
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/goccy/go-json/internal/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
func CompileToGetCodeSet(typeptr uintptr) (*OpcodeSet, error) {
|
|
|
|
if typeptr > typeAddr.MaxTypeAddr {
|
|
|
|
return compileToGetCodeSetSlowPath(typeptr)
|
|
|
|
}
|
|
|
|
index := typeptr - typeAddr.BaseTypeAddr
|
|
|
|
if codeSet := cachedOpcodeSets[index]; codeSet != nil {
|
|
|
|
return codeSet, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// noescape trick for header.typ ( reflect.*rtype )
|
|
|
|
copiedType := *(**runtime.Type)(unsafe.Pointer(&typeptr))
|
|
|
|
|
|
|
|
code, err := compileHead(&compileContext{
|
|
|
|
typ: copiedType,
|
|
|
|
structTypeToCompiledCode: map[uintptr]*CompiledCode{},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
code = copyOpcode(code)
|
|
|
|
codeLength := code.TotalLength()
|
|
|
|
codeSet := &OpcodeSet{
|
2021-03-27 06:35:43 +03:00
|
|
|
Type: copiedType,
|
2021-03-16 13:44:32 +03:00
|
|
|
Code: code,
|
|
|
|
CodeLength: codeLength,
|
|
|
|
}
|
|
|
|
cachedOpcodeSets[index] = codeSet
|
|
|
|
return codeSet, nil
|
|
|
|
}
|