aboutsummaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/github.com/robertkrimen/otto/clone.go
blob: f79901b35749525cfd6615927087a81fe5a0bcf9 (plain) (blame)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package otto

import (
    "fmt"
)

type _clone struct {
    runtime      *_runtime
    _object      map[*_object]*_object
    _objectStash map[*_objectStash]*_objectStash
    _dclStash    map[*_dclStash]*_dclStash
    _fnStash     map[*_fnStash]*_fnStash
}

func (in *_runtime) clone() *_runtime {

    in.lck.Lock()
    defer in.lck.Unlock()

    out := &_runtime{}
    clone := _clone{
        runtime:      out,
        _object:      make(map[*_object]*_object),
        _objectStash: make(map[*_objectStash]*_objectStash),
        _dclStash:    make(map[*_dclStash]*_dclStash),
        _fnStash:     make(map[*_fnStash]*_fnStash),
    }

    globalObject := clone.object(in.globalObject)
    out.globalStash = out.newObjectStash(globalObject, nil)
    out.globalObject = globalObject
    out.global = _global{
        clone.object(in.global.Object),
        clone.object(in.global.Function),
        clone.object(in.global.Array),
        clone.object(in.global.String),
        clone.object(in.global.Boolean),
        clone.object(in.global.Number),
        clone.object(in.global.Math),
        clone.object(in.global.Date),
        clone.object(in.global.RegExp),
        clone.object(in.global.Error),
        clone.object(in.global.EvalError),
        clone.object(in.global.TypeError),
        clone.object(in.global.RangeError),
        clone.object(in.global.ReferenceError),
        clone.object(in.global.SyntaxError),
        clone.object(in.global.URIError),
        clone.object(in.global.JSON),

        clone.object(in.global.ObjectPrototype),
        clone.object(in.global.FunctionPrototype),
        clone.object(in.global.ArrayPrototype),
        clone.object(in.global.StringPrototype),
        clone.object(in.global.BooleanPrototype),
        clone.object(in.global.NumberPrototype),
        clone.object(in.global.DatePrototype),
        clone.object(in.global.RegExpPrototype),
        clone.object(in.global.ErrorPrototype),
        clone.object(in.global.EvalErrorPrototype),
        clone.object(in.global.TypeErrorPrototype),
        clone.object(in.global.RangeErrorPrototype),
        clone.object(in.global.ReferenceErrorPrototype),
        clone.object(in.global.SyntaxErrorPrototype),
        clone.object(in.global.URIErrorPrototype),
    }

    out.eval = out.globalObject.property["eval"].value.(Value).value.(*_object)
    out.globalObject.prototype = out.global.ObjectPrototype

    // Not sure if this is necessary, but give some help to the GC
    clone.runtime = nil
    clone._object = nil
    clone._objectStash = nil
    clone._dclStash = nil
    clone._fnStash = nil

    return out
}

func (clone *_clone) object(in *_object) *_object {
    if out, exists := clone._object[in]; exists {
        return out
    }
    out := &_object{}
    clone._object[in] = out
    return in.objectClass.clone(in, out, clone)
}

func (clone *_clone) dclStash(in *_dclStash) (*_dclStash, bool) {
    if out, exists := clone._dclStash[in]; exists {
        return out, true
    }
    out := &_dclStash{}
    clone._dclStash[in] = out
    return out, false
}

func (clone *_clone) objectStash(in *_objectStash) (*_objectStash, bool) {
    if out, exists := clone._objectStash[in]; exists {
        return out, true
    }
    out := &_objectStash{}
    clone._objectStash[in] = out
    return out, false
}

func (clone *_clone) fnStash(in *_fnStash) (*_fnStash, bool) {
    if out, exists := clone._fnStash[in]; exists {
        return out, true
    }
    out := &_fnStash{}
    clone._fnStash[in] = out
    return out, false
}

func (clone *_clone) value(in Value) Value {
    out := in
    switch value := in.value.(type) {
    case *_object:
        out.value = clone.object(value)
    }
    return out
}

func (clone *_clone) valueArray(in []Value) []Value {
    out := make([]Value, len(in))
    for index, value := range in {
        out[index] = clone.value(value)
    }
    return out
}

func (clone *_clone) stash(in _stash) _stash {
    if in == nil {
        return nil
    }
    return in.clone(clone)
}

func (clone *_clone) property(in _property) _property {
    out := in
    if value, valid := in.value.(Value); valid {
        out.value = clone.value(value)
    } else {
        panic(fmt.Errorf("in.value.(Value) != true"))
    }
    return out
}

func (clone *_clone) dclProperty(in _dclProperty) _dclProperty {
    out := in
    out.value = clone.value(in.value)
    return out
}