public
Description: Haskell implemented JavaScript interpreter
Homepage:
Clone URL: git://github.com/motemen/jusk.git
jusk / Operator.hs
100644 203 lines (167 sloc) 6.756 kb
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
{-# OPTIONS_GHC -fglasgow-exts #-}
 
module Operator where
import Monad
import Data.Bits
 
import DataTypes
import Eval
import Internal
 
data OperatorDef = Unary { opName :: String, opUnaryFunc :: Value -> Evaluate Value }
                 | Binary { opName :: String, opBinaryFunc :: Value -> Value -> Evaluate Value }
 
operatorsTable :: [OperatorDef]
operatorsTable = [
        Unary "typeof" $ typeOf,
 
        Unary "+" $ numericUnaryOp id,
        Unary "-" $ numericUnaryOp negate,
 
        Unary "~" $ (.~.),
        Unary "!" $ (.!.),
 
        Binary "*" $ numericBinaryOp (*),
        Binary "/" $ numericBinaryOp (/),
        Binary "%" $ withNoRef2 (.%.),
 
        Binary "+" $ (.+.),
        Binary "-" $ numericBinaryOp (-),
 
        Binary ">>" $ (.>>.),
        Binary "<<" $ (.<<.),
        Binary ">>>" $ (.>>>.),
 
        Binary "<" $ comparisonOp (<),
        Binary ">" $ comparisonOp (>),
        Binary ">=" $ comparisonOp (>=),
        Binary "<=" $ comparisonOp (<=),
        Binary "instanceof" $ instanceofOperator,
        Binary "in" $ inOperator,
 
        Binary "==" $ (.==.),
        Binary "!=" $ (.!=.),
 
        Binary "===" $ (.===.),
        Binary "!==" $ (.!==.),
 
        Binary "&" $ bitwiseBinaryOp (.&.),
        Binary "|" $ bitwiseBinaryOp (.|.),
        Binary "^" $ bitwiseBinaryOp (xor)
    ]
 
typeOf :: Value -> Evaluate Value
typeOf ref@(Reference object name) =
    do klass <- classOf object
       if klass == "Global" || klass == "Activation"
          then ifM (isBound name)
                   (getVar name >>= readRef >>= typeOf)
                   (return $ String "undefined")
          else liftM (toValue . typeString) $ readRef =<< getValue ref
 
typeOf object =
    liftM (toValue . typeString) $ readRef =<< getValue object
 
numericUnaryOp :: (Double -> Double) -> Value -> Evaluate Value
numericUnaryOp op x =
    do x <- readRef =<< getValue x
       case x of
           (Number NaN) -> return x
           (Number n) -> return $ Number $ Double $ op $ toDouble n
           _ -> do n <- toNumber x
                   numericUnaryOp op (Number n)
 
numericBinaryOp :: (Double -> Double -> Double) -> Value -> Value -> Evaluate Value
numericBinaryOp op x y =
    do n <- toNumber x
       m <- toNumber y
       return $ Number $ maybe NaN Double (applyNumericOp op n m)
 
bitwiseBinaryOp :: (Int -> Int -> Int) -> Value -> Value -> Evaluate Value
bitwiseBinaryOp op n m =
    do n <- toInt n
       m <- toInt m
       return $ toValue $ n `op` m
 
(.+.) :: Value -> Value -> Evaluate Value
(.+.) x y =
    do x <- toPrimitive x "Number"
       y <- toPrimitive y "Number"
       if isString x || isString y
          then do s <- toString x
                  t <- toString y
                  return $ String $ s ++ t
          else do n <- toNumber x
                  m <- toNumber y
                  numericBinaryOp (+) (Number n) (Number m)
 
(.%.) :: Value -> Value -> Evaluate Value
(.%.) (Number (Integer n)) (Number (Integer m)) =
    return $ Number $ Integer $ rem n m
 
(.%.) (Number n) (Number m) =
    let n' = toDouble n
        m' = toDouble m
        in return $ Number $ Double $ n' - m' * fromIntegral (floor (n' / m'))
 
(.%.) _ _ = return $ Number NaN
 
(.~.) :: Value -> Evaluate Value
(.~.) = liftM (toValue . complement) . toInt
 
(.!.) :: Value -> Evaluate Value
(.!.) = liftM (toValue . not) . toBoolean
 
(.>>.) :: Value -> Value -> Evaluate Value
(.>>.) n m =
    do n <- toInt n
       m <- liftM (0x1F .&.) (toUInt m)
       return $ toValue $ n `shiftR` m
 
(.<<.) :: Value -> Value -> Evaluate Value
(.<<.) n m =
    do n <- toInt n
       m <- liftM (0x1F .&.) (toUInt m)
       return $ toValue $ n `shiftL` m
 
(.>>>.) :: Value -> Value -> Evaluate Value
(.>>>.) n m =
    do n <- toUInt n
       m <- liftM (0x1F .&.) (toUInt m)
       return $ toValue $ foldl clearBit (n `shiftR` m) [31,30..(31-m+1)]
 
(.==.) :: Value -> Value -> Evaluate Value
(.==.) xRef yRef =
    do xRef <- getValue xRef; yRef <- getValue yRef
       x <- readRef xRef; y <- readRef yRef
       case (x, y) of
            (Undefined, Null) -> return $ Boolean True
            (Null, Undefined) -> return $ Boolean True
            _ | isPrimitive x && isPrimitive y ->
                return $ Boolean $ x == y
            (Object { }, y) | isNumber y || isString y ->
                (.==. y) =<< toPrimitive x ""
            (x, Object { }) | isNumber x || isString x ->
                (x .==.) =<< toPrimitive y ""
            (Object { }, Object { }) ->
                return $ Boolean $ xRef == yRef
            _ -> return $ Boolean False
 
(.!=.) :: Value -> Value -> Evaluate Value
(.!=.) x y = liftM not' $ x .==. y
    where not' (Boolean b) = Boolean (not b)
 
(.===.) :: Value -> Value -> Evaluate Value
(.===.) x y =
    do x <- getValue x; y <- getValue y
       x' <- readRef x; y' <- readRef y
       if isPrimitive x' || isPrimitive y'
          then return $ toValue $ x' == y'
          else return $ toValue $ x == y
 
(.!==.) :: Value -> Value -> Evaluate Value
(.!==.) x y = liftM not' $ x .===. y
    where not' (Boolean b) = Boolean (not b)
 
instanceofOperator :: Value -> Value -> Evaluate Value
instanceofOperator value klass =
    do value <- getValue value
       klass <- getValue klass
       klassProto <- getProp klass "prototype"
       liftM toValue $ liftM2 (||) (isProtoEq value klass) (isProtoEq value klassProto)
    where isProtoEq value obj =
              do proto <- prototypeOf value
                 case proto of
                      Null -> return False
                      _ | proto == obj -> return True
                      _ -> isProtoEq proto obj
 
inOperator :: Value -> Value -> Evaluate Value
inOperator name object =
    if isPrimitive object
       then do objStr <- toString object
               throw "TypeError" $ "invalid 'in' operand " ++ objStr
       else do name <- toString name
               hasProp <- hasProperty object name
               return $ Boolean hasProp
 
-- http://www2u.biglobe.ne.jp/~oz-07ams/prog/ecma262r3/11_Expressions.html#section-11.8.5
comparisonOp :: (forall a. (Ord a) => a -> a -> Bool) -> Value -> Value -> Evaluate Value
comparisonOp op x y =
    do x <- toPrimitive x "Number"
       y <- toPrimitive y "Number"
       case (x, y) of
            (String s, String t) -> return $ Boolean $ s `op` t
            _ -> do n <- toNumber x
                    m <- toNumber y
                    return $ Boolean $ maybe False id (applyNumericOp op n m)
 
applyNumericOp :: (Double -> Double -> a) -> Number -> Number -> Maybe a
applyNumericOp _ NaN _ = Nothing
applyNumericOp _ _ NaN = Nothing
applyNumericOp op n m = Just $ (toDouble n) `op` (toDouble m)