gc: fix reflect table method receiver

Fixes #451.
Fixes #770.

R=ken2
CC=golang-dev
https://golang.org/cl/2207045
This commit is contained in:
Russ Cox 2010-09-28 13:43:50 -04:00
parent 05cc83bf4e
commit 00ffd59c1a
8 changed files with 221 additions and 50 deletions

View file

@ -1046,6 +1046,11 @@ func TestMethod(t *testing.T) {
t.Errorf("Type Method returned %d; want 250", i)
}
i = Typeof(&p).Method(0).Func.Call([]Value{NewValue(&p), NewValue(10)})[0].(*IntValue).Get()
if i != 250 {
t.Errorf("Pointer Type Method returned %d; want 250", i)
}
// Curried method of value.
i = NewValue(p).Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get()
if i != 250 {
@ -1288,9 +1293,12 @@ func TestDotDotDot(t *testing.T) {
t.Error(s)
}
type inner struct{}
type inner struct {
x int
}
type outer struct {
y int
inner
}
@ -1307,3 +1315,42 @@ func TestNestedMethods(t *testing.T) {
}
}
}
type innerInt struct {
x int
}
type outerInt struct {
y int
innerInt
}
func (i *innerInt) m() int {
return i.x
}
func TestEmbeddedMethods(t *testing.T) {
typ := Typeof((*outerInt)(nil))
if typ.NumMethod() != 1 || typ.Method(0).Func.Get() != NewValue((*outerInt).m).(*FuncValue).Get() {
t.Errorf("Wrong method table for outerInt: (m=%p)", (*outerInt).m)
for i := 0; i < typ.NumMethod(); i++ {
m := typ.Method(i)
t.Errorf("\t%d: %s %#x\n", i, m.Name, m.Func.Get())
}
}
i := &innerInt{3}
if v := NewValue(i).Method(0).Call(nil)[0].(*IntValue).Get(); v != 3 {
t.Errorf("i.m() = %d, want 3", v)
}
o := &outerInt{1, innerInt{2}}
if v := NewValue(o).Method(0).Call(nil)[0].(*IntValue).Get(); v != 2 {
t.Errorf("i.m() = %d, want 2", v)
}
f := (*outerInt).m
if v := f(o); v != 2 {
t.Errorf("f(o) = %d, want 2", v)
}
}