Skip to content

Commit 6a25fd4

Browse files
ydnardeadprogram
authored andcommitted
reflect, internal/reflectlite: add Value.SetIter{Key,Value} and MapIter.Reset
Fixes #4790. Depends on #4787 (merge that first).
1 parent d95c1b5 commit 6a25fd4

File tree

3 files changed

+44
-18
lines changed

3 files changed

+44
-18
lines changed

src/internal/reflectlite/value.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,20 +1102,9 @@ func hashmapNewIterator() unsafe.Pointer
11021102
func hashmapNext(m unsafe.Pointer, it unsafe.Pointer, key, value unsafe.Pointer) bool
11031103

11041104
func (v Value) MapRange() *MapIter {
1105-
if v.Kind() != Map {
1106-
panic(&ValueError{Method: "MapRange", Kind: v.Kind()})
1107-
}
1108-
1109-
keyType := v.typecode.key()
1110-
1111-
keyTypeIsEmptyInterface := keyType.Kind() == Interface && keyType.NumMethod() == 0
1112-
shouldUnpackInterface := !keyTypeIsEmptyInterface && keyType.Kind() != String && !keyType.isBinary()
1113-
1114-
return &MapIter{
1115-
m: v,
1116-
it: hashmapNewIterator(),
1117-
unpackKeyInterface: shouldUnpackInterface,
1118-
}
1105+
iter := &MapIter{}
1106+
iter.Reset(v)
1107+
return iter
11191108
}
11201109

11211110
type MapIter struct {
@@ -1142,6 +1131,10 @@ func (it *MapIter) Key() Value {
11421131
return it.key.Elem()
11431132
}
11441133

1134+
func (v Value) SetIterKey(iter *MapIter) {
1135+
v.Set(iter.Key())
1136+
}
1137+
11451138
func (it *MapIter) Value() Value {
11461139
if !it.valid {
11471140
panic("reflect.MapIter.Value called on invalid iterator")
@@ -1150,6 +1143,10 @@ func (it *MapIter) Value() Value {
11501143
return it.val.Elem()
11511144
}
11521145

1146+
func (v Value) SetIterValue(iter *MapIter) {
1147+
v.Set(iter.Value())
1148+
}
1149+
11531150
func (it *MapIter) Next() bool {
11541151
it.key = New(it.m.typecode.Key())
11551152
it.val = New(it.m.typecode.Elem())
@@ -1158,6 +1155,23 @@ func (it *MapIter) Next() bool {
11581155
return it.valid
11591156
}
11601157

1158+
func (iter *MapIter) Reset(v Value) {
1159+
if v.Kind() != Map {
1160+
panic(&ValueError{Method: "MapRange", Kind: v.Kind()})
1161+
}
1162+
1163+
keyType := v.typecode.key()
1164+
1165+
keyTypeIsEmptyInterface := keyType.Kind() == Interface && keyType.NumMethod() == 0
1166+
shouldUnpackInterface := !keyTypeIsEmptyInterface && keyType.Kind() != String && !keyType.isBinary()
1167+
1168+
*iter = MapIter{
1169+
m: v,
1170+
it: hashmapNewIterator(),
1171+
unpackKeyInterface: shouldUnpackInterface,
1172+
}
1173+
}
1174+
11611175
func (v Value) Set(x Value) {
11621176
v.checkAddressable()
11631177
v.checkRO()

src/reflect/all_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,6 @@ func TestSetValue(t *testing.T) {
381381
}
382382
}
383383

384-
/*
385-
386384
func TestMapIterSet(t *testing.T) {
387385
m := make(map[string]any, len(valueTests))
388386
for _, tt := range valueTests {
@@ -430,8 +428,6 @@ func TestMapIterSet(t *testing.T) {
430428
}
431429
}
432430

433-
*/
434-
435431
func TestCanIntUintFloatComplex(t *testing.T) {
436432
type integer int
437433
type uinteger uint
@@ -7955,6 +7951,8 @@ func TestConvertibleTo(t *testing.T) {
79557951
}
79567952
}
79577953
7954+
*/
7955+
79587956
func TestSetIter(t *testing.T) {
79597957
data := map[string]int{
79607958
"foo": 1,
@@ -8044,6 +8042,8 @@ func TestSetIter(t *testing.T) {
80448042
}
80458043
}
80468044

8045+
/*
8046+
80478047
func TestMethodCallValueCodePtr(t *testing.T) {
80488048
m := ValueOf(Point{}).Method(1)
80498049
want := MethodValueCallCodePtr()

src/reflect/value.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,26 @@ func (it *MapIter) Key() Value {
6565
return Value{((*reflectlite.MapIter)(it)).Key()}
6666
}
6767

68+
func (v Value) SetIterKey(iter *MapIter) {
69+
v.Value.SetIterKey((*reflectlite.MapIter)(iter))
70+
}
71+
6872
func (it *MapIter) Value() Value {
6973
return Value{((*reflectlite.MapIter)(it)).Value()}
7074
}
7175

76+
func (v Value) SetIterValue(iter *MapIter) {
77+
v.Value.SetIterValue((*reflectlite.MapIter)(iter))
78+
}
79+
7280
func (it *MapIter) Next() bool {
7381
return ((*reflectlite.MapIter)(it)).Next()
7482
}
7583

84+
func (iter *MapIter) Reset(v Value) {
85+
(*reflectlite.MapIter)(iter).Reset(v.Value)
86+
}
87+
7688
func (v Value) Set(x Value) {
7789
v.Value.Set(x.Value)
7890
}

0 commit comments

Comments
 (0)