| 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:_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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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:_js_names' show | 63 import 'dart:_js_names' show |
| 64 extractKeys, | 64 extractKeys, |
| 65 mangledNames, | 65 mangledNames; |
| 66 unmangleGlobalNameIfPreservedAnyways, | |
| 67 unmangleAllIdentifiersIfPreservedAnyways; | |
| 68 | 66 |
| 69 part 'annotations.dart'; | 67 part 'annotations.dart'; |
| 70 part 'constant_map.dart'; | 68 part 'constant_map.dart'; |
| 71 part 'native_helper.dart'; | 69 part 'native_helper.dart'; |
| 72 part 'regexp_helper.dart'; | 70 part 'regexp_helper.dart'; |
| 73 part 'string_helper.dart'; | 71 part 'string_helper.dart'; |
| 74 part 'js_rti.dart'; | 72 part 'js_rti.dart'; |
| 75 | 73 |
| 74 // TODO(jacobr): remove. |
| 75 String unmangleGlobalNameIfPreservedAnyways(String str) => str; |
| 76 // TODO(jacobr): remove. |
| 77 String unmangleAllIdentifiersIfPreservedAnyways(String str) => str; |
| 78 |
| 76 class _Patch { | 79 class _Patch { |
| 77 const _Patch(); | 80 const _Patch(); |
| 78 } | 81 } |
| 79 | 82 |
| 80 const _Patch patch = const _Patch(); | 83 const _Patch patch = const _Patch(); |
| 81 | 84 |
| 82 | 85 |
| 83 /// Marks the internal map in dart2js, so that internal libraries can is-check | 86 /// Marks the internal map in dart2js, so that internal libraries can is-check |
| 84 // them. | 87 // them. |
| 85 abstract class InternalMap { | 88 abstract class InternalMap { |
| (...skipping 588 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 674 return result; | 677 return result; |
| 675 } | 678 } |
| 676 return handleError(source); | 679 return handleError(source); |
| 677 } | 680 } |
| 678 return result; | 681 return result; |
| 679 } | 682 } |
| 680 | 683 |
| 681 /** [: r"$".codeUnitAt(0) :] */ | 684 /** [: r"$".codeUnitAt(0) :] */ |
| 682 static const int DOLLAR_CHAR_VALUE = 36; | 685 static const int DOLLAR_CHAR_VALUE = 36; |
| 683 | 686 |
| 687 // TODO(jmesserly): remove this method. |
| 688 static constructorNameFallback = JS('Function', r''' |
| 689 function getTagFallback(o) { |
| 690 var constructor = o.constructor; |
| 691 if (typeof constructor == "function") { |
| 692 var name = constructor.name; |
| 693 // If the name is a non-empty string, we use that as the type name of this |
| 694 // object. There are various cases where that does not work, so we have t
o |
| 695 // detect them and fall through to the toString() based implementation. |
| 696 |
| 697 if (typeof name == "string" && |
| 698 |
| 699 // Sometimes the string is empty. This test also catches minified |
| 700 // shadow dom polyfil wrapper for Window on Firefox where the faked |
| 701 // constructor name does not 'stick'. The shortest real DOM object |
| 702 // names have three characters (e.g. URL, CSS). |
| 703 name.length > 2 && |
| 704 |
| 705 // On Firefox we often get "Object" as the constructor name, even for |
| 706 // more specialized DOM objects. |
| 707 name !== "Object" && |
| 708 |
| 709 // This can happen in Opera. |
| 710 name !== "Function.prototype") { |
| 711 return name; |
| 712 } |
| 713 } |
| 714 var s = Object.prototype.toString.call(o); |
| 715 return s.substring(8, s.length - 1); |
| 716 }'''); |
| 717 |
| 684 /// Creates a string containing the complete type for the class [className] | 718 /// Creates a string containing the complete type for the class [className] |
| 685 /// with the given type arguments. | 719 /// with the given type arguments. |
| 686 /// | 720 /// |
| 687 /// In minified mode, uses the unminified names if available. | 721 /// In minified mode, uses the unminified names if available. |
| 688 static String formatType(String className, List typeArguments) { | 722 static String formatType(String className, List typeArguments) { |
| 689 return unmangleAllIdentifiersIfPreservedAnyways | 723 return unmangleAllIdentifiersIfPreservedAnyways |
| 690 ('$className${joinArguments(typeArguments, 0)}'); | 724 ('$className${joinArguments(typeArguments, 0)}'); |
| 691 } | 725 } |
| 692 | 726 |
| 693 /// Returns the type of [object] as a string (including type arguments). | 727 /// Returns the type of [object] as a string (including type arguments). |
| 694 /// | 728 /// |
| 695 /// In minified mode, uses the unminified names if available. | 729 /// In minified mode, uses the unminified names if available. |
| 696 static String objectTypeName(Object object) { | 730 static String objectTypeName(Object object) { |
| 697 String name = constructorNameFallback(getInterceptor(object)); | 731 String name = JS('String', '#(#)', constructorNameFallback, object); |
| 698 if (name == 'Object') { | 732 if (name == 'Object') { |
| 699 // Try to decompile the constructor by turning it into a string and get | 733 // Try to decompile the constructor by turning it into a string and get |
| 700 // the name out of that. If the decompiled name is a string containing an | 734 // the name out of that. If the decompiled name is a string containing an |
| 701 // identifier, we use that instead of the very generic 'Object'. | 735 // identifier, we use that instead of the very generic 'Object'. |
| 702 var decompiled = | 736 var decompiled = |
| 703 JS('var', r'#.match(/^\s*function\s*(\S*)\s*\(/)[1]', | 737 JS('var', r'#.match(/^\s*function\s*(\S*)\s*\(/)[1]', |
| 704 JS('var', r'String(#.constructor)', object)); | 738 JS('var', r'String(#.constructor)', object)); |
| 705 if (decompiled is String) | 739 if (decompiled is String) |
| 706 if (JS('bool', r'/^\w+$/.test(#)', decompiled)) | 740 if (JS('bool', r'/^\w+$/.test(#)', decompiled)) |
| 707 name = decompiled; | 741 name = decompiled; |
| 708 } | 742 } |
| 709 // TODO(kasperl): If the namer gave us a fresh global name, we may | 743 // TODO(kasperl): If the namer gave us a fresh global name, we may |
| 710 // want to remove the numeric suffix that makes it unique too. | 744 // want to remove the numeric suffix that makes it unique too. |
| 745 // TODO(jacobr): commented this out as it seems bogus and it breaks as |
| 746 // codeUnitAt is not yet supported. |
| 747 /* |
| 711 if (name.length > 1 && identical(name.codeUnitAt(0), DOLLAR_CHAR_VALUE)) { | 748 if (name.length > 1 && identical(name.codeUnitAt(0), DOLLAR_CHAR_VALUE)) { |
| 712 name = name.substring(1); | 749 name = name.substring(1); |
| 713 } | 750 } |
| 751 */ |
| 714 return formatType(name, getRuntimeTypeInfo(object)); | 752 return formatType(name, getRuntimeTypeInfo(object)); |
| 715 } | 753 } |
| 716 | 754 |
| 717 /// In minified mode, uses the unminified names if available. | 755 /// In minified mode, uses the unminified names if available. |
| 718 static String objectToString(Object object) { | 756 static String objectToString(Object object) { |
| 719 String name = objectTypeName(object); | 757 String name = objectTypeName(object); |
| 720 return "Instance of '$name'"; | 758 return "Instance of '$name'"; |
| 721 } | 759 } |
| 722 | 760 |
| 723 static num dateNow() => JS('int', r'Date.now()'); | 761 static num dateNow() => JS('int', r'Date.now()'); |
| (...skipping 2708 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3432 throw new MainError("No top-level function named 'main'."); | 3470 throw new MainError("No top-level function named 'main'."); |
| 3433 } | 3471 } |
| 3434 | 3472 |
| 3435 void badMain() { | 3473 void badMain() { |
| 3436 throw new MainError("'main' is not a function."); | 3474 throw new MainError("'main' is not a function."); |
| 3437 } | 3475 } |
| 3438 | 3476 |
| 3439 void mainHasTooManyParameters() { | 3477 void mainHasTooManyParameters() { |
| 3440 throw new MainError("'main' expects too many parameters."); | 3478 throw new MainError("'main' expects too many parameters."); |
| 3441 } | 3479 } |
| OLD | NEW |