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

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

Issue 9368033: Support is checks on primitive types. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 8 years, 10 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
« no previous file with comments | « no previous file | dart/frog/leg/resolver.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 10 matching lines...) Expand all
21 } 21 }
22 throw e; 22 throw e;
23 } 23 }
24 24
25 /** 25 /**
26 * Returns true if both arguments are numbers. 26 * Returns true if both arguments are numbers.
27 * If only the first argument is a number, throws the given message as 27 * If only the first argument is a number, throws the given message as
28 * exception. 28 * exception.
29 */ 29 */
30 bool checkNumbers(var a, var b, var message) { 30 bool checkNumbers(var a, var b, var message) {
31 if (JS("bool", @"typeof $0 === 'number'", a)) { 31 if (a is num) {
32 if (JS("bool", @"typeof $0 === 'number'", b)) { 32 if (b is num) {
33 return true; 33 return true;
34 } else { 34 } else {
35 throw message; 35 throw message;
36 } 36 }
37 } 37 }
38 return false; 38 return false;
39 } 39 }
40 40
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 (a is String) {
51 b = b.toString(); 51 b = b.toString();
52 if (JS("bool", @"typeof $0 === 'string'", b)) { 52 if (b is String) {
53 return JS("String", @"$0 + $1", a, b); 53 return JS("String", @"$0 + $1", a, b);
54 } 54 }
55 // 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 +
56 // operator, since that's what we are defining here. 56 // operator, since that's what we are defining here.
57 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";
58 } 58 }
59 return UNINTERCEPTED(a + b); 59 return UNINTERCEPTED(a + b);
60 } 60 }
61 61
62 div(var a, var b) { 62 div(var a, var b) {
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 if (JS("bool", @"typeof $0 === 'number'", a)) return JS("num", @"~$0", a); 209 if (JS("bool", @"typeof $0 === 'number'", a)) return JS("num", @"~$0", a);
210 return UNINTERCEPTED(~a); 210 return UNINTERCEPTED(~a);
211 } 211 }
212 212
213 neg(var a) { 213 neg(var a) {
214 if (JS("bool", @"typeof $0 === 'number'", a)) return JS("num", @"-$0", a); 214 if (JS("bool", @"typeof $0 === 'number'", a)) return JS("num", @"-$0", a);
215 return UNINTERCEPTED(-a); 215 return UNINTERCEPTED(-a);
216 } 216 }
217 217
218 index(var a, var index) { 218 index(var a, var index) {
219 if (JS("bool", @"typeof $0 === 'string'", a) || isJSArray(a)) { 219 if (a is String || isJSArray(a)) {
220 if (!isInt(index)) $throw('Illegal argument'); 220 if (!(index is int)) $throw('Illegal argument');
221 if (index < 0 || index >= a.length) $throw('Out of bounds'); 221 if (index < 0 || index >= a.length) $throw('Out of bounds');
222 return JS("Object", @"$0[$1]", a, index); 222 return JS("Object", @"$0[$1]", a, index);
223 } 223 }
224 return UNINTERCEPTED(a[index]); 224 return UNINTERCEPTED(a[index]);
225 } 225 }
226 226
227 indexSet(var a, var index, var value) { 227 indexSet(var a, var index, var value) {
228 if (isJSArray(a)) { 228 if (isJSArray(a)) {
229 if (!isInt(index)) $throw('Illegal argument'); 229 if (!(index is int)) $throw('Illegal argument');
230 if (index < 0 || index >= a.length) $throw('Out of bounds'); 230 if (index < 0 || index >= a.length) $throw('Out of bounds');
231 return JS("Object", @"$0[$1] = $2", a, index, value); 231 return JS("Object", @"$0[$1] = $2", a, index, value);
232 } 232 }
233 return UNINTERCEPTED(a[index] = value); 233 return UNINTERCEPTED(a[index] = value);
234 } 234 }
235 235
236 builtin$add$1(var receiver, var value) { 236 builtin$add$1(var receiver, var value) {
237 if (isJSArray(receiver)) { 237 if (isJSArray(receiver)) {
238 JS("Object", @"$0.push($1)", receiver, value); 238 JS("Object", @"$0.push($1)", receiver, value);
239 return; 239 return;
(...skipping 11 matching lines...) Expand all
251 builtin$filter$1(var receiver, var predicate) { 251 builtin$filter$1(var receiver, var predicate) {
252 if (isJSArray(receiver)) { 252 if (isJSArray(receiver)) {
253 return JS("Object", @"$0.filter(function(v) { return $1(v) === true; })", 253 return JS("Object", @"$0.filter(function(v) { return $1(v) === true; })",
254 receiver, predicate); 254 receiver, predicate);
255 } 255 }
256 return UNINTERCEPTED(receiver.filter(predicate)); 256 return UNINTERCEPTED(receiver.filter(predicate));
257 } 257 }
258 258
259 259
260 builtin$get$length(var receiver) { 260 builtin$get$length(var receiver) {
261 if (JS("bool", @"typeof $0 === 'string'", receiver) || isJSArray(receiver)) { 261 if (receiver is String || isJSArray(receiver)) {
262 return JS("num", @"$0.length", receiver); 262 return JS("num", @"$0.length", receiver);
263 } 263 }
264 return UNINTERCEPTED(receiver.length); 264 return UNINTERCEPTED(receiver.length);
265 } 265 }
266 266
267 267
268 builtin$toString$0(var value) { 268 builtin$toString$0(var value) {
269 if (JS("bool", @"typeof $0 == 'object'", value)) { 269 if (JS("bool", @"typeof $0 == 'object'", value)) {
270 if (isJSArray(value)) { 270 if (isJSArray(value)) {
271 return "Instance of 'List'"; 271 return "Instance of 'List'";
272 } 272 }
273 return UNINTERCEPTED(value.toString()); 273 return UNINTERCEPTED(value.toString());
274 } 274 }
275 if (JS("bool", @"$0 === 0 && (1 / $0) < 0", value)) { 275 if (JS("bool", @"$0 === 0 && (1 / $0) < 0", value)) {
276 return "-0.0"; 276 return "-0.0";
277 } 277 }
278 if (value === null) return "null"; 278 if (value === null) return "null";
279 return JS("string", @"String($0)", value); 279 return JS("string", @"String($0)", value);
280 } 280 }
281 281
282 282
283 builtin$iterator$0(var receiver) { 283 builtin$iterator$0(var receiver) {
284 if (isJSArray(receiver)) { 284 if (isJSArray(receiver)) {
285 return new ListIterator(receiver); 285 return new ListIterator(receiver);
286 } 286 }
287 return UNINTERCEPTED(receiver.iterator()); 287 return UNINTERCEPTED(receiver.iterator());
288 } 288 }
289 289
290 builtin$charCodeAt$1(var receiver, int index) { 290 builtin$charCodeAt$1(var receiver, int index) {
291 if (JS("bool", @"typeof receiver === 'string'")) { 291 if (receiver is String) {
292 return JS("string", @"$0.charCodeAt($1)", receiver, index); 292 return JS("string", @"$0.charCodeAt($1)", receiver, index);
293 } else { 293 } else {
294 return UNINTERCEPTED(receiver.charCodeAt(index)); 294 return UNINTERCEPTED(receiver.charCodeAt(index));
295 } 295 }
296 } 296 }
297 297
298 builtin$isEmpty$0(var receiver) { 298 builtin$isEmpty$0(var receiver) {
299 if (JS("bool", @"typeof $0 === 'string'", receiver) || isJSArray(receiver)) { 299 if (receiver is String || isJSArray(receiver)) {
300 return JS("bool", @"$0.length === 0", receiver); 300 return JS("bool", @"$0.length === 0", receiver);
301 } 301 }
302 return UNINTERCEPTED(receiver.isEmpty()); 302 return UNINTERCEPTED(receiver.isEmpty());
303 } 303 }
304 304
305 305
306 bool isInt(var v) {
307 return JS("bool", @"($0 | 0) === $1", v, v);
308 }
309
310 /* Include when interfaces are implemented 306 /* Include when interfaces are implemented
311 interface Iterable<T> { 307 interface Iterable<T> {
312 Iterator<T> iterator(); 308 Iterator<T> iterator();
313 } 309 }
314 310
315 interface Iterator<T> { 311 interface Iterator<T> {
316 bool hasNext(); 312 bool hasNext();
317 T next(); 313 T next();
318 } 314 }
319 */ 315 */
320 class int {} 316 class int {}
321 class double {} 317 class double {}
322 class String {} 318 class String {}
323 class bool {} 319 class bool {}
320 class num {}
324 class Object { 321 class Object {
325 String toString() { 322 String toString() {
326 String name = JS('String', @'this.constructor.name'); 323 String name = JS('String', @'this.constructor.name');
327 if (name === null) { 324 if (name === null) {
328 name = JS('String', @'$0.match(/^\s*function\s*(\S*)\s*\(/)[1]', 325 name = JS('String', @'$0.match(/^\s*function\s*(\S*)\s*\(/)[1]',
329 JS('String', @'this.constructor.toString()')); 326 JS('String', @'this.constructor.toString()'));
330 } 327 }
331 return "Instance of '$name'"; 328 return "Instance of '$name'";
332 } 329 }
333 330
(...skipping 10 matching lines...) Expand all
344 String toString() { 341 String toString() {
345 return "NoSuchMethodException - receiver: '$receiver'" + 342 return "NoSuchMethodException - receiver: '$receiver'" +
346 "function name: '$name' arguments: [$args]"; 343 "function name: '$name' arguments: [$args]";
347 } 344 }
348 } 345 }
349 346
350 class List<T> /* implements Iterable<T> */ { 347 class List<T> /* implements Iterable<T> */ {
351 348
352 factory List([int length]) { 349 factory List([int length]) {
353 if (length == null) return JS("Object", @"new Array()"); 350 if (length == null) return JS("Object", @"new Array()");
354 if (!isInt(length)) throw "Invalid argument"; 351 if (!(length is int)) throw "Invalid argument";
355 if (length < 0) throw "Negative size"; 352 if (length < 0) throw "Negative size";
356 return JS("Object", @"new Array($0)", length); 353 return JS("Object", @"new Array($0)", length);
357 } 354 }
358 } 355 }
359 356
360 class ListIterator<T> /* implements Iterator<T> */ { 357 class ListIterator<T> /* implements Iterator<T> */ {
361 int i; 358 int i;
362 List<T> list; 359 List<T> list;
363 ListIterator(List<T> this.list) : i = 0; 360 ListIterator(List<T> this.list) : i = 0;
364 bool hasNext() => i < JS("int", @"$0.length", list); 361 bool hasNext() => i < JS("int", @"$0.length", list);
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 } else { 447 } else {
451 return 448 return
452 JS("num", @"Math.floor($0 + (Date.now() - $1))", elapsedMs, startMs); 449 JS("num", @"Math.floor($0 + (Date.now() - $1))", elapsedMs, startMs);
453 } 450 }
454 } 451 }
455 452
456 int frequency() { 453 int frequency() {
457 return 1000; 454 return 1000;
458 } 455 }
459 } 456 }
OLDNEW
« no previous file with comments | « no previous file | dart/frog/leg/resolver.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698