2015-02-16 13:28:33 +00:00
|
|
|
package otto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/robertkrimen/otto/token"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (self *_runtime) evaluateMultiply(left float64, right float64) Value {
|
|
|
|
// TODO 11.5.1
|
2015-03-20 12:22:01 +00:00
|
|
|
return Value{}
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *_runtime) evaluateDivide(left float64, right float64) Value {
|
|
|
|
if math.IsNaN(left) || math.IsNaN(right) {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
|
|
|
if math.IsInf(left, 0) && math.IsInf(right, 0) {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
|
|
|
if left == 0 && right == 0 {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
|
|
|
if math.IsInf(left, 0) {
|
|
|
|
if math.Signbit(left) == math.Signbit(right) {
|
|
|
|
return positiveInfinityValue()
|
|
|
|
} else {
|
|
|
|
return negativeInfinityValue()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if math.IsInf(right, 0) {
|
|
|
|
if math.Signbit(left) == math.Signbit(right) {
|
|
|
|
return positiveZeroValue()
|
|
|
|
} else {
|
|
|
|
return negativeZeroValue()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if right == 0 {
|
|
|
|
if math.Signbit(left) == math.Signbit(right) {
|
|
|
|
return positiveInfinityValue()
|
|
|
|
} else {
|
|
|
|
return negativeInfinityValue()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return toValue_float64(left / right)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *_runtime) evaluateModulo(left float64, right float64) Value {
|
|
|
|
// TODO 11.5.3
|
2015-03-20 12:22:01 +00:00
|
|
|
return Value{}
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *_runtime) calculateBinaryExpression(operator token.Token, left Value, right Value) Value {
|
|
|
|
|
2015-03-20 12:22:01 +00:00
|
|
|
leftValue := left.resolve()
|
2015-02-16 13:28:33 +00:00
|
|
|
|
|
|
|
switch operator {
|
|
|
|
|
|
|
|
// Additive
|
|
|
|
case token.PLUS:
|
|
|
|
leftValue = toPrimitive(leftValue)
|
2015-03-20 12:22:01 +00:00
|
|
|
rightValue := right.resolve()
|
2015-02-16 13:28:33 +00:00
|
|
|
rightValue = toPrimitive(rightValue)
|
|
|
|
|
|
|
|
if leftValue.IsString() || rightValue.IsString() {
|
2015-03-20 12:22:01 +00:00
|
|
|
return toValue_string(strings.Join([]string{leftValue.string(), rightValue.string()}, ""))
|
2015-02-16 13:28:33 +00:00
|
|
|
} else {
|
2015-03-20 12:22:01 +00:00
|
|
|
return toValue_float64(leftValue.float64() + rightValue.float64())
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
case token.MINUS:
|
2015-03-20 12:22:01 +00:00
|
|
|
rightValue := right.resolve()
|
|
|
|
return toValue_float64(leftValue.float64() - rightValue.float64())
|
2015-02-16 13:28:33 +00:00
|
|
|
|
|
|
|
// Multiplicative
|
|
|
|
case token.MULTIPLY:
|
2015-03-20 12:22:01 +00:00
|
|
|
rightValue := right.resolve()
|
|
|
|
return toValue_float64(leftValue.float64() * rightValue.float64())
|
2015-02-16 13:28:33 +00:00
|
|
|
case token.SLASH:
|
2015-03-20 12:22:01 +00:00
|
|
|
rightValue := right.resolve()
|
|
|
|
return self.evaluateDivide(leftValue.float64(), rightValue.float64())
|
2015-02-16 13:28:33 +00:00
|
|
|
case token.REMAINDER:
|
2015-03-20 12:22:01 +00:00
|
|
|
rightValue := right.resolve()
|
|
|
|
return toValue_float64(math.Mod(leftValue.float64(), rightValue.float64()))
|
2015-02-16 13:28:33 +00:00
|
|
|
|
|
|
|
// Logical
|
|
|
|
case token.LOGICAL_AND:
|
2015-03-20 12:22:01 +00:00
|
|
|
left := leftValue.bool()
|
2015-02-16 13:28:33 +00:00
|
|
|
if !left {
|
2015-03-20 12:22:01 +00:00
|
|
|
return falseValue
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
2015-03-20 12:22:01 +00:00
|
|
|
return toValue_bool(right.resolve().bool())
|
2015-02-16 13:28:33 +00:00
|
|
|
case token.LOGICAL_OR:
|
2015-03-20 12:22:01 +00:00
|
|
|
left := leftValue.bool()
|
2015-02-16 13:28:33 +00:00
|
|
|
if left {
|
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(right.resolve().bool())
|
2015-02-16 13:28:33 +00:00
|
|
|
|
|
|
|
// Bitwise
|
|
|
|
case token.AND:
|
2015-03-20 12:22:01 +00:00
|
|
|
rightValue := right.resolve()
|
2015-02-16 13:28:33 +00:00
|
|
|
return toValue_int32(toInt32(leftValue) & toInt32(rightValue))
|
|
|
|
case token.OR:
|
2015-03-20 12:22:01 +00:00
|
|
|
rightValue := right.resolve()
|
2015-02-16 13:28:33 +00:00
|
|
|
return toValue_int32(toInt32(leftValue) | toInt32(rightValue))
|
|
|
|
case token.EXCLUSIVE_OR:
|
2015-03-20 12:22:01 +00:00
|
|
|
rightValue := right.resolve()
|
2015-02-16 13:28:33 +00:00
|
|
|
return toValue_int32(toInt32(leftValue) ^ toInt32(rightValue))
|
|
|
|
|
|
|
|
// Shift
|
|
|
|
// (Masking of 0x1f is to restrict the shift to a maximum of 31 places)
|
|
|
|
case token.SHIFT_LEFT:
|
2015-03-20 12:22:01 +00:00
|
|
|
rightValue := right.resolve()
|
2015-02-16 13:28:33 +00:00
|
|
|
return toValue_int32(toInt32(leftValue) << (toUint32(rightValue) & 0x1f))
|
|
|
|
case token.SHIFT_RIGHT:
|
2015-03-20 12:22:01 +00:00
|
|
|
rightValue := right.resolve()
|
2015-02-16 13:28:33 +00:00
|
|
|
return toValue_int32(toInt32(leftValue) >> (toUint32(rightValue) & 0x1f))
|
|
|
|
case token.UNSIGNED_SHIFT_RIGHT:
|
2015-03-20 12:22:01 +00:00
|
|
|
rightValue := right.resolve()
|
2015-02-16 13:28:33 +00:00
|
|
|
// Shifting an unsigned integer is a logical shift
|
|
|
|
return toValue_uint32(toUint32(leftValue) >> (toUint32(rightValue) & 0x1f))
|
|
|
|
|
|
|
|
case token.INSTANCEOF:
|
2015-03-20 12:22:01 +00:00
|
|
|
rightValue := right.resolve()
|
2015-02-16 13:28:33 +00:00
|
|
|
if !rightValue.IsObject() {
|
2015-03-20 12:22:01 +00:00
|
|
|
panic(self.panicTypeError("Expecting a function in instanceof check, but got: %v", rightValue))
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
2015-03-20 12:22:01 +00:00
|
|
|
return toValue_bool(rightValue._object().hasInstance(leftValue))
|
2015-02-16 13:28:33 +00:00
|
|
|
|
|
|
|
case token.IN:
|
2015-03-20 12:22:01 +00:00
|
|
|
rightValue := right.resolve()
|
2015-02-16 13:28:33 +00:00
|
|
|
if !rightValue.IsObject() {
|
2015-03-20 12:22:01 +00:00
|
|
|
panic(self.panicTypeError())
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
2015-03-20 12:22:01 +00:00
|
|
|
return toValue_bool(rightValue._object().hasProperty(leftValue.string()))
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
panic(hereBeDragons(operator))
|
|
|
|
}
|
|
|
|
|
2015-03-20 12:22:01 +00:00
|
|
|
func valueKindDispatchKey(left _valueKind, right _valueKind) int {
|
2015-02-16 13:28:33 +00:00
|
|
|
return (int(left) << 2) + int(right)
|
|
|
|
}
|
|
|
|
|
|
|
|
var equalDispatch map[int](func(Value, Value) bool) = makeEqualDispatch()
|
|
|
|
|
|
|
|
func makeEqualDispatch() map[int](func(Value, Value) bool) {
|
|
|
|
key := valueKindDispatchKey
|
|
|
|
return map[int](func(Value, Value) bool){
|
|
|
|
|
2015-03-20 12:22:01 +00:00
|
|
|
key(valueNumber, valueObject): func(x Value, y Value) bool { return x.float64() == y.float64() },
|
|
|
|
key(valueString, valueObject): func(x Value, y Value) bool { return x.float64() == y.float64() },
|
|
|
|
key(valueObject, valueNumber): func(x Value, y Value) bool { return x.float64() == y.float64() },
|
|
|
|
key(valueObject, valueString): func(x Value, y Value) bool { return x.float64() == y.float64() },
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type _lessThanResult int
|
|
|
|
|
|
|
|
const (
|
|
|
|
lessThanFalse _lessThanResult = iota
|
|
|
|
lessThanTrue
|
|
|
|
lessThanUndefined
|
|
|
|
)
|
|
|
|
|
|
|
|
func calculateLessThan(left Value, right Value, leftFirst bool) _lessThanResult {
|
|
|
|
|
2015-03-20 12:22:01 +00:00
|
|
|
x := Value{}
|
2015-02-16 13:28:33 +00:00
|
|
|
y := x
|
|
|
|
|
|
|
|
if leftFirst {
|
|
|
|
x = toNumberPrimitive(left)
|
|
|
|
y = toNumberPrimitive(right)
|
|
|
|
} else {
|
|
|
|
y = toNumberPrimitive(right)
|
|
|
|
x = toNumberPrimitive(left)
|
|
|
|
}
|
|
|
|
|
|
|
|
result := false
|
2015-03-20 12:22:01 +00:00
|
|
|
if x.kind != valueString || y.kind != valueString {
|
|
|
|
x, y := x.float64(), y.float64()
|
2015-02-16 13:28:33 +00:00
|
|
|
if math.IsNaN(x) || math.IsNaN(y) {
|
|
|
|
return lessThanUndefined
|
|
|
|
}
|
|
|
|
result = x < y
|
|
|
|
} else {
|
2015-03-20 12:22:01 +00:00
|
|
|
x, y := x.string(), y.string()
|
2015-02-16 13:28:33 +00:00
|
|
|
result = x < y
|
|
|
|
}
|
|
|
|
|
|
|
|
if result {
|
|
|
|
return lessThanTrue
|
|
|
|
}
|
|
|
|
|
|
|
|
return lessThanFalse
|
|
|
|
}
|
|
|
|
|
2015-03-20 12:22:01 +00:00
|
|
|
// FIXME Probably a map is not the most efficient way to do this
|
2015-02-16 13:28:33 +00:00
|
|
|
var lessThanTable [4](map[_lessThanResult]bool) = [4](map[_lessThanResult]bool){
|
|
|
|
// <
|
|
|
|
map[_lessThanResult]bool{
|
|
|
|
lessThanFalse: false,
|
|
|
|
lessThanTrue: true,
|
|
|
|
lessThanUndefined: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// >
|
|
|
|
map[_lessThanResult]bool{
|
|
|
|
lessThanFalse: false,
|
|
|
|
lessThanTrue: true,
|
|
|
|
lessThanUndefined: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// <=
|
|
|
|
map[_lessThanResult]bool{
|
|
|
|
lessThanFalse: true,
|
|
|
|
lessThanTrue: false,
|
|
|
|
lessThanUndefined: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// >=
|
|
|
|
map[_lessThanResult]bool{
|
|
|
|
lessThanFalse: true,
|
|
|
|
lessThanTrue: false,
|
|
|
|
lessThanUndefined: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *_runtime) calculateComparison(comparator token.Token, left Value, right Value) bool {
|
|
|
|
|
|
|
|
// FIXME Use strictEqualityComparison?
|
|
|
|
// TODO This might be redundant now (with regards to evaluateComparison)
|
2015-03-20 12:22:01 +00:00
|
|
|
x := left.resolve()
|
|
|
|
y := right.resolve()
|
2015-02-16 13:28:33 +00:00
|
|
|
|
|
|
|
kindEqualKind := false
|
|
|
|
result := true
|
|
|
|
negate := false
|
|
|
|
|
|
|
|
switch comparator {
|
|
|
|
case token.LESS:
|
|
|
|
result = lessThanTable[0][calculateLessThan(x, y, true)]
|
|
|
|
case token.GREATER:
|
|
|
|
result = lessThanTable[1][calculateLessThan(y, x, false)]
|
|
|
|
case token.LESS_OR_EQUAL:
|
|
|
|
result = lessThanTable[2][calculateLessThan(y, x, false)]
|
|
|
|
case token.GREATER_OR_EQUAL:
|
|
|
|
result = lessThanTable[3][calculateLessThan(x, y, true)]
|
|
|
|
case token.STRICT_NOT_EQUAL:
|
|
|
|
negate = true
|
|
|
|
fallthrough
|
|
|
|
case token.STRICT_EQUAL:
|
2015-03-20 12:22:01 +00:00
|
|
|
if x.kind != y.kind {
|
2015-02-16 13:28:33 +00:00
|
|
|
result = false
|
|
|
|
} else {
|
|
|
|
kindEqualKind = true
|
|
|
|
}
|
|
|
|
case token.NOT_EQUAL:
|
|
|
|
negate = true
|
|
|
|
fallthrough
|
|
|
|
case token.EQUAL:
|
2015-03-20 12:22:01 +00:00
|
|
|
if x.kind == y.kind {
|
2015-02-16 13:28:33 +00:00
|
|
|
kindEqualKind = true
|
2015-03-20 12:22:01 +00:00
|
|
|
} else if x.kind <= valueNull && y.kind <= valueNull {
|
2015-02-16 13:28:33 +00:00
|
|
|
result = true
|
2015-03-20 12:22:01 +00:00
|
|
|
} else if x.kind <= valueNull || y.kind <= valueNull {
|
2015-02-16 13:28:33 +00:00
|
|
|
result = false
|
2015-03-20 12:22:01 +00:00
|
|
|
} else if x.kind <= valueString && y.kind <= valueString {
|
|
|
|
result = x.float64() == y.float64()
|
|
|
|
} else if x.kind == valueBoolean {
|
|
|
|
result = self.calculateComparison(token.EQUAL, toValue_float64(x.float64()), y)
|
|
|
|
} else if y.kind == valueBoolean {
|
|
|
|
result = self.calculateComparison(token.EQUAL, x, toValue_float64(y.float64()))
|
|
|
|
} else if x.kind == valueObject {
|
2015-02-16 13:28:33 +00:00
|
|
|
result = self.calculateComparison(token.EQUAL, toPrimitive(x), y)
|
2015-03-20 12:22:01 +00:00
|
|
|
} else if y.kind == valueObject {
|
2015-02-16 13:28:33 +00:00
|
|
|
result = self.calculateComparison(token.EQUAL, x, toPrimitive(y))
|
|
|
|
} else {
|
|
|
|
panic(hereBeDragons("Unable to test for equality: %v ==? %v", x, y))
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("Unknown comparator %s", comparator.String()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if kindEqualKind {
|
2015-03-20 12:22:01 +00:00
|
|
|
switch x.kind {
|
2015-02-16 13:28:33 +00:00
|
|
|
case valueUndefined, valueNull:
|
|
|
|
result = true
|
|
|
|
case valueNumber:
|
2015-03-20 12:22:01 +00:00
|
|
|
x := x.float64()
|
|
|
|
y := y.float64()
|
2015-02-16 13:28:33 +00:00
|
|
|
if math.IsNaN(x) || math.IsNaN(y) {
|
|
|
|
result = false
|
|
|
|
} else {
|
|
|
|
result = x == y
|
|
|
|
}
|
|
|
|
case valueString:
|
2015-03-20 12:22:01 +00:00
|
|
|
result = x.string() == y.string()
|
2015-02-16 13:28:33 +00:00
|
|
|
case valueBoolean:
|
2015-03-20 12:22:01 +00:00
|
|
|
result = x.bool() == y.bool()
|
2015-02-16 13:28:33 +00:00
|
|
|
case valueObject:
|
|
|
|
result = x._object() == y._object()
|
|
|
|
default:
|
|
|
|
goto ERROR
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if negate {
|
|
|
|
result = !result
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
ERROR:
|
2015-03-20 12:22:01 +00:00
|
|
|
panic(hereBeDragons("%v (%v) %s %v (%v)", x, x.kind, comparator, y, y.kind))
|
2015-02-16 13:28:33 +00:00
|
|
|
}
|