forked from robertkrimen/otto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate_statement.go
316 lines (274 loc) · 7.23 KB
/
evaluate_statement.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package otto
func (self *_runtime) evaluateTryCatch(node *_tryCatchNode) Value {
tryCatchValue, exception := self.tryCatchEvaluate(func() Value {
return self.evaluate(node.Try)
})
if exception && node.Catch != nil {
lexicalEnvironment := self._executionContext(0).newDeclarativeEnvironment(self)
defer func() {
self._executionContext(0).LexicalEnvironment = lexicalEnvironment
}()
// TODO If necessary, convert TypeError<runtime> => TypeError
// That, is, such errors can be thrown despite not being JavaScript "native"
self.localSet(node.Catch.Identifier, tryCatchValue)
tryCatchValue, exception = self.tryCatchEvaluate(func() Value {
return self.evaluate(node.Catch.Body)
})
}
if node.Finally != nil {
finallyValue := self.evaluate(node.Finally)
if finallyValue.isResult() {
return finallyValue
}
}
if exception {
panic(tryCatchValue)
}
return tryCatchValue
}
func (self *_runtime) evaluateVariableDeclarationList(node *_variableDeclarationListNode) Value {
for _, node := range node.VariableList {
self.evaluateVariableDeclaration(node)
}
return emptyValue()
}
func (self *_runtime) evaluateVariableDeclaration(node *_variableDeclarationNode) Value {
if node.Operator != "" {
// FIXME If reference is nil
left := getIdentifierReference(self.LexicalEnvironment(), node.Identifier, false, node)
right := self.evaluate(node.Initializer)
rightValue := self.GetValue(right)
self.PutValue(left, rightValue)
}
return toValue_string(node.Identifier)
}
func (self *_runtime) evaluateThrow(node *_throwNode) Value {
panic(self.GetValue(self.evaluate(node.Argument)))
}
func (self *_runtime) evaluateReturn(node *_returnNode) Value {
value := UndefinedValue()
if node.Argument != nil {
value = self.GetValue(self.evaluate(node.Argument))
}
return toValue(newReturnResult(value))
}
func (self *_runtime) evaluateIf(node *_ifNode) Value {
test := self.evaluate(node.Test)
testValue := self.GetValue(test)
if toBoolean(testValue) {
return self.evaluate(node.Consequent)
} else if node.Alternate != nil {
return self.evaluate(node.Alternate)
}
return emptyValue()
}
func (self *_runtime) evaluateWith(node *_withNode) Value {
object := self.evaluate(node.Object)
objectValue := self.GetValue(object)
previousLexicalEnvironment, lexicalEnvironment := self._executionContext(0).newLexicalEnvironment(self.toObject(objectValue))
lexicalEnvironment.ProvideThis = true
defer func() {
self._executionContext(0).LexicalEnvironment = previousLexicalEnvironment
}()
return self.evaluate(node.Body)
}
func (self *_runtime) evaluateBlock(node *_blockNode) Value {
body := node.Body
labelSet := node.labelSet
blockValue := self.evaluateBody(body)
if blockValue.evaluateBreak(labelSet) == resultBreak {
return Value{}
}
return blockValue
}
func (self *_runtime) evaluateDoWhile(node *_doWhileNode) Value {
test := node.Test
body := node.body
labelSet := node.labelSet
doWhileValue := Value{}
resultBreak:
for {
for _, node := range body {
value := self.evaluate(node)
switch value.evaluateBreakContinue(labelSet) {
case resultReturn:
return value
case resultBreak:
break resultBreak
case resultContinue:
goto resultContinue
default: // resultNormal
}
if !value.isEmpty() {
doWhileValue = value
}
}
resultContinue:
if !self.GetValue(self.evaluate(test)).isTrue() {
// Stahp: do ... while (false)
break
}
}
return doWhileValue
}
func (self *_runtime) evaluateWhile(node *_whileNode) Value {
test := node.Test
body := node.body
labelSet := node.labelSet
whileValue := Value{}
resultBreakContinue:
for {
if !self.GetValue(self.evaluate(test)).isTrue() {
// Stahp: while (false) ...
break
}
for _, node := range body {
value := self.evaluate(node)
switch value.evaluateBreakContinue(labelSet) {
case resultReturn:
return value
case resultBreak:
break resultBreakContinue
case resultContinue:
continue resultBreakContinue
default: // resultNormal
}
if !value.isEmpty() {
whileValue = value
}
}
}
return whileValue
}
func (self *_runtime) evaluateFor(node *_forNode) Value {
initial := node.Initial
test := node.Test
body := node.body
update := node.Update
labelSet := node.labelSet
if initial != nil {
initialResult := self.evaluate(initial)
self.GetValue(initialResult) // Side-effect trigger
}
forValue := Value{}
resultBreak:
for {
if test != nil {
testResult := self.evaluate(test)
testResultValue := self.GetValue(testResult)
if toBoolean(testResultValue) == false {
break
}
}
if test != nil {
if !self.GetValue(self.evaluate(test)).isTrue() {
// Stahp: for (...; false; ...) ...
break
}
}
for _, node := range body {
value := self.evaluate(node)
switch value.evaluateBreakContinue(labelSet) {
case resultReturn:
return value
case resultBreak:
break resultBreak
case resultContinue:
goto resultContinue
default: // resultNormal
}
if !value.isEmpty() {
forValue = value
}
}
resultContinue:
if update != nil {
updateResult := self.evaluate(update)
self.GetValue(updateResult) // Side-effect trigger
}
}
return forValue
}
func (self *_runtime) evaluateForIn(node *_forInNode) Value {
source := self.evaluate(node.Source)
sourceValue := self.GetValue(source)
switch sourceValue._valueType {
case valueUndefined, valueNull:
return emptyValue()
}
sourceObject := self.toObject(sourceValue)
into := node.Into
body := node.body
labelSet := node.labelSet
forInValue := Value{}
object := sourceObject
for object != nil {
enumerateValue := Value{}
object.enumerate(false, func(name string) bool {
into := self.evaluate(into)
// In the case of: for (var abc in def) ...
if into.reference() == nil {
identifier := toString(into)
// TODO Should be true or false (strictness) depending on context
into = toValue(getIdentifierReference(self.LexicalEnvironment(), identifier, false, node))
}
self.PutValue(into.reference(), toValue_string(name))
for _, node := range body {
value := self.evaluate(node)
switch value.evaluateBreakContinue(labelSet) {
case resultReturn:
enumerateValue = value
return false
case resultBreak:
object = nil
return false
case resultContinue:
return true
default: // resultNormal
}
if !value.isEmpty() {
enumerateValue = value
}
}
return true
})
if object == nil {
break
}
object = object.prototype
if !enumerateValue.isEmpty() {
forInValue = enumerateValue
}
}
return forInValue
}
func (self *_runtime) evaluateSwitch(node *_switchNode) Value {
discriminantResult := self.evaluate(node.Discriminant)
target := node.Default
for index, clause := range node.CaseList {
test := clause.Test
if test != nil {
if self.calculateComparison("===", discriminantResult, self.evaluate(test)) {
target = index
break
}
}
}
switchValue := Value{}
if target != -1 {
labelSet := node.labelSet
for _, clause := range node.CaseList[target:] {
value := self.evaluateBody(clause.Body)
switch value.evaluateBreak(labelSet) {
case resultReturn:
return value
case resultBreak:
return Value{}
}
if !value.isEmpty() {
switchValue = value
}
}
}
return switchValue
}