go-json/decode_ptr.go

44 lines
934 B
Go
Raw Permalink Normal View History

2020-04-23 19:39:20 +03:00
package json
import (
2020-11-17 21:26:32 +03:00
"sync"
2020-04-23 19:39:20 +03:00
"unsafe"
)
type ptrDecoder struct {
2020-11-17 21:26:32 +03:00
dec decoder
typ *rtype
mu sync.Mutex
keepRefs []unsafe.Pointer
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
2020-11-17 21:26:32 +03:00
func unsafe_New(*rtype) unsafe.Pointer
2020-04-24 14:23:26 +03:00
2020-07-30 16:41:53 +03:00
func (d *ptrDecoder) decodeStream(s *stream, p uintptr) error {
newptr := unsafe_New(d.typ)
2020-11-17 21:26:32 +03:00
if err := d.dec.decodeStream(s, uintptr(newptr)); err != nil {
2020-07-30 16:41:53 +03:00
return err
}
2020-11-17 21:26:32 +03:00
**(**unsafe.Pointer)(unsafe.Pointer(&p)) = newptr
2020-07-30 16:41:53 +03:00
return nil
}
2020-05-23 06:51:09 +03:00
func (d *ptrDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error) {
2020-11-17 21:26:32 +03:00
d.mu.Lock()
2020-04-24 14:23:26 +03:00
newptr := unsafe_New(d.typ)
2020-11-17 21:26:32 +03:00
d.keepRefs = append(d.keepRefs, newptr)
**(**unsafe.Pointer)(unsafe.Pointer(&p)) = newptr
d.mu.Unlock()
c, err := d.dec.decode(buf, cursor, uintptr(newptr))
2020-05-06 20:37:29 +03:00
if err != nil {
return 0, err
2020-04-23 19:39:20 +03:00
}
2020-05-06 20:37:29 +03:00
cursor = c
return cursor, nil
2020-04-23 19:39:20 +03:00
}