| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 /** | 5 /** |
| 6 * Generates JS helpers for dart:core. This used to be in a file "core.js". | 6 * Generates JS helpers for dart:core. This used to be in a file "core.js". |
| 7 * Having them in Dart code means we can easily control which are generated. | 7 * Having them in Dart code means we can easily control which are generated. |
| 8 */ | 8 */ |
| 9 // TODO(jmesserly): one idea to make this cleaner: put these as private "native" | 9 // TODO(jmesserly): one idea to make this cleaner: put these as private "native" |
| 10 // methods somewhere in a library that we import. This would be rather elegant | 10 // methods somewhere in a library that we import. This would be rather elegant |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 // here. | 207 // here. |
| 208 w.writeln('\$defProp(Object.prototype, "get\$typeName", ' + | 208 w.writeln('\$defProp(Object.prototype, "get\$typeName", ' + |
| 209 'Object.prototype.\$typeNameOf);'); | 209 'Object.prototype.\$typeNameOf);'); |
| 210 } | 210 } |
| 211 } | 211 } |
| 212 } | 212 } |
| 213 | 213 |
| 214 | 214 |
| 215 /** Snippet for `$ne`. */ | 215 /** Snippet for `$ne`. */ |
| 216 final String _NE_FUNCTION = @""" | 216 final String _NE_FUNCTION = @""" |
| 217 function $$ne(x, y) { | 217 function $ne$(x, y) { |
| 218 if (x == null) return y != null; | 218 if (x == null) return y != null; |
| 219 return (typeof(x) != 'object') ? x !== y : !x.$eq(y); | 219 return (typeof(x) != 'object') ? x !== y : !x.$eq(y); |
| 220 }"""; | 220 }"""; |
| 221 | 221 |
| 222 /** Snippet for `$eq`. */ | 222 /** Snippet for `$eq`. */ |
| 223 final String _EQ_FUNCTION = @""" | 223 final String _EQ_FUNCTION = @""" |
| 224 function $$eq(x, y) { | 224 function $eq$(x, y) { |
| 225 if (x == null) return y == null; | 225 if (x == null) return y == null; |
| 226 return (typeof(x) != 'object') ? x === y : x.$eq(y); | 226 return (typeof(x) != 'object') ? x === y : x.$eq(y); |
| 227 } | 227 } |
| 228 // TODO(jimhug): Should this or should it not match equals? | 228 // TODO(jimhug): Should this or should it not match equals? |
| 229 $defProp(Object.prototype, '$eq', function(other) { | 229 $defProp(Object.prototype, '$eq', function(other) { |
| 230 return this === other; | 230 return this === other; |
| 231 });"""; | 231 });"""; |
| 232 | 232 |
| 233 /** Snippet for `$bit_not`. */ | 233 /** Snippet for `$bit_not`. */ |
| 234 final String _BIT_NOT_FUNCTION = @""" | 234 final String _BIT_NOT_FUNCTION = @""" |
| 235 function $$bit_not(x) { | 235 function $bit_not$(x) { |
| 236 if (typeof(x) == 'number') return ~x; | 236 if (typeof(x) == 'number') return ~x; |
| 237 if (typeof(x) == 'object') return x.$bit_not(); | 237 if (typeof(x) == 'object') return x.$bit_not(); |
| 238 $throw(new NoSuchMethodException(x, "operator ~", [])); | 238 $throw(new NoSuchMethodException(x, "operator ~", [])); |
| 239 }"""; | 239 }"""; |
| 240 | 240 |
| 241 /** Snippet for `$negate`. */ | 241 /** Snippet for `$negate`. */ |
| 242 final String _NEGATE_FUNCTION = @""" | 242 final String _NEGATE_FUNCTION = @""" |
| 243 function $$negate(x) { | 243 function $negate$(x) { |
| 244 if (typeof(x) == 'number') return -x; | 244 if (typeof(x) == 'number') return -x; |
| 245 if (typeof(x) == 'object') return x.$negate(); | 245 if (typeof(x) == 'object') return x.$negate(); |
| 246 $throw(new NoSuchMethodException(x, "operator negate", [])); | 246 $throw(new NoSuchMethodException(x, "operator negate", [])); |
| 247 }"""; | 247 }"""; |
| 248 | 248 |
| 249 /** Snippet for `$add`. This relies on JS's string "+" to match Dart's. */ | 249 /** Snippet for `$add`. This relies on JS's string "+" to match Dart's. */ |
| 250 final String _ADD_FUNCTION = @""" | 250 final String _ADD_FUNCTION = @""" |
| 251 function $$add$complex(x, y) { | 251 function $add$complex$(x, y) { |
| 252 if (typeof(x) == 'number') { | 252 if (typeof(x) == 'number') { |
| 253 $throw(new IllegalArgumentException(y)); | 253 $throw(new IllegalArgumentException(y)); |
| 254 } else if (typeof(x) == 'string') { | 254 } else if (typeof(x) == 'string') { |
| 255 var str = (y == null) ? 'null' : y.toString(); | 255 var str = (y == null) ? 'null' : y.toString(); |
| 256 if (typeof(str) != 'string') { | 256 if (typeof(str) != 'string') { |
| 257 throw new Error("calling toString() on right hand operand of operator " + | 257 throw new Error("calling toString() on right hand operand of operator " + |
| 258 "+ did not return a String"); | 258 "+ did not return a String"); |
| 259 } | 259 } |
| 260 return x + str; | 260 return x + str; |
| 261 } else if (typeof(x) == 'object') { | 261 } else if (typeof(x) == 'object') { |
| 262 return x.$add(y); | 262 return x.$add(y); |
| 263 } else { | 263 } else { |
| 264 $throw(new NoSuchMethodException(x, "operator +", [y])); | 264 $throw(new NoSuchMethodException(x, "operator +", [y])); |
| 265 } | 265 } |
| 266 } | 266 } |
| 267 | 267 |
| 268 function $$add(x, y) { | 268 function $add$(x, y) { |
| 269 if (typeof(x) == 'number' && typeof(y) == 'number') return x + y; | 269 if (typeof(x) == 'number' && typeof(y) == 'number') return x + y; |
| 270 return $$add$complex(x, y); | 270 return $add$complex$(x, y); |
| 271 }"""; | 271 }"""; |
| 272 | 272 |
| 273 /** Snippet for `$truncdiv`. This uses `$throw`. */ | 273 /** Snippet for `$truncdiv`. This uses `$throw`. */ |
| 274 final String _TRUNCDIV_FUNCTION = @""" | 274 final String _TRUNCDIV_FUNCTION = @""" |
| 275 function $$truncdiv(x, y) { | 275 function $truncdiv$(x, y) { |
| 276 if (typeof(x) == 'number') { | 276 if (typeof(x) == 'number') { |
| 277 if (typeof(y) == 'number') { | 277 if (typeof(y) == 'number') { |
| 278 if (y == 0) $throw(new IntegerDivisionByZeroException()); | 278 if (y == 0) $throw(new IntegerDivisionByZeroException()); |
| 279 var tmp = x / y; | 279 var tmp = x / y; |
| 280 return (tmp < 0) ? Math.ceil(tmp) : Math.floor(tmp); | 280 return (tmp < 0) ? Math.ceil(tmp) : Math.floor(tmp); |
| 281 } else { | 281 } else { |
| 282 $throw(new IllegalArgumentException(y)); | 282 $throw(new IllegalArgumentException(y)); |
| 283 } | 283 } |
| 284 } else if (typeof(x) == 'object') { | 284 } else if (typeof(x) == 'object') { |
| 285 return x.$truncdiv(y); | 285 return x.$truncdiv(y); |
| 286 } else { | 286 } else { |
| 287 $throw(new NoSuchMethodException(x, "operator ~/", [y])); | 287 $throw(new NoSuchMethodException(x, "operator ~/", [y])); |
| 288 } | 288 } |
| 289 }"""; | 289 }"""; |
| 290 | 290 |
| 291 /** Snippet for `$mod`. */ | 291 /** Snippet for `$mod`. */ |
| 292 final String _MOD_FUNCTION = @""" | 292 final String _MOD_FUNCTION = @""" |
| 293 function $$mod(x, y) { | 293 function $mod$(x, y) { |
| 294 if (typeof(x) == 'number') { | 294 if (typeof(x) == 'number') { |
| 295 if (typeof(y) == 'number') { | 295 if (typeof(y) == 'number') { |
| 296 var result = x % y; | 296 var result = x % y; |
| 297 if (result == 0) { | 297 if (result == 0) { |
| 298 return 0; // Make sure we don't return -0.0. | 298 return 0; // Make sure we don't return -0.0. |
| 299 } else if (result < 0) { | 299 } else if (result < 0) { |
| 300 if (y < 0) { | 300 if (y < 0) { |
| 301 return result - y; | 301 return result - y; |
| 302 } else { | 302 } else { |
| 303 return result + y; | 303 return result + y; |
| 304 } | 304 } |
| 305 } | 305 } |
| 306 return result; | 306 return result; |
| 307 } else { | 307 } else { |
| 308 $throw(new IllegalArgumentException(y)); | 308 $throw(new IllegalArgumentException(y)); |
| 309 } | 309 } |
| 310 } else if (typeof(x) == 'object') { | 310 } else if (typeof(x) == 'object') { |
| 311 return x.$mod(y); | 311 return x.$mod(y); |
| 312 } else { | 312 } else { |
| 313 $throw(new NoSuchMethodException(x, "operator %", [y])); | 313 $throw(new NoSuchMethodException(x, "operator %", [y])); |
| 314 } | 314 } |
| 315 }"""; | 315 }"""; |
| 316 | 316 |
| 317 /** Code snippet for all other operators. */ | 317 /** Code snippet for all other operators. */ |
| 318 String _otherOperator(String jsname, String op) { | 318 String _otherOperator(String jsname, String op) { |
| 319 return """ | 319 return """ |
| 320 function \$$jsname\$complex(x, y) { | 320 function $jsname\$complex\$(x, y) { |
| 321 if (typeof(x) == 'number') { | 321 if (typeof(x) == 'number') { |
| 322 \$throw(new IllegalArgumentException(y)); | 322 \$throw(new IllegalArgumentException(y)); |
| 323 } else if (typeof(x) == 'object') { | 323 } else if (typeof(x) == 'object') { |
| 324 return x.$jsname(y); | 324 return x.$jsname(y); |
| 325 } else { | 325 } else { |
| 326 \$throw(new NoSuchMethodException(x, "operator $op", [y])); | 326 \$throw(new NoSuchMethodException(x, "operator $op", [y])); |
| 327 } | 327 } |
| 328 } | 328 } |
| 329 function \$$jsname(x, y) { | 329 function $jsname\$(x, y) { |
| 330 if (typeof(x) == 'number' && typeof(y) == 'number') return x $op y; | 330 if (typeof(x) == 'number' && typeof(y) == 'number') return x $op y; |
| 331 return \$$jsname\$complex(x, y); | 331 return $jsname\$complex\$(x, y); |
| 332 }"""; | 332 }"""; |
| 333 } | 333 } |
| 334 | 334 |
| 335 /** | 335 /** |
| 336 * Snippet for `$dynamic`. Usage: | 336 * Snippet for `$dynamic`. Usage: |
| 337 * $dynamic(name).SomeTypeName = ... method ...; | 337 * $dynamic(name).SomeTypeName = ... method ...; |
| 338 * $dynamic(name).Object = ... noSuchMethod ...; | 338 * $dynamic(name).Object = ... noSuchMethod ...; |
| 339 */ | 339 */ |
| 340 final String _DYNAMIC_FUNCTION = @""" | 340 final String _DYNAMIC_FUNCTION = @""" |
| 341 function $dynamic(name) { | 341 function $dynamic(name) { |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 594 var newArgs = Array.prototype.slice.call(arguments); | 594 var newArgs = Array.prototype.slice.call(arguments); |
| 595 Array.prototype.unshift.apply(newArgs, boundArgs); | 595 Array.prototype.unshift.apply(newArgs, boundArgs); |
| 596 return func.apply(thisObj, newArgs); | 596 return func.apply(thisObj, newArgs); |
| 597 }; | 597 }; |
| 598 } else { | 598 } else { |
| 599 return function() { | 599 return function() { |
| 600 return func.apply(thisObj, arguments); | 600 return func.apply(thisObj, arguments); |
| 601 }; | 601 }; |
| 602 } | 602 } |
| 603 };"""; | 603 };"""; |
| OLD | NEW |