--- /dev/null
+pkg reflect, method (Value) Clear() #55002
}
wg.Wait()
}
+
+func TestClear(t *testing.T) {
+ m := make(map[string]any, len(valueTests))
+ for _, tt := range valueTests {
+ m[tt.s] = tt.i
+ }
+ mapTestFn := func(v Value) bool { v.Clear(); return v.Len() == 0 }
+
+ s := make([]*pair, len(valueTests))
+ for i := range s {
+ s[i] = &valueTests[i]
+ }
+ sliceTestFn := func(v Value) bool {
+ v.Clear()
+ for i := 0; i < v.Len(); i++ {
+ if !v.Index(i).IsZero() {
+ return false
+ }
+ }
+ return true
+ }
+
+ panicTestFn := func(v Value) bool { shouldPanic("reflect.Value.Clear", func() { v.Clear() }); return true }
+
+ tests := []struct {
+ name string
+ value Value
+ testFunc func(v Value) bool
+ }{
+ {"map", ValueOf(m), mapTestFn},
+ {"slice no pointer", ValueOf([]int{1, 2, 3, 4, 5}), sliceTestFn},
+ {"slice has pointer", ValueOf(s), sliceTestFn},
+ {"non-map/slice", ValueOf(1), panicTestFn},
+ }
+
+ for _, tc := range tests {
+ tc := tc
+ t.Run(tc.name, func(t *testing.T) {
+ t.Parallel()
+ if !tc.testFunc(tc.value) {
+ t.Errorf("unexpected result for value.Clear(): %value", tc.value)
+ }
+ })
+ }
+}
return v
}
+// Clear clears the contents of a map or zeros the contents of a slice.
+//
+// It panics if v's Kind is not Map or Slice.
+func (v Value) Clear() {
+ switch v.Kind() {
+ case Slice:
+ sh := *(*unsafeheader.Slice)(v.ptr)
+ st := (*sliceType)(unsafe.Pointer(v.typ))
+ typedarrayclear(st.elem, sh.Data, sh.Len)
+ case Map:
+ mapclear(v.typ, v.pointer())
+ default:
+ panic(&ValueError{"reflect.Value.Clear", v.Kind()})
+ }
+}
+
// Append appends the values x to a slice s and returns the resulting slice.
// As in Go, each x's value must be assignable to the slice's element type.
func Append(s Value, x ...Value) Value {
//go:noescape
func maplen(m unsafe.Pointer) int
+func mapclear(t *rtype, m unsafe.Pointer)
+
// call calls fn with "stackArgsSize" bytes of stack arguments laid out
// at stackArgs and register arguments laid out in regArgs. frameSize is
// the total amount of stack space that will be reserved by call, so this
//go:noescape
func typedslicecopy(elemType *rtype, dst, src unsafeheader.Slice) int
+// typedarrayclear zeroes the value at ptr of an array of elemType,
+// only clears len elem.
+//
+//go:noescape
+func typedarrayclear(elemType *rtype, ptr unsafe.Pointer, len int)
+
//go:noescape
func typehash(t *rtype, p unsafe.Pointer, h uintptr) uintptr
return h.count
}
+//go:linkname reflect_mapclear reflect.mapclear
+func reflect_mapclear(t *maptype, h *hmap) {
+ mapclear(t, h)
+}
+
//go:linkname reflectlite_maplen internal/reflectlite.maplen
func reflectlite_maplen(h *hmap) int {
if h == nil {
memclrNoHeapPointers(ptr, size)
}
+//go:linkname reflect_typedarrayclear reflect.typedarrayclear
+func reflect_typedarrayclear(typ *_type, ptr unsafe.Pointer, len int) {
+ size := typ.size * uintptr(len)
+ if writeBarrier.needed && typ.ptrdata != 0 {
+ bulkBarrierPreWrite(uintptr(ptr), 0, size)
+ }
+ memclrNoHeapPointers(ptr, size)
+}
+
// memclrHasPointers clears n bytes of typed memory starting at ptr.
// The caller must ensure that the type of the object at ptr has
// pointers, usually by checking typ.ptrdata. However, ptr