Chromium Code Reviews| OLD | NEW |
|---|---|
| 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:_async_await_error_codes' as async_error_codes; | 7 import 'dart:_async_await_error_codes' as async_error_codes; |
| 8 | 8 |
| 9 import 'dart:_js_embedded_names' show | 9 import 'dart:_js_embedded_names' show |
| 10 DEFERRED_LIBRARY_URIS, | 10 DEFERRED_LIBRARY_URIS, |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 187 return JS('String', r'"" + (#)', value); | 187 return JS('String', r'"" + (#)', value); |
| 188 } | 188 } |
| 189 } else if (true == value) { | 189 } else if (true == value) { |
| 190 return 'true'; | 190 return 'true'; |
| 191 } else if (false == value) { | 191 } else if (false == value) { |
| 192 return 'false'; | 192 return 'false'; |
| 193 } else if (value == null) { | 193 } else if (value == null) { |
| 194 return 'null'; | 194 return 'null'; |
| 195 } | 195 } |
| 196 var res = value.toString(); | 196 var res = value.toString(); |
| 197 if (res is !String) throw new ArgumentError(value); | 197 if (res is !String) throw _ArgumentError(value); |
| 198 return res; | 198 return res; |
| 199 } | 199 } |
| 200 | 200 |
| 201 createInvocationMirror(String name, internalName, kind, arguments, | 201 createInvocationMirror(String name, internalName, kind, arguments, |
| 202 argumentNames) { | 202 argumentNames) { |
| 203 return new JSInvocationMirror(name, | 203 return new JSInvocationMirror(name, |
| 204 internalName, | 204 internalName, |
| 205 kind, | 205 kind, |
| 206 arguments, | 206 arguments, |
| 207 argumentNames); | 207 argumentNames); |
| (...skipping 475 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 683 // Cannot fail because we know that the digits are all decimal. | 683 // Cannot fail because we know that the digits are all decimal. |
| 684 return JS('int', r'parseInt(#, 10)', source); | 684 return JS('int', r'parseInt(#, 10)', source); |
| 685 } | 685 } |
| 686 if (match[hexIndex] != null) { | 686 if (match[hexIndex] != null) { |
| 687 // Cannot fail because we know that the digits are all hex. | 687 // Cannot fail because we know that the digits are all hex. |
| 688 return JS('int', r'parseInt(#, 16)', source); | 688 return JS('int', r'parseInt(#, 16)', source); |
| 689 } | 689 } |
| 690 return _parseIntError(source, handleError); | 690 return _parseIntError(source, handleError); |
| 691 } | 691 } |
| 692 | 692 |
| 693 if (radix is! int) throw new ArgumentError("Radix is not an integer"); | 693 if (radix is! int) { |
| 694 throw new ArgumentError.value(radix, 'radix', 'is not an integer'); | |
| 695 } | |
| 694 if (radix < 2 || radix > 36) { | 696 if (radix < 2 || radix > 36) { |
| 695 throw new RangeError.range(radix, 2, 36, "radix"); | 697 throw new RangeError.range(radix, 2, 36, 'radix'); |
| 696 } | 698 } |
| 697 if (radix == 10 && decimalMatch != null) { | 699 if (radix == 10 && decimalMatch != null) { |
| 698 // Cannot fail because we know that the digits are all decimal. | 700 // Cannot fail because we know that the digits are all decimal. |
| 699 return JS('int', r'parseInt(#, 10)', source); | 701 return JS('int', r'parseInt(#, 10)', source); |
| 700 } | 702 } |
| 701 // If radix >= 10 and we have only decimal digits the string is safe. | 703 // If radix >= 10 and we have only decimal digits the string is safe. |
| 702 // Otherwise we need to check the digits. | 704 // Otherwise we need to check the digits. |
| 703 if (radix < 10 || decimalMatch == null) { | 705 if (radix < 10 || decimalMatch == null) { |
| 704 // We know that the characters must be ASCII as otherwise the | 706 // We know that the characters must be ASCII as otherwise the |
| 705 // regexp wouldn't have matched. Lowercasing by doing `| 0x20` is thus | 707 // regexp wouldn't have matched. Lowercasing by doing `| 0x20` is thus |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 730 } | 732 } |
| 731 // The above matching and checks ensures the source has at least one digits | 733 // The above matching and checks ensures the source has at least one digits |
| 732 // and all digits are suitable for the radix, so parseInt cannot return NaN. | 734 // and all digits are suitable for the radix, so parseInt cannot return NaN. |
| 733 return JS('int', r'parseInt(#, #)', source, radix); | 735 return JS('int', r'parseInt(#, #)', source, radix); |
| 734 } | 736 } |
| 735 | 737 |
| 736 @NoInline() | 738 @NoInline() |
| 737 static double _parseDoubleError(String source, | 739 static double _parseDoubleError(String source, |
| 738 double handleError(String source)) { | 740 double handleError(String source)) { |
| 739 if (handleError == null) { | 741 if (handleError == null) { |
| 740 throw new FormatException("Invalid double", source); | 742 throw new FormatException('Invalid double', source); |
| 741 } | 743 } |
| 742 return handleError(source); | 744 return handleError(source); |
| 743 } | 745 } |
| 744 | 746 |
| 745 static double parseDouble(String source, double handleError(String source)) { | 747 static double parseDouble(String source, double handleError(String source)) { |
| 746 checkString(source); | 748 checkString(source); |
| 747 // Notice that JS parseFloat accepts garbage at the end of the string. | 749 // Notice that JS parseFloat accepts garbage at the end of the string. |
| 748 // Accept only: | 750 // Accept only: |
| 749 // - [+/-]NaN | 751 // - [+/-]NaN |
| 750 // - [+/-]Infinity | 752 // - [+/-]Infinity |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 865 result = JS('String', | 867 result = JS('String', |
| 866 r'# + String.fromCharCode.apply(null, #.slice(#, #))', | 868 r'# + String.fromCharCode.apply(null, #.slice(#, #))', |
| 867 result, array, i, chunkEnd); | 869 result, array, i, chunkEnd); |
| 868 } | 870 } |
| 869 return result; | 871 return result; |
| 870 } | 872 } |
| 871 | 873 |
| 872 static String stringFromCodePoints(codePoints) { | 874 static String stringFromCodePoints(codePoints) { |
| 873 List<int> a = <int>[]; | 875 List<int> a = <int>[]; |
| 874 for (var i in codePoints) { | 876 for (var i in codePoints) { |
| 875 if (i is !int) throw new ArgumentError(i); | 877 if (i is !int) throw _ArgumentError(i); |
| 876 if (i <= 0xffff) { | 878 if (i <= 0xffff) { |
| 877 a.add(i); | 879 a.add(i); |
| 878 } else if (i <= 0x10ffff) { | 880 } else if (i <= 0x10ffff) { |
| 879 a.add(0xd800 + ((((i - 0x10000) >> 10) & 0x3ff))); | 881 a.add(0xd800 + ((((i - 0x10000) >> 10) & 0x3ff))); |
| 880 a.add(0xdc00 + (i & 0x3ff)); | 882 a.add(0xdc00 + (i & 0x3ff)); |
| 881 } else { | 883 } else { |
| 882 throw new ArgumentError(i); | 884 throw _ArgumentError(i); |
| 883 } | 885 } |
| 884 } | 886 } |
| 885 return _fromCharCodeApply(a); | 887 return _fromCharCodeApply(a); |
| 886 } | 888 } |
| 887 | 889 |
| 888 static String stringFromCharCodes(charCodes) { | 890 static String stringFromCharCodes(charCodes) { |
| 889 for (var i in charCodes) { | 891 for (var i in charCodes) { |
| 890 if (i is !int) throw new ArgumentError(i); | 892 if (i is !int) throw _ArgumentError(i); |
| 891 if (i < 0) throw new ArgumentError(i); | 893 if (i < 0) throw _ArgumentError(i); |
| 892 if (i > 0xffff) return stringFromCodePoints(charCodes); | 894 if (i > 0xffff) return stringFromCodePoints(charCodes); |
| 893 } | 895 } |
| 894 return _fromCharCodeApply(charCodes); | 896 return _fromCharCodeApply(charCodes); |
| 895 } | 897 } |
| 896 | 898 |
| 897 // [start] and [end] are validated. | 899 // [start] and [end] are validated. |
| 898 static String stringFromNativeUint8List( | 900 static String stringFromNativeUint8List( |
| 899 NativeUint8List charCodes, int start, int end) { | 901 NativeUint8List charCodes, int start, int end) { |
| 900 const kMaxApply = 500; | 902 const kMaxApply = 500; |
| 901 if (end <= kMaxApply && start == 0 && end == charCodes.length) { | 903 if (end <= kMaxApply && start == 0 && end == charCodes.length) { |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1070 | 1072 |
| 1071 static getWeekday(receiver) { | 1073 static getWeekday(receiver) { |
| 1072 int weekday = (receiver.isUtc) | 1074 int weekday = (receiver.isUtc) |
| 1073 ? JS('int', r'#.getUTCDay() + 0', lazyAsJsDate(receiver)) | 1075 ? JS('int', r'#.getUTCDay() + 0', lazyAsJsDate(receiver)) |
| 1074 : JS('int', r'#.getDay() + 0', lazyAsJsDate(receiver)); | 1076 : JS('int', r'#.getDay() + 0', lazyAsJsDate(receiver)); |
| 1075 // Adjust by one because JS weeks start on Sunday. | 1077 // Adjust by one because JS weeks start on Sunday. |
| 1076 return (weekday + 6) % 7 + 1; | 1078 return (weekday + 6) % 7 + 1; |
| 1077 } | 1079 } |
| 1078 | 1080 |
| 1079 static valueFromDateString(str) { | 1081 static valueFromDateString(str) { |
| 1080 if (str is !String) throw new ArgumentError(str); | 1082 if (str is !String) throw _ArgumentError(str); |
| 1081 var value = JS('num', r'Date.parse(#)', str); | 1083 var value = JS('num', r'Date.parse(#)', str); |
| 1082 if (value.isNaN) throw new ArgumentError(str); | 1084 if (value.isNaN) throw _ArgumentError(str); |
| 1083 return value; | 1085 return value; |
| 1084 } | 1086 } |
| 1085 | 1087 |
| 1086 static getProperty(object, key) { | 1088 static getProperty(object, key) { |
| 1087 if (object == null || object is bool || object is num || object is String) { | 1089 if (object == null || object is bool || object is num || object is String) { |
| 1088 throw new ArgumentError(object); | 1090 throw _ArgumentError(object); |
| 1089 } | 1091 } |
| 1090 return JS('var', '#[#]', object, key); | 1092 return JS('var', '#[#]', object, key); |
| 1091 } | 1093 } |
| 1092 | 1094 |
| 1093 static void setProperty(object, key, value) { | 1095 static void setProperty(object, key, value) { |
| 1094 if (object == null || object is bool || object is num || object is String) { | 1096 if (object == null || object is bool || object is num || object is String) { |
| 1095 throw new ArgumentError(object); | 1097 throw _ArgumentError(object); |
| 1096 } | 1098 } |
| 1097 JS('void', '#[#] = #', object, key, value); | 1099 JS('void', '#[#] = #', object, key, value); |
| 1098 } | 1100 } |
| 1099 | 1101 |
| 1100 static functionNoSuchMethod(function, | 1102 static functionNoSuchMethod(function, |
| 1101 List positionalArguments, | 1103 List positionalArguments, |
| 1102 Map<String, dynamic> namedArguments) { | 1104 Map<String, dynamic> namedArguments) { |
| 1103 int argumentCount = 0; | 1105 int argumentCount = 0; |
| 1104 List arguments = []; | 1106 List arguments = []; |
| 1105 List namedArgumentList = []; | 1107 List namedArgumentList = []; |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1377 JS('void', '#[#] = #', cache, key, value); | 1379 JS('void', '#[#] = #', cache, key, value); |
| 1378 } | 1380 } |
| 1379 } | 1381 } |
| 1380 | 1382 |
| 1381 /** | 1383 /** |
| 1382 * Called by generated code to throw an illegal-argument exception, | 1384 * Called by generated code to throw an illegal-argument exception, |
| 1383 * for example, if a non-integer index is given to an optimized | 1385 * for example, if a non-integer index is given to an optimized |
| 1384 * indexed access. | 1386 * indexed access. |
| 1385 */ | 1387 */ |
| 1386 iae(argument) { | 1388 iae(argument) { |
| 1387 throw new ArgumentError(argument); | 1389 throw _ArgumentError(argument); |
| 1388 } | 1390 } |
| 1389 | 1391 |
| 1390 /** | 1392 /** |
| 1391 * Called by generated code to throw an index-out-of-range exception, | 1393 * Called by generated code to throw an index-out-of-range exception, |
| 1392 * for example, if a bounds check fails in an optimized indexed | 1394 * for example, if a bounds check fails in an optimized indexed |
| 1393 * access. This may also be called when the index is not an integer, in | 1395 * access. This may also be called when the index is not an integer, in |
| 1394 * which case it throws an illegal-argument exception instead, like | 1396 * which case it throws an illegal-argument exception instead, like |
| 1395 * [iae], or when the receiver is null. | 1397 * [iae], or when the receiver is null. |
| 1396 */ | 1398 */ |
| 1397 ioore(receiver, index) { | 1399 ioore(receiver, index) { |
| 1398 if (receiver == null) receiver.length; // Force a NoSuchMethodError. | 1400 if (receiver == null) receiver.length; // Force a NoSuchMethodError. |
| 1399 if (index is !int) iae(index); | 1401 if (index is !int) iae(index); |
| 1400 throw new RangeError.value(index); | 1402 throw new RangeError.value(index); |
| 1401 } | 1403 } |
| 1402 | 1404 |
| 1403 stringLastIndexOfUnchecked(receiver, element, start) | 1405 stringLastIndexOfUnchecked(receiver, element, start) |
| 1404 => JS('int', r'#.lastIndexOf(#, #)', receiver, element, start); | 1406 => JS('int', r'#.lastIndexOf(#, #)', receiver, element, start); |
| 1405 | 1407 |
| 1406 | 1408 |
| 1409 /// 'factory' for constructing ArgumentError.value. | |
| 1410 @NoInline() | |
| 1411 ArgumentError _ArgumentError(object) { | |
|
floitsch
2015/05/13 05:11:29
it's a function. -> lower case.
(even if it's to
sra1
2015/05/13 22:17:48
Done.
| |
| 1412 return new ArgumentError.value(object); | |
| 1413 } | |
| 1414 | |
| 1407 checkNull(object) { | 1415 checkNull(object) { |
| 1408 if (object == null) throw new ArgumentError(null); | 1416 if (object == null) throw _ArgumentError(object); |
| 1409 return object; | 1417 return object; |
| 1410 } | 1418 } |
| 1411 | 1419 |
| 1412 checkNum(value) { | 1420 checkNum(value) { |
| 1413 if (value is !num) { | 1421 if (value is !num) throw _ArgumentError(value); |
| 1414 throw new ArgumentError(value); | |
| 1415 } | |
| 1416 return value; | 1422 return value; |
| 1417 } | 1423 } |
| 1418 | 1424 |
| 1419 checkInt(value) { | 1425 checkInt(value) { |
| 1420 if (value is !int) { | 1426 if (value is !int) throw _ArgumentError(value); |
| 1421 throw new ArgumentError(value); | |
| 1422 } | |
| 1423 return value; | 1427 return value; |
| 1424 } | 1428 } |
| 1425 | 1429 |
| 1426 checkBool(value) { | 1430 checkBool(value) { |
| 1427 if (value is !bool) { | 1431 if (value is !bool) throw _ArgumentError(value); |
| 1428 throw new ArgumentError(value); | |
| 1429 } | |
| 1430 return value; | 1432 return value; |
| 1431 } | 1433 } |
| 1432 | 1434 |
| 1433 checkString(value) { | 1435 checkString(value) { |
| 1434 if (value is !String) { | 1436 if (value is !String) throw _ArgumentError(value); |
| 1435 throw new ArgumentError(value); | |
| 1436 } | |
| 1437 return value; | 1437 return value; |
| 1438 } | 1438 } |
| 1439 | 1439 |
| 1440 /** | 1440 /** |
| 1441 * Wrap the given Dart object and record a stack trace. | 1441 * Wrap the given Dart object and record a stack trace. |
| 1442 * | 1442 * |
| 1443 * The code in [unwrapException] deals with getting the original Dart | 1443 * The code in [unwrapException] deals with getting the original Dart |
| 1444 * object out of the wrapper again. | 1444 * object out of the wrapper again. |
| 1445 */ | 1445 */ |
| 1446 @NoInline() | 1446 @NoInline() |
| (...skipping 2639 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 4086 // This is a function that will return a helper function that does the | 4086 // This is a function that will return a helper function that does the |
| 4087 // iteration of the sync*. | 4087 // iteration of the sync*. |
| 4088 // | 4088 // |
| 4089 // Each invocation should give a body with fresh state. | 4089 // Each invocation should give a body with fresh state. |
| 4090 final dynamic /* js function */ _outerHelper; | 4090 final dynamic /* js function */ _outerHelper; |
| 4091 | 4091 |
| 4092 SyncStarIterable(this._outerHelper); | 4092 SyncStarIterable(this._outerHelper); |
| 4093 | 4093 |
| 4094 Iterator get iterator => new SyncStarIterator(JS('', '#()', _outerHelper)); | 4094 Iterator get iterator => new SyncStarIterator(JS('', '#()', _outerHelper)); |
| 4095 } | 4095 } |
| OLD | NEW |