-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
87 lines (73 loc) · 1.78 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package prototype
import (
"reflect"
"sync"
)
const startDetectingCyclesAfter = 1000
// cloneContext 记录递归调用路径中的指针,避免可能导致的堆栈溢出
// 记录克隆中的错误
type cloneContext struct {
ptrLevel uint
ptrSeen map[any]struct{}
options *options
errors []error
}
func (g *cloneContext) forward() {
g.ptrLevel++
}
func (g *cloneContext) back() {
g.ptrLevel--
}
func (g *cloneContext) isTooDeep() bool {
return g.ptrLevel > startDetectingCyclesAfter
}
func (g *cloneContext) isSeen(ptr any) bool {
_, ok := g.ptrSeen[ptr]
return ok
}
func (g *cloneContext) remember(ptr any) {
g.ptrSeen[ptr] = struct{}{}
}
func (g *cloneContext) forget(ptr any) {
delete(g.ptrSeen, ptr)
}
func (g *cloneContext) checkPointerCycle(ptrFunc func(srcVal reflect.Value) any, cloner clonerFunc) clonerFunc {
return func(g *cloneContext, labels []string, tgtVal, srcVal reflect.Value, opts *options) error {
if g.forward(); g.isTooDeep() {
ptr := ptrFunc(srcVal)
if g.isSeen(ptr) {
return newPointerCycleError(labels, srcVal.Type())
}
g.remember(ptr)
defer g.forget(ptr)
}
defer g.back()
return cloner(g, labels, tgtVal, srcVal, opts)
}
}
func (g *cloneContext) error(err error) error {
if err == nil {
return nil
}
if err != nil && g.options.InterruptOnError {
return err
}
g.errors = append(g.errors, err)
return nil
}
var cloneContextPool sync.Pool
func newCloneContext(o *options) *cloneContext {
if v := cloneContextPool.Get(); v != nil {
g := v.(*cloneContext)
if len(g.ptrSeen) > 0 {
panic("cloner should have emptied ptrSeen via defers")
}
g.ptrLevel = 0
g.options = o
return g
}
return &cloneContext{ptrSeen: make(map[any]struct{})}
}
func freeCloneContext(ctx *cloneContext) {
cloneContextPool.Put(ctx)
}