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.
|
|
|
|
|
|
|
|
|
|
package gob
|
|
|
|
|
|
|
|
|
|
import (
|
2009-12-15 15:35:38 -08:00
|
|
|
"bytes"
|
|
|
|
|
"math"
|
|
|
|
|
"reflect"
|
|
|
|
|
"unsafe"
|
2009-06-29 15:15:07 -07:00
|
|
|
)
|
|
|
|
|
|
2009-07-28 17:20:19 -07:00
|
|
|
const uint64Size = unsafe.Sizeof(uint64(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
|
|
|
// 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
|
2009-12-15 15:35:38 -08:00
|
|
|
b *bytes.Buffer
|
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
|
|
|
}
|
|
|
|
|
|
2011-03-15 21:30:07 -07:00
|
|
|
func (enc *Encoder) newEncoderState(b *bytes.Buffer) *encoderState {
|
|
|
|
|
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
|
|
|
|
|
return e
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (enc *Encoder) freeEncoderState(e *encoderState) {
|
|
|
|
|
e.next = enc.freeList
|
|
|
|
|
enc.freeList = e
|
2010-10-22 11:17:40 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-28 17:20:19 -07:00
|
|
|
// Unsigned integers have a two-state encoding. If the number is less
|
|
|
|
|
// 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 {
|
2010-10-22 15:16:34 -07:00
|
|
|
err := state.b.WriteByte(uint8(x))
|
|
|
|
|
if err != nil {
|
|
|
|
|
error(err)
|
|
|
|
|
}
|
2009-12-15 15:35:38 -08:00
|
|
|
return
|
2009-07-28 17:20:19 -07:00
|
|
|
}
|
2009-12-15 15:35:38 -08:00
|
|
|
var n, m int
|
|
|
|
|
m = uint64Size
|
2009-07-28 17:20:19 -07:00
|
|
|
for n = 1; x > 0; n++ {
|
2009-12-15 15:35:38 -08:00
|
|
|
state.buf[m] = uint8(x & 0xFF)
|
|
|
|
|
x >>= 8
|
|
|
|
|
m--
|
2009-06-29 15:15:07 -07:00
|
|
|
}
|
2009-12-15 15:35:38 -08:00
|
|
|
state.buf[m] = uint8(-(n - 1))
|
2010-10-22 15:16:34 -07:00
|
|
|
n, err := state.b.Write(state.buf[m : uint64Size+1])
|
|
|
|
|
if err != nil {
|
|
|
|
|
error(err)
|
|
|
|
|
}
|
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
|
|
|
}
|
2011-01-11 17:56:45 -08:00
|
|
|
state.encodeUint(uint64(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.
|
2009-07-15 16:10:17 -07:00
|
|
|
type encOp func(i *encInstr, state *encoderState, p unsafe.Pointer)
|
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 {
|
2009-12-15 15:35:38 -08:00
|
|
|
op encOp
|
|
|
|
|
field int // field number
|
|
|
|
|
indir int // how many pointer indirections to reach the value in the struct
|
|
|
|
|
offset uintptr // offset in the structure of the field to encode
|
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.
|
|
|
|
|
// If any pointer so reached is nil, no bytes are written. If the
|
|
|
|
|
// data item is zero, no bytes are written. Single values - ints,
|
|
|
|
|
// 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
|
|
|
|
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
|
|
|
// encIndirect dereferences p indir times and returns the result.
|
2009-06-30 17:59:41 -07:00
|
|
|
func encIndirect(p unsafe.Pointer, indir int) unsafe.Pointer {
|
|
|
|
|
for ; indir > 0; indir-- {
|
2009-12-15 15:35:38 -08:00
|
|
|
p = *(*unsafe.Pointer)(p)
|
2009-06-30 15:37:46 -07:00
|
|
|
if p == nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
return unsafe.Pointer(nil)
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
|
|
|
|
}
|
2009-12-15 15:35:38 -08:00
|
|
|
return p
|
2009-06-30 17:59:41 -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
|
|
|
// encBool encodes the bool with address p as an unsigned 0 or 1.
|
2009-07-15 16:10:17 -07:00
|
|
|
func encBool(i *encInstr, state *encoderState, p unsafe.Pointer) {
|
2009-12-15 15:35:38 -08:00
|
|
|
b := *(*bool)(p)
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
// encInt encodes the int with address p.
|
2009-07-15 16:10:17 -07:00
|
|
|
func encInt(i *encInstr, state *encoderState, p unsafe.Pointer) {
|
2009-12-15 15:35:38 -08:00
|
|
|
v := int64(*(*int)(p))
|
2010-06-28 14:09:47 -07:00
|
|
|
if v != 0 || state.sendZero {
|
2009-12-15 15:35:38 -08:00
|
|
|
state.update(i)
|
2011-01-11 17:56:45 -08:00
|
|
|
state.encodeInt(v)
|
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
|
|
|
// encUint encodes the uint with address p.
|
2009-07-15 16:10:17 -07:00
|
|
|
func encUint(i *encInstr, state *encoderState, p unsafe.Pointer) {
|
2009-12-15 15:35:38 -08:00
|
|
|
v := uint64(*(*uint)(p))
|
2010-06-28 14:09:47 -07:00
|
|
|
if v != 0 || state.sendZero {
|
2009-12-15 15:35:38 -08:00
|
|
|
state.update(i)
|
2011-01-11 17:56:45 -08:00
|
|
|
state.encodeUint(v)
|
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
|
|
|
// encInt8 encodes the int8 with address p.
|
2009-07-15 16:10:17 -07:00
|
|
|
func encInt8(i *encInstr, state *encoderState, p unsafe.Pointer) {
|
2009-12-15 15:35:38 -08:00
|
|
|
v := int64(*(*int8)(p))
|
2010-06-28 14:09:47 -07:00
|
|
|
if v != 0 || state.sendZero {
|
2009-12-15 15:35:38 -08:00
|
|
|
state.update(i)
|
2011-01-11 17:56:45 -08:00
|
|
|
state.encodeInt(v)
|
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
|
|
|
// encUint8 encodes the uint8 with address p.
|
2009-07-15 16:10:17 -07:00
|
|
|
func encUint8(i *encInstr, state *encoderState, p unsafe.Pointer) {
|
2009-12-15 15:35:38 -08:00
|
|
|
v := uint64(*(*uint8)(p))
|
2010-06-28 14:09:47 -07:00
|
|
|
if v != 0 || state.sendZero {
|
2009-12-15 15:35:38 -08:00
|
|
|
state.update(i)
|
2011-01-11 17:56:45 -08:00
|
|
|
state.encodeUint(v)
|
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
|
|
|
// encInt16 encodes the int16 with address p.
|
2009-07-15 16:10:17 -07:00
|
|
|
func encInt16(i *encInstr, state *encoderState, p unsafe.Pointer) {
|
2009-12-15 15:35:38 -08:00
|
|
|
v := int64(*(*int16)(p))
|
2010-06-28 14:09:47 -07:00
|
|
|
if v != 0 || state.sendZero {
|
2009-12-15 15:35:38 -08:00
|
|
|
state.update(i)
|
2011-01-11 17:56:45 -08:00
|
|
|
state.encodeInt(v)
|
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
|
|
|
// encUint16 encodes the uint16 with address p.
|
2009-07-15 16:10:17 -07:00
|
|
|
func encUint16(i *encInstr, state *encoderState, p unsafe.Pointer) {
|
2009-12-15 15:35:38 -08:00
|
|
|
v := uint64(*(*uint16)(p))
|
2010-06-28 14:09:47 -07:00
|
|
|
if v != 0 || state.sendZero {
|
2009-12-15 15:35:38 -08:00
|
|
|
state.update(i)
|
2011-01-11 17:56:45 -08:00
|
|
|
state.encodeUint(v)
|
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
|
|
|
// encInt32 encodes the int32 with address p.
|
2009-07-15 16:10:17 -07:00
|
|
|
func encInt32(i *encInstr, state *encoderState, p unsafe.Pointer) {
|
2009-12-15 15:35:38 -08:00
|
|
|
v := int64(*(*int32)(p))
|
2010-06-28 14:09:47 -07:00
|
|
|
if v != 0 || state.sendZero {
|
2009-12-15 15:35:38 -08:00
|
|
|
state.update(i)
|
2011-01-11 17:56:45 -08:00
|
|
|
state.encodeInt(v)
|
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
|
|
|
// encUint encodes the uint32 with address p.
|
2009-07-15 16:10:17 -07:00
|
|
|
func encUint32(i *encInstr, state *encoderState, p unsafe.Pointer) {
|
2009-12-15 15:35:38 -08:00
|
|
|
v := uint64(*(*uint32)(p))
|
2010-06-28 14:09:47 -07:00
|
|
|
if v != 0 || state.sendZero {
|
2009-12-15 15:35:38 -08:00
|
|
|
state.update(i)
|
2011-01-11 17:56:45 -08:00
|
|
|
state.encodeUint(v)
|
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
|
|
|
// encInt64 encodes the int64 with address p.
|
2009-07-15 16:10:17 -07:00
|
|
|
func encInt64(i *encInstr, state *encoderState, p unsafe.Pointer) {
|
2009-12-15 15:35:38 -08:00
|
|
|
v := *(*int64)(p)
|
2010-06-28 14:09:47 -07:00
|
|
|
if v != 0 || state.sendZero {
|
2009-12-15 15:35:38 -08:00
|
|
|
state.update(i)
|
2011-01-11 17:56:45 -08:00
|
|
|
state.encodeInt(v)
|
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
|
|
|
// encInt64 encodes the uint64 with address p.
|
2009-07-15 16:10:17 -07:00
|
|
|
func encUint64(i *encInstr, state *encoderState, p unsafe.Pointer) {
|
2009-12-15 15:35:38 -08:00
|
|
|
v := *(*uint64)(p)
|
2010-06-28 14:09:47 -07:00
|
|
|
if v != 0 || state.sendZero {
|
2009-12-15 15:35:38 -08:00
|
|
|
state.update(i)
|
2011-01-11 17:56:45 -08:00
|
|
|
state.encodeUint(v)
|
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
|
|
|
// encUintptr encodes the uintptr with address p.
|
2009-07-15 16:10:17 -07:00
|
|
|
func encUintptr(i *encInstr, state *encoderState, p unsafe.Pointer) {
|
2009-12-15 15:35:38 -08:00
|
|
|
v := uint64(*(*uintptr)(p))
|
2010-06-28 14:09:47 -07:00
|
|
|
if v != 0 || state.sendZero {
|
2009-12-15 15:35:38 -08:00
|
|
|
state.update(i)
|
2011-01-11 17:56:45 -08:00
|
|
|
state.encodeUint(v)
|
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
|
|
|
|
|
// of the underlying representation. They are sent byte-reversed, with
|
|
|
|
|
// the exponent end coming out first, so integer floating point numbers
|
|
|
|
|
// (for example) transmit more compactly. This routine does the
|
|
|
|
|
// swizzling.
|
|
|
|
|
func floatBits(f float64) uint64 {
|
2009-12-15 15:35:38 -08:00
|
|
|
u := math.Float64bits(f)
|
|
|
|
|
var v uint64
|
2009-06-30 15:37:46 -07:00
|
|
|
for i := 0; i < 8; i++ {
|
2009-12-15 15:35:38 -08:00
|
|
|
v <<= 8
|
|
|
|
|
v |= u & 0xFF
|
|
|
|
|
u >>= 8
|
2009-06-30 15:37:46 -07:00
|
|
|
}
|
2009-12-15 15:35:38 -08:00
|
|
|
return v
|
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
|
|
|
// encFloat32 encodes the float32 with address p.
|
2009-07-15 16:10:17 -07:00
|
|
|
func encFloat32(i *encInstr, state *encoderState, p unsafe.Pointer) {
|
2010-06-24 15:07:28 -07:00
|
|
|
f := *(*float32)(p)
|
2010-06-28 14:09:47 -07:00
|
|
|
if f != 0 || state.sendZero {
|
2009-12-15 15:35:38 -08:00
|
|
|
v := floatBits(float64(f))
|
|
|
|
|
state.update(i)
|
2011-01-11 17:56:45 -08:00
|
|
|
state.encodeUint(v)
|
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
|
|
|
// encFloat64 encodes the float64 with address p.
|
2009-07-15 16:10:17 -07:00
|
|
|
func encFloat64(i *encInstr, state *encoderState, p unsafe.Pointer) {
|
2009-12-15 15:35:38 -08:00
|
|
|
f := *(*float64)(p)
|
2010-06-28 14:09:47 -07:00
|
|
|
if f != 0 || state.sendZero {
|
2009-12-15 15:35:38 -08:00
|
|
|
state.update(i)
|
|
|
|
|
v := floatBits(f)
|
2011-01-11 17:56:45 -08:00
|
|
|
state.encodeUint(v)
|
2009-06-30 15:37:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
// encComplex64 encodes the complex64 with address p.
|
2010-06-24 15:07:28 -07:00
|
|
|
// Complex numbers are just a pair of floating-point numbers, real part first.
|
|
|
|
|
func encComplex64(i *encInstr, state *encoderState, p unsafe.Pointer) {
|
|
|
|
|
c := *(*complex64)(p)
|
2010-06-28 14:09:47 -07:00
|
|
|
if c != 0+0i || state.sendZero {
|
2010-06-24 15:07:28 -07:00
|
|
|
rpart := floatBits(float64(real(c)))
|
|
|
|
|
ipart := floatBits(float64(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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
// encComplex128 encodes the complex128 with address p.
|
2010-06-24 15:07:28 -07:00
|
|
|
func encComplex128(i *encInstr, state *encoderState, p unsafe.Pointer) {
|
|
|
|
|
c := *(*complex128)(p)
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
// encUint8Array encodes the byte slice whose header has address p.
|
2009-07-02 11:21:01 -07:00
|
|
|
// Byte arrays are encoded as an unsigned count followed by the raw bytes.
|
2009-07-15 16:10:17 -07:00
|
|
|
func encUint8Array(i *encInstr, state *encoderState, p unsafe.Pointer) {
|
2009-12-15 15:35:38 -08:00
|
|
|
b := *(*[]byte)(p)
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
// encString encodes the string whose header has address p.
|
2009-07-02 11:21:01 -07:00
|
|
|
// Strings are encoded as an unsigned count followed by the raw bytes.
|
2009-07-15 16:10:17 -07:00
|
|
|
func encString(i *encInstr, state *encoderState, p unsafe.Pointer) {
|
2009-12-15 15:35:38 -08:00
|
|
|
s := *(*string)(p)
|
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.
|
2009-07-15 16:10:17 -07:00
|
|
|
func encStructTerminator(i *encInstr, state *encoderState, p unsafe.Pointer) {
|
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
|
2009-07-01 18:25:13 -07:00
|
|
|
// data, typically a struct. It is executed top to bottom, walking the struct.
|
|
|
|
|
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
|
|
|
|
|
|
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.
|
2010-10-31 13:41:30 -07:00
|
|
|
func (enc *Encoder) encodeSingle(b *bytes.Buffer, engine *encEngine, basep uintptr) {
|
2011-03-15 21:30:07 -07:00
|
|
|
state := enc.newEncoderState(b)
|
2010-06-28 14:09:47 -07:00
|
|
|
state.fieldnum = singletonField
|
|
|
|
|
// There is no surrounding struct to frame the transmission, so we must
|
|
|
|
|
// generate data even if the item is zero. To do this, set sendZero.
|
|
|
|
|
state.sendZero = true
|
|
|
|
|
instr := &engine.instr[singletonField]
|
|
|
|
|
p := unsafe.Pointer(basep) // offset will be zero
|
|
|
|
|
if instr.indir > 0 {
|
|
|
|
|
if p = encIndirect(p, instr.indir); p == nil {
|
2010-10-22 16:07:26 -07:00
|
|
|
return
|
2010-06-28 14:09:47 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
instr.op(instr, state, p)
|
2011-03-15 21:30:07 -07:00
|
|
|
enc.freeEncoderState(state)
|
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.
|
2010-10-31 13:41:30 -07:00
|
|
|
func (enc *Encoder) encodeStruct(b *bytes.Buffer, engine *encEngine, basep uintptr) {
|
2011-03-15 21:30:07 -07:00
|
|
|
state := enc.newEncoderState(b)
|
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]
|
|
|
|
|
p := unsafe.Pointer(basep + instr.offset)
|
2009-07-02 13:43:47 -07:00
|
|
|
if instr.indir > 0 {
|
|
|
|
|
if p = encIndirect(p, instr.indir); p == nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
continue
|
2009-07-02 13:43:47 -07:00
|
|
|
}
|
|
|
|
|
}
|
2009-12-15 15:35:38 -08:00
|
|
|
instr.op(instr, state, p)
|
2009-07-02 13:43:47 -07:00
|
|
|
}
|
2011-03-15 21:30:07 -07:00
|
|
|
enc.freeEncoderState(state)
|
2009-07-02 13:43: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
|
|
|
// encodeArray encodes the array whose 0th element is at p.
|
2010-10-31 13:41:30 -07:00
|
|
|
func (enc *Encoder) encodeArray(b *bytes.Buffer, p uintptr, op encOp, elemWid uintptr, elemIndir int, length int) {
|
2011-03-15 21:30:07 -07:00
|
|
|
state := enc.newEncoderState(b)
|
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))
|
2010-10-22 15:16:34 -07:00
|
|
|
for i := 0; i < length; i++ {
|
2009-12-15 15:35:38 -08:00
|
|
|
elemp := p
|
|
|
|
|
up := unsafe.Pointer(elemp)
|
2009-07-02 17:21:48 -07:00
|
|
|
if elemIndir > 0 {
|
|
|
|
|
if up = encIndirect(up, elemIndir); up == nil {
|
2011-04-20 14:22:52 -07:00
|
|
|
errorf("encodeArray: nil element")
|
2009-07-02 17:21:48 -07:00
|
|
|
}
|
2009-12-15 15:35:38 -08:00
|
|
|
elemp = uintptr(up)
|
2009-07-02 17:21:48 -07:00
|
|
|
}
|
2009-12-15 15:35:38 -08:00
|
|
|
op(nil, state, unsafe.Pointer(elemp))
|
|
|
|
|
p += uintptr(elemWid)
|
2009-07-02 16:43:46 -07:00
|
|
|
}
|
2011-03-15 21:30:07 -07:00
|
|
|
enc.freeEncoderState(state)
|
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
|
|
|
}
|
2011-04-18 14:36:22 -04:00
|
|
|
op(nil, state, unsafe.Pointer(unsafeAddr(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.
|
|
|
|
|
// Because map internals are not exposed, we must use reflection rather than
|
|
|
|
|
// addresses.
|
2011-04-08 12:27:58 -04:00
|
|
|
func (enc *Encoder) encodeMap(b *bytes.Buffer, 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
|
2011-04-08 12:27:58 -04:00
|
|
|
keys := mv.MapKeys()
|
2011-01-11 17:56:45 -08:00
|
|
|
state.encodeUint(uint64(len(keys)))
|
2010-05-05 16:46:39 -07:00
|
|
|
for _, key := range keys {
|
|
|
|
|
encodeReflectValue(state, key, keyOp, keyIndir)
|
2011-04-08 12:27:58 -04:00
|
|
|
encodeReflectValue(state, mv.MapIndex(key), 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
|
|
|
|
|
// by the concrete value. A nil value gets sent as the empty string for the name,
|
|
|
|
|
// followed by no value.
|
2011-04-08 12:27:58 -04:00
|
|
|
func (enc *Encoder) encodeInterface(b *bytes.Buffer, iv reflect.Value) {
|
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())
|
|
|
|
|
name, ok := concreteTypeToName[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
|
|
|
}
|
|
|
|
|
// Send the name.
|
2011-01-11 17:56:45 -08:00
|
|
|
state.encodeUint(uint64(len(name)))
|
2011-03-16 18:03:13 -07:00
|
|
|
_, err := state.b.WriteString(name)
|
2010-10-22 15:16:34 -07:00
|
|
|
if err != nil {
|
|
|
|
|
error(err)
|
2010-10-22 11:17:40 -07:00
|
|
|
}
|
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)
|
2011-01-28 10:53:42 -08:00
|
|
|
// Encode the value into a new buffer. Any nested type definitions
|
|
|
|
|
// should be written to b, before the encoded value.
|
|
|
|
|
enc.pushWriter(b)
|
2010-10-31 13:41:30 -07:00
|
|
|
data := new(bytes.Buffer)
|
2011-03-16 18:03:13 -07:00
|
|
|
enc.encode(data, iv.Elem(), ut)
|
|
|
|
|
if enc.err != nil {
|
|
|
|
|
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)
|
|
|
|
|
if enc.err != nil {
|
2010-10-22 15:16:34 -07:00
|
|
|
error(err)
|
|
|
|
|
}
|
2011-03-15 21:30:07 -07:00
|
|
|
enc.freeEncoderState(state)
|
2010-10-22 11:17:40 -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
|
|
|
// encGobEncoder encodes a value that implements the GobEncoder interface.
|
|
|
|
|
// The data is sent as a byte array.
|
|
|
|
|
func (enc *Encoder) encodeGobEncoder(b *bytes.Buffer, v reflect.Value, index int) {
|
|
|
|
|
// TODO: should we catch panics from the called method?
|
|
|
|
|
// We know it's a GobEncoder, so just call the method directly.
|
2011-03-04 14:18:52 -08:00
|
|
|
data, err := v.Interface().(GobEncoder).GobEncode()
|
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 {
|
|
|
|
|
error(err)
|
|
|
|
|
}
|
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,
|
|
|
|
|
reflect.Int8: encInt8,
|
|
|
|
|
reflect.Int16: encInt16,
|
|
|
|
|
reflect.Int32: encInt32,
|
|
|
|
|
reflect.Int64: encInt64,
|
|
|
|
|
reflect.Uint: encUint,
|
|
|
|
|
reflect.Uint8: encUint8,
|
|
|
|
|
reflect.Uint16: encUint16,
|
|
|
|
|
reflect.Uint32: encUint32,
|
|
|
|
|
reflect.Uint64: encUint64,
|
|
|
|
|
reflect.Uintptr: encUintptr,
|
|
|
|
|
reflect.Float32: encFloat32,
|
|
|
|
|
reflect.Float64: encFloat64,
|
|
|
|
|
reflect.Complex64: encComplex64,
|
|
|
|
|
reflect.Complex128: encComplex128,
|
|
|
|
|
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.
|
2011-02-25 09:45:06 -08:00
|
|
|
func (enc *Encoder) encOpFor(rt reflect.Type, inProgress map[reflect.Type]*encOp) (*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.
|
|
|
|
|
if ut.isGobEncoder {
|
|
|
|
|
return enc.gobEncodeOpFor(ut)
|
|
|
|
|
}
|
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.
|
2011-02-25 09:45:06 -08:00
|
|
|
elemOp, indir := enc.encOpFor(t.Elem(), inProgress)
|
2009-07-15 16:10:17 -07:00
|
|
|
op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
|
2009-12-15 15:35:38 -08:00
|
|
|
slice := (*reflect.SliceHeader)(p)
|
2010-12-09 11:01:32 -05:00
|
|
|
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)
|
2011-02-25 09:45:06 -08:00
|
|
|
state.enc.encodeArray(state.b, slice.Data, *elemOp, t.Elem().Size(), indir, int(slice.Len))
|
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.
|
2011-02-25 09:45:06 -08:00
|
|
|
elemOp, indir := enc.encOpFor(t.Elem(), inProgress)
|
2009-07-15 16:10:17 -07:00
|
|
|
op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
|
2009-12-15 15:35:38 -08:00
|
|
|
state.update(i)
|
2011-02-25 09:45:06 -08:00
|
|
|
state.enc.encodeArray(state.b, uintptr(p), *elemOp, t.Elem().Size(), indir, t.Len())
|
2010-05-05 16:46:39 -07:00
|
|
|
}
|
2011-04-08 12:27:58 -04:00
|
|
|
case reflect.Map:
|
2011-02-25 09:45:06 -08:00
|
|
|
keyOp, keyIndir := enc.encOpFor(t.Key(), inProgress)
|
|
|
|
|
elemOp, elemIndir := enc.encOpFor(t.Elem(), inProgress)
|
2010-05-05 16:46:39 -07:00
|
|
|
op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
|
2010-06-28 14:09:47 -07:00
|
|
|
// Maps cannot be accessed by moving addresses around the way
|
|
|
|
|
// that slices etc. can. We must recover a full reflection value for
|
|
|
|
|
// the iteration.
|
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
|
|
|
v := reflect.NewValue(unsafe.Unreflect(t, unsafe.Pointer(p)))
|
2011-04-08 12:27:58 -04:00
|
|
|
mv := reflect.Indirect(v)
|
2010-12-09 11:01:32 -05:00
|
|
|
if !state.sendZero && mv.Len() == 0 {
|
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.
|
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
|
|
|
enc.getEncEngine(userType(typ))
|
2009-12-15 15:35:38 -08:00
|
|
|
info := mustGetTypeInfo(typ)
|
2009-07-15 16:10:17 -07:00
|
|
|
op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
|
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
|
2010-10-31 13:41:30 -07:00
|
|
|
state.enc.encodeStruct(state.b, info.encoder, uintptr(p))
|
2009-12-15 15:35:38 -08:00
|
|
|
}
|
2011-04-08 12:27:58 -04:00
|
|
|
case reflect.Interface:
|
2010-10-22 11:17:40 -07:00
|
|
|
op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
|
|
|
|
|
// Interfaces transmit the name and contents of the concrete
|
|
|
|
|
// value they contain.
|
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
|
|
|
v := reflect.NewValue(unsafe.Unreflect(t, unsafe.Pointer(p)))
|
2011-04-08 12:27:58 -04:00
|
|
|
iv := reflect.Indirect(v)
|
|
|
|
|
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-04-20 14:22:52 -07:00
|
|
|
errorf("can't happen: encode type %s", rt.String())
|
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
|
|
|
}
|
|
|
|
|
|
2011-03-08 00:02:07 -08:00
|
|
|
// methodIndex returns which method of rt implements the method.
|
|
|
|
|
func methodIndex(rt reflect.Type, method string) int {
|
|
|
|
|
for i := 0; i < rt.NumMethod(); i++ {
|
|
|
|
|
if rt.Method(i).Name == method {
|
|
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-04-20 14:22:52 -07:00
|
|
|
errorf("internal error: can't find method %s", method)
|
2011-03-09 17:30:27 -08:00
|
|
|
return 0
|
2011-03-08 00:02:07 -08: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
|
|
|
// gobEncodeOpFor returns the op for a type that is known to implement
|
|
|
|
|
// GobEncoder.
|
|
|
|
|
func (enc *Encoder) gobEncodeOpFor(ut *userTypeInfo) (*encOp, int) {
|
|
|
|
|
rt := ut.user
|
2011-03-07 12:08:31 -08:00
|
|
|
if ut.encIndir == -1 {
|
|
|
|
|
rt = reflect.PtrTo(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
|
|
|
|
|
op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
|
2011-03-07 12:08:31 -08:00
|
|
|
var 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.
|
2011-03-07 12:08:31 -08:00
|
|
|
v = reflect.NewValue(unsafe.Unreflect(rt, unsafe.Pointer(&p)))
|
2011-03-08 00:02:07 -08:00
|
|
|
} else {
|
|
|
|
|
v = reflect.NewValue(unsafe.Unreflect(rt, p))
|
2011-03-07 12:08:31 -08: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
|
|
|
state.update(i)
|
2011-03-08 00:02:07 -08:00
|
|
|
state.enc.encodeGobEncoder(state.b, v, methodIndex(rt, gobEncodeMethodName))
|
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.
|
|
|
|
|
func (enc *Encoder) compileEnc(ut *userTypeInfo) *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
|
|
|
|
|
if ut.isGobEncoder {
|
|
|
|
|
rt = ut.user
|
|
|
|
|
}
|
2011-04-08 12:27:58 -04:00
|
|
|
if !ut.isGobEncoder &&
|
|
|
|
|
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)
|
2011-01-11 13:44:00 -08:00
|
|
|
if !isExported(f.Name) {
|
2011-01-22 00:10:11 -08:00
|
|
|
continue
|
2011-01-11 13:44:00 -08:00
|
|
|
}
|
2011-02-25 09:45:06 -08:00
|
|
|
op, indir := enc.encOpFor(f.Type, seen)
|
2011-03-05 16:05:35 -08:00
|
|
|
engine.instr = append(engine.instr, encInstr{*op, wireFieldNum, indir, uintptr(f.Offset)})
|
|
|
|
|
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
|
|
|
}
|
2011-01-22 00:10:11 -08:00
|
|
|
engine.instr = append(engine.instr, encInstr{encStructTerminator, 0, 0, 0})
|
2010-06-28 14:09:47 -07:00
|
|
|
} else {
|
|
|
|
|
engine.instr = make([]encInstr, 1)
|
2011-02-25 09:45:06 -08:00
|
|
|
op, indir := enc.encOpFor(rt, seen)
|
|
|
|
|
engine.instr[0] = encInstr{*op, singletonField, indir, 0} // offset is zero
|
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.
|
2009-07-09 14:33:43 -07:00
|
|
|
// typeLock must be held (or we're in initialization and guaranteed single-threaded).
|
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
|
|
|
func (enc *Encoder) getEncEngine(ut *userTypeInfo) *encEngine {
|
|
|
|
|
info, err1 := getTypeInfo(ut)
|
2010-10-22 16:07:26 -07:00
|
|
|
if err1 != nil {
|
|
|
|
|
error(err1)
|
2009-07-29 17:24:25 -07:00
|
|
|
}
|
2009-07-09 14:33:43 -07:00
|
|
|
if info.encoder == nil {
|
|
|
|
|
// mark this engine as underway before compiling to handle recursive types.
|
2009-12-15 15:35:38 -08:00
|
|
|
info.encoder = new(encEngine)
|
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
|
|
|
info.encoder = enc.compileEnc(ut)
|
2009-07-01 18:25:13 -07:00
|
|
|
}
|
2010-10-22 16:07:26 -07:00
|
|
|
return info.encoder
|
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
|
|
|
// lockAndGetEncEngine is a function that locks and compiles.
|
|
|
|
|
// This lets us hold the lock only while compiling, not when encoding.
|
|
|
|
|
func (enc *Encoder) lockAndGetEncEngine(ut *userTypeInfo) *encEngine {
|
2010-10-22 16:07:26 -07:00
|
|
|
typeLock.Lock()
|
|
|
|
|
defer typeLock.Unlock()
|
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
|
|
|
return enc.getEncEngine(ut)
|
2010-10-22 16:07:26 -07:00
|
|
|
}
|
|
|
|
|
|
2011-03-16 18:03:13 -07:00
|
|
|
func (enc *Encoder) encode(b *bytes.Buffer, value reflect.Value, ut *userTypeInfo) {
|
|
|
|
|
defer catchError(&enc.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
|
|
|
engine := enc.lockAndGetEncEngine(ut)
|
|
|
|
|
indir := ut.indir
|
|
|
|
|
if ut.isGobEncoder {
|
|
|
|
|
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
|
|
|
}
|
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 !ut.isGobEncoder && value.Type().Kind() == reflect.Struct {
|
2011-04-18 14:36:22 -04:00
|
|
|
enc.encodeStruct(b, engine, unsafeAddr(value))
|
2010-10-22 16:07:26 -07:00
|
|
|
} else {
|
2011-04-18 14:36:22 -04:00
|
|
|
enc.encodeSingle(b, engine, unsafeAddr(value))
|
2010-06-28 14:09:47 -07:00
|
|
|
}
|
2009-07-01 18:25:13 -07:00
|
|
|
}
|