| 1 |
package decoder |
| 2 |
|
| 3 |
import ( |
| 4 |
"reflect" |
| 5 |
"time" |
| 6 |
"unsafe" |
| 7 |
|
| 8 |
"github.com/goccy/go-json/internal/runtime" |
| 9 |
) |
| 10 |
|
| 11 |
// The decoders allocate and assign values through the reflect package, never through the internal |
| 12 |
// functions of the runtime: the pointers to the type descriptors they need are computed once, when |
| 13 |
// a decoder is compiled. |
| 14 |
|
| 15 |
// valueAt returns the addressable reflect.Value of the value at p, whose pointer type is ptrType |
| 16 |
// ( the type descriptor of *T for a value of T ). It is reflect.NewAt(T, p).Elem() without the lookup |
| 17 |
// of the pointer type which reflect.NewAt does for every call. |
| 18 |
func valueAt(ptrType, p unsafe.Pointer) reflect.Value { |
| 19 |
return reflect.ValueOf(*(*any)(unsafe.Pointer(&emptyInterface{typ: ptrType, ptr: p}))).Elem() |
| 20 |
} |
| 21 |
|
| 22 |
// ptrTypeOf returns the type descriptor of *typ. |
| 23 |
func ptrTypeOf(typ reflect.Type) unsafe.Pointer { |
| 24 |
return runtime.TypePtr(reflect.PointerTo(typ)) |
| 25 |
} |
| 26 |
|
| 27 |
// newValue allocates a zero value of typ. |
| 28 |
func newValue(typ reflect.Type) unsafe.Pointer { |
| 29 |
return reflect.New(typ).UnsafePointer() |
| 30 |
} |
| 31 |
|
| 32 |
// allocatorOf returns the function which allocates a zero value of typ, for a decoder which allocates one for |
| 33 |
// every value it decodes, as the one of a pointer does. For the kinds of the basic types it is new of the basic |
| 34 |
// type of the kind, whose layout, pointers included, is the one of typ whatever its name; for time.Time, it is |
| 35 |
// new of it. They cost less than reflect.New, which looks up the pointer type of typ at every call. It is nil |
| 36 |
// for any other type, whose pointer is decoded by ptrDecoder, which allocates by newValue ( see basicPtrDecoder ). |
| 37 |
func allocatorOf(typ reflect.Type) func() unsafe.Pointer { |
| 38 |
if typ == reflect.TypeOf(time.Time{}) { |
| 39 |
return func() unsafe.Pointer { return unsafe.Pointer(new(time.Time)) } |
| 40 |
} |
| 41 |
switch typ.Kind() { |
| 42 |
case reflect.Bool: |
| 43 |
return func() unsafe.Pointer { return unsafe.Pointer(new(bool)) } |
| 44 |
case reflect.Int: |
| 45 |
return func() unsafe.Pointer { return unsafe.Pointer(new(int)) } |
| 46 |
case reflect.Int8: |
| 47 |
return func() unsafe.Pointer { return unsafe.Pointer(new(int8)) } |
| 48 |
case reflect.Int16: |
| 49 |
return func() unsafe.Pointer { return unsafe.Pointer(new(int16)) } |
| 50 |
case reflect.Int32: |
| 51 |
return func() unsafe.Pointer { return unsafe.Pointer(new(int32)) } |
| 52 |
case reflect.Int64: |
| 53 |
return func() unsafe.Pointer { return unsafe.Pointer(new(int64)) } |
| 54 |
case reflect.Uint: |
| 55 |
return func() unsafe.Pointer { return unsafe.Pointer(new(uint)) } |
| 56 |
case reflect.Uint8: |
| 57 |
return func() unsafe.Pointer { return unsafe.Pointer(new(uint8)) } |
| 58 |
case reflect.Uint16: |
| 59 |
return func() unsafe.Pointer { return unsafe.Pointer(new(uint16)) } |
| 60 |
case reflect.Uint32: |
| 61 |
return func() unsafe.Pointer { return unsafe.Pointer(new(uint32)) } |
| 62 |
case reflect.Uint64: |
| 63 |
return func() unsafe.Pointer { return unsafe.Pointer(new(uint64)) } |
| 64 |
case reflect.Uintptr: |
| 65 |
return func() unsafe.Pointer { return unsafe.Pointer(new(uintptr)) } |
| 66 |
case reflect.Float32: |
| 67 |
return func() unsafe.Pointer { return unsafe.Pointer(new(float32)) } |
| 68 |
case reflect.Float64: |
| 69 |
return func() unsafe.Pointer { return unsafe.Pointer(new(float64)) } |
| 70 |
case reflect.Complex64: |
| 71 |
return func() unsafe.Pointer { return unsafe.Pointer(new(complex64)) } |
| 72 |
case reflect.Complex128: |
| 73 |
return func() unsafe.Pointer { return unsafe.Pointer(new(complex128)) } |
| 74 |
case reflect.String: |
| 75 |
return func() unsafe.Pointer { return unsafe.Pointer(new(string)) } |
| 76 |
} |
| 77 |
return nil |
| 78 |
} |
| 79 |
|
| 80 |
// zeroBase is the address of the data of an empty slice which is not nil. |
| 81 |
var zeroBase [0]byte |