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

Side by Side Diff: sdk/lib/_internal/compiler/js_lib/js_helper.dart

Issue 860273002: Recognize and special case String.fromCharCodes for Uint8List in JavaScript. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 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) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 'dart:_js_embedded_names' show 7 import 'dart:_js_embedded_names' show
8 ALL_CLASSES, 8 ALL_CLASSES,
9 GET_ISOLATE_TAG, 9 GET_ISOLATE_TAG,
10 INTERCEPTED_NAMES, 10 INTERCEPTED_NAMES,
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 JS_OPERATOR_AS_PREFIX, 53 JS_OPERATOR_AS_PREFIX,
54 JS_OPERATOR_IS_PREFIX, 54 JS_OPERATOR_IS_PREFIX,
55 JS_SIGNATURE_NAME, 55 JS_SIGNATURE_NAME,
56 JS_STRING_CONCAT, 56 JS_STRING_CONCAT,
57 RAW_DART_FUNCTION_REF; 57 RAW_DART_FUNCTION_REF;
58 58
59 import 'dart:_interceptors'; 59 import 'dart:_interceptors';
60 import 'dart:_internal' as _symbol_dev; 60 import 'dart:_internal' as _symbol_dev;
61 import 'dart:_internal' show MappedIterable; 61 import 'dart:_internal' show MappedIterable;
62 62
63 import 'dart:_native_typed_data';
64
63 import 'dart:_js_names' show 65 import 'dart:_js_names' show
64 extractKeys, 66 extractKeys,
65 mangledNames, 67 mangledNames,
66 unmangleGlobalNameIfPreservedAnyways, 68 unmangleGlobalNameIfPreservedAnyways,
67 unmangleAllIdentifiersIfPreservedAnyways; 69 unmangleAllIdentifiersIfPreservedAnyways;
68 70
69 part 'annotations.dart'; 71 part 'annotations.dart';
70 part 'constant_map.dart'; 72 part 'constant_map.dart';
71 part 'native_helper.dart'; 73 part 'native_helper.dart';
72 part 'regexp_helper.dart'; 74 part 'regexp_helper.dart';
(...skipping 684 matching lines...) Expand 10 before | Expand all | Expand 10 after
757 if (JS('bool', '!!self.location')) { 759 if (JS('bool', '!!self.location')) {
758 return JS('String', 'self.location.href'); 760 return JS('String', 'self.location.href');
759 } 761 }
760 762
761 return null; 763 return null;
762 } 764 }
763 765
764 // This is to avoid stack overflows due to very large argument arrays in 766 // This is to avoid stack overflows due to very large argument arrays in
765 // apply(). It fixes http://dartbug.com/6919 767 // apply(). It fixes http://dartbug.com/6919
766 static String _fromCharCodeApply(List<int> array) { 768 static String _fromCharCodeApply(List<int> array) {
767 String result = "";
768 const kMaxApply = 500; 769 const kMaxApply = 500;
769 int end = array.length; 770 int end = array.length;
770 for (var i = 0; i < end; i += kMaxApply) { 771 if (end <= kMaxApply) {
771 var subarray; 772 return JS('String', r'String.fromCharCode.apply(null, #)', array);
772 if (end <= kMaxApply) { 773 }
773 subarray = array; 774 String result = '';
774 } else { 775 for (int i = 0; i < end; i += kMaxApply) {
775 subarray = JS('JSExtendableArray', r'#.slice(#, #)', array, 776 int chunkEnd = (i + kMaxApply < end) ? i + kMaxApply : end;
776 i, i + kMaxApply < end ? i + kMaxApply : end); 777 result = JS('String',
777 } 778 r'# + String.fromCharCode.apply(null, #.slice(#, #))',
778 result = JS('String', '# + String.fromCharCode.apply(#, #)', 779 result, array, i, chunkEnd);
779 result, null, subarray);
780 } 780 }
781 return result; 781 return result;
782 } 782 }
783 783
784 static String stringFromCodePoints(codePoints) { 784 static String stringFromCodePoints(codePoints) {
785 List<int> a = <int>[]; 785 List<int> a = <int>[];
786 for (var i in codePoints) { 786 for (var i in codePoints) {
787 if (i is !int) throw new ArgumentError(i); 787 if (i is !int) throw new ArgumentError(i);
788 if (i <= 0xffff) { 788 if (i <= 0xffff) {
789 a.add(i); 789 a.add(i);
790 } else if (i <= 0x10ffff) { 790 } else if (i <= 0x10ffff) {
791 a.add(0xd800 + ((((i - 0x10000) >> 10) & 0x3ff))); 791 a.add(0xd800 + ((((i - 0x10000) >> 10) & 0x3ff)));
792 a.add(0xdc00 + (i & 0x3ff)); 792 a.add(0xdc00 + (i & 0x3ff));
793 } else { 793 } else {
794 throw new ArgumentError(i); 794 throw new ArgumentError(i);
795 } 795 }
796 } 796 }
797 return _fromCharCodeApply(a); 797 return _fromCharCodeApply(a);
798 } 798 }
799 799
800 static String stringFromCharCodes(charCodes) { 800 static String stringFromCharCodes(charCodes) {
801 for (var i in charCodes) { 801 for (var i in charCodes) {
802 if (i is !int) throw new ArgumentError(i); 802 if (i is !int) throw new ArgumentError(i);
803 if (i < 0) throw new ArgumentError(i); 803 if (i < 0) throw new ArgumentError(i);
804 if (i > 0xffff) return stringFromCodePoints(charCodes); 804 if (i > 0xffff) return stringFromCodePoints(charCodes);
805 } 805 }
806 return _fromCharCodeApply(charCodes); 806 return _fromCharCodeApply(charCodes);
807 } 807 }
808 808
809 // [start] and [end] are validated.
810 static String stringFromNativeUint8List(
811 NativeUint8List charCodes, int start, int end) {
812 const kMaxApply = 500;
813 if (end <= kMaxApply && start == 0 && end == charCodes.length) {
814 return JS('String', r'String.fromCharCode.apply(null, #)', charCodes);
815 }
816 String result = '';
817 for (int i = start; i < end; i += kMaxApply) {
818 int chunkEnd = (i + kMaxApply < end) ? i + kMaxApply : end;
819 result = JS('String',
820 r'# + String.fromCharCode.apply(null, #.subarray(#, #))',
821 result, charCodes, i, chunkEnd);
822 }
823 return result;
824 }
825
826
809 static String stringFromCharCode(charCode) { 827 static String stringFromCharCode(charCode) {
810 if (0 <= charCode) { 828 if (0 <= charCode) {
811 if (charCode <= 0xffff) { 829 if (charCode <= 0xffff) {
812 return JS('String', 'String.fromCharCode(#)', charCode); 830 return JS('String', 'String.fromCharCode(#)', charCode);
813 } 831 }
814 if (charCode <= 0x10ffff) { 832 if (charCode <= 0x10ffff) {
815 var bits = charCode - 0x10000; 833 var bits = charCode - 0x10000;
816 var low = 0xDC00 | (bits & 0x3ff); 834 var low = 0xDC00 | (bits & 0x3ff);
817 var high = 0xD800 | (bits >> 10); 835 var high = 0xD800 | (bits >> 10);
818 return JS('String', 'String.fromCharCode(#, #)', high, low); 836 return JS('String', 'String.fromCharCode(#, #)', high, low);
(...skipping 2618 matching lines...) Expand 10 before | Expand all | Expand 10 after
3437 throw new MainError("No top-level function named 'main'."); 3455 throw new MainError("No top-level function named 'main'.");
3438 } 3456 }
3439 3457
3440 void badMain() { 3458 void badMain() {
3441 throw new MainError("'main' is not a function."); 3459 throw new MainError("'main' is not a function.");
3442 } 3460 }
3443 3461
3444 void mainHasTooManyParameters() { 3462 void mainHasTooManyParameters() {
3445 throw new MainError("'main' expects too many parameters."); 3463 throw new MainError("'main' expects too many parameters.");
3446 } 3464 }
OLDNEW
« no previous file with comments | « sdk/lib/_internal/compiler/js_lib/core_patch.dart ('k') | tests/corelib/string_fromcharcodes_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698