func valuedump(s, t string) {
typ := reflect.ParseTypeString("", s);
v := reflect.NewInitValue(typ);
+ if v == nil {
+ panicln("valuedump", s);
+ }
switch v.Kind() {
case reflect.IntKind:
v.(reflect.IntValue).Set(132);
typedump("**P.integer", "**P.integer");
typedump("[32]int32", "[32]int32");
typedump("[]int8", "[]int8");
- typedump("*map[string]int32", "*map[string]int32");
- typedump("*chan<-string", "*chan<-string");
- typedump("struct {c *chan *int32; d float32}", "struct{c *chan*int32; d float32}");
+ typedump("map[string]int32", "map[string]int32");
+ typedump("chan<-string", "chan<-string");
+ typedump("struct {c chan *int32; d float32}", "struct{c chan*int32; d float32}");
typedump("*(a int8, b int32)", "*(a int8, b int32)");
- typedump("struct {c *(? *chan *P.integer, ? *int8)}", "struct{c *(*chan*P.integer, *int8)}");
+ typedump("struct {c *(? chan *P.integer, ? *int8)}", "struct{c *(chan*P.integer, *int8)}");
typedump("struct {a int8; b int32}", "struct{a int8; b int32}");
typedump("struct {a int8; b int8; b int32}", "struct{a int8; b int8; b int32}");
typedump("struct {a int8; b int8; c int8; b int32}", "struct{a int8; b int8; c int8; b int32}");
valuedump("**int8", "**int8(0)");
valuedump("[5]int32", "[5]int32{0, 0, 0, 0, 0}");
valuedump("**P.integer", "**P.integer(0)");
- valuedump("*map[string]int32", "*map[string]int32(0)");
- valuedump("*chan<-string", "*chan<-string(0)");
- valuedump("struct {c *chan *int32; d float32}", "struct{c *chan*int32; d float32}{*chan*int32(0), 0}");
+ valuedump("map[string]int32", "map[string]int32{<can't iterate on maps>}");
+ valuedump("chan<-string", "chan<-string");
+ valuedump("struct {c chan *int32; d float32}", "struct{c chan*int32; d float32}{chan*int32, 0}");
valuedump("*(a int8, b int32)", "*(a int8, b int32)(0)");
- valuedump("struct {c *(? *chan *P.integer, ? *int8)}", "struct{c *(*chan*P.integer, *int8)}{*(*chan*P.integer, *int8)(0)}");
+ valuedump("struct {c *(? chan *P.integer, ? *int8)}", "struct{c *(chan*P.integer, *int8)}{*(chan*P.integer, *int8)(0)}");
valuedump("struct {a int8; b int32}", "struct{a int8; b int32}{0, 0}");
valuedump("struct {a int8; b int8; b int32}", "struct{a int8; b int8; b int32}{0, 0, 0}");
}
{
type C chan *T; // TODO: should not be necessary
- var tmp = new(C);
+ var tmp = new(*C);
value := reflect.NewValue(tmp);
assert(reflect.ValueToString(value), "*reflect.C·all_test(@)");
}
// value.(reflect.PtrValue).Sub().(reflect.ArrayValue).Elem(4).(reflect.IntValue).Set(123);
// assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "reflect.A·all_test{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}");
// }
-// {
-// type AA []int;
-// tmp1 := [10]int{1,2,3,4,5,6,7,8,9,10}; // TODO: should not be necessary to use tmp1
-// var tmp *AA = &tmp1;
-// value := reflect.NewValue(tmp);
-// assert(reflect.ValueToString(value.(reflect.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);
-// assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "reflect.AA·all_test{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}");
-// }
+ {
+ type AA []int;
+ var tmp = AA{1,2,3,4,5,6,7,8,9,10};
+ value := reflect.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}");
+ value.(reflect.PtrValue).Sub().(reflect.ArrayValue).Elem(4).(reflect.IntValue).Set(123);
+ assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "reflect.AA·all_test{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}");
+ }
{
var ip *int32;
pt = t.(reflect.PtrType);
assert(pt.Sub().String(), "int8");
- t = reflect.ParseTypeString("", "*struct {c *chan *int32; d float32}");
- assert(t.String(), "*struct {c *chan *int32; d float32}");
+ t = reflect.ParseTypeString("", "*struct {c chan *int32; d float32}");
+ assert(t.String(), "*struct {c chan *int32; d float32}");
pt = t.(reflect.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);
name, typ, tag, offset = st.Field(0);
- assert(typ.String(), "*chan *int32");
+ assert(typ.String(), "chan *int32");
name, typ, tag, offset = st.Field(1);
assert(typ.String(), "float32");
func (t *ArrayTypeStruct) Size() int {
if t.open {
- return ptrsize // open arrays are pointers to structures
+ return ptrsize*2 // open arrays are 2-word headers
}
return t.len * t.elem.Get().Size();
}
}
func NewMapTypeStruct(name, typestring string, key, elem *StubType) *MapTypeStruct {
- return &MapTypeStruct{ Common{MapKind, typestring, name, 0}, key, elem}
-}
-
-func (t *MapTypeStruct) Size() int {
- panic("reflect.type: map.Size(): cannot happen");
- return 0
+ return &MapTypeStruct{ Common{MapKind, typestring, name, ptrsize}, key, elem}
}
func (t *MapTypeStruct) Key() Type {
}
func NewChanTypeStruct(name, typestring string, dir int, elem *StubType) *ChanTypeStruct {
- return &ChanTypeStruct{ Common{ChanKind, typestring, name, 0}, elem, dir}
-}
-
-func (t *ChanTypeStruct) Size() int {
- panic("reflect.type: chan.Size(): cannot happen");
- return 0
+ return &ChanTypeStruct{ Common{ChanKind, typestring, name, ptrsize}, elem, dir}
}
func (t *ChanTypeStruct) Dir() int {
}
// Cache of expanded types keyed by type name.
-var types *map[string] Type
+var types map[string] Type
// List of typename, typestring pairs
-var typestring *map[string] string
+var typestring map[string] string
var initialized bool = false
// Map of basic types to prebuilt StubTypes
-var basicstub *map[string] *StubType
+var basicstub map[string] *StubType
var MissingStub *StubType;
var DotDotDotStub *StubType;
functiontype
typename =
name '.' name
- doublequotedstring =
+ doublequotedstring =
string in " "; escapes are \x00 (NUL) \n \t \" \\
fieldlist =
[ field { [ ',' | ';' ] field } ]
// If the typestring is empty, it represents (the type of) a nil interface value
return NilInterface
}
- p := new(Parser);
+ p := new(*Parser);
p.str = typestring;
p.Next();
return p.Type(name).Get();
}
func (c *Common) Interface() interface {} {
+ if uintptr(c.addr) == 0 {
+ panicln("reflect: address 0 for", c.typ.String());
+ }
return sys.unreflect(uint64(uintptr(*c.addr.(*Addr))), c.typ.String());
}
func ArrayCreator(typ Type, addr Addr) Value {
arraytype := typ.(ArrayType);
if arraytype.Open() {
- v := new(OpenArrayValueStruct);
+ v := new(*OpenArrayValueStruct);
v.kind = ArrayKind;
v.addr = addr;
v.typ = typ;
v.array = addr.(*RuntimeArray);
return v;
}
- v := new(FixedArrayValueStruct);
+ v := new(*FixedArrayValueStruct);
v.kind = ArrayKind;
v.addr = addr;
v.typ = typ;
export func NewInitValue(typ Type) Value {
// Some values cannot be made this way.
switch typ.Kind() {
- case FuncKind, ChanKind, MapKind: // must be pointers, at least for now (TODO?)
+ case FuncKind: // must be pointers, at least for now (TODO?)
return nil;
case ArrayKind:
if typ.(ArrayType).Open() {
return nil
}
- array := new(RuntimeArray);
+ array := new(*RuntimeArray);
size := typ.Elem().Size() * cap;
if size == 0 {
size = 1;
p, ok := typecache[typestring];
if !ok {
typ := ParseTypeString("", typestring);
- p = new(Type);
+ p = new(*Type);
*p = typ;
typecache[typestring] = p;
}
// Content of interface is a value; need a permanent copy to take its address
// so we can modify the contents. Values contain pointers to 'values'.
- ap := new(uint64);
+ ap := new(*uint64);
*ap = value;
return NewValueAddr(*p, ap.(Addr));
}