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.
319 lines
8.1 KiB
319 lines
8.1 KiB
10 years ago
|
package otto
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"math"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/robertkrimen/otto/token"
|
||
|
)
|
||
|
|
||
|
func (self *_runtime) evaluateMultiply(left float64, right float64) Value {
|
||
|
// TODO 11.5.1
|
||
10 years ago
|
return Value{}
|
||
10 years ago
|
}
|
||
|
|
||
|
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
|
||
10 years ago
|
return Value{}
|
||
10 years ago
|
}
|
||
|
|
||
|
func (self *_runtime) calculateBinaryExpression(operator token.Token, left Value, right Value) Value {
|
||
|
|
||
10 years ago
|
leftValue := left.resolve()
|
||
10 years ago
|
|
||
|
switch operator {
|
||
|
|
||
|
// Additive
|
||
|
case token.PLUS:
|
||
|
leftValue = toPrimitive(leftValue)
|
||
10 years ago
|
rightValue := right.resolve()
|
||
10 years ago
|
rightValue = toPrimitive(rightValue)
|
||
|
|
||
|
if leftValue.IsString() || rightValue.IsString() {
|
||
10 years ago
|
return toValue_string(strings.Join([]string{leftValue.string(), rightValue.string()}, ""))
|
||
10 years ago
|
} else {
|
||
10 years ago
|
return toValue_float64(leftValue.float64() + rightValue.float64())
|
||
10 years ago
|
}
|
||
|
case token.MINUS:
|
||
10 years ago
|
rightValue := right.resolve()
|
||
|
return toValue_float64(leftValue.float64() - rightValue.float64())
|
||
10 years ago
|
|
||
|
// Multiplicative
|
||
|
case token.MULTIPLY:
|
||
10 years ago
|
rightValue := right.resolve()
|
||
|
return toValue_float64(leftValue.float64() * rightValue.float64())
|
||
10 years ago
|
case token.SLASH:
|
||
10 years ago
|
rightValue := right.resolve()
|
||
|
return self.evaluateDivide(leftValue.float64(), rightValue.float64())
|
||
10 years ago
|
case token.REMAINDER:
|
||
10 years ago
|
rightValue := right.resolve()
|
||
|
return toValue_float64(math.Mod(leftValue.float64(), rightValue.float64()))
|
||
10 years ago
|
|
||
|
// Logical
|
||
|
case token.LOGICAL_AND:
|
||
10 years ago
|
left := leftValue.bool()
|
||
10 years ago
|
if !left {
|
||
10 years ago
|
return falseValue
|
||
10 years ago
|
}
|
||
10 years ago
|
return toValue_bool(right.resolve().bool())
|
||
10 years ago
|
case token.LOGICAL_OR:
|
||
10 years ago
|
left := leftValue.bool()
|
||
10 years ago
|
if left {
|
||
10 years ago
|
return trueValue
|
||
10 years ago
|
}
|
||
10 years ago
|
return toValue_bool(right.resolve().bool())
|
||
10 years ago
|
|
||
|
// Bitwise
|
||
|
case token.AND:
|
||
10 years ago
|
rightValue := right.resolve()
|
||
10 years ago
|
return toValue_int32(toInt32(leftValue) & toInt32(rightValue))
|
||
|
case token.OR:
|
||
10 years ago
|
rightValue := right.resolve()
|
||
10 years ago
|
return toValue_int32(toInt32(leftValue) | toInt32(rightValue))
|
||
|
case token.EXCLUSIVE_OR:
|
||
10 years ago
|
rightValue := right.resolve()
|
||
10 years ago
|
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:
|
||
10 years ago
|
rightValue := right.resolve()
|
||
10 years ago
|
return toValue_int32(toInt32(leftValue) << (toUint32(rightValue) & 0x1f))
|
||
|
case token.SHIFT_RIGHT:
|
||
10 years ago
|
rightValue := right.resolve()
|
||
10 years ago
|
return toValue_int32(toInt32(leftValue) >> (toUint32(rightValue) & 0x1f))
|
||
|
case token.UNSIGNED_SHIFT_RIGHT:
|
||
10 years ago
|
rightValue := right.resolve()
|
||
10 years ago
|
// Shifting an unsigned integer is a logical shift
|
||
|
return toValue_uint32(toUint32(leftValue) >> (toUint32(rightValue) & 0x1f))
|
||
|
|
||
|
case token.INSTANCEOF:
|
||
10 years ago
|
rightValue := right.resolve()
|
||
10 years ago
|
if !rightValue.IsObject() {
|
||
10 years ago
|
panic(self.panicTypeError("Expecting a function in instanceof check, but got: %v", rightValue))
|
||
10 years ago
|
}
|
||
10 years ago
|
return toValue_bool(rightValue._object().hasInstance(leftValue))
|
||
10 years ago
|
|
||
|
case token.IN:
|
||
10 years ago
|
rightValue := right.resolve()
|
||
10 years ago
|
if !rightValue.IsObject() {
|
||
10 years ago
|
panic(self.panicTypeError())
|
||
10 years ago
|
}
|
||
10 years ago
|
return toValue_bool(rightValue._object().hasProperty(leftValue.string()))
|
||
10 years ago
|
}
|
||
|
|
||
|
panic(hereBeDragons(operator))
|
||
|
}
|
||
|
|
||
10 years ago
|
func valueKindDispatchKey(left _valueKind, right _valueKind) int {
|
||
10 years ago
|
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){
|
||
|
|
||
10 years ago
|
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() },
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
type _lessThanResult int
|
||
|
|
||
|
const (
|
||
|
lessThanFalse _lessThanResult = iota
|
||
|
lessThanTrue
|
||
|
lessThanUndefined
|
||
|
)
|
||
|
|
||
|
func calculateLessThan(left Value, right Value, leftFirst bool) _lessThanResult {
|
||
|
|
||
10 years ago
|
x := Value{}
|
||
10 years ago
|
y := x
|
||
|
|
||
|
if leftFirst {
|
||
|
x = toNumberPrimitive(left)
|
||
|
y = toNumberPrimitive(right)
|
||
|
} else {
|
||
|
y = toNumberPrimitive(right)
|
||
|
x = toNumberPrimitive(left)
|
||
|
}
|
||
|
|
||
|
result := false
|
||
10 years ago
|
if x.kind != valueString || y.kind != valueString {
|
||
|
x, y := x.float64(), y.float64()
|
||
10 years ago
|
if math.IsNaN(x) || math.IsNaN(y) {
|
||
|
return lessThanUndefined
|
||
|
}
|
||
|
result = x < y
|
||
|
} else {
|
||
10 years ago
|
x, y := x.string(), y.string()
|
||
10 years ago
|
result = x < y
|
||
|
}
|
||
|
|
||
|
if result {
|
||
|
return lessThanTrue
|
||
|
}
|
||
|
|
||
|
return lessThanFalse
|
||
|
}
|
||
|
|
||
10 years ago
|
// FIXME Probably a map is not the most efficient way to do this
|
||
10 years ago
|
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)
|
||
10 years ago
|
x := left.resolve()
|
||
|
y := right.resolve()
|
||
10 years ago
|
|
||
|
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:
|
||
10 years ago
|
if x.kind != y.kind {
|
||
10 years ago
|
result = false
|
||
|
} else {
|
||
|
kindEqualKind = true
|
||
|
}
|
||
|
case token.NOT_EQUAL:
|
||
|
negate = true
|
||
|
fallthrough
|
||
|
case token.EQUAL:
|
||
10 years ago
|
if x.kind == y.kind {
|
||
10 years ago
|
kindEqualKind = true
|
||
10 years ago
|
} else if x.kind <= valueNull && y.kind <= valueNull {
|
||
10 years ago
|
result = true
|
||
10 years ago
|
} else if x.kind <= valueNull || y.kind <= valueNull {
|
||
10 years ago
|
result = false
|
||
10 years ago
|
} 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 {
|
||
10 years ago
|
result = self.calculateComparison(token.EQUAL, toPrimitive(x), y)
|
||
10 years ago
|
} else if y.kind == valueObject {
|
||
10 years ago
|
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 {
|
||
10 years ago
|
switch x.kind {
|
||
10 years ago
|
case valueUndefined, valueNull:
|
||
|
result = true
|
||
|
case valueNumber:
|
||
10 years ago
|
x := x.float64()
|
||
|
y := y.float64()
|
||
10 years ago
|
if math.IsNaN(x) || math.IsNaN(y) {
|
||
|
result = false
|
||
|
} else {
|
||
|
result = x == y
|
||
|
}
|
||
|
case valueString:
|
||
10 years ago
|
result = x.string() == y.string()
|
||
10 years ago
|
case valueBoolean:
|
||
10 years ago
|
result = x.bool() == y.bool()
|
||
10 years ago
|
case valueObject:
|
||
|
result = x._object() == y._object()
|
||
|
default:
|
||
|
goto ERROR
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if negate {
|
||
|
result = !result
|
||
|
}
|
||
|
|
||
|
return result
|
||
|
|
||
|
ERROR:
|
||
10 years ago
|
panic(hereBeDragons("%v (%v) %s %v (%v)", x, x.kind, comparator, y, y.kind))
|
||
10 years ago
|
}
|