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 ( ...@@ -10,7 +10,7 @@ import (
func typedump(s string) { func typedump(s string) {
t := reflect.ParseTypeString("", s); 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) { func valuedump(s string) {
...@@ -45,12 +45,13 @@ func valuedump(s string) { ...@@ -45,12 +45,13 @@ func valuedump(s string) {
export type empty interface {} 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() { func main() {
var s string; var s string;
var t reflect.Type; var t reflect.Type;
if false{
typedump("int8"); typedump("int8");
typedump("int16"); typedump("int16");
typedump("int32"); typedump("int32");
...@@ -105,9 +106,50 @@ func main() { ...@@ -105,9 +106,50 @@ func main() {
valuedump("struct {a int8; b int8; c int8; b int32}"); 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; b int32}");
valuedump("struct {a int8; b int8; c int8; d int8; e int8; b int32}"); valuedump("struct {a int8; b int8; c int8; d int8; e int8; b int32}");
}
v := new(T); { var tmp = 123;
a, b := sys.reflect(v.(empty)); value := reflect.NewValue(tmp);
println(a, b); println(reflect.ValueToString(value));
typedump(b); }
{ 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 ( ...@@ -12,7 +12,7 @@ import (
"strings"; "strings";
) )
export func TypeToString(typ Type) string export func TypeToString(typ Type, expand bool) string
export func ValueToString(val Value) string export func ValueToString(val Value) string
type HasFields interface { type HasFields interface {
...@@ -24,7 +24,7 @@ func TypeFieldsToString(t HasFields, sep string) string { ...@@ -24,7 +24,7 @@ func TypeFieldsToString(t HasFields, sep string) string {
var str string; var str string;
for i := 0; i < t.Len(); i++ { for i := 0; i < t.Len(); i++ {
str1, typ, offset := t.Field(i); str1, typ, offset := t.Field(i);
str1 += " " + TypeToString(typ); str1 += " " + TypeToString(typ, false);
if i < t.Len() - 1 { if i < t.Len() - 1 {
str1 += sep + " "; str1 += sep + " ";
} }
...@@ -33,9 +33,9 @@ func TypeFieldsToString(t HasFields, sep string) string { ...@@ -33,9 +33,9 @@ func TypeFieldsToString(t HasFields, sep string) string {
return str; return str;
} }
func TypeToString(typ Type) string { func TypeToString(typ Type, expand bool) string {
var str string; var str string;
if name := typ.Name(); name != "" { if name := typ.Name(); !expand && name != "" {
return name return name
} }
switch(typ.Kind()) { switch(typ.Kind()) {
...@@ -67,7 +67,7 @@ func TypeToString(typ Type) string { ...@@ -67,7 +67,7 @@ func TypeToString(typ Type) string {
return "string"; return "string";
case PtrKind: case PtrKind:
p := typ.(PtrType); p := typ.(PtrType);
return "*" + TypeToString(p.Sub()); return "*" + TypeToString(p.Sub(), false);
case ArrayKind: case ArrayKind:
a := typ.(ArrayType); a := typ.(ArrayType);
if a.Open() { if a.Open() {
...@@ -75,11 +75,11 @@ func TypeToString(typ Type) string { ...@@ -75,11 +75,11 @@ func TypeToString(typ Type) string {
} else { } else {
str = "[" + strings.ltoa(int64(a.Len())) + "]" str = "[" + strings.ltoa(int64(a.Len())) + "]"
} }
return str + TypeToString(a.Elem()); return str + TypeToString(a.Elem(), false);
case MapKind: case MapKind:
m := typ.(MapType); m := typ.(MapType);
str = "map[" + TypeToString(m.Key()) + "]"; str = "map[" + TypeToString(m.Key(), false) + "]";
return str + TypeToString(m.Elem()); return str + TypeToString(m.Elem(), false);
case ChanKind: case ChanKind:
c := typ.(ChanType); c := typ.(ChanType);
switch c.Dir() { switch c.Dir() {
...@@ -92,7 +92,7 @@ func TypeToString(typ Type) string { ...@@ -92,7 +92,7 @@ func TypeToString(typ Type) string {
default: default:
panicln("reflect.TypeToString: unknown chan direction"); panicln("reflect.TypeToString: unknown chan direction");
} }
return str + TypeToString(c.Elem()); return str + TypeToString(c.Elem(), false);
case StructKind: case StructKind:
return "struct{" + TypeFieldsToString(typ, ";") + "}"; return "struct{" + TypeFieldsToString(typ, ";") + "}";
case InterfaceKind: case InterfaceKind:
...@@ -151,13 +151,13 @@ func ValueToString(val Value) string { ...@@ -151,13 +151,13 @@ func ValueToString(val Value) string {
return val.(StringValue).Get(); return val.(StringValue).Get();
case PtrKind: case PtrKind:
v := val.(PtrValue); v := val.(PtrValue);
return TypeToString(typ) + "(" + integer(int64(v.Addr())) + ")"; return TypeToString(typ, false) + "(" + integer(int64(v.Indirect())) + ")";
case ArrayKind: case ArrayKind:
t := typ.(ArrayType); t := typ.(ArrayType);
v := val.(ArrayValue); v := val.(ArrayValue);
str += TypeToString(t); str += TypeToString(t, false);
str += "{"; str += "{";
for i := 0; i < v.Len(); i++ { for i := uint64(0); i < v.Len(); i++ {
if i > 0 { if i > 0 {
str += ", " str += ", "
} }
...@@ -168,7 +168,7 @@ func ValueToString(val Value) string { ...@@ -168,7 +168,7 @@ func ValueToString(val Value) string {
case MapKind: case MapKind:
t := typ.(MapType); t := typ.(MapType);
v := val.(ArrayValue); v := val.(ArrayValue);
str = TypeToString(t); str = TypeToString(t, false);
str += "{"; str += "{";
str += "<can't iterate on maps>"; str += "<can't iterate on maps>";
str += "}"; str += "}";
...@@ -178,7 +178,7 @@ func ValueToString(val Value) string { ...@@ -178,7 +178,7 @@ func ValueToString(val Value) string {
case StructKind: case StructKind:
t := typ.(StructType); t := typ.(StructType);
v := val.(StructValue); v := val.(StructValue);
str += TypeToString(t); // TODO: use the name? str += TypeToString(t, false);
str += "{"; str += "{";
for i := 0; i < v.Len(); i++ { for i := 0; i < v.Len(); i++ {
if i > 0 { 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