Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 package reflect | 5 package reflect |
6 | 6 |
7 import ( | 7 import ( |
8 "math" | 8 "math" |
9 "runtime" | 9 "runtime" |
10 "strconv" | 10 "strconv" |
(...skipping 912 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
923 | 923 |
924 // Len returns v's length. | 924 // Len returns v's length. |
925 // It panics if v's Kind is not Array, Chan, Map, Slice, or String. | 925 // It panics if v's Kind is not Array, Chan, Map, Slice, or String. |
926 func (v Value) Len() int { | 926 func (v Value) Len() int { |
927 k := v.kind() | 927 k := v.kind() |
928 switch k { | 928 switch k { |
929 case Array: | 929 case Array: |
930 tt := (*arrayType)(unsafe.Pointer(v.typ)) | 930 tt := (*arrayType)(unsafe.Pointer(v.typ)) |
931 return int(tt.len) | 931 return int(tt.len) |
932 case Chan: | 932 case Chan: |
933 » » return int(chanlen(v.iword())) | 933 » » return chanlen(v.iword()) |
934 case Map: | 934 case Map: |
935 » » return int(maplen(v.iword())) | 935 » » return maplen(v.iword()) |
936 case Slice: | 936 case Slice: |
937 // Slice is bigger than a word; assume flagIndir. | 937 // Slice is bigger than a word; assume flagIndir. |
938 return (*SliceHeader)(v.val).Len | 938 return (*SliceHeader)(v.val).Len |
939 case String: | 939 case String: |
940 // String is bigger than a word; assume flagIndir. | 940 // String is bigger than a word; assume flagIndir. |
941 return (*StringHeader)(v.val).Len | 941 return (*StringHeader)(v.val).Len |
942 } | 942 } |
943 panic(&ValueError{"reflect.Value.Len", k}) | 943 panic(&ValueError{"reflect.Value.Len", k}) |
944 } | 944 } |
945 | 945 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
982 tt := (*mapType)(unsafe.Pointer(v.typ)) | 982 tt := (*mapType)(unsafe.Pointer(v.typ)) |
983 keyType := toCommonType(tt.key) | 983 keyType := toCommonType(tt.key) |
984 | 984 |
985 fl := v.flag & flagRO | 985 fl := v.flag & flagRO |
986 fl |= flag(keyType.Kind()) << flagKindShift | 986 fl |= flag(keyType.Kind()) << flagKindShift |
987 if keyType.size > ptrSize { | 987 if keyType.size > ptrSize { |
988 fl |= flagIndir | 988 fl |= flagIndir |
989 } | 989 } |
990 | 990 |
991 m := v.iword() | 991 m := v.iword() |
992 » mlen := int32(0) | 992 » mlen := int(0) |
993 if m != nil { | 993 if m != nil { |
994 mlen = maplen(m) | 994 mlen = maplen(m) |
995 } | 995 } |
996 it := mapiterinit(v.typ.runtimeType(), m) | 996 it := mapiterinit(v.typ.runtimeType(), m) |
997 a := make([]Value, mlen) | 997 a := make([]Value, mlen) |
998 var i int | 998 var i int |
999 for i = 0; i < len(a); i++ { | 999 for i = 0; i < len(a); i++ { |
1000 keyWord, ok := mapiterkey(it) | 1000 keyWord, ok := mapiterkey(it) |
1001 if !ok { | 1001 if !ok { |
1002 break | 1002 break |
(...skipping 1225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2228 func cvtI2I(v Value, typ Type) Value { | 2228 func cvtI2I(v Value, typ Type) Value { |
2229 if v.IsNil() { | 2229 if v.IsNil() { |
2230 ret := Zero(typ) | 2230 ret := Zero(typ) |
2231 ret.flag |= v.flag & flagRO | 2231 ret.flag |= v.flag & flagRO |
2232 return ret | 2232 return ret |
2233 } | 2233 } |
2234 return cvtT2I(v.Elem(), typ) | 2234 return cvtT2I(v.Elem(), typ) |
2235 } | 2235 } |
2236 | 2236 |
2237 // implemented in ../pkg/runtime | 2237 // implemented in ../pkg/runtime |
2238 func chancap(ch iword) int32 | 2238 func chancap(ch iword) int |
iant
2012/09/24 17:17:07
s/int32/int/
rsc
2012/09/24 18:56:42
Ouch, thanks. Looked for others and didn't find an
| |
2239 func chanclose(ch iword) | 2239 func chanclose(ch iword) |
2240 func chanlen(ch iword) int32 | 2240 func chanlen(ch iword) int |
iant
2012/09/24 17:17:07
s/int32/int/
rsc
2012/09/24 18:56:42
Done.
| |
2241 func chanrecv(t *runtimeType, ch iword, nb bool) (val iword, selected, received bool) | 2241 func chanrecv(t *runtimeType, ch iword, nb bool) (val iword, selected, received bool) |
2242 func chansend(t *runtimeType, ch iword, val iword, nb bool) bool | 2242 func chansend(t *runtimeType, ch iword, val iword, nb bool) bool |
2243 | 2243 |
2244 func makechan(typ *runtimeType, size uint64) (ch iword) | 2244 func makechan(typ *runtimeType, size uint64) (ch iword) |
2245 func makemap(t *runtimeType) (m iword) | 2245 func makemap(t *runtimeType) (m iword) |
2246 func mapaccess(t *runtimeType, m iword, key iword) (val iword, ok bool) | 2246 func mapaccess(t *runtimeType, m iword, key iword) (val iword, ok bool) |
2247 func mapassign(t *runtimeType, m iword, key, val iword, ok bool) | 2247 func mapassign(t *runtimeType, m iword, key, val iword, ok bool) |
2248 func mapiterinit(t *runtimeType, m iword) *byte | 2248 func mapiterinit(t *runtimeType, m iword) *byte |
2249 func mapiterkey(it *byte) (key iword, ok bool) | 2249 func mapiterkey(it *byte) (key iword, ok bool) |
2250 func mapiternext(it *byte) | 2250 func mapiternext(it *byte) |
2251 func maplen(m iword) int32 | 2251 func maplen(m iword) int |
2252 | 2252 |
2253 func call(fn, arg unsafe.Pointer, n uint32) | 2253 func call(fn, arg unsafe.Pointer, n uint32) |
2254 func ifaceE2I(t *runtimeType, src interface{}, dst unsafe.Pointer) | 2254 func ifaceE2I(t *runtimeType, src interface{}, dst unsafe.Pointer) |
2255 | 2255 |
2256 // Dummy annotation marking that the value x escapes, | 2256 // Dummy annotation marking that the value x escapes, |
2257 // for use in cases where the reflect code is so clever that | 2257 // for use in cases where the reflect code is so clever that |
2258 // the compiler cannot follow. | 2258 // the compiler cannot follow. |
2259 func escapes(x interface{}) { | 2259 func escapes(x interface{}) { |
2260 if dummy.b { | 2260 if dummy.b { |
2261 dummy.x = x | 2261 dummy.x = x |
2262 } | 2262 } |
2263 } | 2263 } |
2264 | 2264 |
2265 var dummy struct { | 2265 var dummy struct { |
2266 b bool | 2266 b bool |
2267 x interface{} | 2267 x interface{} |
2268 } | 2268 } |
LEFT | RIGHT |