Skip to content

Commit

Permalink
reflect: add SetZero
Browse files Browse the repository at this point in the history
This was added in Go 1.20 and is required by encoding/json starting with
Go 1.21.
  • Loading branch information
aykevl committed Jul 7, 2023
1 parent 9d817c7 commit 23b24c8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
3 changes: 0 additions & 3 deletions src/reflect/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1559,16 +1559,13 @@ func TestIsZero(t *testing.T) {
t.Errorf("%d: IsZero(Zero(TypeOf((%s)(%+v)))) is false", i, x.Kind(), tt.x)
}

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

}
*/

}

Expand Down
10 changes: 10 additions & 0 deletions src/reflect/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,13 @@ func (v Value) Set(x Value) {
memcpy(v.value, xptr, size)
}

func (v Value) SetZero() {
v.checkAddressable()
v.checkRO()
size := v.typecode.Size()
memzero(v.value, size)
}

func (v Value) SetBool(x bool) {
v.checkAddressable()
v.checkRO()
Expand Down Expand Up @@ -1569,6 +1576,9 @@ func (e *ValueError) Error() string {
//go:linkname memcpy runtime.memcpy
func memcpy(dst, src unsafe.Pointer, size uintptr)

//go:linkname memzero runtime.memzero
func memzero(ptr unsafe.Pointer, size uintptr)

//go:linkname alloc runtime.alloc
func alloc(size uintptr, layout unsafe.Pointer) unsafe.Pointer

Expand Down

0 comments on commit 23b24c8

Please sign in to comment.