import (
"runtime"
"strconv"
+ "sync"
"unsafe"
)
// NumMethods returns the number of methods in the type's method set.
NumMethod() int
+
+ common() *commonType
uncommon() *uncommonType
}
func (t *commonType) Kind() Kind { return Kind(t.kind & kindMask) }
+func (t *commonType) common() *commonType { return t }
+
func (t *uncommonType) Method(i int) (m Method) {
if t == nil || i < 0 || i >= len(t.methods) {
return
}
m.Type = toType(*p.typ).(*FuncType)
fn := p.tfn
- m.Func = &FuncValue{value: value{m.Type, addr(&fn), true}}
+ m.Func = &FuncValue{value: value{m.Type, addr(&fn), canSet}}
return
}
// Typeof returns the reflection Type of the value in the interface{}.
func Typeof(i interface{}) Type { return toType(unsafe.Typeof(i)) }
+
+// ptrMap is the cache for PtrTo.
+var ptrMap struct {
+ sync.RWMutex
+ m map[Type]*PtrType
+}
+
+// runtimePtrType is the runtime layout for a *PtrType.
+// The memory immediately before the *PtrType is always
+// the canonical runtime.Type to be used for a *runtime.Type
+// describing this PtrType.
+type runtimePtrType struct {
+ runtime.Type
+ runtime.PtrType
+}
+
+// PtrTo returns the pointer type with element t.
+// For example, if t represents type Foo, PtrTo(t) represents *Foo.
+func PtrTo(t Type) *PtrType {
+ // If t records its pointer-to type, use it.
+ ct := t.common()
+ if p := ct.ptrToThis; p != nil {
+ return toType(*p).(*PtrType)
+ }
+
+ // Otherwise, synthesize one.
+ // This only happens for pointers with no methods.
+ // We keep the mapping in a map on the side, because
+ // this operation is rare and a separate map lets us keep
+ // the type structures in read-only memory.
+ ptrMap.RLock()
+ if m := ptrMap.m; m != nil {
+ if p := m[t]; p != nil {
+ ptrMap.RUnlock()
+ return p
+ }
+ }
+ ptrMap.RUnlock()
+ ptrMap.Lock()
+ if ptrMap.m == nil {
+ ptrMap.m = make(map[Type]*PtrType)
+ }
+ p := ptrMap.m[t]
+ if p != nil {
+ // some other goroutine won the race and created it
+ ptrMap.Unlock()
+ return p
+ }
+
+ // runtime.Type value is always right before type structure.
+ // 2*ptrSize is size of interface header
+ rt := (*runtime.Type)(unsafe.Pointer(uintptr(unsafe.Pointer(ct)) - uintptr(unsafe.Sizeof(runtime.Type(nil)))))
+
+ rp := new(runtimePtrType)
+ rp.Type = &rp.PtrType
+
+ // initialize rp.PtrType using *byte's PtrType as a prototype.
+ // have to do assignment as PtrType, not runtime.PtrType,
+ // in order to write to unexported fields.
+ p = (*PtrType)(unsafe.Pointer(&rp.PtrType))
+ bp := (*PtrType)(unsafe.Pointer(unsafe.Typeof((*byte)(nil)).(*runtime.PtrType)))
+ *p = *bp
+
+ s := "*" + *ct.string
+ p.string = &s
+
+ // For the type structures linked into the binary, the
+ // compiler provides a good hash of the string.
+ // Create a good hash for the new string by using
+ // the FNV-1 hash's mixing function to combine the
+ // old hash and the new "*".
+ p.hash = ct.hash*16777619 ^ '*'
+
+ p.uncommonType = nil
+ p.ptrToThis = nil
+ p.elem = rt
+
+ ptrMap.m[t] = (*PtrType)(unsafe.Pointer(&rp.PtrType))
+ ptrMap.Unlock()
+ return p
+}
)
const ptrSize = uintptr(unsafe.Sizeof((*byte)(nil)))
-const cannotSet = "cannot set value obtained via unexported struct field"
+const cannotSet = "cannot set value obtained from unexported struct field"
type addr unsafe.Pointer
// Interface returns the value as an interface{}.
Interface() interface{}
- // CanSet returns whether the value can be changed.
+ // CanSet returns true if the value can be changed.
// Values obtained by the use of non-exported struct fields
// can be used in Get but not Set.
- // If CanSet() returns false, calling the type-specific Set
- // will cause a crash.
+ // If CanSet returns false, calling the type-specific Set will panic.
CanSet() bool
// SetValue assigns v to the value; v must have the same type as the value.
SetValue(v Value)
- // Addr returns a pointer to the underlying data.
- // It is for advanced clients that also
- // import the "unsafe" package.
- Addr() uintptr
+ // CanAddr returns true if the value's address can be obtained with Addr.
+ // Such values are called addressable. A value is addressable if it is
+ // an element of a slice, an element of an addressable array,
+ // a field of an addressable struct, the result of dereferencing a pointer,
+ // or the result of a call to NewValue, MakeChan, MakeMap, or MakeZero.
+ // If CanAddr returns false, calling Addr will panic.
+ CanAddr() bool
+
+ // Addr returns the address of the value.
+ // If the value is not addressable, Addr panics.
+ // Addr is typically used to obtain a pointer to a struct field or slice element
+ // in order to call a method that requires a pointer receiver.
+ Addr() *PtrValue
+
+ // UnsafeAddr returns a pointer to the underlying data.
+ // It is for advanced clients that also import the "unsafe" package.
+ UnsafeAddr() uintptr
// Method returns a FuncValue corresponding to the value's i'th method.
// The arguments to a Call on the returned FuncValue
getAddr() addr
}
+// flags for value
+const (
+ canSet uint32 = 1 << iota // can set value (write to *v.addr)
+ canAddr // can take address of value
+ canStore // can store through value (write to **v.addr)
+)
+
// value is the common implementation of most values.
// It is embedded in other, public struct types, but always
// with a unique tag like "uint" or "float" so that the client cannot
// convert from, say, *UintValue to *FloatValue.
type value struct {
- typ Type
- addr addr
- canSet bool
+ typ Type
+ addr addr
+ flag uint32
}
func (v *value) Type() Type { return v.typ }
-func (v *value) Addr() uintptr { return uintptr(v.addr) }
+func (v *value) Addr() *PtrValue {
+ if !v.CanAddr() {
+ panic("reflect: cannot take address of value")
+ }
+ a := v.addr
+ flag := canSet
+ if v.CanSet() {
+ flag |= canStore
+ }
+ // We could safely set canAddr here too -
+ // the caller would get the address of a -
+ // but it doesn't match the Go model.
+ // The language doesn't let you say &&v.
+ return newValue(PtrTo(v.typ), addr(&a), flag).(*PtrValue)
+}
+
+func (v *value) UnsafeAddr() uintptr { return uintptr(v.addr) }
func (v *value) getAddr() addr { return v.addr }
return unsafe.Unreflect(v.typ, unsafe.Pointer(v.addr))
}
-func (v *value) CanSet() bool { return v.canSet }
+func (v *value) CanSet() bool { return v.flag&canSet != 0 }
+
+func (v *value) CanAddr() bool { return v.flag&canAddr != 0 }
+
/*
* basic types
// Set sets v to the value x.
func (v *BoolValue) Set(x bool) {
- if !v.canSet {
+ if !v.CanSet() {
panic(cannotSet)
}
*(*bool)(v.addr) = x
// Set sets v to the value x.
func (v *FloatValue) Set(x float64) {
- if !v.canSet {
+ if !v.CanSet() {
panic(cannotSet)
}
switch v.typ.Kind() {
// Set sets v to the value x.
func (v *ComplexValue) Set(x complex128) {
- if !v.canSet {
+ if !v.CanSet() {
panic(cannotSet)
}
switch v.typ.Kind() {
// Set sets v to the value x.
func (v *IntValue) Set(x int64) {
- if !v.canSet {
+ if !v.CanSet() {
panic(cannotSet)
}
switch v.typ.Kind() {
// Set sets v to the value x.
func (v *StringValue) Set(x string) {
- if !v.canSet {
+ if !v.CanSet() {
panic(cannotSet)
}
*(*string)(v.addr) = x
// Set sets v to the value x.
func (v *UintValue) Set(x uint64) {
- if !v.canSet {
+ if !v.CanSet() {
panic(cannotSet)
}
switch v.typ.Kind() {
// Set sets v to the value x.
func (v *UnsafePointerValue) Set(x unsafe.Pointer) {
- if !v.canSet {
+ if !v.CanSet() {
panic(cannotSet)
}
*(*unsafe.Pointer)(v.addr) = x
// Set assigns x to v.
// The new value x must have the same type as v.
func (v *ArrayValue) Set(x *ArrayValue) {
- if !v.canSet {
+ if !v.CanSet() {
panic(cannotSet)
}
typesMustMatch(v.typ, x.typ)
panic("array index out of bounds")
}
p := addr(uintptr(v.addr()) + uintptr(i)*typ.Size())
- return newValue(typ, p, v.canSet)
+ return newValue(typ, p, v.flag)
}
/*
// Set assigns x to v.
// The new value x must have the same type as v.
func (v *SliceValue) Set(x *SliceValue) {
- if !v.canSet {
+ if !v.CanSet() {
panic(cannotSet)
}
typesMustMatch(v.typ, x.typ)
s.Data = uintptr(v.addr()) + uintptr(beg)*typ.Elem().Size()
s.Len = end - beg
s.Cap = cap - beg
- return newValue(typ, addr(s), v.canSet).(*SliceValue)
+
+ // Like the result of Addr, we treat Slice as an
+ // unaddressable temporary, so don't set canAddr.
+ flag := canSet
+ if v.flag&canStore != 0 {
+ flag |= canStore
+ }
+ return newValue(typ, addr(s), flag).(*SliceValue)
}
// Elem returns the i'th element of v.
panic("reflect: slice index out of range")
}
p := addr(uintptr(v.addr()) + uintptr(i)*typ.Size())
- return newValue(typ, p, v.canSet)
+ flag := canAddr
+ if v.flag&canStore != 0 {
+ flag |= canSet | canStore
+ }
+ return newValue(typ, p, flag)
}
// MakeSlice creates a new zero-initialized slice value
Len: len,
Cap: cap,
}
- return newValue(typ, addr(s), true).(*SliceValue)
+ return newValue(typ, addr(s), canAddr|canSet|canStore).(*SliceValue)
}
/*
// Set assigns x to v.
// The new value x must have the same type as v.
func (v *ChanValue) Set(x *ChanValue) {
- if !v.canSet {
+ if !v.CanSet() {
panic(cannotSet)
}
typesMustMatch(v.typ, x.typ)
// Set assigns x to v.
// The new value x must have the same type as v.
func (v *FuncValue) Set(x *FuncValue) {
- if !v.canSet {
+ if !v.CanSet() {
panic(cannotSet)
}
typesMustMatch(v.typ, x.typ)
}
p := &t.methods[i]
fn := p.tfn
- fv := &FuncValue{value: value{toType(*p.typ), addr(&fn), true}, first: v, isInterface: false}
+ fv := &FuncValue{value: value{toType(*p.typ), addr(&fn), 0}, first: v, isInterface: false}
return fv
}
b byte
}
+// Interface returns the fv as an interface value.
+// If fv is a method obtained by invoking Value.Method
+// (as opposed to Type.Method), Interface cannot return an
+// interface value, so it panics.
+func (fv *FuncValue) Interface() interface{} {
+ if fv.first != nil {
+ panic("FuncValue: cannot create interface value for method with bound receiver")
+ }
+ return fv.value.Interface()
+}
+
// Call calls the function fv with input parameters in.
// It returns the function's output parameters as Values.
func (fv *FuncValue) Call(in []Value) []Value {
if x != nil {
i = x.Interface()
}
- if !v.canSet {
+ if !v.CanSet() {
panic(cannotSet)
}
// Two different representations; see comment in Get.
// Interface is two words: itable, data.
tab := *(**runtime.Itable)(v.addr)
- data := &value{Typeof((*byte)(nil)), addr(uintptr(v.addr) + ptrSize), true}
+ data := &value{Typeof((*byte)(nil)), addr(uintptr(v.addr) + ptrSize), 0}
// Function pointer is at p.perm in the table.
fn := tab.Fn[i]
- fv := &FuncValue{value: value{toType(*p.typ), addr(&fn), true}, first: data, isInterface: true}
+ fv := &FuncValue{value: value{toType(*p.typ), addr(&fn), 0}, first: data, isInterface: true}
return fv
}
// Set assigns x to v.
// The new value x must have the same type as v.
func (v *MapValue) Set(x *MapValue) {
- if !v.canSet {
+ if !v.CanSet() {
panic(cannotSet)
}
if x == nil {
func (v *PtrValue) Get() uintptr { return *(*uintptr)(v.addr) }
// Set assigns x to v.
-// The new value x must have the same type as v.
+// The new value x must have the same type as v, and x.Elem().CanSet() must be true.
func (v *PtrValue) Set(x *PtrValue) {
if x == nil {
*(**uintptr)(v.addr) = nil
return
}
- if !v.canSet {
+ if !v.CanSet() {
panic(cannotSet)
}
+ if x.flag&canStore == 0 {
+ panic("cannot copy pointer obtained from unexported struct field")
+ }
typesMustMatch(v.typ, x.typ)
// TODO: This will have to move into the runtime
// once the new gc goes in
typesMustMatch(v.typ.(*PtrType).Elem(), x.Type())
// TODO: This will have to move into the runtime
// once the new gc goes in.
- *(*uintptr)(v.addr) = x.Addr()
+ *(*uintptr)(v.addr) = x.UnsafeAddr()
}
// Elem returns the value that v points to.
if v.IsNil() {
return nil
}
- return newValue(v.typ.(*PtrType).Elem(), *(*addr)(v.addr), v.canSet)
+ flag := canAddr
+ if v.flag&canStore != 0 {
+ flag |= canSet | canStore
+ }
+ return newValue(v.typ.(*PtrType).Elem(), *(*addr)(v.addr), flag)
}
// Indirect returns the value that v points to.
func (v *StructValue) Set(x *StructValue) {
// TODO: This will have to move into the runtime
// once the gc goes in.
- if !v.canSet {
+ if !v.CanSet() {
panic(cannotSet)
}
typesMustMatch(v.typ, x.typ)
return nil
}
f := t.Field(i)
- return newValue(f.Type, addr(uintptr(v.addr)+f.Offset), v.canSet && f.PkgPath == "")
+ flag := v.flag
+ if f.PkgPath != "" {
+ // unexported field
+ flag &^= canSet | canStore
+ }
+ return newValue(f.Type, addr(uintptr(v.addr)+f.Offset), flag)
}
// FieldByIndex returns the nested field corresponding to index.
return nil
}
t, a := unsafe.Reflect(i)
- return newValue(toType(t), addr(a), true)
+ return newValue(toType(t), addr(a), canSet|canAddr|canStore)
}
-func newValue(typ Type, addr addr, canSet bool) Value {
- v := value{typ, addr, canSet}
+func newValue(typ Type, addr addr, flag uint32) Value {
+ v := value{typ, addr, flag}
switch typ.(type) {
case *ArrayType:
return &ArrayValue{v}
if typ == nil {
return nil
}
- return newValue(typ, addr(unsafe.New(typ)), true)
+ return newValue(typ, addr(unsafe.New(typ)), canSet|canAddr|canStore)
}