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.
13 const ptrSize = uintptr(unsafe.Sizeof((*byte)(nil)))
14 const cannotSet = "cannot set value obtained via unexported struct field"
16 type addr unsafe.Pointer
18 // TODO: This will have to go away when
19 // the new gc goes in.
20 func memmove(adst, asrc addr, n uintptr) {
24 case src < dst && src+n > dst:
26 // careful: i is unsigned
29 *(*byte)(addr(dst + i)) = *(*byte)(addr(src + i))
31 case (n|src|dst)&(ptrSize-1) != 0:
33 for i := uintptr(0); i < n; i++ {
34 *(*byte)(addr(dst + i)) = *(*byte)(addr(src + i))
38 for i := uintptr(0); i < n; i += ptrSize {
39 *(*uintptr)(addr(dst + i)) = *(*uintptr)(addr(src + i))
44 // Value is the common interface to reflection values.
45 // The implementations of Value (e.g., ArrayValue, StructValue)
46 // have additional type-specific methods.
47 type Value interface {
48 // Type returns the value's type.
51 // Interface returns the value as an interface{}.
52 Interface() interface{}
54 // CanSet returns whether the value can be changed.
55 // Values obtained by the use of non-exported struct fields
56 // can be used in Get but not Set.
57 // If CanSet() returns false, calling the type-specific Set
58 // will cause a crash.
61 // SetValue assigns v to the value; v must have the same type as the value.
64 // Addr returns a pointer to the underlying data.
65 // It is for advanced clients that also
66 // import the "unsafe" package.
69 // Method returns a FuncValue corresponding to the value's i'th method.
70 // The arguments to a Call on the returned FuncValue
71 // should not include a receiver; the FuncValue will use
72 // the value as the receiver.
73 Method(i int) *FuncValue
78 // value is the common implementation of most values.
79 // It is embedded in other, public struct types, but always
80 // with a unique tag like "uint" or "float" so that the client cannot
81 // convert from, say, *UintValue to *FloatValue.
88 func (v *value) Type() Type { return v.typ }
90 func (v *value) Addr() uintptr { return uintptr(v.addr) }
92 func (v *value) getAddr() addr { return v.addr }
94 func (v *value) Interface() interface{} {
95 if typ, ok := v.typ.(*InterfaceType); ok {
96 // There are two different representations of interface values,
97 // one if the interface type has methods and one if it doesn't.
98 // These two representations require different expressions
99 // to extract correctly.
100 if typ.NumMethod() == 0 {
101 // Extract as interface value without methods.
102 return *(*interface{})(v.addr)
104 // Extract from v.addr as interface value with methods.
105 return *(*interface {
109 return unsafe.Unreflect(v.typ, unsafe.Pointer(v.addr))
112 func (v *value) CanSet() bool { return v.canSet }
118 // BoolValue represents a bool value.
119 type BoolValue struct {
123 // Get returns the underlying bool value.
124 func (v *BoolValue) Get() bool { return *(*bool)(v.addr) }
126 // Set sets v to the value x.
127 func (v *BoolValue) Set(x bool) {
134 // Set sets v to the value x.
135 func (v *BoolValue) SetValue(x Value) { v.Set(x.(*BoolValue).Get()) }
137 // FloatValue represents a float value.
138 type FloatValue struct {
142 // Get returns the underlying int value.
143 func (v *FloatValue) Get() float64 {
144 switch v.typ.Kind() {
146 return float64(*(*float32)(v.addr))
148 return *(*float64)(v.addr)
150 panic("reflect: invalid float kind")
153 // Set sets v to the value x.
154 func (v *FloatValue) Set(x float64) {
158 switch v.typ.Kind() {
160 panic("reflect: invalid float kind")
162 *(*float32)(v.addr) = float32(x)
164 *(*float64)(v.addr) = x
168 // Overflow returns true if x cannot be represented by the type of v.
169 func (v *FloatValue) Overflow(x float64) bool {
170 if v.typ.Size() == 8 {
176 return math.MaxFloat32 < x && x <= math.MaxFloat64
179 // Set sets v to the value x.
180 func (v *FloatValue) SetValue(x Value) { v.Set(x.(*FloatValue).Get()) }
182 // ComplexValue represents a complex value.
183 type ComplexValue struct {
187 // Get returns the underlying complex value.
188 func (v *ComplexValue) Get() complex128 {
189 switch v.typ.Kind() {
191 return complex128(*(*complex64)(v.addr))
193 return *(*complex128)(v.addr)
195 panic("reflect: invalid complex kind")
198 // Set sets v to the value x.
199 func (v *ComplexValue) Set(x complex128) {
203 switch v.typ.Kind() {
205 panic("reflect: invalid complex kind")
207 *(*complex64)(v.addr) = complex64(x)
209 *(*complex128)(v.addr) = x
213 // Set sets v to the value x.
214 func (v *ComplexValue) SetValue(x Value) { v.Set(x.(*ComplexValue).Get()) }
216 // IntValue represents an int value.
217 type IntValue struct {
221 // Get returns the underlying int value.
222 func (v *IntValue) Get() int64 {
223 switch v.typ.Kind() {
225 return int64(*(*int)(v.addr))
227 return int64(*(*int8)(v.addr))
229 return int64(*(*int16)(v.addr))
231 return int64(*(*int32)(v.addr))
233 return *(*int64)(v.addr)
235 panic("reflect: invalid int kind")
238 // Set sets v to the value x.
239 func (v *IntValue) Set(x int64) {
243 switch v.typ.Kind() {
245 panic("reflect: invalid int kind")
247 *(*int)(v.addr) = int(x)
249 *(*int8)(v.addr) = int8(x)
251 *(*int16)(v.addr) = int16(x)
253 *(*int32)(v.addr) = int32(x)
255 *(*int64)(v.addr) = x
259 // Set sets v to the value x.
260 func (v *IntValue) SetValue(x Value) { v.Set(x.(*IntValue).Get()) }
262 // Overflow returns true if x cannot be represented by the type of v.
263 func (v *IntValue) Overflow(x int64) bool {
264 bitSize := uint(v.typ.Bits())
265 trunc := (x << (64 - bitSize)) >> (64 - bitSize)
269 // StringHeader is the runtime representation of a string.
270 type StringHeader struct {
275 // StringValue represents a string value.
276 type StringValue struct {
280 // Get returns the underlying string value.
281 func (v *StringValue) Get() string { return *(*string)(v.addr) }
283 // Set sets v to the value x.
284 func (v *StringValue) Set(x string) {
288 *(*string)(v.addr) = x
291 // Set sets v to the value x.
292 func (v *StringValue) SetValue(x Value) { v.Set(x.(*StringValue).Get()) }
294 // UintValue represents a uint value.
295 type UintValue struct {
299 // Get returns the underlying uuint value.
300 func (v *UintValue) Get() uint64 {
301 switch v.typ.Kind() {
303 return uint64(*(*uint)(v.addr))
305 return uint64(*(*uint8)(v.addr))
307 return uint64(*(*uint16)(v.addr))
309 return uint64(*(*uint32)(v.addr))
311 return *(*uint64)(v.addr)
313 return uint64(*(*uintptr)(v.addr))
315 panic("reflect: invalid uint kind")
318 // Set sets v to the value x.
319 func (v *UintValue) Set(x uint64) {
323 switch v.typ.Kind() {
325 panic("reflect: invalid uint kind")
327 *(*uint)(v.addr) = uint(x)
329 *(*uint8)(v.addr) = uint8(x)
331 *(*uint16)(v.addr) = uint16(x)
333 *(*uint32)(v.addr) = uint32(x)
335 *(*uint64)(v.addr) = x
337 *(*uintptr)(v.addr) = uintptr(x)
341 // Overflow returns true if x cannot be represented by the type of v.
342 func (v *UintValue) Overflow(x uint64) bool {
343 bitSize := uint(v.typ.Bits())
344 trunc := (x << (64 - bitSize)) >> (64 - bitSize)
348 // Set sets v to the value x.
349 func (v *UintValue) SetValue(x Value) { v.Set(x.(*UintValue).Get()) }
351 // UnsafePointerValue represents an unsafe.Pointer value.
352 type UnsafePointerValue struct {
353 value "unsafe.Pointer"
356 // Get returns the underlying uintptr value.
357 // Get returns uintptr, not unsafe.Pointer, so that
358 // programs that do not import "unsafe" cannot
359 // obtain a value of unsafe.Pointer type from "reflect".
360 func (v *UnsafePointerValue) Get() uintptr { return uintptr(*(*unsafe.Pointer)(v.addr)) }
362 // Set sets v to the value x.
363 func (v *UnsafePointerValue) Set(x unsafe.Pointer) {
367 *(*unsafe.Pointer)(v.addr) = x
370 // Set sets v to the value x.
371 func (v *UnsafePointerValue) SetValue(x Value) {
372 v.Set(unsafe.Pointer(x.(*UnsafePointerValue).Get()))
375 func typesMustMatch(t1, t2 Type) {
377 panic("type mismatch: " + t1.String() + " != " + t2.String())
385 // ArrayOrSliceValue is the common interface
386 // implemented by both ArrayValue and SliceValue.
387 type ArrayOrSliceValue interface {
395 // grow grows the slice s so that it can hold extra more values, allocating
396 // more capacity if needed. It also returns the old and new slice lengths.
397 func grow(s *SliceValue, extra int) (*SliceValue, int, int) {
401 panic("append: slice overflow")
405 return s.Slice(0, i1), i0, i1
418 t := MakeSlice(s.Type().(*SliceType), i1, m)
423 // Append appends the values x to a slice s and returns the resulting slice.
424 // Each x must have the same type as s' element type.
425 func Append(s *SliceValue, x ...Value) *SliceValue {
426 s, i0, i1 := grow(s, len(x))
427 for i, j := i0, 0; i < i1; i, j = i+1, j+1 {
428 s.Elem(i).SetValue(x[j])
433 // AppendSlice appends a slice t to a slice s and returns the resulting slice.
434 // The slices s and t must have the same element type.
435 func AppendSlice(s, t *SliceValue) *SliceValue {
436 s, i0, i1 := grow(s, t.Len())
437 Copy(s.Slice(i0, i1), t)
441 // Copy copies the contents of src into dst until either
442 // dst has been filled or src has been exhausted.
443 // It returns the number of elements copied.
444 // The arrays dst and src must have the same element type.
445 func Copy(dst, src ArrayOrSliceValue) int {
446 // TODO: This will have to move into the runtime
447 // once the real gc goes in.
448 de := dst.Type().(ArrayOrSliceType).Elem()
449 se := src.Type().(ArrayOrSliceType).Elem()
450 typesMustMatch(de, se)
452 if xn := src.Len(); n > xn {
455 memmove(dst.addr(), src.addr(), uintptr(n)*de.Size())
459 // An ArrayValue represents an array.
460 type ArrayValue struct {
464 // Len returns the length of the array.
465 func (v *ArrayValue) Len() int { return v.typ.(*ArrayType).Len() }
467 // Cap returns the capacity of the array (equal to Len()).
468 func (v *ArrayValue) Cap() int { return v.typ.(*ArrayType).Len() }
470 // addr returns the base address of the data in the array.
471 func (v *ArrayValue) addr() addr { return v.value.addr }
473 // Set assigns x to v.
474 // The new value x must have the same type as v.
475 func (v *ArrayValue) Set(x *ArrayValue) {
479 typesMustMatch(v.typ, x.typ)
483 // Set sets v to the value x.
484 func (v *ArrayValue) SetValue(x Value) { v.Set(x.(*ArrayValue)) }
486 // Elem returns the i'th element of v.
487 func (v *ArrayValue) Elem(i int) Value {
488 typ := v.typ.(*ArrayType).Elem()
491 panic("array index out of bounds")
493 p := addr(uintptr(v.addr()) + uintptr(i)*typ.Size())
494 return newValue(typ, p, v.canSet)
501 // runtime representation of slice
502 type SliceHeader struct {
508 // A SliceValue represents a slice.
509 type SliceValue struct {
513 func (v *SliceValue) slice() *SliceHeader { return (*SliceHeader)(v.value.addr) }
515 // IsNil returns whether v is a nil slice.
516 func (v *SliceValue) IsNil() bool { return v.slice().Data == 0 }
518 // Len returns the length of the slice.
519 func (v *SliceValue) Len() int { return int(v.slice().Len) }
521 // Cap returns the capacity of the slice.
522 func (v *SliceValue) Cap() int { return int(v.slice().Cap) }
524 // addr returns the base address of the data in the slice.
525 func (v *SliceValue) addr() addr { return addr(v.slice().Data) }
527 // SetLen changes the length of v.
528 // The new length n must be between 0 and the capacity, inclusive.
529 func (v *SliceValue) SetLen(n int) {
531 if n < 0 || n > int(s.Cap) {
532 panic("reflect: slice length out of range in SetLen")
537 // Set assigns x to v.
538 // The new value x must have the same type as v.
539 func (v *SliceValue) Set(x *SliceValue) {
543 typesMustMatch(v.typ, x.typ)
544 *v.slice() = *x.slice()
547 // Set sets v to the value x.
548 func (v *SliceValue) SetValue(x Value) { v.Set(x.(*SliceValue)) }
550 // Get returns the uintptr address of the v.Cap()'th element. This gives
551 // the same result for all slices of the same array.
552 // It is mainly useful for printing.
553 func (v *SliceValue) Get() uintptr {
554 typ := v.typ.(*SliceType)
555 return uintptr(v.addr()) + uintptr(v.Cap())*typ.Elem().Size()
558 // Slice returns a sub-slice of the slice v.
559 func (v *SliceValue) Slice(beg, end int) *SliceValue {
561 if beg < 0 || end < beg || end > cap {
562 panic("slice index out of bounds")
564 typ := v.typ.(*SliceType)
565 s := new(SliceHeader)
566 s.Data = uintptr(v.addr()) + uintptr(beg)*typ.Elem().Size()
569 return newValue(typ, addr(s), v.canSet).(*SliceValue)
572 // Elem returns the i'th element of v.
573 func (v *SliceValue) Elem(i int) Value {
574 typ := v.typ.(*SliceType).Elem()
577 panic("reflect: slice index out of range")
579 p := addr(uintptr(v.addr()) + uintptr(i)*typ.Size())
580 return newValue(typ, p, v.canSet)
583 // MakeSlice creates a new zero-initialized slice value
584 // for the specified slice type, length, and capacity.
585 func MakeSlice(typ *SliceType, len, cap int) *SliceValue {
587 Data: uintptr(unsafe.NewArray(typ.Elem(), cap)),
591 return newValue(typ, addr(s), true).(*SliceValue)
598 // A ChanValue represents a chan.
599 type ChanValue struct {
603 // IsNil returns whether v is a nil channel.
604 func (v *ChanValue) IsNil() bool { return *(*uintptr)(v.addr) == 0 }
606 // Set assigns x to v.
607 // The new value x must have the same type as v.
608 func (v *ChanValue) Set(x *ChanValue) {
612 typesMustMatch(v.typ, x.typ)
613 *(*uintptr)(v.addr) = *(*uintptr)(x.addr)
616 // Set sets v to the value x.
617 func (v *ChanValue) SetValue(x Value) { v.Set(x.(*ChanValue)) }
619 // Get returns the uintptr value of v.
620 // It is mainly useful for printing.
621 func (v *ChanValue) Get() uintptr { return *(*uintptr)(v.addr) }
623 // implemented in ../pkg/runtime/reflect.cgo
624 func makechan(typ *runtime.ChanType, size uint32) (ch *byte)
625 func chansend(ch, val *byte, pres *bool)
626 func chanrecv(ch, val *byte, pres *bool)
627 func chanclosed(ch *byte) bool
628 func chanclose(ch *byte)
629 func chanlen(ch *byte) int32
630 func chancap(ch *byte) int32
632 // Closed returns the result of closed(c) on the underlying channel.
633 func (v *ChanValue) Closed() bool {
634 ch := *(**byte)(v.addr)
635 return chanclosed(ch)
638 // Close closes the channel.
639 func (v *ChanValue) Close() {
640 ch := *(**byte)(v.addr)
644 func (v *ChanValue) Len() int {
645 ch := *(**byte)(v.addr)
646 return int(chanlen(ch))
649 func (v *ChanValue) Cap() int {
650 ch := *(**byte)(v.addr)
651 return int(chancap(ch))
654 // internal send; non-blocking if b != nil
655 func (v *ChanValue) send(x Value, b *bool) {
656 t := v.Type().(*ChanType)
657 if t.Dir()&SendDir == 0 {
658 panic("send on recv-only channel")
660 typesMustMatch(t.Elem(), x.Type())
661 ch := *(**byte)(v.addr)
662 chansend(ch, (*byte)(x.getAddr()), b)
665 // internal recv; non-blocking if b != nil
666 func (v *ChanValue) recv(b *bool) Value {
667 t := v.Type().(*ChanType)
668 if t.Dir()&RecvDir == 0 {
669 panic("recv on send-only channel")
671 ch := *(**byte)(v.addr)
672 x := MakeZero(t.Elem())
673 chanrecv(ch, (*byte)(x.getAddr()), b)
677 // Send sends x on the channel v.
678 func (v *ChanValue) Send(x Value) { v.send(x, nil) }
680 // Recv receives and returns a value from the channel v.
681 func (v *ChanValue) Recv() Value { return v.recv(nil) }
683 // TrySend attempts to sends x on the channel v but will not block.
684 // It returns true if the value was sent, false otherwise.
685 func (v *ChanValue) TrySend(x Value) bool {
691 // TryRecv attempts to receive a value from the channel v but will not block.
692 // It returns the value if one is received, nil otherwise.
693 func (v *ChanValue) TryRecv() Value {
702 // MakeChan creates a new channel with the specified type and buffer size.
703 func MakeChan(typ *ChanType, buffer int) *ChanValue {
705 panic("MakeChan: negative buffer size")
707 if typ.Dir() != BothDir {
708 panic("MakeChan: unidirectional channel type")
710 v := MakeZero(typ).(*ChanValue)
711 *(**byte)(v.addr) = makechan((*runtime.ChanType)(unsafe.Pointer(typ)), uint32(buffer))
719 // A FuncValue represents a function value.
720 type FuncValue struct {
726 // IsNil returns whether v is a nil function.
727 func (v *FuncValue) IsNil() bool { return *(*uintptr)(v.addr) == 0 }
729 // Get returns the uintptr value of v.
730 // It is mainly useful for printing.
731 func (v *FuncValue) Get() uintptr { return *(*uintptr)(v.addr) }
733 // Set assigns x to v.
734 // The new value x must have the same type as v.
735 func (v *FuncValue) Set(x *FuncValue) {
739 typesMustMatch(v.typ, x.typ)
740 *(*uintptr)(v.addr) = *(*uintptr)(x.addr)
743 // Set sets v to the value x.
744 func (v *FuncValue) SetValue(x Value) { v.Set(x.(*FuncValue)) }
746 // Method returns a FuncValue corresponding to v's i'th method.
747 // The arguments to a Call on the returned FuncValue
748 // should not include a receiver; the FuncValue will use v
750 func (v *value) Method(i int) *FuncValue {
751 t := v.Type().uncommon()
752 if t == nil || i < 0 || i >= len(t.methods) {
757 fv := &FuncValue{value: value{runtimeToType(p.typ), addr(&fn), true}, first: v, isInterface: false}
761 // implemented in ../pkg/runtime/*/asm.s
762 func call(typ *FuncType, fnaddr *byte, isInterface bool, params *addr, results *addr)
764 // Call calls the function fv with input parameters in.
765 // It returns the function's output parameters as Values.
766 func (fv *FuncValue) Call(in []Value) []Value {
767 t := fv.Type().(*FuncType)
769 if fv.first != nil && !fv.isInterface {
772 if nin != t.NumIn() {
773 panic("FuncValue: wrong argument count")
775 if fv.first != nil && fv.isInterface {
780 params := make([]addr, nin)
783 if v := fv.first; v != nil {
784 // Hard-wired first argument.
786 // v is a single uninterpreted word
787 params[0] = v.getAddr()
792 // This is a method, so we need to always pass
795 if ptv, ok := tv.(*PtrType); ok {
796 typesMustMatch(t.In(0), tv)
801 typesMustMatch(t.In(0).(*PtrType).Elem(), tv)
809 for i, v := range in {
811 tf := t.In(i + delta)
813 // If this is really a method, and we are explicitly
814 // passing the object, then we need to pass the address
815 // of the object instead. Unfortunately, we don't
816 // have any way to know that this is a method, so we just
817 // check the type. FIXME: This is ugly.
819 if i == 0 && tf != tv {
820 if ptf, ok := tf.(*PtrType); ok {
828 typesMustMatch(tf, tv)
829 params[i+off] = vAddr
832 ret := make([]Value, nout)
833 results := make([]addr, nout)
834 for i := 0; i < nout; i++ {
837 results[i] = v.getAddr()
841 call(t, *(**byte)(fv.addr), fv.isInterface, ¶ms[0], &results[0])
850 // An InterfaceValue represents an interface value.
851 type InterfaceValue struct {
855 // IsNil returns whether v is a nil interface value.
856 func (v *InterfaceValue) IsNil() bool { return v.Interface() == nil }
858 // No single uinptr Get because v.Interface() is available.
860 // Get returns the two words that represent an interface in the runtime.
861 // Those words are useful only when playing unsafe games.
862 func (v *InterfaceValue) Get() [2]uintptr {
863 return *(*[2]uintptr)(v.addr)
866 // Elem returns the concrete value stored in the interface value v.
867 func (v *InterfaceValue) Elem() Value { return NewValue(v.Interface()) }
869 // ../runtime/reflect.cgo
870 func setiface(typ *InterfaceType, x *interface{}, addr addr)
872 // Set assigns x to v.
873 func (v *InterfaceValue) Set(x Value) {
881 // Two different representations; see comment in Get.
882 // Empty interface is easy.
883 t := v.typ.(*InterfaceType)
884 if t.NumMethod() == 0 {
885 *(*interface{})(v.addr) = i
889 // Non-empty interface requires a runtime check.
890 setiface(t, &i, v.addr)
893 // Set sets v to the value x.
894 func (v *InterfaceValue) SetValue(x Value) { v.Set(x) }
896 // Method returns a FuncValue corresponding to v's i'th method.
897 // The arguments to a Call on the returned FuncValue
898 // should not include a receiver; the FuncValue will use v
900 func (v *InterfaceValue) Method(i int) *FuncValue {
901 t := v.Type().(*InterfaceType)
902 if t == nil || i < 0 || i >= len(t.methods) {
907 // Interface is two words: itable, data.
908 tab := *(**[10000]addr)(v.addr)
909 data := &value{Typeof((*byte)(nil)), addr(uintptr(v.addr) + ptrSize), true}
912 fv := &FuncValue{value: value{runtimeToType(p.typ), addr(&fn), true}, first: data, isInterface: true}
920 // A MapValue represents a map value.
921 type MapValue struct {
925 // IsNil returns whether v is a nil map value.
926 func (v *MapValue) IsNil() bool { return *(*uintptr)(v.addr) == 0 }
928 // Set assigns x to v.
929 // The new value x must have the same type as v.
930 func (v *MapValue) Set(x *MapValue) {
935 *(**uintptr)(v.addr) = nil
938 typesMustMatch(v.typ, x.typ)
939 *(*uintptr)(v.addr) = *(*uintptr)(x.addr)
942 // Set sets v to the value x.
943 func (v *MapValue) SetValue(x Value) {
951 // Get returns the uintptr value of v.
952 // It is mainly useful for printing.
953 func (v *MapValue) Get() uintptr { return *(*uintptr)(v.addr) }
955 // implemented in ../pkg/runtime/reflect.cgo
956 func mapaccess(m, key, val *byte) bool
957 func mapassign(m, key, val *byte)
958 func maplen(m *byte) int32
959 func mapiterinit(m *byte) *byte
960 func mapiternext(it *byte)
961 func mapiterkey(it *byte, key *byte) bool
962 func makemap(t *runtime.MapType) *byte
964 // Elem returns the value associated with key in the map v.
965 // It returns nil if key is not found in the map.
966 func (v *MapValue) Elem(key Value) Value {
967 t := v.Type().(*MapType)
968 typesMustMatch(t.Key(), key.Type())
969 m := *(**byte)(v.addr)
973 newval := MakeZero(t.Elem())
974 if !mapaccess(m, (*byte)(key.getAddr()), (*byte)(newval.getAddr())) {
980 // SetElem sets the value associated with key in the map v to val.
981 // If val is nil, Put deletes the key from map.
982 func (v *MapValue) SetElem(key, val Value) {
983 t := v.Type().(*MapType)
984 typesMustMatch(t.Key(), key.Type())
987 typesMustMatch(t.Elem(), val.Type())
988 vaddr = (*byte)(val.getAddr())
990 m := *(**byte)(v.addr)
991 mapassign(m, (*byte)(key.getAddr()), vaddr)
994 // Len returns the number of keys in the map v.
995 func (v *MapValue) Len() int {
996 m := *(**byte)(v.addr)
1000 return int(maplen(m))
1003 // Keys returns a slice containing all the keys present in the map,
1004 // in unspecified order.
1005 func (v *MapValue) Keys() []Value {
1006 tk := v.Type().(*MapType).Key()
1007 m := *(**byte)(v.addr)
1012 it := mapiterinit(m)
1013 a := make([]Value, mlen)
1015 for i = 0; i < len(a); i++ {
1017 if !mapiterkey(it, (*byte)(k.getAddr())) {
1026 // MakeMap creates a new map of the specified type.
1027 func MakeMap(typ *MapType) *MapValue {
1028 v := MakeZero(typ).(*MapValue)
1029 *(**byte)(v.addr) = makemap((*runtime.MapType)(unsafe.Pointer(typ)))
1037 // A PtrValue represents a pointer.
1038 type PtrValue struct {
1042 // IsNil returns whether v is a nil pointer.
1043 func (v *PtrValue) IsNil() bool { return *(*uintptr)(v.addr) == 0 }
1045 // Get returns the uintptr value of v.
1046 // It is mainly useful for printing.
1047 func (v *PtrValue) Get() uintptr { return *(*uintptr)(v.addr) }
1049 // Set assigns x to v.
1050 // The new value x must have the same type as v.
1051 func (v *PtrValue) Set(x *PtrValue) {
1053 *(**uintptr)(v.addr) = nil
1059 typesMustMatch(v.typ, x.typ)
1060 // TODO: This will have to move into the runtime
1061 // once the new gc goes in
1062 *(*uintptr)(v.addr) = *(*uintptr)(x.addr)
1065 // Set sets v to the value x.
1066 func (v *PtrValue) SetValue(x Value) {
1071 v.Set(x.(*PtrValue))
1074 // PointTo changes v to point to x.
1075 // If x is a nil Value, PointTo sets v to nil.
1076 func (v *PtrValue) PointTo(x Value) {
1078 *(**uintptr)(v.addr) = nil
1082 panic("cannot set x; cannot point to x")
1084 typesMustMatch(v.typ.(*PtrType).Elem(), x.Type())
1085 // TODO: This will have to move into the runtime
1086 // once the new gc goes in.
1087 *(*uintptr)(v.addr) = x.Addr()
1090 // Elem returns the value that v points to.
1091 // If v is a nil pointer, Elem returns a nil Value.
1092 func (v *PtrValue) Elem() Value {
1096 return newValue(v.typ.(*PtrType).Elem(), *(*addr)(v.addr), v.canSet)
1099 // Indirect returns the value that v points to.
1100 // If v is a nil pointer, Indirect returns a nil Value.
1101 // If v is not a pointer, Indirect returns v.
1102 func Indirect(v Value) Value {
1103 if pv, ok := v.(*PtrValue); ok {
1113 // A StructValue represents a struct value.
1114 type StructValue struct {
1118 // Set assigns x to v.
1119 // The new value x must have the same type as v.
1120 func (v *StructValue) Set(x *StructValue) {
1121 // TODO: This will have to move into the runtime
1122 // once the gc goes in.
1126 typesMustMatch(v.typ, x.typ)
1127 memmove(v.addr, x.addr, v.typ.Size())
1130 // Set sets v to the value x.
1131 func (v *StructValue) SetValue(x Value) { v.Set(x.(*StructValue)) }
1133 // Field returns the i'th field of the struct.
1134 func (v *StructValue) Field(i int) Value {
1135 t := v.typ.(*StructType)
1136 if i < 0 || i >= t.NumField() {
1140 return newValue(f.Type, addr(uintptr(v.addr)+f.Offset), v.canSet && f.PkgPath == "")
1143 // FieldByIndex returns the nested field corresponding to index.
1144 func (t *StructValue) FieldByIndex(index []int) (v Value) {
1146 for i, x := range index {
1148 if p, ok := v.(*PtrValue); ok {
1151 if s, ok := v.(*StructValue); ok {
1163 // FieldByName returns the struct field with the given name.
1164 // The result is nil if no field was found.
1165 func (t *StructValue) FieldByName(name string) Value {
1166 if f, ok := t.Type().(*StructType).FieldByName(name); ok {
1167 return t.FieldByIndex(f.Index)
1172 // FieldByNameFunc returns the struct field with a name that satisfies the
1174 // The result is nil if no field was found.
1175 func (t *StructValue) FieldByNameFunc(match func(string) bool) Value {
1176 if f, ok := t.Type().(*StructType).FieldByNameFunc(match); ok {
1177 return t.FieldByIndex(f.Index)
1182 // NumField returns the number of fields in the struct.
1183 func (v *StructValue) NumField() int { return v.typ.(*StructType).NumField() }
1189 // NewValue returns a new Value initialized to the concrete value
1190 // stored in the interface i. NewValue(nil) returns nil.
1191 func NewValue(i interface{}) Value {
1195 t, a := unsafe.Reflect(i)
1196 return newValue(canonicalize(toType(t)), addr(a), true)
1199 func newValue(typ Type, addr addr, canSet bool) Value {
1200 v := value{typ, addr, canSet}
1203 return &ArrayValue{v}
1205 return &BoolValue{v}
1207 return &ChanValue{v}
1209 return &FloatValue{v}
1211 return &FuncValue{value: v}
1213 return &ComplexValue{v}
1216 case *InterfaceType:
1217 return &InterfaceValue{v}
1223 return &SliceValue{v}
1225 return &StringValue{v}
1227 return &StructValue{v}
1229 return &UintValue{v}
1230 case *UnsafePointerType:
1231 return &UnsafePointerValue{v}
1233 panic("newValue" + typ.String())
1236 // MakeZero returns a zero Value for the specified Type.
1237 func MakeZero(typ Type) Value {
1241 return newValue(typ, addr(unsafe.New(typ)), true)