Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(314)

Side by Side Diff: dart/frog/leg/lib/core.dart

Issue 9114053: Record names of constructors and use that to implement Object.toString. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Avoid constant folding Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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
297 282
298 bool isInt(var v) { 283 bool isInt(var v) {
299 return JS("bool", @"($0 | 0) === $1", v, v); 284 return JS("bool", @"($0 | 0) === $1", v, v);
300 } 285 }
301 286
302 class int {} 287 class int {}
303 class double {} 288 class double {}
304 class String {} 289 class String {}
305 class bool {} 290 class bool {}
306 class Object {} 291 class Object {
292 String toString() {
293 String name = JS('String', @'this.constructor.name');
294 if (name === null) {
295 name = JS('String', @'$0.match(/^\s*function\s*(\S*)\s*\(/)[1]',
296 JS('String', @'this.constructor.toString()'));
297 }
298 return "Instance of '$name'";
299 }
300 }
307 class List<T> { 301 class List<T> {
308 static void _checkConstructorInput(n) { 302 static void _checkConstructorInput(n) {
309 // TODO(ngeoffray): Inline once we support optional parameters or 303 // TODO(ngeoffray): Inline once we support optional parameters or
310 // bailout. 304 // bailout.
311 if (!isInt(n)) throw "Invalid argument"; 305 if (!isInt(n)) throw "Invalid argument";
312 if (n < 0) throw "Negative size"; 306 if (n < 0) throw "Negative size";
313 } 307 }
314 308
315 factory List(n) { 309 factory List(n) {
316 // TODO(ngeoffray): Adjust to optional parameters. 310 // TODO(ngeoffray): Adjust to optional parameters.
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 } else { 374 } else {
381 return 375 return
382 JS("num", @"Math.floor($0 + (Date.now() - $1))", elapsedMs, startMs); 376 JS("num", @"Math.floor($0 + (Date.now() - $1))", elapsedMs, startMs);
383 } 377 }
384 } 378 }
385 379
386 int frequency() { 380 int frequency() {
387 return 1000; 381 return 1000;
388 } 382 }
389 } 383 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698