aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2023-07-07 15:58:08 +0200
committerRon Evans <[email protected]>2023-07-07 18:12:57 +0200
commitfffad84a63488d2ffd6d143229412dc0913d5458 (patch)
treea2b0ea459a6d636963150bd3e651225a5683293e
parente075e0591d555d3e657858f5186627f412dd500f (diff)
downloadtinygo-fffad84a63488d2ffd6d143229412dc0913d5458.tar.gz
tinygo-fffad84a63488d2ffd6d143229412dc0913d5458.zip
reflect: add SetZero
This was added in Go 1.20 and is required by encoding/json starting with Go 1.21.
-rw-r--r--src/reflect/all_test.go3
-rw-r--r--src/reflect/value.go10
2 files changed, 10 insertions, 3 deletions
diff --git a/src/reflect/all_test.go b/src/reflect/all_test.go
index cb7e4d2ac..39fc7a374 100644
--- a/src/reflect/all_test.go
+++ b/src/reflect/all_test.go
@@ -1559,8 +1559,6 @@ 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()
@@ -1568,7 +1566,6 @@ func TestIsZero(t *testing.T) {
t.Errorf("%d: IsZero((%s)(%+v)) is true after SetZero", i, p.Kind(), tt.x)
}
- */
}
diff --git a/src/reflect/value.go b/src/reflect/value.go
index 60df82686..2499fca78 100644
--- a/src/reflect/value.go
+++ b/src/reflect/value.go
@@ -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()
@@ -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