Commit 96cfd154 authored by Russ Cox's avatar Russ Cox

direct all interface extraction to InterfaceValue.Get.

delete unnecessary reflect. throughout

R=r
DELTA=124  (18 added, 0 deleted, 106 changed)
OCL=29173
CL=29175
parent fb5aa46e
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
package reflect package reflect
import ( import (
"io";
"os";
"reflect"; "reflect";
"testing"; "testing";
"unsafe"; "unsafe";
...@@ -43,56 +45,56 @@ func assert(s, t string) { ...@@ -43,56 +45,56 @@ func assert(s, t string) {
} }
func typedump(s, t string) { func typedump(s, t string) {
typ := reflect.ParseTypeString("", s); typ := ParseTypeString("", s);
assert(reflect.typeToString(typ, true), t); assert(typeToString(typ, true), t);
} }
func valuedump(s, t string) { func valuedump(s, t string) {
typ := reflect.ParseTypeString("", s); typ := ParseTypeString("", s);
v := reflect.NewZeroValue(typ); v := NewZeroValue(typ);
if v == nil { if v == nil {
panicln("valuedump", s); panicln("valuedump", s);
} }
switch v.Kind() { switch v.Kind() {
case reflect.IntKind: case IntKind:
v.(reflect.IntValue).Set(132); v.(IntValue).Set(132);
case reflect.Int8Kind: case Int8Kind:
v.(reflect.Int8Value).Set(8); v.(Int8Value).Set(8);
case reflect.Int16Kind: case Int16Kind:
v.(reflect.Int16Value).Set(16); v.(Int16Value).Set(16);
case reflect.Int32Kind: case Int32Kind:
v.(reflect.Int32Value).Set(32); v.(Int32Value).Set(32);
case reflect.Int64Kind: case Int64Kind:
v.(reflect.Int64Value).Set(64); v.(Int64Value).Set(64);
case reflect.UintKind: case UintKind:
v.(reflect.UintValue).Set(132); v.(UintValue).Set(132);
case reflect.Uint8Kind: case Uint8Kind:
v.(reflect.Uint8Value).Set(8); v.(Uint8Value).Set(8);
case reflect.Uint16Kind: case Uint16Kind:
v.(reflect.Uint16Value).Set(16); v.(Uint16Value).Set(16);
case reflect.Uint32Kind: case Uint32Kind:
v.(reflect.Uint32Value).Set(32); v.(Uint32Value).Set(32);
case reflect.Uint64Kind: case Uint64Kind:
v.(reflect.Uint64Value).Set(64); v.(Uint64Value).Set(64);
case reflect.FloatKind: case FloatKind:
v.(reflect.FloatValue).Set(3200.0); v.(FloatValue).Set(3200.0);
case reflect.Float32Kind: case Float32Kind:
v.(reflect.Float32Value).Set(32.1); v.(Float32Value).Set(32.1);
case reflect.Float64Kind: case Float64Kind:
v.(reflect.Float64Value).Set(64.2); v.(Float64Value).Set(64.2);
case reflect.StringKind: case StringKind:
v.(reflect.StringValue).Set("stringy cheese"); v.(StringValue).Set("stringy cheese");
case reflect.BoolKind: case BoolKind:
v.(reflect.BoolValue).Set(true); v.(BoolValue).Set(true);
} }
assert(reflect.valueToString(v), t); assert(valueToString(v), t);
} }
type T struct { a int; b float64; c string; d *int } type T struct { a int; b float64; c string; d *int }
func TestAll(tt *testing.T) { // TODO(r): wrap up better func TestAll(tt *testing.T) { // TODO(r): wrap up better
var s string; var s string;
var t reflect.Type; var t Type;
// Types // Types
typedump("missing", "$missing$"); typedump("missing", "$missing$");
...@@ -156,136 +158,136 @@ func TestAll(tt *testing.T) { // TODO(r): wrap up better ...@@ -156,136 +158,136 @@ func TestAll(tt *testing.T) { // TODO(r): wrap up better
valuedump("struct {a int8; b int8; b int32}", "struct{a int8; b int8; b int32}{0, 0, 0}"); valuedump("struct {a int8; b int8; b int32}", "struct{a int8; b int8; b int32}{0, 0, 0}");
{ var tmp = 123; { var tmp = 123;
value := reflect.NewValue(tmp); value := NewValue(tmp);
assert(reflect.valueToString(value), "123"); assert(valueToString(value), "123");
} }
{ var tmp = 123.4; { var tmp = 123.4;
value := reflect.NewValue(tmp); value := NewValue(tmp);
assert(reflect.valueToString(value), "123.4"); assert(valueToString(value), "123.4");
} }
{ {
var tmp = byte(123); var tmp = byte(123);
value := reflect.NewValue(tmp); value := NewValue(tmp);
assert(reflect.valueToString(value), "123"); assert(valueToString(value), "123");
assert(reflect.typeToString(value.Type(), false), "uint8"); assert(typeToString(value.Type(), false), "uint8");
} }
{ var tmp = "abc"; { var tmp = "abc";
value := reflect.NewValue(tmp); value := NewValue(tmp);
assert(reflect.valueToString(value), "abc"); assert(valueToString(value), "abc");
} }
{ {
var i int = 7; var i int = 7;
var tmp = &T{123, 456.75, "hello", &i}; var tmp = &T{123, 456.75, "hello", &i};
value := reflect.NewValue(tmp); value := NewValue(tmp);
assert(reflect.valueToString(value.(reflect.PtrValue).Sub()), "reflect.T{123, 456.75, hello, *int(@)}"); assert(valueToString(value.(PtrValue).Sub()), "reflect.T{123, 456.75, hello, *int(@)}");
} }
{ {
type C chan *T; // TODO: should not be necessary type C chan *T; // TODO: should not be necessary
var tmp = new(C); var tmp = new(C);
value := reflect.NewValue(tmp); value := NewValue(tmp);
assert(reflect.valueToString(value), "*reflect.C·all_test(@)"); assert(valueToString(value), "*reflect.C·all_test(@)");
} }
// { // {
// type A [10]int; // type A [10]int;
// var tmp A = A{1,2,3,4,5,6,7,8,9,10}; // var tmp A = A{1,2,3,4,5,6,7,8,9,10};
// value := reflect.NewValue(&tmp); // value := NewValue(&tmp);
// assert(reflect.valueToString(value.(reflect.PtrValue).Sub()), "reflect.A·all_test{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}"); // assert(valueToString(value.(PtrValue).Sub()), "reflect.A·all_test{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}");
// value.(reflect.PtrValue).Sub().(reflect.ArrayValue).Elem(4).(reflect.IntValue).Set(123); // value.(PtrValue).Sub().(ArrayValue).Elem(4).(IntValue).Set(123);
// assert(reflect.valueToString(value.(reflect.PtrValue).Sub()), "reflect.A·all_test{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}"); // assert(valueToString(value.(PtrValue).Sub()), "reflect.A·all_test{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}");
// } // }
{ {
type AA []int; type AA []int;
var tmp = AA{1,2,3,4,5,6,7,8,9,10}; var tmp = AA{1,2,3,4,5,6,7,8,9,10};
value := reflect.NewValue(&tmp); // TODO: NewValue(tmp) too value := NewValue(&tmp); // TODO: NewValue(tmp) too
assert(reflect.valueToString(value.(reflect.PtrValue).Sub()), "reflect.AA·all_test{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}"); assert(valueToString(value.(PtrValue).Sub()), "reflect.AA·all_test{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}");
value.(reflect.PtrValue).Sub().(reflect.ArrayValue).Elem(4).(reflect.IntValue).Set(123); value.(PtrValue).Sub().(ArrayValue).Elem(4).(IntValue).Set(123);
assert(reflect.valueToString(value.(reflect.PtrValue).Sub()), "reflect.AA·all_test{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}"); assert(valueToString(value.(PtrValue).Sub()), "reflect.AA·all_test{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}");
} }
{ {
var ip *int32; var ip *int32;
var i int32 = 1234; var i int32 = 1234;
vip := reflect.NewValue(&ip); vip := NewValue(&ip);
vi := reflect.NewValue(i); vi := NewValue(i);
vip.(reflect.PtrValue).Sub().(reflect.PtrValue).SetSub(vi); vip.(PtrValue).Sub().(PtrValue).SetSub(vi);
if *ip != 1234 { if *ip != 1234 {
panicln("SetSub failure", *ip); panicln("SetSub failure", *ip);
} }
} }
var pt reflect.PtrType; var pt PtrType;
var st reflect.StructType; var st StructType;
var mt reflect.MapType; var mt MapType;
var at reflect.ArrayType; var at ArrayType;
var ct reflect.ChanType; var ct ChanType;
var name string; var name string;
var typ reflect.Type; var typ Type;
var tag string; var tag string;
var offset int; var offset int;
// Type strings // Type strings
t = reflect.ParseTypeString("", "int8"); t = ParseTypeString("", "int8");
assert(t.String(), "int8"); assert(t.String(), "int8");
t = reflect.ParseTypeString("", "*int8"); t = ParseTypeString("", "*int8");
assert(t.String(), "*int8"); assert(t.String(), "*int8");
pt = t.(reflect.PtrType); pt = t.(PtrType);
assert(pt.Sub().String(), "int8"); assert(pt.Sub().String(), "int8");
t = reflect.ParseTypeString("", "*struct {c chan *int32; d float32}"); t = ParseTypeString("", "*struct {c chan *int32; d float32}");
assert(t.String(), "*struct {c chan *int32; d float32}"); assert(t.String(), "*struct {c chan *int32; d float32}");
pt = t.(reflect.PtrType); pt = t.(PtrType);
assert(pt.Sub().String(), "struct {c chan *int32; d float32}"); assert(pt.Sub().String(), "struct {c chan *int32; d float32}");
st = pt.Sub().(reflect.StructType); st = pt.Sub().(StructType);
name, typ, tag, offset = st.Field(0); name, typ, tag, offset = st.Field(0);
assert(typ.String(), "chan *int32"); assert(typ.String(), "chan *int32");
name, typ, tag, offset = st.Field(1); name, typ, tag, offset = st.Field(1);
assert(typ.String(), "float32"); assert(typ.String(), "float32");
t = reflect.ParseTypeString("", "interface {a() *int}"); t = ParseTypeString("", "interface {a() *int}");
assert(t.String(), "interface {a() *int}"); assert(t.String(), "interface {a() *int}");
t = reflect.ParseTypeString("", "func(a int8, b int32)"); t = ParseTypeString("", "func(a int8, b int32)");
assert(t.String(), "func(a int8, b int32)"); assert(t.String(), "func(a int8, b int32)");
t = reflect.ParseTypeString("", "func(a int8, b int32) float"); t = ParseTypeString("", "func(a int8, b int32) float");
assert(t.String(), "func(a int8, b int32) float"); assert(t.String(), "func(a int8, b int32) float");
t = reflect.ParseTypeString("", "func(a int8, b int32) (a float, b float)"); t = ParseTypeString("", "func(a int8, b int32) (a float, b float)");
assert(t.String(), "func(a int8, b int32) (a float, b float)"); assert(t.String(), "func(a int8, b int32) (a float, b float)");
t = reflect.ParseTypeString("", "[32]int32"); t = ParseTypeString("", "[32]int32");
assert(t.String(), "[32]int32"); assert(t.String(), "[32]int32");
at = t.(reflect.ArrayType); at = t.(ArrayType);
assert(at.Elem().String(), "int32"); assert(at.Elem().String(), "int32");
t = reflect.ParseTypeString("", "map[string]*int32"); t = ParseTypeString("", "map[string]*int32");
assert(t.String(), "map[string]*int32"); assert(t.String(), "map[string]*int32");
mt = t.(reflect.MapType); mt = t.(MapType);
assert(mt.Key().String(), "string"); assert(mt.Key().String(), "string");
assert(mt.Elem().String(), "*int32"); assert(mt.Elem().String(), "*int32");
t = reflect.ParseTypeString("", "chan<-string"); t = ParseTypeString("", "chan<-string");
assert(t.String(), "chan<-string"); assert(t.String(), "chan<-string");
ct = t.(reflect.ChanType); ct = t.(ChanType);
assert(ct.Elem().String(), "string"); assert(ct.Elem().String(), "string");
// make sure tag strings are not part of element type // make sure tag strings are not part of element type
t = reflect.ParseTypeString("", "struct{d []uint32 \"TAG\"}"); t = ParseTypeString("", "struct{d []uint32 \"TAG\"}");
st = t.(reflect.StructType); st = t.(StructType);
name, typ, tag, offset = st.Field(0); name, typ, tag, offset = st.Field(0);
assert(typ.String(), "[]uint32"); assert(typ.String(), "[]uint32");
t = reflect.ParseTypeString("", "[]int32"); t = ParseTypeString("", "[]int32");
v := reflect.NewSliceValue(t.(ArrayType), 5, 10); v := NewSliceValue(t.(ArrayType), 5, 10);
t1 := reflect.ParseTypeString("", "*[]int32"); t1 := ParseTypeString("", "*[]int32");
v1 := reflect.NewZeroValue(t1); v1 := NewZeroValue(t1);
if v1 == nil { panic("V1 is nil"); } if v1 == nil { panic("V1 is nil"); }
v1.(reflect.PtrValue).SetSub(v); v1.(PtrValue).SetSub(v);
a := v1.Interface().(*[]int32); a := v1.Interface().(*[]int32);
println(&a, len(a), cap(a)); println(&a, len(a), cap(a));
for i := 0; i < len(a); i++ { for i := 0; i < len(a); i++ {
v.Elem(i).(reflect.Int32Value).Set(int32(i)); v.Elem(i).(Int32Value).Set(int32(i));
} }
for i := 0; i < len(a); i++ { for i := 0; i < len(a); i++ {
println(a[i]); println(a[i]);
...@@ -295,21 +297,21 @@ func TestAll(tt *testing.T) { // TODO(r): wrap up better ...@@ -295,21 +297,21 @@ func TestAll(tt *testing.T) { // TODO(r): wrap up better
func TestInterfaceGet(t *testing.T) { func TestInterfaceGet(t *testing.T) {
var inter struct { e interface{ } }; var inter struct { e interface{ } };
inter.e = 123.456; inter.e = 123.456;
v1 := reflect.NewValue(&inter); v1 := NewValue(&inter);
v2 := v1.(reflect.PtrValue).Sub().(reflect.StructValue).Field(0); v2 := v1.(PtrValue).Sub().(StructValue).Field(0);
assert(v2.Type().String(), "interface { }"); assert(v2.Type().String(), "interface { }");
i2 := v2.(reflect.InterfaceValue).Get(); i2 := v2.(InterfaceValue).Get();
v3 := reflect.NewValue(i2); v3 := NewValue(i2);
assert(v3.Type().String(), "float"); assert(v3.Type().String(), "float");
} }
func TestInterfaceValue(t *testing.T) { func TestInterfaceValue(t *testing.T) {
var inter struct { e interface{ } }; var inter struct { e interface{ } };
inter.e = 123.456; inter.e = 123.456;
v1 := reflect.NewValue(&inter); v1 := NewValue(&inter);
v2 := v1.(reflect.PtrValue).Sub().(reflect.StructValue).Field(0); v2 := v1.(PtrValue).Sub().(StructValue).Field(0);
assert(v2.Type().String(), "interface { }"); assert(v2.Type().String(), "interface { }");
v3 := v2.(reflect.InterfaceValue).Value(); v3 := v2.(InterfaceValue).Value();
assert(v3.Type().String(), "float"); assert(v3.Type().String(), "float");
i3 := v2.Interface(); i3 := v2.Interface();
...@@ -320,7 +322,7 @@ func TestInterfaceValue(t *testing.T) { ...@@ -320,7 +322,7 @@ func TestInterfaceValue(t *testing.T) {
} }
func TestFunctionValue(t *testing.T) { func TestFunctionValue(t *testing.T) {
v := reflect.NewValue(func() {}); v := NewValue(func() {});
if v.Interface() != v.Interface() { if v.Interface() != v.Interface() {
t.Fatalf("TestFunction != itself"); t.Fatalf("TestFunction != itself");
} }
...@@ -476,8 +478,8 @@ func TestDeepEqualComplexStructInequality(t *testing.T) { ...@@ -476,8 +478,8 @@ func TestDeepEqualComplexStructInequality(t *testing.T) {
func check2ndField(x interface{}, offs uintptr, t *testing.T) { func check2ndField(x interface{}, offs uintptr, t *testing.T) {
s := reflect.NewValue(x).(reflect.StructValue); s := NewValue(x).(StructValue);
name, ftype, tag, reflect_offset := s.Type().(reflect.StructType).Field(1); name, ftype, tag, reflect_offset := s.Type().(StructType).Field(1);
if uintptr(reflect_offset) != offs { if uintptr(reflect_offset) != offs {
t.Error("mismatched offsets in structure alignment:", reflect_offset, offs); t.Error("mismatched offsets in structure alignment:", reflect_offset, offs);
} }
...@@ -532,16 +534,16 @@ func TestIsNil(t *testing.T) { ...@@ -532,16 +534,16 @@ func TestIsNil(t *testing.T) {
// These do implement IsNil // These do implement IsNil
doNil := []string{"*int", "interface{}", "map[string]int", "func() bool", "chan int", "[]string"}; doNil := []string{"*int", "interface{}", "map[string]int", "func() bool", "chan int", "[]string"};
for i, ts := range doNotNil { for i, ts := range doNotNil {
ty := reflect.ParseTypeString("", ts); ty := ParseTypeString("", ts);
v := reflect.NewZeroValue(ty); v := NewZeroValue(ty);
if nilable, ok := v.(Nillable); ok { if nilable, ok := v.(Nillable); ok {
t.Errorf("%s is nilable; should not be", ts) t.Errorf("%s is nilable; should not be", ts)
} }
} }
for i, ts := range doNil { for i, ts := range doNil {
ty := reflect.ParseTypeString("", ts); ty := ParseTypeString("", ts);
v := reflect.NewZeroValue(ty); v := NewZeroValue(ty);
if nilable, ok := v.(Nillable); !ok { if nilable, ok := v.(Nillable); !ok {
t.Errorf("%s %T is not nilable; should be", ts, v) t.Errorf("%s %T is not nilable; should be", ts, v)
} }
...@@ -569,3 +571,15 @@ func TestIsNil(t *testing.T) { ...@@ -569,3 +571,15 @@ func TestIsNil(t *testing.T) {
fi = TestIsNil; fi = TestIsNil;
NotNil(fi, t); NotNil(fi, t);
} }
func TestInterfaceExtraction(t *testing.T) {
var s struct {
w io.Writer;
}
s.w = os.Stdout;
v := Indirect(NewValue(&s)).(StructValue).Field(0).Interface();
if v != s.w.(interface{}) {
t.Errorf("Interface() on interface: ", v, s.w);
}
}
...@@ -60,7 +60,7 @@ func (c *commonValue) Interface() interface {} { ...@@ -60,7 +60,7 @@ func (c *commonValue) Interface() interface {} {
var i interface {}; var i interface {};
switch { switch {
case c.typ.Kind() == InterfaceKind: case c.typ.Kind() == InterfaceKind:
i = *(*interface{})(c.addr); panic("not reached"); // InterfaceValue overrides this method
case c.typ.Size() > 8: // TODO(rsc): how do we know it is 8? case c.typ.Size() > 8: // TODO(rsc): how do we know it is 8?
i = unsafe.Unreflect(uint64(uintptr(c.addr)), c.typ.String(), true); i = unsafe.Unreflect(uint64(uintptr(c.addr)), c.typ.String(), true);
default: default:
...@@ -794,6 +794,10 @@ func (v *interfaceValueStruct) Get() interface{} { ...@@ -794,6 +794,10 @@ func (v *interfaceValueStruct) Get() interface{} {
return *(*interface{ m() })(v.addr) return *(*interface{ m() })(v.addr)
} }
func (v *interfaceValueStruct) Interface() interface{} {
return v.Get();
}
func (v *interfaceValueStruct) Value() Value { func (v *interfaceValueStruct) Value() Value {
i := v.Get(); i := v.Get();
if i == nil { if i == nil {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment