The Go Programming Language

Source file src/pkg/gob/encode.go

     1	// Copyright 2009 The Go Authors. All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	package gob
     6	
     7	import (
     8		"bytes"
     9		"math"
    10		"reflect"
    11		"unsafe"
    12	)
    13	
    14	const uint64Size = int(unsafe.Sizeof(uint64(0)))
    15	
    16	// encoderState is the global execution state of an instance of the encoder.
    17	// Field numbers are delta encoded and always increase. The field
    18	// number is initialized to -1 so 0 comes out as delta(1). A delta of
    19	// 0 terminates the structure.
    20	type encoderState struct {
    21		enc      *Encoder
    22		b        *bytes.Buffer
    23		sendZero bool                 // encoding an array element or map key/value pair; send zero values
    24		fieldnum int                  // the last field number written.
    25		buf      [1 + uint64Size]byte // buffer used by the encoder; here to avoid allocation.
    26		next     *encoderState        // for free list
    27	}
    28	
    29	func (enc *Encoder) newEncoderState(b *bytes.Buffer) *encoderState {
    30		e := enc.freeList
    31		if e == nil {
    32			e = new(encoderState)
    33			e.enc = enc
    34		} else {
    35			enc.freeList = e.next
    36		}
    37		e.sendZero = false
    38		e.fieldnum = 0
    39		e.b = b
    40		return e
    41	}
    42	
    43	func (enc *Encoder) freeEncoderState(e *encoderState) {
    44		e.next = enc.freeList
    45		enc.freeList = e
    46	}
    47	
    48	// Unsigned integers have a two-state encoding.  If the number is less
    49	// than 128 (0 through 0x7F), its value is written directly.
    50	// Otherwise the value is written in big-endian byte order preceded
    51	// by the byte length, negated.
    52	
    53	// encodeUint writes an encoded unsigned integer to state.b.
    54	func (state *encoderState) encodeUint(x uint64) {
    55		if x <= 0x7F {
    56			err := state.b.WriteByte(uint8(x))
    57			if err != nil {
    58				error(err)
    59			}
    60			return
    61		}
    62		var n, m int
    63		m = uint64Size
    64		for n = 1; x > 0; n++ {
    65			state.buf[m] = uint8(x)
    66			x >>= 8
    67			m--
    68		}
    69		state.buf[m] = uint8(-(n - 1))
    70		n, err := state.b.Write(state.buf[m : uint64Size+1])
    71		if err != nil {
    72			error(err)
    73		}
    74	}
    75	
    76	// encodeInt writes an encoded signed integer to state.w.
    77	// The low bit of the encoding says whether to bit complement the (other bits of the)
    78	// uint to recover the int.
    79	func (state *encoderState) encodeInt(i int64) {
    80		var x uint64
    81		if i < 0 {
    82			x = uint64(^i<<1) | 1
    83		} else {
    84			x = uint64(i << 1)
    85		}
    86		state.encodeUint(uint64(x))
    87	}
    88	
    89	// encOp is the signature of an encoding operator for a given type.
    90	type encOp func(i *encInstr, state *encoderState, p unsafe.Pointer)
    91	
    92	// The 'instructions' of the encoding machine
    93	type encInstr struct {
    94		op     encOp
    95		field  int     // field number
    96		indir  int     // how many pointer indirections to reach the value in the struct
    97		offset uintptr // offset in the structure of the field to encode
    98	}
    99	
   100	// update emits a field number and updates the state to record its value for delta encoding.
   101	// If the instruction pointer is nil, it does nothing
   102	func (state *encoderState) update(instr *encInstr) {
   103		if instr != nil {
   104			state.encodeUint(uint64(instr.field - state.fieldnum))
   105			state.fieldnum = instr.field
   106		}
   107	}
   108	
   109	// Each encoder for a composite is responsible for handling any
   110	// indirections associated with the elements of the data structure.
   111	// If any pointer so reached is nil, no bytes are written.  If the
   112	// data item is zero, no bytes are written.  Single values - ints,
   113	// strings etc. - are indirected before calling their encoders.
   114	// Otherwise, the output (for a scalar) is the field number, as an
   115	// encoded integer, followed by the field data in its appropriate
   116	// format.
   117	
   118	// encIndirect dereferences p indir times and returns the result.
   119	func encIndirect(p unsafe.Pointer, indir int) unsafe.Pointer {
   120		for ; indir > 0; indir-- {
   121			p = *(*unsafe.Pointer)(p)
   122			if p == nil {
   123				return unsafe.Pointer(nil)
   124			}
   125		}
   126		return p
   127	}
   128	
   129	// encBool encodes the bool with address p as an unsigned 0 or 1.
   130	func encBool(i *encInstr, state *encoderState, p unsafe.Pointer) {
   131		b := *(*bool)(p)
   132		if b || state.sendZero {
   133			state.update(i)
   134			if b {
   135				state.encodeUint(1)
   136			} else {
   137				state.encodeUint(0)
   138			}
   139		}
   140	}
   141	
   142	// encInt encodes the int with address p.
   143	func encInt(i *encInstr, state *encoderState, p unsafe.Pointer) {
   144		v := int64(*(*int)(p))
   145		if v != 0 || state.sendZero {
   146			state.update(i)
   147			state.encodeInt(v)
   148		}
   149	}
   150	
   151	// encUint encodes the uint with address p.
   152	func encUint(i *encInstr, state *encoderState, p unsafe.Pointer) {
   153		v := uint64(*(*uint)(p))
   154		if v != 0 || state.sendZero {
   155			state.update(i)
   156			state.encodeUint(v)
   157		}
   158	}
   159	
   160	// encInt8 encodes the int8 with address p.
   161	func encInt8(i *encInstr, state *encoderState, p unsafe.Pointer) {
   162		v := int64(*(*int8)(p))
   163		if v != 0 || state.sendZero {
   164			state.update(i)
   165			state.encodeInt(v)
   166		}
   167	}
   168	
   169	// encUint8 encodes the uint8 with address p.
   170	func encUint8(i *encInstr, state *encoderState, p unsafe.Pointer) {
   171		v := uint64(*(*uint8)(p))
   172		if v != 0 || state.sendZero {
   173			state.update(i)
   174			state.encodeUint(v)
   175		}
   176	}
   177	
   178	// encInt16 encodes the int16 with address p.
   179	func encInt16(i *encInstr, state *encoderState, p unsafe.Pointer) {
   180		v := int64(*(*int16)(p))
   181		if v != 0 || state.sendZero {
   182			state.update(i)
   183			state.encodeInt(v)
   184		}
   185	}
   186	
   187	// encUint16 encodes the uint16 with address p.
   188	func encUint16(i *encInstr, state *encoderState, p unsafe.Pointer) {
   189		v := uint64(*(*uint16)(p))
   190		if v != 0 || state.sendZero {
   191			state.update(i)
   192			state.encodeUint(v)
   193		}
   194	}
   195	
   196	// encInt32 encodes the int32 with address p.
   197	func encInt32(i *encInstr, state *encoderState, p unsafe.Pointer) {
   198		v := int64(*(*int32)(p))
   199		if v != 0 || state.sendZero {
   200			state.update(i)
   201			state.encodeInt(v)
   202		}
   203	}
   204	
   205	// encUint encodes the uint32 with address p.
   206	func encUint32(i *encInstr, state *encoderState, p unsafe.Pointer) {
   207		v := uint64(*(*uint32)(p))
   208		if v != 0 || state.sendZero {
   209			state.update(i)
   210			state.encodeUint(v)
   211		}
   212	}
   213	
   214	// encInt64 encodes the int64 with address p.
   215	func encInt64(i *encInstr, state *encoderState, p unsafe.Pointer) {
   216		v := *(*int64)(p)
   217		if v != 0 || state.sendZero {
   218			state.update(i)
   219			state.encodeInt(v)
   220		}
   221	}
   222	
   223	// encInt64 encodes the uint64 with address p.
   224	func encUint64(i *encInstr, state *encoderState, p unsafe.Pointer) {
   225		v := *(*uint64)(p)
   226		if v != 0 || state.sendZero {
   227			state.update(i)
   228			state.encodeUint(v)
   229		}
   230	}
   231	
   232	// encUintptr encodes the uintptr with address p.
   233	func encUintptr(i *encInstr, state *encoderState, p unsafe.Pointer) {
   234		v := uint64(*(*uintptr)(p))
   235		if v != 0 || state.sendZero {
   236			state.update(i)
   237			state.encodeUint(v)
   238		}
   239	}
   240	
   241	// floatBits returns a uint64 holding the bits of a floating-point number.
   242	// Floating-point numbers are transmitted as uint64s holding the bits
   243	// of the underlying representation.  They are sent byte-reversed, with
   244	// the exponent end coming out first, so integer floating point numbers
   245	// (for example) transmit more compactly.  This routine does the
   246	// swizzling.
   247	func floatBits(f float64) uint64 {
   248		u := math.Float64bits(f)
   249		var v uint64
   250		for i := 0; i < 8; i++ {
   251			v <<= 8
   252			v |= u & 0xFF
   253			u >>= 8
   254		}
   255		return v
   256	}
   257	
   258	// encFloat32 encodes the float32 with address p.
   259	func encFloat32(i *encInstr, state *encoderState, p unsafe.Pointer) {
   260		f := *(*float32)(p)
   261		if f != 0 || state.sendZero {
   262			v := floatBits(float64(f))
   263			state.update(i)
   264			state.encodeUint(v)
   265		}
   266	}
   267	
   268	// encFloat64 encodes the float64 with address p.
   269	func encFloat64(i *encInstr, state *encoderState, p unsafe.Pointer) {
   270		f := *(*float64)(p)
   271		if f != 0 || state.sendZero {
   272			state.update(i)
   273			v := floatBits(f)
   274			state.encodeUint(v)
   275		}
   276	}
   277	
   278	// encComplex64 encodes the complex64 with address p.
   279	// Complex numbers are just a pair of floating-point numbers, real part first.
   280	func encComplex64(i *encInstr, state *encoderState, p unsafe.Pointer) {
   281		c := *(*complex64)(p)
   282		if c != 0+0i || state.sendZero {
   283			rpart := floatBits(float64(real(c)))
   284			ipart := floatBits(float64(imag(c)))
   285			state.update(i)
   286			state.encodeUint(rpart)
   287			state.encodeUint(ipart)
   288		}
   289	}
   290	
   291	// encComplex128 encodes the complex128 with address p.
   292	func encComplex128(i *encInstr, state *encoderState, p unsafe.Pointer) {
   293		c := *(*complex128)(p)
   294		if c != 0+0i || state.sendZero {
   295			rpart := floatBits(real(c))
   296			ipart := floatBits(imag(c))
   297			state.update(i)
   298			state.encodeUint(rpart)
   299			state.encodeUint(ipart)
   300		}
   301	}
   302	
   303	// encUint8Array encodes the byte slice whose header has address p.
   304	// Byte arrays are encoded as an unsigned count followed by the raw bytes.
   305	func encUint8Array(i *encInstr, state *encoderState, p unsafe.Pointer) {
   306		b := *(*[]byte)(p)
   307		if len(b) > 0 || state.sendZero {
   308			state.update(i)
   309			state.encodeUint(uint64(len(b)))
   310			state.b.Write(b)
   311		}
   312	}
   313	
   314	// encString encodes the string whose header has address p.
   315	// Strings are encoded as an unsigned count followed by the raw bytes.
   316	func encString(i *encInstr, state *encoderState, p unsafe.Pointer) {
   317		s := *(*string)(p)
   318		if len(s) > 0 || state.sendZero {
   319			state.update(i)
   320			state.encodeUint(uint64(len(s)))
   321			state.b.WriteString(s)
   322		}
   323	}
   324	
   325	// encStructTerminator encodes the end of an encoded struct
   326	// as delta field number of 0.
   327	func encStructTerminator(i *encInstr, state *encoderState, p unsafe.Pointer) {
   328		state.encodeUint(0)
   329	}
   330	
   331	// Execution engine
   332	
   333	// encEngine an array of instructions indexed by field number of the encoding
   334	// data, typically a struct.  It is executed top to bottom, walking the struct.
   335	type encEngine struct {
   336		instr []encInstr
   337	}
   338	
   339	const singletonField = 0
   340	
   341	// encodeSingle encodes a single top-level non-struct value.
   342	func (enc *Encoder) encodeSingle(b *bytes.Buffer, engine *encEngine, basep uintptr) {
   343		state := enc.newEncoderState(b)
   344		state.fieldnum = singletonField
   345		// There is no surrounding struct to frame the transmission, so we must
   346		// generate data even if the item is zero.  To do this, set sendZero.
   347		state.sendZero = true
   348		instr := &engine.instr[singletonField]
   349		p := unsafe.Pointer(basep) // offset will be zero
   350		if instr.indir > 0 {
   351			if p = encIndirect(p, instr.indir); p == nil {
   352				return
   353			}
   354		}
   355		instr.op(instr, state, p)
   356		enc.freeEncoderState(state)
   357	}
   358	
   359	// encodeStruct encodes a single struct value.
   360	func (enc *Encoder) encodeStruct(b *bytes.Buffer, engine *encEngine, basep uintptr) {
   361		state := enc.newEncoderState(b)
   362		state.fieldnum = -1
   363		for i := 0; i < len(engine.instr); i++ {
   364			instr := &engine.instr[i]
   365			p := unsafe.Pointer(basep + instr.offset)
   366			if instr.indir > 0 {
   367				if p = encIndirect(p, instr.indir); p == nil {
   368					continue
   369				}
   370			}
   371			instr.op(instr, state, p)
   372		}
   373		enc.freeEncoderState(state)
   374	}
   375	
   376	// encodeArray encodes the array whose 0th element is at p.
   377	func (enc *Encoder) encodeArray(b *bytes.Buffer, p uintptr, op encOp, elemWid uintptr, elemIndir int, length int) {
   378		state := enc.newEncoderState(b)
   379		state.fieldnum = -1
   380		state.sendZero = true
   381		state.encodeUint(uint64(length))
   382		for i := 0; i < length; i++ {
   383			elemp := p
   384			up := unsafe.Pointer(elemp)
   385			if elemIndir > 0 {
   386				if up = encIndirect(up, elemIndir); up == nil {
   387					errorf("encodeArray: nil element")
   388				}
   389				elemp = uintptr(up)
   390			}
   391			op(nil, state, unsafe.Pointer(elemp))
   392			p += uintptr(elemWid)
   393		}
   394		enc.freeEncoderState(state)
   395	}
   396	
   397	// encodeReflectValue is a helper for maps. It encodes the value v.
   398	func encodeReflectValue(state *encoderState, v reflect.Value, op encOp, indir int) {
   399		for i := 0; i < indir && v.IsValid(); i++ {
   400			v = reflect.Indirect(v)
   401		}
   402		if !v.IsValid() {
   403			errorf("encodeReflectValue: nil element")
   404		}
   405		op(nil, state, unsafe.Pointer(unsafeAddr(v)))
   406	}
   407	
   408	// encodeMap encodes a map as unsigned count followed by key:value pairs.
   409	// Because map internals are not exposed, we must use reflection rather than
   410	// addresses.
   411	func (enc *Encoder) encodeMap(b *bytes.Buffer, mv reflect.Value, keyOp, elemOp encOp, keyIndir, elemIndir int) {
   412		state := enc.newEncoderState(b)
   413		state.fieldnum = -1
   414		state.sendZero = true
   415		keys := mv.MapKeys()
   416		state.encodeUint(uint64(len(keys)))
   417		for _, key := range keys {
   418			encodeReflectValue(state, key, keyOp, keyIndir)
   419			encodeReflectValue(state, mv.MapIndex(key), elemOp, elemIndir)
   420		}
   421		enc.freeEncoderState(state)
   422	}
   423	
   424	// encodeInterface encodes the interface value iv.
   425	// To send an interface, we send a string identifying the concrete type, followed
   426	// by the type identifier (which might require defining that type right now), followed
   427	// by the concrete value.  A nil value gets sent as the empty string for the name,
   428	// followed by no value.
   429	func (enc *Encoder) encodeInterface(b *bytes.Buffer, iv reflect.Value) {
   430		state := enc.newEncoderState(b)
   431		state.fieldnum = -1
   432		state.sendZero = true
   433		if iv.IsNil() {
   434			state.encodeUint(0)
   435			return
   436		}
   437	
   438		ut := userType(iv.Elem().Type())
   439		name, ok := concreteTypeToName[ut.base]
   440		if !ok {
   441			errorf("type not registered for interface: %s", ut.base)
   442		}
   443		// Send the name.
   444		state.encodeUint(uint64(len(name)))
   445		_, err := state.b.WriteString(name)
   446		if err != nil {
   447			error(err)
   448		}
   449		// Define the type id if necessary.
   450		enc.sendTypeDescriptor(enc.writer(), state, ut)
   451		// Send the type id.
   452		enc.sendTypeId(state, ut)
   453		// Encode the value into a new buffer.  Any nested type definitions
   454		// should be written to b, before the encoded value.
   455		enc.pushWriter(b)
   456		data := new(bytes.Buffer)
   457		enc.encode(data, iv.Elem(), ut)
   458		if enc.err != nil {
   459			error(enc.err)
   460		}
   461		enc.popWriter()
   462		enc.writeMessage(b, data)
   463		if enc.err != nil {
   464			error(err)
   465		}
   466		enc.freeEncoderState(state)
   467	}
   468	
   469	// isZero returns whether the value is the zero of its type.
   470	func isZero(val reflect.Value) bool {
   471		switch val.Kind() {
   472		case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
   473			return val.Len() == 0
   474		case reflect.Bool:
   475			return !val.Bool()
   476		case reflect.Complex64, reflect.Complex128:
   477			return val.Complex() == 0
   478		case reflect.Chan, reflect.Func, reflect.Ptr:
   479			return val.IsNil()
   480		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   481			return val.Int() == 0
   482		case reflect.Float32, reflect.Float64:
   483			return val.Float() == 0
   484		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
   485			return val.Uint() == 0
   486		}
   487		panic("unknown type in isZero " + val.Type().String())
   488	}
   489	
   490	// encGobEncoder encodes a value that implements the GobEncoder interface.
   491	// The data is sent as a byte array.
   492	func (enc *Encoder) encodeGobEncoder(b *bytes.Buffer, v reflect.Value) {
   493		// TODO: should we catch panics from the called method?
   494		// We know it's a GobEncoder, so just call the method directly.
   495		data, err := v.Interface().(GobEncoder).GobEncode()
   496		if err != nil {
   497			error(err)
   498		}
   499		state := enc.newEncoderState(b)
   500		state.fieldnum = -1
   501		state.encodeUint(uint64(len(data)))
   502		state.b.Write(data)
   503		enc.freeEncoderState(state)
   504	}
   505	
   506	var encOpTable = [...]encOp{
   507		reflect.Bool:       encBool,
   508		reflect.Int:        encInt,
   509		reflect.Int8:       encInt8,
   510		reflect.Int16:      encInt16,
   511		reflect.Int32:      encInt32,
   512		reflect.Int64:      encInt64,
   513		reflect.Uint:       encUint,
   514		reflect.Uint8:      encUint8,
   515		reflect.Uint16:     encUint16,
   516		reflect.Uint32:     encUint32,
   517		reflect.Uint64:     encUint64,
   518		reflect.Uintptr:    encUintptr,
   519		reflect.Float32:    encFloat32,
   520		reflect.Float64:    encFloat64,
   521		reflect.Complex64:  encComplex64,
   522		reflect.Complex128: encComplex128,
   523		reflect.String:     encString,
   524	}
   525	
   526	// encOpFor returns (a pointer to) the encoding op for the base type under rt and
   527	// the indirection count to reach it.
   528	func (enc *Encoder) encOpFor(rt reflect.Type, inProgress map[reflect.Type]*encOp) (*encOp, int) {
   529		ut := userType(rt)
   530		// If the type implements GobEncoder, we handle it without further processing.
   531		if ut.isGobEncoder {
   532			return enc.gobEncodeOpFor(ut)
   533		}
   534		// If this type is already in progress, it's a recursive type (e.g. map[string]*T).
   535		// Return the pointer to the op we're already building.
   536		if opPtr := inProgress[rt]; opPtr != nil {
   537			return opPtr, ut.indir
   538		}
   539		typ := ut.base
   540		indir := ut.indir
   541		k := typ.Kind()
   542		var op encOp
   543		if int(k) < len(encOpTable) {
   544			op = encOpTable[k]
   545		}
   546		if op == nil {
   547			inProgress[rt] = &op
   548			// Special cases
   549			switch t := typ; t.Kind() {
   550			case reflect.Slice:
   551				if t.Elem().Kind() == reflect.Uint8 {
   552					op = encUint8Array
   553					break
   554				}
   555				// Slices have a header; we decode it to find the underlying array.
   556				elemOp, indir := enc.encOpFor(t.Elem(), inProgress)
   557				op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
   558					slice := (*reflect.SliceHeader)(p)
   559					if !state.sendZero && slice.Len == 0 {
   560						return
   561					}
   562					state.update(i)
   563					state.enc.encodeArray(state.b, slice.Data, *elemOp, t.Elem().Size(), indir, int(slice.Len))
   564				}
   565			case reflect.Array:
   566				// True arrays have size in the type.
   567				elemOp, indir := enc.encOpFor(t.Elem(), inProgress)
   568				op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
   569					state.update(i)
   570					state.enc.encodeArray(state.b, uintptr(p), *elemOp, t.Elem().Size(), indir, t.Len())
   571				}
   572			case reflect.Map:
   573				keyOp, keyIndir := enc.encOpFor(t.Key(), inProgress)
   574				elemOp, elemIndir := enc.encOpFor(t.Elem(), inProgress)
   575				op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
   576					// Maps cannot be accessed by moving addresses around the way
   577					// that slices etc. can.  We must recover a full reflection value for
   578					// the iteration.
   579					v := reflect.ValueOf(unsafe.Unreflect(t, unsafe.Pointer(p)))
   580					mv := reflect.Indirect(v)
   581					// We send zero-length (but non-nil) maps because the
   582					// receiver might want to use the map.  (Maps don't use append.)
   583					if !state.sendZero && mv.IsNil() {
   584						return
   585					}
   586					state.update(i)
   587					state.enc.encodeMap(state.b, mv, *keyOp, *elemOp, keyIndir, elemIndir)
   588				}
   589			case reflect.Struct:
   590				// Generate a closure that calls out to the engine for the nested type.
   591				enc.getEncEngine(userType(typ))
   592				info := mustGetTypeInfo(typ)
   593				op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
   594					state.update(i)
   595					// indirect through info to delay evaluation for recursive structs
   596					state.enc.encodeStruct(state.b, info.encoder, uintptr(p))
   597				}
   598			case reflect.Interface:
   599				op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
   600					// Interfaces transmit the name and contents of the concrete
   601					// value they contain.
   602					v := reflect.ValueOf(unsafe.Unreflect(t, unsafe.Pointer(p)))
   603					iv := reflect.Indirect(v)
   604					if !state.sendZero && (!iv.IsValid() || iv.IsNil()) {
   605						return
   606					}
   607					state.update(i)
   608					state.enc.encodeInterface(state.b, iv)
   609				}
   610			}
   611		}
   612		if op == nil {
   613			errorf("can't happen: encode type %s", rt.String())
   614		}
   615		return &op, indir
   616	}
   617	
   618	// gobEncodeOpFor returns the op for a type that is known to implement
   619	// GobEncoder.
   620	func (enc *Encoder) gobEncodeOpFor(ut *userTypeInfo) (*encOp, int) {
   621		rt := ut.user
   622		if ut.encIndir == -1 {
   623			rt = reflect.PtrTo(rt)
   624		} else if ut.encIndir > 0 {
   625			for i := int8(0); i < ut.encIndir; i++ {
   626				rt = rt.Elem()
   627			}
   628		}
   629		var op encOp
   630		op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
   631			var v reflect.Value
   632			if ut.encIndir == -1 {
   633				// Need to climb up one level to turn value into pointer.
   634				v = reflect.ValueOf(unsafe.Unreflect(rt, unsafe.Pointer(&p)))
   635			} else {
   636				v = reflect.ValueOf(unsafe.Unreflect(rt, p))
   637			}
   638			if !state.sendZero && isZero(v) {
   639				return
   640			}
   641			state.update(i)
   642			state.enc.encodeGobEncoder(state.b, v)
   643		}
   644		return &op, int(ut.encIndir) // encIndir: op will get called with p == address of receiver.
   645	}
   646	
   647	// compileEnc returns the engine to compile the type.
   648	func (enc *Encoder) compileEnc(ut *userTypeInfo) *encEngine {
   649		srt := ut.base
   650		engine := new(encEngine)
   651		seen := make(map[reflect.Type]*encOp)
   652		rt := ut.base
   653		if ut.isGobEncoder {
   654			rt = ut.user
   655		}
   656		if !ut.isGobEncoder &&
   657			srt.Kind() == reflect.Struct {
   658			for fieldNum, wireFieldNum := 0, 0; fieldNum < srt.NumField(); fieldNum++ {
   659				f := srt.Field(fieldNum)
   660				if !isExported(f.Name) {
   661					continue
   662				}
   663				op, indir := enc.encOpFor(f.Type, seen)
   664				engine.instr = append(engine.instr, encInstr{*op, wireFieldNum, indir, uintptr(f.Offset)})
   665				wireFieldNum++
   666			}
   667			if srt.NumField() > 0 && len(engine.instr) == 0 {
   668				errorf("type %s has no exported fields", rt)
   669			}
   670			engine.instr = append(engine.instr, encInstr{encStructTerminator, 0, 0, 0})
   671		} else {
   672			engine.instr = make([]encInstr, 1)
   673			op, indir := enc.encOpFor(rt, seen)
   674			engine.instr[0] = encInstr{*op, singletonField, indir, 0} // offset is zero
   675		}
   676		return engine
   677	}
   678	
   679	// getEncEngine returns the engine to compile the type.
   680	// typeLock must be held (or we're in initialization and guaranteed single-threaded).
   681	func (enc *Encoder) getEncEngine(ut *userTypeInfo) *encEngine {
   682		info, err1 := getTypeInfo(ut)
   683		if err1 != nil {
   684			error(err1)
   685		}
   686		if info.encoder == nil {
   687			// mark this engine as underway before compiling to handle recursive types.
   688			info.encoder = new(encEngine)
   689			info.encoder = enc.compileEnc(ut)
   690		}
   691		return info.encoder
   692	}
   693	
   694	// lockAndGetEncEngine is a function that locks and compiles.
   695	// This lets us hold the lock only while compiling, not when encoding.
   696	func (enc *Encoder) lockAndGetEncEngine(ut *userTypeInfo) *encEngine {
   697		typeLock.Lock()
   698		defer typeLock.Unlock()
   699		return enc.getEncEngine(ut)
   700	}
   701	
   702	func (enc *Encoder) encode(b *bytes.Buffer, value reflect.Value, ut *userTypeInfo) {
   703		defer catchError(&enc.err)
   704		engine := enc.lockAndGetEncEngine(ut)
   705		indir := ut.indir
   706		if ut.isGobEncoder {
   707			indir = int(ut.encIndir)
   708		}
   709		for i := 0; i < indir; i++ {
   710			value = reflect.Indirect(value)
   711		}
   712		if !ut.isGobEncoder && value.Type().Kind() == reflect.Struct {
   713			enc.encodeStruct(b, engine, unsafeAddr(value))
   714		} else {
   715			enc.encodeSingle(b, engine, unsafeAddr(value))
   716		}
   717	}

release.r60.3. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.