Commit 34b88737 authored by Rob Pike's avatar Rob Pike

Reflection values.

R=rsc
DELTA=206  (79 added, 25 deleted, 102 changed)
OCL=17652
CL=17669
parent 7dbee69b
......@@ -10,7 +10,7 @@ import (
func typedump(s string) {
t := reflect.ParseTypeString("", s);
print(reflect.TypeToString(t),"; size = ", t.Size(), "\n");
print(reflect.TypeToString(t, true),"; size = ", t.Size(), "\n");
}
func valuedump(s string) {
......@@ -45,12 +45,13 @@ func valuedump(s string) {
export type empty interface {}
export type T struct { a int; b float64 }
export type T struct { a int; b float64; c string; d *int }
func main() {
var s string;
var t reflect.Type;
if false{
typedump("int8");
typedump("int16");
typedump("int32");
......@@ -77,7 +78,7 @@ func main() {
typedump("struct {a int8; b int8; c int8; b int32}");
typedump("struct {a int8; b int8; c int8; d int8; b int32}");
typedump("struct {a int8; b int8; c int8; d int8; e int8; b int32}");
valuedump("int8");
valuedump("int16");
valuedump("int32");
......@@ -105,9 +106,50 @@ func main() {
valuedump("struct {a int8; b int8; c int8; b int32}");
valuedump("struct {a int8; b int8; c int8; d int8; b int32}");
valuedump("struct {a int8; b int8; c int8; d int8; e int8; b int32}");
v := new(T);
a, b := sys.reflect(v.(empty));
println(a, b);
typedump(b);
}
{ var tmp = 123;
value := reflect.NewValue(tmp);
println(reflect.ValueToString(value));
}
{ var tmp = 123.4;
value := reflect.NewValue(tmp);
println(reflect.ValueToString(value));
}
{ var tmp = "abc";
value := reflect.NewValue(tmp);
println(reflect.ValueToString(value));
}
{
var i int = 7;
var tmp = &T{123, 456.0, "hello", &i};
value := reflect.NewValue(tmp);
println(reflect.ValueToString(value.(reflect.PtrValue).Sub()));
}
{
type C chan *T; // TODO: should not be necessary
var tmp = new(C);
value := reflect.NewValue(tmp);
println(reflect.ValueToString(value));
}
{
type A [10]int;
var tmp A = A{1,2,3,4,5,6,7,8,9,10};
value := reflect.NewValue(&tmp);
println(reflect.TypeToString(value.Type().(reflect.PtrType).Sub(), true));
println(reflect.TypeToString(value.(reflect.PtrValue).Sub().Type(), true));
println(reflect.ValueToString(value.(reflect.PtrValue).Sub()));
value.(reflect.PtrValue).Sub().(reflect.ArrayValue).Elem(4).(reflect.Int32Value).Put(123);
println(reflect.ValueToString(value.(reflect.PtrValue).Sub()));
}
{
type AA []int;
tmp1 := [10]int{1,2,3,4,5,6,7,8,9,10};
var tmp *AA = &tmp1;
value := reflect.NewValue(tmp);
println(reflect.TypeToString(value.Type().(reflect.PtrType).Sub(), true));
println(reflect.TypeToString(value.(reflect.PtrValue).Sub().Type(), true));
println(reflect.ValueToString(value.(reflect.PtrValue).Sub()));
value.(reflect.PtrValue).Sub().(reflect.ArrayValue).Elem(4).(reflect.Int32Value).Put(123);
println(reflect.ValueToString(value.(reflect.PtrValue).Sub()));
}
}
......@@ -12,7 +12,7 @@ import (
"strings";
)
export func TypeToString(typ Type) string
export func TypeToString(typ Type, expand bool) string
export func ValueToString(val Value) string
type HasFields interface {
......@@ -24,7 +24,7 @@ func TypeFieldsToString(t HasFields, sep string) string {
var str string;
for i := 0; i < t.Len(); i++ {
str1, typ, offset := t.Field(i);
str1 += " " + TypeToString(typ);
str1 += " " + TypeToString(typ, false);
if i < t.Len() - 1 {
str1 += sep + " ";
}
......@@ -33,9 +33,9 @@ func TypeFieldsToString(t HasFields, sep string) string {
return str;
}
func TypeToString(typ Type) string {
func TypeToString(typ Type, expand bool) string {
var str string;
if name := typ.Name(); name != "" {
if name := typ.Name(); !expand && name != "" {
return name
}
switch(typ.Kind()) {
......@@ -67,7 +67,7 @@ func TypeToString(typ Type) string {
return "string";
case PtrKind:
p := typ.(PtrType);
return "*" + TypeToString(p.Sub());
return "*" + TypeToString(p.Sub(), false);
case ArrayKind:
a := typ.(ArrayType);
if a.Open() {
......@@ -75,11 +75,11 @@ func TypeToString(typ Type) string {
} else {
str = "[" + strings.ltoa(int64(a.Len())) + "]"
}
return str + TypeToString(a.Elem());
return str + TypeToString(a.Elem(), false);
case MapKind:
m := typ.(MapType);
str = "map[" + TypeToString(m.Key()) + "]";
return str + TypeToString(m.Elem());
str = "map[" + TypeToString(m.Key(), false) + "]";
return str + TypeToString(m.Elem(), false);
case ChanKind:
c := typ.(ChanType);
switch c.Dir() {
......@@ -92,7 +92,7 @@ func TypeToString(typ Type) string {
default:
panicln("reflect.TypeToString: unknown chan direction");
}
return str + TypeToString(c.Elem());
return str + TypeToString(c.Elem(), false);
case StructKind:
return "struct{" + TypeFieldsToString(typ, ";") + "}";
case InterfaceKind:
......@@ -151,13 +151,13 @@ func ValueToString(val Value) string {
return val.(StringValue).Get();
case PtrKind:
v := val.(PtrValue);
return TypeToString(typ) + "(" + integer(int64(v.Addr())) + ")";
return TypeToString(typ, false) + "(" + integer(int64(v.Indirect())) + ")";
case ArrayKind:
t := typ.(ArrayType);
v := val.(ArrayValue);
str += TypeToString(t);
str += TypeToString(t, false);
str += "{";
for i := 0; i < v.Len(); i++ {
for i := uint64(0); i < v.Len(); i++ {
if i > 0 {
str += ", "
}
......@@ -168,7 +168,7 @@ func ValueToString(val Value) string {
case MapKind:
t := typ.(MapType);
v := val.(ArrayValue);
str = TypeToString(t);
str = TypeToString(t, false);
str += "{";
str += "<can't iterate on maps>";
str += "}";
......@@ -178,7 +178,7 @@ func ValueToString(val Value) string {
case StructKind:
t := typ.(StructType);
v := val.(StructValue);
str += TypeToString(t); // TODO: use the name?
str += TypeToString(t, false);
str += "{";
for i := 0; i < v.Len(); i++ {
if i > 0 {
......
This diff is collapsed.
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