mirror of https://github.com/ethereum/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
425 lines
10 KiB
425 lines
10 KiB
10 years ago
|
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
|
||
10 years ago
|
if self.otto.Interrupt != nil {
|
||
10 years ago
|
runtime.Gosched()
|
||
|
select {
|
||
10 years ago
|
case value := <-self.otto.Interrupt:
|
||
10 years ago
|
value()
|
||
|
default:
|
||
|
}
|
||
|
}
|
||
|
|
||
|
switch node := node.(type) {
|
||
|
|
||
|
case *_nodeBlockStatement:
|
||
10 years ago
|
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
|
||
10 years ago
|
|
||
|
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:
|
||
9 years ago
|
if self.debugger != nil {
|
||
|
self.debugger(self.otto)
|
||
|
}
|
||
10 years ago
|
return emptyValue // Nothing happens.
|
||
10 years ago
|
|
||
|
case *_nodeDoWhileStatement:
|
||
|
return self.cmpl_evaluate_nodeDoWhileStatement(node)
|
||
|
|
||
|
case *_nodeEmptyStatement:
|
||
10 years ago
|
return emptyValue
|
||
10 years ago
|
|
||
|
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 {
|
||
10 years ago
|
return toValue(newReturnResult(self.cmpl_evaluate_nodeExpression(node.argument).resolve()))
|
||
10 years ago
|
}
|
||
10 years ago
|
return toValue(newReturnResult(Value{}))
|
||
10 years ago
|
|
||
|
case *_nodeSwitchStatement:
|
||
|
return self.cmpl_evaluate_nodeSwitchStatement(node)
|
||
|
|
||
|
case *_nodeThrowStatement:
|
||
10 years ago
|
value := self.cmpl_evaluate_nodeExpression(node.argument).resolve()
|
||
10 years ago
|
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))
|
||
|
}
|
||
10 years ago
|
return emptyValue
|
||
10 years ago
|
|
||
|
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)
|
||
10 years ago
|
switch value.kind {
|
||
10 years ago
|
case valueResult:
|
||
|
return value
|
||
|
case valueEmpty:
|
||
|
default:
|
||
10 years ago
|
// We have getValue here to (for example) trigger a
|
||
10 years ago
|
// 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?
|
||
10 years ago
|
result = value.resolve()
|
||
10 years ago
|
}
|
||
|
}
|
||
|
return result
|
||
|
}
|
||
|
|
||
|
func (self *_runtime) cmpl_evaluate_nodeDoWhileStatement(node *_nodeDoWhileStatement) Value {
|
||
|
|
||
|
labels := append(self.labels, "")
|
||
|
self.labels = nil
|
||
|
|
||
|
test := node.test
|
||
|
|
||
10 years ago
|
result := emptyValue
|
||
10 years ago
|
resultBreak:
|
||
|
for {
|
||
|
for _, node := range node.body {
|
||
|
value := self.cmpl_evaluate_nodeStatement(node)
|
||
10 years ago
|
switch value.kind {
|
||
10 years ago
|
case valueResult:
|
||
|
switch value.evaluateBreakContinue(labels) {
|
||
|
case resultReturn:
|
||
|
return value
|
||
|
case resultBreak:
|
||
|
break resultBreak
|
||
|
case resultContinue:
|
||
|
goto resultContinue
|
||
|
}
|
||
|
case valueEmpty:
|
||
|
default:
|
||
|
result = value
|
||
|
}
|
||
|
}
|
||
|
resultContinue:
|
||
10 years ago
|
if !self.cmpl_evaluate_nodeExpression(test).resolve().bool() {
|
||
10 years ago
|
// 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)
|
||
10 years ago
|
sourceValue := source.resolve()
|
||
10 years ago
|
|
||
10 years ago
|
switch sourceValue.kind {
|
||
10 years ago
|
case valueUndefined, valueNull:
|
||
10 years ago
|
return emptyValue
|
||
10 years ago
|
}
|
||
|
|
||
|
sourceObject := self.toObject(sourceValue)
|
||
|
|
||
|
into := node.into
|
||
|
body := node.body
|
||
|
|
||
10 years ago
|
result := emptyValue
|
||
10 years ago
|
object := sourceObject
|
||
|
for object != nil {
|
||
10 years ago
|
enumerateValue := emptyValue
|
||
10 years ago
|
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 {
|
||
10 years ago
|
identifier := into.string()
|
||
10 years ago
|
// TODO Should be true or false (strictness) depending on context
|
||
10 years ago
|
into = toValue(getIdentifierReference(self, self.scope.lexical, identifier, false, -1))
|
||
10 years ago
|
}
|
||
10 years ago
|
self.putValue(into.reference(), toValue_string(name))
|
||
10 years ago
|
for _, node := range body {
|
||
|
value := self.cmpl_evaluate_nodeStatement(node)
|
||
10 years ago
|
switch value.kind {
|
||
10 years ago
|
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)
|
||
10 years ago
|
initialResult.resolve() // Side-effect trigger
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
result := emptyValue
|
||
10 years ago
|
resultBreak:
|
||
|
for {
|
||
|
if test != nil {
|
||
|
testResult := self.cmpl_evaluate_nodeExpression(test)
|
||
10 years ago
|
testResultValue := testResult.resolve()
|
||
|
if testResultValue.bool() == false {
|
||
10 years ago
|
break
|
||
|
}
|
||
|
}
|
||
|
for _, node := range body {
|
||
|
value := self.cmpl_evaluate_nodeStatement(node)
|
||
10 years ago
|
switch value.kind {
|
||
10 years ago
|
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)
|
||
10 years ago
|
updateResult.resolve() // Side-effect trigger
|
||
10 years ago
|
}
|
||
|
}
|
||
|
return result
|
||
|
}
|
||
|
|
||
|
func (self *_runtime) cmpl_evaluate_nodeIfStatement(node *_nodeIfStatement) Value {
|
||
|
test := self.cmpl_evaluate_nodeExpression(node.test)
|
||
10 years ago
|
testValue := test.resolve()
|
||
|
if testValue.bool() {
|
||
10 years ago
|
return self.cmpl_evaluate_nodeStatement(node.consequent)
|
||
|
} else if node.alternate != nil {
|
||
|
return self.cmpl_evaluate_nodeStatement(node.alternate)
|
||
|
}
|
||
|
|
||
10 years ago
|
return emptyValue
|
||
10 years ago
|
}
|
||
|
|
||
|
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
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
result := emptyValue
|
||
10 years ago
|
if target != -1 {
|
||
|
for _, clause := range node.body[target:] {
|
||
|
for _, statement := range clause.consequent {
|
||
|
value := self.cmpl_evaluate_nodeStatement(statement)
|
||
10 years ago
|
switch value.kind {
|
||
10 years ago
|
case valueResult:
|
||
|
switch value.evaluateBreak(labels) {
|
||
|
case resultReturn:
|
||
|
return value
|
||
|
case resultBreak:
|
||
10 years ago
|
return emptyValue
|
||
10 years ago
|
}
|
||
|
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 {
|
||
10 years ago
|
outer := self.scope.lexical
|
||
|
self.scope.lexical = self.newDeclarationStash(outer)
|
||
10 years ago
|
defer func() {
|
||
10 years ago
|
self.scope.lexical = outer
|
||
10 years ago
|
}()
|
||
|
// TODO If necessary, convert TypeError<runtime> => TypeError
|
||
|
// That, is, such errors can be thrown despite not being JavaScript "native"
|
||
10 years ago
|
// strict = false
|
||
|
self.scope.lexical.setValue(node.catch.parameter, tryCatchValue, false)
|
||
10 years ago
|
|
||
|
// 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)
|
||
10 years ago
|
if finallyValue.kind == valueResult {
|
||
10 years ago
|
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
|
||
|
|
||
10 years ago
|
result := emptyValue
|
||
10 years ago
|
resultBreakContinue:
|
||
|
for {
|
||
10 years ago
|
if !self.cmpl_evaluate_nodeExpression(test).resolve().bool() {
|
||
10 years ago
|
// Stahp: while (false) ...
|
||
|
break
|
||
|
}
|
||
|
for _, node := range body {
|
||
|
value := self.cmpl_evaluate_nodeStatement(node)
|
||
10 years ago
|
switch value.kind {
|
||
10 years ago
|
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)
|
||
10 years ago
|
outer := self.scope.lexical
|
||
|
lexical := self.newObjectStash(self.toObject(object.resolve()), outer)
|
||
|
self.scope.lexical = lexical
|
||
10 years ago
|
defer func() {
|
||
10 years ago
|
self.scope.lexical = outer
|
||
10 years ago
|
}()
|
||
|
|
||
|
return self.cmpl_evaluate_nodeStatement(node.body)
|
||
|
}
|