Pass unsafe.Pointer instead of uintptr

This commit is contained in:
Masaaki Goshima 2020-11-19 12:47:42 +09:00
parent 5ab988208f
commit fd7a72c0b8
17 changed files with 103 additions and 89 deletions

View File

@ -16,8 +16,8 @@ func (d Delim) String() string {
} }
type decoder interface { type decoder interface {
decode([]byte, int64, uintptr) (int64, error) decode([]byte, int64, unsafe.Pointer) (int64, error)
decodeStream(*stream, uintptr) error decodeStream(*stream, unsafe.Pointer) error
} }
type Decoder struct { type Decoder struct {
@ -101,7 +101,7 @@ func (d *Decoder) decode(src []byte, header *interfaceHeader) error {
cachedDecoder.set(typeptr, compiledDec) cachedDecoder.set(typeptr, compiledDec)
dec = compiledDec dec = compiledDec
} }
if _, err := dec.decode(src, 0, ptr); err != nil { if _, err := dec.decode(src, 0, header.ptr); err != nil {
return err return err
} }
return nil return nil
@ -170,7 +170,7 @@ func (d *Decoder) Decode(v interface{}) error {
return err return err
} }
s := d.s s := d.s
if err := dec.decodeStream(s, ptr); err != nil { if err := dec.decodeStream(s, header.ptr); err != nil {
return err return err
} }
return nil return nil

View File

