| 1 |
package decoder |
| 2 |
|
| 3 |
import ( |
| 4 |
"reflect" |
| 5 |
"sync" |
| 6 |
"sync/atomic" |
| 7 |
"unsafe" |
| 8 |
|
| 9 |
"github.com/goccy/go-json/internal/errors" |
| 10 |
) |
| 11 |
|
| 12 |
type sliceDecoder struct { |
| 13 |
elemType reflect.Type |
| 14 |
// slicePtrType is the type descriptor of the pointer to the slice. |
| 15 |
slicePtrType unsafe.Pointer |
| 16 |
valueDecoder Decoder |
| 17 |
size uintptr |
| 18 |
// bufPool holds the buffers into which the elements of a long array are decoded before its length is known |
| 19 |
// ( see decodeLong ). A buffer in the pool has only zero values, so that an element is decoded into a zero |
| 20 |
// value. |
| 21 |
bufPool sync.Pool |
| 22 |
// lastLen is the length of the array which the decoder decoded last, which an empty slice is allocated for, |
| 23 |
// up to inPlaceSliceLength: the arrays of a kind are often of a length. It is only a hint, for any goroutine. |
| 24 |
lastLen atomic.Int32 |
| 25 |
structName string |
| 26 |
fieldName string |
| 27 |
// typ is the type of the slice, which the type errors report. |
| 28 |
typ reflect.Type |
| 29 |
} |
| 30 |
|
| 31 |
// If use reflect.SliceHeader, data type is uintptr. |
| 32 |
// In this case, Go compiler cannot trace reference created by newArray(). |
| 33 |
// So, define using unsafe.Pointer as data type |
| 34 |
type sliceHeader struct { |
| 35 |
data unsafe.Pointer |
| 36 |
len int |
| 37 |
cap int |
| 38 |
} |
| 39 |
|
| 40 |
// sliceBuf is a buffer of the elements of a slice, allocated and grown by reflect.Value.Grow, |
| 41 |
// so that its memory has the type of the elements. |
| 42 |
type sliceBuf struct { |
| 43 |
hdr sliceHeader |
| 44 |
} |
| 45 |
|
| 46 |
const ( |
| 47 |
// inPlaceSliceLength is the number of the elements of an array which are decoded into the slice itself, if it |
| 48 |
// has no more capacity: most arrays are no longer, and the elements of a longer one are decoded into a buffer |
| 49 |
// from there, so that its slice is allocated once, of its length. |
| 50 |
inPlaceSliceLength = 64 |
| 51 |
// defaultSliceCapacity is the capacity of a new buffer of the elements. |
| 52 |
defaultSliceCapacity = 4 |
| 53 |
) |
| 54 |
|
| 55 |
func newSliceDecoder(dec Decoder, elemType reflect.Type, size uintptr, structName, fieldName string) *sliceDecoder { |
| 56 |
return &sliceDecoder{ |
| 57 |
typ: reflect.SliceOf(elemType), |
| 58 |
valueDecoder: dec, |
| 59 |
elemType: elemType, |
| 60 |
slicePtrType: ptrTypeOf(reflect.SliceOf(elemType)), |
| 61 |
size: size, |
| 62 |
bufPool: sync.Pool{ |
| 63 |
New: func() any { |
| 64 |
return &sliceBuf{} |
| 65 |
}, |
| 66 |
}, |
| 67 |
structName: structName, |
| 68 |
fieldName: fieldName, |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
// sliceValue returns the reflect.Value of the slice whose header is at p. |
| 73 |
func (d *sliceDecoder) sliceValue(p *sliceHeader) reflect.Value { |
| 74 |
return valueAt(d.slicePtrType, unsafe.Pointer(p)) |
| 75 |
} |
| 76 |
|
| 77 |
// grow makes the capacity of the slice at dst n or more, keeping its first length elements, as append does. |
| 78 |
func (d *sliceDecoder) grow(dst *sliceHeader, length, n int) { |
| 79 |
if n <= dst.cap { |
| 80 |
return |
| 81 |
} |
| 82 |
dst.len = length |
| 83 |
if n < defaultSliceCapacity { |
| 84 |
n = defaultSliceCapacity |
| 85 |
} |
| 86 |
d.sliceValue(dst).Grow(n - length) |
| 87 |
} |
| 88 |
|
| 89 |
// decodeLong decodes the elements of the array from cursor, the one at index n, which follows the n elements |
| 90 |
// decoded into the slice at dst, into a buffer whose length is not known yet, and stores them all into the slice, |
| 91 |
// allocated once, of their length. It is not inlined, so that Decode keeps the size it had: most arrays are shorter. |
| 92 |
// |
| 93 |
//go:noinline |
| 94 |
func (d *sliceDecoder) decodeLong(ctx *RuntimeContext, cursor, depth int64, dst *sliceHeader, n int) (int64, error) { |
| 95 |
buf := ctx.Buf |
| 96 |
elems := d.bufPool.Get().(*sliceBuf) |
| 97 |
d.growBuf(elems, 0, 2*n) |
| 98 |
elems.hdr.len = n |
| 99 |
head := *dst |
| 100 |
head.len = n |
| 101 |
reflect.Copy(d.sliceValue(&elems.hdr), d.sliceValue(&head)) |
| 102 |
idx := n |
| 103 |
for { |
| 104 |
d.growBuf(elems, idx, idx+1) |
| 105 |
ep := unsafe.Add(elems.hdr.data, uintptr(idx)*d.size) |
| 106 |
c, err := d.valueDecoder.Decode(ctx, cursor, depth, ep) |
| 107 |
if err != nil { |
| 108 |
d.store(dst, elems, idx+1) |
| 109 |
d.releaseBuf(elems, idx+1) |
| 110 |
return 0, err |
| 111 |
} |
| 112 |
cursor = skipWhiteSpace(buf, c) |
| 113 |
switch buf[cursor] { |
| 114 |
case ']': |
| 115 |
d.store(dst, elems, idx+1) |
| 116 |
d.releaseBuf(elems, idx+1) |
| 117 |
d.lastLen.Store(int32(min(idx+1, inPlaceSliceLength))) |
| 118 |
return cursor + 1, nil |
| 119 |
case ',': |
| 120 |
idx++ |
| 121 |
default: |
| 122 |
d.store(dst, elems, idx+1) |
| 123 |
d.releaseBuf(elems, idx+1) |
| 124 |
return 0, errors.ErrInvalidCharacter(buf[cursor], "slice", cursor) |
| 125 |
} |
| 126 |
cursor++ |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
// growBuf makes the capacity of the buffer n or more, keeping its first length elements. |
| 131 |
// The elements after them are zero values. |
| 132 |
func (d *sliceDecoder) growBuf(buf *sliceBuf, length, n int) { |
| 133 |
d.grow(&buf.hdr, length, n) |
| 134 |
} |
| 135 |
|
| 136 |
// releaseBuf clears the first n elements of the buffer, which are all it may have used, |
| 137 |
// and puts it back to the pool: it keeps nothing the decoded value refers to. |
| 138 |
func (d *sliceDecoder) releaseBuf(buf *sliceBuf, n int) { |
| 139 |
if n > 0 { |
| 140 |
buf.hdr.len = n |
| 141 |
d.sliceValue(&buf.hdr).Clear() |
| 142 |
} |
| 143 |
buf.hdr.len = 0 |
| 144 |
d.bufPool.Put(buf) |
| 145 |
} |
| 146 |
|
| 147 |
// store copies the n elements of the buffer to the slice at dst: into its array when it has room |
| 148 |
// for them, or else into a new array of their length. |
| 149 |
func (d *sliceDecoder) store(dst *sliceHeader, buf *sliceBuf, n int) { |
| 150 |
if dst.cap < n { |
| 151 |
*dst = sliceHeader{} |
| 152 |
d.sliceValue(dst).Grow(n) |
| 153 |
} |
| 154 |
dst.len = n |
| 155 |
buf.hdr.len = n |
| 156 |
reflect.Copy(d.sliceValue(dst), d.sliceValue(&buf.hdr)) |
| 157 |
} |
| 158 |
|
| 159 |
func (d *sliceDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) { |
| 160 |
buf := ctx.Buf |
| 161 |
depth++ |
| 162 |
if depth > maxDecodeNestingDepth { |
| 163 |
return 0, errors.ErrExceededMaxDepth(buf[cursor], cursor) |
| 164 |
} |
| 165 |
|
| 166 |
for { |
| 167 |
switch buf[cursor] { |
| 168 |
case ' ', '\n', '\t', '\r': |
| 169 |
cursor++ |
| 170 |
continue |
| 171 |
case 'n': |
| 172 |
if err := validateNull(buf, cursor); err != nil { |
| 173 |
return 0, err |
| 174 |
} |
| 175 |
cursor += 4 |
| 176 |
*(*sliceHeader)(p) = sliceHeader{} |
| 177 |
return cursor, nil |
| 178 |
case '[': |
| 179 |
cursor++ |
| 180 |
cursor = skipWhiteSpace(buf, cursor) |
| 181 |
dst := (*sliceHeader)(p) |
| 182 |
if buf[cursor] == ']' { |
| 183 |
if dst.data == nil { |
| 184 |
dst.data = unsafe.Pointer(&zeroBase) |
| 185 |
} else { |
| 186 |
dst.len = 0 |
| 187 |
} |
| 188 |
cursor++ |
| 189 |
return cursor, nil |
| 190 |
} |
| 191 |
// The elements are decoded into the slice, which grows as append grows it: the elements of its array are |
| 192 |
// decoded into, the ones after its length too, as encoding/json does. The elements beyond its capacity |
| 193 |
// and inPlaceSliceLength are decoded into a buffer ( see decodeLong ). |
| 194 |
limit := max(dst.cap, inPlaceSliceLength) |
| 195 |
if dst.cap == 0 { |
| 196 |
// the length of the last array, which an array of the kind is likely to have |
| 197 |
if n := int(d.lastLen.Load()); n > 0 { |
| 198 |
d.grow(dst, 0, min(n, inPlaceSliceLength)) |
| 199 |
} |
| 200 |
} |
| 201 |
idx := 0 |
| 202 |
for { |
| 203 |
if idx == limit { |
| 204 |
return d.decodeLong(ctx, cursor, depth, dst, idx) |
| 205 |
} |
| 206 |
d.grow(dst, idx, idx+1) |
| 207 |
ep := unsafe.Add(dst.data, uintptr(idx)*d.size) |
| 208 |
c, err := d.valueDecoder.Decode(ctx, cursor, depth, ep) |
| 209 |
if err != nil { |
| 210 |
dst.len = idx + 1 |
| 211 |
return 0, err |
| 212 |
} |
| 213 |
cursor = skipWhiteSpace(buf, c) |
| 214 |
switch buf[cursor] { |
| 215 |
case ']': |
| 216 |
dst.len = idx + 1 |
| 217 |
d.lastLen.Store(int32(idx + 1)) |
| 218 |
cursor++ |
| 219 |
return cursor, nil |
| 220 |
case ',': |
| 221 |
idx++ |
| 222 |
default: |
| 223 |
dst.len = idx + 1 |
| 224 |
return 0, errors.ErrInvalidCharacter(buf[cursor], "slice", cursor) |
| 225 |
} |
| 226 |
cursor++ |
| 227 |
} |
| 228 |
default: |
| 229 |
return d.decodeOther(ctx, cursor, depth-1) |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
// decodeOther skips the value at cursor, which is not an array: a value of another kind is a type error, and |
| 235 |
// anything else a syntax error. It is a function of its own, so that Decode keeps the size it had. |
| 236 |
// |
| 237 |
//go:noinline |
| 238 |
func (d *sliceDecoder) decodeOther(ctx *RuntimeContext, cursor, depth int64) (int64, error) { |
| 239 |
if isOtherValue(ctx.Buf[cursor], arrayValue) { |
| 240 |
return ctx.skipTypeError(cursor, depth, d.typ) |
| 241 |
} |
| 242 |
return 0, errors.ErrUnexpectedEndOfJSON("slice", cursor) |
| 243 |
} |
| 244 |
|
| 245 |
func (d *sliceDecoder) DecodePath(ctx *RuntimeContext, cursor, depth int64) ([][]byte, int64, error) { |
| 246 |
buf := ctx.Buf |
| 247 |
depth++ |
| 248 |
if depth > maxDecodeNestingDepth { |
| 249 |
return nil, 0, errors.ErrExceededMaxDepth(buf[cursor], cursor) |
| 250 |
} |
| 251 |
|
| 252 |
ret := [][]byte{} |
| 253 |
for { |
| 254 |
switch buf[cursor] { |
| 255 |
case ' ', '\n', '\t', '\r': |
| 256 |
cursor++ |
| 257 |
continue |
| 258 |
case 'n': |
| 259 |
if err := validateNull(buf, cursor); err != nil { |
| 260 |
return nil, 0, err |
| 261 |
} |
| 262 |
cursor += 4 |
| 263 |
return [][]byte{nullbytes}, cursor, nil |
| 264 |
case '[': |
| 265 |
cursor++ |
| 266 |
cursor = skipWhiteSpace(buf, cursor) |
| 267 |
if buf[cursor] == ']' { |
| 268 |
cursor++ |
| 269 |
return ret, cursor, nil |
| 270 |
} |
| 271 |
idx := 0 |
| 272 |
for { |
| 273 |
child, found, err := ctx.Option.Path.node.Index(idx) |
| 274 |
if err != nil { |
| 275 |
return nil, 0, err |
| 276 |
} |
| 277 |
if found { |
| 278 |
if child != nil { |
| 279 |
oldPath := ctx.Option.Path.node |
| 280 |
ctx.Option.Path.node = child |
| 281 |
paths, c, err := d.valueDecoder.DecodePath(ctx, cursor, depth) |
| 282 |
if err != nil { |
| 283 |
return nil, 0, err |
| 284 |
} |
| 285 |
ctx.Option.Path.node = oldPath |
| 286 |
ret = append(ret, paths...) |
| 287 |
cursor = c |
| 288 |
} else { |
| 289 |
start := cursor |
| 290 |
end, err := skipValue(buf, cursor, depth) |
| 291 |
if err != nil { |
| 292 |
return nil, 0, err |
| 293 |
} |
| 294 |
ret = append(ret, buf[start:end]) |
| 295 |
cursor = end |
| 296 |
} |
| 297 |
} else { |
| 298 |
c, err := skipValue(buf, cursor, depth) |
| 299 |
if err != nil { |
| 300 |
return nil, 0, err |
| 301 |
} |
| 302 |
cursor = c |
| 303 |
} |
| 304 |
cursor = skipWhiteSpace(buf, cursor) |
| 305 |
switch buf[cursor] { |
| 306 |
case ']': |
| 307 |
cursor++ |
| 308 |
return ret, cursor, nil |
| 309 |
case ',': |
| 310 |
idx++ |
| 311 |
default: |
| 312 |
return nil, 0, errors.ErrInvalidCharacter(buf[cursor], "slice", cursor) |
| 313 |
} |
| 314 |
cursor++ |
| 315 |
} |
| 316 |
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': |
| 317 |
return nil, 0, &errors.UnmarshalTypeError{Value: "number", Type: d.typ, Offset: cursor} |
| 318 |
default: |
| 319 |
return nil, 0, errors.ErrUnexpectedEndOfJSON("slice", cursor) |
| 320 |
} |
| 321 |
} |
| 322 |
} |