| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #library('js_helper'); | 5 #library('js_helper'); |
| 6 | 6 |
| 7 #import('coreimpl.dart'); | 7 #import('coreimpl.dart'); |
| 8 | 8 |
| 9 #source('date_helper.dart'); | 9 #source('date_helper.dart'); |
| 10 | 10 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 | 33 |
| 34 add(var a, var b) { | 34 add(var a, var b) { |
| 35 if (checkNumbers(a, b, "num+ expects a number as second operand.")) { | 35 if (checkNumbers(a, b, "num+ expects a number as second operand.")) { |
| 36 return JS("num", @"$0 + $1", a, b); | 36 return JS("num", @"$0 + $1", a, b); |
| 37 } else if (a is String) { | 37 } else if (a is String) { |
| 38 b = b.toString(); | 38 b = b.toString(); |
| 39 if (b is String) { | 39 if (b is String) { |
| 40 return JS("String", @"$0 + $1", a, b); | 40 return JS("String", @"$0 + $1", a, b); |
| 41 } | 41 } |
| 42 // The following line is too long, but we can't break it using the + | 42 checkNull(b); |
| 43 // operator, since that's what we are defining here. | 43 throw new IllegalArgumentException(b); |
| 44 throw "calling toString() on right hand operand of operator + did not return
a String"; | |
| 45 } | 44 } |
| 45 checkNull(a); |
| 46 return UNINTERCEPTED(a + b); | 46 return UNINTERCEPTED(a + b); |
| 47 } | 47 } |
| 48 | 48 |
| 49 div(var a, var b) { | 49 div(var a, var b) { |
| 50 if (checkNumbers(a, b, "num/ expects a number as second operand.")) { | 50 if (checkNumbers(a, b, "num/ expects a number as second operand.")) { |
| 51 return JS("num", @"$0 / $1", a, b); | 51 return JS("num", @"$0 / $1", a, b); |
| 52 } | 52 } |
| 53 checkNull(a); |
| 53 return UNINTERCEPTED(a / b); | 54 return UNINTERCEPTED(a / b); |
| 54 } | 55 } |
| 55 | 56 |
| 56 mul(var a, var b) { | 57 mul(var a, var b) { |
| 57 if (checkNumbers(a, b, "num* expects a number as second operand.")) { | 58 if (checkNumbers(a, b, "num* expects a number as second operand.")) { |
| 58 return JS("num", @"$0 * $1", a, b); | 59 return JS("num", @"$0 * $1", a, b); |
| 59 } | 60 } |
| 61 checkNull(a); |
| 60 return UNINTERCEPTED(a * b); | 62 return UNINTERCEPTED(a * b); |
| 61 } | 63 } |
| 62 | 64 |
| 63 sub(var a, var b) { | 65 sub(var a, var b) { |
| 64 if (checkNumbers(a, b, "num- expects a number as second operand.")) { | 66 if (checkNumbers(a, b, "num- expects a number as second operand.")) { |
| 65 return JS("num", @"$0 - $1", a, b); | 67 return JS("num", @"$0 - $1", a, b); |
| 66 } | 68 } |
| 69 checkNull(a); |
| 67 return UNINTERCEPTED(a - b); | 70 return UNINTERCEPTED(a - b); |
| 68 } | 71 } |
| 69 | 72 |
| 70 mod(var a, var b) { | 73 mod(var a, var b) { |
| 71 if (checkNumbers(a, b, "int% expects an int as second operand.")) { | 74 if (checkNumbers(a, b, "int% expects an int as second operand.")) { |
| 72 if (b === 0) throw new IntegerDivisionByZeroException(); | 75 if (b === 0) throw new IntegerDivisionByZeroException(); |
| 73 // Euclidean Modulo. | 76 // Euclidean Modulo. |
| 74 int result = JS("num", @"$0 % $1", a, b); | 77 int result = JS("num", @"$0 % $1", a, b); |
| 75 if (result == 0) return 0; // Make sure we don't return -0.0. | 78 if (result == 0) return 0; // Make sure we don't return -0.0. |
| 76 if (result > 0) return result; | 79 if (result > 0) return result; |
| 77 if (b < 0) { | 80 if (b < 0) { |
| 78 return result - b; | 81 return result - b; |
| 79 } else { | 82 } else { |
| 80 return result + b; | 83 return result + b; |
| 81 } | 84 } |
| 82 } | 85 } |
| 86 checkNull(a); |
| 83 return UNINTERCEPTED(a % b); | 87 return UNINTERCEPTED(a % b); |
| 84 } | 88 } |
| 85 | 89 |
| 86 tdiv(var a, var b) { | 90 tdiv(var a, var b) { |
| 87 if (checkNumbers(a, b, "num~/ expects a number as second operand.")) { | 91 if (checkNumbers(a, b, "num~/ expects a number as second operand.")) { |
| 88 if (b === 0) throw new IntegerDivisionByZeroException(); | 92 if (b === 0) throw new IntegerDivisionByZeroException(); |
| 89 return (a / b).truncate(); | 93 return (a / b).truncate(); |
| 90 } | 94 } |
| 95 checkNull(a); |
| 91 return UNINTERCEPTED(a ~/ b); | 96 return UNINTERCEPTED(a ~/ b); |
| 92 } | 97 } |
| 93 | 98 |
| 94 eq(var a, var b) { | 99 eq(var a, var b) { |
| 95 if (JS("bool", @"typeof $0 === 'object'", a)) { | 100 if (JS("bool", @"typeof $0 === 'object'", a)) { |
| 96 if (JS_HAS_EQUALS(a)) { | 101 if (JS_HAS_EQUALS(a)) { |
| 97 return UNINTERCEPTED(a == b) === true; | 102 return UNINTERCEPTED(a == b) === true; |
| 98 } else { | 103 } else { |
| 99 return JS("bool", @"$0 === $1", a, b); | 104 return JS("bool", @"$0 === $1", a, b); |
| 100 } | 105 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 116 } | 121 } |
| 117 } else { | 122 } else { |
| 118 return JS("bool", @"typeof $0 === 'undefined'", a); | 123 return JS("bool", @"typeof $0 === 'undefined'", a); |
| 119 } | 124 } |
| 120 } | 125 } |
| 121 | 126 |
| 122 gt(var a, var b) { | 127 gt(var a, var b) { |
| 123 if (checkNumbers(a, b, "num> expects a number as second operand.")) { | 128 if (checkNumbers(a, b, "num> expects a number as second operand.")) { |
| 124 return JS("bool", @"$0 > $1", a, b); | 129 return JS("bool", @"$0 > $1", a, b); |
| 125 } | 130 } |
| 131 checkNull(a); |
| 126 return UNINTERCEPTED(a > b); | 132 return UNINTERCEPTED(a > b); |
| 127 } | 133 } |
| 128 | 134 |
| 129 ge(var a, var b) { | 135 ge(var a, var b) { |
| 130 if (checkNumbers(a, b, "num>= expects a number as second operand.")) { | 136 if (checkNumbers(a, b, "num>= expects a number as second operand.")) { |
| 131 return JS("bool", @"$0 >= $1", a, b); | 137 return JS("bool", @"$0 >= $1", a, b); |
| 132 } | 138 } |
| 139 checkNull(a); |
| 133 return UNINTERCEPTED(a >= b); | 140 return UNINTERCEPTED(a >= b); |
| 134 } | 141 } |
| 135 | 142 |
| 136 lt(var a, var b) { | 143 lt(var a, var b) { |
| 137 if (checkNumbers(a, b, "num< expects a number as second operand.")) { | 144 if (checkNumbers(a, b, "num< expects a number as second operand.")) { |
| 138 return JS("bool", @"$0 < $1", a, b); | 145 return JS("bool", @"$0 < $1", a, b); |
| 139 } | 146 } |
| 147 checkNull(a); |
| 140 return UNINTERCEPTED(a < b); | 148 return UNINTERCEPTED(a < b); |
| 141 } | 149 } |
| 142 | 150 |
| 143 le(var a, var b) { | 151 le(var a, var b) { |
| 144 if (checkNumbers(a, b, "num<= expects a number as second operand.")) { | 152 if (checkNumbers(a, b, "num<= expects a number as second operand.")) { |
| 145 return JS("bool", @"$0 <= $1", a, b); | 153 return JS("bool", @"$0 <= $1", a, b); |
| 146 } | 154 } |
| 155 checkNull(a); |
| 147 return UNINTERCEPTED(a <= b); | 156 return UNINTERCEPTED(a <= b); |
| 148 } | 157 } |
| 149 | 158 |
| 150 shl(var a, var b) { | 159 shl(var a, var b) { |
| 151 // TODO(floitsch): inputs must be integers. | 160 // TODO(floitsch): inputs must be integers. |
| 152 if (checkNumbers(a, b, "int<< expects an int as second operand.")) { | 161 if (checkNumbers(a, b, "int<< expects an int as second operand.")) { |
| 153 if (b < 0) throw new IllegalArgumentException(b); | 162 if (b < 0) throw new IllegalArgumentException(b); |
| 154 return JS("num", @"$0 << $1", a, b); | 163 return JS("num", @"$0 << $1", a, b); |
| 155 } | 164 } |
| 165 checkNull(a); |
| 156 return UNINTERCEPTED(a << b); | 166 return UNINTERCEPTED(a << b); |
| 157 } | 167 } |
| 158 | 168 |
| 159 shr(var a, var b) { | 169 shr(var a, var b) { |
| 160 // TODO(floitsch): inputs must be integers. | 170 // TODO(floitsch): inputs must be integers. |
| 161 if (checkNumbers(a, b, "int>> expects an int as second operand.")) { | 171 if (checkNumbers(a, b, "int>> expects an int as second operand.")) { |
| 162 if (b < 0) throw new IllegalArgumentException(b); | 172 if (b < 0) throw new IllegalArgumentException(b); |
| 163 return JS("num", @"$0 >> $1", a, b); | 173 return JS("num", @"$0 >> $1", a, b); |
| 164 } | 174 } |
| 175 checkNull(a); |
| 165 return UNINTERCEPTED(a >> b); | 176 return UNINTERCEPTED(a >> b); |
| 166 } | 177 } |
| 167 | 178 |
| 168 and(var a, var b) { | 179 and(var a, var b) { |
| 169 // TODO(floitsch): inputs must be integers. | 180 // TODO(floitsch): inputs must be integers. |
| 170 if (checkNumbers(a, b, "int& expects an int as second operand.")) { | 181 if (checkNumbers(a, b, "int& expects an int as second operand.")) { |
| 171 return JS("num", @"$0 & $1", a, b); | 182 return JS("num", @"$0 & $1", a, b); |
| 172 } | 183 } |
| 184 checkNull(a); |
| 173 return UNINTERCEPTED(a & b); | 185 return UNINTERCEPTED(a & b); |
| 174 } | 186 } |
| 175 | 187 |
| 176 or(var a, var b) { | 188 or(var a, var b) { |
| 177 // TODO(floitsch): inputs must be integers. | 189 // TODO(floitsch): inputs must be integers. |
| 178 if (checkNumbers(a, b, "int| expects an int as second operand.")) { | 190 if (checkNumbers(a, b, "int| expects an int as second operand.")) { |
| 179 return JS("num", @"$0 | $1", a, b); | 191 return JS("num", @"$0 | $1", a, b); |
| 180 } | 192 } |
| 193 checkNull(a); |
| 181 return UNINTERCEPTED(a | b); | 194 return UNINTERCEPTED(a | b); |
| 182 } | 195 } |
| 183 | 196 |
| 184 xor(var a, var b) { | 197 xor(var a, var b) { |
| 185 // TODO(floitsch): inputs must be integers. | 198 // TODO(floitsch): inputs must be integers. |
| 186 if (checkNumbers(a, b, "int^ expects an int as second operand.")) { | 199 if (checkNumbers(a, b, "int^ expects an int as second operand.")) { |
| 187 return JS("num", @"$0 ^ $1", a, b); | 200 return JS("num", @"$0 ^ $1", a, b); |
| 188 } | 201 } |
| 202 checkNull(a); |
| 189 return UNINTERCEPTED(a ^ b); | 203 return UNINTERCEPTED(a ^ b); |
| 190 } | 204 } |
| 191 | 205 |
| 192 not(var a) { | 206 not(var a) { |
| 193 if (JS("bool", @"typeof $0 === 'number'", a)) return JS("num", @"~$0", a); | 207 if (JS("bool", @"typeof $0 === 'number'", a)) return JS("num", @"~$0", a); |
| 208 checkNull(a); |
| 194 return UNINTERCEPTED(~a); | 209 return UNINTERCEPTED(~a); |
| 195 } | 210 } |
| 196 | 211 |
| 197 neg(var a) { | 212 neg(var a) { |
| 198 if (JS("bool", @"typeof $0 === 'number'", a)) return JS("num", @"-$0", a); | 213 if (JS("bool", @"typeof $0 === 'number'", a)) return JS("num", @"-$0", a); |
| 214 checkNull(a); |
| 199 return UNINTERCEPTED(-a); | 215 return UNINTERCEPTED(-a); |
| 200 } | 216 } |
| 201 | 217 |
| 202 index(var a, var index) { | 218 index(var a, var index) { |
| 203 checkNull(a); | 219 checkNull(a); |
| 204 if (a is String || isJSArray(a)) { | 220 if (a is String || isJSArray(a)) { |
| 205 if (index is !int) { | 221 if (index is !int) { |
| 206 if (index is !num) throw new IllegalArgumentException(index); | 222 if (index is !num) throw new IllegalArgumentException(index); |
| 207 if (index.truncate() !== index) throw new IllegalArgumentException(index); | 223 if (index.truncate() !== index) throw new IllegalArgumentException(index); |
| 208 } | 224 } |
| 209 if (index < 0 || index >= a.length) { | 225 if (index < 0 || index >= a.length) { |
| 210 throw new IndexOutOfRangeException(index); | 226 throw new IndexOutOfRangeException(index); |
| 211 } | 227 } |
| 212 return JS("Object", @"$0[$1]", a, index); | 228 return JS("Object", @"$0[$1]", a, index); |
| 213 } | 229 } |
| 230 checkNull(a); |
| 214 return UNINTERCEPTED(a[index]); | 231 return UNINTERCEPTED(a[index]); |
| 215 } | 232 } |
| 216 | 233 |
| 217 indexSet(var a, var index, var value) { | 234 indexSet(var a, var index, var value) { |
| 218 checkNull(a); | 235 checkNull(a); |
| 219 if (isJSArray(a)) { | 236 if (isJSArray(a)) { |
| 220 if (!(index is int)) { | 237 if (!(index is int)) { |
| 221 throw new IllegalArgumentException(index); | 238 throw new IllegalArgumentException(index); |
| 222 } | 239 } |
| 223 if (index < 0 || index >= a.length) { | 240 if (index < 0 || index >= a.length) { |
| 224 throw new IndexOutOfRangeException(index); | 241 throw new IndexOutOfRangeException(index); |
| 225 } | 242 } |
| 226 return JS("Object", @"$0[$1] = $2", a, index, value); | 243 return JS("Object", @"$0[$1] = $2", a, index, value); |
| 227 } | 244 } |
| 245 checkNull(a); |
| 228 return UNINTERCEPTED(a[index] = value); | 246 return UNINTERCEPTED(a[index] = value); |
| 229 } | 247 } |
| 230 | 248 |
| 231 builtin$add$1(var receiver, var value) { | 249 builtin$add$1(var receiver, var value) { |
| 232 checkNull(receiver); | 250 checkNull(receiver); |
| 233 if (isJSArray(receiver)) { | 251 if (isJSArray(receiver)) { |
| 234 JS("Object", @"$0.push($1)", receiver, value); | 252 JS("Object", @"$0.push($1)", receiver, value); |
| 235 return; | 253 return; |
| 236 } | 254 } |
| 237 return UNINTERCEPTED(receiver.add(value)); | 255 return UNINTERCEPTED(receiver.add(value)); |
| (...skipping 878 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1116 builtin$hashCode$0(receiver) { | 1134 builtin$hashCode$0(receiver) { |
| 1117 if (receiver is num) return receiver & 0x1FFFFFFF; | 1135 if (receiver is num) return receiver & 0x1FFFFFFF; |
| 1118 if (receiver is String) { | 1136 if (receiver is String) { |
| 1119 throw 'String.hashCode is not implemented'; | 1137 throw 'String.hashCode is not implemented'; |
| 1120 } | 1138 } |
| 1121 if (isJSArray(receiver)) { | 1139 if (isJSArray(receiver)) { |
| 1122 throw 'List.hashCode is not implemented'; | 1140 throw 'List.hashCode is not implemented'; |
| 1123 } | 1141 } |
| 1124 return UNINTERCEPTED(receiver.hashCode()); | 1142 return UNINTERCEPTED(receiver.hashCode()); |
| 1125 } | 1143 } |
| OLD | NEW |