diff options
author | Ondřej Surý <ondrej@sury.org> | 2011-08-03 16:54:30 +0200 |
---|---|---|
committer | Ondřej Surý <ondrej@sury.org> | 2011-08-03 16:54:30 +0200 |
commit | 28592ee1ea1f5cdffcf85472f9de0285d928cf12 (patch) | |
tree | 32944e18b23f7fe4a0818a694aa2a6dfb1835463 /src/pkg/gob | |
parent | e836bee4716dc0d4d913537ad3ad1925a7ac32d0 (diff) | |
download | golang-upstream/59.tar.gz |
Imported Upstream version 59upstream/59
Diffstat (limited to 'src/pkg/gob')
-rw-r--r-- | src/pkg/gob/codec_test.go | 2 | ||||
-rw-r--r-- | src/pkg/gob/decode.go | 44 | ||||
-rw-r--r-- | src/pkg/gob/decoder.go | 8 | ||||
-rw-r--r-- | src/pkg/gob/doc.go | 34 | ||||
-rw-r--r-- | src/pkg/gob/encode.go | 17 | ||||
-rw-r--r-- | src/pkg/gob/encoder.go | 2 | ||||
-rw-r--r-- | src/pkg/gob/gobencdec_test.go | 8 | ||||
-rw-r--r-- | src/pkg/gob/type.go | 9 |
8 files changed, 54 insertions, 70 deletions
diff --git a/src/pkg/gob/codec_test.go b/src/pkg/gob/codec_test.go index 8961336cd..da8e59c74 100644 --- a/src/pkg/gob/codec_test.go +++ b/src/pkg/gob/codec_test.go @@ -330,7 +330,7 @@ func newDecodeStateFromData(data []byte) *decoderState { // Test instruction execution for decoding. // Do not run the machine yet; instead do individual instructions crafted by hand. func TestScalarDecInstructions(t *testing.T) { - ovfl := os.ErrorString("overflow") + ovfl := os.NewError("overflow") // bool { diff --git a/src/pkg/gob/decode.go b/src/pkg/gob/decode.go index 381d44c05..bf7cb95f2 100644 --- a/src/pkg/gob/decode.go +++ b/src/pkg/gob/decode.go @@ -17,9 +17,9 @@ import ( ) var ( - errBadUint = os.ErrorString("gob: encoded unsigned integer out of range") - errBadType = os.ErrorString("gob: unknown type id or corrupted data") - errRange = os.ErrorString("gob: bad data: field numbers out of bounds") + errBadUint = os.NewError("gob: encoded unsigned integer out of range") + errBadType = os.NewError("gob: unknown type id or corrupted data") + errRange = os.NewError("gob: bad data: field numbers out of bounds") ) // decoderState is the execution state of an instance of the decoder. A new state @@ -54,8 +54,8 @@ func (dec *Decoder) freeDecoderState(d *decoderState) { dec.freeList = d } -func overflow(name string) os.ErrorString { - return os.ErrorString(`value for "` + name + `" out of range`) +func overflow(name string) os.Error { + return os.NewError(`value for "` + name + `" out of range`) } // decodeUintReader reads an encoded unsigned integer from an io.Reader. @@ -135,10 +135,10 @@ type decOp func(i *decInstr, state *decoderState, p unsafe.Pointer) // The 'instructions' of the decoding machine type decInstr struct { op decOp - field int // field number of the wire type - indir int // how many pointer indirections to reach the value in the struct - offset uintptr // offset in the structure of the field to encode - ovfl os.ErrorString // error message for overflow/underflow (for arrays, of the elements) + field int // field number of the wire type + indir int // how many pointer indirections to reach the value in the struct + offset uintptr // offset in the structure of the field to encode + ovfl os.Error // error message for overflow/underflow (for arrays, of the elements) } // Since the encoder writes no zeros, if we arrive at a decoder we have @@ -367,7 +367,7 @@ func decComplex64(i *decInstr, state *decoderState, p unsafe.Pointer) { p = *(*unsafe.Pointer)(p) } storeFloat32(i, state, p) - storeFloat32(i, state, unsafe.Pointer(uintptr(p)+uintptr(unsafe.Sizeof(float32(0))))) + storeFloat32(i, state, unsafe.Pointer(uintptr(p)+unsafe.Sizeof(float32(0)))) } // decComplex128 decodes a pair of unsigned integers, treats them as a @@ -552,7 +552,7 @@ func (dec *Decoder) ignoreSingle(engine *decEngine) { } // decodeArrayHelper does the work for decoding arrays and slices. -func (dec *Decoder) decodeArrayHelper(state *decoderState, p uintptr, elemOp decOp, elemWid uintptr, length, elemIndir int, ovfl os.ErrorString) { +func (dec *Decoder) decodeArrayHelper(state *decoderState, p uintptr, elemOp decOp, elemWid uintptr, length, elemIndir int, ovfl os.Error) { instr := &decInstr{elemOp, 0, elemIndir, 0, ovfl} for i := 0; i < length; i++ { up := unsafe.Pointer(p) @@ -567,7 +567,7 @@ func (dec *Decoder) decodeArrayHelper(state *decoderState, p uintptr, elemOp dec // decodeArray decodes an array and stores it through p, that is, p points to the zeroth element. // The length is an unsigned integer preceding the elements. Even though the length is redundant // (it's part of the type), it's a useful check and is included in the encoding. -func (dec *Decoder) decodeArray(atyp reflect.Type, state *decoderState, p uintptr, elemOp decOp, elemWid uintptr, length, indir, elemIndir int, ovfl os.ErrorString) { +func (dec *Decoder) decodeArray(atyp reflect.Type, state *decoderState, p uintptr, elemOp decOp, elemWid uintptr, length, indir, elemIndir int, ovfl os.Error) { if indir > 0 { p = allocate(atyp, p, 1) // All but the last level has been allocated by dec.Indirect } @@ -579,7 +579,7 @@ func (dec *Decoder) decodeArray(atyp reflect.Type, state *decoderState, p uintpt // decodeIntoValue is a helper for map decoding. Since maps are decoded using reflection, // unlike the other items we can't use a pointer directly. -func decodeIntoValue(state *decoderState, op decOp, indir int, v reflect.Value, ovfl os.ErrorString) reflect.Value { +func decodeIntoValue(state *decoderState, op decOp, indir int, v reflect.Value, ovfl os.Error) reflect.Value { instr := &decInstr{op, 0, indir, 0, ovfl} up := unsafe.Pointer(unsafeAddr(v)) if indir > 1 { @@ -593,7 +593,7 @@ func decodeIntoValue(state *decoderState, op decOp, indir int, v reflect.Value, // Maps are encoded as a length followed by key:value pairs. // Because the internals of maps are not visible to us, we must // use reflection rather than pointer magic. -func (dec *Decoder) decodeMap(mtyp reflect.Type, state *decoderState, p uintptr, keyOp, elemOp decOp, indir, keyIndir, elemIndir int, ovfl os.ErrorString) { +func (dec *Decoder) decodeMap(mtyp reflect.Type, state *decoderState, p uintptr, keyOp, elemOp decOp, indir, keyIndir, elemIndir int, ovfl os.Error) { if indir > 0 { p = allocate(mtyp, p, 1) // All but the last level has been allocated by dec.Indirect } @@ -616,7 +616,7 @@ func (dec *Decoder) decodeMap(mtyp reflect.Type, state *decoderState, p uintptr, // ignoreArrayHelper does the work for discarding arrays and slices. func (dec *Decoder) ignoreArrayHelper(state *decoderState, elemOp decOp, length int) { - instr := &decInstr{elemOp, 0, 0, 0, os.ErrorString("no error")} + instr := &decInstr{elemOp, 0, 0, 0, os.NewError("no error")} for i := 0; i < length; i++ { elemOp(instr, state, nil) } @@ -633,8 +633,8 @@ func (dec *Decoder) ignoreArray(state *decoderState, elemOp decOp, length int) { // ignoreMap discards the data for a map value with no destination. func (dec *Decoder) ignoreMap(state *decoderState, keyOp, elemOp decOp) { n := int(state.decodeUint()) - keyInstr := &decInstr{keyOp, 0, 0, 0, os.ErrorString("no error")} - elemInstr := &decInstr{elemOp, 0, 0, 0, os.ErrorString("no error")} + keyInstr := &decInstr{keyOp, 0, 0, 0, os.NewError("no error")} + elemInstr := &decInstr{elemOp, 0, 0, 0, os.NewError("no error")} for i := 0; i < n; i++ { keyOp(keyInstr, state, nil) elemOp(elemInstr, state, nil) @@ -643,7 +643,7 @@ func (dec *Decoder) ignoreMap(state *decoderState, keyOp, elemOp decOp) { // decodeSlice decodes a slice and stores the slice header through p. // Slices are encoded as an unsigned length followed by the elements. -func (dec *Decoder) decodeSlice(atyp reflect.Type, state *decoderState, p uintptr, elemOp decOp, elemWid uintptr, indir, elemIndir int, ovfl os.ErrorString) { +func (dec *Decoder) decodeSlice(atyp reflect.Type, state *decoderState, p uintptr, elemOp decOp, elemWid uintptr, indir, elemIndir int, ovfl os.Error) { n := int(uintptr(state.decodeUint())) if indir > 0 { up := unsafe.Pointer(p) @@ -741,7 +741,7 @@ func (dec *Decoder) ignoreInterface(state *decoderState) { // decodeGobDecoder decodes something implementing the GobDecoder interface. // The data is encoded as a byte slice. -func (dec *Decoder) decodeGobDecoder(state *decoderState, v reflect.Value, index int) { +func (dec *Decoder) decodeGobDecoder(state *decoderState, v reflect.Value) { // Read the bytes for the value. b := make([]byte, state.decodeUint()) _, err := state.b.Read(b) @@ -969,7 +969,7 @@ func (dec *Decoder) gobDecodeOpFor(ut *userTypeInfo) (*decOp, int) { } else { v = reflect.ValueOf(unsafe.Unreflect(rcvrType, p)) } - state.dec.decodeGobDecoder(state, v, methodIndex(rcvrType, gobDecodeMethodName)) + state.dec.decodeGobDecoder(state, v) } return &op, int(ut.indir) @@ -1064,10 +1064,10 @@ func (dec *Decoder) compileSingle(remoteId typeId, ut *userTypeInfo) (engine *de engine.instr = make([]decInstr, 1) // one item name := rt.String() // best we can do if !dec.compatibleType(rt, remoteId, make(map[reflect.Type]typeId)) { - return nil, os.ErrorString("gob: wrong type received for local value " + name + ": " + dec.typeString(remoteId)) + return nil, os.NewError("gob: wrong type received for local value " + name + ": " + dec.typeString(remoteId)) } op, indir := dec.decOpFor(remoteId, rt, name, make(map[reflect.Type]*decOp)) - ovfl := os.ErrorString(`value for "` + name + `" out of range`) + ovfl := os.NewError(`value for "` + name + `" out of range`) engine.instr[singletonField] = decInstr{*op, singletonField, indir, 0, ovfl} engine.numInstr = 1 return diff --git a/src/pkg/gob/decoder.go b/src/pkg/gob/decoder.go index b83904a71..281947132 100644 --- a/src/pkg/gob/decoder.go +++ b/src/pkg/gob/decoder.go @@ -44,7 +44,7 @@ func NewDecoder(r io.Reader) *Decoder { func (dec *Decoder) recvType(id typeId) { // Have we already seen this type? That's an error if id < firstUserId || dec.wireType[id] != nil { - dec.err = os.ErrorString("gob: duplicate type received") + dec.err = os.NewError("gob: duplicate type received") return } @@ -143,7 +143,7 @@ func (dec *Decoder) decodeTypeSequence(isInterface bool) typeId { // will be absorbed by recvMessage.) if dec.buf.Len() > 0 { if !isInterface { - dec.err = os.ErrorString("extra data in buffer") + dec.err = os.NewError("extra data in buffer") break } dec.nextUint() @@ -165,7 +165,7 @@ func (dec *Decoder) Decode(e interface{}) os.Error { // If e represents a value as opposed to a pointer, the answer won't // get back to the caller. Make sure it's a pointer. if value.Type().Kind() != reflect.Ptr { - dec.err = os.ErrorString("gob: attempt to decode into a non-pointer") + dec.err = os.NewError("gob: attempt to decode into a non-pointer") return dec.err } return dec.DecodeValue(value) @@ -180,7 +180,7 @@ func (dec *Decoder) DecodeValue(v reflect.Value) os.Error { if v.Kind() == reflect.Ptr && !v.IsNil() { // That's okay, we'll store through the pointer. } else if !v.CanSet() { - return os.ErrorString("gob: DecodeValue of unassignable value") + return os.NewError("gob: DecodeValue of unassignable value") } } // Make sure we're single-threaded through here. diff --git a/src/pkg/gob/doc.go b/src/pkg/gob/doc.go index 850759bbd..aaf429c43 100644 --- a/src/pkg/gob/doc.go +++ b/src/pkg/gob/doc.go @@ -29,29 +29,29 @@ receiver and transmitter will do all necessary indirection and dereferencing to convert between gobs and actual Go values. For instance, a gob type that is schematically, - struct { a, b int } + struct { A, B int } can be sent from or received into any of these Go types: - struct { a, b int } // the same - *struct { a, b int } // extra indirection of the struct - struct { *a, **b int } // extra indirection of the fields - struct { a, b int64 } // different concrete value type; see below + struct { A, B int } // the same + *struct { A, B int } // extra indirection of the struct + struct { *A, **B int } // extra indirection of the fields + struct { A, B int64 } // different concrete value type; see below It may also be received into any of these: - struct { a, b int } // the same - struct { b, a int } // ordering doesn't matter; matching is by name - struct { a, b, c int } // extra field (c) ignored - struct { b int } // missing field (a) ignored; data will be dropped - struct { b, c int } // missing field (a) ignored; extra field (c) ignored. + struct { A, B int } // the same + struct { B, A int } // ordering doesn't matter; matching is by name + struct { A, B, C int } // extra field (C) ignored + struct { B int } // missing field (A) ignored; data will be dropped + struct { B, C int } // missing field (A) ignored; extra field (C) ignored. Attempting to receive into these types will draw a decode error: - struct { a int; b uint } // change of signedness for b - struct { a int; b float } // change of type for b + struct { A int; B uint } // change of signedness for B + struct { A int; B float } // change of type for B struct { } // no field names in common - struct { c, d int } // no field names in common + struct { C, D int } // no field names in common Integers are transmitted two ways: arbitrary precision signed integers or arbitrary precision unsigned integers. There is no int8, int16 etc. @@ -269,12 +269,12 @@ StructValue: /* For implementers and the curious, here is an encoded example. Given - type Point struct {x, y int} + type Point struct {X, Y int} and the value p := Point{22, 33} the bytes transmitted that encode p will be: 1f ff 81 03 01 01 05 50 6f 69 6e 74 01 ff 82 00 - 01 02 01 01 78 01 04 00 01 01 79 01 04 00 00 00 + 01 02 01 01 58 01 04 00 01 01 59 01 04 00 00 00 07 ff 82 01 2c 01 42 00 They are determined as follows. @@ -310,13 +310,13 @@ reserved). 02 // There are two fields in the type (len(structType.field)) 01 // Start of first field structure; add 1 to get field number 0: field[0].name 01 // 1 byte - 78 // structType.field[0].name = "x" + 58 // structType.field[0].name = "X" 01 // Add 1 to get field number 1: field[0].id 04 // structType.field[0].typeId is 2 (signed int). 00 // End of structType.field[0]; start structType.field[1]; set field number to -1. 01 // Add 1 to get field number 0: field[1].name 01 // 1 byte - 79 // structType.field[1].name = "y" + 59 // structType.field[1].name = "Y" 01 // Add 1 to get field number 1: field[0].id 04 // struct.Type.field[1].typeId is 2 (signed int). 00 // End of structType.field[1]; end of structType.field. diff --git a/src/pkg/gob/encode.go b/src/pkg/gob/encode.go index f9e691a2f..941e26052 100644 --- a/src/pkg/gob/encode.go +++ b/src/pkg/gob/encode.go @@ -11,7 +11,7 @@ import ( "unsafe" ) -const uint64Size = unsafe.Sizeof(uint64(0)) +const uint64Size = int(unsafe.Sizeof(uint64(0))) // encoderState is the global execution state of an instance of the encoder. // Field numbers are delta encoded and always increase. The field @@ -468,7 +468,7 @@ func (enc *Encoder) encodeInterface(b *bytes.Buffer, iv reflect.Value) { // 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) { +func (enc *Encoder) encodeGobEncoder(b *bytes.Buffer, v reflect.Value) { // TODO: should we catch panics from the called method? // We know it's a GobEncoder, so just call the method directly. data, err := v.Interface().(GobEncoder).GobEncode() @@ -592,17 +592,6 @@ func (enc *Encoder) encOpFor(rt reflect.Type, inProgress map[reflect.Type]*encOp return &op, indir } -// 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 - } - } - errorf("internal error: can't find method %s", method) - return 0 -} - // gobEncodeOpFor returns the op for a type that is known to implement // GobEncoder. func (enc *Encoder) gobEncodeOpFor(ut *userTypeInfo) (*encOp, int) { @@ -624,7 +613,7 @@ func (enc *Encoder) gobEncodeOpFor(ut *userTypeInfo) (*encOp, int) { v = reflect.ValueOf(unsafe.Unreflect(rt, p)) } state.update(i) - state.enc.encodeGobEncoder(state.b, v, methodIndex(rt, gobEncodeMethodName)) + state.enc.encodeGobEncoder(state.b, v) } return &op, int(ut.encIndir) // encIndir: op will get called with p == address of receiver. } diff --git a/src/pkg/gob/encoder.go b/src/pkg/gob/encoder.go index 65ee5bf67..96101d92b 100644 --- a/src/pkg/gob/encoder.go +++ b/src/pkg/gob/encoder.go @@ -50,7 +50,7 @@ func (enc *Encoder) popWriter() { } func (enc *Encoder) badType(rt reflect.Type) { - enc.setError(os.ErrorString("gob: can't encode type " + rt.String())) + enc.setError(os.NewError("gob: can't encode type " + rt.String())) } func (enc *Encoder) setError(err os.Error) { diff --git a/src/pkg/gob/gobencdec_test.go b/src/pkg/gob/gobencdec_test.go index 3e1906020..25cb5d11b 100644 --- a/src/pkg/gob/gobencdec_test.go +++ b/src/pkg/gob/gobencdec_test.go @@ -44,7 +44,7 @@ func (g *ByteStruct) GobEncode() ([]byte, os.Error) { func (g *ByteStruct) GobDecode(data []byte) os.Error { if g == nil { - return os.ErrorString("NIL RECEIVER") + return os.NewError("NIL RECEIVER") } // Expect N sequential-valued bytes. if len(data) == 0 { @@ -53,7 +53,7 @@ func (g *ByteStruct) GobDecode(data []byte) os.Error { g.a = data[0] for i, c := range data { if c != g.a+byte(i) { - return os.ErrorString("invalid data sequence") + return os.NewError("invalid data sequence") } } return nil @@ -71,7 +71,7 @@ func (g *StringStruct) GobDecode(data []byte) os.Error { a := data[0] for i, c := range data { if c != a+byte(i) { - return os.ErrorString("invalid data sequence") + return os.NewError("invalid data sequence") } } g.s = string(data) @@ -84,7 +84,7 @@ func (a *ArrayStruct) GobEncode() ([]byte, os.Error) { func (a *ArrayStruct) GobDecode(data []byte) os.Error { if len(data) != len(a.a) { - return os.ErrorString("wrong length in array decode") + return os.NewError("wrong length in array decode") } copy(a.a[:], data) return nil diff --git a/src/pkg/gob/type.go b/src/pkg/gob/type.go index c6542633a..552faa4d6 100644 --- a/src/pkg/gob/type.go +++ b/src/pkg/gob/type.go @@ -67,7 +67,7 @@ func validUserType(rt reflect.Type) (ut *userTypeInfo, err os.Error) { ut.base = pt.Elem() if ut.base == slowpoke { // ut.base lapped slowpoke // recursive pointer type. - return nil, os.ErrorString("can't represent recursive pointer type " + ut.base.String()) + return nil, os.NewError("can't represent recursive pointer type " + ut.base.String()) } if ut.indir%2 == 0 { slowpoke = slowpoke.Elem() @@ -80,11 +80,6 @@ func validUserType(rt reflect.Type) (ut *userTypeInfo, err os.Error) { return } -const ( - gobEncodeMethodName = "GobEncode" - gobDecodeMethodName = "GobDecode" -) - var ( gobEncoderInterfaceType = reflect.TypeOf(new(GobEncoder)).Elem() gobDecoderInterfaceType = reflect.TypeOf(new(GobDecoder)).Elem() @@ -508,7 +503,7 @@ func newTypeObject(name string, ut *userTypeInfo, rt reflect.Type) (gobType, os. return st, nil default: - return nil, os.ErrorString("gob NewTypeObject can't handle type: " + rt.String()) + return nil, os.NewError("gob NewTypeObject can't handle type: " + rt.String()) } return nil, nil } |