LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
| 5 //go:generate go run decgen.go -output dec_helpers.go |
| 6 |
5 package gob | 7 package gob |
6 | 8 |
7 import ( | 9 import ( |
8 "bytes" | |
9 "encoding" | 10 "encoding" |
10 "errors" | 11 "errors" |
11 "io" | 12 "io" |
12 "math" | 13 "math" |
13 "reflect" | 14 "reflect" |
14 ) | 15 ) |
15 | 16 |
16 var ( | 17 var ( |
17 errBadUint = errors.New("gob: encoded unsigned integer out of range") | 18 errBadUint = errors.New("gob: encoded unsigned integer out of range") |
18 errBadType = errors.New("gob: unknown type id or corrupted data") | 19 errBadType = errors.New("gob: unknown type id or corrupted data") |
19 errRange = errors.New("gob: bad data: field numbers out of bounds") | 20 errRange = errors.New("gob: bad data: field numbers out of bounds") |
20 ) | 21 ) |
21 | 22 |
| 23 type decHelper func(state *decoderState, v reflect.Value, length int, ovfl error
) bool |
| 24 |
22 // decoderState is the execution state of an instance of the decoder. A new stat
e | 25 // decoderState is the execution state of an instance of the decoder. A new stat
e |
23 // is created for nested objects. | 26 // is created for nested objects. |
24 type decoderState struct { | 27 type decoderState struct { |
25 dec *Decoder | 28 dec *Decoder |
26 // The buffer is stored with an extra indirection because it may be repl
aced | 29 // The buffer is stored with an extra indirection because it may be repl
aced |
27 // if we load a type during decode (when reading an interface value). | 30 // if we load a type during decode (when reading an interface value). |
28 » b *bytes.Buffer | 31 » b *decBuffer |
29 fieldnum int // the last field number read. | 32 fieldnum int // the last field number read. |
30 buf []byte | 33 buf []byte |
31 next *decoderState // for free list | 34 next *decoderState // for free list |
32 } | 35 } |
33 | 36 |
| 37 // decBuffer is an extremely simple, fast implementation of a read-only byte buf
fer. |
| 38 // It is initialized by calling Size and then copying the data into the slice re
turned by Bytes(). |
| 39 type decBuffer struct { |
| 40 data []byte |
| 41 offset int // Read offset. |
| 42 } |
| 43 |
| 44 func (d *decBuffer) Read(p []byte) (int, error) { |
| 45 n := copy(p, d.data[d.offset:]) |
| 46 if n == 0 && len(p) != 0 { |
| 47 return 0, io.EOF |
| 48 } |
| 49 d.offset += n |
| 50 return n, nil |
| 51 } |
| 52 |
| 53 func (d *decBuffer) Drop(n int) { |
| 54 if n > d.Len() { |
| 55 panic("drop") |
| 56 } |
| 57 d.offset += n |
| 58 } |
| 59 |
| 60 // Size grows the buffer to exactly n bytes, so d.Bytes() will |
| 61 // return a slice of length n. Existing data is first discarded. |
| 62 func (d *decBuffer) Size(n int) { |
| 63 d.Reset() |
| 64 if cap(d.data) < n { |
| 65 d.data = make([]byte, n) |
| 66 } else { |
| 67 d.data = d.data[0:n] |
| 68 } |
| 69 } |
| 70 |
| 71 func (d *decBuffer) ReadByte() (byte, error) { |
| 72 if d.offset >= len(d.data) { |
| 73 return 0, io.EOF |
| 74 } |
| 75 c := d.data[d.offset] |
| 76 d.offset++ |
| 77 return c, nil |
| 78 } |
| 79 |
| 80 func (d *decBuffer) Len() int { |
| 81 return len(d.data) - d.offset |
| 82 } |
| 83 |
| 84 func (d *decBuffer) Bytes() []byte { |
| 85 return d.data[d.offset:] |
| 86 } |
| 87 |
| 88 func (d *decBuffer) Reset() { |
| 89 d.data = d.data[0:0] |
| 90 d.offset = 0 |
| 91 } |
| 92 |
34 // We pass the bytes.Buffer separately for easier testing of the infrastructure | 93 // We pass the bytes.Buffer separately for easier testing of the infrastructure |
35 // without requiring a full Decoder. | 94 // without requiring a full Decoder. |
36 func (dec *Decoder) newDecoderState(buf *bytes.Buffer) *decoderState { | 95 func (dec *Decoder) newDecoderState(buf *decBuffer) *decoderState { |
37 d := dec.freeList | 96 d := dec.freeList |
38 if d == nil { | 97 if d == nil { |
39 d = new(decoderState) | 98 d = new(decoderState) |
40 d.dec = dec | 99 d.dec = dec |
41 d.buf = make([]byte, uint64Size) | 100 d.buf = make([]byte, uint64Size) |
42 } else { | 101 } else { |
43 dec.freeList = d.next | 102 dec.freeList = d.next |
44 } | 103 } |
45 d.b = buf | 104 d.b = buf |
46 return d | 105 return d |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 v |= u & 0xFF | 309 v |= u & 0xFF |
251 u >>= 8 | 310 u >>= 8 |
252 } | 311 } |
253 return math.Float64frombits(v) | 312 return math.Float64frombits(v) |
254 } | 313 } |
255 | 314 |
256 // float32FromBits decodes an unsigned integer, treats it as a 32-bit floating-p
oint | 315 // float32FromBits decodes an unsigned integer, treats it as a 32-bit floating-p
oint |
257 // number, and returns it. It's a helper function for float32 and complex64. | 316 // number, and returns it. It's a helper function for float32 and complex64. |
258 // It returns a float64 because that's what reflection needs, but its return | 317 // It returns a float64 because that's what reflection needs, but its return |
259 // value is known to be accurately representable in a float32. | 318 // value is known to be accurately representable in a float32. |
260 func float32FromBits(i *decInstr, u uint64) float64 { | 319 func float32FromBits(u uint64, ovfl error) float64 { |
261 v := float64FromBits(u) | 320 v := float64FromBits(u) |
262 av := v | 321 av := v |
263 if av < 0 { | 322 if av < 0 { |
264 av = -av | 323 av = -av |
265 } | 324 } |
266 // +Inf is OK in both 32- and 64-bit floats. Underflow is always OK. | 325 // +Inf is OK in both 32- and 64-bit floats. Underflow is always OK. |
267 if math.MaxFloat32 < av && av <= math.MaxFloat64 { | 326 if math.MaxFloat32 < av && av <= math.MaxFloat64 { |
268 » » error_(i.ovfl) | 327 » » error_(ovfl) |
269 } | 328 } |
270 return v | 329 return v |
271 } | 330 } |
272 | 331 |
273 // decFloat32 decodes an unsigned integer, treats it as a 32-bit floating-point | 332 // decFloat32 decodes an unsigned integer, treats it as a 32-bit floating-point |
274 // number, and stores it in value. | 333 // number, and stores it in value. |
275 func decFloat32(i *decInstr, state *decoderState, value reflect.Value) { | 334 func decFloat32(i *decInstr, state *decoderState, value reflect.Value) { |
276 » value.SetFloat(float32FromBits(i, state.decodeUint())) | 335 » value.SetFloat(float32FromBits(state.decodeUint(), i.ovfl)) |
277 } | 336 } |
278 | 337 |
279 // decFloat64 decodes an unsigned integer, treats it as a 64-bit floating-point | 338 // decFloat64 decodes an unsigned integer, treats it as a 64-bit floating-point |
280 // number, and stores it in value. | 339 // number, and stores it in value. |
281 func decFloat64(i *decInstr, state *decoderState, value reflect.Value) { | 340 func decFloat64(i *decInstr, state *decoderState, value reflect.Value) { |
282 value.SetFloat(float64FromBits(state.decodeUint())) | 341 value.SetFloat(float64FromBits(state.decodeUint())) |
283 } | 342 } |
284 | 343 |
285 // decComplex64 decodes a pair of unsigned integers, treats them as a | 344 // decComplex64 decodes a pair of unsigned integers, treats them as a |
286 // pair of floating point numbers, and stores them as a complex64 in value. | 345 // pair of floating point numbers, and stores them as a complex64 in value. |
287 // The real part comes first. | 346 // The real part comes first. |
288 func decComplex64(i *decInstr, state *decoderState, value reflect.Value) { | 347 func decComplex64(i *decInstr, state *decoderState, value reflect.Value) { |
289 » real := float32FromBits(i, state.decodeUint()) | 348 » real := float32FromBits(state.decodeUint(), i.ovfl) |
290 » imag := float32FromBits(i, state.decodeUint()) | 349 » imag := float32FromBits(state.decodeUint(), i.ovfl) |
291 value.SetComplex(complex(real, imag)) | 350 value.SetComplex(complex(real, imag)) |
292 } | 351 } |
293 | 352 |
294 // decComplex128 decodes a pair of unsigned integers, treats them as a | 353 // decComplex128 decodes a pair of unsigned integers, treats them as a |
295 // pair of floating point numbers, and stores them as a complex128 in value. | 354 // pair of floating point numbers, and stores them as a complex128 in value. |
296 // The real part comes first. | 355 // The real part comes first. |
297 func decComplex128(i *decInstr, state *decoderState, value reflect.Value) { | 356 func decComplex128(i *decInstr, state *decoderState, value reflect.Value) { |
298 real := float64FromBits(state.decodeUint()) | 357 real := float64FromBits(state.decodeUint()) |
299 imag := float64FromBits(state.decodeUint()) | 358 imag := float64FromBits(state.decodeUint()) |
300 value.SetComplex(complex(real, imag)) | 359 value.SetComplex(complex(real, imag)) |
301 } | 360 } |
302 | 361 |
303 // decUint8Slice decodes a byte slice and stores in value a slice header | 362 // decUint8Slice decodes a byte slice and stores in value a slice header |
304 // describing the data. | 363 // describing the data. |
305 // uint8 slices are encoded as an unsigned count followed by the raw bytes. | 364 // uint8 slices are encoded as an unsigned count followed by the raw bytes. |
306 func decUint8Slice(i *decInstr, state *decoderState, value reflect.Value) { | 365 func decUint8Slice(i *decInstr, state *decoderState, value reflect.Value) { |
307 u := state.decodeUint() | 366 u := state.decodeUint() |
308 n := int(u) | 367 n := int(u) |
309 if n < 0 || uint64(n) != u { | 368 if n < 0 || uint64(n) != u { |
310 errorf("length of %s exceeds input size (%d bytes)", value.Type(
), u) | 369 errorf("length of %s exceeds input size (%d bytes)", value.Type(
), u) |
311 } | 370 } |
312 if n > state.b.Len() { | 371 if n > state.b.Len() { |
313 errorf("%s data too long for buffer: %d", value.Type(), n) | 372 errorf("%s data too long for buffer: %d", value.Type(), n) |
| 373 } |
| 374 if n > tooBig { |
| 375 errorf("byte slice too big: %d", n) |
314 } | 376 } |
315 if value.Cap() < n { | 377 if value.Cap() < n { |
316 value.Set(reflect.MakeSlice(value.Type(), n, n)) | 378 value.Set(reflect.MakeSlice(value.Type(), n, n)) |
317 } else { | 379 } else { |
318 value.Set(value.Slice(0, n)) | 380 value.Set(value.Slice(0, n)) |
319 } | 381 } |
320 if _, err := state.b.Read(value.Bytes()); err != nil { | 382 if _, err := state.b.Read(value.Bytes()); err != nil { |
321 errorf("error decoding []byte: %s", err) | 383 errorf("error decoding []byte: %s", err) |
322 } | 384 } |
323 } | 385 } |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
440 state.fieldnum = singletonField | 502 state.fieldnum = singletonField |
441 delta := int(state.decodeUint()) | 503 delta := int(state.decodeUint()) |
442 if delta != 0 { | 504 if delta != 0 { |
443 errorf("decode: corrupted data: non-zero delta for singleton") | 505 errorf("decode: corrupted data: non-zero delta for singleton") |
444 } | 506 } |
445 instr := &engine.instr[singletonField] | 507 instr := &engine.instr[singletonField] |
446 instr.op(instr, state, noValue) | 508 instr.op(instr, state, noValue) |
447 } | 509 } |
448 | 510 |
449 // decodeArrayHelper does the work for decoding arrays and slices. | 511 // decodeArrayHelper does the work for decoding arrays and slices. |
450 func (dec *Decoder) decodeArrayHelper(state *decoderState, value reflect.Value,
elemOp decOp, length int, ovfl error) { | 512 func (dec *Decoder) decodeArrayHelper(state *decoderState, value reflect.Value,
elemOp decOp, length int, ovfl error, helper decHelper) { |
| 513 » if helper != nil && helper(state, value, length, ovfl) { |
| 514 » » return |
| 515 » } |
451 instr := &decInstr{elemOp, 0, nil, ovfl} | 516 instr := &decInstr{elemOp, 0, nil, ovfl} |
452 isPtr := value.Type().Elem().Kind() == reflect.Ptr | 517 isPtr := value.Type().Elem().Kind() == reflect.Ptr |
453 for i := 0; i < length; i++ { | 518 for i := 0; i < length; i++ { |
454 if state.b.Len() == 0 { | 519 if state.b.Len() == 0 { |
455 errorf("decoding array or slice: length exceeds input si
ze (%d elements)", length) | 520 errorf("decoding array or slice: length exceeds input si
ze (%d elements)", length) |
456 } | 521 } |
457 v := value.Index(i) | 522 v := value.Index(i) |
458 if isPtr { | 523 if isPtr { |
459 v = decAlloc(v) | 524 v = decAlloc(v) |
460 } | 525 } |
461 elemOp(instr, state, v) | 526 elemOp(instr, state, v) |
462 } | 527 } |
463 } | 528 } |
464 | 529 |
465 // decodeArray decodes an array and stores it in value. | 530 // decodeArray decodes an array and stores it in value. |
466 // The length is an unsigned integer preceding the elements. Even though the le
ngth is redundant | 531 // The length is an unsigned integer preceding the elements. Even though the le
ngth is redundant |
467 // (it's part of the type), it's a useful check and is included in the encoding. | 532 // (it's part of the type), it's a useful check and is included in the encoding. |
468 func (dec *Decoder) decodeArray(atyp reflect.Type, state *decoderState, value re
flect.Value, elemOp decOp, length int, ovfl error) { | 533 func (dec *Decoder) decodeArray(atyp reflect.Type, state *decoderState, value re
flect.Value, elemOp decOp, length int, ovfl error, helper decHelper) { |
469 if n := state.decodeUint(); n != uint64(length) { | 534 if n := state.decodeUint(); n != uint64(length) { |
470 errorf("length mismatch in decodeArray") | 535 errorf("length mismatch in decodeArray") |
471 } | 536 } |
472 » dec.decodeArrayHelper(state, value, elemOp, length, ovfl) | 537 » dec.decodeArrayHelper(state, value, elemOp, length, ovfl, helper) |
473 } | 538 } |
474 | 539 |
475 // decodeIntoValue is a helper for map decoding. | 540 // decodeIntoValue is a helper for map decoding. |
476 func decodeIntoValue(state *decoderState, op decOp, isPtr bool, value reflect.Va
lue, ovfl error) reflect.Value { | 541 func decodeIntoValue(state *decoderState, op decOp, isPtr bool, value reflect.Va
lue, ovfl error) reflect.Value { |
477 instr := &decInstr{op, 0, nil, ovfl} | 542 instr := &decInstr{op, 0, nil, ovfl} |
478 v := value | 543 v := value |
479 if isPtr { | 544 if isPtr { |
480 v = decAlloc(value) | 545 v = decAlloc(value) |
481 } | 546 } |
482 op(instr, state, v) | 547 op(instr, state, v) |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
524 keyInstr := &decInstr{keyOp, 0, nil, errors.New("no error")} | 589 keyInstr := &decInstr{keyOp, 0, nil, errors.New("no error")} |
525 elemInstr := &decInstr{elemOp, 0, nil, errors.New("no error")} | 590 elemInstr := &decInstr{elemOp, 0, nil, errors.New("no error")} |
526 for i := 0; i < n; i++ { | 591 for i := 0; i < n; i++ { |
527 keyOp(keyInstr, state, noValue) | 592 keyOp(keyInstr, state, noValue) |
528 elemOp(elemInstr, state, noValue) | 593 elemOp(elemInstr, state, noValue) |
529 } | 594 } |
530 } | 595 } |
531 | 596 |
532 // decodeSlice decodes a slice and stores it in value. | 597 // decodeSlice decodes a slice and stores it in value. |
533 // Slices are encoded as an unsigned length followed by the elements. | 598 // Slices are encoded as an unsigned length followed by the elements. |
534 func (dec *Decoder) decodeSlice(state *decoderState, value reflect.Value, elemOp
decOp, ovfl error) { | 599 func (dec *Decoder) decodeSlice(state *decoderState, value reflect.Value, elemOp
decOp, ovfl error, helper decHelper) { |
535 u := state.decodeUint() | 600 u := state.decodeUint() |
| 601 typ := value.Type() |
| 602 size := uint64(typ.Elem().Size()) |
| 603 nBytes := u * size |
536 n := int(u) | 604 n := int(u) |
537 » if n < 0 || uint64(n) != u { | 605 » // Take care with overflow in this calculation. |
| 606 » if n < 0 || uint64(n) != u || nBytes > tooBig || (size > 0 && nBytes/siz
e != u) { |
538 // We don't check n against buffer length here because if it's a
slice | 607 // We don't check n against buffer length here because if it's a
slice |
539 // of interfaces, there will be buffer reloads. | 608 // of interfaces, there will be buffer reloads. |
540 » » errorf("length of %s is negative (%d bytes)", value.Type(), u) | 609 » » errorf("%s slice too big: %d elements of %d bytes", typ.Elem(),
u, size) |
541 } | 610 } |
542 if value.Cap() < n { | 611 if value.Cap() < n { |
543 » » value.Set(reflect.MakeSlice(value.Type(), n, n)) | 612 » » value.Set(reflect.MakeSlice(typ, n, n)) |
544 } else { | 613 } else { |
545 value.Set(value.Slice(0, n)) | 614 value.Set(value.Slice(0, n)) |
546 } | 615 } |
547 » dec.decodeArrayHelper(state, value, elemOp, n, ovfl) | 616 » dec.decodeArrayHelper(state, value, elemOp, n, ovfl, helper) |
548 } | 617 } |
549 | 618 |
550 // ignoreSlice skips over the data for a slice value with no destination. | 619 // ignoreSlice skips over the data for a slice value with no destination. |
551 func (dec *Decoder) ignoreSlice(state *decoderState, elemOp decOp) { | 620 func (dec *Decoder) ignoreSlice(state *decoderState, elemOp decOp) { |
552 dec.ignoreArrayHelper(state, elemOp, int(state.decodeUint())) | 621 dec.ignoreArrayHelper(state, elemOp, int(state.decodeUint())) |
553 } | 622 } |
554 | 623 |
555 // decodeInterface decodes an interface value and stores it in value. | 624 // decodeInterface decodes an interface value and stores it in value. |
556 // Interfaces are encoded as the name of a concrete type followed by a value. | 625 // Interfaces are encoded as the name of a concrete type followed by a value. |
557 // If the name is empty, the value is nil and no value is sent. | 626 // If the name is empty, the value is nil and no value is sent. |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
612 b := make([]byte, state.decodeUint()) | 681 b := make([]byte, state.decodeUint()) |
613 _, err := state.b.Read(b) | 682 _, err := state.b.Read(b) |
614 if err != nil { | 683 if err != nil { |
615 error_(err) | 684 error_(err) |
616 } | 685 } |
617 id := dec.decodeTypeSequence(true) | 686 id := dec.decodeTypeSequence(true) |
618 if id < 0 { | 687 if id < 0 { |
619 error_(dec.err) | 688 error_(dec.err) |
620 } | 689 } |
621 // At this point, the decoder buffer contains a delimited value. Just to
ss it. | 690 // At this point, the decoder buffer contains a delimited value. Just to
ss it. |
622 » state.b.Next(int(state.decodeUint())) | 691 » state.b.Drop(int(state.decodeUint())) |
623 } | 692 } |
624 | 693 |
625 // decodeGobDecoder decodes something implementing the GobDecoder interface. | 694 // decodeGobDecoder decodes something implementing the GobDecoder interface. |
626 // The data is encoded as a byte slice. | 695 // The data is encoded as a byte slice. |
627 func (dec *Decoder) decodeGobDecoder(ut *userTypeInfo, state *decoderState, valu
e reflect.Value) { | 696 func (dec *Decoder) decodeGobDecoder(ut *userTypeInfo, state *decoderState, valu
e reflect.Value) { |
628 // Read the bytes for the value. | 697 // Read the bytes for the value. |
629 b := make([]byte, state.decodeUint()) | 698 b := make([]byte, state.decodeUint()) |
630 _, err := state.b.Read(b) | 699 _, err := state.b.Read(b) |
631 if err != nil { | 700 if err != nil { |
632 error_(err) | 701 error_(err) |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
706 } | 775 } |
707 if op == nil { | 776 if op == nil { |
708 inProgress[rt] = &op | 777 inProgress[rt] = &op |
709 // Special cases | 778 // Special cases |
710 switch t := typ; t.Kind() { | 779 switch t := typ; t.Kind() { |
711 case reflect.Array: | 780 case reflect.Array: |
712 name = "element of " + name | 781 name = "element of " + name |
713 elemId := dec.wireType[wireId].ArrayT.Elem | 782 elemId := dec.wireType[wireId].ArrayT.Elem |
714 elemOp := dec.decOpFor(elemId, t.Elem(), name, inProgres
s) | 783 elemOp := dec.decOpFor(elemId, t.Elem(), name, inProgres
s) |
715 ovfl := overflow(name) | 784 ovfl := overflow(name) |
| 785 helper := decArrayHelper[t.Elem().Kind()] |
716 op = func(i *decInstr, state *decoderState, value reflec
t.Value) { | 786 op = func(i *decInstr, state *decoderState, value reflec
t.Value) { |
717 » » » » state.dec.decodeArray(t, state, value, *elemOp,
t.Len(), ovfl) | 787 » » » » state.dec.decodeArray(t, state, value, *elemOp,
t.Len(), ovfl, helper) |
718 } | 788 } |
719 | 789 |
720 case reflect.Map: | 790 case reflect.Map: |
721 keyId := dec.wireType[wireId].MapT.Key | 791 keyId := dec.wireType[wireId].MapT.Key |
722 elemId := dec.wireType[wireId].MapT.Elem | 792 elemId := dec.wireType[wireId].MapT.Elem |
723 keyOp := dec.decOpFor(keyId, t.Key(), "key of "+name, in
Progress) | 793 keyOp := dec.decOpFor(keyId, t.Key(), "key of "+name, in
Progress) |
724 elemOp := dec.decOpFor(elemId, t.Elem(), "element of "+n
ame, inProgress) | 794 elemOp := dec.decOpFor(elemId, t.Elem(), "element of "+n
ame, inProgress) |
725 ovfl := overflow(name) | 795 ovfl := overflow(name) |
726 op = func(i *decInstr, state *decoderState, value reflec
t.Value) { | 796 op = func(i *decInstr, state *decoderState, value reflec
t.Value) { |
727 state.dec.decodeMap(t, state, value, *keyOp, *el
emOp, ovfl) | 797 state.dec.decodeMap(t, state, value, *keyOp, *el
emOp, ovfl) |
728 } | 798 } |
729 | 799 |
730 case reflect.Slice: | 800 case reflect.Slice: |
731 name = "element of " + name | 801 name = "element of " + name |
732 if t.Elem().Kind() == reflect.Uint8 { | 802 if t.Elem().Kind() == reflect.Uint8 { |
733 op = decUint8Slice | 803 op = decUint8Slice |
734 break | 804 break |
735 } | 805 } |
736 var elemId typeId | 806 var elemId typeId |
737 if tt, ok := builtinIdToType[wireId]; ok { | 807 if tt, ok := builtinIdToType[wireId]; ok { |
738 elemId = tt.(*sliceType).Elem | 808 elemId = tt.(*sliceType).Elem |
739 } else { | 809 } else { |
740 elemId = dec.wireType[wireId].SliceT.Elem | 810 elemId = dec.wireType[wireId].SliceT.Elem |
741 } | 811 } |
742 elemOp := dec.decOpFor(elemId, t.Elem(), name, inProgres
s) | 812 elemOp := dec.decOpFor(elemId, t.Elem(), name, inProgres
s) |
743 ovfl := overflow(name) | 813 ovfl := overflow(name) |
| 814 helper := decSliceHelper[t.Elem().Kind()] |
744 op = func(i *decInstr, state *decoderState, value reflec
t.Value) { | 815 op = func(i *decInstr, state *decoderState, value reflec
t.Value) { |
745 » » » » state.dec.decodeSlice(state, value, *elemOp, ovf
l) | 816 » » » » state.dec.decodeSlice(state, value, *elemOp, ovf
l, helper) |
746 } | 817 } |
747 | 818 |
748 case reflect.Struct: | 819 case reflect.Struct: |
749 // Generate a closure that calls out to the engine for t
he nested type. | 820 // Generate a closure that calls out to the engine for t
he nested type. |
750 ut := userType(typ) | 821 ut := userType(typ) |
751 enginePtr, err := dec.getDecEnginePtr(wireId, ut) | 822 enginePtr, err := dec.getDecEnginePtr(wireId, ut) |
752 if err != nil { | 823 if err != nil { |
753 error_(err) | 824 error_(err) |
754 } | 825 } |
755 op = func(i *decInstr, state *decoderState, value reflec
t.Value) { | 826 op = func(i *decInstr, state *decoderState, value reflec
t.Value) { |
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1137 decOpTable[reflect.Uintptr] = uop | 1208 decOpTable[reflect.Uintptr] = uop |
1138 } | 1209 } |
1139 | 1210 |
1140 // Gob depends on being able to take the address | 1211 // Gob depends on being able to take the address |
1141 // of zeroed Values it creates, so use this wrapper instead | 1212 // of zeroed Values it creates, so use this wrapper instead |
1142 // of the standard reflect.Zero. | 1213 // of the standard reflect.Zero. |
1143 // Each call allocates once. | 1214 // Each call allocates once. |
1144 func allocValue(t reflect.Type) reflect.Value { | 1215 func allocValue(t reflect.Type) reflect.Value { |
1145 return reflect.New(t).Elem() | 1216 return reflect.New(t).Elem() |
1146 } | 1217 } |
LEFT | RIGHT |