forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stash.go
275 lines (238 loc) · 5.81 KB
/
stash.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package otto
import (
"fmt"
)
// ======
// _stash
// ======
type _stash interface {
hasBinding(string) bool //
createBinding(string, bool, Value) // CreateMutableBinding
setBinding(string, Value, bool) // SetMutableBinding
getBinding(string, bool) Value // GetBindingValue
deleteBinding(string) bool //
setValue(string, Value, bool) // createBinding + setBinding
outer() _stash
runtime() *_runtime
newReference(string, bool, _at) _reference
clone(clone *_clone) _stash
}
// ==========
// _objectStash
// ==========
type _objectStash struct {
_runtime *_runtime
_outer _stash
object *_object
}
func (self *_objectStash) runtime() *_runtime {
return self._runtime
}
func (runtime *_runtime) newObjectStash(object *_object, outer _stash) *_objectStash {
if object == nil {
object = runtime.newBaseObject()
object.class = "environment"
}
return &_objectStash{
_runtime: runtime,
_outer: outer,
object: object,
}
}
func (in *_objectStash) clone(clone *_clone) _stash {
out, exists := clone.objectStash(in)
if exists {
return out
}
*out = _objectStash{
clone.runtime,
clone.stash(in._outer),
clone.object(in.object),
}
return out
}
func (self *_objectStash) hasBinding(name string) bool {
return self.object.hasProperty(name)
}
func (self *_objectStash) createBinding(name string, deletable bool, value Value) {
if self.object.hasProperty(name) {
panic(hereBeDragons())
}
mode := _propertyMode(0111)
if !deletable {
mode = _propertyMode(0110)
}
// TODO False?
self.object.defineProperty(name, value, mode, false)
}
func (self *_objectStash) setBinding(name string, value Value, strict bool) {
self.object.put(name, value, strict)
}
func (self *_objectStash) setValue(name string, value Value, throw bool) {
if !self.hasBinding(name) {
self.createBinding(name, true, value) // Configurable by default
} else {
self.setBinding(name, value, throw)
}
}
func (self *_objectStash) getBinding(name string, throw bool) Value {
if self.object.hasProperty(name) {
return self.object.get(name)
}
if throw { // strict?
panic(self._runtime.panicReferenceError("Not Defined", name))
}
return Value{}
}
func (self *_objectStash) deleteBinding(name string) bool {
return self.object.delete(name, false)
}
func (self *_objectStash) outer() _stash {
return self._outer
}
func (self *_objectStash) newReference(name string, strict bool, at _at) _reference {
return newPropertyReference(self._runtime, self.object, name, strict, at)
}
// =========
// _dclStash
// =========
type _dclStash struct {
_runtime *_runtime
_outer _stash
property map[string]_dclProperty
}
type _dclProperty struct {
value Value
mutable bool
deletable bool
readable bool
}
func (runtime *_runtime) newDeclarationStash(outer _stash) *_dclStash {
return &_dclStash{
_runtime: runtime,
_outer: outer,
property: map[string]_dclProperty{},
}
}
func (in *_dclStash) clone(clone *_clone) _stash {
out, exists := clone.dclStash(in)
if exists {
return out
}
property := make(map[string]_dclProperty, len(in.property))
for index, value := range in.property {
property[index] = clone.dclProperty(value)
}
*out = _dclStash{
clone.runtime,
clone.stash(in._outer),
property,
}
return out
}
func (self *_dclStash) hasBinding(name string) bool {
_, exists := self.property[name]
return exists
}
func (self *_dclStash) runtime() *_runtime {
return self._runtime
}
func (self *_dclStash) createBinding(name string, deletable bool, value Value) {
_, exists := self.property[name]
if exists {
panic(fmt.Errorf("createBinding: %s: already exists", name))
}
self.property[name] = _dclProperty{
value: value,
mutable: true,
deletable: deletable,
readable: false,
}
}
func (self *_dclStash) setBinding(name string, value Value, strict bool) {
property, exists := self.property[name]
if !exists {
panic(fmt.Errorf("setBinding: %s: missing", name))
}
if property.mutable {
property.value = value
self.property[name] = property
} else {
self._runtime.typeErrorResult(strict)
}
}
func (self *_dclStash) setValue(name string, value Value, throw bool) {
if !self.hasBinding(name) {
self.createBinding(name, false, value) // NOT deletable by default
} else {
self.setBinding(name, value, throw)
}
}
// FIXME This is called a __lot__
func (self *_dclStash) getBinding(name string, throw bool) Value {
property, exists := self.property[name]
if !exists {
panic(fmt.Errorf("getBinding: %s: missing", name))
}
if !property.mutable && !property.readable {
if throw { // strict?
panic(self._runtime.panicTypeError())
}
return Value{}
}
return property.value
}
func (self *_dclStash) deleteBinding(name string) bool {
property, exists := self.property[name]
if !exists {
return true
}
if !property.deletable {
return false
}
delete(self.property, name)
return true
}
func (self *_dclStash) outer() _stash {
return self._outer
}
func (self *_dclStash) newReference(name string, strict bool, _ _at) _reference {
return &_stashReference{
name: name,
base: self,
}
}
// ========
// _fnStash
// ========
type _fnStash struct {
_dclStash
arguments *_object
indexOfArgumentName map[string]string
}
func (runtime *_runtime) newFunctionStash(outer _stash) *_fnStash {
return &_fnStash{
_dclStash: _dclStash{
_runtime: runtime,
_outer: outer,
property: map[string]_dclProperty{},
},
}
}
func (in *_fnStash) clone(clone *_clone) _stash {
out, exists := clone.fnStash(in)
if exists {
return out
}
dclStash := in._dclStash.clone(clone).(*_dclStash)
index := make(map[string]string, len(in.indexOfArgumentName))
for name, value := range in.indexOfArgumentName {
index[name] = value
}
*out = _fnStash{
_dclStash: *dclStash,
arguments: clone.object(in.arguments),
indexOfArgumentName: index,
}
return out
}