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-07-01 18:25:13 -07:00
|
|
|
"gob";
|
2009-06-29 15:15:07 -07:00
|
|
|
"io";
|
2009-06-30 15:37:46 -07:00
|
|
|
"math";
|
2009-06-29 15:15:07 -07:00
|
|
|
"os";
|
2009-07-01 18:25:13 -07:00
|
|
|
"reflect";
|
|
|
|
|
"sync";
|
2009-06-30 15:37:46 -07:00
|
|
|
"unsafe";
|
2009-06-29 15:15:07 -07:00
|
|
|
)
|
|
|
|
|
|
2009-06-30 16:20:31 -07:00
|
|
|
// 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-06-30 16:20:31 -07:00
|
|
|
type EncState struct {
|
|
|
|
|
w io.Writer;
|
|
|
|
|
err os.Error; // error encountered during encoding;
|
2009-07-01 18:25:13 -07:00
|
|
|
fieldnum int; // the last field number written.
|
2009-06-30 16:20:31 -07:00
|
|
|
buf [16]byte; // buffer used by the encoder; here to avoid allocation.
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-29 15:15:07 -07:00
|
|
|
// Integers encode as a variant of Google's protocol buffer varint (varvarint?).
|
|
|
|
|
// The variant is that the continuation bytes have a zero top bit instead of a one.
|
|
|
|
|
// That way there's only one bit to clear and the value is a little easier to see if
|
|
|
|
|
// you're the unfortunate sort of person who must read the hex to debug.
|
|
|
|
|
|
2009-06-30 16:20:31 -07:00
|
|
|
// EncodeUint writes an encoded unsigned integer to state.w. Sets state.err.
|
|
|
|
|
// If state.err is already non-nil, it does nothing.
|
|
|
|
|
func EncodeUint(state *EncState, x uint64) {
|
2009-06-29 15:15:07 -07:00
|
|
|
var n int;
|
2009-06-30 16:20:31 -07:00
|
|
|
if state.err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2009-06-29 15:15:07 -07:00
|
|
|
for n = 0; x > 127; n++ {
|
2009-06-30 16:20:31 -07:00
|
|
|
state.buf[n] = uint8(x & 0x7F);
|
2009-06-29 15:15:07 -07:00
|
|
|
x >>= 7;
|
|
|
|
|
}
|
2009-06-30 16:20:31 -07:00
|
|
|
state.buf[n] = 0x80 | uint8(x);
|
|
|
|
|
var nn int;
|
|
|
|
|
nn, state.err = state.w.Write(state.buf[0:n+1]);
|
2009-06-29 15:15:07 -07:00
|
|
|
}
|
|
|
|
|
|
2009-06-30 16:20:31 -07:00
|
|
|
// EncodeInt writes an encoded signed integer to state.w.
|
2009-06-29 15:15:07 -07:00
|
|
|
// The low bit of the encoding says whether to bit complement the (other bits of the) uint to recover the int.
|
2009-06-30 16:20:31 -07:00
|
|
|
// Sets state.err. If state.err is already non-nil, it does nothing.
|
|
|
|
|
func EncodeInt(state *EncState, i int64){
|
2009-06-29 15:15:07 -07:00
|
|
|
var x uint64;
|
|
|
|
|
if i < 0 {
|
|
|
|
|
x = uint64(^i << 1) | 1
|
|
|
|
|
} else {
|
|
|
|
|
x = uint64(i << 1)
|
|
|
|
|
}
|
2009-06-30 16:20:31 -07:00
|
|
|
EncodeUint(state, uint64(x))
|
2009-06-30 15:37:46 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-01 23:04:27 -07:00
|
|
|
type encInstr struct
|
|
|
|
|
type encOp func(i *encInstr, state *EncState, p unsafe.Pointer)
|
|
|
|
|
|
2009-06-30 15:37:46 -07:00
|
|
|
// The 'instructions' of the encoding machine
|
|
|
|
|
type encInstr struct {
|
2009-07-01 23:04:27 -07:00
|
|
|
op encOp;
|
2009-06-30 15:37:46 -07:00
|
|
|
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-07-02 16:43:46 -07:00
|
|
|
// Emit a field number and update the state to record its value for delta encoding.
|
|
|
|
|
// If the instruction pointer is nil, do nothing
|
|
|
|
|
func (state *EncState) update(instr *encInstr) {
|
|
|
|
|
if instr != nil {
|
|
|
|
|
EncodeUint(state, uint64(instr.field - state.fieldnum));
|
|
|
|
|
state.fieldnum = instr.field;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 16:20:31 -07:00
|
|
|
// Each encoder is responsible for handling any indirections associated
|
|
|
|
|
// with the data structure. If any pointer so reached is nil, no bytes are written.
|
|
|
|
|
// If the data item is zero, no bytes are written.
|
|
|
|
|
// 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 17:59:41 -07:00
|
|
|
func encIndirect(p unsafe.Pointer, indir int) unsafe.Pointer {
|
|
|
|
|
for ; indir > 0; indir-- {
|
2009-06-30 15:37:46 -07:00
|
|
|
p = *(*unsafe.Pointer)(p);
|
|
|
|
|
if p == nil {
|
2009-06-30 17:59:41 -07:00
|
|
|
return unsafe.Pointer(nil)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return p
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-01 23:04:27 -07:00
|
|
|
func encBool(i *encInstr, state *EncState, p unsafe.Pointer) {
|
2009-06-30 15:37:46 -07:00
|
|
|
b := *(*bool)(p);
|
|
|
|
|
if b {
|
2009-07-02 16:43:46 -07:00
|
|
|
state.update(i);
|
2009-06-30 16:20:31 -07:00
|
|
|
EncodeUint(state, 1);
|
2009-06-30 15:37:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-01 23:04:27 -07:00
|
|
|
func encInt(i *encInstr, state *EncState, p unsafe.Pointer) {
|
2009-06-30 15:37:46 -07:00
|
|
|
v := int64(*(*int)(p));
|
|
|
|
|
if v != 0 {
|
2009-07-02 16:43:46 -07:00
|
|
|
state.update(i);
|
2009-06-30 16:20:31 -07:00
|
|
|
EncodeInt(state, v);
|
2009-06-30 15:37:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-01 23:04:27 -07:00
|
|
|
func encUint(i *encInstr, state *EncState, p unsafe.Pointer) {
|
2009-06-30 15:37:46 -07:00
|
|
|
v := uint64(*(*uint)(p));
|
|
|
|
|
if v != 0 {
|
2009-07-02 16:43:46 -07:00
|
|
|
state.update(i);
|
2009-06-30 16:20:31 -07:00
|
|
|
EncodeUint(state, v);
|
2009-06-30 15:37:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-01 23:04:27 -07:00
|
|
|
func encInt8(i *encInstr, state *EncState, p unsafe.Pointer) {
|
2009-06-30 15:37:46 -07:00
|
|
|
v := int64(*(*int8)(p));
|
|
|
|
|
if v != 0 {
|
2009-07-02 16:43:46 -07:00
|
|
|
state.update(i);
|
2009-06-30 16:20:31 -07:00
|
|
|
EncodeInt(state, v);
|
2009-06-30 15:37:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-01 23:04:27 -07:00
|
|
|
func encUint8(i *encInstr, state *EncState, p unsafe.Pointer) {
|
2009-06-30 15:37:46 -07:00
|
|
|
v := uint64(*(*uint8)(p));
|
|
|
|
|
if v != 0 {
|
2009-07-02 16:43:46 -07:00
|
|
|
state.update(i);
|
2009-06-30 16:20:31 -07:00
|
|
|
EncodeUint(state, v);
|
2009-06-30 15:37:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-01 23:04:27 -07:00
|
|
|
func encInt16(i *encInstr, state *EncState, p unsafe.Pointer) {
|
2009-06-30 15:37:46 -07:00
|
|
|
v := int64(*(*int16)(p));
|
|
|
|
|
if v != 0 {
|
2009-07-02 16:43:46 -07:00
|
|
|
state.update(i);
|
2009-06-30 16:20:31 -07:00
|
|
|
EncodeInt(state, v);
|
2009-06-30 15:37:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-01 23:04:27 -07:00
|
|
|
func encUint16(i *encInstr, state *EncState, p unsafe.Pointer) {
|
2009-06-30 15:37:46 -07:00
|
|
|
v := uint64(*(*uint16)(p));
|
|
|
|
|
if v != 0 {
|
2009-07-02 16:43:46 -07:00
|
|
|
state.update(i);
|
2009-06-30 16:20:31 -07:00
|
|
|
EncodeUint(state, v);
|
2009-06-30 15:37:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-01 23:04:27 -07:00
|
|
|
func encInt32(i *encInstr, state *EncState, p unsafe.Pointer) {
|
2009-06-30 15:37:46 -07:00
|
|
|
v := int64(*(*int32)(p));
|
|
|
|
|
if v != 0 {
|
2009-07-02 16:43:46 -07:00
|
|
|
state.update(i);
|
2009-06-30 16:20:31 -07:00
|
|
|
EncodeInt(state, v);
|
2009-06-30 15:37:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-01 23:04:27 -07:00
|
|
|
func encUint32(i *encInstr, state *EncState, p unsafe.Pointer) {
|
2009-06-30 15:37:46 -07:00
|
|
|
v := uint64(*(*uint32)(p));
|
|
|
|
|
if v != 0 {
|
2009-07-02 16:43:46 -07:00
|
|
|
state.update(i);
|
2009-06-30 16:20:31 -07:00
|
|
|
EncodeUint(state, v);
|
2009-06-30 15:37:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-01 23:04:27 -07:00
|
|
|
func encInt64(i *encInstr, state *EncState, p unsafe.Pointer) {
|
2009-06-30 15:37:46 -07:00
|
|
|
v := *(*int64)(p);
|
|
|
|
|
if v != 0 {
|
2009-07-02 16:43:46 -07:00
|
|
|
state.update(i);
|
2009-06-30 16:20:31 -07:00
|
|
|
EncodeInt(state, v);
|
2009-06-30 15:37:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-01 23:04:27 -07:00
|
|
|
func encUint64(i *encInstr, state *EncState, p unsafe.Pointer) {
|
2009-06-30 15:37:46 -07:00
|
|
|
v := *(*uint64)(p);
|
|
|
|
|
if v != 0 {
|
2009-07-02 16:43:46 -07:00
|
|
|
state.update(i);
|
2009-06-30 16:20:31 -07:00
|
|
|
EncodeUint(state, v);
|
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 {
|
|
|
|
|
u := math.Float64bits(f);
|
|
|
|
|
var v uint64;
|
|
|
|
|
for i := 0; i < 8; i++ {
|
|
|
|
|
v <<= 8;
|
|
|
|
|
v |= u & 0xFF;
|
|
|
|
|
u >>= 8;
|
|
|
|
|
}
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-01 23:04:27 -07:00
|
|
|
func encFloat(i *encInstr, state *EncState, p unsafe.Pointer) {
|
2009-06-30 15:37:46 -07:00
|
|
|
f := float(*(*float)(p));
|
|
|
|
|
if f != 0 {
|
|
|
|
|
v := floatBits(float64(f));
|
2009-07-02 16:43:46 -07:00
|
|
|
state.update(i);
|
2009-06-30 16:20:31 -07:00
|
|
|
EncodeUint(state, v);
|
2009-06-30 15:37:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-01 23:04:27 -07:00
|
|
|
func encFloat32(i *encInstr, state *EncState, p unsafe.Pointer) {
|
2009-06-30 15:37:46 -07:00
|
|
|
f := float32(*(*float32)(p));
|
|
|
|
|
if f != 0 {
|
|
|
|
|
v := floatBits(float64(f));
|
2009-07-02 16:43:46 -07:00
|
|
|
state.update(i);
|
2009-06-30 16:20:31 -07:00
|
|
|
EncodeUint(state, v);
|
2009-06-30 15:37:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-01 23:04:27 -07:00
|
|
|
func encFloat64(i *encInstr, state *EncState, p unsafe.Pointer) {
|
2009-06-30 15:37:46 -07:00
|
|
|
f := *(*float64)(p);
|
|
|
|
|
if f != 0 {
|
2009-07-02 16:43:46 -07:00
|
|
|
state.update(i);
|
2009-06-30 15:37:46 -07:00
|
|
|
v := floatBits(f);
|
2009-06-30 16:20:31 -07:00
|
|
|
EncodeUint(state, v);
|
2009-06-30 15:37:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
2009-07-01 18:25:13 -07:00
|
|
|
|
2009-07-02 11:21:01 -07:00
|
|
|
// Byte arrays are encoded as an unsigned count followed by the raw bytes.
|
|
|
|
|
func encUint8Array(i *encInstr, state *EncState, p unsafe.Pointer) {
|
|
|
|
|
b := *(*[]byte)(p);
|
|
|
|
|
if len(b) > 0 {
|
2009-07-02 16:43:46 -07:00
|
|
|
state.update(i);
|
2009-07-02 11:21:01 -07:00
|
|
|
EncodeUint(state, uint64(len(b)));
|
|
|
|
|
state.w.Write(b);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Strings are encoded as an unsigned count followed by the raw bytes.
|
|
|
|
|
func encString(i *encInstr, state *EncState, p unsafe.Pointer) {
|
|
|
|
|
s := *(*string)(p);
|
|
|
|
|
if len(s) > 0 {
|
2009-07-02 16:43:46 -07:00
|
|
|
state.update(i);
|
2009-07-02 11:21:01 -07:00
|
|
|
EncodeUint(state, uint64(len(s)));
|
|
|
|
|
io.WriteString(state.w, s);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-01 18:25:13 -07:00
|
|
|
// The end of a struct is marked by a delta field number of 0.
|
2009-07-01 23:04:27 -07:00
|
|
|
func encStructTerminator(i *encInstr, state *EncState, p unsafe.Pointer) {
|
2009-07-01 18:25:13 -07:00
|
|
|
EncodeUint(state, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Execution engine
|
|
|
|
|
|
|
|
|
|
// The encoder engine is an array of instructions indexed by field number of the encoding
|
|
|
|
|
// data, typically a struct. It is executed top to bottom, walking the struct.
|
|
|
|
|
type encEngine struct {
|
|
|
|
|
instr []encInstr
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-02 16:43:46 -07:00
|
|
|
func encodeStruct(engine *encEngine, w io.Writer, basep uintptr) os.Error {
|
2009-07-02 13:43:47 -07:00
|
|
|
state := new(EncState);
|
|
|
|
|
state.w = w;
|
|
|
|
|
state.fieldnum = -1;
|
|
|
|
|
for i := 0; i < len(engine.instr); i++ {
|
|
|
|
|
instr := &engine.instr[i];
|
2009-07-02 16:43:46 -07:00
|
|
|
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 {
|
|
|
|
|
state.fieldnum = i;
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
instr.op(instr, state, p);
|
|
|
|
|
if state.err != nil {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return state.err
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-07 11:04:42 -07:00
|
|
|
func encodeArray(w io.Writer, p uintptr, op encOp, elemWid uintptr, length int, elemIndir int) os.Error {
|
2009-07-02 16:43:46 -07:00
|
|
|
state := new(EncState);
|
|
|
|
|
state.w = w;
|
|
|
|
|
state.fieldnum = -1;
|
|
|
|
|
EncodeUint(state, uint64(length));
|
|
|
|
|
for i := 0; i < length && state.err == nil; i++ {
|
2009-07-02 17:21:48 -07:00
|
|
|
up := unsafe.Pointer(p);
|
|
|
|
|
if elemIndir > 0 {
|
|
|
|
|
if up = encIndirect(up, elemIndir); up == nil {
|
|
|
|
|
state.err = os.ErrorString("encodeArray: nil element");
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
p = uintptr(up);
|
|
|
|
|
}
|
|
|
|
|
op(nil, state, unsafe.Pointer(p));
|
2009-07-02 16:43:46 -07:00
|
|
|
p += uintptr(elemWid);
|
|
|
|
|
}
|
|
|
|
|
return state.err
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-01 18:25:13 -07:00
|
|
|
var encEngineMap = make(map[reflect.Type] *encEngine)
|
2009-07-07 11:04:42 -07:00
|
|
|
var encOpMap = map[reflect.Type] encOp {
|
|
|
|
|
reflect.Typeof((*reflect.BoolType)(nil)): encBool,
|
|
|
|
|
reflect.Typeof((*reflect.IntType)(nil)): encInt,
|
|
|
|
|
reflect.Typeof((*reflect.Int8Type)(nil)): encInt8,
|
|
|
|
|
reflect.Typeof((*reflect.Int16Type)(nil)): encInt16,
|
|
|
|
|
reflect.Typeof((*reflect.Int32Type)(nil)): encInt32,
|
|
|
|
|
reflect.Typeof((*reflect.Int64Type)(nil)): encInt64,
|
|
|
|
|
reflect.Typeof((*reflect.UintType)(nil)): encUint,
|
|
|
|
|
reflect.Typeof((*reflect.Uint8Type)(nil)): encUint8,
|
|
|
|
|
reflect.Typeof((*reflect.Uint16Type)(nil)): encUint16,
|
|
|
|
|
reflect.Typeof((*reflect.Uint32Type)(nil)): encUint32,
|
|
|
|
|
reflect.Typeof((*reflect.Uint64Type)(nil)): encUint64,
|
|
|
|
|
reflect.Typeof((*reflect.FloatType)(nil)): encFloat,
|
|
|
|
|
reflect.Typeof((*reflect.Float32Type)(nil)): encFloat32,
|
|
|
|
|
reflect.Typeof((*reflect.Float64Type)(nil)): encFloat64,
|
|
|
|
|
reflect.Typeof((*reflect.StringType)(nil)): encString,
|
2009-07-02 11:21:01 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-02 13:43:47 -07:00
|
|
|
func getEncEngine(rt reflect.Type) *encEngine
|
|
|
|
|
|
2009-07-02 11:21:01 -07:00
|
|
|
func encOpFor(typ reflect.Type) encOp {
|
2009-07-07 11:04:42 -07:00
|
|
|
op, ok := encOpMap[reflect.Typeof(typ)];
|
2009-07-02 11:21:01 -07:00
|
|
|
if !ok {
|
|
|
|
|
// Special cases
|
2009-07-07 11:04:42 -07:00
|
|
|
switch t := typ.(type) {
|
|
|
|
|
case *reflect.SliceType:
|
|
|
|
|
if _, ok := t.Elem().(*reflect.Uint8Type); ok {
|
|
|
|
|
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.
|
|
|
|
|
elemOp := encOpFor(t.Elem());
|
|
|
|
|
_, indir := indirect(t.Elem());
|
|
|
|
|
op = func(i *encInstr, state *EncState, p unsafe.Pointer) {
|
|
|
|
|
slice := (*reflect.SliceHeader)(p);
|
|
|
|
|
if slice.Len == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
state.update(i);
|
|
|
|
|
state.err = encodeArray(state.w, slice.Data, elemOp, t.Elem().Size(), int(slice.Len), indir);
|
|
|
|
|
};
|
|
|
|
|
case *reflect.ArrayType:
|
|
|
|
|
// True arrays have size in the type.
|
|
|
|
|
elemOp := encOpFor(t.Elem());
|
|
|
|
|
_, indir := indirect(t.Elem());
|
|
|
|
|
op = func(i *encInstr, state *EncState, p unsafe.Pointer) {
|
|
|
|
|
state.update(i);
|
|
|
|
|
state.err = encodeArray(state.w, uintptr(p), elemOp, t.Elem().Size(), t.Len(), indir);
|
|
|
|
|
};
|
|
|
|
|
case *reflect.StructType:
|
2009-07-02 13:43:47 -07:00
|
|
|
// Generate a closure that calls out to the engine for the nested type.
|
|
|
|
|
engine := getEncEngine(typ);
|
|
|
|
|
op = func(i *encInstr, state *EncState, p unsafe.Pointer) {
|
2009-07-02 16:43:46 -07:00
|
|
|
state.update(i);
|
|
|
|
|
state.err = encodeStruct(engine, state.w, uintptr(p));
|
2009-07-02 13:43:47 -07:00
|
|
|
};
|
|
|
|
|
}
|
2009-07-02 11:21:01 -07:00
|
|
|
}
|
|
|
|
|
if op == nil {
|
|
|
|
|
panicln("encode can't handle type", typ.String());
|
|
|
|
|
}
|
|
|
|
|
return op
|
2009-07-01 18:25:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The local Type was compiled from the actual value, so we know
|
|
|
|
|
// it's compatible.
|
|
|
|
|
// TODO(r): worth checking? typ is unused here.
|
|
|
|
|
func compileEnc(rt reflect.Type, typ Type) *encEngine {
|
2009-07-07 11:04:42 -07:00
|
|
|
srt, ok := rt.(*reflect.StructType);
|
2009-07-01 18:25:13 -07:00
|
|
|
if !ok {
|
|
|
|
|
panicln("TODO: can't handle non-structs");
|
|
|
|
|
}
|
|
|
|
|
engine := new(encEngine);
|
2009-07-07 11:04:42 -07:00
|
|
|
engine.instr = make([]encInstr, srt.NumField()+1); // +1 for terminator
|
|
|
|
|
for fieldnum := 0; fieldnum < srt.NumField(); fieldnum++ {
|
|
|
|
|
f := srt.Field(fieldnum);
|
|
|
|
|
ftyp, indir := indirect(f.Type);
|
2009-07-02 11:21:01 -07:00
|
|
|
op := encOpFor(ftyp);
|
2009-07-07 11:04:42 -07:00
|
|
|
engine.instr[fieldnum] = encInstr{op, fieldnum, indir, uintptr(f.Offset)};
|
2009-07-01 18:25:13 -07:00
|
|
|
}
|
2009-07-07 11:04:42 -07:00
|
|
|
engine.instr[srt.NumField()] = encInstr{encStructTerminator, 0, 0, 0};
|
2009-07-01 18:25:13 -07:00
|
|
|
return engine;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// typeLock must be held.
|
|
|
|
|
func getEncEngine(rt reflect.Type) *encEngine {
|
|
|
|
|
engine, ok := encEngineMap[rt];
|
|
|
|
|
if !ok {
|
2009-07-07 11:04:42 -07:00
|
|
|
pkg, name := rt.Name();
|
|
|
|
|
engine = compileEnc(rt, newType(name, rt));
|
2009-07-01 18:25:13 -07:00
|
|
|
encEngineMap[rt] = engine;
|
|
|
|
|
}
|
|
|
|
|
return engine
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Encode(w io.Writer, e interface{}) os.Error {
|
|
|
|
|
// Dereference down to the underlying object.
|
2009-07-02 17:21:48 -07:00
|
|
|
rt, indir := indirect(reflect.Typeof(e));
|
2009-07-01 18:25:13 -07:00
|
|
|
v := reflect.NewValue(e);
|
2009-07-02 17:21:48 -07:00
|
|
|
for i := 0; i < indir; i++ {
|
2009-07-01 18:25:13 -07:00
|
|
|
v = reflect.Indirect(v);
|
|
|
|
|
}
|
2009-07-07 11:04:42 -07:00
|
|
|
if _, ok := v.(*reflect.StructValue); !ok {
|
2009-07-02 17:21:48 -07:00
|
|
|
return os.ErrorString("encode can't handle " + v.Type().String())
|
2009-07-02 13:43:47 -07:00
|
|
|
}
|
2009-07-01 18:25:13 -07:00
|
|
|
typeLock.Lock();
|
|
|
|
|
engine := getEncEngine(rt);
|
|
|
|
|
typeLock.Unlock();
|
2009-07-07 11:04:42 -07:00
|
|
|
return encodeStruct(engine, w, v.Addr());
|
2009-07-01 18:25:13 -07:00
|
|
|
}
|