forked from mirror/go-json
Fix decoding of interface type
This commit is contained in:
parent
5f1fcddcf5
commit
5227e49c39
2
codec.go
2
codec.go
|
@ -19,6 +19,7 @@ var (
|
|||
cachedDecoderMap unsafe.Pointer // map[uintptr]decoder
|
||||
existsCachedDecoder bool
|
||||
baseTypeAddr uintptr
|
||||
maxTypeAddr uintptr
|
||||
)
|
||||
|
||||
//go:linkname typelinks reflect.typelinks
|
||||
|
@ -72,6 +73,7 @@ func setupCodec() error {
|
|||
cachedDecoder = make([]decoder, addrRange)
|
||||
existsCachedDecoder = true
|
||||
baseTypeAddr = min
|
||||
maxTypeAddr = max
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -245,7 +245,7 @@ func (d *Decoder) compileMap(typ *rtype, structName, fieldName string) (decoder,
|
|||
}
|
||||
|
||||
func (d *Decoder) compileInterface(typ *rtype, structName, fieldName string) (decoder, error) {
|
||||
return newInterfaceDecoder(typ, structName, fieldName), nil
|
||||
return newInterfaceDecoder(d, typ, structName, fieldName), nil
|
||||
}
|
||||
|
||||
func (d *Decoder) removeConflictFields(fieldMap map[string]*structFieldSet, conflictedMap map[string]struct{}, dec *structDecoder, baseOffset uintptr) {
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
package json
|
||||
|
||||
func (d *Decoder) compileToGetDecoder(typeptr uintptr, typ *rtype) (decoder, error) {
|
||||
if !existsCachedDecoder {
|
||||
if typeptr > maxTypeAddr {
|
||||
return d.compileToGetDecoderSlowPath(typeptr, typ)
|
||||
}
|
||||
|
||||
|
|
|
@ -2,12 +2,14 @@
|
|||
|
||||
package json
|
||||
|
||||
import "sync"
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
var decMu sync.RWMutex
|
||||
|
||||
func (d *Decoder) compileToGetDecoder(typeptr uintptr, typ *rtype) (decoder, error) {
|
||||
if !existsCachedDecoder {
|
||||
if typeptr > maxTypeAddr {
|
||||
return d.compileToGetDecoderSlowPath(typeptr, typ)
|
||||
}
|
||||
|
||||
|
|
|
@ -10,13 +10,15 @@ type interfaceDecoder struct {
|
|||
typ *rtype
|
||||
structName string
|
||||
fieldName string
|
||||
dec *Decoder
|
||||
}
|
||||
|
||||
func newInterfaceDecoder(typ *rtype, structName, fieldName string) *interfaceDecoder {
|
||||
func newInterfaceDecoder(dec *Decoder, typ *rtype, structName, fieldName string) *interfaceDecoder {
|
||||
return &interfaceDecoder{
|
||||
typ: typ,
|
||||
structName: structName,
|
||||
fieldName: fieldName,
|
||||
dec: dec,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -70,26 +72,11 @@ func decodeWithTextUnmarshaler(s *stream, unmarshaler encoding.TextUnmarshaler)
|
|||
return nil
|
||||
}
|
||||
|
||||
func (d *interfaceDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
|
||||
func (d *interfaceDecoder) decodeEmptyInterface(s *stream, p unsafe.Pointer) error {
|
||||
s.skipWhiteSpace()
|
||||
for {
|
||||
switch s.char() {
|
||||
case '{':
|
||||
runtimeInterfaceValue := *(*interface{})(unsafe.Pointer(&interfaceHeader{
|
||||
typ: d.typ,
|
||||
ptr: p,
|
||||
}))
|
||||
rv := reflect.ValueOf(runtimeInterfaceValue)
|
||||
if rv.NumMethod() > 0 && rv.CanInterface() {
|
||||
if u, ok := rv.Interface().(Unmarshaler); ok {
|
||||
return decodeWithUnmarshaler(s, u)
|
||||
}
|
||||
if u, ok := rv.Interface().(encoding.TextUnmarshaler); ok {
|
||||
return decodeWithTextUnmarshaler(s, u)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// empty interface
|
||||
var v map[string]interface{}
|
||||
ptr := unsafe.Pointer(&v)
|
||||
if err := newMapDecoder(
|
||||
|
@ -97,7 +84,7 @@ func (d *interfaceDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
|
|||
stringType,
|
||||
newStringDecoder(d.structName, d.fieldName),
|
||||
interfaceMapType.Elem(),
|
||||
newInterfaceDecoder(d.typ, d.structName, d.fieldName),
|
||||
newInterfaceDecoder(d.dec, d.typ, d.structName, d.fieldName),
|
||||
d.structName,
|
||||
d.fieldName,
|
||||
).decodeStream(s, ptr); err != nil {
|
||||
|
@ -109,7 +96,7 @@ func (d *interfaceDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
|
|||
var v []interface{}
|
||||
ptr := unsafe.Pointer(&v)
|
||||
if err := newSliceDecoder(
|
||||
newInterfaceDecoder(d.typ, d.structName, d.fieldName),
|
||||
newInterfaceDecoder(d.dec, d.typ, d.structName, d.fieldName),
|
||||
d.typ,
|
||||
d.typ.Size(),
|
||||
d.structName,
|
||||
|
@ -171,6 +158,35 @@ func (d *interfaceDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
|
|||
return errNotAtBeginningOfValue(s.totalOffset())
|
||||
}
|
||||
|
||||
func (d *interfaceDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
|
||||
runtimeInterfaceValue := *(*interface{})(unsafe.Pointer(&interfaceHeader{
|
||||
typ: d.typ,
|
||||
ptr: p,
|
||||
}))
|
||||
rv := reflect.ValueOf(runtimeInterfaceValue)
|
||||
if rv.NumMethod() > 0 && rv.CanInterface() {
|
||||
if u, ok := rv.Interface().(Unmarshaler); ok {
|
||||
return decodeWithUnmarshaler(s, u)
|
||||
}
|
||||
if u, ok := rv.Interface().(encoding.TextUnmarshaler); ok {
|
||||
return decodeWithTextUnmarshaler(s, u)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
iface := rv.Interface()
|
||||
ifaceHeader := (*interfaceHeader)(unsafe.Pointer(&iface))
|
||||
typ := ifaceHeader.typ
|
||||
if d.typ == typ || typ == nil {
|
||||
// concrete type is empty interface
|
||||
return d.decodeEmptyInterface(s, p)
|
||||
}
|
||||
decoder, err := d.dec.compileToGetDecoder(uintptr(unsafe.Pointer(typ)), typ)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return decoder.decodeStream(s, ifaceHeader.ptr)
|
||||
}
|
||||
|
||||
func (d *interfaceDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
|
||||
cursor = skipWhiteSpace(buf, cursor)
|
||||
switch buf[cursor] {
|
||||
|
@ -182,7 +198,7 @@ func (d *interfaceDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (i
|
|||
stringType,
|
||||
newStringDecoder(d.structName, d.fieldName),
|
||||
interfaceMapType.Elem(),
|
||||
newInterfaceDecoder(d.typ, d.structName, d.fieldName),
|
||||
newInterfaceDecoder(d.dec, d.typ, d.structName, d.fieldName),
|
||||
d.structName, d.fieldName,
|
||||
)
|
||||
cursor, err := dec.decode(buf, cursor, ptr)
|
||||
|
@ -195,7 +211,7 @@ func (d *interfaceDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (i
|
|||
var v []interface{}
|
||||
ptr := unsafe.Pointer(&v)
|
||||
dec := newSliceDecoder(
|
||||
newInterfaceDecoder(d.typ, d.structName, d.fieldName),
|
||||
newInterfaceDecoder(d.dec, d.typ, d.structName, d.fieldName),
|
||||
d.typ,
|
||||
d.typ.Size(),
|
||||
d.structName, d.fieldName,
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package json
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"sync"
|
||||
"unsafe"
|
||||
)
|
||||
|
@ -62,6 +63,16 @@ func copySlice(elemType *rtype, dst, src sliceHeader) int
|
|||
//go:linkname newArray reflect.unsafe_NewArray
|
||||
func newArray(*rtype, int) unsafe.Pointer
|
||||
|
||||
func (d *sliceDecoder) errNumber(buf []byte, offset int64) *UnmarshalTypeError {
|
||||
return &UnmarshalTypeError{
|
||||
Value: "number",
|
||||
Type: reflect.SliceOf(rtype2type(d.elemType)),
|
||||
Struct: d.structName,
|
||||
Field: d.fieldName,
|
||||
Offset: offset,
|
||||
}
|
||||
}
|
||||
|
||||
func (d *sliceDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
|
||||
for {
|
||||
switch s.char() {
|
||||
|
@ -140,11 +151,15 @@ func (d *sliceDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
|
|||
}
|
||||
s.cursor++
|
||||
}
|
||||
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
||||
return d.errNumber([]byte{s.char()}, s.totalOffset())
|
||||
case nul:
|
||||
if s.read() {
|
||||
continue
|
||||
}
|
||||
goto ERROR
|
||||
default:
|
||||
goto ERROR
|
||||
}
|
||||
}
|
||||
ERROR:
|
||||
|
|
|
@ -35,7 +35,7 @@ func (d *stringDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*(*string)(p) = *(*string)(unsafe.Pointer(&bytes))
|
||||
**(**string)(unsafe.Pointer(&p)) = *(*string)(unsafe.Pointer(&bytes))
|
||||
s.reset()
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -5,10 +5,11 @@ package json
|
|||
import "unsafe"
|
||||
|
||||
func encodeCompileToGetCodeSet(typeptr uintptr) (*opcodeSet, error) {
|
||||
if !existsCachedOpcodeSets {
|
||||
if typeptr > maxTypeAddr {
|
||||
return encodeCompileToGetCodeSetSlowPath(typeptr)
|
||||
}
|
||||
if codeSet := cachedOpcodeSets[typeptr-baseTypeAddr]; codeSet != nil {
|
||||
index := typeptr - baseTypeAddr
|
||||
if codeSet := cachedOpcodeSets[index]; codeSet != nil {
|
||||
return codeSet, nil
|
||||
}
|
||||
|
||||
|
@ -29,6 +30,6 @@ func encodeCompileToGetCodeSet(typeptr uintptr) (*opcodeSet, error) {
|
|||
code: code,
|
||||
codeLength: codeLength,
|
||||
}
|
||||
cachedOpcodeSets[int(typeptr-baseTypeAddr)] = codeSet
|
||||
cachedOpcodeSets[index] = codeSet
|
||||
return codeSet, nil
|
||||
}
|
||||
|
|
|
@ -10,11 +10,12 @@ import (
|
|||
var setsMu sync.RWMutex
|
||||
|
||||
func encodeCompileToGetCodeSet(typeptr uintptr) (*opcodeSet, error) {
|
||||
if !existsCachedOpcodeSets {
|
||||
if typeptr > maxTypeAddr {
|
||||
return encodeCompileToGetCodeSetSlowPath(typeptr)
|
||||
}
|
||||
index := typeptr - baseTypeAddr
|
||||
setsMu.RLock()
|
||||
if codeSet := cachedOpcodeSets[typeptr-baseTypeAddr]; codeSet != nil {
|
||||
if codeSet := cachedOpcodeSets[index]; codeSet != nil {
|
||||
setsMu.RUnlock()
|
||||
return codeSet, nil
|
||||
}
|
||||
|
@ -38,7 +39,7 @@ func encodeCompileToGetCodeSet(typeptr uintptr) (*opcodeSet, error) {
|
|||
codeLength: codeLength,
|
||||
}
|
||||
setsMu.Lock()
|
||||
cachedOpcodeSets[int(typeptr-baseTypeAddr)] = codeSet
|
||||
cachedOpcodeSets[index] = codeSet
|
||||
setsMu.Unlock()
|
||||
return codeSet, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue