| 1 |
// Copyright 2019 Huan Du. All rights reserved. |
| 2 |
// Licensed under the MIT license that can be found in the LICENSE file. |
| 3 |
|
| 4 |
// Package clone provides functions to deep clone any Go data. |
| 5 |
// It also provides a wrapper to protect a pointer from any unexpected mutation. |
| 6 |
package clone |
| 7 |
|
| 8 |
import ( |
| 9 |
"fmt" |
| 10 |
"reflect" |
| 11 |
"unsafe" |
| 12 |
) |
| 13 |
|
| 14 |
var heapCloneState = &cloneState{ |
| 15 |
allocator: defaultAllocator, |
| 16 |
} |
| 17 |
var cloner = MakeCloner(defaultAllocator) |
| 18 |
|
| 19 |
// Clone recursively deep clone v to a new value in heap. |
| 20 |
// It assumes that there is no pointer cycle in v, |
| 21 |
// e.g. v has a pointer points to v itself. |
| 22 |
// If there is a pointer cycle, use Slowly instead. |
| 23 |
// |
| 24 |
// Clone allocates memory and deeply copies values inside v in depth-first sequence. |
| 25 |
// There are a few special rules for following types. |
| 26 |
// |
| 27 |
// - Scalar types: all number-like types are copied by value. |
| 28 |
// - func: Copied by value as func is an opaque pointer at runtime. |
| 29 |
// - string: Copied by value as string is immutable by design. |
| 30 |
// - unsafe.Pointer: Copied by value as we don't know what's in it. |
| 31 |
// - chan: A new empty chan is created as we cannot read data inside the old chan. |
| 32 |
// |
| 33 |
// Unlike many other packages, Clone is able to clone unexported fields of any struct. |
| 34 |
// Use this feature wisely. |
| 35 |
func Clone(v interface{}) interface{} { |
| 36 |
return cloner.Clone(v) |
| 37 |
} |
| 38 |
|
| 39 |
func clone(allocator *Allocator, v interface{}) interface{} { |
| 40 |
if v == nil { |
| 41 |
return nil |
| 42 |
} |
| 43 |
|
| 44 |
val := reflect.ValueOf(v) |
| 45 |
cloned := allocator.clone(val, false) |
| 46 |
return cloned.Interface() |
| 47 |
} |
| 48 |
|
| 49 |
// Slowly recursively deep clone v to a new value in heap. |
| 50 |
// It marks all cloned values internally, thus it can clone v with cycle pointer. |
| 51 |
// |
| 52 |
// Slowly works exactly the same as Clone. See Clone doc for more details. |
| 53 |
func Slowly(v interface{}) interface{} { |
| 54 |
return cloner.CloneSlowly(v) |
| 55 |
} |
| 56 |
|
| 57 |
func cloneSlowly(allocator *Allocator, v interface{}) interface{} { |
| 58 |
if v == nil { |
| 59 |
return nil |
| 60 |
} |
| 61 |
|
| 62 |
val := reflect.ValueOf(v) |
| 63 |
cloned := allocator.cloneSlowly(val, false) |
| 64 |
return cloned.Interface() |
| 65 |
} |
| 66 |
|
| 67 |
type cloneState struct { |
| 68 |
allocator *Allocator |
| 69 |
visited visitMap |
| 70 |
invalid invalidPointers |
| 71 |
|
| 72 |
// The value that should not be cloned by custom func. |
| 73 |
// It's useful to avoid infinite loop when custom func calls allocator.Clone(). |
| 74 |
skipCustomFuncValue reflect.Value |
| 75 |
} |
| 76 |
|
| 77 |
type visit struct { |
| 78 |
p uintptr |
| 79 |
extra int |
| 80 |
t reflect.Type |
| 81 |
} |
| 82 |
|
| 83 |
type visitMap map[visit]reflect.Value |
| 84 |
type invalidPointers map[visit]reflect.Value |
| 85 |
|
| 86 |
func (state *cloneState) clone(v reflect.Value) reflect.Value { |
| 87 |
if state.allocator.isScalar(v.Kind()) { |
| 88 |
return copyScalarValue(v) |
| 89 |
} |
| 90 |
|
| 91 |
switch v.Kind() { |
| 92 |
case reflect.Array: |
| 93 |
return state.cloneArray(v) |
| 94 |
case reflect.Chan: |
| 95 |
return state.allocator.MakeChan(v.Type(), v.Cap()) |
| 96 |
case reflect.Interface: |
| 97 |
return state.cloneInterface(v) |
| 98 |
case reflect.Map: |
| 99 |
return state.cloneMap(v) |
| 100 |
case reflect.Ptr: |
| 101 |
return state.clonePtr(v) |
| 102 |
case reflect.Slice: |
| 103 |
return state.cloneSlice(v) |
| 104 |
case reflect.Struct: |
| 105 |
return state.cloneStruct(v) |
| 106 |
case reflect.String: |
| 107 |
return state.cloneString(v) |
| 108 |
default: |
| 109 |
panic(fmt.Errorf("go-clone: <bug> unsupported type `%v`", v.Type())) |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
func (state *cloneState) cloneArray(v reflect.Value) reflect.Value { |
| 114 |
dst := state.allocator.New(v.Type()) |
| 115 |
state.copyArray(v, dst) |
| 116 |
return dst.Elem() |
| 117 |
} |
| 118 |
|
| 119 |
func (state *cloneState) copyArray(src, nv reflect.Value) { |
| 120 |
p := unsafe.Pointer(nv.Pointer()) // dst must be a Ptr. |
| 121 |
dst := nv.Elem() |
| 122 |
num := src.Len() |
| 123 |
|
| 124 |
if state.allocator.isScalar(src.Type().Elem().Kind()) { |
| 125 |
shadowCopy(src, p) |
| 126 |
return |
| 127 |
} |
| 128 |
|
| 129 |
for i := 0; i < num; i++ { |
| 130 |
dst.Index(i).Set(state.clone(src.Index(i))) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
func (state *cloneState) cloneInterface(v reflect.Value) reflect.Value { |
| 135 |
if v.IsNil() { |
| 136 |
return reflect.Zero(v.Type()) |
| 137 |
} |
| 138 |
|
| 139 |
t := v.Type() |
| 140 |
elem := v.Elem() |
| 141 |
return state.clone(elem).Convert(elem.Type()).Convert(t) |
| 142 |
} |
| 143 |
|
| 144 |
func (state *cloneState) cloneMap(v reflect.Value) reflect.Value { |
| 145 |
if v.IsNil() { |
| 146 |
return reflect.Zero(v.Type()) |
| 147 |
} |
| 148 |
|
| 149 |
t := v.Type() |
| 150 |
|
| 151 |
if state.visited != nil { |
| 152 |
vst := visit{ |
| 153 |
p: v.Pointer(), |
| 154 |
t: t, |
| 155 |
} |
| 156 |
|
| 157 |
if val, ok := state.visited[vst]; ok { |
| 158 |
return val |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
nv := state.allocator.MakeMap(t, v.Len()) |
| 163 |
|
| 164 |
if state.visited != nil { |
| 165 |
vst := visit{ |
| 166 |
p: v.Pointer(), |
| 167 |
t: t, |
| 168 |
} |
| 169 |
state.visited[vst] = nv |
| 170 |
} |
| 171 |
|
| 172 |
for iter := mapIter(v); iter.Next(); { |
| 173 |
key := state.clone(iter.Key()) |
| 174 |
value := state.clone(iter.Value()) |
| 175 |
nv.SetMapIndex(key, value) |
| 176 |
} |
| 177 |
|
| 178 |
return nv |
| 179 |
} |
| 180 |
|
| 181 |
func (state *cloneState) clonePtr(v reflect.Value) reflect.Value { |
| 182 |
if v.IsNil() { |
| 183 |
return reflect.Zero(v.Type()) |
| 184 |
} |
| 185 |
|
| 186 |
t := v.Type() |
| 187 |
|
| 188 |
if state.allocator.isOpaquePointer(t) { |
| 189 |
if v.CanInterface() { |
| 190 |
return v |
| 191 |
} |
| 192 |
|
| 193 |
ptr := state.allocator.New(t) |
| 194 |
p := unsafe.Pointer(ptr.Pointer()) |
| 195 |
shadowCopy(v, p) |
| 196 |
return ptr.Elem() |
| 197 |
} |
| 198 |
|
| 199 |
if state.visited != nil { |
| 200 |
vst := visit{ |
| 201 |
p: v.Pointer(), |
| 202 |
t: t, |
| 203 |
} |
| 204 |
|
| 205 |
if val, ok := state.visited[vst]; ok { |
| 206 |
return val |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
src := v.Elem() |
| 211 |
elemType := src.Type() |
| 212 |
elemKind := src.Kind() |
| 213 |
nv := state.allocator.New(elemType) |
| 214 |
|
| 215 |
if state.visited != nil { |
| 216 |
vst := visit{ |
| 217 |
p: v.Pointer(), |
| 218 |
t: t, |
| 219 |
} |
| 220 |
state.visited[vst] = nv |
| 221 |
} |
| 222 |
|
| 223 |
switch elemKind { |
| 224 |
case reflect.Struct: |
| 225 |
state.copyStruct(src, nv) |
| 226 |
case reflect.Array: |
| 227 |
state.copyArray(src, nv) |
| 228 |
default: |
| 229 |
nv.Elem().Set(state.clone(src)) |
| 230 |
} |
| 231 |
|
| 232 |
// If this pointer is the address of a struct field and it's a cycle pointer, |
| 233 |
// it may be updated. |
| 234 |
if state.visited != nil { |
| 235 |
vst := visit{ |
| 236 |
p: v.Pointer(), |
| 237 |
t: t, |
| 238 |
} |
| 239 |
nv = state.visited[vst] |
| 240 |
} |
| 241 |
|
| 242 |
return nv |
| 243 |
} |
| 244 |
|
| 245 |
func (state *cloneState) cloneSlice(v reflect.Value) reflect.Value { |
| 246 |
if v.IsNil() { |
| 247 |
return reflect.Zero(v.Type()) |
| 248 |
} |
| 249 |
|
| 250 |
t := v.Type() |
| 251 |
num := v.Len() |
| 252 |
|
| 253 |
if state.visited != nil { |
| 254 |
vst := visit{ |
| 255 |
p: v.Pointer(), |
| 256 |
extra: num, |
| 257 |
t: t, |
| 258 |
} |
| 259 |
|
| 260 |
if val, ok := state.visited[vst]; ok { |
| 261 |
return val |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
c := v.Cap() |
| 266 |
nv := state.allocator.MakeSlice(t, num, c) |
| 267 |
|
| 268 |
if state.visited != nil { |
| 269 |
vst := visit{ |
| 270 |
p: v.Pointer(), |
| 271 |
extra: num, |
| 272 |
t: t, |
| 273 |
} |
| 274 |
state.visited[vst] = nv |
| 275 |
} |
| 276 |
|
| 277 |
// For scalar slice, copy underlying values directly. |
| 278 |
if state.allocator.isScalar(t.Elem().Kind()) { |
| 279 |
src := unsafe.Pointer(v.Pointer()) |
| 280 |
dst := unsafe.Pointer(nv.Pointer()) |
| 281 |
sz := int(t.Elem().Size()) |
| 282 |
l := num * sz |
| 283 |
cc := c * sz |
| 284 |
copy((*[maxByteSize]byte)(dst)[:l:cc], (*[maxByteSize]byte)(src)[:l:cc]) |
| 285 |
} else { |
| 286 |
for i := 0; i < num; i++ { |
| 287 |
nv.Index(i).Set(state.clone(v.Index(i))) |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
return nv |
| 292 |
} |
| 293 |
|
| 294 |
func (state *cloneState) cloneStruct(v reflect.Value) reflect.Value { |
| 295 |
t := v.Type() |
| 296 |
nv := state.allocator.New(t) |
| 297 |
state.copyStruct(v, nv) |
| 298 |
return nv.Elem() |
| 299 |
} |
| 300 |
|
| 301 |
var typeOfByteSlice = reflect.TypeOf([]byte(nil)) |
| 302 |
|
| 303 |
func (state *cloneState) cloneString(v reflect.Value) reflect.Value { |
| 304 |
t := v.Type() |
| 305 |
l := v.Len() |
| 306 |
data := state.allocator.MakeSlice(typeOfByteSlice, l, l) |
| 307 |
|
| 308 |
// The v is an unexported struct field. |
| 309 |
if !v.CanInterface() { |
| 310 |
v = reflect.ValueOf(v.String()) |
| 311 |
} |
| 312 |
|
| 313 |
reflect.Copy(data, v) |
| 314 |
|
| 315 |
nv := state.allocator.New(t) |
| 316 |
slice := data.Interface().([]byte) |
| 317 |
*(*stringHeader)(unsafe.Pointer(nv.Pointer())) = *(*stringHeader)(unsafe.Pointer(&slice)) |
| 318 |
|
| 319 |
return nv.Elem() |
| 320 |
} |
| 321 |
|
| 322 |
func (state *cloneState) copyStruct(src, nv reflect.Value) { |
| 323 |
t := src.Type() |
| 324 |
st := state.allocator.loadStructType(t) |
| 325 |
ptr := unsafe.Pointer(nv.Pointer()) |
| 326 |
|
| 327 |
if st.Init(state.allocator, src, nv, state.skipCustomFuncValue == src) { |
| 328 |
return |
| 329 |
} |
| 330 |
|
| 331 |
for _, pf := range st.PointerFields { |
| 332 |
i := int(pf.Index) |
| 333 |
p := unsafe.Pointer(uintptr(ptr) + pf.Offset) |
| 334 |
field := src.Field(i) |
| 335 |
|
| 336 |
// This field can be referenced by a pointer or interface inside itself. |
| 337 |
// Put the pointer to this field to visited to avoid any error. |
| 338 |
// |
| 339 |
// See https://github.com/huandu/go-clone/issues/3. |
| 340 |
if state.visited != nil && field.CanAddr() { |
| 341 |
ft := field.Type() |
| 342 |
fp := field.Addr().Pointer() |
| 343 |
vst := visit{ |
| 344 |
p: fp, |
| 345 |
t: reflect.PtrTo(ft), |
| 346 |
} |
| 347 |
nv := reflect.NewAt(ft, p) |
| 348 |
|
| 349 |
// The address of this field was visited, so fp must be a cycle pointer. |
| 350 |
// As this field is not fully cloned, the val stored in visited[visit] must be wrong. |
| 351 |
// It must be replaced by nv which will be the right value (it's incomplete right now). |
| 352 |
// |
| 353 |
// Unfortunately, if the val was used by previous clone routines, |
| 354 |
// there is no easy way to fix wrong values - all pointers must be traversed and fixed. |
| 355 |
if val, ok := state.visited[vst]; ok { |
| 356 |
state.invalid[visit{ |
| 357 |
p: val.Pointer(), |
| 358 |
t: vst.t, |
| 359 |
}] = nv |
| 360 |
} |
| 361 |
|
| 362 |
state.visited[vst] = nv |
| 363 |
} |
| 364 |
|
| 365 |
v := state.clone(field) |
| 366 |
shadowCopy(v, p) |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
var typeOfString = reflect.TypeOf("") |
| 371 |
|
| 372 |
func shadowCopy(src reflect.Value, p unsafe.Pointer) { |
| 373 |
switch src.Kind() { |
| 374 |
case reflect.Bool: |
| 375 |
*(*bool)(p) = src.Bool() |
| 376 |
case reflect.Int: |
| 377 |
*(*int)(p) = int(src.Int()) |
| 378 |
case reflect.Int8: |
| 379 |
*(*int8)(p) = int8(src.Int()) |
| 380 |
case reflect.Int16: |
| 381 |
*(*int16)(p) = int16(src.Int()) |
| 382 |
case reflect.Int32: |
| 383 |
*(*int32)(p) = int32(src.Int()) |
| 384 |
case reflect.Int64: |
| 385 |
*(*int64)(p) = src.Int() |
| 386 |
case reflect.Uint: |
| 387 |
*(*uint)(p) = uint(src.Uint()) |
| 388 |
case reflect.Uint8: |
| 389 |
*(*uint8)(p) = uint8(src.Uint()) |
| 390 |
case reflect.Uint16: |
| 391 |
*(*uint16)(p) = uint16(src.Uint()) |
| 392 |
case reflect.Uint32: |
| 393 |
*(*uint32)(p) = uint32(src.Uint()) |
| 394 |
case reflect.Uint64: |
| 395 |
*(*uint64)(p) = src.Uint() |
| 396 |
case reflect.Uintptr: |
| 397 |
*(*uintptr)(p) = uintptr(src.Uint()) |
| 398 |
case reflect.Float32: |
| 399 |
*(*float32)(p) = float32(src.Float()) |
| 400 |
case reflect.Float64: |
| 401 |
*(*float64)(p) = src.Float() |
| 402 |
case reflect.Complex64: |
| 403 |
*(*complex64)(p) = complex64(src.Complex()) |
| 404 |
case reflect.Complex128: |
| 405 |
*(*complex128)(p) = src.Complex() |
| 406 |
|
| 407 |
case reflect.Array: |
| 408 |
t := src.Type() |
| 409 |
|
| 410 |
if src.CanAddr() { |
| 411 |
srcPtr := unsafe.Pointer(src.UnsafeAddr()) |
| 412 |
sz := t.Size() |
| 413 |
copy((*[maxByteSize]byte)(p)[:sz:sz], (*[maxByteSize]byte)(srcPtr)[:sz:sz]) |
| 414 |
return |
| 415 |
} |
| 416 |
|
| 417 |
val := reflect.NewAt(t, p).Elem() |
| 418 |
|
| 419 |
if src.CanInterface() { |
| 420 |
val.Set(src) |
| 421 |
return |
| 422 |
} |
| 423 |
|
| 424 |
sz := t.Elem().Size() |
| 425 |
num := src.Len() |
| 426 |
|
| 427 |
for i := 0; i < num; i++ { |
| 428 |
elemPtr := unsafe.Pointer(uintptr(p) + uintptr(i)*sz) |
| 429 |
shadowCopy(src.Index(i), elemPtr) |
| 430 |
} |
| 431 |
case reflect.Chan: |
| 432 |
*((*uintptr)(p)) = src.Pointer() |
| 433 |
case reflect.Func: |
| 434 |
t := src.Type() |
| 435 |
src = copyScalarValue(src) |
| 436 |
val := reflect.NewAt(t, p).Elem() |
| 437 |
val.Set(src) |
| 438 |
case reflect.Interface: |
| 439 |
*((*interfaceData)(p)) = parseReflectValue(src) |
| 440 |
case reflect.Map: |
| 441 |
*((*uintptr)(p)) = src.Pointer() |
| 442 |
case reflect.Ptr: |
| 443 |
*((*uintptr)(p)) = src.Pointer() |
| 444 |
case reflect.Slice: |
| 445 |
*(*sliceHeader)(p) = sliceHeader{ |
| 446 |
Data: src.Pointer(), |
| 447 |
Len: src.Len(), |
| 448 |
Cap: src.Cap(), |
| 449 |
} |
| 450 |
case reflect.String: |
| 451 |
s := src.String() |
| 452 |
val := reflect.NewAt(typeOfString, p).Elem() |
| 453 |
val.SetString(s) |
| 454 |
case reflect.Struct: |
| 455 |
t := src.Type() |
| 456 |
val := reflect.NewAt(t, p).Elem() |
| 457 |
|
| 458 |
if src.CanInterface() { |
| 459 |
val.Set(src) |
| 460 |
return |
| 461 |
} |
| 462 |
|
| 463 |
num := t.NumField() |
| 464 |
|
| 465 |
for i := 0; i < num; i++ { |
| 466 |
field := t.Field(i) |
| 467 |
fieldPtr := unsafe.Pointer(uintptr(p) + field.Offset) |
| 468 |
shadowCopy(src.Field(i), fieldPtr) |
| 469 |
} |
| 470 |
case reflect.UnsafePointer: |
| 471 |
// There is no way to copy unsafe.Pointer value. |
| 472 |
*((*uintptr)(p)) = src.Pointer() |
| 473 |
|
| 474 |
default: |
| 475 |
panic(fmt.Errorf("go-clone: <bug> impossible type `%v` when cloning private field", src.Type())) |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
// fix tranverses v to update all pointer values in state.invalid. |
| 480 |
func (state *cloneState) fix(v reflect.Value) { |
| 481 |
if state == nil || len(state.invalid) == 0 { |
| 482 |
return |
| 483 |
} |
| 484 |
|
| 485 |
fix := &fixState{ |
| 486 |
allocator: state.allocator, |
| 487 |
fixed: fixMap{}, |
| 488 |
invalid: state.invalid, |
| 489 |
} |
| 490 |
fix.fix(v) |
| 491 |
} |
| 492 |
|
| 493 |
type fixState struct { |
| 494 |
allocator *Allocator |
| 495 |
fixed fixMap |
| 496 |
invalid invalidPointers |
| 497 |
} |
| 498 |
|
| 499 |
type fixMap map[visit]struct{} |
| 500 |
|
| 501 |
func (fix *fixState) new(t reflect.Type) reflect.Value { |
| 502 |
return fix.allocator.New(t) |
| 503 |
} |
| 504 |
|
| 505 |
func (fix *fixState) fix(v reflect.Value) (copied reflect.Value, changed int) { |
| 506 |
if fix.allocator.isScalar(v.Kind()) { |
| 507 |
return |
| 508 |
} |
| 509 |
|
| 510 |
switch v.Kind() { |
| 511 |
case reflect.Array: |
| 512 |
return fix.fixArray(v) |
| 513 |
case reflect.Chan: |
| 514 |
// Do nothing. |
| 515 |
return |
| 516 |
case reflect.Interface: |
| 517 |
return fix.fixInterface(v) |
| 518 |
case reflect.Map: |
| 519 |
return fix.fixMap(v) |
| 520 |
case reflect.Ptr: |
| 521 |
return fix.fixPtr(v) |
| 522 |
case reflect.Slice: |
| 523 |
return fix.fixSlice(v) |
| 524 |
case reflect.Struct: |
| 525 |
return fix.fixStruct(v) |
| 526 |
case reflect.String: |
| 527 |
// Do nothing. |
| 528 |
return |
| 529 |
default: |
| 530 |
panic(fmt.Errorf("go-clone: <bug> unsupported type `%v`", v.Type())) |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
func (fix *fixState) fixArray(v reflect.Value) (copied reflect.Value, changed int) { |
| 535 |
t := v.Type() |
| 536 |
et := t.Elem() |
| 537 |
kind := et.Kind() |
| 538 |
|
| 539 |
if fix.allocator.isScalar(kind) { |
| 540 |
return |
| 541 |
} |
| 542 |
|
| 543 |
l := v.Len() |
| 544 |
|
| 545 |
for i := 0; i < l; i++ { |
| 546 |
elem := v.Index(i) |
| 547 |
|
| 548 |
if kind == reflect.Ptr { |
| 549 |
vst := visit{ |
| 550 |
p: elem.Pointer(), |
| 551 |
t: et, |
| 552 |
} |
| 553 |
|
| 554 |
if nv, ok := fix.invalid[vst]; ok { |
| 555 |
// If elem cannot be set, v must be copied to make it settable. |
| 556 |
// Don't do it unless there is no other choices. |
| 557 |
if !elem.CanSet() { |
| 558 |
copied = fix.new(t).Elem() |
| 559 |
shadowCopy(v, unsafe.Pointer(copied.Addr().Pointer())) |
| 560 |
_, changed = fix.fixArray(copied) |
| 561 |
return |
| 562 |
} |
| 563 |
|
| 564 |
elem.Set(nv) |
| 565 |
changed++ |
| 566 |
continue |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
fixed, c := fix.fix(elem) |
| 571 |
changed += c |
| 572 |
|
| 573 |
if fixed.IsValid() { |
| 574 |
// If elem cannot be set, v must be copied to make it settable. |
| 575 |
// Don't do it unless there is no other choices. |
| 576 |
if !elem.CanSet() { |
| 577 |
copied = fix.new(t).Elem() |
| 578 |
shadowCopy(v, unsafe.Pointer(copied.Addr().Pointer())) |
| 579 |
_, changed = fix.fixArray(copied) |
| 580 |
return |
| 581 |
} |
| 582 |
|
| 583 |
elem.Set(fixed) |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
return |
| 588 |
} |
| 589 |
|
| 590 |
func (fix *fixState) fixInterface(v reflect.Value) (copied reflect.Value, changed int) { |
| 591 |
if v.IsNil() { |
| 592 |
return |
| 593 |
} |
| 594 |
|
| 595 |
elem := v.Elem() |
| 596 |
t := elem.Type() |
| 597 |
kind := elem.Kind() |
| 598 |
|
| 599 |
if kind == reflect.Ptr { |
| 600 |
vst := visit{ |
| 601 |
p: elem.Pointer(), |
| 602 |
t: t, |
| 603 |
} |
| 604 |
|
| 605 |
if nv, ok := fix.invalid[vst]; ok { |
| 606 |
copied = nv.Convert(v.Type()) |
| 607 |
changed++ |
| 608 |
return |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
copied, changed = fix.fix(elem) |
| 613 |
|
| 614 |
if copied.IsValid() { |
| 615 |
copied = copied.Convert(v.Type()) |
| 616 |
} |
| 617 |
|
| 618 |
return |
| 619 |
} |
| 620 |
|
| 621 |
func (fix *fixState) fixMap(v reflect.Value) (copied reflect.Value, changed int) { |
| 622 |
if v.IsNil() { |
| 623 |
return |
| 624 |
} |
| 625 |
|
| 626 |
t := v.Type() |
| 627 |
vst := visit{ |
| 628 |
p: v.Pointer(), |
| 629 |
t: t, |
| 630 |
} |
| 631 |
|
| 632 |
if _, ok := fix.fixed[vst]; ok { |
| 633 |
return |
| 634 |
} |
| 635 |
|
| 636 |
fix.fixed[vst] = struct{}{} |
| 637 |
|
| 638 |
kt := t.Key() |
| 639 |
et := t.Elem() |
| 640 |
keyKind := kt.Kind() |
| 641 |
elemKind := et.Kind() |
| 642 |
|
| 643 |
if isScalar := fix.allocator.isScalar; isScalar(keyKind) && isScalar(elemKind) { |
| 644 |
return |
| 645 |
} |
| 646 |
|
| 647 |
invalidKeys := map[reflect.Value][2]reflect.Value{} |
| 648 |
|
| 649 |
for iter := mapIter(v); iter.Next(); { |
| 650 |
key := iter.Key() |
| 651 |
elem := iter.Value() |
| 652 |
var fixed reflect.Value |
| 653 |
c := 0 |
| 654 |
|
| 655 |
if elemKind == reflect.Ptr { |
| 656 |
vst := visit{ |
| 657 |
p: elem.Pointer(), |
| 658 |
t: et, |
| 659 |
} |
| 660 |
|
| 661 |
if nv, ok := fix.invalid[vst]; ok { |
| 662 |
fixed = nv |
| 663 |
c++ |
| 664 |
} else { |
| 665 |
fixed, c = fix.fixPtr(elem) |
| 666 |
} |
| 667 |
} else { |
| 668 |
fixed, c = fix.fix(elem) |
| 669 |
} |
| 670 |
|
| 671 |
changed += c |
| 672 |
c = 0 |
| 673 |
|
| 674 |
if fixed.IsValid() { |
| 675 |
v = forceSetMapIndex(v, key, fixed) |
| 676 |
elem = fixed |
| 677 |
fixed = reflect.Value{} |
| 678 |
} |
| 679 |
|
| 680 |
if keyKind == reflect.Ptr { |
| 681 |
vst := visit{ |
| 682 |
p: key.Pointer(), |
| 683 |
t: kt, |
| 684 |
} |
| 685 |
|
| 686 |
if nv, ok := fix.invalid[vst]; ok { |
| 687 |
fixed = nv |
| 688 |
c++ |
| 689 |
} else { |
| 690 |
fixed, c = fix.fixPtr(key) |
| 691 |
} |
| 692 |
} else { |
| 693 |
fixed, c = fix.fix(key) |
| 694 |
} |
| 695 |
|
| 696 |
changed += c |
| 697 |
|
| 698 |
// Key cannot be changed immediately inside map range iteration. |
| 699 |
// Do it later. |
| 700 |
if fixed.IsValid() { |
| 701 |
invalidKeys[key] = [2]reflect.Value{fixed, elem} |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
for key, kv := range invalidKeys { |
| 706 |
v = forceSetMapIndex(v, key, reflect.Value{}) |
| 707 |
v = forceSetMapIndex(v, kv[0], kv[1]) |
| 708 |
} |
| 709 |
|
| 710 |
return |
| 711 |
} |
| 712 |
|
| 713 |
func forceSetMapIndex(v, key, elem reflect.Value) (nv reflect.Value) { |
| 714 |
nv = v |
| 715 |
|
| 716 |
if !v.CanInterface() { |
| 717 |
nv = forceClearROFlag(v) |
| 718 |
} |
| 719 |
|
| 720 |
if !key.CanInterface() { |
| 721 |
key = forceClearROFlag(key) |
| 722 |
} |
| 723 |
|
| 724 |
if elem.IsValid() && !elem.CanInterface() { |
| 725 |
elem = forceClearROFlag(elem) |
| 726 |
} |
| 727 |
|
| 728 |
nv.SetMapIndex(key, elem) |
| 729 |
return |
| 730 |
} |
| 731 |
|
| 732 |
func (fix *fixState) fixPtr(v reflect.Value) (copied reflect.Value, changed int) { |
| 733 |
if v.IsNil() { |
| 734 |
return |
| 735 |
} |
| 736 |
|
| 737 |
vst := visit{ |
| 738 |
p: v.Pointer(), |
| 739 |
t: v.Type(), |
| 740 |
} |
| 741 |
|
| 742 |
if _, ok := fix.invalid[vst]; ok { |
| 743 |
panic(fmt.Errorf("go-clone: <bug> invalid pointers must have been fixed in other methods")) |
| 744 |
} |
| 745 |
|
| 746 |
if _, ok := fix.fixed[vst]; ok { |
| 747 |
return |
| 748 |
} |
| 749 |
|
| 750 |
fix.fixed[vst] = struct{}{} |
| 751 |
|
| 752 |
elem := v.Elem() |
| 753 |
_, changed = fix.fix(elem) |
| 754 |
return |
| 755 |
} |
| 756 |
|
| 757 |
func (fix *fixState) fixSlice(v reflect.Value) (copied reflect.Value, changed int) { |
| 758 |
if v.IsNil() { |
| 759 |
return |
| 760 |
} |
| 761 |
|
| 762 |
t := v.Type() |
| 763 |
et := t.Elem() |
| 764 |
kind := et.Kind() |
| 765 |
|
| 766 |
if fix.allocator.isScalar(kind) { |
| 767 |
return |
| 768 |
} |
| 769 |
|
| 770 |
l := v.Len() |
| 771 |
p := unsafe.Pointer(v.Pointer()) |
| 772 |
vst := visit{ |
| 773 |
p: uintptr(p), |
| 774 |
extra: l, |
| 775 |
t: t, |
| 776 |
} |
| 777 |
|
| 778 |
if _, ok := fix.fixed[vst]; ok { |
| 779 |
return |
| 780 |
} |
| 781 |
|
| 782 |
fix.fixed[vst] = struct{}{} |
| 783 |
|
| 784 |
for i := 0; i < l; i++ { |
| 785 |
elem := v.Index(i) |
| 786 |
var fixed reflect.Value |
| 787 |
c := 0 |
| 788 |
|
| 789 |
if kind == reflect.Ptr { |
| 790 |
vst := visit{ |
| 791 |
p: elem.Pointer(), |
| 792 |
t: et, |
| 793 |
} |
| 794 |
|
| 795 |
if nv, ok := fix.invalid[vst]; ok { |
| 796 |
fixed = nv |
| 797 |
} else { |
| 798 |
fixed, c = fix.fixPtr(elem) |
| 799 |
} |
| 800 |
} else { |
| 801 |
fixed, c = fix.fix(elem) |
| 802 |
} |
| 803 |
|
| 804 |
changed += c |
| 805 |
|
| 806 |
if fixed.IsValid() { |
| 807 |
sz := et.Size() |
| 808 |
elemPtr := unsafe.Pointer(uintptr(p) + sz*uintptr(i)) |
| 809 |
shadowCopy(fixed, elemPtr) |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
return |
| 814 |
} |
| 815 |
|
| 816 |
func (fix *fixState) fixStruct(v reflect.Value) (copied reflect.Value, changed int) { |
| 817 |
t := v.Type() |
| 818 |
st := fix.allocator.loadStructType(t) |
| 819 |
|
| 820 |
if len(st.PointerFields) == 0 { |
| 821 |
return |
| 822 |
} |
| 823 |
|
| 824 |
for _, pf := range st.PointerFields { |
| 825 |
i := int(pf.Index) |
| 826 |
field := v.Field(i) |
| 827 |
|
| 828 |
ft := field.Type() |
| 829 |
|
| 830 |
if ft.Kind() == reflect.Ptr { |
| 831 |
vst := visit{ |
| 832 |
p: field.Pointer(), |
| 833 |
t: ft, |
| 834 |
} |
| 835 |
|
| 836 |
if nv, ok := fix.invalid[vst]; ok { |
| 837 |
// If v is not addressable, a new struct must be allocated. |
| 838 |
// Don't do it unless there is no other choices. |
| 839 |
if !v.CanAddr() { |
| 840 |
copied = fix.new(t).Elem() |
| 841 |
shadowCopy(v, unsafe.Pointer(copied.Addr().Pointer())) |
| 842 |
_, changed = fix.fixStruct(copied) |
| 843 |
return |
| 844 |
} |
| 845 |
|
| 846 |
ptr := unsafe.Pointer(v.Addr().Pointer()) |
| 847 |
p := unsafe.Pointer(uintptr(ptr) + pf.Offset) |
| 848 |
shadowCopy(nv, p) |
| 849 |
continue |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
fixed, c := fix.fix(field) |
| 854 |
changed += c |
| 855 |
|
| 856 |
if fixed.IsValid() { |
| 857 |
// If v is not addressable, a new struct must be allocated. |
| 858 |
// Don't do it unless there is no other choices. |
| 859 |
if !v.CanAddr() { |
| 860 |
copied = fix.new(t).Elem() |
| 861 |
shadowCopy(v, unsafe.Pointer(copied.Addr().Pointer())) |
| 862 |
_, changed = fix.fixStruct(copied) |
| 863 |
return |
| 864 |
} |
| 865 |
|
| 866 |
ptr := unsafe.Pointer(v.Addr().Pointer()) |
| 867 |
p := unsafe.Pointer(uintptr(ptr) + pf.Offset) |
| 868 |
shadowCopy(fixed, p) |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
return |
| 873 |
} |