Skip to content

Commit fa91988

Browse files
committed
reflect: add SetZero
This was added in Go 1.20 and is required by encoding/json starting with Go 1.21.
1 parent 51b1ba5 commit fa91988

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/reflect/all_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,16 +1559,13 @@ func TestIsZero(t *testing.T) {
15591559
t.Errorf("%d: IsZero(Zero(TypeOf((%s)(%+v)))) is false", i, x.Kind(), tt.x)
15601560
}
15611561

1562-
/* // TODO(tinygo): missing SetZero support
1563-
15641562
p := New(x.Type()).Elem()
15651563
p.Set(x)
15661564
p.SetZero()
15671565
if !p.IsZero() {
15681566
t.Errorf("%d: IsZero((%s)(%+v)) is true after SetZero", i, p.Kind(), tt.x)
15691567

15701568
}
1571-
*/
15721569

15731570
}
15741571

src/reflect/value.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,13 @@ func (v Value) Set(x Value) {
10681068
memcpy(v.value, xptr, size)
10691069
}
10701070

1071+
func (v Value) SetZero() {
1072+
v.checkAddressable()
1073+
v.checkRO()
1074+
size := v.typecode.Size()
1075+
memzero(v.value, size)
1076+
}
1077+
10711078
func (v Value) SetBool(x bool) {
10721079
v.checkAddressable()
10731080
v.checkRO()
@@ -1569,6 +1576,9 @@ func (e *ValueError) Error() string {
15691576
//go:linkname memcpy runtime.memcpy
15701577
func memcpy(dst, src unsafe.Pointer, size uintptr)
15711578

1579+
//go:linkname memzero runtime.memzero
1580+
func memzero(ptr unsafe.Pointer, size uintptr)
1581+
15721582
//go:linkname alloc runtime.alloc
15731583
func alloc(size uintptr, layout unsafe.Pointer) unsafe.Pointer
15741584

0 commit comments

Comments
 (0)