]> Cypherpunks repositories - gostls13.git/commitdiff
reflect.PtrValue.SetSub() to set pointers
authorRob Pike <r@golang.org>
Wed, 12 Nov 2008 22:19:39 +0000 (14:19 -0800)
committerRob Pike <r@golang.org>
Wed, 12 Nov 2008 22:19:39 +0000 (14:19 -0800)
R=rsc
OCL=19101
CL=19101

src/lib/reflect/test.go
src/lib/reflect/value.go

index fa55c9208294ff1361f60e07a8b5768f9081f36b..70880943836e5a23c9b19f65b84de4a9c17931f1 100644 (file)
@@ -91,6 +91,17 @@ func main() {
        var s string;
        var t reflect.Type;
 
+{
+       var ip *int32;
+       var i int32 = 1234;
+       vip := reflect.NewValue(&ip);
+       vi := reflect.NewValue(i);
+       vip.(reflect.PtrValue).Sub().(reflect.PtrValue).SetSub(vi);
+       if *ip != 1234 {
+               panicln("SetSub failure", *ip);
+       }
+}
+
        // Types
        typedump("missing", "$missing$");
        typedump("int", "int");
index fe41e3f92687f825d5e909c3ddca9e410193e4ad..bace93b6d1d5ee994d7e2a98ca79f48113d90aa3 100644 (file)
@@ -39,6 +39,7 @@ export type Empty interface {}        // TODO(r): Delete when no longer needed?
 export type Value interface {
        Kind()  int;
        Type()  Type;
+       Addr()  Addr;
        Interface()     Empty;
 }
 
@@ -58,6 +59,10 @@ func (c *Common) Type() Type {
        return c.typ
 }
 
+func (c *Common) Addr() Addr {
+       return c.addr
+}
+
 func (c *Common) Interface() Empty {
        return sys.unreflect(*AddrToPtrAddr(c.addr), c.typ.String());
 }
@@ -493,6 +498,7 @@ export type PtrValue interface {
        Type()  Type;
        Sub()   Value;
        Get()   Addr;
+       SetSub(Value);
 }
 
 type PtrValueStruct struct {
@@ -507,6 +513,10 @@ func (v *PtrValueStruct) Sub() Value {
        return NewValueAddr(v.typ.(PtrType).Sub(), v.Get());
 }
 
+func (v *PtrValueStruct) SetSub(subv Value)  {
+       *AddrToPtrAddr(v.addr) = subv.Addr();
+}
+
 func PtrCreator(typ Type, addr Addr) Value {
        return &PtrValueStruct{ Common{PtrKind, typ, addr} };
 }