Skip to content

Commit

Permalink
go1.3: add rtype.zero, Value.scalar
Browse files Browse the repository at this point in the history
  • Loading branch information
achun committed Feb 14, 2015
1 parent e523a04 commit cde17ff
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
6 changes: 4 additions & 2 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ func (k Kind) String() string {
}

type reflectValue struct {
typ *Type //*rtype
val unsafe.Pointer
typ *Type //*rtype
val unsafe.Pointer
scalar uintptr // go1.3
flag
}

Expand All @@ -23,6 +24,7 @@ func FromValue(v reflect.Value) Value {
rv.typ,
sur{
rv.val,
rv.scalar,
rv.flag,
unsafe.Pointer(rv.typ),
},
Expand Down
6 changes: 6 additions & 0 deletions surface.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ type sur struct {
// knows that val could be a pointer.
val unsafe.Pointer

// Non-pointer-valued data. When the data is smaller
// than a word, it begins at the first byte (in the memory
// address sense) of this field.
// Valid when flagIndir is not set and typ.pointers() is false.
scalar uintptr // go1.3

// flag holds metadata about the value.
// The lowest bits are flag bits:
// - flagRO: obtained via unexported field, so read-only
Expand Down
2 changes: 1 addition & 1 deletion surface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestStructAndFields(t *testing.T) {
wt.Equal(rt.String(), sv.Type.String())
wt.Equal(rt.PkgPath(), sv.Type.PkgPath())

wt.Equal(rt.NumField(), st.NumField())
wt.Equal(rt.NumField(), st.NumField(), i, s)
wt.Equal(rv.Interface(), sv.Interface())

wt.Equal(rv.CanSet(), sv.CanSet())
Expand Down
2 changes: 2 additions & 0 deletions type.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type Type struct {
string *string // string form; unnecessary but undeniably useful
*uncommonType // (relatively) uncommon fields
PtrToThis *Type // type for pointer to this type, if used in binary or has methods
// go 1.3
zero unsafe.Pointer // pointer to zero value
}

// uncommonType is present only for types with names or methods
Expand Down
11 changes: 6 additions & 5 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (v Value) Surface() Interface {
if typ != nil && typ.Size > ptrSize {
fl |= flagIndir
}
return Interface{ifacetype, sur{val, fl, v.typ}, typ}
return Interface{ifacetype, sur{val, v.scalar, fl, v.typ}, typ}
}

func (v Interface) InterfaceData() [2]uintptr {
Expand All @@ -155,7 +155,7 @@ func (v Ptr) Elem() Value {
fl := v.flag&flagRO | flagIndir | flagAddr
fl |= flag(typ.Kind() << flagKindShift)

return Value{typ, sur{val, fl, unsafe.Pointer(tt.Elem)}}
return Value{typ, sur{val, v.scalar, fl, unsafe.Pointer(tt.Elem)}}
}

// Indirect returns the value that v points to.
Expand Down Expand Up @@ -238,7 +238,7 @@ func (v Array) Index(i int) Value {
// Direct. Discard leading bytes.
val = unsafe.Pointer(uintptr(v.val) >> (offset * 8))
}
return Value{typ, sur{val, fl, unsafe.Pointer(tt.Elem)}}
return Value{typ, sur{val, v.scalar, fl, unsafe.Pointer(tt.Elem)}}

}
func (v Slice) Index(i int) Value {
Expand All @@ -252,7 +252,7 @@ func (v Slice) Index(i int) Value {
typ := v.Type.Elem
fl |= flag(typ.Kind()) << flagKindShift
val := unsafe.Pointer(s.Data + uintptr(i)*typ.Size)
return Value{typ, sur{val, fl, unsafe.Pointer(v.Type.Elem)}}
return Value{typ, sur{val, v.scalar, fl, unsafe.Pointer(v.Type.Elem)}}
}

func (v Struct) Field(i int) Value {
Expand Down Expand Up @@ -286,7 +286,7 @@ func (v Struct) field(i int) Value {
val = unsafe.Pointer(uintptr(v.val) >> (field.offset * 8))
}

return Value{typ, sur{val, fl, unsafe.Pointer(field.Type)}}
return Value{typ, sur{val, v.scalar, fl, unsafe.Pointer(field.Type)}}
}

// FieldByIndex returns the nested field corresponding to index.
Expand Down Expand Up @@ -340,6 +340,7 @@ func ValueOf(i interface{}) Value {
typ,
sur{
unsafe.Pointer(eface.word),
uintptr(eface.word),
fl,
unsafe.Pointer(eface.Type),
},
Expand Down

0 comments on commit cde17ff

Please sign in to comment.