File: cmpl_evaluate_statement.go

package info (click to toggle)
golang-github-robertkrimen-otto 0.0~git20200922.ef014fd-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,796 kB
  • sloc: perl: 1,227; makefile: 79
file content (424 lines) | stat: -rw-r--r-- 10,397 bytes parent folder | download | duplicates (2)
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package otto

import (
	"fmt"
	"runtime"

	"github.com/robertkrimen/otto/token"
)

func (self *_runtime) cmpl_evaluate_nodeStatement(node _nodeStatement) Value {
	// Allow interpreter interruption
	// If the Interrupt channel is nil, then
	// we avoid runtime.Gosched() overhead (if any)
	// FIXME: Test this
	if self.otto.Interrupt != nil {
		runtime.Gosched()
		select {
		case value := <-self.otto.Interrupt:
			value()
		default:
		}
	}

	switch node := node.(type) {

	case *_nodeBlockStatement:
		labels := self.labels
		self.labels = nil

		value := self.cmpl_evaluate_nodeStatementList(node.list)
		switch value.kind {
		case valueResult:
			switch value.evaluateBreak(labels) {
			case resultBreak:
				return emptyValue
			}
		}
		return value

	case *_nodeBranchStatement:
		target := node.label
		switch node.branch { // FIXME Maybe node.kind? node.operator?
		case token.BREAK:
			return toValue(newBreakResult(target))
		case token.CONTINUE:
			return toValue(newContinueResult(target))
		}

	case *_nodeDebuggerStatement:
		if self.debugger != nil {
			self.debugger(self.otto)
		}
		return emptyValue // Nothing happens.

	case *_nodeDoWhileStatement:
		return self.cmpl_evaluate_nodeDoWhileStatement(node)

	case *_nodeEmptyStatement:
		return emptyValue

	case *_nodeExpressionStatement:
		return self.cmpl_evaluate_nodeExpression(node.expression)

	case *_nodeForInStatement:
		return self.cmpl_evaluate_nodeForInStatement(node)

	case *_nodeForStatement:
		return self.cmpl_evaluate_nodeForStatement(node)

	case *_nodeIfStatement:
		return self.cmpl_evaluate_nodeIfStatement(node)

	case *_nodeLabelledStatement:
		self.labels = append(self.labels, node.label)
		defer func() {
			if len(self.labels) > 0 {
				self.labels = self.labels[:len(self.labels)-1] // Pop the label
			} else {
				self.labels = nil
			}
		}()
		return self.cmpl_evaluate_nodeStatement(node.statement)

	case *_nodeReturnStatement:
		if node.argument != nil {
			return toValue(newReturnResult(self.cmpl_evaluate_nodeExpression(node.argument).resolve()))
		}
		return toValue(newReturnResult(Value{}))

	case *_nodeSwitchStatement:
		return self.cmpl_evaluate_nodeSwitchStatement(node)

	case *_nodeThrowStatement:
		value := self.cmpl_evaluate_nodeExpression(node.argument).resolve()
		panic(newException(value))

	case *_nodeTryStatement:
		return self.cmpl_evaluate_nodeTryStatement(node)

	case *_nodeVariableStatement:
		// Variables are already defined, this is initialization only
		for _, variable := range node.list {
			self.cmpl_evaluate_nodeVariableExpression(variable.(*_nodeVariableExpression))
		}
		return emptyValue

	case *_nodeWhileStatement:
		return self.cmpl_evaluate_nodeWhileStatement(node)

	case *_nodeWithStatement:
		return self.cmpl_evaluate_nodeWithStatement(node)

	}

	panic(fmt.Errorf("Here be dragons: evaluate_nodeStatement(%T)", node))
}

