| 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 void print(var obj) { | 5 void print(var obj) { |
| 6 obj = obj.toString(); | 6 obj = obj.toString(); |
| 7 var hasConsole = JS("bool", @"typeof console == 'object'"); | 7 var hasConsole = JS("bool", @"typeof console == 'object'"); |
| 8 if (hasConsole) { | 8 if (hasConsole) { |
| 9 JS("void", @"console.log($0)", obj); | 9 JS("void", @"console.log($0)", obj); |
| 10 } else { | 10 } else { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 | 41 |
| 42 bool isJSArray(var value) { | 42 bool isJSArray(var value) { |
| 43 return JS("bool", @"$0.constructor === Array", value); | 43 return JS("bool", @"$0.constructor === Array", value); |
| 44 } | 44 } |
| 45 | 45 |
| 46 | 46 |
| 47 add(var a, var b) { | 47 add(var a, var b) { |
| 48 if (checkNumbers(a, b, "num+ expects a number as second operand.")) { | 48 if (checkNumbers(a, b, "num+ expects a number as second operand.")) { |
| 49 return JS("num", @"$0 + $1", a, b); | 49 return JS("num", @"$0 + $1", a, b); |
| 50 } else if (JS("bool", @"typeof $0 === 'string'", a)) { | 50 } else if (JS("bool", @"typeof $0 === 'string'", a)) { |
| 51 if (JS("bool", @"typeof $0 === 'string'", b) || | 51 b = b.toString(); |
| 52 JS("bool", @"typeof $0 === 'boolean'", b)) { | |
| 53 return JS("String", @"$0 + $1", a, b); | |
| 54 } | |
| 55 if (JS("bool", @"typeof $0 === 'number'", b)) { | |
| 56 if (JS("bool", @"$0 === 0 && (1/$0) < 0", b)) { | |
| 57 return JS("String", @"$0 + '-0.0'", a); | |
| 58 } | |
| 59 return JS("String", @"$0 + $1", a, b); | |
| 60 } | |
| 61 if (b === null) return JS("String", @"$0 + 'null'", a); | |
| 62 if (isJSArray(b)) { | |
| 63 return JS("String", @"$0 + 'Instance of \'List\''", a); | |
| 64 } | |
| 65 // No further native values. | |
| 66 b = UNINTERCEPTED(b.toString()); | |
| 67 if (JS("bool", @"typeof $0 === 'string'", b)) { | 52 if (JS("bool", @"typeof $0 === 'string'", b)) { |
| 68 return JS("String", @"$0 + $1", a, b); | 53 return JS("String", @"$0 + $1", a, b); |
| 69 } | 54 } |
| 70 // The following line is too long, but we can't break it using the + | 55 // The following line is too long, but we can't break it using the + |
| 71 // operator, since that's what we are defining here. | 56 // operator, since that's what we are defining here. |
| 72 throw "calling toString() on right hand operand of operator + did not return
a String"; | 57 throw "calling toString() on right hand operand of operator + did not return
a String"; |
| 73 } | 58 } |
| 74 throw "Unimplemented user-defined +."; | 59 throw "Unimplemented user-defined +."; |
| 75 } | 60 } |
| 76 | 61 |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 if (JS("bool", @"typeof $0 === 'string'", receiver) || isJSArray(receiver)) { | 261 if (JS("bool", @"typeof $0 === 'string'", receiver) || isJSArray(receiver)) { |
| 277 return JS("num", @"$0.length", receiver); | 262 return JS("num", @"$0.length", receiver); |
| 278 } | 263 } |
| 279 throw "Unimplemented user-defined length."; | 264 throw "Unimplemented user-defined length."; |
| 280 } | 265 } |
| 281 | 266 |
| 282 | 267 |
| 283 builtin$toString$0(var value) { | 268 builtin$toString$0(var value) { |
| 284 if (JS("bool", @"typeof $0 == 'object'", value)) { | 269 if (JS("bool", @"typeof $0 == 'object'", value)) { |
| 285 if (isJSArray(value)) { | 270 if (isJSArray(value)) { |
| 286 return "Instance of List"; | 271 return "Instance of 'List'"; |
| 287 } | 272 } |
| 288 return UNINTERCEPTED(value.toString()); | 273 return UNINTERCEPTED(value.toString()); |
| 289 } | 274 } |
| 290 if (JS("bool", @"$0 === 0 && (1 / $0) < 0", value)) { | 275 if (JS("bool", @"$0 === 0 && (1 / $0) < 0", value)) { |
| 291 return "-0.0"; | 276 return "-0.0"; |
| 292 } | 277 } |
| 293 if (value === null) return "null"; | 278 if (value === null) return "null"; |
| 294 return JS("string", @"String($0)", value); | 279 return JS("string", @"String($0)", value); |
| 295 } | 280 } |
| 296 | 281 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 314 | 299 |
| 315 interface Iterator<T> { | 300 interface Iterator<T> { |
| 316 bool hasNext(); | 301 bool hasNext(); |
| 317 T next(); | 302 T next(); |
| 318 } | 303 } |
| 319 */ | 304 */ |
| 320 class int {} | 305 class int {} |
| 321 class double {} | 306 class double {} |
| 322 class String {} | 307 class String {} |
| 323 class bool {} | 308 class bool {} |
| 324 class Object {} | 309 class Object { |
| 310 String toString() { |
| 311 String name = JS('String', @'this.constructor.name'); |
| 312 if (name === null) { |
| 313 name = JS('String', @'$0.match(/^\s*function\s*(\S*)\s*\(/)[1]', |
| 314 JS('String', @'this.constructor.toString()')); |
| 315 } |
| 316 return "Instance of '$name'"; |
| 317 } |
| 318 } |
| 325 class List<T> /* implements Iterable<T> */ { | 319 class List<T> /* implements Iterable<T> */ { |
| 320 |
| 326 static void _checkConstructorInput(n) { | 321 static void _checkConstructorInput(n) { |
| 327 // TODO(ngeoffray): Inline once we support optional parameters or | 322 // TODO(ngeoffray): Inline once we support optional parameters or |
| 328 // bailout. | 323 // bailout. |
| 329 if (!isInt(n)) throw "Invalid argument"; | 324 if (!isInt(n)) throw "Invalid argument"; |
| 330 if (n < 0) throw "Negative size"; | 325 if (n < 0) throw "Negative size"; |
| 331 } | 326 } |
| 332 | 327 |
| 333 factory List(n) { | 328 factory List(n) { |
| 334 // TODO(ngeoffray): Adjust to optional parameters. | 329 // TODO(ngeoffray): Adjust to optional parameters. |
| 335 if (JS("bool", @"$0 === (void 0)", n)) return JS("Object", @"new Array()"); | 330 if (JS("bool", @"$0 === (void 0)", n)) return JS("Object", @"new Array()"); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 } else { | 405 } else { |
| 411 return | 406 return |
| 412 JS("num", @"Math.floor($0 + (Date.now() - $1))", elapsedMs, startMs); | 407 JS("num", @"Math.floor($0 + (Date.now() - $1))", elapsedMs, startMs); |
| 413 } | 408 } |
| 414 } | 409 } |
| 415 | 410 |
| 416 int frequency() { | 411 int frequency() { |
| 417 return 1000; | 412 return 1000; |
| 418 } | 413 } |
| 419 } | 414 } |
| OLD | NEW |