}
func (c *Common) Interface() interface {} {
- if uintptr(c.addr) == 0 {
- panicln("reflect: address 0 for", c.typ.String());
+ var i interface {};
+ if c.typ.Size() > 8 { // TODO(rsc): how do we know it is 8?
+ i = sys.unreflect(c.addr.(uintptr).(uint64), c.typ.String(), true);
+ } else {
+ if uintptr(c.addr) == 0 {
+ panicln("reflect: address 0 for", c.typ.String());
+ }
+ i = sys.unreflect(uint64(uintptr(*c.addr.(*Addr))), c.typ.String(), false);
}
- return sys.unreflect(uint64(uintptr(*c.addr.(*Addr))), c.typ.String());
+ return i;
}
func NewValueAddr(typ Type, addr Addr) Value
FuncKind : &FuncCreator,
}
-var typecache = make(map[string] *Type);
+var typecache = make(map[string] Type);
func NewValueAddr(typ Type, addr Addr) Value {
c, ok := creator[typ.Kind()];
export func NewValue(e interface {}) Value {
- value, typestring := sys.reflect(e);
- p, ok := typecache[typestring];
+ value, typestring, indir := sys.reflect(e);
+ typ, ok := typecache[typestring];
if !ok {
- typ := ParseTypeString("", typestring);
- p = new(Type);
- *p = typ;
- typecache[typestring] = p;
+ typ = ParseTypeString("", typestring);
+ typecache[typestring] = typ;
}
- // 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'.
+
+ if indir {
+ // Content of interface is a pointer.
+ return NewValueAddr(typ, value.(uintptr).(Addr));
+ }
+
+ // Content of interface is a value;
+ // need a permanent copy to take its address.
ap := new(uint64);
*ap = value;
- return NewValueAddr(*p, ap.(Addr));
+ return NewValueAddr(typ, ap.(Addr));
}
void (*fun[])(void);
};
+static Iface niliface;
static Itype* hash[1009];
Sigi sigi·empty[2] = { (byte*)"interface { }" };
static void
printiface(Iface i)
{
- int32 j;
-
prints("(");
sys·printpointer(i.type);
prints(",");
- for(j=0; j<nelem(i.data); j++) {
- if(j > 0)
- prints(".");
- sys·printpointer(i.data[0]);
- }
+ sys·printpointer(i.data);
prints(")");
}
alg = st->hash;
wid = st->offset;
if(wid <= sizeof ret->data)
- algarray[alg].copy(wid, ret->data, elem);
+ algarray[alg].copy(wid, &ret->data, elem);
else{
- ret->data[0] = mal(wid);
+ ret->data = mal(wid);
if(iface_debug)
- printf("T2I mal %d %p\n", wid, ret->data[0]);
- algarray[alg].copy(wid, ret->data[0], elem);
+ printf("T2I mal %d %p\n", wid, ret->data);
+ algarray[alg].copy(wid, ret->data, elem);
}
if(iface_debug) {
alg = st->hash;
wid = st->offset;
if(wid <= sizeof i.data)
- algarray[alg].copy(wid, ret, i.data);
+ algarray[alg].copy(wid, ret, &i.data);
else
- algarray[alg].copy(wid, ret, i.data[0]);
+ algarray[alg].copy(wid, ret, i.data);
if(iface_debug) {
prints("I2T ret=");
} else {
*ok = true;
if(wid <= sizeof i.data)
- algarray[alg].copy(wid, ret, i.data);
+ algarray[alg].copy(wid, ret, &i.data);
else
- algarray[alg].copy(wid, ret, i.data[0]);
+ algarray[alg].copy(wid, ret, i.data);
}
if(iface_debug) {
prints("I2T2 ret=");
sys·ifaceI2I(Sigi *si, Iface i, Iface ret)
{
Itype *im;
- int32 j;
if(iface_debug) {
prints("I2I sigi=");
if(im == nil) {
// If incoming interface is uninitialized (zeroed)
// make the outgoing interface zeroed as well.
- ret.type = nil;
- for(j=0; j<nelem(ret.data); j++)
- ret.data[j] = nil;
+ ret = niliface;
} else {
ret = i;
if(im->sigi != si)
sys·ifaceI2I2(Sigi *si, Iface i, Iface ret, bool ok)
{
Itype *im;
- int32 j;
if(iface_debug) {
prints("I2I2 sigi=");
if(im == nil) {
// If incoming interface is uninitialized (zeroed)
// make the outgoing interface zeroed as well.
- ret.type = nil;
- for(j=0; j<nelem(ret.data); j++)
- ret.data[j] = nil;
+ ret = niliface;
ok = 1;
} else {
ret = i;
if(im->sigi != si) {
ret.type = itype(si, im->sigt, 1);
if(ret.type == nil) {
- for(j=0; j<nelem(ret.data); j++)
- ret.data[j] = nil;
+ ret = niliface;
ok = 0;
}
}
goto no;
if(wid <= sizeof i1.data) {
- if(!algarray[alg].equal(wid, i1.data, i2.data))
+ if(!algarray[alg].equal(wid, &i1.data, &i2.data))
goto no;
} else {
- if(!algarray[alg].equal(wid, i1.data[0], i2.data[0]))
+ if(!algarray[alg].equal(wid, i1.data, i2.data))
goto no;
}
}
void
-sys·reflect(Itype *im, void *it, uint64 retit, string rettype)
+sys·reflect(Iface i, uint64 retit, string rettype, bool retindir)
{
- if(im == nil) {
+ int32 wid;
+
+ if(i.type == nil) {
retit = 0;
rettype = nil;
+ retindir = false;
} else {
- retit = (uint64)it;
- rettype = gostring(im->sigt->name);
+ retit = (uint64)i.data;
+ rettype = gostring(i.type->sigt->name);
+ wid = i.type->sigt->offset;
+ retindir = wid > sizeof i.data;
}
FLUSH(&retit);
FLUSH(&rettype);
+ FLUSH(&retindir);
}
extern Sigt *gotypesigs[];
extern int32 ngotypesigs;
+
+// The reflection library can ask to unreflect on a type
+// that has never been used, so we don't have a signature for it.
+// For concreteness, suppose a program does
+//
+// type T struct{ x []int }
+// var t T;
+// v := reflect.NewValue(v);
+// vv := v.Field(0);
+// if s, ok := vv.Interface().(string) {
+// print("first field is string");
+// }
+//
+// vv.Interface() returns the result of sys.unreflect with
+// a typestring of "[]int". If []int is not used with interfaces
+// in the rest of the program, there will be no signature in gotypesigs
+// for "[]int", so we have to invent one. The only requirements
+// on the fake signature are:
+//
+// (1) any interface conversion using the signature will fail
+// (2) calling sys.reflect() returns the args to unreflect
+//
+// (1) is ensured by the fact that we allocate a new Sigt,
+// so it will necessarily be != any Sigt in gotypesigs.
+// (2) is ensured by storing the type string in the signature
+// and setting the width to force the correct value of the bool indir.
+//
+// Note that (1) is correct behavior: if the program had tested
+// for .([]int) instead of .(string) above, then there would be a
+// signature with type string "[]int" in gotypesigs, and unreflect
+// wouldn't call fakesigt.
static Sigt*
-fakesigt(string type)
+fakesigt(string type, bool indir)
{
// TODO(rsc): Cache these by type string.
Sigt *sigt;
sigt[0].name = mal(type->len + 1);
mcpy(sigt[0].name, type->str, type->len);
sigt[0].hash = ASIMP; // alg
- sigt[0].offset = sizeof(void*); // width
+ if(indir)
+ sigt[0].offset = 2*sizeof(niliface.data); // big width
+ else
+ sigt[0].offset = 1; // small width
return sigt;
}
}
static Sigt*
-findtype(string type)
+findtype(string type, bool indir)
{
int32 i;
for(i=0; i<ngotypesigs; i++)
if(cmpstringchars(type, gotypesigs[i]->name) == 0)
return gotypesigs[i];
- return fakesigt(type);
+ return fakesigt(type, indir);
}
+
void
-sys·unreflect(uint64 it, string type, Itype *retim, void *retit)
+sys·unreflect(uint64 it, string type, bool indir, Iface ret)
{
- if(cmpstring(type, emptystring) == 0) {
- retim = 0;
- retit = 0;
- } else {
- retim = itype(sigi·empty, findtype(type), 0);
- retit = (void*)it;
- }
- FLUSH(&retim);
- FLUSH(&retit);
+ Sigt *sigt;
+
+ ret = niliface;
+
+ if(cmpstring(type, emptystring) == 0)
+ goto out;
+
+ // if we think the type should be indirect
+ // and caller does not, play it safe, return nil.
+ sigt = findtype(type, indir);
+ if(indir != (sigt[0].offset > sizeof ret.data))
+ goto out;
+
+ ret.type = itype(sigi·empty, sigt, 0);
+ ret.data = (void*)it;
+
+out:
+ FLUSH(&ret);
}