func (self *_runtime) cmpl_evaluate_nodeStatementList(list []_nodeStatement) Value {
	var result Value
	for _, node := range list {
		value := self.cmpl_evaluate_nodeStatement(node)
		switch value.kind {
		case valueResult:
			return value
		case valueEmpty:
		default:
			// We have getValue here to (for example) trigger a
			// ReferenceError (of the not defined variety)
			// Not sure if this is the best way to error out early
			// for such errors or if there is a better way
			// TODO Do we still need this?
			result = value.resolve()
		}
	}
	return result
}

func (self *_runtime) cmpl_evaluate_nodeDoWhileStatement(node *_nodeDoWhileStatement) Value {

	labels := append(self.labels, "")
	self.labels = nil

	test := node.test

	result := emptyValue
resultBreak:
	for {
		for _, node := range node.body {
			value := self.cmpl_evaluate_nodeStatement(node)
			switch value.kind {
			case valueResult:
				switch value.evaluateBreakContinue(labels) {
				case resultReturn:
					return value
				case resultBreak:
					break resultBreak
				case resultContinue:
					goto resultContinue
				}
			case valueEmpty:
			default:
				result = value
			}
		}
	resultContinue:
		if !self.cmpl_evaluate_nodeExpression(test).resolve().bool() {
			// Stahp: do ... while (false)
			break
		}
	}
	return result
}

func (self *_runtime) cmpl_evaluate_nodeForInStatement(node *_nodeForInStatement) Value {

	labels := append(self.labels, "")
	self.labels = nil

	source := self.cmpl_evaluate_nodeExpression(node.source)
	sourceValue := source.resolve()

	switch sourceValue.kind {
	case valueUndefined, valueNull:
		return emptyValue
	}

	sourceObject := self.toObject(sourceValue)

	into := node.into
	body := node.body

	result := emptyValue
	object := sourceObject
	for object != nil {
		enumerateValue := emptyValue
		object.enumerate(false, func(name string) bool {
			into := self.cmpl_evaluate_nodeExpression(into)
			// In the case of: for (var abc in def) ...
			if into.reference() == nil {
				identifier := into.string()
				// TODO Should be true or false (strictness) depending on context
				into = toValue(getIdentifierReference(self, self.scope.lexical, identifier, false, -1))
			}
			self.putValue(into.reference(), toValue_string(name))
			for _, node := range body {
				value := self.cmpl_evaluate_nodeStatement(node)
				switch value.kind {
				case valueResult:
					switch value.evaluateBreakContinue(labels) {
					case resultReturn:
						enumerateValue = value
						return false
					case resultBreak:
						object = nil
						return false
					case resultContinue:
						return true
					}
				case valueEmpty:
				default:
					enumerateValue = value
				}
			}
			return true
		})
		if object == nil {
			break
		}
		object = object.prototype
		if !enumerateValue.isEmpty() {
			result = enumerateValue
		}
	}
	return result
}

func (self *_runtime) cmpl_evaluate_nodeForStatement(node *_nodeForStatement) Value {

	labels := append(self.labels, "")
	self.labels = nil

	initializer := node.initializer
	test := node.test
	update := node.update
	body := node.body

	if initializer != nil {
		initialResult := self.cmpl_evaluate_nodeExpression(initializer)
		initialResult.resolve() // Side-effect trigger
	}

	result := emptyValue
resultBreak:
	for {
		if test != nil {
			testResult := self.cmpl_evaluate_nodeExpression(test)
			testResultValue := testResult.resolve()
			if testResultValue.bool() == false {
				break
			}
		}
		for _, node := range body {
			value := self.cmpl_evaluate_nodeStatement(node)
			switch value.kind {
			case valueResult:
				switch value.evaluateBreakContinue(labels) {
				case resultReturn:
					return value
				case resultBreak:
					break resultBreak
				case resultContinue:
					goto resultContinue
				}
			case valueEmpty:
			default:
				result = value
			}
		}
	resultContinue:
		if update != nil {
			updateResult := self.cmpl_evaluate_nodeExpression(update)
			updateResult.resolve() // Side-effect trigger
		}
	}
	return result
}

