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

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

Issue 9373043: Move helpers and interceptors to own library. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Add TODO 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 | « dart/frog/leg/lib/coreimpl.dart ('k') | dart/frog/leg/scanner/scanner_task.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) 2012, 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 #library('js_helper');
6 obj = obj.toString(); 6
7 var hasConsole = JS("bool", @"typeof console == 'object'"); 7 #import('core.dart');
8 if (hasConsole) {
9 JS("void", @"console.log($0)", obj);
10 } else {
11 JS("void", @"write($0)", obj);
12 JS("void", @"write('\n')");
13 }
14 }
15 8
16 $throw(String msg) { 9 $throw(String msg) {
17 var e = JS("Object", @"new Error($0)", msg); 10 var e = JS("Object", @"new Error($0)", msg);
18 var hasTrace = JS("bool", @"Error.captureStackTrace !== (void 0)"); 11 var hasTrace = JS("bool", @"Error.captureStackTrace !== (void 0)");
19 if (hasTrace) { 12 if (hasTrace) {
20 JS("void", @"Error.captureStackTrace($0)", e); 13 JS("void", @"Error.captureStackTrace($0)", e);
21 } 14 }
22 throw e; 15 throw e;
23 } 16 }
24 17
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 if (JS("bool", @"typeof $0 === 'string'", receiver) || isJSArray(receiver)) { 292 if (JS("bool", @"typeof $0 === 'string'", receiver) || isJSArray(receiver)) {
300 return JS("bool", @"$0.length === 0", receiver); 293 return JS("bool", @"$0.length === 0", receiver);
301 } 294 }
302 return UNINTERCEPTED(receiver.isEmpty()); 295 return UNINTERCEPTED(receiver.isEmpty());
303 } 296 }
304 297
305 298
306 bool isInt(var v) { 299 bool isInt(var v) {
307 return JS("bool", @"($0 | 0) === $1", v, v); 300 return JS("bool", @"($0 | 0) === $1", v, v);
308 } 301 }
309
310 /* Include when interfaces are implemented
311 interface Iterable<T> {
312 Iterator<T> iterator();
313 }
314
315 interface Iterator<T> {
316 bool hasNext();
317 T next();
318 }
319 */
320 class int {}
321 class double {}
322 class String {}
323 class bool {}
324 class Object {
325 String toString() {
326 String name = JS('String', @'this.constructor.name');
327 if (name === null) {
328 name = JS('String', @'$0.match(/^\s*function\s*(\S*)\s*\(/)[1]',
329 JS('String', @'this.constructor.toString()'));
330 }
331 return "Instance of '$name'";
332 }
333
334 void noSuchMethod(String name, List args) {
335 throw new NoSuchMethodException(this, name, args);
336 }
337 }
338
339 class NoSuchMethodException {
340 Object receiver;
341 String name;
342 List args;
343 NoSuchMethodException(Object this.receiver, String this.name, List this.args);
344 String toString() {
345 return "NoSuchMethodException - receiver: '$receiver'" +
346 "function name: '$name' arguments: [$args]";
347 }
348 }
349
350 class List<T> /* implements Iterable<T> */ {
351
352 factory List([int length]) {
353 if (length == null) return JS("Object", @"new Array()");
354 if (!isInt(length)) throw "Invalid argument";
355 if (length < 0) throw "Negative size";
356 return JS("Object", @"new Array($0)", length);
357 }
358 }
359
360 class ListIterator<T> /* implements Iterator<T> */ {
361 int i;
362 List<T> list;
363 ListIterator(List<T> this.list) : i = 0;
364 bool hasNext() => i < JS("int", @"$0.length", list);
365 T next() {
366 var value = JS("Object", @"$0[$1]", list, i);
367 i += 1;
368 return value;
369 }
370 }
371
372 class Expect {
373 // TODO(ngeoffray): add optional message parameters to these
374 // methods.
375 static void equals(var expected, var actual) {
376 if (expected == actual) return;
377 _fail('Expect.equals(expected: <$expected>, actual:<$actual> fails.');
378 }
379
380 static void fail(String message) {
381 _fail("Expect.fail('$message')");
382 }
383
384 static void _fail(String message) {
385 $throw(message);
386 }
387
388 static void isTrue(var actual) {
389 if (actual === true) return;
390 _fail("Expect.isTrue($actual) fails.");
391 }
392
393 static void isFalse(var actual) {
394 if (actual === false) return;
395 _fail("Expect.isFalse($actual) fails.");
396 }
397
398 static void listEquals(List expected, List actual) {
399 // We check on length at the end in order to provide better error
400 // messages when an unexpected item is inserted in a list.
401 if (expected.length != actual.length) {
402 _fail('list not equals');
403 }
404
405 for (int i = 0; i < expected.length; i++) {
406 if (expected[i] != actual[i]) {
407 _fail('list not equals');
408 }
409 }
410 }
411 }
412
413 class Stopwatch {
414 double startMs;
415 double elapsedMs;
416
417 Stopwatch() {
418 elapsedMs = 0.0;
419 }
420
421 void start() {
422 if (startMs == null) {
423 startMs = JS("num", @"Date.now()");
424 }
425 }
426
427 void stop() {
428 if (startMs == null) return;
429 elapsedMs += JS("num", @"Date.now() - $0", startMs);
430 startMs = null;
431 }
432
433 void reset() {
434 elapsedMs = 0.0;
435 if (startMs == null) return;
436 startMs = JS("num", @"Date.now()");
437 }
438
439 int elapsed() {
440 return elapsedInMs();
441 }
442
443 int elapsedInUs() {
444 return elapsedInMs() * 1000;
445 }
446
447 int elapsedInMs() {
448 if (startMs == null) {
449 return JS("num", @"Math.floor($0)", elapsedMs);
450 } else {
451 return
452 JS("num", @"Math.floor($0 + (Date.now() - $1))", elapsedMs, startMs);
453 }
454 }
455
456 int frequency() {
457 return 1000;
458 }
459 }
OLDNEW
« no previous file with comments | « dart/frog/leg/lib/coreimpl.dart ('k') | dart/frog/leg/scanner/scanner_task.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698