-
Notifications
You must be signed in to change notification settings - Fork 928
/
Copy pathvalue.go
2063 lines (1786 loc) · 51.2 KB
/
value.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package reflect
import (
"math"
"unsafe"
)
type valueFlags uint8
// Flags list some useful flags that contain some extra information not
// contained in an interface{} directly, like whether this value was exported at
// all (it is possible to read unexported fields using reflection, but it is not
// possible to modify them).
const (
valueFlagIndirect valueFlags = 1 << iota
valueFlagExported
valueFlagEmbedRO
valueFlagStickyRO
valueFlagRO = valueFlagEmbedRO | valueFlagStickyRO
)
func (v valueFlags) ro() valueFlags {
if v&valueFlagRO != 0 {
return valueFlagStickyRO
}
return 0
}
type Value struct {
typecode *rawType
value unsafe.Pointer
flags valueFlags
}
// isIndirect returns whether the value pointer in this Value is always a
// pointer to the value. If it is false, it is only a pointer to the value if
// the value is bigger than a pointer.
func (v Value) isIndirect() bool {
return v.flags&valueFlagIndirect != 0
}
// isExported returns whether the value represented by this Value could be
// accessed without violating type system constraints. For example, it is not
// set for unexported struct fields.
func (v Value) isExported() bool {
return v.flags&valueFlagExported != 0
}
func (v Value) isRO() bool {
return v.flags&(valueFlagRO) != 0
}
func (v Value) checkRO() {
if v.isRO() {
panic("reflect: value is not settable")
}
}
func Indirect(v Value) Value {
if v.Kind() != Ptr {
return v
}
return v.Elem()
}
//go:linkname composeInterface runtime.composeInterface
func composeInterface(unsafe.Pointer, unsafe.Pointer) interface{}
//go:linkname decomposeInterface runtime.decomposeInterface
func decomposeInterface(i interface{}) (unsafe.Pointer, unsafe.Pointer)
func ValueOf(i interface{}) Value {
typecode, value := decomposeInterface(i)
return Value{
typecode: (*rawType)(typecode),
value: value,
flags: valueFlagExported,
}
}
func (v Value) Interface() interface{} {
if !v.isExported() {
panic("(reflect.Value).Interface: unexported")
}
return valueInterfaceUnsafe(v)
}
// valueInterfaceUnsafe is used by the runtime to hash map keys. It should not
// be subject to the isExported check.
func valueInterfaceUnsafe(v Value) interface{} {
if v.typecode.Kind() == Interface {
// The value itself is an interface. This can happen when getting the
// value of a struct field of interface type, like this:
// type T struct {
// X interface{}
// }
return *(*interface{})(v.value)
}
if v.isIndirect() && v.typecode.Size() <= unsafe.Sizeof(uintptr(0)) {
// Value was indirect but must be put back directly in the interface
// value.
var value uintptr
for j := v.typecode.Size(); j != 0; j-- {
value = (value << 8) | uintptr(*(*uint8)(unsafe.Add(v.value, j-1)))
}
v.value = unsafe.Pointer(value)
}
return composeInterface(unsafe.Pointer(v.typecode), v.value)
}
func (v Value) Type() Type {
return v.typecode
}
// IsZero reports whether v is the zero value for its type.
// It panics if the argument is invalid.
func (v Value) IsZero() bool {
switch v.Kind() {
case Bool:
return !v.Bool()
case Int, Int8, Int16, Int32, Int64:
return v.Int() == 0
case Uint, Uint8, Uint16, Uint32, Uint64, Uintptr:
return v.Uint() == 0
case Float32, Float64:
return v.Float() == 0
case Complex64, Complex128:
return v.Complex() == 0
case Array:
for i := 0; i < v.Len(); i++ {
if !v.Index(i).IsZero() {
return false
}
}
return true
case Chan, Func, Interface, Map, Pointer, Slice, UnsafePointer:
return v.IsNil()
case String:
return v.Len() == 0
case Struct:
for i := 0; i < v.NumField(); i++ {
if !v.Field(i).IsZero() && v.Type().Field(i).Name != "_" {
return false
}
}
return true
default:
// This should never happens, but will act as a safeguard for
// later, as a default value doesn't makes sense here.
panic(&ValueError{Method: "reflect.Value.IsZero", Kind: v.Kind()})
}
}
// Internal function only, do not use.
//
// RawType returns the raw, underlying type code. It is used in the runtime
// package and needs to be exported for the runtime package to access it.
func (v Value) RawType() *rawType {
return v.typecode
}
func (v Value) Kind() Kind {
return v.typecode.Kind()
}
// IsNil returns whether the value is the nil value. It panics if the value Kind
// is not a channel, map, pointer, function, slice, or interface.
func (v Value) IsNil() bool {
switch v.Kind() {
case Chan, Map, Ptr, UnsafePointer:
return v.pointer() == nil
case Func:
if v.value == nil {
return true
}
fn := (*funcHeader)(v.value)
return fn.Code == nil
case Slice:
if v.value == nil {
return true
}
slice := (*sliceHeader)(v.value)
return slice.data == nil
case Interface:
val := *(*interface{})(v.value)
return val == nil
default:
panic(&ValueError{Method: "IsNil", Kind: v.Kind()})
}
}
// Pointer returns the underlying pointer of the given value for the following
// types: chan, map, pointer, unsafe.Pointer, slice, func.
func (v Value) Pointer() uintptr {
return uintptr(v.UnsafePointer())
}
// UnsafePointer returns the underlying pointer of the given value for the
// following types: chan, map, pointer, unsafe.Pointer, slice, func.
func (v Value) UnsafePointer() unsafe.Pointer {
switch v.Kind() {
case Chan, Map, Ptr, UnsafePointer:
return v.pointer()
case Slice:
slice := (*sliceHeader)(v.value)
return slice.data
case Func:
fn := (*funcHeader)(v.value)
if fn.Context != nil {
return fn.Context
}
return fn.Code
default:
panic(&ValueError{Method: "UnsafePointer", Kind: v.Kind()})
}
}
// pointer returns the underlying pointer represented by v.
// v.Kind() must be Ptr, Map, Chan, or UnsafePointer
func (v Value) pointer() unsafe.Pointer {
if v.isIndirect() {
return *(*unsafe.Pointer)(v.value)
}
return v.value
}
func (v Value) IsValid() bool {
return v.typecode != nil
}
func (v Value) CanInterface() bool {
return v.isExported() && !v.isRO()
}
func (v Value) CanAddr() bool {
return v.flags&(valueFlagIndirect) == valueFlagIndirect
}
func (v Value) Comparable() bool {
k := v.Kind()
switch k {
case Invalid:
return false
case Array:
switch v.Type().Elem().Kind() {
case Interface, Array, Struct:
for i := 0; i < v.Type().Len(); i++ {
if !v.Index(i).Comparable() {
return false
}
}
return true
}
return v.Type().Comparable()
case Interface:
return v.Elem().Comparable()
case Struct:
for i := 0; i < v.NumField(); i++ {
if !v.Field(i).Comparable() {
return false
}
}
return true
default:
return v.Type().Comparable()
}
}
func (v Value) Addr() Value {
if !v.CanAddr() {
panic("reflect.Value.Addr of unaddressable value")
}
// Preserve flagRO instead of using v.flag.ro() so that
// v.Addr().Elem() is equivalent to v (#32772)
flags := v.flags & (valueFlagExported | valueFlagRO)
return Value{
typecode: pointerTo(v.typecode),
value: v.value,
flags: flags,
}
}
func (v Value) UnsafeAddr() uintptr {
return uintptr(v.Addr().UnsafePointer())
}
func (v Value) CanSet() bool {
return v.flags&(valueFlagExported|valueFlagIndirect|valueFlagRO) == valueFlagExported|valueFlagIndirect
}
func (v Value) Bool() bool {
switch v.Kind() {
case Bool:
if v.isIndirect() {
return *((*bool)(v.value))
} else {
return uintptr(v.value) != 0
}
default:
panic(&ValueError{Method: "Bool", Kind: v.Kind()})
}
}
// CanInt reports whether Uint can be used without panicking.
func (v Value) CanInt() bool {
switch v.Kind() {
case Int, Int8, Int16, Int32, Int64:
return true
default:
return false
}
}
func (v Value) Int() int64 {
switch v.Kind() {
case Int:
if v.isIndirect() || unsafe.Sizeof(int(0)) > unsafe.Sizeof(uintptr(0)) {
return int64(*(*int)(v.value))
} else {
return int64(int(uintptr(v.value)))
}
case Int8:
if v.isIndirect() {
return int64(*(*int8)(v.value))
} else {
return int64(int8(uintptr(v.value)))
}
case Int16:
if v.isIndirect() {
return int64(*(*int16)(v.value))
} else {
return int64(int16(uintptr(v.value)))
}
case Int32:
if v.isIndirect() || unsafe.Sizeof(int32(0)) > unsafe.Sizeof(uintptr(0)) {
return int64(*(*int32)(v.value))
} else {
return int64(int32(uintptr(v.value)))
}
case Int64:
if v.isIndirect() || unsafe.Sizeof(int64(0)) > unsafe.Sizeof(uintptr(0)) {
return int64(*(*int64)(v.value))
} else {
return int64(int64(uintptr(v.value)))
}
default:
panic(&ValueError{Method: "Int", Kind: v.Kind()})
}
}
// CanUint reports whether Uint can be used without panicking.
func (v Value) CanUint() bool {
switch v.Kind() {
case Uint, Uint8, Uint16, Uint32, Uint64, Uintptr:
return true
default:
return false
}
}
func (v Value) Uint() uint64 {
switch v.Kind() {
case Uintptr:
if v.isIndirect() {
return uint64(*(*uintptr)(v.value))
} else {
return uint64(uintptr(v.value))
}
case Uint8:
if v.isIndirect() {
return uint64(*(*uint8)(v.value))
} else {
return uint64(uintptr(v.value))
}
case Uint16:
if v.isIndirect() {
return uint64(*(*uint16)(v.value))
} else {
return uint64(uintptr(v.value))
}
case Uint:
if v.isIndirect() || unsafe.Sizeof(uint(0)) > unsafe.Sizeof(uintptr(0)) {
return uint64(*(*uint)(v.value))
} else {
return uint64(uintptr(v.value))
}
case Uint32:
if v.isIndirect() || unsafe.Sizeof(uint32(0)) > unsafe.Sizeof(uintptr(0)) {
return uint64(*(*uint32)(v.value))
} else {
return uint64(uintptr(v.value))
}
case Uint64:
if v.isIndirect() || unsafe.Sizeof(uint64(0)) > unsafe.Sizeof(uintptr(0)) {
return uint64(*(*uint64)(v.value))
} else {
return uint64(uintptr(v.value))
}
default:
panic(&ValueError{Method: "Uint", Kind: v.Kind()})
}
}
// CanFloat reports whether Float can be used without panicking.
func (v Value) CanFloat() bool {
switch v.Kind() {
case Float32, Float64:
return true
default:
return false
}
}
func (v Value) Float32() float32 {
switch v.Kind() {
case Float32:
if v.isIndirect() || unsafe.Sizeof(float32(0)) > unsafe.Sizeof(uintptr(0)) {
// The float is stored as an external value on systems with 16-bit
// pointers.
return *(*float32)(v.value)
} else {
// The float is directly stored in the interface value on systems
// with 32-bit and 64-bit pointers.
return *(*float32)(unsafe.Pointer(&v.value))
}
case Float64:
return float32(v.Float())
}
panic(&ValueError{Method: "Float", Kind: v.Kind()})
}
func (v Value) Float() float64 {
switch v.Kind() {
case Float32:
if v.isIndirect() || unsafe.Sizeof(float32(0)) > unsafe.Sizeof(uintptr(0)) {
// The float is stored as an external value on systems with 16-bit
// pointers.
return float64(*(*float32)(v.value))
} else {
// The float is directly stored in the interface value on systems
// with 32-bit and 64-bit pointers.
return float64(*(*float32)(unsafe.Pointer(&v.value)))
}
case Float64:
if v.isIndirect() || unsafe.Sizeof(float64(0)) > unsafe.Sizeof(uintptr(0)) {
// For systems with 16-bit and 32-bit pointers.
return *(*float64)(v.value)
} else {
// The float is directly stored in the interface value on systems
// with 64-bit pointers.
return *(*float64)(unsafe.Pointer(&v.value))
}
default:
panic(&ValueError{Method: "Float", Kind: v.Kind()})
}
}
// CanComplex reports whether Complex can be used without panicking.
func (v Value) CanComplex() bool {
switch v.Kind() {
case Complex64, Complex128:
return true
default:
return false
}
}
func (v Value) Complex() complex128 {
switch v.Kind() {
case Complex64:
if v.isIndirect() || unsafe.Sizeof(complex64(0)) > unsafe.Sizeof(uintptr(0)) {
// The complex number is stored as an external value on systems with
// 16-bit and 32-bit pointers.
return complex128(*(*complex64)(v.value))
} else {
// The complex number is directly stored in the interface value on
// systems with 64-bit pointers.
return complex128(*(*complex64)(unsafe.Pointer(&v.value)))
}
case Complex128:
// This is a 128-bit value, which is always stored as an external value.
// It may be stored in the pointer directly on very uncommon
// architectures with 128-bit pointers, however.
return *(*complex128)(v.value)
default:
panic(&ValueError{Method: "Complex", Kind: v.Kind()})
}
}
func (v Value) String() string {
switch v.Kind() {
case String:
// A string value is always bigger than a pointer as it is made of a
// pointer and a length.
return *(*string)(v.value)
default:
// Special case because of the special treatment of .String() in Go.
return "<" + v.typecode.String() + " Value>"
}
}
func (v Value) Bytes() []byte {
switch v.Kind() {
case Slice:
if v.typecode.elem().Kind() != Uint8 {
panic(&ValueError{Method: "Bytes", Kind: v.Kind()})
}
return *(*[]byte)(v.value)
case Array:
v.checkAddressable()
if v.typecode.elem().Kind() != Uint8 {
panic(&ValueError{Method: "Bytes", Kind: v.Kind()})
}
// Small inline arrays are not addressable, so we only have to
// handle addressable arrays which will be stored as pointers
// in v.value
return unsafe.Slice((*byte)(v.value), v.Len())
}
panic(&ValueError{Method: "Bytes", Kind: v.Kind()})
}
func (v Value) Slice(i, j int) Value {
switch v.Kind() {
case Slice:
hdr := *(*sliceHeader)(v.value)
i, j := uintptr(i), uintptr(j)
if j < i || hdr.cap < j {
slicePanic()
}
elemSize := v.typecode.underlying().elem().Size()
hdr.len = j - i
hdr.cap = hdr.cap - i
hdr.data = unsafe.Add(hdr.data, i*elemSize)
return Value{
typecode: v.typecode,
value: unsafe.Pointer(&hdr),
flags: v.flags,
}
case Array:
v.checkAddressable()
buf, length := buflen(v)
i, j := uintptr(i), uintptr(j)
if j < i || length < j {
slicePanic()
}
elemSize := v.typecode.underlying().elem().Size()
var hdr sliceHeader
hdr.len = j - i
hdr.cap = length - i
hdr.data = unsafe.Add(buf, i*elemSize)
sliceType := (*arrayType)(unsafe.Pointer(v.typecode.underlying())).slicePtr
return Value{
typecode: sliceType,
value: unsafe.Pointer(&hdr),
flags: v.flags,
}
case String:
i, j := uintptr(i), uintptr(j)
str := *(*stringHeader)(v.value)
if j < i || str.len < j {
slicePanic()
}
hdr := stringHeader{
data: unsafe.Add(str.data, i),
len: j - i,
}
return Value{
typecode: v.typecode,
value: unsafe.Pointer(&hdr),
flags: v.flags,
}
}
panic(&ValueError{Method: "Slice", Kind: v.Kind()})
}
func (v Value) Slice3(i, j, k int) Value {
switch v.Kind() {
case Slice:
hdr := *(*sliceHeader)(v.value)
i, j, k := uintptr(i), uintptr(j), uintptr(k)
if j < i || k < j || hdr.len < k {
slicePanic()
}
elemSize := v.typecode.underlying().elem().Size()
hdr.len = j - i
hdr.cap = k - i
hdr.data = unsafe.Add(hdr.data, i*elemSize)
return Value{
typecode: v.typecode,
value: unsafe.Pointer(&hdr),
flags: v.flags,
}
case Array:
v.checkAddressable()
buf, length := buflen(v)
i, j, k := uintptr(i), uintptr(j), uintptr(k)
if j < i || k < j || length < k {
slicePanic()
}
elemSize := v.typecode.underlying().elem().Size()
var hdr sliceHeader
hdr.len = j - i
hdr.cap = k - i
hdr.data = unsafe.Add(buf, i*elemSize)
sliceType := (*arrayType)(unsafe.Pointer(v.typecode.underlying())).slicePtr
return Value{
typecode: sliceType,
value: unsafe.Pointer(&hdr),
flags: v.flags,
}
}
panic("unimplemented: (reflect.Value).Slice3()")
}
//go:linkname maplen runtime.hashmapLen
func maplen(p unsafe.Pointer) int
//go:linkname chanlen runtime.chanLen
func chanlen(p unsafe.Pointer) int
// Len returns the length of this value for slices, strings, arrays, channels,
// and maps. For other types, it panics.
func (v Value) Len() int {
switch v.typecode.Kind() {
case Array:
return v.typecode.Len()
case Chan:
return chanlen(v.pointer())
case Map:
return maplen(v.pointer())
case Slice:
return int((*sliceHeader)(v.value).len)
case String:
return int((*stringHeader)(v.value).len)
default:
panic(&ValueError{Method: "Len", Kind: v.Kind()})
}
}
//go:linkname chancap runtime.chanCap
func chancap(p unsafe.Pointer) int
// Cap returns the capacity of this value for arrays, channels and slices.
// For other types, it panics.
func (v Value) Cap() int {
switch v.typecode.Kind() {
case Array:
return v.typecode.Len()
case Chan:
return chancap(v.pointer())
case Slice:
return int((*sliceHeader)(v.value).cap)
default:
panic(&ValueError{Method: "Cap", Kind: v.Kind()})
}
}
//go:linkname mapclear runtime.hashmapClear
func mapclear(p unsafe.Pointer)
// Clear clears the contents of a map or zeros the contents of a slice
//
// It panics if v's Kind is not Map or Slice.
func (v Value) Clear() {
switch v.typecode.Kind() {
case Map:
mapclear(v.pointer())
case Slice:
hdr := (*sliceHeader)(v.value)
elemSize := v.typecode.underlying().elem().Size()
memzero(hdr.data, elemSize*hdr.len)
default:
panic(&ValueError{Method: "Clear", Kind: v.Kind()})
}
}
// NumField returns the number of fields of this struct. It panics for other
// value types.
func (v Value) NumField() int {
return v.typecode.NumField()
}
func (v Value) Elem() Value {
switch v.Kind() {
case Ptr:
ptr := v.pointer()
if ptr == nil {
return Value{}
}
// Don't copy RO flags
flags := (v.flags & (valueFlagIndirect | valueFlagExported)) | valueFlagIndirect
return Value{
typecode: v.typecode.elem(),
value: ptr,
flags: flags,
}
case Interface:
typecode, value := decomposeInterface(*(*interface{})(v.value))
return Value{
typecode: (*rawType)(typecode),
value: value,
flags: v.flags &^ valueFlagIndirect,
}
default:
panic(&ValueError{Method: "Elem", Kind: v.Kind()})
}
}
// Field returns the value of the i'th field of this struct.
func (v Value) Field(i int) Value {
if v.Kind() != Struct {
panic(&ValueError{Method: "Field", Kind: v.Kind()})
}
structField := v.typecode.rawField(i)
// Copy flags but clear EmbedRO; we're not an embedded field anymore
flags := v.flags & ^valueFlagEmbedRO
if structField.PkgPath != "" {
// No PkgPath => not exported.
// Clear exported flag even if the parent was exported.
flags &^= valueFlagExported
// Update the RO flag
if structField.Anonymous {
// Embedded field
flags |= valueFlagEmbedRO
} else {
flags |= valueFlagStickyRO
}
} else {
// Parent field may not have been exported but we are
flags |= valueFlagExported
}
size := v.typecode.Size()
fieldType := structField.Type
fieldSize := fieldType.Size()
if v.isIndirect() || fieldSize > unsafe.Sizeof(uintptr(0)) {
// v.value was already a pointer to the value and it should stay that
// way.
return Value{
flags: flags,
typecode: fieldType,
value: unsafe.Add(v.value, structField.Offset),
}
}
// The fieldSize is smaller than uintptr, which means that the value will
// have to be stored directly in the interface value.
if fieldSize == 0 {
// The struct field is zero sized.
// This is a rare situation, but because it's undefined behavior
// to shift the size of the value (zeroing the value), handle this
// situation explicitly.
return Value{
flags: flags,
typecode: fieldType,
value: unsafe.Pointer(nil),
}
}
if size > unsafe.Sizeof(uintptr(0)) {
// The value was not stored in the interface before but will be
// afterwards, so load the value (from the correct offset) and return
// it.
ptr := unsafe.Add(v.value, structField.Offset)
value := unsafe.Pointer(loadValue(ptr, fieldSize))
return Value{
flags: flags &^ valueFlagIndirect,
typecode: fieldType,
value: value,
}
}
// The value was already stored directly in the interface and it still
// is. Cut out the part of the value that we need.
value := maskAndShift(uintptr(v.value), structField.Offset, fieldSize)
return Value{
flags: flags,
typecode: fieldType,
value: unsafe.Pointer(value),
}
}
var uint8Type = TypeOf(uint8(0)).(*rawType)
func (v Value) Index(i int) Value {
switch v.Kind() {
case Slice:
// Extract an element from the slice.
slice := *(*sliceHeader)(v.value)
if uint(i) >= uint(slice.len) {
panic("reflect: slice index out of range")
}
flags := (v.flags & (valueFlagExported | valueFlagIndirect)) | valueFlagIndirect | v.flags.ro()
elem := Value{
typecode: v.typecode.elem(),
flags: flags,
}
elem.value = unsafe.Add(slice.data, elem.typecode.Size()*uintptr(i)) // pointer to new value
return elem
case String:
// Extract a character from a string.
// A string is never stored directly in the interface, but always as a
// pointer to the string value.
// Keeping valueFlagExported if set, but don't set valueFlagIndirect
// otherwise CanSet will return true for string elements (which is bad,
// strings are read-only).
s := *(*stringHeader)(v.value)
if uint(i) >= uint(s.len) {
panic("reflect: string index out of range")
}
return Value{
typecode: uint8Type,
value: unsafe.Pointer(uintptr(*(*uint8)(unsafe.Add(s.data, i)))),
flags: v.flags & valueFlagExported,
}
case Array:
// Extract an element from the array.
elemType := v.typecode.elem()
elemSize := elemType.Size()
size := v.typecode.Size()
if size == 0 {
// The element size is 0 and/or the length of the array is 0.
return Value{
typecode: v.typecode.elem(),
flags: v.flags,
}
}
if elemSize > unsafe.Sizeof(uintptr(0)) {
// The resulting value doesn't fit in a pointer so must be
// indirect. Also, because size != 0 this implies that the array
// length must be != 0, and thus that the total size is at least
// elemSize.
addr := unsafe.Add(v.value, elemSize*uintptr(i)) // pointer to new value
return Value{
typecode: v.typecode.elem(),
flags: v.flags,
value: addr,
}
}
if size > unsafe.Sizeof(uintptr(0)) || v.isIndirect() {
// The element fits in a pointer, but the array is not stored in the pointer directly.
// Load the value from the pointer.
addr := unsafe.Add(v.value, elemSize*uintptr(i)) // pointer to new value
value := addr
if !v.isIndirect() {
// Use a pointer to the value (don't load the value) if the
// 'indirect' flag is set.
value = unsafe.Pointer(loadValue(addr, elemSize))
}
return Value{
typecode: v.typecode.elem(),
flags: v.flags,
value: value,
}
}
// The value fits in a pointer, so extract it with some shifting and
// masking.
offset := elemSize * uintptr(i)
value := maskAndShift(uintptr(v.value), offset, elemSize)
return Value{
typecode: v.typecode.elem(),
flags: v.flags,
value: unsafe.Pointer(value),
}
default:
panic(&ValueError{Method: "Index", Kind: v.Kind()})
}
}
func (v Value) NumMethod() int {
if v.typecode == nil {
panic(&ValueError{Method: "reflect.Value.NumMethod", Kind: Invalid})
}
return v.typecode.NumMethod()
}
// OverflowFloat reports whether the float64 x cannot be represented by v's type.
// It panics if v's Kind is not Float32 or Float64.
func (v Value) OverflowFloat(x float64) bool {
k := v.Kind()
switch k {
case Float32:
return overflowFloat32(x)
case Float64:
return false
}
panic(&ValueError{Method: "reflect.Value.OverflowFloat", Kind: v.Kind()})
}
func overflowFloat32(x float64) bool {
if x < 0 {
x = -x
}
return math.MaxFloat32 < x && x <= math.MaxFloat64
}
func (v Value) MapKeys() []Value {
if v.Kind() != Map {
panic(&ValueError{Method: "MapKeys", Kind: v.Kind()})
}
// empty map
if v.Len() == 0 {
return nil
}
keys := make([]Value, 0, v.Len())
it := hashmapNewIterator()
k := New(v.typecode.Key())
e := New(v.typecode.Elem())
keyType := v.typecode.key()
keyTypeIsEmptyInterface := keyType.Kind() == Interface && keyType.NumMethod() == 0
shouldUnpackInterface := !keyTypeIsEmptyInterface && keyType.Kind() != String && !keyType.isBinary()
for hashmapNext(v.pointer(), it, k.value, e.value) {
if shouldUnpackInterface {
intf := *(*interface{})(k.value)
v := ValueOf(intf)
keys = append(keys, v)
} else {
keys = append(keys, k.Elem())
}
k = New(v.typecode.Key())
}
return keys
}
//go:linkname hashmapStringGet runtime.hashmapStringGet
func hashmapStringGet(m unsafe.Pointer, key string, value unsafe.Pointer, valueSize uintptr) bool
//go:linkname hashmapBinaryGet runtime.hashmapBinaryGet
func hashmapBinaryGet(m unsafe.Pointer, key, value unsafe.Pointer, valueSize uintptr) bool
//go:linkname hashmapInterfaceGet runtime.hashmapInterfaceGet
func hashmapInterfaceGet(m unsafe.Pointer, key interface{}, value unsafe.Pointer, valueSize uintptr) bool
func (v Value) MapIndex(key Value) Value {
if v.Kind() != Map {
panic(&ValueError{Method: "MapIndex", Kind: v.Kind()})
}
vkey := v.typecode.key()
// compare key type with actual key type of map
if !key.typecode.AssignableTo(vkey) {
// type error?
panic("reflect.Value.MapIndex: incompatible types for key")
}
elemType := v.typecode.Elem()
elem := New(elemType)
if vkey.Kind() == String {
if ok := hashmapStringGet(v.pointer(), *(*string)(key.value), elem.value, elemType.Size()); !ok {
return Value{}
}
return elem.Elem()
} else if vkey.isBinary() {