func (self *_runtime) cmpl_evaluate_nodeIfStatement(node *_nodeIfStatement) Value {
	test := self.cmpl_evaluate_nodeExpression(node.test)
	testValue := test.resolve()
	if testValue.bool() {
		return self.cmpl_evaluate_nodeStatement(node.consequent)
	} else if node.alternate != nil {
		return self.cmpl_evaluate_nodeStatement(node.alternate)
	}

	return emptyValue
}

func (self *_runtime) cmpl_evaluate_nodeSwitchStatement(node *_nodeSwitchStatement) Value {

	labels := append(self.labels, "")
	self.labels = nil

	discriminantResult := self.cmpl_evaluate_nodeExpression(node.discriminant)
	target := node.default_

	for index, clause := range node.body {
		test := clause.test
		if test != nil {
			if self.calculateComparison(token.STRICT_EQUAL, discriminantResult, self.cmpl_evaluate_nodeExpression(test)) {
				target = index
				break
			}
		}
	}

	result := emptyValue
	if target != -1 {
		for _, clause := range node.body[target:] {
			for _, statement := range clause.consequent {
				value := self.cmpl_evaluate_nodeStatement(statement)
				switch value.kind {
				case valueResult:
					switch value.evaluateBreak(labels) {
					case resultReturn:
						return value
					case resultBreak:
						return emptyValue
					}
				case valueEmpty:
				default:
					result = value
				}
			}
		}
	}

	return result
}

func (self *_runtime) cmpl_evaluate_nodeTryStatement(node *_nodeTryStatement) Value {
	tryCatchValue, exception := self.tryCatchEvaluate(func() Value {
		return self.cmpl_evaluate_nodeStatement(node.body)
	})

	if exception && node.catch != nil {
		outer := self.scope.lexical
		self.scope.lexical = self.newDeclarationStash(outer)
		defer func() {
			self.scope.lexical = outer
		}()
		// TODO If necessary, convert TypeError<runtime> => TypeError
		// That, is, such errors can be thrown despite not being JavaScript "native"
		// strict = false
		self.scope.lexical.setValue(node.catch.parameter, tryCatchValue, false)

		// FIXME node.CatchParameter
		// FIXME node.Catch
		tryCatchValue, exception = self.tryCatchEvaluate(func() Value {
			return self.cmpl_evaluate_nodeStatement(node.catch.body)
		})
	}

	if node.finally != nil {
		finallyValue := self.cmpl_evaluate_nodeStatement(node.finally)
		if finallyValue.kind == valueResult {
			return finallyValue
		}
	}

	if exception {
		panic(newException(tryCatchValue))
	}

	return tryCatchValue
}

func (self *_runtime) cmpl_evaluate_nodeWhileStatement(node *_nodeWhileStatement) Value {

	test := node.test
	body := node.body
	labels := append(self.labels, "")
	self.labels = nil

	result := emptyValue
resultBreakContinue:
	for {
		if !self.cmpl_evaluate_nodeExpression(test).resolve().bool() {
			// Stahp: while (false) ...
			break
		}
		for _, node := range body {
			value := self.cmpl_evaluate_nodeStatement(node)
			switch value.kind {
			case valueResult:
				switch value.evaluateBreakContinue(labels) {
				case resultReturn:
					return value
				case resultBreak:
					break resultBreakContinue
				case resultContinue:
					continue resultBreakContinue
				}
			case valueEmpty:
			default:
				result = value
			}
		}
	}
	return result
}

func (self *_runtime) cmpl_evaluate_nodeWithStatement(node *_nodeWithStatement) Value {
	object := self.cmpl_evaluate_nodeExpression(node.object)
	outer := self.scope.lexical
	lexical := self.newObjectStash(self.toObject(object.resolve()), outer)
	self.scope.lexical = lexical
	defer func() {
		self.scope.lexical = outer
	}()

	return self.cmpl_evaluate_nodeStatement(node.body)
}