2009-06-29 15:15:07 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
2014-10-17 09:00:07 -07:00
|
|
|
//go:generate go run encgen.go -output enc_helpers.go
|
|
|
|
|
|
2009-06-29 15:15:07 -07:00
|
|
|
package gob
|
|
|
|
|
|
|
|
|
|
import (
|
2013-08-14 00:18:48 -04:00
|
|
|
"encoding"
|
2017-03-17 18:42:58 +01:00
|
|
|
"encoding/binary"
|
2009-12-15 15:35:38 -08:00
|
|
|
"math"
|
2017-03-17 18:42:58 +01:00
|
|
|
"math/bits"
|
2009-12-15 15:35:38 -08:00
|
|
|
"reflect"
|
2015-08-15 13:04:46 +03:00
|
|
|
"sync"
|
2009-06-29 15:15:07 -07:00
|
|
|
)
|
|
|
|
|
|
2014-06-30 11:06:47 -07:00
|
|
|
const uint64Size = 8
|
2009-07-28 17:20:19 -07:00
|
|
|
|
2014-10-17 09:00:07 -07:00
|
|
|
type encHelper func(state *encoderState, v reflect.Value) bool
|
|
|
|
|
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
// encoderState is the global execution state of an instance of the encoder.
|
2009-07-01 18:25:13 -07:00
|
|
|
// Field numbers are delta encoded and always increase. The field
|
|
|
|
|
// number is initialized to -1 so 0 comes out as delta(1). A delta of
|
|
|
|
|
// 0 terminates the structure.
|
2009-07-15 16:10:17 -07:00
|
|
|
type encoderState struct {
|
2010-10-31 13:41:30 -07:00
|
|
|
enc *Encoder
|
2014-10-17 20:51:15 -07:00
|
|
|
b *encBuffer
|
2010-06-28 14:09:47 -07:00
|
|
|
sendZero bool // encoding an array element or map key/value pair; send zero values
|
2009-12-15 15:35:38 -08:00
|
|
|
fieldnum int // the last field number written.
|
|
|
|
|
buf [1 + uint64Size]byte // buffer used by the encoder; here to avoid allocation.
|
2011-03-15 21:30:07 -07:00
|
|
|
next *encoderState // for free list
|
2009-06-30 16:20:31 -07:00
|
|
|
}
|
|
|
|
|
|
2014-10-17 20:51:15 -07:00
|
|
|
// encBuffer is an extremely simple, fast implementation of a write-only byte buffer.
|
|
|
|
|
// It never returns a non-nil error, but Write returns an error value so it matches io.Writer.
|
|
|
|
|
type encBuffer struct {
|
|
|
|
|
data []byte
|
|
|
|
|
scratch [64]byte
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-15 13:04:46 +03:00
|
|
|
var encBufferPool = sync.Pool{
|
2021-12-01 12:15:45 -05:00
|
|
|
New: func() any {
|
2015-08-15 13:04:46 +03:00
|
|
|
e := new(encBuffer)
|
|
|
|
|
e.data = e.scratch[0:0]
|
|
|
|
|
return e
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 18:50:52 -04:00
|
|
|
func (e *encBuffer) writeByte(c byte) {
|
2014-10-17 20:51:15 -07:00
|
|
|
e.data = append(e.data, c)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *encBuffer) Write(p []byte) (int, error) {
|
|
|
|
|
e.data = append(e.data, p...)
|
|
|
|
|
return len(p), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *encBuffer) WriteString(s string) {
|
|
|
|
|
e.data = append(e.data, s...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *encBuffer) Len() int {
|
|
|
|
|
return len(e.data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *encBuffer) Bytes() []byte {
|
|
|
|
|
return e.data
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *encBuffer) Reset() {
|
2015-08-15 13:04:46 +03:00
|
|
|
if len(e.data) >= tooBig {
|
|
|
|
|
e.data = e.scratch[0:0]
|
|
|
|
|
} else {
|
|
|
|
|
e.data = e.data[0:0]
|
|
|
|
|
}
|
2014-10-17 20:51:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (enc *Encoder) newEncoderState(b *encBuffer) *encoderState {
|
2011-03-15 21:30:07 -07:00
|
|
|
e := enc.freeList
|
|
|
|
|
if e == nil {
|
|
|
|
|
e = new(encoderState)
|
|
|
|
|
e.enc = enc
|
|
|
|
|
} else {
|
|
|
|
|
enc.freeList = e.next
|
|
|
|
|
}
|
|
|
|
|
e.sendZero = false
|
|
|
|
|
e.fieldnum = 0
|
|
|
|
|
e.b = b
|
2014-10-17 20:51:15 -07:00
|
|
|
if len(b.data) == 0 {
|
|
|
|
|
b.data = b.scratch[0:0]
|
|
|
|
|
}
|
2011-03-15 21:30:07 -07:00
|
|
|
return e
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (enc *Encoder) freeEncoderState(e *encoderState) {
|
|
|
|
|
e.next = enc.freeList
|
|
|
|
|
enc.freeList = e
|
2010-10-22 11:17:40 -07:00
|
|
|
}
|
|
|
|
|
|
2016-03-01 23:21:55 +00:00
|
|
|
// Unsigned integers have a two-state encoding. If the number is less
|
2009-07-28 17:20:19 -07:00
|
|
|
// than 128 (0 through 0x7F), its value is written directly.
|
|
|
|
|
// Otherwise the value is written in big-endian byte order preceded
|
|
|
|
|
// by the byte length, negated.
|
2009-06-29 15:15:07 -07:00
|
|
|
|
2010-10-22 16:07:26 -07:00
|
|
|
// encodeUint writes an encoded unsigned integer to state.b.
|
2011-01-11 17:56:45 -08:00
|
|
|
func (state *encoderState) encodeUint(x uint64) {
|
2009-07-28 17:20:19 -07:00
|
|
|
if x <= 0x7F {
|
2019-05-08 18:50:52 -04:00
|
|
|
state.b.writeByte(uint8(x))
|
2009-12-15 15:35:38 -08:00
|
|
|
return
|
2009-07-28 17:20:19 -07:00
|
|
|
}
|
2017-03-17 18:42:58 +01:00
|
|
|
|
|
|
|
|
binary.BigEndian.PutUint64(state.buf[1:], x)
|
|
|
|
|
bc := bits.LeadingZeros64(x) >> 3 // 8 - bytelen(x)
|
|
|
|
|
state.buf[bc] = uint8(bc - uint64Size) // and then we subtract 8 to get -bytelen(x)
|
|
|
|
|
|
|
|
|
|
state.b.Write(state.buf[bc : uint64Size+1])
|
2009-06-29 15:15:07 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-15 16:10:17 -07:00
|
|
|
// encodeInt writes an encoded signed integer to state.w.
|
2010-10-22 16:07:26 -07:00
|
|
|
// The low bit of the encoding says whether to bit complement the (other bits of the)
|
|
|
|
|
// uint to recover the int.
|
2011-01-11 17:56:45 -08:00
|
|
|
func (state *encoderState) encodeInt(i int64) {
|
2009-12-15 15:35:38 -08:00
|
|
|
var x uint64
|
2009-06-29 15:15:07 -07:00
|
|
|
if i < 0 {
|
2009-11-09 21:13:17 -08:00
|
|
|
x = uint64(^i<<1) | 1
|
2009-06-29 15:15:07 -07:00
|
|
|
} else {
|
2009-11-09 21:13:17 -08:00
|
|
|
x = uint64(i << 1)
|
2009-06-29 15:15:07 -07:00
|
|
|
}
|
2016-04-14 19:09:36 -07:00
|
|
|
state.encodeUint(x)
|
2009-06-30 15:37:46 -07:00
|
|
|
}
|
|
|
|
|
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
// encOp is the signature of an encoding operator for a given type.
|
2014-06-30 11:06:47 -07:00
|
|
|
type encOp func(i *encInstr, state *encoderState, v reflect.Value)
|
2009-07-01 23:04:27 -07:00
|
|
|
|
2009-06-30 15:37:46 -07:00
|
|
|
// The 'instructions' of the encoding machine
|
|
|
|
|
type encInstr struct {
|
2014-06-30 11:06:47 -07:00
|
|
|
op encOp
|
|
|
|
|
field int // field number in input
|
|
|
|
|
index []int // struct index
|
|
|
|
|
indir int // how many pointer indirections to reach the value in the struct
|
2009-06-30 15:37:46 -07:00
|
|
|
}
|
|
|
|
|
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
// update emits a field number and updates the state to record its value for delta encoding.
|
|
|
|
|
// If the instruction pointer is nil, it does nothing
|
2009-07-15 16:10:17 -07:00
|
|
|
func (state *encoderState) update(instr *encInstr) {
|
2009-07-02 16:43:46 -07:00
|
|
|
if instr != nil {
|
2011-01-11 17:56:45 -08:00
|
|
|
state.encodeUint(uint64(instr.field - state.fieldnum))
|
2009-12-15 15:35:38 -08:00
|
|
|
state.fieldnum = instr.field
|
2009-07-02 16:43:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-08 00:02:07 -08:00
|
|
|
// Each encoder for a composite is responsible for handling any
|
|
|
|
|
// indirections associated with the elements of the data structure.
|
2016-03-01 23:21:55 +00:00
|
|
|
// If any pointer so reached is nil, no bytes are written. If the
|
|
|
|
|
// data item is zero, no bytes are written. Single values - ints,
|
2011-03-08 00:02:07 -08:00
|
|
|
// strings etc. - are indirected before calling their encoders.
|
|
|
|
|
// Otherwise, the output (for a scalar) is the field number, as an
|
|
|
|
|
// encoded integer, followed by the field data in its appropriate
|
|
|
|
|
// format.
|
2009-06-30 16:20:31 -07:00
|
|
|
|
2014-06-30 11:06:47 -07:00
|
|
|
// encIndirect dereferences pv indir times and returns the result.
|
|
|
|
|
func encIndirect(pv reflect.Value, indir int) reflect.Value {
|
2009-06-30 17:59:41 -07:00
|
|
|
for ; indir > 0; indir-- {
|
2014-06-30 11:06:47 -07:00
|
|
|
if pv.IsNil() {
|
|
|
|
|
break
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
2014-06-30 11:06:47 -07:00
|
|
|
pv = pv.Elem()
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
2014-06-30 11:06:47 -07:00
|
|
|
return pv
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
|
|
|
|
|
2014-06-30 11:06:47 -07:00
|
|
|
// encBool encodes the bool referenced by v as an unsigned 0 or 1.
|
|
|
|
|
func encBool(i *encInstr, state *encoderState, v reflect.Value) {
|
|
|
|
|
b := v.Bool()
|
2010-06-28 14:09:47 -07:00
|
|
|
if b || state.sendZero {
|
2009-12-15 15:35:38 -08:00
|
|
|
state.update(i)
|
2009-12-29 07:41:53 +11:00
|
|
|
if b {
|
2011-01-11 17:56:45 -08:00
|
|
|
state.encodeUint(1)
|
2009-12-29 07:41:53 +11:00
|
|
|
} else {
|
2011-01-11 17:56:45 -08:00
|
|
|
state.encodeUint(0)
|
2009-12-29 07:41:53 +11:00
|
|
|
}
|
2009-06-30 15:37:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-30 11:06:47 -07:00
|
|
|
// encInt encodes the signed integer (int int8 int16 int32 int64) referenced by v.
|
|
|
|
|
func encInt(i *encInstr, state *encoderState, v reflect.Value) {
|
|
|
|
|
value := v.Int()
|
|
|
|
|
if value != 0 || state.sendZero {
|
2009-12-15 15:35:38 -08:00
|
|
|
state.update(i)
|
2014-06-30 11:06:47 -07:00
|
|
|
state.encodeInt(value)
|
2009-06-30 15:37:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-30 11:06:47 -07:00
|
|
|
// encUint encodes the unsigned integer (uint uint8 uint16 uint32 uint64 uintptr) referenced by v.
|
|
|
|
|
func encUint(i *encInstr, state *encoderState, v reflect.Value) {
|
|
|
|
|
value := v.Uint()
|
|
|
|
|
if value != 0 || state.sendZero {
|
2009-12-15 15:35:38 -08:00
|
|
|
state.update(i)
|
2014-06-30 11:06:47 -07:00
|
|
|
state.encodeUint(value)
|
2009-07-09 14:33:43 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
// floatBits returns a uint64 holding the bits of a floating-point number.
|
2009-06-30 15:37:46 -07:00
|
|
|
// Floating-point numbers are transmitted as uint64s holding the bits
|
2016-03-01 23:21:55 +00:00
|
|
|
// of the underlying representation. They are sent byte-reversed, with
|
2009-06-30 15:37:46 -07:00
|
|
|
// the exponent end coming out first, so integer floating point numbers
|
2016-03-01 23:21:55 +00:00
|
|
|
// (for example) transmit more compactly. This routine does the
|
2009-06-30 15:37:46 -07:00
|
|
|
// swizzling.
|
|
|
|
|
func floatBits(f float64) uint64 {
|
2009-12-15 15:35:38 -08:00
|
|
|
u := math.Float64bits(f)
|
2017-03-18 18:43:20 +01:00
|
|
|
return bits.ReverseBytes64(u)
|
2009-06-30 15:37:46 -07:00
|
|
|
}
|
|
|
|
|
|
2014-06-30 11:06:47 -07:00
|
|
|
// encFloat encodes the floating point value (float32 float64) referenced by v.
|
|
|
|
|
func encFloat(i *encInstr, state *encoderState, v reflect.Value) {
|
|
|
|
|
f := v.Float()
|
2010-06-28 14:09:47 -07:00
|
|
|
if f != 0 || state.sendZero {
|
2014-06-30 11:06:47 -07:00
|
|
|
bits := floatBits(f)
|
2009-12-15 15:35:38 -08:00
|
|
|
state.update(i)
|
2014-06-30 11:06:47 -07:00
|
|
|
state.encodeUint(bits)
|
2009-06-30 15:37:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
2009-07-01 18:25:13 -07:00
|
|
|
|
2014-06-30 11:06:47 -07:00
|
|
|
// encComplex encodes the complex value (complex64 complex128) referenced by v.
|
2010-06-24 15:07:28 -07:00
|
|
|
// Complex numbers are just a pair of floating-point numbers, real part first.
|
2014-06-30 11:06:47 -07:00
|
|
|
func encComplex(i *encInstr, state *encoderState, v reflect.Value) {
|
|
|
|
|
c := v.Complex()
|
2010-06-28 14:09:47 -07:00
|
|
|
if c != 0+0i || state.sendZero {
|
2010-06-24 15:07:28 -07:00
|
|
|
rpart := floatBits(real(c))
|
|
|
|
|
ipart := floatBits(imag(c))
|
|
|
|
|
state.update(i)
|
2011-01-11 17:56:45 -08:00
|
|
|
state.encodeUint(rpart)
|
|
|
|
|
state.encodeUint(ipart)
|
2010-06-24 15:07:28 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-30 11:06:47 -07:00
|
|
|
// encUint8Array encodes the byte array referenced by v.
|
2009-07-02 11:21:01 -07:00
|
|
|
// Byte arrays are encoded as an unsigned count followed by the raw bytes.
|
2014-06-30 11:06:47 -07:00
|
|
|
func encUint8Array(i *encInstr, state *encoderState, v reflect.Value) {
|
|
|
|
|
b := v.Bytes()
|
2010-06-28 14:09:47 -07:00
|
|
|
if len(b) > 0 || state.sendZero {
|
2009-12-15 15:35:38 -08:00
|
|
|
state.update(i)
|
2011-01-11 17:56:45 -08:00
|
|
|
state.encodeUint(uint64(len(b)))
|
2009-12-15 15:35:38 -08:00
|
|
|
state.b.Write(b)
|
2009-07-02 11:21:01 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-30 11:06:47 -07:00
|
|
|
// encString encodes the string referenced by v.
|
2009-07-02 11:21:01 -07:00
|
|
|
// Strings are encoded as an unsigned count followed by the raw bytes.
|
2014-06-30 11:06:47 -07:00
|
|
|
func encString(i *encInstr, state *encoderState, v reflect.Value) {
|
|
|
|
|
s := v.String()
|
2010-06-28 14:09:47 -07:00
|
|
|
if len(s) > 0 || state.sendZero {
|
2009-12-15 15:35:38 -08:00
|
|
|
state.update(i)
|
2011-01-11 17:56:45 -08:00
|
|
|
state.encodeUint(uint64(len(s)))
|
2011-03-16 18:03:13 -07:00
|
|
|
state.b.WriteString(s)
|
2009-07-02 11:21:01 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
// encStructTerminator encodes the end of an encoded struct
|
|
|
|
|
// as delta field number of 0.
|
2014-06-30 11:06:47 -07:00
|
|
|
func encStructTerminator(i *encInstr, state *encoderState, v reflect.Value) {
|
2011-01-11 17:56:45 -08:00
|
|
|
state.encodeUint(0)
|
2009-07-01 18:25:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Execution engine
|
|
|
|
|
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
// encEngine an array of instructions indexed by field number of the encoding
|
2016-03-01 23:21:55 +00:00
|
|
|
// data, typically a struct. It is executed top to bottom, walking the struct.
|
2009-07-01 18:25:13 -07:00
|
|
|
type encEngine struct {
|
2009-12-15 15:35:38 -08:00
|
|
|
instr []encInstr
|
2009-07-01 18:25:13 -07:00
|
|
|
}
|
|
|
|
|
|
2010-06-28 14:09:47 -07:00
|
|
|
const singletonField = 0
|
|
|
|
|
|
2014-06-30 11:06:47 -07:00
|
|
|
// valid reports whether the value is valid and a non-nil pointer.
|
|
|
|
|
// (Slices, maps, and chans take care of themselves.)
|
|
|
|
|
func valid(v reflect.Value) bool {
|
|
|
|
|
switch v.Kind() {
|
|
|
|
|
case reflect.Invalid:
|
|
|
|
|
return false
|
2021-10-25 23:00:56 +07:00
|
|
|
case reflect.Pointer:
|
2014-06-30 11:06:47 -07:00
|
|
|
return !v.IsNil()
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
// encodeSingle encodes a single top-level non-struct value.
|
2014-10-17 20:51:15 -07:00
|
|
|
func (enc *Encoder) encodeSingle(b *encBuffer, engine *encEngine, value reflect.Value) {
|
2011-03-15 21:30:07 -07:00
|
|
|
state := enc.newEncoderState(b)
|
2014-06-30 11:06:47 -07:00
|
|
|
defer enc.freeEncoderState(state)
|
2010-06-28 14:09:47 -07:00
|
|
|
state.fieldnum = singletonField
|
|
|
|
|
// There is no surrounding struct to frame the transmission, so we must
|
2016-03-01 23:21:55 +00:00
|
|
|
// generate data even if the item is zero. To do this, set sendZero.
|
2010-06-28 14:09:47 -07:00
|
|
|
state.sendZero = true
|
|
|
|
|
instr := &engine.instr[singletonField]
|
|
|
|
|
if instr.indir > 0 {
|
2014-06-30 11:06:47 -07:00
|
|
|
value = encIndirect(value, instr.indir)
|
|
|
|
|
}
|
|
|
|
|
if valid(value) {
|
|
|
|
|
instr.op(instr, state, value)
|
2010-06-28 14:09:47 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
// encodeStruct encodes a single struct value.
|
2014-10-17 20:51:15 -07:00
|
|
|
func (enc *Encoder) encodeStruct(b *encBuffer, engine *encEngine, value reflect.Value) {
|
2014-06-30 11:06:47 -07:00
|
|
|
if !valid(value) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2011-03-15 21:30:07 -07:00
|
|
|
state := enc.newEncoderState(b)
|
2014-06-30 11:06:47 -07:00
|
|
|
defer enc.freeEncoderState(state)
|
2009-12-15 15:35:38 -08:00
|
|
|
state.fieldnum = -1
|
2009-07-02 13:43:47 -07:00
|
|
|
for i := 0; i < len(engine.instr); i++ {
|
2009-12-15 15:35:38 -08:00
|
|
|
instr := &engine.instr[i]
|
2014-06-30 11:06:47 -07:00
|
|
|
if i >= value.NumField() {
|
|
|
|
|
// encStructTerminator
|
|
|
|
|
instr.op(instr, state, reflect.Value{})
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
field := value.FieldByIndex(instr.index)
|
2009-07-02 13:43:47 -07:00
|
|
|
if instr.indir > 0 {
|
2014-06-30 11:06:47 -07:00
|
|
|
field = encIndirect(field, instr.indir)
|
2014-10-14 20:03:35 -07:00
|
|
|
// TODO: Is field guaranteed valid? If so we could avoid this check.
|
|
|
|
|
if !valid(field) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2009-07-02 13:43:47 -07:00
|
|
|
}
|
2014-06-30 11:06:47 -07:00
|
|
|
instr.op(instr, state, field)
|
2009-07-02 13:43:47 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-14 20:03:35 -07:00
|
|
|
// encodeArray encodes an array.
|
2014-10-17 20:51:15 -07:00
|
|
|
func (enc *Encoder) encodeArray(b *encBuffer, value reflect.Value, op encOp, elemIndir int, length int, helper encHelper) {
|
2011-03-15 21:30:07 -07:00
|
|
|
state := enc.newEncoderState(b)
|
2014-06-30 11:06:47 -07:00
|
|
|
defer enc.freeEncoderState(state)
|
2009-12-15 15:35:38 -08:00
|
|
|
state.fieldnum = -1
|
2010-06-28 14:09:47 -07:00
|
|
|
state.sendZero = true
|
2011-01-11 17:56:45 -08:00
|
|
|
state.encodeUint(uint64(length))
|
2014-10-17 09:00:07 -07:00
|
|
|
if helper != nil && helper(state, value) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2010-10-22 15:16:34 -07:00
|
|
|
for i := 0; i < length; i++ {
|
2014-06-30 11:06:47 -07:00
|
|
|
elem := value.Index(i)
|
2009-07-02 17:21:48 -07:00
|
|
|
if elemIndir > 0 {
|
2014-06-30 11:06:47 -07:00
|
|
|
elem = encIndirect(elem, elemIndir)
|
2014-10-14 20:03:35 -07:00
|
|
|
// TODO: Is elem guaranteed valid? If so we could avoid this check.
|
2014-06-30 11:06:47 -07:00
|
|
|
if !valid(elem) {
|
2011-04-20 14:22:52 -07:00
|
|
|
errorf("encodeArray: nil element")
|
2009-07-02 17:21:48 -07:00
|
|
|
}
|
|
|
|
|
}
|
2014-06-30 11:06:47 -07:00
|
|
|
op(nil, state, elem)
|
2009-07-02 16:43:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
// encodeReflectValue is a helper for maps. It encodes the value v.
|
2010-05-05 16:46:39 -07:00
|
|
|
func encodeReflectValue(state *encoderState, v reflect.Value, op encOp, indir int) {
|
2011-04-08 12:27:58 -04:00
|
|
|
for i := 0; i < indir && v.IsValid(); i++ {
|
2010-05-05 16:46:39 -07:00
|
|
|
v = reflect.Indirect(v)
|
|
|
|
|
}
|
2011-04-08 12:27:58 -04:00
|
|
|
if !v.IsValid() {
|
2011-04-20 14:22:52 -07:00
|
|
|
errorf("encodeReflectValue: nil element")
|
2010-05-05 16:46:39 -07:00
|
|
|
}
|
2014-06-30 11:06:47 -07:00
|
|
|
op(nil, state, v)
|
2010-05-05 16:46:39 -07:00
|
|
|
}
|
|
|
|
|
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
// encodeMap encodes a map as unsigned count followed by key:value pairs.
|
2014-10-17 20:51:15 -07:00
|
|
|
func (enc *Encoder) encodeMap(b *encBuffer, mv reflect.Value, keyOp, elemOp encOp, keyIndir, elemIndir int) {
|
2011-03-15 21:30:07 -07:00
|
|
|
state := enc.newEncoderState(b)
|
2010-05-05 16:46:39 -07:00
|
|
|
state.fieldnum = -1
|
2010-06-28 14:09:47 -07:00
|
|
|
state.sendZero = true
|
2021-08-04 02:47:57 +00:00
|
|
|
state.encodeUint(uint64(mv.Len()))
|
|
|
|
|
mi := mv.MapRange()
|
|
|
|
|
for mi.Next() {
|
|
|
|
|
encodeReflectValue(state, mi.Key(), keyOp, keyIndir)
|
|
|
|
|
encodeReflectValue(state, mi.Value(), elemOp, elemIndir)
|
2010-05-05 16:46:39 -07:00
|
|
|
}
|
2011-03-15 21:30:07 -07:00
|
|
|
enc.freeEncoderState(state)
|
2010-05-05 16:46:39 -07:00
|
|
|
}
|
|
|
|
|
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
// encodeInterface encodes the interface value iv.
|
2010-10-22 11:17:40 -07:00
|
|
|
// To send an interface, we send a string identifying the concrete type, followed
|
|
|
|
|
// by the type identifier (which might require defining that type right now), followed
|
2016-03-01 23:21:55 +00:00
|
|
|
// by the concrete value. A nil value gets sent as the empty string for the name,
|
2010-10-22 11:17:40 -07:00
|
|
|
// followed by no value.
|
2014-10-17 20:51:15 -07:00
|
|
|
func (enc *Encoder) encodeInterface(b *encBuffer, iv reflect.Value) {
|
2012-06-12 00:36:39 -04:00
|
|
|
// Gobs can encode nil interface values but not typed interface
|
|
|
|
|
// values holding nil pointers, since nil pointers point to no value.
|
|
|
|
|
elem := iv.Elem()
|
2021-10-25 23:00:56 +07:00
|
|
|
if elem.Kind() == reflect.Pointer && elem.IsNil() {
|
2012-06-12 00:36:39 -04:00
|
|
|
errorf("gob: cannot encode nil pointer of type %s inside interface", iv.Elem().Type())
|
|
|
|
|
}
|
2011-03-15 21:30:07 -07:00
|
|
|
state := enc.newEncoderState(b)
|
2010-10-22 11:17:40 -07:00
|
|
|
state.fieldnum = -1
|
|
|
|
|
state.sendZero = true
|
|
|
|
|
if iv.IsNil() {
|
2011-01-11 17:56:45 -08:00
|
|
|
state.encodeUint(0)
|
2010-10-22 15:16:34 -07:00
|
|
|
return
|
2010-10-22 11:17:40 -07:00
|
|
|
}
|
|
|
|
|
|
2011-02-22 12:31:57 -08:00
|
|
|
ut := userType(iv.Elem().Type())
|
2017-02-16 17:37:30 -05:00
|
|
|
namei, ok := concreteTypeToName.Load(ut.base)
|
2010-10-22 11:17:40 -07:00
|
|
|
if !ok {
|
2011-04-20 14:22:52 -07:00
|
|
|
errorf("type not registered for interface: %s", ut.base)
|
2010-10-22 11:17:40 -07:00
|
|
|
}
|
2017-02-16 17:37:30 -05:00
|
|
|
name := namei.(string)
|
|
|
|
|
|
2010-10-22 11:17:40 -07:00
|
|
|
// Send the name.
|
2011-01-11 17:56:45 -08:00
|
|
|
state.encodeUint(uint64(len(name)))
|
2014-10-17 20:51:15 -07:00
|
|
|
state.b.WriteString(name)
|
2011-01-28 10:53:42 -08:00
|
|
|
// Define the type id if necessary.
|
2011-02-22 12:31:57 -08:00
|
|
|
enc.sendTypeDescriptor(enc.writer(), state, ut)
|
2011-01-28 10:53:42 -08:00
|
|
|
// Send the type id.
|
2011-02-22 12:31:57 -08:00
|
|
|
enc.sendTypeId(state, ut)
|
2016-03-01 23:21:55 +00:00
|
|
|
// Encode the value into a new buffer. Any nested type definitions
|
2011-01-28 10:53:42 -08:00
|
|
|
// should be written to b, before the encoded value.
|
|
|
|
|
enc.pushWriter(b)
|
2015-08-15 13:04:46 +03:00
|
|
|
data := encBufferPool.Get().(*encBuffer)
|
2011-10-10 12:38:49 -07:00
|
|
|
data.Write(spaceForLength)
|
2012-06-12 00:36:39 -04:00
|
|
|
enc.encode(data, elem, ut)
|
2011-03-16 18:03:13 -07:00
|
|
|
if enc.err != nil {
|
2011-11-01 22:04:37 -04:00
|
|
|
error_(enc.err)
|
2010-10-31 13:41:30 -07:00
|
|
|
}
|
2011-01-28 10:53:42 -08:00
|
|
|
enc.popWriter()
|
|
|
|
|
enc.writeMessage(b, data)
|
2015-08-15 13:04:46 +03:00
|
|
|
data.Reset()
|
|
|
|
|
encBufferPool.Put(data)
|
2011-01-28 10:53:42 -08:00
|
|
|
if enc.err != nil {
|
2014-10-17 20:51:15 -07:00
|
|
|
error_(enc.err)
|
2010-10-22 15:16:34 -07:00
|
|
|
}
|
2011-03-15 21:30:07 -07:00
|
|
|
enc.freeEncoderState(state)
|
2010-10-22 11:17:40 -07:00
|
|
|
}
|
|
|
|
|
|
2023-01-29 14:40:22 +08:00
|
|
|
// encodeGobEncoder encodes a value that implements the GobEncoder interface.
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
// The data is sent as a byte array.
|
2014-10-17 20:51:15 -07:00
|
|
|
func (enc *Encoder) encodeGobEncoder(b *encBuffer, ut *userTypeInfo, v reflect.Value) {
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
// TODO: should we catch panics from the called method?
|
2013-08-14 00:18:48 -04:00
|
|
|
|
|
|
|
|
var data []byte
|
|
|
|
|
var err error
|
|
|
|
|
// We know it's one of these.
|
|
|
|
|
switch ut.externalEnc {
|
|
|
|
|
case xGob:
|
|
|
|
|
data, err = v.Interface().(GobEncoder).GobEncode()
|
|
|
|
|
case xBinary:
|
|
|
|
|
data, err = v.Interface().(encoding.BinaryMarshaler).MarshalBinary()
|
|
|
|
|
case xText:
|
|
|
|
|
data, err = v.Interface().(encoding.TextMarshaler).MarshalText()
|
|
|
|
|
}
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
if err != nil {
|
2011-11-01 22:04:37 -04:00
|
|
|
error_(err)
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
}
|
2011-03-15 21:30:07 -07:00
|
|
|
state := enc.newEncoderState(b)
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
state.fieldnum = -1
|
|
|
|
|
state.encodeUint(uint64(len(data)))
|
|
|
|
|
state.b.Write(data)
|
2011-03-15 21:30:07 -07:00
|
|
|
enc.freeEncoderState(state)
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
}
|
|
|
|
|
|
2011-02-25 09:45:06 -08:00
|
|
|
var encOpTable = [...]encOp{
|
2010-06-24 15:07:28 -07:00
|
|
|
reflect.Bool: encBool,
|
|
|
|
|
reflect.Int: encInt,
|
2014-06-30 11:06:47 -07:00
|
|
|
reflect.Int8: encInt,
|
|
|
|
|
reflect.Int16: encInt,
|
|
|
|
|
reflect.Int32: encInt,
|
|
|
|
|
reflect.Int64: encInt,
|
2010-06-24 15:07:28 -07:00
|
|
|
reflect.Uint: encUint,
|
2014-06-30 11:06:47 -07:00
|
|
|
reflect.Uint8: encUint,
|
|
|
|
|
reflect.Uint16: encUint,
|
|
|
|
|
reflect.Uint32: encUint,
|
|
|
|
|
reflect.Uint64: encUint,
|
|
|
|
|
reflect.Uintptr: encUint,
|
|
|
|
|
reflect.Float32: encFloat,
|
|
|
|
|
reflect.Float64: encFloat,
|
|
|
|
|
reflect.Complex64: encComplex,
|
|
|
|
|
reflect.Complex128: encComplex,
|
2010-06-24 15:07:28 -07:00
|
|
|
reflect.String: encString,
|
2009-07-02 11:21:01 -07:00
|
|
|
}
|
|
|
|
|
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
// encOpFor returns (a pointer to) the encoding op for the base type under rt and
|
2009-07-07 21:05:24 -07:00
|
|
|
// the indirection count to reach it.
|
2014-09-18 10:13:15 -07:00
|
|
|
func encOpFor(rt reflect.Type, inProgress map[reflect.Type]*encOp, building map[*typeInfo]bool) (*encOp, int) {
|
2011-02-22 12:31:57 -08:00
|
|
|
ut := userType(rt)
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
// If the type implements GobEncoder, we handle it without further processing.
|
2013-08-14 00:18:48 -04:00
|
|
|
if ut.externalEnc != 0 {
|
2014-07-18 12:39:00 +04:00
|
|
|
return gobEncodeOpFor(ut)
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
}
|
2011-02-25 09:45:06 -08:00
|
|
|
// If this type is already in progress, it's a recursive type (e.g. map[string]*T).
|
|
|
|
|
// Return the pointer to the op we're already building.
|
|
|
|
|
if opPtr := inProgress[rt]; opPtr != nil {
|
|
|
|
|
return opPtr, ut.indir
|
|
|
|
|
}
|
2011-02-22 12:31:57 -08:00
|
|
|
typ := ut.base
|
|
|
|
|
indir := ut.indir
|
2010-06-21 13:19:29 -07:00
|
|
|
k := typ.Kind()
|
2011-02-25 09:45:06 -08:00
|
|
|
var op encOp
|
|
|
|
|
if int(k) < len(encOpTable) {
|
|
|
|
|
op = encOpTable[k]
|
2010-06-21 13:19:29 -07:00
|
|
|
}
|
|
|
|
|
if op == nil {
|
2011-02-25 09:45:06 -08:00
|
|
|
inProgress[rt] = &op
|
2009-07-02 11:21:01 -07:00
|
|
|
// Special cases
|
2011-04-08 12:27:58 -04:00
|
|
|
switch t := typ; t.Kind() {
|
|
|
|
|
case reflect.Slice:
|
2010-06-20 12:16:25 -07:00
|
|
|
if t.Elem().Kind() == reflect.Uint8 {
|
2009-12-15 15:35:38 -08:00
|
|
|
op = encUint8Array
|
|
|
|
|
break
|
2009-07-02 11:21:01 -07:00
|
|
|
}
|
2009-07-07 11:04:42 -07:00
|
|
|
// Slices have a header; we decode it to find the underlying array.
|
2014-09-18 10:13:15 -07:00
|
|
|
elemOp, elemIndir := encOpFor(t.Elem(), inProgress, building)
|
2014-10-17 12:37:41 -07:00
|
|
|
helper := encSliceHelper[t.Elem().Kind()]
|
2014-06-30 11:06:47 -07:00
|
|
|
op = func(i *encInstr, state *encoderState, slice reflect.Value) {
|
|
|
|
|
if !state.sendZero && slice.Len() == 0 {
|
2009-11-09 12:07:39 -08:00
|
|
|
return
|
2009-07-07 11:04:42 -07:00
|
|
|
}
|
2009-12-15 15:35:38 -08:00
|
|
|
state.update(i)
|
2014-10-17 09:00:07 -07:00
|
|
|
state.enc.encodeArray(state.b, slice, *elemOp, elemIndir, slice.Len(), helper)
|
2009-12-15 15:35:38 -08:00
|
|
|
}
|
2011-04-08 12:27:58 -04:00
|
|
|
case reflect.Array:
|
2009-07-07 11:04:42 -07:00
|
|
|
// True arrays have size in the type.
|
2014-09-18 10:13:15 -07:00
|
|
|
elemOp, elemIndir := encOpFor(t.Elem(), inProgress, building)
|
2014-10-17 12:37:41 -07:00
|
|
|
helper := encArrayHelper[t.Elem().Kind()]
|
2014-06-30 11:06:47 -07:00
|
|
|
op = func(i *encInstr, state *encoderState, array reflect.Value) {
|
2009-12-15 15:35:38 -08:00
|
|
|
state.update(i)
|
2014-10-17 09:00:07 -07:00
|
|
|
state.enc.encodeArray(state.b, array, *elemOp, elemIndir, array.Len(), helper)
|
2010-05-05 16:46:39 -07:00
|
|
|
}
|
2011-04-08 12:27:58 -04:00
|
|
|
case reflect.Map:
|
2014-09-18 10:13:15 -07:00
|
|
|
keyOp, keyIndir := encOpFor(t.Key(), inProgress, building)
|
|
|
|
|
elemOp, elemIndir := encOpFor(t.Elem(), inProgress, building)
|
2014-06-30 11:06:47 -07:00
|
|
|
op = func(i *encInstr, state *encoderState, mv reflect.Value) {
|
2011-07-21 10:27:11 +10:00
|
|
|
// We send zero-length (but non-nil) maps because the
|
|
|
|
|
// receiver might want to use the map. (Maps don't use append.)
|
|
|
|
|
if !state.sendZero && mv.IsNil() {
|
2010-06-28 14:09:47 -07:00
|
|
|
return
|
|
|
|
|
}
|
2010-05-05 16:46:39 -07:00
|
|
|
state.update(i)
|
2011-02-25 09:45:06 -08:00
|
|
|
state.enc.encodeMap(state.b, mv, *keyOp, *elemOp, keyIndir, elemIndir)
|
2009-12-15 15:35:38 -08:00
|
|
|
}
|
2011-04-08 12:27:58 -04:00
|
|
|
case reflect.Struct:
|
2009-07-02 13:43:47 -07:00
|
|
|
// Generate a closure that calls out to the engine for the nested type.
|
2014-09-18 10:13:15 -07:00
|
|
|
getEncEngine(userType(typ), building)
|
2009-12-15 15:35:38 -08:00
|
|
|
info := mustGetTypeInfo(typ)
|
2014-06-30 11:06:47 -07:00
|
|
|
op = func(i *encInstr, state *encoderState, sv reflect.Value) {
|
2009-12-15 15:35:38 -08:00
|
|
|
state.update(i)
|
2009-07-09 14:33:43 -07:00
|
|
|
// indirect through info to delay evaluation for recursive structs
|
2022-08-08 14:13:52 -07:00
|
|
|
enc := info.encoder.Load()
|
2014-09-18 10:13:15 -07:00
|
|
|
state.enc.encodeStruct(state.b, enc, sv)
|
2009-12-15 15:35:38 -08:00
|
|
|
}
|
2011-04-08 12:27:58 -04:00
|
|
|
case reflect.Interface:
|
2014-06-30 11:06:47 -07:00
|
|
|
op = func(i *encInstr, state *encoderState, iv reflect.Value) {
|
2011-04-08 12:27:58 -04:00
|
|
|
if !state.sendZero && (!iv.IsValid() || iv.IsNil()) {
|
2010-10-22 11:17:40 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
state.update(i)
|
2010-10-31 13:41:30 -07:00
|
|
|
state.enc.encodeInterface(state.b, iv)
|
2010-10-22 11:17:40 -07:00
|
|
|
}
|
2009-07-02 13:43:47 -07:00
|
|
|
}
|
2009-07-02 11:21:01 -07:00
|
|
|
}
|
|
|
|
|
if op == nil {
|
2011-10-27 18:03:52 -07:00
|
|
|
errorf("can't happen: encode type %s", rt)
|
2009-07-02 11:21:01 -07:00
|
|
|
}
|
2011-02-25 09:45:06 -08:00
|
|
|
return &op, indir
|
2009-07-01 18:25:13 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-18 12:39:00 +04:00
|
|
|
// gobEncodeOpFor returns the op for a type that is known to implement GobEncoder.
|
|
|
|
|
func gobEncodeOpFor(ut *userTypeInfo) (*encOp, int) {
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
rt := ut.user
|
2011-03-07 12:08:31 -08:00
|
|
|
if ut.encIndir == -1 {
|
2021-10-25 23:00:56 +07:00
|
|
|
rt = reflect.PointerTo(rt)
|
2011-03-08 00:02:07 -08:00
|
|
|
} else if ut.encIndir > 0 {
|
|
|
|
|
for i := int8(0); i < ut.encIndir; i++ {
|
2011-04-08 12:27:58 -04:00
|
|
|
rt = rt.Elem()
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var op encOp
|
2014-06-30 11:06:47 -07:00
|
|
|
op = func(i *encInstr, state *encoderState, v reflect.Value) {
|
2011-03-08 00:02:07 -08:00
|
|
|
if ut.encIndir == -1 {
|
|
|
|
|
// Need to climb up one level to turn value into pointer.
|
2014-06-30 11:06:47 -07:00
|
|
|
if !v.CanAddr() {
|
|
|
|
|
errorf("unaddressable value of type %s", rt)
|
|
|
|
|
}
|
|
|
|
|
v = v.Addr()
|
2011-03-07 12:08:31 -08:00
|
|
|
}
|
2023-03-25 11:23:12 +00:00
|
|
|
if !state.sendZero && v.IsZero() {
|
2011-08-04 14:39:44 +10:00
|
|
|
return
|
|
|
|
|
}
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
state.update(i)
|
2013-08-14 00:18:48 -04:00
|
|
|
state.enc.encodeGobEncoder(state.b, ut, v)
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
}
|
2011-03-08 00:02:07 -08:00
|
|
|
return &op, int(ut.encIndir) // encIndir: op will get called with p == address of receiver.
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// compileEnc returns the engine to compile the type.
|
2014-09-18 10:13:15 -07:00
|
|
|
func compileEnc(ut *userTypeInfo, building map[*typeInfo]bool) *encEngine {
|
2011-04-08 12:27:58 -04:00
|
|
|
srt := ut.base
|
2009-12-15 15:35:38 -08:00
|
|
|
engine := new(encEngine)
|
2011-02-25 09:45:06 -08:00
|
|
|
seen := make(map[reflect.Type]*encOp)
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
rt := ut.base
|
2013-08-14 00:18:48 -04:00
|
|
|
if ut.externalEnc != 0 {
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
rt = ut.user
|
|
|
|
|
}
|
2013-09-16 10:26:23 +10:00
|
|
|
if ut.externalEnc == 0 && srt.Kind() == reflect.Struct {
|
2011-03-05 16:05:35 -08:00
|
|
|
for fieldNum, wireFieldNum := 0, 0; fieldNum < srt.NumField(); fieldNum++ {
|
2011-01-22 00:10:11 -08:00
|
|
|
f := srt.Field(fieldNum)
|
2013-09-16 10:26:23 +10:00
|
|
|
if !isSent(&f) {
|
2011-01-22 00:10:11 -08:00
|
|
|
continue
|
2011-01-11 13:44:00 -08:00
|
|
|
}
|
2014-09-18 10:13:15 -07:00
|
|
|
op, indir := encOpFor(f.Type, seen, building)
|
2014-06-30 11:06:47 -07:00
|
|
|
engine.instr = append(engine.instr, encInstr{*op, wireFieldNum, f.Index, indir})
|
2011-03-05 16:05:35 -08:00
|
|
|
wireFieldNum++
|
2011-01-22 00:10:11 -08:00
|
|
|
}
|
|
|
|
|
if srt.NumField() > 0 && len(engine.instr) == 0 {
|
2011-04-20 14:22:52 -07:00
|
|
|
errorf("type %s has no exported fields", rt)
|
2010-06-28 14:09:47 -07:00
|
|
|
}
|
2014-06-30 11:06:47 -07:00
|
|
|
engine.instr = append(engine.instr, encInstr{encStructTerminator, 0, nil, 0})
|
2010-06-28 14:09:47 -07:00
|
|
|
} else {
|
|
|
|
|
engine.instr = make([]encInstr, 1)
|
2014-09-18 10:13:15 -07:00
|
|
|
op, indir := encOpFor(rt, seen, building)
|
2014-06-30 11:06:47 -07:00
|
|
|
engine.instr[0] = encInstr{*op, singletonField, nil, indir}
|
2009-07-01 18:25:13 -07:00
|
|
|
}
|
2010-10-22 16:07:26 -07:00
|
|
|
return engine
|
2009-07-01 18:25:13 -07:00
|
|
|
}
|
|
|
|
|
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
// getEncEngine returns the engine to compile the type.
|
2014-09-18 10:13:15 -07:00
|
|
|
func getEncEngine(ut *userTypeInfo, building map[*typeInfo]bool) *encEngine {
|
|
|
|
|
info, err := getTypeInfo(ut)
|
|
|
|
|
if err != nil {
|
|
|
|
|
error_(err)
|
|
|
|
|
}
|
2022-08-08 14:13:52 -07:00
|
|
|
enc := info.encoder.Load()
|
|
|
|
|
if enc == nil {
|
2014-09-18 10:13:15 -07:00
|
|
|
enc = buildEncEngine(info, ut, building)
|
2009-07-01 18:25:13 -07:00
|
|
|
}
|
2014-09-18 10:13:15 -07:00
|
|
|
return enc
|
2009-07-01 18:25:13 -07:00
|
|
|
}
|
|
|
|
|
|
2014-09-18 10:13:15 -07:00
|
|
|
func buildEncEngine(info *typeInfo, ut *userTypeInfo, building map[*typeInfo]bool) *encEngine {
|
|
|
|
|
// Check for recursive types.
|
|
|
|
|
if building != nil && building[info] {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
info.encInit.Lock()
|
|
|
|
|
defer info.encInit.Unlock()
|
2022-08-08 14:13:52 -07:00
|
|
|
enc := info.encoder.Load()
|
|
|
|
|
if enc == nil {
|
2014-09-18 10:13:15 -07:00
|
|
|
if building == nil {
|
|
|
|
|
building = make(map[*typeInfo]bool)
|
|
|
|
|
}
|
|
|
|
|
building[info] = true
|
|
|
|
|
enc = compileEnc(ut, building)
|
|
|
|
|
info.encoder.Store(enc)
|
|
|
|
|
}
|
|
|
|
|
return enc
|
2010-10-22 16:07:26 -07:00
|
|
|
}
|
|
|
|
|
|
2014-10-17 20:51:15 -07:00
|
|
|
func (enc *Encoder) encode(b *encBuffer, value reflect.Value, ut *userTypeInfo) {
|
2011-03-16 18:03:13 -07:00
|
|
|
defer catchError(&enc.err)
|
2014-09-18 10:13:15 -07:00
|
|
|
engine := getEncEngine(ut, nil)
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
indir := ut.indir
|
2013-08-14 00:18:48 -04:00
|
|
|
if ut.externalEnc != 0 {
|
gob: beginning of support for GobEncoder/GobDecoder interfaces.
This allows a data item that can marshal itself to be transmitted by its
own encoding, enabling some types to be handled that cannot be
normally, plus providing a way to use gobs on data with unexported
fields.
In this CL, the necessary methods are protected by leading _, so only
package gob can use the facilities (in its tests, of course); this
code is not ready for real use yet. I could be talked into enabling
it for experimentation, though. The main drawback is that the
methods must be implemented by the actual type passed through,
not by an indirection from it. For instance, if *T implements
GobEncoder, you must send a *T, not a T. This will be addressed
in due course.
Also there is improved commentary and a couple of unrelated
minor bug fixes.
R=rsc
CC=golang-dev
https://golang.org/cl/4243056
2011-03-04 12:25:18 -08:00
|
|
|
indir = int(ut.encIndir)
|
|
|
|
|
}
|
|
|
|
|
for i := 0; i < indir; i++ {
|
2010-06-28 17:11:54 -07:00
|
|
|
value = reflect.Indirect(value)
|
2009-07-01 18:25:13 -07:00
|
|
|
}
|
2013-08-14 00:18:48 -04:00
|
|
|
if ut.externalEnc == 0 && value.Type().Kind() == reflect.Struct {
|
2014-06-30 11:06:47 -07:00
|
|
|
enc.encodeStruct(b, engine, value)
|
2010-10-22 16:07:26 -07:00
|
|
|
} else {
|
2014-06-30 11:06:47 -07:00
|
|
|
enc.encodeSingle(b, engine, value)
|
2010-06-28 14:09:47 -07:00
|
|
|
}
|
2009-07-01 18:25:13 -07:00
|
|
|
}
|