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

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

Issue 9423034: Implement most of String API. (Closed) Base URL: https://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 | « dart/frog/file_system_vm.dart ('k') | dart/frog/leg/lib/mockimpl.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) 2012, 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 #library('js_helper'); 5 #library('js_helper');
6 6
7 #import('coreimpl.dart'); 7 #import('coreimpl.dart');
8 8
9 /** 9 /**
10 * Returns true if both arguments are numbers. 10 * Returns true if both arguments are numbers.
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 } 193 }
194 194
195 neg(var a) { 195 neg(var a) {
196 if (JS("bool", @"typeof $0 === 'number'", a)) return JS("num", @"-$0", a); 196 if (JS("bool", @"typeof $0 === 'number'", a)) return JS("num", @"-$0", a);
197 return UNINTERCEPTED(-a); 197 return UNINTERCEPTED(-a);
198 } 198 }
199 199
200 index(var a, var index) { 200 index(var a, var index) {
201 checkNull(a); 201 checkNull(a);
202 if (a is String || isJSArray(a)) { 202 if (a is String || isJSArray(a)) {
203 if (!(index is int)) { 203 if (index is !int) {
204 throw new IllegalArgumentException(index); 204 if (index is !num) throw new IllegalArgumentException(index);
ngeoffray 2012/02/21 09:45:37 Why not checkNum?
205 if (index.truncate() !== index) throw new IllegalArgumentException(index);
ngeoffray 2012/02/21 09:45:37 What's this about?
205 } 206 }
206 if (index < 0 || index >= a.length) { 207 if (index < 0 || index >= a.length) {
207 throw new IndexOutOfRangeException(index); 208 throw new IndexOutOfRangeException(index);
208 } 209 }
209 return JS("Object", @"$0[$1]", a, index); 210 return JS("Object", @"$0[$1]", a, index);
210 } 211 }
211 return UNINTERCEPTED(a[index]); 212 return UNINTERCEPTED(a[index]);
212 } 213 }
213 214
214 indexSet(var a, var index, var value) { 215 indexSet(var a, var index, var value) {
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 bool hasNext() => i < JS("int", @"$0.length", list); 307 bool hasNext() => i < JS("int", @"$0.length", list);
307 T next() { 308 T next() {
308 if (!hasNext()) throw new NoMoreElementsException(); 309 if (!hasNext()) throw new NoMoreElementsException();
309 var value = JS("Object", @"$0[$1]", list, i); 310 var value = JS("Object", @"$0[$1]", list, i);
310 i += 1; 311 i += 1;
311 return value; 312 return value;
312 } 313 }
313 } 314 }
314 315
315 builtin$charCodeAt$1(var receiver, int index) { 316 builtin$charCodeAt$1(var receiver, int index) {
317 checkNull(receiver);
ngeoffray 2012/02/21 09:45:37 No need for check null
316 if (receiver is String) { 318 if (receiver is String) {
319 if (index is !num) throw new IllegalArgumentException(index);
ngeoffray 2012/02/21 09:45:37 Why not checkNum?
320 if (index < 0) throw new IndexOutOfRangeException(index);
321 if (index >= receiver.length) throw new IndexOutOfRangeException(index);
317 return JS("string", @"$0.charCodeAt($1)", receiver, index); 322 return JS("string", @"$0.charCodeAt($1)", receiver, index);
318 } else { 323 } else {
319 return UNINTERCEPTED(receiver.charCodeAt(index)); 324 return UNINTERCEPTED(receiver.charCodeAt(index));
320 } 325 }
321 } 326 }
322 327
323 builtin$isEmpty$0(var receiver) { 328 builtin$isEmpty$0(var receiver) {
324 checkNull(receiver); 329 checkNull(receiver);
325 if (receiver is String || isJSArray(receiver)) { 330 if (receiver is String || isJSArray(receiver)) {
326 return JS("bool", @"$0.length === 0", receiver); 331 return JS("bool", @"$0.length === 0", receiver);
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
387 static int getMinutes(int secondsSinceEpoch, bool isUtc) { 392 static int getMinutes(int secondsSinceEpoch, bool isUtc) {
388 throw 'Primitives.getMinutes is not implemented'; 393 throw 'Primitives.getMinutes is not implemented';
389 } 394 }
390 395
391 static int getSeconds(int secondsSinceEpoch, bool isUtc) { 396 static int getSeconds(int secondsSinceEpoch, bool isUtc) {
392 throw 'Primitives.getSeconds is not implemented'; 397 throw 'Primitives.getSeconds is not implemented';
393 } 398 }
394 399
395 static String stringFromCharCodes(charCodes) { 400 static String stringFromCharCodes(charCodes) {
396 for (var i in charCodes) { 401 for (var i in charCodes) {
397 checkNum(i); 402 if (i is !int) throw new IllegalArgumentException(i);
ngeoffray 2012/02/21 09:45:37 Why not checkInt?
398 } 403 }
399 return JS('String', @'String.fromCharCode.apply($0, $1)', null, charCodes); 404 return JS('String', @'String.fromCharCode.apply($0, $1)', null, charCodes);
400 } 405 }
401 } 406 }
402 407
403 builtin$compareTo$1(a, b) { 408 builtin$compareTo$1(a, b) {
404 checkNull(a); 409 checkNull(a);
405 if (checkNumbers(a, b, 'illegal argument')) { 410 if (checkNumbers(a, b, 'illegal argument')) {
406 if (a < b) { 411 if (a < b) {
407 return -1; 412 return -1;
(...skipping 10 matching lines...) Expand all
418 return 0; 423 return 0;
419 } else if (a.isNaN()) { 424 } else if (a.isNaN()) {
420 if (b.isNaN()) { 425 if (b.isNaN()) {
421 return 0; 426 return 0;
422 } 427 }
423 return 1; 428 return 1;
424 } else { 429 } else {
425 return -1; 430 return -1;
426 } 431 }
427 } else if (a is String) { 432 } else if (a is String) {
428 throw 'String.compareTo is not implemented'; 433 if (b is !String) throw new IllegalArgumentException(b);
ngeoffray 2012/02/21 09:45:37 checkString?
434 return (a === b) ? 0 : JS('bool', @'$0 < $1', a, b) ? -1 : 1;
kasperl 2012/02/20 10:44:51 Shouldn't this be (a == b)?
ahe 2012/02/20 16:31:58 Not sure. Perhaps I should use JS('bool', '$0 == $
429 } else { 435 } else {
430 return UNINTERCEPTED(a.compareTo(b)); 436 return UNINTERCEPTED(a.compareTo(b));
431 } 437 }
432 } 438 }
433 439
434 iae(argument) { 440 iae(argument) {
435 throw new IllegalArgumentException(argument); 441 throw new IllegalArgumentException(argument);
436 } 442 }
437 443
438 ioore(index) { 444 ioore(index) {
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
498 return UNINTERCEPTED(receiver.indexOf(element)); 504 return UNINTERCEPTED(receiver.indexOf(element));
499 } 505 }
500 506
501 builtin$indexOf$2(receiver, element, start) { 507 builtin$indexOf$2(receiver, element, start) {
502 checkNull(receiver); 508 checkNull(receiver);
503 if (isJSArray(receiver)) { 509 if (isJSArray(receiver)) {
504 if (start is !int) throw new IllegalArgumentException(start); 510 if (start is !int) throw new IllegalArgumentException(start);
505 var length = JS("num", @"$0.length", receiver); 511 var length = JS("num", @"$0.length", receiver);
506 return Arrays.indexOf(receiver, element, start, length); 512 return Arrays.indexOf(receiver, element, start, length);
507 } else if (receiver is String) { 513 } else if (receiver is String) {
508 throw new NotImplementedException(); 514 checkNull(element);
ngeoffray 2012/02/21 09:45:37 No need for check null
515 if (start is !int) throw new IllegalArgumentException(start);
ngeoffray 2012/02/21 09:45:37 checkInt, checkString?
516 if (element is !String) throw new IllegalArgumentException(element);
517 if (start < 0) return -1; // TODO(ahe): Is this correct?
518 return JS('int', @'$0.indexOf($1, $2)', receiver, element, start);
509 } 519 }
510 return UNINTERCEPTED(receiver.indexOf(element, start)); 520 return UNINTERCEPTED(receiver.indexOf(element, start));
511 } 521 }
512 522
513 builtin$insertRange$2(receiver, start, length) { 523 builtin$insertRange$2(receiver, start, length) {
514 checkNull(receiver); 524 checkNull(receiver);
515 if (isJSArray(receiver)) { 525 if (isJSArray(receiver)) {
516 return builtin$insertRange$3(receiver, start, length, null); 526 return builtin$insertRange$3(receiver, start, length, null);
517 } 527 }
518 return UNINTERCEPTED(receiver.insertRange(start, length)); 528 return UNINTERCEPTED(receiver.insertRange(start, length));
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
561 } 571 }
562 return receiver[receiver.length - 1]; 572 return receiver[receiver.length - 1];
563 } 573 }
564 574
565 builtin$lastIndexOf$1(receiver, element) { 575 builtin$lastIndexOf$1(receiver, element) {
566 checkNull(receiver); 576 checkNull(receiver);
567 if (isJSArray(receiver)) { 577 if (isJSArray(receiver)) {
568 var start = JS("num", @"$0.length", receiver); 578 var start = JS("num", @"$0.length", receiver);
569 return Arrays.lastIndexOf(receiver, element, start); 579 return Arrays.lastIndexOf(receiver, element, start);
570 } else if (receiver is String) { 580 } else if (receiver is String) {
571 throw new NotImplementedException(); 581 checkNull(element);
ngeoffray 2012/02/21 09:45:37 No need.
582 if (element is !String) throw new IllegalArgumentException(element);
583 return JS('int', @'$0.lastIndexOf($1)', receiver, element);
572 } 584 }
573 return UNINTERCEPTED(receiver.lastIndexOf(element)); 585 return UNINTERCEPTED(receiver.lastIndexOf(element));
574 } 586 }
575 587
576 builtin$lastIndexOf$2(receiver, element, start) { 588 builtin$lastIndexOf$2(receiver, element, start) {
577 checkNull(receiver); 589 checkNull(receiver);
578 if (isJSArray(receiver)) { 590 if (isJSArray(receiver)) {
579 return Arrays.lastIndexOf(receiver, element, start); 591 return Arrays.lastIndexOf(receiver, element, start);
580 } else if (receiver is String) { 592 } else if (receiver is String) {
581 throw new NotImplementedException(); 593 checkNull(element);
ngeoffray 2012/02/21 09:45:37 No need.
594 if (element is !String) throw new IllegalArgumentException(element);
595 if (start !== null) {
596 if (start is !num) throw new IllegalArgumentException(start);
597 if (start < 0) return -1;
598 if (start >= receiver.length) start = receiver.length - 1;
599 }
600 return rawStringLastIndexOf(receiver, element, start);
582 } 601 }
583 return UNINTERCEPTED(receiver.lastIndexOf(element, start)); 602 return UNINTERCEPTED(receiver.lastIndexOf(element, start));
584 } 603 }
585 604
605 rawStringLastIndexOf(receiver, element, start)
606 => JS('int', @'$0.lastIndexOf($1, $2)', receiver, element, start);
607
586 builtin$removeRange$2(receiver, start, length) { 608 builtin$removeRange$2(receiver, start, length) {
587 checkNull(receiver); 609 checkNull(receiver);
588 if (!isJSArray(receiver)) { 610 if (!isJSArray(receiver)) {
589 return UNINTERCEPTED(receiver.removeRange(start, length)); 611 return UNINTERCEPTED(receiver.removeRange(start, length));
590 } 612 }
591 if (length == 0) { 613 if (length == 0) {
592 return; 614 return;
593 } 615 }
594 checkNull(start); // TODO(ahe): This is not specified but co19 tests it. 616 checkNull(start); // TODO(ahe): This is not specified but co19 tests it.
595 checkNull(length); // TODO(ahe): This is not specified but co19 tests it. 617 checkNull(length); // TODO(ahe): This is not specified but co19 tests it.
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
801 } 823 }
802 checkNum(radix); 824 checkNum(radix);
803 825
804 return JS("String", @"$0.toString($1)", receiver, radix); 826 return JS("String", @"$0.toString($1)", receiver, radix);
805 } 827 }
806 828
807 builtin$allMatches$1(receiver, str) { 829 builtin$allMatches$1(receiver, str) {
808 checkNull(receiver); 830 checkNull(receiver);
809 if (receiver is !String) return UNINTERCEPTED(receiver.allMatches(str)); 831 if (receiver is !String) return UNINTERCEPTED(receiver.allMatches(str));
810 832
811 throw new NotImplementedException(); 833 throw 'String.allMatches is not implemented';
812 } 834 }
813 835
814 builtin$concat$1(receiver, other) { 836 builtin$concat$1(receiver, other) {
815 checkNull(receiver); 837 checkNull(receiver);
816 if (receiver is !String) return UNINTERCEPTED(receiver.concat(other)); 838 if (receiver is !String) return UNINTERCEPTED(receiver.concat(other));
817 839
818 throw new NotImplementedException(); 840 if (other is !String) throw new IllegalArgumentException(other);
841 return JS('String', @'$0.concat($1)', receiver, other);
819 } 842 }
820 843
821 builtin$contains$2(receiver, other, startIndex) { 844 builtin$contains$2(receiver, other, startIndex) {
822 checkNull(receiver); 845 checkNull(receiver);
823 if (receiver is !String) { 846 if (receiver is !String) {
824 return UNINTERCEPTED(receiver.contains(other, startIndex)); 847 return UNINTERCEPTED(receiver.contains(other, startIndex));
825 } 848 }
826 849 checkNull(other);
827 throw new NotImplementedException(); 850 if (other is !String) {
851 throw 'String.contains with non-String is not implemented';
852 }
853 if ((startIndex !== null) || (startIndex is !num)) {
854 throw new IllegalArgumentException(startIndex);
855 }
856 return receiver.indexOf(other, startIndex) >= 0;
828 } 857 }
829 858
830 builtin$endsWith$1(receiver, other) { 859 builtin$endsWith$1(receiver, other) {
831 checkNull(receiver); 860 checkNull(receiver);
832 if (receiver is !String) return UNINTERCEPTED(receiver.endsWith(other)); 861 if (receiver is !String) return UNINTERCEPTED(receiver.endsWith(other));
833 862
834 throw new NotImplementedException(); 863 checkNull(other);
864 if (other is !String) throw new IllegalArgumentException(other);
865
866 int receiverLength = receiver.length;
867 int otherLength = other.length;
868 if (otherLength > receiverLength) return false;
869 return other == receiver.substring(receiverLength - otherLength);
835 } 870 }
836 871
837 builtin$replaceAll$2(receiver, from, to) { 872 builtin$replaceAll$2(receiver, from, to) {
838 checkNull(receiver); 873 checkNull(receiver);
839 if (receiver is !String) return UNINTERCEPTED(receiver.replaceAll(from, to)); 874 if (receiver is !String) return UNINTERCEPTED(receiver.replaceAll(from, to));
840 875
841 throw new NotImplementedException(); 876 throw 'String.replaceAll is not implemented';
842 } 877 }
843 878
844 builtin$replaceFirst$2(receiver, from, to) { 879 builtin$replaceFirst$2(receiver, from, to) {
845 checkNull(receiver); 880 checkNull(receiver);
846 if (receiver is !String) { 881 if (receiver is !String) {
847 return UNINTERCEPTED(receiver.replaceFirst(from, to)); 882 return UNINTERCEPTED(receiver.replaceFirst(from, to));
848 } 883 }
884 if (from is !String) throw new IllegalArgumentException(from);
kasperl 2012/02/20 10:44:51 Duplicate line. Did you want to check to too?
ahe 2012/02/20 16:31:58 Done.
885 if (from is !String) throw new IllegalArgumentException(from);
849 886
850 throw new NotImplementedException(); 887 return JS('String', @'$0.replace($1, $2)', receiver, from, to);
851 } 888 }
852 889
853 builtin$split$1(receiver, pattern) { 890 builtin$split$1(receiver, pattern) {
854 checkNull(receiver); 891 checkNull(receiver);
855 if (receiver is !String) return UNINTERCEPTED(receiver.split(pattern)); 892 if (receiver is !String) return UNINTERCEPTED(receiver.split(pattern));
893 checkNull(pattern);
894 if (pattern is !String) throw new IllegalArgumentException(pattern);
856 895
857 throw new NotImplementedException(); 896 return JS('List', @'$0.split($1)', receiver, pattern);
858 } 897 }
859 898
860 builtin$splitChars$0(receiver) { 899 builtin$splitChars$0(receiver) {
861 checkNull(receiver); 900 checkNull(receiver);
862 if (receiver is !String) return UNINTERCEPTED(receiver.splitChars()); 901 if (receiver is !String) return UNINTERCEPTED(receiver.splitChars());
863 902
864 throw new NotImplementedException(); 903 return JS('List', @'$0.split("")', receiver);
865 } 904 }
866 905
867 builtin$startsWith$1(receiver, other) { 906 builtin$startsWith$1(receiver, other) {
868 checkNull(receiver); 907 checkNull(receiver);
869 if (receiver is !String) return UNINTERCEPTED(receiver.startsWith(other)); 908 if (receiver is !String) return UNINTERCEPTED(receiver.startsWith(other));
909 checkNull(other);
910 if (other is !String) throw new IllegalArgumentException(other);
870 911
871 throw new NotImplementedException(); 912 int length = other.length;
913 if (length > receiver.length) return false;
914 return other === JS('String', @'$0.substring(0, $1)', receiver, length);
kasperl 2012/02/20 10:44:51 == rather than ===
ahe 2012/02/20 16:31:58 Using JS.
872 } 915 }
873 916
874 builtin$substring$1(receiver, startIndex) { 917 builtin$substring$1(receiver, startIndex) {
875 checkNull(receiver); 918 checkNull(receiver);
876 if (receiver is !String) return UNINTERCEPTED(receiver.substring(startIndex)); 919 if (receiver is !String) return UNINTERCEPTED(receiver.substring(startIndex));
877 920
878 throw new NotImplementedException(); 921 return builtin$substring$2(receiver, startIndex, null);
879 } 922 }
880 923
881 builtin$substring$2(receiver, startIndex, endIndex) { 924 builtin$substring$2(receiver, startIndex, endIndex) {
882 checkNull(receiver); 925 checkNull(receiver);
883 if (receiver is !String) { 926 if (receiver is !String) {
884 return UNINTERCEPTED(receiver.substring(startIndex, endIndex)); 927 return UNINTERCEPTED(receiver.substring(startIndex, endIndex));
885 } 928 }
929 checkNum(startIndex);
930 var length = receiver.length;
931 if (endIndex === null) endIndex = length;
932 checkNum(endIndex);
933 if (startIndex < 0 ) throw new IndexOutOfRangeException(startIndex);
934 if (startIndex > endIndex) throw new IndexOutOfRangeException(startIndex);
935 if (endIndex > length) throw new IndexOutOfRangeException(endIndex);
936 return substringUnchecked(receiver, startIndex, endIndex);
937 }
886 938
887 throw new NotImplementedException(); 939 substringUnchecked(receiver, startIndex, endIndex)
kasperl 2012/02/20 10:44:51 You're using rawXxx for some methods and xxxUnchec
ahe 2012/02/20 16:31:58 Done.
888 } 940 => JS('String', @'$0.substring($1, $2)', receiver, startIndex, endIndex);
941
889 942
890 builtin$toLowerCase$0(receiver) { 943 builtin$toLowerCase$0(receiver) {
891 checkNull(receiver); 944 checkNull(receiver);
892 if (receiver is !String) return UNINTERCEPTED(receiver.toLowerCase()); 945 if (receiver is !String) return UNINTERCEPTED(receiver.toLowerCase());
893 946
894 throw new NotImplementedException(); 947 return JS('String', @'$0.toLowerCase()', receiver);
895 } 948 }
896 949
897 builtin$toUpperCase$0(receiver) { 950 builtin$toUpperCase$0(receiver) {
898 checkNull(receiver); 951 checkNull(receiver);
899 if (receiver is !String) return UNINTERCEPTED(receiver.toUpperCase()); 952 if (receiver is !String) return UNINTERCEPTED(receiver.toUpperCase());
900 953
901 throw new NotImplementedException(); 954 return JS('String', @'$0.toUpperCase()', receiver);
902 } 955 }
903 956
904 builtin$trim$0(receiver) { 957 builtin$trim$0(receiver) {
905 checkNull(receiver); 958 checkNull(receiver);
906 if (receiver is !String) return UNINTERCEPTED(receiver.trim()); 959 if (receiver is !String) return UNINTERCEPTED(receiver.trim());
907 960
908 return JS('String', @'$0.trim()', receiver); 961 return JS('String', @'$0.trim()', receiver);
909 } 962 }
910 963
911 class MathNatives { 964 class MathNatives {
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
964 checkNum(exponent); 1017 checkNum(exponent);
965 return JS("num", @"Math.pow($0, $1)", value, exponent); 1018 return JS("num", @"Math.pow($0, $1)", value, exponent);
966 } 1019 }
967 1020
968 static double random() => JS("double", @"Math.random()"); 1021 static double random() => JS("double", @"Math.random()");
969 } 1022 }
970 1023
971 builtin$hashCode$0(receiver) { 1024 builtin$hashCode$0(receiver) {
972 if (receiver is num) return receiver & 0x1FFFFFFF; 1025 if (receiver is num) return receiver & 0x1FFFFFFF;
973 if (receiver is String) { 1026 if (receiver is String) {
974 throw new NotImplementedException(); 1027 throw 'String.hashCode is not implemented';
975 } 1028 }
976 if (isJSArray(receiver)) { 1029 if (isJSArray(receiver)) {
977 throw new NotImplementedException(); 1030 throw 'List.hashCode is not implemented';
978 } 1031 }
979 return UNINTERCEPTED(receiver.hashCode()); 1032 return UNINTERCEPTED(receiver.hashCode());
980 } 1033 }
OLDNEW
« no previous file with comments | « dart/frog/file_system_vm.dart ('k') | dart/frog/leg/lib/mockimpl.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698