Chromium Code Reviews| 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 if (JS("bool", @"typeof $0 === 'string'", b) || |
|
floitsch
2012/01/11 11:52:40
as discussed we should be able to just call b.toSt
| |
| 52 JS("bool", @"typeof $0 === 'boolean'", b)) { | 52 JS("bool", @"typeof $0 === 'boolean'", b)) { |
| 53 return JS("String", @"$0 + $1", a, b); | 53 return JS("String", @"$0 + $1", a, b); |
| 54 } | 54 } |
| 55 if (JS("bool", @"typeof $0 === 'number'", b)) { | 55 if (JS("bool", @"typeof $0 === 'number'", b)) { |
| 56 if (JS("bool", @"$0 === 0 && (1/$0) < 0", b)) { | 56 if (JS("bool", @"$0 === 0 && (1/$0) < 0", b)) { |
| 57 return JS("String", @"$0 + '-0.0'", a); | 57 return JS("String", @"$0 + '-0.0'", a); |
| 58 } | 58 } |
| 59 return JS("String", @"$0 + $1", a, b); | 59 return JS("String", @"$0 + $1", a, b); |
| 60 } | 60 } |
| 61 if (b === null) return JS("String", @"$0 + 'null'", a); | 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. | 62 // No further native values. |
| 66 b = UNINTERCEPTED(b.toString()); | 63 b = b.toString(); |
| 67 if (JS("bool", @"typeof $0 === 'string'", b)) { | 64 if (JS("bool", @"typeof $0 === 'string'", b)) { |
| 68 return JS("String", @"$0 + $1", a, b); | 65 return JS("String", @"$0 + $1", a, b); |
| 69 } | 66 } |
| 70 // The following line is too long, but we can't break it using the + | 67 // The following line is too long, but we can't break it using the + |
| 71 // operator, since that's what we are defining here. | 68 // operator, since that's what we are defining here. |
| 72 throw "calling toString() on right hand operand of operator + did not return a String"; | 69 throw "calling toString() on right hand operand of operator + did not return a String"; |
| 73 } | 70 } |
| 74 throw "Unimplemented user-defined +."; | 71 throw "Unimplemented user-defined +."; |
| 75 } | 72 } |
| 76 | 73 |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 276 if (JS("bool", @"typeof $0 === 'string'", receiver) || isJSArray(receiver)) { | 273 if (JS("bool", @"typeof $0 === 'string'", receiver) || isJSArray(receiver)) { |
| 277 return JS("num", @"$0.length", receiver); | 274 return JS("num", @"$0.length", receiver); |
| 278 } | 275 } |
| 279 throw "Unimplemented user-defined length."; | 276 throw "Unimplemented user-defined length."; |
| 280 } | 277 } |
| 281 | 278 |
| 282 | 279 |
| 283 builtin$toString$0(var value) { | 280 builtin$toString$0(var value) { |
| 284 if (JS("bool", @"typeof $0 == 'object'", value)) { | 281 if (JS("bool", @"typeof $0 == 'object'", value)) { |
| 285 if (isJSArray(value)) { | 282 if (isJSArray(value)) { |
| 286 return "Instance of List"; | 283 return "Instance of 'List'"; |
| 287 } | 284 } |
| 288 return UNINTERCEPTED(value.toString()); | 285 return UNINTERCEPTED(value.toString()); |
| 289 } | 286 } |
| 290 if (JS("bool", @"$0 === 0 && (1 / $0) < 0", value)) { | 287 if (JS("bool", @"$0 === 0 && (1 / $0) < 0", value)) { |
| 291 return "-0.0"; | 288 return "-0.0"; |
| 292 } | 289 } |
| 293 if (value === null) return "null"; | 290 if (value === null) return "null"; |
| 294 return JS("string", @"String($0)", value); | 291 return JS("string", @"String($0)", value); |
| 295 } | 292 } |
| 296 | 293 |
| 297 | 294 |
| 298 bool isInt(var v) { | 295 bool isInt(var v) { |
| 299 return JS("bool", @"($0 | 0) === $1", v, v); | 296 return JS("bool", @"($0 | 0) === $1", v, v); |
| 300 } | 297 } |
| 301 | 298 |
| 302 class int {} | 299 class int {} |
| 303 class double {} | 300 class double {} |
| 304 class String {} | 301 class String {} |
| 305 class bool {} | 302 class bool {} |
| 306 class Object {} | 303 class Object { |
| 304 String toString() { | |
| 305 String name = JS('String', @'this.constructor.name'); | |
| 306 if (name === null) { | |
| 307 name = JS('String', @'$0.match(/^\s*function\s*(\S*)\s*\(/)[1]', | |
| 308 JS('String', @'this.constructor.toString()')); | |
| 309 } | |
| 310 return "Instance of '$name'"; | |
| 311 } | |
| 312 } | |
| 307 class List<T> { | 313 class List<T> { |
| 308 static void _checkConstructorInput(n) { | 314 static void _checkConstructorInput(n) { |
| 309 // TODO(ngeoffray): Inline once we support optional parameters or | 315 // TODO(ngeoffray): Inline once we support optional parameters or |
| 310 // bailout. | 316 // bailout. |
| 311 if (!isInt(n)) throw "Invalid argument"; | 317 if (!isInt(n)) throw "Invalid argument"; |
| 312 if (n < 0) throw "Negative size"; | 318 if (n < 0) throw "Negative size"; |
| 313 } | 319 } |
| 314 | 320 |
| 315 factory List(n) { | 321 factory List(n) { |
| 316 // TODO(ngeoffray): Adjust to optional parameters. | 322 // TODO(ngeoffray): Adjust to optional parameters. |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 380 } else { | 386 } else { |
| 381 return | 387 return |
| 382 JS("num", @"Math.floor($0 + (Date.now() - $1))", elapsedMs, startMs); | 388 JS("num", @"Math.floor($0 + (Date.now() - $1))", elapsedMs, startMs); |
| 383 } | 389 } |
| 384 } | 390 } |
| 385 | 391 |
| 386 int frequency() { | 392 int frequency() { |
| 387 return 1000; | 393 return 1000; |
| 388 } | 394 } |
| 389 } | 395 } |
| OLD | NEW |