2015-02-16 13:28:33 +00:00
|
|
|
package otto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/robertkrimen/otto/token"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (self *_runtime) cmpl_evaluate_nodeExpression(node _nodeExpression) Value {
|
|
|
|
// Allow interpreter interruption
|
|
|
|
// If the Interrupt channel is nil, then
|
|
|
|
// we avoid runtime.Gosched() overhead (if any)
|
|
|
|
// FIXME: Test this
|
2015-03-20 12:22:01 +00:00
|
|
|
if self.otto.Interrupt != nil {
|
2015-02-16 13:28:33 +00:00
|
|
|
runtime.Gosched()
|
|
|
|
select {
|
2015-03-20 12:22:01 +00:00
|
|
|
case value := <-self.otto.Interrupt:
|
2015-02-16 13:28:33 +00:00
|
|
|
value()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch node := node.(type) {
|
|
|
|
|
|
|
|
case *_nodeArrayLiteral:
|
|
|
|
return self.cmpl_evaluate_nodeArrayLiteral(node)
|
|
|
|
|
|
|
|
case *_nodeAssignExpression:
|
|
|
|
return self.cmpl_evaluate_nodeAssignExpression(node)
|
|
|
|
|
|
|
|
case *_nodeBinaryExpression:
|
|
|
|
if node.comparison {
|
|
|
|
return self.cmpl_evaluate_nodeBinaryExpression_comparison(node)
|
|
|
|
} else {
|
|
|
|
return self.cmpl_evaluate_nodeBinaryExpression(node)
|
|
|
|
}
|
|
|
|
|
|
|
|
case *_nodeBracketExpression:
|
|
|
|
return self.cmpl_evaluate_nodeBracketExpression(node)
|
|
|
|
|
|
|
|
case *_nodeCallExpression:
|
|
|
|
return self.cmpl_evaluate_nodeCallExpression(node, nil)
|
|
|
|
|
|
|
|
case *_nodeConditionalExpression:
|
|
|
|
return self.cmpl_evaluate_nodeConditionalExpression(node)
|
|
|
|
|
|
|
|
case *_nodeDotExpression:
|
|
|
|
return self.cmpl_evaluate_nodeDotExpression(node)
|
|
|
|
|
|
|
|
case *_nodeFunctionLiteral:
|
2015-03-20 12:22:01 +00:00
|
|
|
var local = self.scope.lexical
|
2015-02-16 13:28:33 +00:00
|
|
|
if node.name != "" {
|
2015-03-20 12:22:01 +00:00
|
|
|
local = self.newDeclarationStash(local)
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
value := toValue_object(self.newNodeFunction(node, local))
|
|
|
|
if node.name != "" {
|
2015-03-20 12:22:01 +00:00
|
|
|
local.createBinding(node.name, false, value)
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
return value
|
|
|
|
|
|
|
|
case *_nodeIdentifier:
|
|
|
|
name := node.name
|
|
|
|
// TODO Should be true or false (strictness) depending on context
|
|
|
|
// getIdentifierReference should not return nil, but we check anyway and panic
|
|
|
|
// so as not to propagate the nil into something else
|
2015-03-20 12:22:01 +00:00
|
|
|
reference := getIdentifierReference(self, self.scope.lexical, name, false, _at(node.idx))
|
2015-02-16 13:28:33 +00:00
|
|
|
if reference == nil {
|
|
|
|
// Should never get here!
|
|
|
|
panic(hereBeDragons("referenceError == nil: " + name))
|
|
|
|
}
|
|
|
|
return toValue(reference)
|
|
|
|
|
|
|
|
case *_nodeLiteral:
|
|
|
|
return node.value
|
|
|
|
|
|
|
|
case *_nodeNewExpression:
|
|
|
|
return self.cmpl_evaluate_nodeNewExpression(node)
|
|
|
|
|
|
|
|
case *_nodeObjectLiteral:
|
|
|
|
return self.cmpl_evaluate_nodeObjectLiteral(node)
|
|
|
|
|
|
|
|
case *_nodeRegExpLiteral:
|
|
|
|
return toValue_object(self._newRegExp(node.pattern, node.flags))
|
|
|
|
|
|
|
|
case *_nodeSequenceExpression:
|
|
|
|
return self.cmpl_evaluate_nodeSequenceExpression(node)
|
|
|
|
|
|
|
|
case *_nodeThisExpression:
|
2015-03-20 12:22:01 +00:00
|
|
|
return toValue_object(self.scope.this)
|
2015-02-16 13:28:33 +00:00
|
|
|
|
|
|
|
case *_nodeUnaryExpression:
|
|
|
|
return self.cmpl_evaluate_nodeUnaryExpression(node)
|
|
|
|
|
|
|
|
case *_nodeVariableExpression:
|
|
|
|
return self.cmpl_evaluate_nodeVariableExpression(node)
|
|
|
|
}
|
|
|
|
|
|
|
|
panic(fmt.Errorf("Here be dragons: evaluate_nodeExpression(%T)", node))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *_runtime) cmpl_evaluate_nodeArrayLiteral(node *_nodeArrayLiteral) Value {
|
|
|
|
|
|
|
|
valueArray := []Value{}
|
|
|
|
|
|
|
|
for _, node := range node.value {
|
|
|
|
if node == nil {
|
2015-03-20 12:22:01 +00:00
|
|
|
valueArray = append(valueArray, emptyValue)
|
2015-02-16 13:28:33 +00:00
|
|
|
} else {
|
2015-03-20 12:22:01 +00:00
|
|
|
valueArray = append(valueArray, self.cmpl_evaluate_nodeExpression(node).resolve())
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result := self.newArrayOf(valueArray)
|
|
|
|
|
|
|
|
return toValue_object(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *_runtime) cmpl_evaluate_nodeAssignExpression(node *_nodeAssignExpression) Value {
|
|
|
|
|
|
|
|
left := self.cmpl_evaluate_nodeExpression(node.left)
|
|
|
|
right := self.cmpl_evaluate_nodeExpression(node.right)
|
2015-03-20 12:22:01 +00:00
|
|
|
rightValue := right.resolve()
|
2015-02-16 13:28:33 +00:00
|
|
|
|
|
|
|
result := rightValue
|
|
|
|
if node.operator != token.ASSIGN {
|
|
|
|
result = self.calculateBinaryExpression(node.operator, left, rightValue)
|
|
|
|
}
|
|
|
|
|
2015-03-20 12:22:01 +00:00
|
|
|
self.putValue(left.reference(), result)
|
2015-02-16 13:28:33 +00:00
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *_runtime) cmpl_evaluate_nodeBinaryExpression(node *_nodeBinaryExpression) Value {
|
|
|
|
|
|
|
|
left := self.cmpl_evaluate_nodeExpression(node.left)
|
2015-03-20 12:22:01 +00:00
|
|
|
leftValue := left.resolve()
|
2015-02-16 13:28:33 +00:00
|
|
|
|
|
|
|
switch node.operator {
|
|
|
|
// Logical
|
|
|
|
case token.LOGICAL_AND:
|
2015-03-20 12:22:01 +00:00
|
|
|
if !leftValue.bool() {
|
2015-02-16 13:28:33 +00:00
|
|
|
return leftValue
|
|
|
|
}
|
|
|
|
right := self.cmpl_evaluate_nodeExpression(node.right)
|
2015-03-20 12:22:01 +00:00
|
|
|
return right.resolve()
|
2015-02-16 13:28:33 +00:00
|
|
|
case token.LOGICAL_OR:
|
2015-03-20 12:22:01 +00:00
|
|
|
if leftValue.bool() {
|
2015-02-16 13:28:33 +00:00
|
|
|
return leftValue
|
|
|
|
}
|
|
|
|
right := self.cmpl_evaluate_nodeExpression(node.right)
|
2015-03-20 12:22:01 +00:00
|
|
|
return right.resolve()
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self.calculateBinaryExpression(node.operator, leftValue, self.cmpl_evaluate_nodeExpression(node.right))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *_runtime) cmpl_evaluate_nodeBinaryExpression_comparison(node *_nodeBinaryExpression) Value {
|
|
|
|
|
2015-03-20 12:22:01 +00:00
|
|
|
left := self.cmpl_evaluate_nodeExpression(node.left).resolve()
|
|
|
|
right := self.cmpl_evaluate_nodeExpression(node.right).resolve()
|
2015-02-16 13:28:33 +00:00
|
|
|
|
|
|
|
return toValue_bool(self.calculateComparison(node.operator, left, right))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *_runtime) cmpl_evaluate_nodeBracketExpression(node *_nodeBracketExpression) Value {
|
|
|
|
target := self.cmpl_evaluate_nodeExpression(node.left)
|
2015-03-20 12:22:01 +00:00
|
|
|
targetValue := target.resolve()
|
2015-02-16 13:28:33 +00:00
|
|
|
member := self.cmpl_evaluate_nodeExpression(node.member)
|
2015-03-20 12:22:01 +00:00
|
|
|
memberValue := member.resolve()
|
2015-02-16 13:28:33 +00:00
|
|
|
|
|
|
|
// TODO Pass in base value as-is, and defer toObject till later?
|
2016-02-11 14:16:52 +00:00
|
|
|
object, err := self.objectCoerce(targetValue)
|
|
|
|
if err != nil {
|
|
|
|
panic(self.panicTypeError("Cannot access member '%s' of %s", memberValue.string(), err.Error(), _at(node.idx)))
|
|
|
|
}
|
|
|
|
return toValue(newPropertyReference(self, object, memberValue.string(), false, _at(node.idx)))
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *_runtime) cmpl_evaluate_nodeCallExpression(node *_nodeCallExpression, withArgumentList []interface{}) Value {
|
2015-03-20 12:22:01 +00:00
|
|
|
rt := self
|
|
|
|
this := Value{}
|
2015-02-16 13:28:33 +00:00
|
|
|
callee := self.cmpl_evaluate_nodeExpression(node.callee)
|
2015-03-20 12:22:01 +00:00
|
|
|
|
2015-02-16 13:28:33 +00:00
|
|
|
argumentList := []Value{}
|
|
|
|
if withArgumentList != nil {
|
|
|
|
argumentList = self.toValueArray(withArgumentList...)
|
|
|
|
} else {
|
|
|
|
for _, argumentNode := range node.argumentList {
|
2015-03-20 12:22:01 +00:00
|
|
|
argumentList = append(argumentList, self.cmpl_evaluate_nodeExpression(argumentNode).resolve())
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-20 12:22:01 +00:00
|
|
|
|
|
|
|
rf := callee.reference()
|
|
|
|
vl := callee.resolve()
|
|
|
|
|
|
|
|
eval := false // Whether this call is a (candidate for) direct call to eval
|
|
|
|
name := ""
|
|
|
|
if rf != nil {
|
|
|
|
switch rf := rf.(type) {
|
|
|
|
case *_propertyReference:
|
|
|
|
name = rf.name
|
|
|
|
object := rf.base
|
|
|
|
this = toValue_object(object)
|
|
|
|
eval = rf.name == "eval" // Possible direct eval
|
|
|
|
case *_stashReference:
|
|
|
|
// TODO ImplicitThisValue
|
|
|
|
name = rf.name
|
|
|
|
eval = rf.name == "eval" // Possible direct eval
|
|
|
|
default:
|
|
|
|
// FIXME?
|
|
|
|
panic(rt.panicTypeError("Here be dragons"))
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-20 12:22:01 +00:00
|
|
|
|
|
|
|
at := _at(-1)
|
|
|
|
switch callee := node.callee.(type) {
|
|
|
|
case *_nodeIdentifier:
|
|
|
|
at = _at(callee.idx)
|
|
|
|
case *_nodeDotExpression:
|
|
|
|
at = _at(callee.idx)
|
|
|
|
case *_nodeBracketExpression:
|
|
|
|
at = _at(callee.idx)
|
|
|
|
}
|
|
|
|
|
|
|
|
frame := _frame{
|
|
|
|
callee: name,
|
|
|
|
file: self.scope.frame.file,
|
|
|
|
}
|
|
|
|
|
|
|
|
if !vl.IsFunction() {
|
|
|
|
if name == "" {
|
|
|
|
// FIXME Maybe typeof?
|
|
|
|
panic(rt.panicTypeError("%v is not a function", vl, at))
|
|
|
|
}
|
|
|
|
panic(rt.panicTypeError("'%s' is not a function", name, at))
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
2015-03-20 12:22:01 +00:00
|
|
|
|
|
|
|
self.scope.frame.offset = int(at)
|
|
|
|
|
|
|
|
return vl._object().call(this, argumentList, eval, frame)
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *_runtime) cmpl_evaluate_nodeConditionalExpression(node *_nodeConditionalExpression) Value {
|
|
|
|
test := self.cmpl_evaluate_nodeExpression(node.test)
|
2015-03-20 12:22:01 +00:00
|
|
|
testValue := test.resolve()
|
|
|
|
if testValue.bool() {
|
2015-02-16 13:28:33 +00:00
|
|
|
return self.cmpl_evaluate_nodeExpression(node.consequent)
|
|
|
|
}
|
|
|
|
return self.cmpl_evaluate_nodeExpression(node.alternate)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *_runtime) cmpl_evaluate_nodeDotExpression(node *_nodeDotExpression) Value {
|
|
|
|
target := self.cmpl_evaluate_nodeExpression(node.left)
|
2015-03-20 12:22:01 +00:00
|
|
|
targetValue := target.resolve()
|
2015-02-16 13:28:33 +00:00
|
|
|
// TODO Pass in base value as-is, and defer toObject till later?
|
|
|
|
object, err := self.objectCoerce(targetValue)
|
|
|
|
if err != nil {
|
2016-02-11 14:16:52 +00:00
|
|
|
panic(self.panicTypeError("Cannot access member '%s' of %s", node.identifier, err.Error(), _at(node.idx)))
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
2015-03-20 12:22:01 +00:00
|
|
|
return toValue(newPropertyReference(self, object, node.identifier, false, _at(node.idx)))
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *_runtime) cmpl_evaluate_nodeNewExpression(node *_nodeNewExpression) Value {
|
2015-03-20 12:22:01 +00:00
|
|
|
rt := self
|
2015-02-16 13:28:33 +00:00
|
|
|
callee := self.cmpl_evaluate_nodeExpression(node.callee)
|
2015-03-20 12:22:01 +00:00
|
|
|
|
2015-02-16 13:28:33 +00:00
|
|
|
argumentList := []Value{}
|
|
|
|
for _, argumentNode := range node.argumentList {
|
2015-03-20 12:22:01 +00:00
|
|
|
argumentList = append(argumentList, self.cmpl_evaluate_nodeExpression(argumentNode).resolve())
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
2015-03-20 12:22:01 +00:00
|
|
|
|
|
|
|
rf := callee.reference()
|
|
|
|
vl := callee.resolve()
|
|
|
|
|
|
|
|
name := ""
|
|
|
|
if rf != nil {
|
|
|
|
switch rf := rf.(type) {
|
|
|
|
case *_propertyReference:
|
|
|
|
name = rf.name
|
|
|
|
case *_stashReference:
|
|
|
|
name = rf.name
|
|
|
|
default:
|
|
|
|
panic(rt.panicTypeError("Here be dragons"))
|
|
|
|
}
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
2015-03-20 12:22:01 +00:00
|
|
|
|
|
|
|
at := _at(-1)
|
|
|
|
switch callee := node.callee.(type) {
|
|
|
|
case *_nodeIdentifier:
|
|
|
|
at = _at(callee.idx)
|
|
|
|
case *_nodeDotExpression:
|
|
|
|
at = _at(callee.idx)
|
|
|
|
case *_nodeBracketExpression:
|
|
|
|
at = _at(callee.idx)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !vl.IsFunction() {
|
|
|
|
if name == "" {
|
|
|
|
// FIXME Maybe typeof?
|
|
|
|
panic(rt.panicTypeError("%v is not a function", vl, at))
|
|
|
|
}
|
|
|
|
panic(rt.panicTypeError("'%s' is not a function", name, at))
|
|
|
|
}
|
|
|
|
|
|
|
|
self.scope.frame.offset = int(at)
|
|
|
|
|
|
|
|
return vl._object().construct(argumentList)
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *_runtime) cmpl_evaluate_nodeObjectLiteral(node *_nodeObjectLiteral) Value {
|
|
|
|
|
|
|
|
result := self.newObject()
|
|
|
|
|
|
|
|
for _, property := range node.value {
|
|
|
|
switch property.kind {
|
|
|
|
case "value":
|
2015-03-20 12:22:01 +00:00
|
|
|
result.defineProperty(property.key, self.cmpl_evaluate_nodeExpression(property.value).resolve(), 0111, false)
|
2015-02-16 13:28:33 +00:00
|
|
|
case "get":
|
2015-03-20 12:22:01 +00:00
|
|
|
getter := self.newNodeFunction(property.value.(*_nodeFunctionLiteral), self.scope.lexical)
|
2015-02-16 13:28:33 +00:00
|
|
|
descriptor := _property{}
|
|
|
|
descriptor.mode = 0211
|
|
|
|
descriptor.value = _propertyGetSet{getter, nil}
|
|
|
|
result.defineOwnProperty(property.key, descriptor, false)
|
|
|
|
case "set":
|
2015-03-20 12:22:01 +00:00
|
|
|
setter := self.newNodeFunction(property.value.(*_nodeFunctionLiteral), self.scope.lexical)
|
2015-02-16 13:28:33 +00:00
|
|
|
descriptor := _property{}
|
|
|
|
descriptor.mode = 0211
|
|
|
|
descriptor.value = _propertyGetSet{nil, setter}
|
|
|
|
result.defineOwnProperty(property.key, descriptor, false)
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("Here be dragons: evaluate_nodeObjectLiteral: invalid property.Kind: %v", property.kind))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return toValue_object(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *_runtime) cmpl_evaluate_nodeSequenceExpression(node *_nodeSequenceExpression) Value {
|
|
|
|
var result Value
|
|
|
|
for _, node := range node.sequence {
|
|
|
|
result = self.cmpl_evaluate_nodeExpression(node)
|
2015-03-20 12:22:01 +00:00
|
|
|
result = result.resolve()
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *_runtime) cmpl_evaluate_nodeUnaryExpression(node *_nodeUnaryExpression) Value {
|
|
|
|
|
|
|
|
target := self.cmpl_evaluate_nodeExpression(node.operand)
|
|
|
|
switch node.operator {
|
|
|
|
case token.TYPEOF, token.DELETE:
|
2015-03-20 12:22:01 +00:00
|
|
|
if target.kind == valueReference && target.reference().invalid() {
|
2015-02-16 13:28:33 +00:00
|
|
|
if node.operator == token.TYPEOF {
|
|
|
|
return toValue_string("undefined")
|
|
|
|
}
|
2015-03-20 12:22:01 +00:00
|
|
|
return trueValue
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch node.operator {
|
|
|
|
case token.NOT:
|
2015-03-20 12:22:01 +00:00
|
|
|
targetValue := target.resolve()
|
|
|
|
if targetValue.bool() {
|
|
|
|
return falseValue
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
2015-03-20 12:22:01 +00:00
|
|
|
return trueValue
|
2015-02-16 13:28:33 +00:00
|
|
|
case token.BITWISE_NOT:
|
2015-03-20 12:22:01 +00:00
|
|
|
targetValue := target.resolve()
|
2015-02-16 13:28:33 +00:00
|
|
|
integerValue := toInt32(targetValue)
|
|
|
|
return toValue_int32(^integerValue)
|
|
|
|
case token.PLUS:
|
2015-03-20 12:22:01 +00:00
|
|
|
targetValue := target.resolve()
|
|
|
|
return toValue_float64(targetValue.float64())
|
2015-02-16 13:28:33 +00:00
|
|
|
case token.MINUS:
|
2015-03-20 12:22:01 +00:00
|
|
|
targetValue := target.resolve()
|
|
|
|
value := targetValue.float64()
|
2015-02-16 13:28:33 +00:00
|
|
|
// TODO Test this
|
|
|
|
sign := float64(-1)
|
|
|
|
if math.Signbit(value) {
|
|
|
|
sign = 1
|
|
|
|
}
|
|
|
|
return toValue_float64(math.Copysign(value, sign))
|
|
|
|
case token.INCREMENT:
|
2015-03-20 12:22:01 +00:00
|
|
|
targetValue := target.resolve()
|
2015-02-16 13:28:33 +00:00
|
|
|
if node.postfix {
|
|
|
|
// Postfix++
|
2015-03-20 12:22:01 +00:00
|
|
|
oldValue := targetValue.float64()
|
2015-02-16 13:28:33 +00:00
|
|
|
newValue := toValue_float64(+1 + oldValue)
|
2015-03-20 12:22:01 +00:00
|
|
|
self.putValue(target.reference(), newValue)
|
2015-02-16 13:28:33 +00:00
|
|
|
return toValue_float64(oldValue)
|
|
|
|
} else {
|
|
|
|
// ++Prefix
|
2015-03-20 12:22:01 +00:00
|
|
|
newValue := toValue_float64(+1 + targetValue.float64())
|
|
|
|
self.putValue(target.reference(), newValue)
|
2015-02-16 13:28:33 +00:00
|
|
|
return newValue
|
|
|
|
}
|
|
|
|
case token.DECREMENT:
|
2015-03-20 12:22:01 +00:00
|
|
|
targetValue := target.resolve()
|
2015-02-16 13:28:33 +00:00
|
|
|
if node.postfix {
|
|
|
|
// Postfix--
|
2015-03-20 12:22:01 +00:00
|
|
|
oldValue := targetValue.float64()
|
2015-02-16 13:28:33 +00:00
|
|
|
newValue := toValue_float64(-1 + oldValue)
|
2015-03-20 12:22:01 +00:00
|
|
|
self.putValue(target.reference(), newValue)
|
2015-02-16 13:28:33 +00:00
|
|
|
return toValue_float64(oldValue)
|
|
|
|
} else {
|
|
|
|
// --Prefix
|
2015-03-20 12:22:01 +00:00
|
|
|
newValue := toValue_float64(-1 + targetValue.float64())
|
|
|
|
self.putValue(target.reference(), newValue)
|
2015-02-16 13:28:33 +00:00
|
|
|
return newValue
|
|
|
|
}
|
|
|
|
case token.VOID:
|
2015-03-20 12:22:01 +00:00
|
|
|
target.resolve() // FIXME Side effect?
|
|
|
|
return Value{}
|
2015-02-16 13:28:33 +00:00
|
|
|
case token.DELETE:
|
|
|
|
reference := target.reference()
|
|
|
|
if reference == nil {
|
2015-03-20 12:22:01 +00:00
|
|
|
return trueValue
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
2015-03-20 12:22:01 +00:00
|
|
|
return toValue_bool(target.reference().delete())
|
2015-02-16 13:28:33 +00:00
|
|
|
case token.TYPEOF:
|
2015-03-20 12:22:01 +00:00
|
|
|
targetValue := target.resolve()
|
|
|
|
switch targetValue.kind {
|
2015-02-16 13:28:33 +00:00
|
|
|
case valueUndefined:
|
|
|
|
return toValue_string("undefined")
|
|
|
|
case valueNull:
|
|
|
|
return toValue_string("object")
|
|
|
|
case valueBoolean:
|
|
|
|
return toValue_string("boolean")
|
|
|
|
case valueNumber:
|
|
|
|
return toValue_string("number")
|
|
|
|
case valueString:
|
|
|
|
return toValue_string("string")
|
|
|
|
case valueObject:
|
2015-03-20 12:22:01 +00:00
|
|
|
if targetValue._object().isCall() {
|
2015-02-16 13:28:33 +00:00
|
|
|
return toValue_string("function")
|
|
|
|
}
|
|
|
|
return toValue_string("object")
|
|
|
|
default:
|
|
|
|
// FIXME ?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
panic(hereBeDragons())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *_runtime) cmpl_evaluate_nodeVariableExpression(node *_nodeVariableExpression) Value {
|
|
|
|
if node.initializer != nil {
|
|
|
|
// FIXME If reference is nil
|
2015-03-20 12:22:01 +00:00
|
|
|
left := getIdentifierReference(self, self.scope.lexical, node.name, false, _at(node.idx))
|
2015-02-16 13:28:33 +00:00
|
|
|
right := self.cmpl_evaluate_nodeExpression(node.initializer)
|
2015-03-20 12:22:01 +00:00
|
|
|
rightValue := right.resolve()
|
2015-02-16 13:28:33 +00:00
|
|
|
|
2015-03-20 12:22:01 +00:00
|
|
|
self.putValue(left, rightValue)
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
return toValue_string(node.name)
|
|
|
|
}
|