2020-04-25 13:55:05 +03:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2020-05-07 15:30:36 +03:00
|
|
|
"sync"
|
2020-04-25 13:55:05 +03:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
type sliceDecoder struct {
|
|
|
|
elemType *rtype
|
|
|
|
valueDecoder decoder
|
|
|
|
size uintptr
|
2020-05-07 15:30:36 +03:00
|
|
|
arrayPool sync.Pool
|
2020-04-25 13:55:05 +03:00
|
|
|
}
|
|
|
|
|
2020-05-30 18:11:50 +03:00
|
|
|
// If use reflect.SliceHeader, data type is uintptr.
|
|
|
|
// In this case, Go compiler cannot trace reference created by newArray().
|
|
|
|
// So, define using unsafe.Pointer as data type
|
|
|
|
type sliceHeader struct {
|
|
|
|
data unsafe.Pointer
|
|
|
|
len int
|
|
|
|
cap int
|
|
|
|
}
|
|
|
|
|
2020-04-25 13:55:05 +03:00
|
|
|
func newSliceDecoder(dec decoder, elemType *rtype, size uintptr) *sliceDecoder {
|
|
|
|
return &sliceDecoder{
|
|
|
|
valueDecoder: dec,
|
|
|
|
elemType: elemType,
|
|
|
|
size: size,
|
2020-05-07 15:30:36 +03:00
|
|
|
arrayPool: sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
cap := 2
|
2020-05-30 18:11:50 +03:00
|
|
|
return &sliceHeader{
|
|
|
|
data: newArray(elemType, cap),
|
|
|
|
len: 0,
|
|
|
|
cap: cap,
|
2020-05-07 15:30:36 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2020-04-25 13:55:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-30 18:11:50 +03:00
|
|
|
func (d *sliceDecoder) newSlice() *sliceHeader {
|
|
|
|
slice := d.arrayPool.Get().(*sliceHeader)
|
|
|
|
slice.len = 0
|
2020-05-07 15:30:36 +03:00
|
|
|
return slice
|
|
|
|
}
|
|
|
|
|
2020-05-30 18:11:50 +03:00
|
|
|
func (d *sliceDecoder) releaseSlice(p *sliceHeader) {
|
2020-05-07 15:30:36 +03:00
|
|
|
d.arrayPool.Put(p)
|
|
|
|
}
|
|
|
|
|
2020-04-25 13:55:05 +03:00
|
|
|
//go:linkname copySlice reflect.typedslicecopy
|
|
|
|
func copySlice(elemType *rtype, dst, src reflect.SliceHeader) int
|
|
|
|
|
|
|
|
//go:linkname newArray reflect.unsafe_NewArray
|
|
|
|
func newArray(*rtype, int) unsafe.Pointer
|
|
|
|
|
2020-07-30 16:41:53 +03:00
|
|
|
func (d *sliceDecoder) decodeStream(s *stream, p uintptr) error {
|
|
|
|
for {
|
|
|
|
switch s.char() {
|
|
|
|
case ' ', '\n', '\t', '\r':
|
2020-07-31 11:10:03 +03:00
|
|
|
s.cursor++
|
2020-07-30 16:41:53 +03:00
|
|
|
continue
|
2020-08-08 07:20:42 +03:00
|
|
|
case 'n':
|
|
|
|
if err := nullBytes(s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2020-07-30 16:41:53 +03:00
|
|
|
case '[':
|
|
|
|
idx := 0
|
|
|
|
slice := d.newSlice()
|
2020-07-31 12:52:22 +03:00
|
|
|
cap := slice.cap
|
|
|
|
data := slice.data
|
2020-07-31 11:10:03 +03:00
|
|
|
for {
|
|
|
|
s.cursor++
|
2020-07-30 16:41:53 +03:00
|
|
|
if cap <= idx {
|
2020-07-31 12:52:22 +03:00
|
|
|
src := reflect.SliceHeader{Data: uintptr(data), Len: idx, Cap: cap}
|
2020-07-30 16:41:53 +03:00
|
|
|
cap *= 2
|
2020-07-31 12:52:22 +03:00
|
|
|
data = newArray(d.elemType, cap)
|
|
|
|
dst := reflect.SliceHeader{Data: uintptr(data), Len: idx, Cap: cap}
|
2020-07-30 16:41:53 +03:00
|
|
|
copySlice(d.elemType, dst, src)
|
|
|
|
}
|
2020-07-31 12:52:22 +03:00
|
|
|
if err := d.valueDecoder.decodeStream(s, uintptr(data)+uintptr(idx)*d.size); err != nil {
|
2020-07-30 16:41:53 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.skipWhiteSpace()
|
2020-07-31 11:10:03 +03:00
|
|
|
RETRY:
|
2020-07-30 16:41:53 +03:00
|
|
|
switch s.char() {
|
|
|
|
case ']':
|
2020-07-31 12:52:22 +03:00
|
|
|
slice.cap = cap
|
|
|
|
slice.len = idx + 1
|
|
|
|
slice.data = data
|
2020-07-30 16:41:53 +03:00
|
|
|
dstCap := idx + 1
|
|
|
|
dst := reflect.SliceHeader{
|
|
|
|
Data: uintptr(newArray(d.elemType, dstCap)),
|
|
|
|
Len: idx + 1,
|
|
|
|
Cap: dstCap,
|
|
|
|
}
|
2020-07-31 12:52:22 +03:00
|
|
|
copySlice(d.elemType, dst, reflect.SliceHeader{
|
|
|
|
Data: uintptr(slice.data),
|
|
|
|
Len: slice.len,
|
|
|
|
Cap: slice.cap,
|
|
|
|
})
|
2020-07-30 16:41:53 +03:00
|
|
|
*(*reflect.SliceHeader)(unsafe.Pointer(p)) = dst
|
|
|
|
d.releaseSlice(slice)
|
2020-07-31 11:10:03 +03:00
|
|
|
s.cursor++
|
2020-07-30 16:41:53 +03:00
|
|
|
return nil
|
|
|
|
case ',':
|
|
|
|
idx++
|
|
|
|
continue
|
2020-07-31 11:10:03 +03:00
|
|
|
case nul:
|
|
|
|
if s.read() {
|
|
|
|
goto RETRY
|
|
|
|
}
|
2020-07-31 12:52:22 +03:00
|
|
|
slice.cap = cap
|
|
|
|
slice.data = data
|
2020-07-31 11:10:03 +03:00
|
|
|
d.releaseSlice(slice)
|
|
|
|
goto ERROR
|
2020-07-30 16:41:53 +03:00
|
|
|
default:
|
2020-07-31 12:52:22 +03:00
|
|
|
slice.cap = cap
|
|
|
|
slice.data = data
|
2020-07-30 16:41:53 +03:00
|
|
|
d.releaseSlice(slice)
|
2020-07-31 11:10:03 +03:00
|
|
|
goto ERROR
|
2020-07-30 16:41:53 +03:00
|
|
|
}
|
|
|
|
}
|
2020-07-31 11:10:03 +03:00
|
|
|
case nul:
|
|
|
|
if s.read() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
goto ERROR
|
2020-07-30 16:41:53 +03:00
|
|
|
}
|
|
|
|
}
|
2020-07-31 11:10:03 +03:00
|
|
|
ERROR:
|
2020-07-30 16:41:53 +03:00
|
|
|
return errUnexpectedEndOfJSON("slice", s.totalOffset())
|
|
|
|
}
|
|
|
|
|
2020-05-23 06:51:09 +03:00
|
|
|
func (d *sliceDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error) {
|
|
|
|
buflen := int64(len(buf))
|
2020-04-25 13:55:05 +03:00
|
|
|
for ; cursor < buflen; cursor++ {
|
|
|
|
switch buf[cursor] {
|
|
|
|
case ' ', '\n', '\t', '\r':
|
|
|
|
continue
|
2020-08-08 07:20:42 +03:00
|
|
|
case 'n':
|
|
|
|
buflen := int64(len(buf))
|
|
|
|
if cursor+3 >= buflen {
|
|
|
|
return 0, errUnexpectedEndOfJSON("null", cursor)
|
|
|
|
}
|
|
|
|
if buf[cursor+1] != 'u' {
|
|
|
|
return 0, errInvalidCharacter(buf[cursor+1], "null", cursor)
|
|
|
|
}
|
|
|
|
if buf[cursor+2] != 'l' {
|
|
|
|
return 0, errInvalidCharacter(buf[cursor+2], "null", cursor)
|
|
|
|
}
|
|
|
|
if buf[cursor+3] != 'l' {
|
|
|
|
return 0, errInvalidCharacter(buf[cursor+3], "null", cursor)
|
|
|
|
}
|
|
|
|
cursor += 4
|
|
|
|
return cursor, nil
|
2020-04-25 13:55:05 +03:00
|
|
|
case '[':
|
|
|
|
idx := 0
|
2020-05-07 15:30:36 +03:00
|
|
|
slice := d.newSlice()
|
2020-05-30 18:11:50 +03:00
|
|
|
cap := slice.cap
|
|
|
|
data := slice.data
|
2020-04-25 13:55:05 +03:00
|
|
|
for {
|
2020-05-06 20:37:29 +03:00
|
|
|
cursor++
|
2020-04-25 13:55:05 +03:00
|
|
|
if cap <= idx {
|
2020-05-30 18:11:50 +03:00
|
|
|
src := reflect.SliceHeader{Data: uintptr(data), Len: idx, Cap: cap}
|
2020-04-25 13:55:05 +03:00
|
|
|
cap *= 2
|
2020-05-30 18:11:50 +03:00
|
|
|
data = newArray(d.elemType, cap)
|
|
|
|
dst := reflect.SliceHeader{Data: uintptr(data), Len: idx, Cap: cap}
|
2020-04-25 13:55:05 +03:00
|
|
|
copySlice(d.elemType, dst, src)
|
|
|
|
}
|
2020-05-30 18:11:50 +03:00
|
|
|
c, err := d.valueDecoder.decode(buf, cursor, uintptr(data)+uintptr(idx)*d.size)
|
2020-05-06 20:37:29 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2020-04-25 13:55:05 +03:00
|
|
|
}
|
2020-05-06 20:37:29 +03:00
|
|
|
cursor = c
|
|
|
|
cursor = skipWhiteSpace(buf, cursor)
|
2020-04-25 13:55:05 +03:00
|
|
|
switch buf[cursor] {
|
|
|
|
case ']':
|
2020-05-30 18:11:50 +03:00
|
|
|
slice.cap = cap
|
|
|
|
slice.len = idx + 1
|
|
|
|
slice.data = data
|
2020-05-07 15:30:36 +03:00
|
|
|
dstCap := idx + 1
|
|
|
|
dst := reflect.SliceHeader{
|
|
|
|
Data: uintptr(newArray(d.elemType, dstCap)),
|
2020-04-25 13:55:05 +03:00
|
|
|
Len: idx + 1,
|
2020-05-07 15:30:36 +03:00
|
|
|
Cap: dstCap,
|
2020-04-25 13:55:05 +03:00
|
|
|
}
|
2020-05-30 18:11:50 +03:00
|
|
|
copySlice(d.elemType, dst, reflect.SliceHeader{
|
|
|
|
Data: uintptr(slice.data),
|
|
|
|
Len: slice.len,
|
|
|
|
Cap: slice.cap,
|
|
|
|
})
|
2020-05-07 15:30:36 +03:00
|
|
|
*(*reflect.SliceHeader)(unsafe.Pointer(p)) = dst
|
|
|
|
d.releaseSlice(slice)
|
2020-05-06 20:37:29 +03:00
|
|
|
cursor++
|
|
|
|
return cursor, nil
|
2020-04-25 13:55:05 +03:00
|
|
|
case ',':
|
|
|
|
idx++
|
|
|
|
continue
|
|
|
|
default:
|
2020-05-30 18:11:50 +03:00
|
|
|
slice.cap = cap
|
|
|
|
slice.data = data
|
2020-05-07 15:30:36 +03:00
|
|
|
d.releaseSlice(slice)
|
2020-05-23 06:51:09 +03:00
|
|
|
return 0, errInvalidCharacter(buf[cursor], "slice", cursor)
|
2020-04-25 13:55:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-23 06:51:09 +03:00
|
|
|
return 0, errUnexpectedEndOfJSON("slice", cursor)
|
2020-04-25 13:55:05 +03:00
|
|
|
}
|