@ -1,5 +1,7 @@
package json package json
import "unsafe"
type arrayDecoder struct { type arrayDecoder struct {
elemType *rtype elemType *rtype
size uintptr size uintptr
@ -16,7 +18,7 @@ func newArrayDecoder(dec decoder, elemType *rtype, alen int) *arrayDecoder {
} }
} }
func (d *arrayDecoder) decodeStream(s *stream, p uintptr) error { func (d *arrayDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
for { for {
switch s.char() { switch s.char() {
case ' ', '\n', '\t', '\r': case ' ', '\n', '\t', '\r':
@ -29,7 +31,8 @@ func (d *arrayDecoder) decodeStream(s *stream, p uintptr) error {
idx := 0 idx := 0
for { for {
s.cursor++ s.cursor++
if err := d.valueDecoder.decodeStream(s, p+uintptr(idx)*d.size); err != nil { addr := uintptr(p) + uintptr(idx)*d.size
if err := d.valueDecoder.decodeStream(s, unsafe.Pointer(addr)); err != nil {
return err return err
} }
s.skipWhiteSpace() s.skipWhiteSpace()
@ -62,7 +65,7 @@ ERROR:
return errUnexpectedEndOfJSON("array", s.totalOffset()) return errUnexpectedEndOfJSON("array", s.totalOffset())
} }
func (d *arrayDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error) { func (d *arrayDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
buflen := int64(len(buf)) buflen := int64(len(buf))
for ; cursor < buflen; cursor++ { for ; cursor < buflen; cursor++ {
switch buf[cursor] { switch buf[cursor] {
@ -88,7 +91,8 @@ func (d *arrayDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error
idx := 0 idx := 0
for { for {
cursor++ cursor++
c, err := d.valueDecoder.decode(buf, cursor, p+uintptr(idx)*d.size) addr := uintptr(p) + uintptr(idx)*d.size
c, err := d.valueDecoder.decode(buf, cursor, unsafe.Pointer(addr))
if err != nil { if err != nil {
return 0, err return 0, err
} }

View File

@ -58,7 +58,7 @@ func falseBytes(s *stream) error {
return nil return nil
} }
func (d *boolDecoder) decodeStream(s *stream, p uintptr) error { func (d *boolDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
s.skipWhiteSpace() s.skipWhiteSpace()
for { for {
switch s.char() { switch s.char() {
@ -86,7 +86,7 @@ ERROR:
return errUnexpectedEndOfJSON("bool", s.totalOffset()) return errUnexpectedEndOfJSON("bool", s.totalOffset())
} }
func (d *boolDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error) { func (d *boolDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
buflen := int64(len(buf)) buflen := int64(len(buf))
cursor = skipWhiteSpace(buf, cursor) cursor = skipWhiteSpace(buf, cursor)
switch buf[cursor] { switch buf[cursor] {

View File

@ -88,74 +88,74 @@ func (d *Decoder) compilePtr(typ *rtype) (decoder, error) {
} }
func (d *Decoder) compileInt() (decoder, error) { func (d *Decoder) compileInt() (decoder, error) {
return newIntDecoder(func(p uintptr, v int64) { return newIntDecoder(func(p unsafe.Pointer, v int64) {
**(**int)(unsafe.Pointer(&p)) = int(v) *(*int)(p) = int(v)
}), nil }), nil
} }
func (d *Decoder) compileInt8() (decoder, error) { func (d *Decoder) compileInt8() (decoder, error) {
return newIntDecoder(func(p uintptr, v int64) { return newIntDecoder(func(p unsafe.Pointer, v int64) {
**(**int8)(unsafe.Pointer(&p)) = int8(v) *(*int8)(p) = int8(v)
}), nil }), nil
} }
func (d *Decoder) compileInt16() (decoder, error) { func (d *Decoder) compileInt16() (decoder, error) {
return newIntDecoder(func(p uintptr, v int64) { return newIntDecoder(func(p unsafe.Pointer, v int64) {
**(**int16)(unsafe.Pointer(&p)) = int16(v) *(*int16)(p) = int16(v)
}), nil }), nil
} }
func (d *Decoder) compileInt32() (decoder, error) { func (d *Decoder) compileInt32() (decoder, error) {
return newIntDecoder(func(p uintptr, v int64) { return newIntDecoder(func(p unsafe.Pointer, v int64) {
**(**int32)(unsafe.Pointer(&p)) = int32(v) *(*int32)(p) = int32(v)
}), nil }), nil
} }
func (d *Decoder) compileInt64() (decoder, error) { func (d *Decoder) compileInt64() (decoder, error) {
return newIntDecoder(func(p uintptr, v int64) { return newIntDecoder(func(p unsafe.Pointer, v int64) {
**(**int64)(unsafe.Pointer(&p)) = v *(*int64)(p) = v
}), nil }), nil
} }
func (d *Decoder) compileUint() (decoder, error) { func (d *Decoder) compileUint() (decoder, error) {
return newUintDecoder(func(p uintptr, v uint64) { return newUintDecoder(func(p unsafe.Pointer, v uint64) {
**(**uint)(unsafe.Pointer(&p)) = uint(v) *(*uint)(p) = uint(v)
}), nil }), nil
} }
func (d *Decoder) compileUint8() (decoder, error) { func (d *Decoder) compileUint8() (decoder, error) {
return newUintDecoder(func(p uintptr, v uint64) { return newUintDecoder(func(p unsafe.Pointer, v uint64) {
**(**uint8)(unsafe.Pointer(&p)) = uint8(v) *(*uint8)(p) = uint8(v)
}), nil }), nil
} }
func (d *Decoder) compileUint16() (decoder, error) { func (d *Decoder) compileUint16() (decoder, error) {
return newUintDecoder(func(p uintptr, v uint64) { return newUintDecoder(func(p unsafe.Pointer, v uint64) {
**(**uint16)(unsafe.Pointer(&p)) = uint16(v) *(*uint16)(p) = uint16(v)
}), nil }), nil
} }
func (d *Decoder) compileUint32() (decoder, error) { func (d *Decoder) compileUint32() (decoder, error) {
return newUintDecoder(func(p uintptr, v uint64) { return newUintDecoder(func(p unsafe.Pointer, v uint64) {
**(**uint32)(unsafe.Pointer(&p)) = uint32(v) *(*uint32)(p) = uint32(v)
}), nil }), nil
} }
func (d *Decoder) compileUint64() (decoder, error) { func (d *Decoder) compileUint64() (decoder, error) {
return newUintDecoder(func(p uintptr, v uint64) { return newUintDecoder(func(p unsafe.Pointer, v uint64) {
**(**uint64)(unsafe.Pointer(&p)) = v *(*uint64)(p) = v
}), nil }), nil
} }
func (d *Decoder) compileFloat32() (decoder, error) { func (d *Decoder) compileFloat32() (decoder, error) {
return newFloatDecoder(func(p uintptr, v float64) { return newFloatDecoder(func(p unsafe.Pointer, v float64) {
**(**float32)(unsafe.Pointer(&p)) = float32(v) *(*float32)(p) = float32(v)
}), nil }), nil
} }
func (d *Decoder) compileFloat64() (decoder, error) { func (d *Decoder) compileFloat64() (decoder, error) {
return newFloatDecoder(func(p uintptr, v float64) { return newFloatDecoder(func(p unsafe.Pointer, v float64) {
**(**float64)(unsafe.Pointer(&p)) = v *(*float64)(p) = v
}), nil }), nil
} }

View File

@ -6,10 +6,10 @@ import (
) )
type floatDecoder struct { type floatDecoder struct {
op func(uintptr, float64) op func(unsafe.Pointer, float64)
} }
func newFloatDecoder(op func(uintptr, float64)) *floatDecoder { func newFloatDecoder(op func(unsafe.Pointer, float64)) *floatDecoder {
return &floatDecoder{op: op} return &floatDecoder{op: op}
} }
@ -107,7 +107,7 @@ func (d *floatDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, erro
return nil, 0, errUnexpectedEndOfJSON("float", cursor) return nil, 0, errUnexpectedEndOfJSON("float", cursor)
} }
func (d *floatDecoder) decodeStream(s *stream, p uintptr) error { func (d *floatDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
bytes, err := d.decodeStreamByte(s) bytes, err := d.decodeStreamByte(s)
if err != nil { if err != nil {
return err return err
@ -124,7 +124,7 @@ func (d *floatDecoder) decodeStream(s *stream, p uintptr) error {
return nil return nil
} }
func (d *floatDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error) { func (d *floatDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
bytes, c, err := d.decodeByte(buf, cursor) bytes, c, err := d.decodeByte(buf, cursor)
if err != nil { if err != nil {
return 0, err return 0, err

View File

@ -1,10 +1,12 @@
package json package json
import "unsafe"
type intDecoder struct { type intDecoder struct {
op func(uintptr, int64) op func(unsafe.Pointer, int64)
} }
func newIntDecoder(op func(uintptr, int64)) *intDecoder { func newIntDecoder(op func(unsafe.Pointer, int64)) *intDecoder {
return &intDecoder{op: op} return &intDecoder{op: op}
} }
@ -128,7 +130,7 @@ func (d *intDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error)
return nil, 0, errUnexpectedEndOfJSON("number(integer)", cursor) return nil, 0, errUnexpectedEndOfJSON("number(integer)", cursor)
} }
func (d *intDecoder) decodeStream(s *stream, p uintptr) error { func (d *intDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
bytes, err := d.decodeStreamByte(s) bytes, err := d.decodeStreamByte(s)
if err != nil { if err != nil {
return err return err
@ -137,7 +139,7 @@ func (d *intDecoder) decodeStream(s *stream, p uintptr) error {
return nil return nil
} }
func (d *intDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error) { func (d *intDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
bytes, c, err := d.decodeByte(buf, cursor) bytes, c, err := d.decodeByte(buf, cursor)
if err != nil { if err != nil {
return 0, err return 0, err

View File

@ -18,12 +18,12 @@ func newInterfaceDecoder(typ *rtype) *interfaceDecoder {
func (d *interfaceDecoder) numDecoder(s *stream) decoder { func (d *interfaceDecoder) numDecoder(s *stream) decoder {
if s.useNumber { if s.useNumber {
return newNumberDecoder(func(p uintptr, v Number) { return newNumberDecoder(func(p unsafe.Pointer, v Number) {
**(**interface{})(unsafe.Pointer(&p)) = v *(*interface{})(p) = v
}) })
} }
return newFloatDecoder(func(p uintptr, v float64) { return newFloatDecoder(func(p unsafe.Pointer, v float64) {
**(**interface{})(unsafe.Pointer(&p)) = v *(*interface{})(p) = v
}) })
} }
@ -33,7 +33,7 @@ var (
) )
) )
func (d *interfaceDecoder) decodeStream(s *stream, p uintptr) error { func (d *interfaceDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
s.skipWhiteSpace() s.skipWhiteSpace()
for { for {
switch s.char() { switch s.char() {
@ -45,7 +45,7 @@ func (d *interfaceDecoder) decodeStream(s *stream, p uintptr) error {
interfaceMapType, interfaceMapType,
newStringDecoder(), newStringDecoder(),
newInterfaceDecoder(d.typ), newInterfaceDecoder(d.typ),
).decodeStream(s, uintptr(ptr)); err != nil { ).decodeStream(s, ptr); err != nil {
return err return err
} }
**(**interface{})(unsafe.Pointer(&p)) = v **(**interface{})(unsafe.Pointer(&p)) = v
@ -58,7 +58,7 @@ func (d *interfaceDecoder) decodeStream(s *stream, p uintptr) error {
newInterfaceDecoder(d.typ), newInterfaceDecoder(d.typ),
d.typ, d.typ,
d.typ.Size(), d.typ.Size(),
).decodeStream(s, uintptr(ptr)); err != nil { ).decodeStream(s, ptr); err != nil {
return err return err
} }
**(**interface{})(unsafe.Pointer(&p)) = v **(**interface{})(unsafe.Pointer(&p)) = v
@ -116,7 +116,7 @@ func (d *interfaceDecoder) decodeStream(s *stream, p uintptr) error {
return errNotAtBeginningOfValue(s.totalOffset()) return errNotAtBeginningOfValue(s.totalOffset())
} }
func (d *interfaceDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error) { func (d *interfaceDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
cursor = skipWhiteSpace(buf, cursor) cursor = skipWhiteSpace(buf, cursor)
switch buf[cursor] { switch buf[cursor] {
case '{': case '{':
@ -128,7 +128,7 @@ func (d *interfaceDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, e
newStringDecoder(), newStringDecoder(),
newInterfaceDecoder(d.typ), newInterfaceDecoder(d.typ),
) )
cursor, err := dec.decode(buf, cursor, uintptr(ptr)) cursor, err := dec.decode(buf, cursor, ptr)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -143,15 +143,15 @@ func (d *interfaceDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, e
d.typ, d.typ,
d.typ.Size(), d.typ.Size(),
) )
cursor, err := dec.decode(buf, cursor, uintptr(ptr)) cursor, err := dec.decode(buf, cursor, ptr)
if err != nil { if err != nil {
return 0, err return 0, err
} }
**(**interface{})(unsafe.Pointer(&p)) = v **(**interface{})(unsafe.Pointer(&p)) = v
return cursor, nil return cursor, nil
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return newFloatDecoder(func(p uintptr, v float64) { return newFloatDecoder(func(p unsafe.Pointer, v float64) {
**(**interface{})(unsafe.Pointer(&p)) = v *(*interface{})(p) = v
}).decode(buf, cursor, p) }).decode(buf, cursor, p)
case '"': case '"':
cursor++ cursor++

View File

@ -29,28 +29,28 @@ func mapassign(t *rtype, m unsafe.Pointer, key, val unsafe.Pointer)
func (d *mapDecoder) setKey(buf []byte, cursor int64, key interface{}) (int64, error) { func (d *mapDecoder) setKey(buf []byte, cursor int64, key interface{}) (int64, error) {
header := (*interfaceHeader)(unsafe.Pointer(&key)) header := (*interfaceHeader)(unsafe.Pointer(&key))
d.dummy = header d.dummy = header
return d.keyDecoder.decode(buf, cursor, uintptr(header.ptr)) return d.keyDecoder.decode(buf, cursor, header.ptr)
} }
func (d *mapDecoder) setValue(buf []byte, cursor int64, key interface{}) (int64, error) { func (d *mapDecoder) setValue(buf []byte, cursor int64, key interface{}) (int64, error) {
header := (*interfaceHeader)(unsafe.Pointer(&key)) header := (*interfaceHeader)(unsafe.Pointer(&key))
d.dummy = header d.dummy = header
return d.valueDecoder.decode(buf, cursor, uintptr(header.ptr)) return d.valueDecoder.decode(buf, cursor, header.ptr)
} }
func (d *mapDecoder) setKeyStream(s *stream, key interface{}) error { func (d *mapDecoder) setKeyStream(s *stream, key interface{}) error {
header := (*interfaceHeader)(unsafe.Pointer(&key)) header := (*interfaceHeader)(unsafe.Pointer(&key))
d.dummy = header d.dummy = header
return d.keyDecoder.decodeStream(s, uintptr(header.ptr)) return d.keyDecoder.decodeStream(s, header.ptr)
} }
func (d *mapDecoder) setValueStream(s *stream, key interface{}) error { func (d *mapDecoder) setValueStream(s *stream, key interface{}) error {
header := (*interfaceHeader)(unsafe.Pointer(&key)) header := (*interfaceHeader)(unsafe.Pointer(&key))
d.dummy = header d.dummy = header
return d.valueDecoder.decodeStream(s, uintptr(header.ptr)) return d.valueDecoder.decodeStream(s, header.ptr)
} }
func (d *mapDecoder) decodeStream(s *stream, p uintptr) error { func (d *mapDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
s.skipWhiteSpace() s.skipWhiteSpace()
switch s.char() { switch s.char() {
case 'n': case 'n':
@ -107,7 +107,7 @@ func (d *mapDecoder) decodeStream(s *stream, p uintptr) error {
return nil return nil
} }
func (d *mapDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error) { func (d *mapDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
cursor = skipWhiteSpace(buf, cursor) cursor = skipWhiteSpace(buf, cursor)
buflen := int64(len(buf)) buflen := int64(len(buf))
if buflen < 2 { if buflen < 2 {

View File

@ -6,17 +6,17 @@ import (
type numberDecoder struct { type numberDecoder struct {
*floatDecoder *floatDecoder
op func(uintptr, Number) op func(unsafe.Pointer, Number)
} }
func newNumberDecoder(op func(uintptr, Number)) *numberDecoder { func newNumberDecoder(op func(unsafe.Pointer, Number)) *numberDecoder {
return &numberDecoder{ return &numberDecoder{
floatDecoder: newFloatDecoder(nil), floatDecoder: newFloatDecoder(nil),
op: op, op: op,
} }
} }
func (d *numberDecoder) decodeStream(s *stream, p uintptr) error { func (d *numberDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
bytes, err := d.floatDecoder.decodeStreamByte(s) bytes, err := d.floatDecoder.decodeStreamByte(s)
if err != nil { if err != nil {
return err return err
@ -26,7 +26,7 @@ func (d *numberDecoder) decodeStream(s *stream, p uintptr) error {
return nil return nil
} }
func (d *numberDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error) { func (d *numberDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
bytes, c, err := d.floatDecoder.decodeByte(buf, cursor) bytes, c, err := d.floatDecoder.decodeByte(buf, cursor)
if err != nil { if err != nil {
return 0, err return 0, err

View File

@ -14,24 +14,24 @@ func newPtrDecoder(dec decoder, typ *rtype) *ptrDecoder {
} }
//go:linkname unsafe_New reflect.unsafe_New //go:linkname unsafe_New reflect.unsafe_New
func unsafe_New(*rtype) uintptr func unsafe_New(*rtype) unsafe.Pointer
func (d *ptrDecoder) decodeStream(s *stream, p uintptr) error { func (d *ptrDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
newptr := unsafe_New(d.typ) newptr := unsafe_New(d.typ)
*(*unsafe.Pointer)(p) = newptr
if err := d.dec.decodeStream(s, newptr); err != nil { if err := d.dec.decodeStream(s, newptr); err != nil {
return err return err
} }
**(**uintptr)(unsafe.Pointer(&p)) = newptr
return nil return nil
} }
func (d *ptrDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error) { func (d *ptrDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
newptr := unsafe_New(d.typ) newptr := unsafe_New(d.typ)
*(*unsafe.Pointer)(p) = newptr
c, err := d.dec.decode(buf, cursor, newptr) c, err := d.dec.decode(buf, cursor, newptr)
if err != nil { if err != nil {
return 0, err return 0, err
} }
cursor = c cursor = c
**(**uintptr)(unsafe.Pointer(&p)) = newptr
return cursor, nil return cursor, nil
} }

View File

@ -55,7 +55,7 @@ func copySlice(elemType *rtype, dst, src sliceHeader) int
//go:linkname newArray reflect.unsafe_NewArray //go:linkname newArray reflect.unsafe_NewArray
func newArray(*rtype, int) unsafe.Pointer func newArray(*rtype, int) unsafe.Pointer
func (d *sliceDecoder) decodeStream(s *stream, p uintptr) error { func (d *sliceDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
for { for {
switch s.char() { switch s.char() {
case ' ', '\n', '\t', '\r': case ' ', '\n', '\t', '\r':
@ -90,7 +90,8 @@ func (d *sliceDecoder) decodeStream(s *stream, p uintptr) error {
dst := sliceHeader{data: data, len: idx, cap: cap} dst := sliceHeader{data: data, len: idx, cap: cap}
copySlice(d.elemType, dst, src) copySlice(d.elemType, dst, src)
} }
if err := d.valueDecoder.decodeStream(s, uintptr(data)+uintptr(idx)*d.size); err != nil { addr := uintptr(data) + uintptr(idx)*d.size
if err := d.valueDecoder.decodeStream(s, unsafe.Pointer(addr)); err != nil {
return err return err
} }
s.skipWhiteSpace() s.skipWhiteSpace()
@ -144,7 +145,7 @@ ERROR:
return errUnexpectedEndOfJSON("slice", s.totalOffset()) return errUnexpectedEndOfJSON("slice", s.totalOffset())
} }
func (d *sliceDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error) { func (d *sliceDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
buflen := int64(len(buf)) buflen := int64(len(buf))
for ; cursor < buflen; cursor++ { for ; cursor < buflen; cursor++ {
switch buf[cursor] { switch buf[cursor] {
@ -190,7 +191,8 @@ func (d *sliceDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error
dst := sliceHeader{data: data, len: idx, cap: cap} dst := sliceHeader{data: data, len: idx, cap: cap}
copySlice(d.elemType, dst, src) copySlice(d.elemType, dst, src)
} }
c, err := d.valueDecoder.decode(buf, cursor, uintptr(data)+uintptr(idx)*d.size) addr := uintptr(data) + uintptr(idx)*d.size
c, err := d.valueDecoder.decode(buf, cursor, unsafe.Pointer(addr))
if err != nil { if err != nil {
return 0, err return 0, err
} }

View File

@ -11,7 +11,7 @@ func newStringDecoder() *stringDecoder {
return &stringDecoder{} return &stringDecoder{}
} }
func (d *stringDecoder) decodeStream(s *stream, p uintptr) error { func (d *stringDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
bytes, err := d.decodeStreamByte(s) bytes, err := d.decodeStreamByte(s)
if err != nil { if err != nil {
return err return err
@ -20,7 +20,7 @@ func (d *stringDecoder) decodeStream(s *stream, p uintptr) error {
return nil return nil
} }
func (d *stringDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error) { func (d *stringDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
bytes, c, err := d.decodeByte(buf, cursor) bytes, c, err := d.decodeByte(buf, cursor)
if err != nil { if err != nil {
return 0, err return 0, err

View File

@ -22,7 +22,7 @@ func newStructDecoder(fieldMap map[string]*structFieldSet) *structDecoder {
} }
} }
func (d *structDecoder) decodeStream(s *stream, p uintptr) error { func (d *structDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
s.skipWhiteSpace() s.skipWhiteSpace()
if s.char() == nul { if s.char() == nul {
s.read() s.read()
@ -54,7 +54,8 @@ func (d *structDecoder) decodeStream(s *stream, p uintptr) error {
k := *(*string)(unsafe.Pointer(&key)) k := *(*string)(unsafe.Pointer(&key))
field, exists := d.fieldMap[k] field, exists := d.fieldMap[k]
if exists { if exists {
if err := field.dec.decodeStream(s, p+field.offset); err != nil { addr := uintptr(p) + field.offset
if err := field.dec.decodeStream(s, unsafe.Pointer(addr)); err != nil {
return err return err
} }
} else if s.disallowUnknownFields { } else if s.disallowUnknownFields {
@ -81,7 +82,7 @@ func (d *structDecoder) decodeStream(s *stream, p uintptr) error {
return nil return nil
} }
func (d *structDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error) { func (d *structDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
buflen := int64(len(buf)) buflen := int64(len(buf))
cursor = skipWhiteSpace(buf, cursor) cursor = skipWhiteSpace(buf, cursor)
if buf[cursor] != '{' { if buf[cursor] != '{' {
@ -108,7 +109,8 @@ func (d *structDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, erro
k := *(*string)(unsafe.Pointer(&key)) k := *(*string)(unsafe.Pointer(&key))
field, exists := d.fieldMap[k] field, exists := d.fieldMap[k]
if exists { if exists {
c, err := field.dec.decode(buf, cursor, p+field.offset) addr := uintptr(p) + field.offset
c, err := field.dec.decode(buf, cursor, unsafe.Pointer(addr))
if err != nil { if err != nil {
return 0, err return 0, err
} }

View File

@ -1,10 +1,12 @@
package json package json
import "unsafe"
type uintDecoder struct { type uintDecoder struct {
op func(uintptr, uint64) op func(unsafe.Pointer, uint64)
} }
func newUintDecoder(op func(uintptr, uint64)) *uintDecoder { func newUintDecoder(op func(unsafe.Pointer, uint64)) *uintDecoder {
return &uintDecoder{op: op} return &uintDecoder{op: op}
} }
@ -81,7 +83,7 @@ func (d *uintDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error
return nil, 0, errUnexpectedEndOfJSON("number(unsigned integer)", cursor) return nil, 0, errUnexpectedEndOfJSON("number(unsigned integer)", cursor)
} }
func (d *uintDecoder) decodeStream(s *stream, p uintptr) error { func (d *uintDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
bytes, err := d.decodeStreamByte(s) bytes, err := d.decodeStreamByte(s)
if err != nil { if err != nil {
return err return err
@ -90,7 +92,7 @@ func (d *uintDecoder) decodeStream(s *stream, p uintptr) error {
return nil return nil
} }
func (d *uintDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error) { func (d *uintDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
bytes, c, err := d.decodeByte(buf, cursor) bytes, c, err := d.decodeByte(buf, cursor)
if err != nil { if err != nil {
return 0, err return 0, err

View File

@ -12,7 +12,7 @@ func newUnmarshalJSONDecoder(typ *rtype) *unmarshalJSONDecoder {
return &unmarshalJSONDecoder{typ: typ} return &unmarshalJSONDecoder{typ: typ}
} }
func (d *unmarshalJSONDecoder) decodeStream(s *stream, p uintptr) error { func (d *unmarshalJSONDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
s.skipWhiteSpace() s.skipWhiteSpace()
start := s.cursor start := s.cursor
if err := s.skipValue(); err != nil { if err := s.skipValue(); err != nil {
@ -29,7 +29,7 @@ func (d *unmarshalJSONDecoder) decodeStream(s *stream, p uintptr) error {
return nil return nil
} }
func (d *unmarshalJSONDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error) { func (d *unmarshalJSONDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
cursor = skipWhiteSpace(buf, cursor) cursor = skipWhiteSpace(buf, cursor)
start := cursor start := cursor
end, err := skipValue(buf, cursor) end, err := skipValue(buf, cursor)

View File

@ -16,7 +16,7 @@ func newUnmarshalTextDecoder(typ *rtype) *unmarshalTextDecoder {
return &unmarshalTextDecoder{typ: typ} return &unmarshalTextDecoder{typ: typ}
} }
func (d *unmarshalTextDecoder) decodeStream(s *stream, p uintptr) error { func (d *unmarshalTextDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
s.skipWhiteSpace() s.skipWhiteSpace()
start := s.cursor start := s.cursor
if err := s.skipValue(); err != nil { if err := s.skipValue(); err != nil {
@ -36,7 +36,7 @@ func (d *unmarshalTextDecoder) decodeStream(s *stream, p uintptr) error {
return nil return nil
} }
func (d *unmarshalTextDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error) { func (d *unmarshalTextDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
cursor = skipWhiteSpace(buf, cursor) cursor = skipWhiteSpace(buf, cursor)
start := cursor start := cursor
end, err := skipValue(buf, cursor) end, err := skipValue(buf, cursor)

View File

@ -1,5 +1,7 @@
package json package json
import "unsafe"
type wrappedStringDecoder struct { type wrappedStringDecoder struct {
dec decoder dec decoder
stringDecoder *stringDecoder stringDecoder *stringDecoder
@ -12,7 +14,7 @@ func newWrappedStringDecoder(dec decoder) *wrappedStringDecoder {
} }
} }
func (d *wrappedStringDecoder) decodeStream(s *stream, p uintptr) error { func (d *wrappedStringDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
bytes, err := d.stringDecoder.decodeStreamByte(s) bytes, err := d.stringDecoder.decodeStreamByte(s)
if err != nil { if err != nil {
return err return err
@ -39,7 +41,7 @@ func (d *wrappedStringDecoder) decodeStream(s *stream, p uintptr) error {
return nil return nil
} }
func (d *wrappedStringDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error) { func (d *wrappedStringDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
bytes, c, err := d.stringDecoder.decodeByte(buf, cursor) bytes, c, err := d.stringDecoder.decodeByte(buf, cursor)
if err != nil { if err != nil {
return 0, err return 0, err