go-json/decode_ptr.go

27 lines
479 B
Go
Raw Normal View History

2020-04-23 19:39:20 +03:00
package json
import (
"unsafe"
)
type ptrDecoder struct {
dec decoder
2020-04-24 14:23:26 +03:00
typ *rtype
2020-04-23 19:39:20 +03:00
}
2020-04-24 14:23:26 +03:00
func newPtrDecoder(dec decoder, typ *rtype) *ptrDecoder {
2020-04-23 19:39:20 +03:00
return &ptrDecoder{dec: dec, typ: typ}
}
2020-04-24 14:23:26 +03:00
//go:linkname unsafe_New reflect.unsafe_New
func unsafe_New(*rtype) uintptr
2020-04-23 19:39:20 +03:00
func (d *ptrDecoder) decode(ctx *context, p uintptr) error {
2020-04-24 14:23:26 +03:00
newptr := unsafe_New(d.typ)
2020-04-23 19:39:20 +03:00
if err := d.dec.decode(ctx, newptr); err != nil {
return err
}
*(*uintptr)(unsafe.Pointer(p)) = newptr
return nil
}