-
Notifications
You must be signed in to change notification settings - Fork 589
/
stash.go
289 lines (254 loc) · 6.15 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package otto
import (
"fmt"
)
// stasher is implemented by types which can stash data.
type stasher interface {
hasBinding(name string) bool //
createBinding(name string, deletable bool, value Value) // CreateMutableBinding
setBinding(name string, value Value, strict bool) // SetMutableBinding
getBinding(name string, throw bool) Value // GetBindingValue
deleteBinding(name string) bool //
setValue(name string, value Value, throw bool) // createBinding + setBinding
outer() stasher
runtime() *runtime
newReference(name string, strict bool, atv at) referencer
clone(cloner *cloner) stasher
}
type objectStash struct {
rt *runtime
outr stasher
object *object
}
func (s *objectStash) runtime() *runtime {
return s.rt
}
func (rt *runtime) newObjectStash(obj *object, outer stasher) *objectStash {
if obj == nil {
obj = rt.newBaseObject()
obj.class = "environment"
}
return &objectStash{
rt: rt,
outr: outer,
object: obj,
}
}
func (s *objectStash) clone(c *cloner) stasher {
out, exists := c.objectStash(s)
if exists {
return out
}
*out = objectStash{
c.runtime,
c.stash(s.outr),
c.object(s.object),
}
return out
}
func (s *objectStash) hasBinding(name string) bool {
return s.object.hasProperty(name)
}
func (s *objectStash) createBinding(name string, deletable bool, value Value) {
if s.object.hasProperty(name) {
panic(hereBeDragons())
}
mode := propertyMode(0o111)
if !deletable {
mode = propertyMode(0o110)
}
// TODO False?
s.object.defineProperty(name, value, mode, false)
}
func (s *objectStash) setBinding(name string, value Value, strict bool) {
s.object.put(name, value, strict)
}
func (s *objectStash) setValue(name string, value Value, throw bool) {
if !s.hasBinding(name) {
s.createBinding(name, true, value) // Configurable by default
} else {
s.setBinding(name, value, throw)
}
}
func (s *objectStash) getBinding(name string, throw bool) Value {
if s.object.hasProperty(name) {
return s.object.get(name)
}
if throw { // strict?
panic(s.rt.panicReferenceError("Not Defined", name))
}
return Value{}
}
func (s *objectStash) deleteBinding(name string) bool {
return s.object.delete(name, false)
}
func (s *objectStash) outer() stasher {
return s.outr
}
func (s *objectStash) newReference(name string, strict bool, atv at) referencer {
return newPropertyReference(s.rt, s.object, name, strict, atv)
}
type dclStash struct {
rt *runtime
outr stasher
property map[string]dclProperty
}
type dclProperty struct {
value Value
mutable bool
deletable bool
readable bool
}
func (rt *runtime) newDeclarationStash(outer stasher) *dclStash {
return &dclStash{
rt: rt,
outr: outer,
property: map[string]dclProperty{},
}
}
func (s *dclStash) clone(c *cloner) stasher {
out, exists := c.dclStash(s)
if exists {
return out
}
prop := make(map[string]dclProperty, len(s.property))
for index, value := range s.property {
prop[index] = c.dclProperty(value)
}
*out = dclStash{
c.runtime,
c.stash(s.outr),
prop,
}
return out
}
func (s *dclStash) hasBinding(name string) bool {
_, exists := s.property[name]
return exists
}
func (s *dclStash) runtime() *runtime {
return s.rt
}
func (s *dclStash) createBinding(name string, deletable bool, value Value) {
if _, exists := s.property[name]; exists {
panic(fmt.Errorf("createBinding: %s: already exists", name))
}
s.property[name] = dclProperty{
value: value,
mutable: true,
deletable: deletable,
readable: false,
}
}
func (s *dclStash) setBinding(name string, value Value, strict bool) {
prop, exists := s.property[name]
if !exists {
panic(fmt.Errorf("setBinding: %s: missing", name))
}
if prop.mutable {
prop.value = value
s.property[name] = prop
} else {
s.rt.typeErrorResult(strict)
}
}
func (s *dclStash) setValue(name string, value Value, throw bool) {
if !s.hasBinding(name) {
s.createBinding(name, false, value) // NOT deletable by default
} else {
s.setBinding(name, value, throw)
}
}
// FIXME This is called a __lot__.
func (s *dclStash) getBinding(name string, throw bool) Value {
prop, exists := s.property[name]
if !exists {
panic(fmt.Errorf("getBinding: %s: missing", name))
}
if !prop.mutable && !prop.readable {
if throw { // strict?
panic(s.rt.panicTypeError("getBinding property %s not mutable and not readable", name))
}
return Value{}
}
return prop.value
}
func (s *dclStash) deleteBinding(name string) bool {
prop, exists := s.property[name]
if !exists {
return true
}
if !prop.deletable {
return false
}
delete(s.property, name)
return true
}
func (s *dclStash) outer() stasher {
return s.outr
}
func (s *dclStash) newReference(name string, strict bool, _ at) referencer {
return &stashReference{
name: name,
base: s,
}
}
// ========
// _fnStash
// ========
type fnStash struct {
dclStash
arguments *object
indexOfArgumentName map[string]string
}
func (rt *runtime) newFunctionStash(outer stasher) *fnStash {
return &fnStash{
dclStash: dclStash{
rt: rt,
outr: outer,
property: map[string]dclProperty{},
},
}
}
func (s *fnStash) clone(c *cloner) stasher {
out, exists := c.fnStash(s)
if exists {
return out
}
dclStash := s.dclStash.clone(c).(*dclStash)
index := make(map[string]string, len(s.indexOfArgumentName))
for name, value := range s.indexOfArgumentName {
index[name] = value
}
*out = fnStash{
dclStash: *dclStash,
arguments: c.object(s.arguments),
indexOfArgumentName: index,
}
return out
}
// getStashProperties returns the properties from stash.
func getStashProperties(stash stasher) []string {
switch vars := stash.(type) {
case *dclStash:
keys := make([]string, 0, len(vars.property))
for k := range vars.property {
keys = append(keys, k)
}
return keys
case *fnStash:
keys := make([]string, 0, len(vars.property))
for k := range vars.property {
keys = append(keys, k)
}
return keys
case *objectStash:
keys := make([]string, 0, len(vars.object.property))
for k := range vars.object.property {
keys = append(keys, k)
}
return keys
default:
panic("unknown stash type")
}
}