go-json/decode_ptr.go

38 lines
754 B
Go
Raw Permalink 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-07-30 16:41:53 +03:00
func (d *ptrDecoder) decodeStream(s *stream, p uintptr) error {
newptr := unsafe_New(d.typ)
if err := d.dec.decodeStream(s, newptr); err != nil {
return err
}
*(*uintptr)(unsafe.Pointer(p)) = newptr
return nil
}
2020-05-23 06:51:09 +03:00
func (d *ptrDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error) {
2020-04-24 14:23:26 +03:00
newptr := unsafe_New(d.typ)
2020-05-06 20:37:29 +03:00
c, err := d.dec.decode(buf, cursor, newptr)
if err != nil {
return 0, err
2020-04-23 19:39:20 +03:00
}
2020-05-06 20:37:29 +03:00
cursor = c
2020-04-23 19:39:20 +03:00
*(*uintptr)(unsafe.Pointer(p)) = newptr
2020-05-06 20:37:29 +03:00
return cursor, nil
2020-04-23 19:39:20 +03:00
}