mirror of
https://github.com/golang/go
synced 2024-11-22 02:24:41 -07:00
reflect: more efficient; cannot Set result of NewValue anymore
* Reduces malloc counts during gob encoder/decoder test from 6/6 to 3/5. The current reflect uses Set to mean two subtly different things. (1) If you have a reflect.Value v, it might just represent itself (as in v = reflect.NewValue(42)), in which case calling v.Set only changed v, not any other data in the program. (2) If you have a reflect Value v derived from a pointer or a slice (as in x := []int{42}; v = reflect.NewValue(x).Index(0)), v represents the value held there. Changing x[0] affects the value returned by v.Int(), and calling v.Set affects x[0]. This was not really by design; it just happened that way. The motivation for the new reflect implementation was to remove mallocs. The use case (1) has an implicit malloc inside it. If you can do: v := reflect.NewValue(0) v.Set(42) i := v.Int() // i = 42 then that implies that v is referring to some underlying chunk of memory in order to remember the 42; that is, NewValue must have allocated some memory. Almost all the time you are using reflect the goal is to inspect or to change other data, not to manipulate data stored solely inside a reflect.Value. This CL removes use case (1), so that an assignable reflect.Value must always refer to some other piece of data in the program. Put another way, removing this case would make v := reflect.NewValue(0) v.Set(42) as illegal as 0 = 42. It would also make this illegal: x := 0 v := reflect.NewValue(x) v.Set(42) for the same reason. (Note that right now, v.Set(42) "succeeds" but does not change the value of x.) If you really wanted to make v refer to x, you'd start with &x and dereference it: x := 0 v := reflect.NewValue(&x).Elem() // v = *&x v.Set(42) It's pretty rare, except in tests, to want to use NewValue and then call Set to change the Value itself instead of some other piece of data in the program. I haven't seen it happen once yet while making the tree build with this change. For the same reasons, reflect.Zero (formerly reflect.MakeZero) would also return an unassignable, unaddressable value. This invalidates the (awkward) idiom: pv := ... some Ptr Value we have ... v := reflect.Zero(pv.Type().Elem()) pv.PointTo(v) which, when the API changed, turned into: pv := ... some Ptr Value we have ... v := reflect.Zero(pv.Type().Elem()) pv.Set(v.Addr()) In both, it is far from clear what the code is trying to do. Now that it is possible, this CL adds reflect.New(Type) Value that does the obvious thing (same as Go's new), so this code would be replaced by: pv := ... some Ptr Value we have ... pv.Set(reflect.New(pv.Type().Elem())) The changes just described can be confusing to think about, but I believe it is because the old API was confusing - it was conflating two different kinds of Values - and that the new API by itself is pretty simple: you can only Set (or call Addr on) a Value if it actually addresses some real piece of data; that is, only if it is the result of dereferencing a Ptr or indexing a Slice. If you really want the old behavior, you'd get it by translating: v := reflect.NewValue(x) into v := reflect.New(reflect.Typeof(x)).Elem() v.Set(reflect.NewValue(x)) Gofix will not be able to help with this, because whether and how to change the code depends on whether the original code meant use (1) or use (2), so the developer has to read and think about the code. You can see the effect on packages in the tree in https://golang.org/cl/4423043/. R=r CC=golang-dev https://golang.org/cl/4435042
This commit is contained in:
parent
6211b596d6
commit
40fccbce6b
@ -10,6 +10,7 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
. "reflect"
|
||||
"runtime"
|
||||
"testing"
|
||||
"unsafe"
|
||||
)
|
||||
@ -155,45 +156,45 @@ var typeTests = []pair{
|
||||
}
|
||||
|
||||
var valueTests = []pair{
|
||||
{(int8)(0), "8"},
|
||||
{(int16)(0), "16"},
|
||||
{(int32)(0), "32"},
|
||||
{(int64)(0), "64"},
|
||||
{(uint8)(0), "8"},
|
||||
{(uint16)(0), "16"},
|
||||
{(uint32)(0), "32"},
|
||||
{(uint64)(0), "64"},
|
||||
{(float32)(0), "256.25"},
|
||||
{(float64)(0), "512.125"},
|
||||
{(string)(""), "stringy cheese"},
|
||||
{(bool)(false), "true"},
|
||||
{(*int8)(nil), "*int8(0)"},
|
||||
{(**int8)(nil), "**int8(0)"},
|
||||
{[5]int32{}, "[5]int32{0, 0, 0, 0, 0}"},
|
||||
{(**integer)(nil), "**reflect_test.integer(0)"},
|
||||
{(map[string]int32)(nil), "map[string] int32{<can't iterate on maps>}"},
|
||||
{(chan<- string)(nil), "chan<- string"},
|
||||
{struct {
|
||||
{new(int8), "8"},
|
||||
{new(int16), "16"},
|
||||
{new(int32), "32"},
|
||||
{new(int64), "64"},
|
||||
{new(uint8), "8"},
|
||||
{new(uint16), "16"},
|
||||
{new(uint32), "32"},
|
||||
{new(uint64), "64"},
|
||||
{new(float32), "256.25"},
|
||||
{new(float64), "512.125"},
|
||||
{new(string), "stringy cheese"},
|
||||
{new(bool), "true"},
|
||||
{new(*int8), "*int8(0)"},
|
||||
{new(**int8), "**int8(0)"},
|
||||
{new([5]int32), "[5]int32{0, 0, 0, 0, 0}"},
|
||||
{new(**integer), "**reflect_test.integer(0)"},
|
||||
{new(map[string]int32), "map[string] int32{<can't iterate on maps>}"},
|
||||
{new(chan<- string), "chan<- string"},
|
||||
{new(func(a int8, b int32)), "func(int8, int32)(0)"},
|
||||
{new(struct {
|
||||
c chan *int32
|
||||
d float32
|
||||
}{},
|
||||
}),
|
||||
"struct { c chan *int32; d float32 }{chan *int32, 0}",
|
||||
},
|
||||
{(func(a int8, b int32))(nil), "func(int8, int32)(0)"},
|
||||
{struct{ c func(chan *integer, *int8) }{},
|
||||
{new(struct{ c func(chan *integer, *int8) }),
|
||||
"struct { c func(chan *reflect_test.integer, *int8) }{func(chan *reflect_test.integer, *int8)(0)}",
|
||||
},
|
||||
{struct {
|
||||
{new(struct {
|
||||
a int8
|
||||
b int32
|
||||
}{},
|
||||
}),
|
||||
"struct { a int8; b int32 }{0, 0}",
|
||||
},
|
||||
{struct {
|
||||
{new(struct {
|
||||
a int8
|
||||
b int8
|
||||
c int32
|
||||
}{},
|
||||
}),
|
||||
"struct { a int8; b int8; c int32 }{0, 0, 0}",
|
||||
},
|
||||
}
|
||||
@ -213,7 +214,7 @@ func TestTypes(t *testing.T) {
|
||||
|
||||
func TestSet(t *testing.T) {
|
||||
for i, tt := range valueTests {
|
||||
v := NewValue(tt.i)
|
||||
v := NewValue(tt.i).Elem()
|
||||
switch v.Kind() {
|
||||
case Int:
|
||||
v.SetInt(132)
|
||||
@ -257,7 +258,7 @@ func TestSet(t *testing.T) {
|
||||
|
||||
func TestSetValue(t *testing.T) {
|
||||
for i, tt := range valueTests {
|
||||
v := NewValue(tt.i)
|
||||
v := NewValue(tt.i).Elem()
|
||||
switch v.Kind() {
|
||||
case Int:
|
||||
v.Set(NewValue(int(132)))
|
||||
@ -324,7 +325,7 @@ func TestValueToString(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestArrayElemSet(t *testing.T) {
|
||||
v := NewValue([10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
|
||||
v := NewValue(&[10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}).Elem()
|
||||
v.Index(4).SetInt(123)
|
||||
s := valueToString(v)
|
||||
const want = "[10]int{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}"
|
||||
@ -345,14 +346,14 @@ func TestPtrPointTo(t *testing.T) {
|
||||
var ip *int32
|
||||
var i int32 = 1234
|
||||
vip := NewValue(&ip)
|
||||
vi := NewValue(i)
|
||||
vi := NewValue(&i).Elem()
|
||||
vip.Elem().Set(vi.Addr())
|
||||
if *ip != 1234 {
|
||||
t.Errorf("got %d, want 1234", *ip)
|
||||
}
|
||||
|
||||
ip = nil
|
||||
vp := NewValue(ip)
|
||||
vp := NewValue(&ip).Elem()
|
||||
vp.Set(Zero(vp.Type()))
|
||||
if ip != nil {
|
||||
t.Errorf("got non-nil (%p), want nil", ip)
|
||||
@ -428,9 +429,9 @@ func TestAll(t *testing.T) {
|
||||
|
||||
func TestInterfaceGet(t *testing.T) {
|
||||
var inter struct {
|
||||
e interface{}
|
||||
E interface{}
|
||||
}
|
||||
inter.e = 123.456
|
||||
inter.E = 123.456
|
||||
v1 := NewValue(&inter)
|
||||
v2 := v1.Elem().Field(0)
|
||||
assert(t, v2.Type().String(), "interface { }")
|
||||
@ -441,9 +442,9 @@ func TestInterfaceGet(t *testing.T) {
|
||||
|
||||
func TestInterfaceValue(t *testing.T) {
|
||||
var inter struct {
|
||||
e interface{}
|
||||
E interface{}
|
||||
}
|
||||
inter.e = 123.456
|
||||
inter.E = 123.456
|
||||
v1 := NewValue(&inter)
|
||||
v2 := v1.Elem().Field(0)
|
||||
assert(t, v2.Type().String(), "interface { }")
|
||||
@ -457,8 +458,9 @@ func TestInterfaceValue(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestFunctionValue(t *testing.T) {
|
||||
v := NewValue(func() {})
|
||||
if v.Interface() != v.Interface() {
|
||||
var x interface{} = func() {}
|
||||
v := NewValue(x)
|
||||
if v.Interface() != v.Interface() || v.Interface() != x {
|
||||
t.Fatalf("TestFunction != itself")
|
||||
}
|
||||
assert(t, v.Type().String(), "func()")
|
||||
@ -471,6 +473,18 @@ var appendTests = []struct {
|
||||
{make([]int, 2, 4), []int{22, 33, 44}},
|
||||
}
|
||||
|
||||
func sameInts(x, y []int) bool {
|
||||
if len(x) != len(y) {
|
||||
return false
|
||||
}
|
||||
for i, xx := range x {
|
||||
if xx != y[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func TestAppend(t *testing.T) {
|
||||
for i, test := range appendTests {
|
||||
origLen, extraLen := len(test.orig), len(test.extra)
|
||||
@ -485,8 +499,8 @@ func TestAppend(t *testing.T) {
|
||||
// Test Append.
|
||||
a0 := NewValue(test.orig)
|
||||
have0 := Append(a0, e0...).Interface().([]int)
|
||||
if !DeepEqual(have0, want) {
|
||||
t.Errorf("Append #%d: have %v, want %v", i, have0, want)
|
||||
if !sameInts(have0, want) {
|
||||
t.Errorf("Append #%d: have %v, want %v (%p %p)", i, have0, want, test.orig, have0)
|
||||
}
|
||||
// Check that the orig and extra slices were not modified.
|
||||
if len(test.orig) != origLen {
|
||||
@ -498,7 +512,7 @@ func TestAppend(t *testing.T) {
|
||||
// Test AppendSlice.
|
||||
a1 := NewValue(test.orig)
|
||||
have1 := AppendSlice(a1, e1).Interface().([]int)
|
||||
if !DeepEqual(have1, want) {
|
||||
if !sameInts(have1, want) {
|
||||
t.Errorf("AppendSlice #%d: have %v, want %v", i, have1, want)
|
||||
}
|
||||
// Check that the orig and extra slices were not modified.
|
||||
@ -520,8 +534,10 @@ func TestCopy(t *testing.T) {
|
||||
t.Fatalf("b != c before test")
|
||||
}
|
||||
}
|
||||
aa := NewValue(a)
|
||||
ab := NewValue(b)
|
||||
a1 := a
|
||||
b1 := b
|
||||
aa := NewValue(&a1).Elem()
|
||||
ab := NewValue(&b1).Elem()
|
||||
for tocopy := 1; tocopy <= 7; tocopy++ {
|
||||
aa.SetLen(tocopy)
|
||||
Copy(ab, aa)
|
||||
@ -809,34 +825,6 @@ func TestInterfaceExtraction(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestInterfaceEditing(t *testing.T) {
|
||||
// strings are bigger than one word,
|
||||
// so the interface conversion allocates
|
||||
// memory to hold a string and puts that
|
||||
// pointer in the interface.
|
||||
var i interface{} = "hello"
|
||||
|
||||
// if i pass the interface value by value
|
||||
// to NewValue, i should get a fresh copy
|
||||
// of the value.
|
||||
v := NewValue(i)
|
||||
|
||||
// and setting that copy to "bye" should
|
||||
// not change the value stored in i.
|
||||
v.SetString("bye")
|
||||
if i.(string) != "hello" {
|
||||
t.Errorf(`Set("bye") changed i to %s`, i.(string))
|
||||
}
|
||||
|
||||
// the same should be true of smaller items.
|
||||
i = 123
|
||||
v = NewValue(i)
|
||||
v.SetInt(234)
|
||||
if i.(int) != 123 {
|
||||
t.Errorf("Set(234) changed i to %d", i.(int))
|
||||
}
|
||||
}
|
||||
|
||||
func TestNilPtrValueSub(t *testing.T) {
|
||||
var pi *int
|
||||
if pv := NewValue(pi); pv.Elem().IsValid() {
|
||||
@ -983,7 +971,7 @@ func TestChan(t *testing.T) {
|
||||
t.Errorf("TrySend on sync chan succeeded")
|
||||
}
|
||||
if v, ok := cv.TryRecv(); v.IsValid() || ok {
|
||||
t.Errorf("TryRecv on sync chan succeeded")
|
||||
t.Errorf("TryRecv on sync chan succeeded: isvalid=%v ok=%v", v.IsValid(), ok)
|
||||
}
|
||||
|
||||
// len/cap
|
||||
@ -1022,7 +1010,10 @@ type Point struct {
|
||||
x, y int
|
||||
}
|
||||
|
||||
func (p Point) Dist(scale int) int { return p.x*p.x*scale + p.y*p.y*scale }
|
||||
func (p Point) Dist(scale int) int {
|
||||
// println("Point.Dist", p.x, p.y, scale)
|
||||
return p.x*p.x*scale + p.y*p.y*scale
|
||||
}
|
||||
|
||||
func TestMethod(t *testing.T) {
|
||||
// Non-curried method of type.
|
||||
@ -1049,18 +1040,12 @@ func TestMethod(t *testing.T) {
|
||||
t.Errorf("Value Method returned %d; want 250", i)
|
||||
}
|
||||
|
||||
// Curried method of pointer to value.
|
||||
i = NewValue(p).Addr().Method(0).Call([]Value{NewValue(10)})[0].Int()
|
||||
if i != 250 {
|
||||
t.Errorf("Value Method returned %d; want 250", i)
|
||||
}
|
||||
|
||||
// Curried method of interface value.
|
||||
// Have to wrap interface value in a struct to get at it.
|
||||
// Passing it to NewValue directly would
|
||||
// access the underlying Point, not the interface.
|
||||
var s = struct {
|
||||
x interface {
|
||||
X interface {
|
||||
Dist(int) int
|
||||
}
|
||||
}{p}
|
||||
@ -1385,7 +1370,8 @@ func TestAddr(t *testing.T) {
|
||||
|
||||
// Again but take address of the NewValue value.
|
||||
// Exercises generation of PtrTypes not present in the binary.
|
||||
v = NewValue(&p)
|
||||
q := &p
|
||||
v = NewValue(&q).Elem()
|
||||
v = v.Addr()
|
||||
v = v.Elem()
|
||||
v = v.Elem()
|
||||
@ -1399,7 +1385,8 @@ func TestAddr(t *testing.T) {
|
||||
|
||||
// Starting without pointer we should get changed value
|
||||
// in interface.
|
||||
v = NewValue(p)
|
||||
qq := p
|
||||
v = NewValue(&qq).Elem()
|
||||
v0 := v
|
||||
v = v.Addr()
|
||||
v = v.Elem()
|
||||
@ -1415,3 +1402,36 @@ func TestAddr(t *testing.T) {
|
||||
t.Errorf("Addr.Elem.Set valued to set value in top value")
|
||||
}
|
||||
}
|
||||
|
||||
func noAlloc(t *testing.T, n int, f func(int)) {
|
||||
// once to prime everything
|
||||
f(-1)
|
||||
runtime.MemStats.Mallocs = 0
|
||||
|
||||
for j := 0; j < n; j++ {
|
||||
f(j)
|
||||
}
|
||||
if runtime.MemStats.Mallocs != 0 {
|
||||
t.Fatalf("%d mallocs after %d iterations", runtime.MemStats.Mallocs, n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAllocations(t *testing.T) {
|
||||
noAlloc(t, 100, func(j int) {
|
||||
var i interface{}
|
||||
var v Value
|
||||
i = 42 + j
|
||||
v = NewValue(i)
|
||||
if int(v.Int()) != 42+j {
|
||||
panic("wrong int")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestSmallNegativeInt(t *testing.T) {
|
||||
i := int16(-1)
|
||||
v := NewValue(i)
|
||||
if v.Int() != -1 {
|
||||
t.Errorf("int16(-1).Int() returned %v", v.Int())
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,6 @@
|
||||
|
||||
package reflect
|
||||
|
||||
|
||||
// During deepValueEqual, must keep track of checks that are
|
||||
// in progress. The comparison algorithm assumes that all
|
||||
// checks in progress are true when it reencounters them.
|
||||
@ -21,7 +20,7 @@ type visit struct {
|
||||
// Tests for deep equality using reflected types. The map argument tracks
|
||||
// comparisons that have already been seen, which allows short circuiting on
|
||||
// recursive types.
|
||||
func deepValueEqual(v1, v2 Value, visited map[uintptr]*visit, depth int) bool {
|
||||
func deepValueEqual(v1, v2 Value, visited map[uintptr]*visit, depth int) (b bool) {
|
||||
if !v1.IsValid() || !v2.IsValid() {
|
||||
return v1.IsValid() == v2.IsValid()
|
||||
}
|
||||
@ -31,6 +30,7 @@ func deepValueEqual(v1, v2 Value, visited map[uintptr]*visit, depth int) bool {
|
||||
|
||||
// if depth > 10 { panic("deepValueEqual") } // for debugging
|
||||
|
||||
if v1.CanAddr() && v2.CanAddr() {
|
||||
addr1 := v1.UnsafeAddr()
|
||||
addr2 := v2.UnsafeAddr()
|
||||
if addr1 > addr2 {
|
||||
@ -55,6 +55,7 @@ func deepValueEqual(v1, v2 Value, visited map[uintptr]*visit, depth int) bool {
|
||||
|
||||
// Remember for later.
|
||||
visited[h] = &visit{addr1, addr2, typ, seen}
|
||||
}
|
||||
|
||||
switch v1.Kind() {
|
||||
case Array:
|
||||
|
@ -47,7 +47,7 @@ type Type interface {
|
||||
// method signature, without a receiver, and the Func field is nil.
|
||||
Method(int) Method
|
||||
|
||||
// NumMethods returns the number of methods in the type's method set.
|
||||
// NumMethod returns the number of methods in the type's method set.
|
||||
NumMethod() int
|
||||
|
||||
// Name returns the type's name within its package.
|
||||
@ -162,6 +162,8 @@ type Type interface {
|
||||
// It panics if i is not in the range [0, NumOut()).
|
||||
Out(i int) Type
|
||||
|
||||
runtimeType() *runtime.Type
|
||||
common() *commonType
|
||||
uncommon() *uncommonType
|
||||
}
|
||||
|
||||
@ -408,9 +410,12 @@ func (t *commonType) String() string { return *t.string }
|
||||
func (t *commonType) Size() uintptr { return t.size }
|
||||
|
||||
func (t *commonType) Bits() int {
|
||||
if t == nil {
|
||||
panic("reflect: Bits of nil Type")
|
||||
}
|
||||
k := t.Kind()
|
||||
if k < Int || k > Complex128 {
|
||||
panic("reflect: Bits of non-arithmetic Type")
|
||||
panic("reflect: Bits of non-arithmetic Type " + t.String())
|
||||
}
|
||||
return int(t.size) * 8
|
||||
}
|
||||
@ -431,12 +436,14 @@ func (t *uncommonType) Method(i int) (m Method) {
|
||||
if p.name != nil {
|
||||
m.Name = *p.name
|
||||
}
|
||||
flag := uint32(0)
|
||||
if p.pkgPath != nil {
|
||||
m.PkgPath = *p.pkgPath
|
||||
flag |= flagRO
|
||||
}
|
||||
m.Type = toType(p.typ)
|
||||
fn := p.tfn
|
||||
m.Func = Value{&funcValue{value: value{m.Type, addr(&fn), canSet}}}
|
||||
m.Func = valueFromIword(flag, m.Type, iword(fn))
|
||||
return
|
||||
}
|
||||
|
||||
@ -772,24 +779,32 @@ func (t *structType) FieldByNameFunc(match func(string) bool) (f StructField, pr
|
||||
}
|
||||
|
||||
// Convert runtime type to reflect type.
|
||||
func toType(p *runtime.Type) Type {
|
||||
func toCommonType(p *runtime.Type) *commonType {
|
||||
if p == nil {
|
||||
return nil
|
||||
}
|
||||
type hdr struct {
|
||||
x interface{}
|
||||
t commonType
|
||||
}
|
||||
t := &(*hdr)(unsafe.Pointer(p)).t
|
||||
return t.toType()
|
||||
x := unsafe.Pointer(p)
|
||||
if uintptr(x)&reflectFlags != 0 {
|
||||
panic("invalid interface value")
|
||||
}
|
||||
return &(*hdr)(x).t
|
||||
}
|
||||
|
||||
func toType(p *runtime.Type) Type {
|
||||
if p == nil {
|
||||
return nil
|
||||
}
|
||||
return toCommonType(p).toType()
|
||||
}
|
||||
|
||||
// Typeof returns the reflection Type of the value in the interface{}.
|
||||
func Typeof(i interface{}) Type {
|
||||
type hdr struct {
|
||||
typ *byte
|
||||
val *commonType
|
||||
}
|
||||
rt := unsafe.Typeof(i)
|
||||
t := (*(*hdr)(unsafe.Pointer(&rt))).val
|
||||
return t.toType()
|
||||
eface := *(*emptyInterface)(unsafe.Pointer(&i))
|
||||
return toType(eface.typ)
|
||||
}
|
||||
|
||||
// ptrMap is the cache for PtrTo.
|
||||
@ -798,6 +813,16 @@ var ptrMap struct {
|
||||
m map[*commonType]*ptrType
|
||||
}
|
||||
|
||||
func (t *commonType) runtimeType() *runtime.Type {
|
||||
// The runtime.Type always precedes the commonType in memory.
|
||||
// Adjust pointer to find it.
|
||||
var rt struct {
|
||||
i runtime.Type
|
||||
ct commonType
|
||||
}
|
||||
return (*runtime.Type)(unsafe.Pointer(uintptr(unsafe.Pointer(t)) - uintptr(unsafe.Offsetof(rt.ct))))
|
||||
}
|
||||
|
||||
// PtrTo returns the pointer type with element t.
|
||||
// For example, if t represents type Foo, PtrTo(t) represents *Foo.
|
||||
func PtrTo(t Type) Type {
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -71,7 +71,6 @@ OFILES=\
|
||||
msize.$O\
|
||||
print.$O\
|
||||
proc.$O\
|
||||
reflect.$O\
|
||||
rune.$O\
|
||||
runtime.$O\
|
||||
runtime1.$O\
|
||||
|
@ -128,6 +128,15 @@ runtime·makechan_c(Type *elem, int64 hint)
|
||||
return c;
|
||||
}
|
||||
|
||||
// For reflect
|
||||
// func makechan(typ *ChanType, size uint32) (chan)
|
||||
void
|
||||
reflect·makechan(ChanType *t, uint32 size, Hchan *c)
|
||||
{
|
||||
c = runtime·makechan_c(t->elem, size);
|
||||
FLUSH(&c);
|
||||
}
|
||||
|
||||
static void
|
||||
destroychan(Hchan *c)
|
||||
{
|
||||
@ -263,6 +272,7 @@ closed:
|
||||
runtime·panicstring("send on closed channel");
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
runtime·chanrecv(Hchan* c, byte *ep, bool *selected, bool *received)
|
||||
{
|
||||
@ -519,6 +529,71 @@ runtime·selectnbrecv2(byte *v, bool *received, Hchan *c, bool selected)
|
||||
runtime·chanrecv(c, v, &selected, received);
|
||||
}
|
||||
|
||||
// For reflect:
|
||||
// func chansend(c chan, val iword, nb bool) (selected bool)
|
||||
// where an iword is the same word an interface value would use:
|
||||
// the actual data if it fits, or else a pointer to the data.
|
||||
//
|
||||
// The "uintptr selected" is really "bool selected" but saying
|
||||
// uintptr gets us the right alignment for the output parameter block.
|
||||
void
|
||||
reflect·chansend(Hchan *c, uintptr val, bool nb, uintptr selected)
|
||||
{
|
||||
bool *sp;
|
||||
byte *vp;
|
||||
|
||||
if(c == nil)
|
||||
runtime·panicstring("send to nil channel");
|
||||
|
||||
if(nb) {
|
||||
selected = false;
|
||||
sp = (bool*)&selected;
|
||||
} else {
|
||||
*(bool*)&selected = true;
|
||||
FLUSH(&selected);
|
||||
sp = nil;
|
||||
}
|
||||
if(c->elemsize <= sizeof(val))
|
||||
vp = (byte*)&val;
|
||||
else
|
||||
vp = (byte*)val;
|
||||
runtime·chansend(c, vp, sp);
|
||||
}
|
||||
|
||||
// For reflect:
|
||||
// func chanrecv(c chan, nb bool) (val iword, selected, received bool)
|
||||
// where an iword is the same word an interface value would use:
|
||||
// the actual data if it fits, or else a pointer to the data.
|
||||
void
|
||||
reflect·chanrecv(Hchan *c, bool nb, uintptr val, bool selected, bool received)
|
||||
{
|
||||
byte *vp;
|
||||
bool *sp;
|
||||
|
||||
if(c == nil)
|
||||
runtime·panicstring("receive from nil channel");
|
||||
|
||||
if(nb) {
|
||||
selected = false;
|
||||
sp = &selected;
|
||||
} else {
|
||||
selected = true;
|
||||
FLUSH(&selected);
|
||||
sp = nil;
|
||||
}
|
||||
received = false;
|
||||
FLUSH(&received);
|
||||
if(c->elemsize <= sizeof(val)) {
|
||||
val = 0;
|
||||
vp = (byte*)&val;
|
||||
} else {
|
||||
vp = runtime·mal(c->elemsize);
|
||||
val = (uintptr)vp;
|
||||
FLUSH(&val);
|
||||
}
|
||||
runtime·chanrecv(c, vp, sp, &received);
|
||||
}
|
||||
|
||||
static void newselect(int32, Select**);
|
||||
|
||||
// newselect(size uint32) (sel *byte);
|
||||
@ -1036,22 +1111,36 @@ runtime·closechan(Hchan *c)
|
||||
runtime·unlock(c);
|
||||
}
|
||||
|
||||
// For reflect
|
||||
// func chanclose(c chan)
|
||||
void
|
||||
runtime·chanclose(Hchan *c)
|
||||
reflect·chanclose(Hchan *c)
|
||||
{
|
||||
runtime·closechan(c);
|
||||
}
|
||||
|
||||
int32
|
||||
runtime·chanlen(Hchan *c)
|
||||
// For reflect
|
||||
// func chanlen(c chan) (len int32)
|
||||
void
|
||||
reflect·chanlen(Hchan *c, int32 len)
|
||||
{
|
||||
return c->qcount;
|
||||
if(c == nil)
|
||||
len = 0;
|
||||
else
|
||||
len = c->qcount;
|
||||
FLUSH(&len);
|
||||
}
|
||||
|
||||
int32
|
||||
runtime·chancap(Hchan *c)
|
||||
// For reflect
|
||||
// func chancap(c chan) (cap int32)
|
||||
void
|
||||
reflect·chancap(Hchan *c, int32 cap)
|
||||
{
|
||||
return c->dataqsiz;
|
||||
if(c == nil)
|
||||
cap = 0;
|
||||
else
|
||||
cap = c->dataqsiz;
|
||||
FLUSH(&cap);
|
||||
}
|
||||
|
||||
static SudoG*
|
||||
|
@ -776,6 +776,15 @@ runtime·makemap(Type *key, Type *val, int64 hint, Hmap *ret)
|
||||
FLUSH(&ret);
|
||||
}
|
||||
|
||||
// For reflect:
|
||||
// func makemap(Type *mapType) (hmap *map)
|
||||
void
|
||||
reflect·makemap(MapType *t, Hmap *ret)
|
||||
{
|
||||
ret = runtime·makemap_c(t->key, t->elem, 0);
|
||||
FLUSH(&ret);
|
||||
}
|
||||
|
||||
void
|
||||
runtime·mapaccess(Hmap *h, byte *ak, byte *av, bool *pres)
|
||||
{
|
||||
@ -855,6 +864,34 @@ runtime·mapaccess2(Hmap *h, ...)
|
||||
}
|
||||
}
|
||||
|
||||
// For reflect:
|
||||
// func mapaccess(h map, key iword) (val iword, pres bool)
|
||||
// where an iword is the same word an interface value would use:
|
||||
// the actual data if it fits, or else a pointer to the data.
|
||||
void
|
||||
reflect·mapaccess(Hmap *h, uintptr key, uintptr val, bool pres)
|
||||
{
|
||||
byte *ak, *av;
|
||||
|
||||
if(h == nil)
|
||||
runtime·panicstring("lookup in nil map");
|
||||
if(h->keysize <= sizeof(key))
|
||||
ak = (byte*)&key;
|
||||
else
|
||||
ak = (byte*)key;
|
||||
val = 0;
|
||||
pres = false;
|
||||
if(h->valsize <= sizeof(val))
|
||||
av = (byte*)&val;
|
||||
else {
|
||||
av = runtime·mal(h->valsize);
|
||||
val = (uintptr)av;
|
||||
}
|
||||
runtime·mapaccess(h, ak, av, &pres);
|
||||
FLUSH(&val);
|
||||
FLUSH(&pres);
|
||||
}
|
||||
|
||||
void
|
||||
runtime·mapassign(Hmap *h, byte *ak, byte *av)
|
||||
{
|
||||
@ -938,6 +975,30 @@ runtime·mapassign2(Hmap *h, ...)
|
||||
}
|
||||
}
|
||||
|
||||
// For reflect:
|
||||
// func mapassign(h map, key, val iword, pres bool)
|
||||
// where an iword is the same word an interface value would use:
|
||||
// the actual data if it fits, or else a pointer to the data.
|
||||
void
|
||||
reflect·mapassign(Hmap *h, uintptr key, uintptr val, bool pres)
|
||||
{
|
||||
byte *ak, *av;
|
||||
|
||||
if(h == nil)
|
||||
runtime·panicstring("lookup in nil map");
|
||||
if(h->keysize <= sizeof(key))
|
||||
ak = (byte*)&key;
|
||||
else
|
||||
ak = (byte*)key;
|
||||
if(h->valsize <= sizeof(val))
|
||||
av = (byte*)&val;
|
||||
else
|
||||
av = (byte*)val;
|
||||
if(!pres)
|
||||
av = nil;
|
||||
runtime·mapassign(h, ak, av);
|
||||
}
|
||||
|
||||
// mapiterinit(hmap *map[any]any, hiter *any);
|
||||
void
|
||||
runtime·mapiterinit(Hmap *h, struct hash_iter *it)
|
||||
@ -959,14 +1020,14 @@ runtime·mapiterinit(Hmap *h, struct hash_iter *it)
|
||||
}
|
||||
}
|
||||
|
||||
struct hash_iter*
|
||||
runtime·newmapiterinit(Hmap *h)
|
||||
// For reflect:
|
||||
// func mapiterinit(h map) (it iter)
|
||||
void
|
||||
reflect·mapiterinit(Hmap *h, struct hash_iter *it)
|
||||
{
|
||||
struct hash_iter *it;
|
||||
|
||||
it = runtime·mal(sizeof *it);
|
||||
FLUSH(&it);
|
||||
runtime·mapiterinit(h, it);
|
||||
return it;
|
||||
}
|
||||
|
||||
// mapiternext(hiter *any);
|
||||
@ -986,6 +1047,14 @@ runtime·mapiternext(struct hash_iter *it)
|
||||
}
|
||||
}
|
||||
|
||||
// For reflect:
|
||||
// func mapiternext(it iter)
|
||||
void
|
||||
reflect·mapiternext(struct hash_iter *it)
|
||||
{
|
||||
runtime·mapiternext(it);
|
||||
}
|
||||
|
||||
// mapiter1(hiter *any) (key any);
|
||||
#pragma textflag 7
|
||||
void
|
||||
@ -1026,6 +1095,48 @@ runtime·mapiterkey(struct hash_iter *it, void *ak)
|
||||
return true;
|
||||
}
|
||||
|
||||
// For reflect:
|
||||
// func mapiterkey(h map) (key iword, ok bool)
|
||||
// where an iword is the same word an interface value would use:
|
||||
// the actual data if it fits, or else a pointer to the data.
|
||||
void
|
||||
reflect·mapiterkey(struct hash_iter *it, uintptr key, bool ok)
|
||||
{
|
||||
Hmap *h;
|
||||
byte *res;
|
||||
|
||||
key = 0;
|
||||
ok = false;
|
||||
h = it->h;
|
||||
res = it->data;
|
||||
if(res == nil) {
|
||||
key = 0;
|
||||
ok = false;
|
||||
} else {
|
||||
key = 0;
|
||||
if(h->keysize <= sizeof(key))
|
||||
h->keyalg->copy(h->keysize, (byte*)&key, res);
|
||||
else
|
||||
key = (uintptr)res;
|
||||
ok = true;
|
||||
}
|
||||
FLUSH(&key);
|
||||
FLUSH(&ok);
|
||||
}
|
||||
|
||||
// For reflect:
|
||||
// func maplen(h map) (len int32)
|
||||
// Like len(m) in the actual language, we treat the nil map as length 0.
|
||||
void
|
||||
reflect·maplen(Hmap *h, int32 len)
|
||||
{
|
||||
if(h == nil)
|
||||
len = 0;
|
||||
else
|
||||
len = h->count;
|
||||
FLUSH(&len);
|
||||
}
|
||||
|
||||
// mapiter2(hiter *any) (key any, val any);
|
||||
#pragma textflag 7
|
||||
void
|
||||
|
@ -6,6 +6,14 @@
|
||||
#include "type.h"
|
||||
#include "malloc.h"
|
||||
|
||||
enum
|
||||
{
|
||||
// If an empty interface has these bits set in its type
|
||||
// pointer, it was copied from a reflect.Value and is
|
||||
// not a valid empty interface.
|
||||
reflectFlags = 3,
|
||||
};
|
||||
|
||||
void
|
||||
runtime·printiface(Iface i)
|
||||
{
|
||||
@ -276,6 +284,8 @@ runtime·assertE2T(Type *t, Eface e, ...)
|
||||
{
|
||||
byte *ret;
|
||||
|
||||
if(((uintptr)e.type&reflectFlags) != 0)
|
||||
runtime·throw("invalid interface value");
|
||||
ret = (byte*)(&e+1);
|
||||
assertE2Tret(t, e, ret);
|
||||
}
|
||||
@ -285,6 +295,8 @@ assertE2Tret(Type *t, Eface e, byte *ret)
|
||||
{
|
||||
Eface err;
|
||||
|
||||
if(((uintptr)e.type&reflectFlags) != 0)
|
||||
runtime·throw("invalid interface value");
|
||||
if(e.type == nil) {
|
||||
runtime·newTypeAssertionError(nil, nil, t,
|
||||
nil, nil, t->string,
|
||||
@ -309,6 +321,8 @@ runtime·assertE2T2(Type *t, Eface e, ...)
|
||||
bool *ok;
|
||||
int32 wid;
|
||||
|
||||
if(((uintptr)e.type&reflectFlags) != 0)
|
||||
runtime·throw("invalid interface value");
|
||||
ret = (byte*)(&e+1);
|
||||
wid = t->size;
|
||||
ok = (bool*)(ret+runtime·rnd(wid, 1));
|
||||
@ -444,6 +458,8 @@ runtime·ifaceE2I(InterfaceType *inter, Eface e, Iface *ret)
|
||||
Type *t;
|
||||
Eface err;
|
||||
|
||||
if(((uintptr)e.type&reflectFlags) != 0)
|
||||
runtime·throw("invalid interface value");
|
||||
t = e.type;
|
||||
if(t == nil) {
|
||||
// explicit conversions require non-nil interface value.
|
||||
@ -456,6 +472,14 @@ runtime·ifaceE2I(InterfaceType *inter, Eface e, Iface *ret)
|
||||
ret->tab = itab(inter, t, 0);
|
||||
}
|
||||
|
||||
// For reflect
|
||||
// func ifaceE2I(t *InterfaceType, e interface{}, dst *Iface)
|
||||
void
|
||||
reflect·ifaceE2I(InterfaceType *inter, Eface e, Iface *dst)
|
||||
{
|
||||
runtime·ifaceE2I(inter, e, dst);
|
||||
}
|
||||
|
||||
// func ifaceE2I(sigi *byte, iface any) (ret any)
|
||||
void
|
||||
runtime·assertE2I(InterfaceType* inter, Eface e, Iface ret)
|
||||
@ -467,6 +491,8 @@ runtime·assertE2I(InterfaceType* inter, Eface e, Iface ret)
|
||||
void
|
||||
runtime·assertE2I2(InterfaceType *inter, Eface e, Iface ret, bool ok)
|
||||
{
|
||||
if(((uintptr)e.type&reflectFlags) != 0)
|
||||
runtime·throw("invalid interface value");
|
||||
if(e.type == nil) {
|
||||
ok = 0;
|
||||
ret.data = nil;
|
||||
@ -489,6 +515,8 @@ runtime·assertE2E(InterfaceType* inter, Eface e, Eface ret)
|
||||
Type *t;
|
||||
Eface err;
|
||||
|
||||
if(((uintptr)e.type&reflectFlags) != 0)
|
||||
runtime·throw("invalid interface value");
|
||||
t = e.type;
|
||||
if(t == nil) {
|
||||
// explicit conversions require non-nil interface value.
|
||||
@ -505,6 +533,8 @@ runtime·assertE2E(InterfaceType* inter, Eface e, Eface ret)
|
||||
void
|
||||
runtime·assertE2E2(InterfaceType* inter, Eface e, Eface ret, bool ok)
|
||||
{
|
||||
if(((uintptr)e.type&reflectFlags) != 0)
|
||||
runtime·throw("invalid interface value");
|
||||
USED(inter);
|
||||
ret = e;
|
||||
ok = e.type != nil;
|
||||
@ -582,6 +612,10 @@ runtime·ifaceeq_c(Iface i1, Iface i2)
|
||||
bool
|
||||
runtime·efaceeq_c(Eface e1, Eface e2)
|
||||
{
|
||||
if(((uintptr)e1.type&reflectFlags) != 0)
|
||||
runtime·throw("invalid interface value");
|
||||
if(((uintptr)e2.type&reflectFlags) != 0)
|
||||
runtime·throw("invalid interface value");
|
||||
if(e1.type != e2.type)
|
||||
return false;
|
||||
if(e1.type == nil)
|
||||
@ -624,6 +658,8 @@ runtime·efacethash(Eface e1, uint32 ret)
|
||||
{
|
||||
Type *t;
|
||||
|
||||
if(((uintptr)e1.type&reflectFlags) != 0)
|
||||
runtime·throw("invalid interface value");
|
||||
ret = 0;
|
||||
t = e1.type;
|
||||
if(t != nil)
|
||||
@ -634,11 +670,14 @@ runtime·efacethash(Eface e1, uint32 ret)
|
||||
void
|
||||
unsafe·Typeof(Eface e, Eface ret)
|
||||
{
|
||||
if(((uintptr)e.type&reflectFlags) != 0)
|
||||
runtime·throw("invalid interface value");
|
||||
if(e.type == nil) {
|
||||
ret.type = nil;
|
||||
ret.data = nil;
|
||||
} else
|
||||
ret = *(Eface*)e.type;
|
||||
} else {
|
||||
ret = *(Eface*)(e.type);
|
||||
}
|
||||
FLUSH(&ret);
|
||||
}
|
||||
|
||||
@ -648,6 +687,8 @@ unsafe·Reflect(Eface e, Eface rettype, void *retaddr)
|
||||
uintptr *p;
|
||||
uintptr x;
|
||||
|
||||
if(((uintptr)e.type&reflectFlags) != 0)
|
||||
runtime·throw("invalid interface value");
|
||||
if(e.type == nil) {
|
||||
rettype.type = nil;
|
||||
rettype.data = nil;
|
||||
@ -678,6 +719,9 @@ unsafe·Reflect(Eface e, Eface rettype, void *retaddr)
|
||||
void
|
||||
unsafe·Unreflect(Eface typ, void *addr, Eface e)
|
||||
{
|
||||
if(((uintptr)typ.type&reflectFlags) != 0)
|
||||
runtime·throw("invalid interface value");
|
||||
|
||||
// Reflect library has reinterpreted typ
|
||||
// as its own kind of type structure.
|
||||
// We know that the pointer to the original
|
||||
@ -702,6 +746,9 @@ unsafe·New(Eface typ, void *ret)
|
||||
{
|
||||
Type *t;
|
||||
|
||||
if(((uintptr)typ.type&reflectFlags) != 0)
|
||||
runtime·throw("invalid interface value");
|
||||
|
||||
// Reflect library has reinterpreted typ
|
||||
// as its own kind of type structure.
|
||||
// We know that the pointer to the original
|
||||
@ -721,6 +768,9 @@ unsafe·NewArray(Eface typ, uint32 n, void *ret)
|
||||
uint64 size;
|
||||
Type *t;
|
||||
|
||||
if(((uintptr)typ.type&reflectFlags) != 0)
|
||||
runtime·throw("invalid interface value");
|
||||
|
||||
// Reflect library has reinterpreted typ
|
||||
// as its own kind of type structure.
|
||||
// We know that the pointer to the original
|
||||
|
@ -1,114 +0,0 @@
|
||||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package reflect
|
||||
#include "runtime.h"
|
||||
#include "type.h"
|
||||
|
||||
static Type*
|
||||
gettype(void *typ)
|
||||
{
|
||||
// typ is a *runtime.Type (or *runtime.MapType, etc), but the Type
|
||||
// defined in type.h includes an interface value header
|
||||
// in front of the raw structure. the -2 below backs up
|
||||
// to the interface value header.
|
||||
return (Type*)((void**)typ - 2);
|
||||
}
|
||||
|
||||
/*
|
||||
* Go wrappers around the C functions near the bottom of hashmap.c
|
||||
* There's no recursion here even though it looks like there is:
|
||||
* the names after func are in the reflect package name space
|
||||
* but the names in the C bodies are in the standard C name space.
|
||||
*/
|
||||
|
||||
func mapaccess(map *byte, key *byte, val *byte) (pres bool) {
|
||||
runtime·mapaccess((Hmap*)map, key, val, &pres);
|
||||
}
|
||||
|
||||
func mapassign(map *byte, key *byte, val *byte) {
|
||||
runtime·mapassign((Hmap*)map, key, val);
|
||||
}
|
||||
|
||||
func maplen(map *byte) (len int32) {
|
||||
// length is first word of map
|
||||
len = *(uint32*)map;
|
||||
}
|
||||
|
||||
func mapiterinit(map *byte) (it *byte) {
|
||||
it = (byte*)runtime·newmapiterinit((Hmap*)map);
|
||||
}
|
||||
|
||||
func mapiternext(it *byte) {
|
||||
runtime·mapiternext((struct hash_iter*)it);
|
||||
}
|
||||
|
||||
func mapiterkey(it *byte, key *byte) (ok bool) {
|
||||
ok = runtime·mapiterkey((struct hash_iter*)it, key);
|
||||
}
|
||||
|
||||
func makemap(typ *byte) (map *byte) {
|
||||
MapType *t;
|
||||
|
||||
t = (MapType*)gettype(typ);
|
||||
map = (byte*)runtime·makemap_c(t->key, t->elem, 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Go wrappers around the C functions in chan.c
|
||||
*/
|
||||
|
||||
func makechan(typ *byte, size uint32) (ch *byte) {
|
||||
ChanType *t;
|
||||
|
||||
// typ is a *runtime.ChanType, but the ChanType
|
||||
// defined in type.h includes an interface value header
|
||||
// in front of the raw ChanType. the -2 below backs up
|
||||
// to the interface value header.
|
||||
t = (ChanType*)gettype(typ);
|
||||
ch = (byte*)runtime·makechan_c(t->elem, size);
|
||||
}
|
||||
|
||||
func chansend(ch *byte, val *byte, selected *bool) {
|
||||
runtime·chansend((Hchan*)ch, val, selected);
|
||||
}
|
||||
|
||||
func chanrecv(ch *byte, val *byte, selected *bool, received *bool) {
|
||||
runtime·chanrecv((Hchan*)ch, val, selected, received);
|
||||
}
|
||||
|
||||
func chanclose(ch *byte) {
|
||||
runtime·chanclose((Hchan*)ch);
|
||||
}
|
||||
|
||||
func chanlen(ch *byte) (r int32) {
|
||||
r = runtime·chanlen((Hchan*)ch);
|
||||
}
|
||||
|
||||
func chancap(ch *byte) (r int32) {
|
||||
r = runtime·chancap((Hchan*)ch);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Go wrappers around the functions in iface.c
|
||||
*/
|
||||
|
||||
func setiface(typ *byte, x *byte, ret *byte) {
|
||||
InterfaceType *t;
|
||||
|
||||
t = (InterfaceType*)gettype(typ);
|
||||
if(t->mhdr.len == 0) {
|
||||
// already an empty interface
|
||||
*(Eface*)ret = *(Eface*)x;
|
||||
return;
|
||||
}
|
||||
if(((Eface*)x)->type == nil) {
|
||||
// can assign nil to any interface
|
||||
((Iface*)ret)->tab = nil;
|
||||
((Iface*)ret)->data = nil;
|
||||
return;
|
||||
}
|
||||
runtime·ifaceE2I((InterfaceType*)gettype(typ), *(Eface*)x, (Iface*)ret);
|
||||
}
|
@ -580,7 +580,6 @@ int32 runtime·gomaxprocsfunc(int32 n);
|
||||
|
||||
void runtime·mapassign(Hmap*, byte*, byte*);
|
||||
void runtime·mapaccess(Hmap*, byte*, byte*, bool*);
|
||||
struct hash_iter* runtime·newmapiterinit(Hmap*);
|
||||
void runtime·mapiternext(struct hash_iter*);
|
||||
bool runtime·mapiterkey(struct hash_iter*, void*);
|
||||
void runtime·mapiterkeyvalue(struct hash_iter*, void*, void*);
|
||||
@ -589,7 +588,6 @@ Hmap* runtime·makemap_c(Type*, Type*, int64);
|
||||
Hchan* runtime·makechan_c(Type*, int64);
|
||||
void runtime·chansend(Hchan*, void*, bool*);
|
||||
void runtime·chanrecv(Hchan*, void*, bool*, bool*);
|
||||
void runtime·chanclose(Hchan*);
|
||||
int32 runtime·chanlen(Hchan*);
|
||||
int32 runtime·chancap(Hchan*);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user