Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // ECMAScript 402 API implementation. | 5 // ECMAScript 402 API implementation. |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Intl object is a single object that has some named properties, | 8 * Intl object is a single object that has some named properties, |
| 9 * all of which are constructors. | 9 * all of which are constructors. |
| 10 */ | 10 */ |
| (...skipping 700 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 711 ((lowercasedPart !== 'es' && | 711 ((lowercasedPart !== 'es' && |
| 712 lowercasedPart !== 'of' && lowercasedPart !== 'au') ? | 712 lowercasedPart !== 'of' && lowercasedPart !== 'au') ? |
| 713 toTitleCaseWord(part) : lowercasedPart); | 713 toTitleCaseWord(part) : lowercasedPart); |
| 714 } | 714 } |
| 715 } | 715 } |
| 716 return result; | 716 return result; |
| 717 } | 717 } |
| 718 | 718 |
| 719 /** | 719 /** |
| 720 * Canonicalizes the language tag, or throws in case the tag is invalid. | 720 * Canonicalizes the language tag, or throws in case the tag is invalid. |
| 721 * ECMA 402 9.2.1 steps 7.c ii ~ v. | |
| 721 */ | 722 */ |
| 722 function canonicalizeLanguageTag(localeID) { | 723 function canonicalizeLanguageTag(localeID) { |
| 723 // null is typeof 'object' so we have to do extra check. | 724 // null is typeof 'object' so we have to do extra check. |
| 724 if ((!IS_STRING(localeID) && !IS_RECEIVER(localeID)) || | 725 if ((!IS_STRING(localeID) && !IS_RECEIVER(localeID)) || |
| 725 IS_NULL(localeID)) { | 726 IS_NULL(localeID)) { |
| 726 throw %make_type_error(kLanguageID); | 727 throw %make_type_error(kLanguageID); |
| 727 } | 728 } |
| 728 | 729 |
| 729 // Optimize for the most common case; a language code alone in | 730 // Optimize for the most common case; a language code alone in |
| 730 // the canonical form/lowercase (e.g. "en", "fil"). | 731 // the canonical form/lowercase (e.g. "en", "fil"). |
| 731 if (IS_STRING(localeID) && | 732 if (IS_STRING(localeID) && |
| 732 !IS_NULL(InternalRegExpMatch(/^[a-z]{2,3}$/, localeID))) { | 733 !IS_NULL(InternalRegExpMatch(/^[a-z]{2,3}$/, localeID))) { |
| 733 return localeID; | 734 return localeID; |
| 734 } | 735 } |
| 735 | 736 |
| 736 var localeString = TO_STRING(localeID); | 737 var localeString = TO_STRING(localeID); |
| 737 | 738 |
| 738 if (isValidLanguageTag(localeString) === false) { | 739 if (isStructuallyValidLanguageTag(localeString) === false) { |
| 739 throw %make_range_error(kInvalidLanguageTag, localeString); | 740 throw %make_range_error(kInvalidLanguageTag, localeString); |
| 740 } | 741 } |
| 741 | 742 |
| 743 // ECMA 402 6.2.3 | |
| 742 var tag = %CanonicalizeLanguageTag(localeString); | 744 var tag = %CanonicalizeLanguageTag(localeString); |
| 745 // TODO(jshin): This should not happen because the structual validity | |
| 746 // is already checked. If that's the case, remove this. | |
| 743 if (tag === 'invalid-tag') { | 747 if (tag === 'invalid-tag') { |
| 744 throw %make_range_error(kInvalidLanguageTag, localeString); | 748 throw %make_range_error(kInvalidLanguageTag, localeString); |
| 745 } | 749 } |
| 746 | 750 |
| 747 return tag; | 751 return tag; |
| 748 } | 752 } |
| 749 | 753 |
| 750 | 754 |
| 751 /** | 755 /** |
| 752 * Returns an array where all locales are canonicalized and duplicates removed. | 756 * Returns an array where all locales are canonicalized and duplicates removed. |
| 753 * Throws on locales that are not well formed BCP47 tags. | 757 * Throws on locales that are not well formed BCP47 tags. |
| 758 * ECMA 402 8.2.1 steps 1 (ECMA 402 9.2.1) and 2. | |
| 754 */ | 759 */ |
| 755 function initializeLocaleList(locales) { | 760 function initializeLocaleList(locales) { |
| 756 var seen = new InternalArray(); | 761 var seen = new InternalArray(); |
| 757 if (!IS_UNDEFINED(locales)) { | 762 if (!IS_UNDEFINED(locales)) { |
| 758 // We allow single string localeID. | 763 // We allow single string localeID. |
| 759 if (typeof locales === 'string') { | 764 if (typeof locales === 'string') { |
| 760 %_Call(ArrayPush, seen, canonicalizeLanguageTag(locales)); | 765 %_Call(ArrayPush, seen, canonicalizeLanguageTag(locales)); |
| 761 return freezeArray(seen); | 766 return freezeArray(seen); |
| 762 } | 767 } |
| 763 | 768 |
| 764 var o = TO_OBJECT(locales); | 769 var o = TO_OBJECT(locales); |
| 765 var len = TO_UINT32(o.length); | 770 var len = TO_LENGTH(o.length); |
| 766 | 771 |
| 767 for (var k = 0; k < len; k++) { | 772 for (var k = 0; k < len; k++) { |
| 773 // ECMA 402 9.2.1 step 7a converts 'k' to String. Does it make | |
| 774 // any difference? | |
| 768 if (k in o) { | 775 if (k in o) { |
| 769 var value = o[k]; | 776 var value = o[k]; |
| 770 | 777 |
| 771 var tag = canonicalizeLanguageTag(value); | 778 var tag = canonicalizeLanguageTag(value); |
| 772 | 779 |
| 773 if (%_Call(ArrayIndexOf, seen, tag) === -1) { | 780 if (%_Call(ArrayIndexOf, seen, tag) === -1) { |
| 774 %_Call(ArrayPush, seen, tag); | 781 %_Call(ArrayPush, seen, tag); |
|
jungshik at Google
2016/08/11 20:56:40
intl402/Intl/getCanonicalLocales/overriden-push.js
| |
| 775 } | 782 } |
| 776 } | 783 } |
| 777 } | 784 } |
| 778 } | 785 } |
| 779 | 786 |
| 780 return freezeArray(seen); | 787 return freezeArray(seen); |
| 781 } | 788 } |
| 782 | 789 |
| 783 | 790 |
| 784 /** | 791 /** |
| 785 * Validates the language tag. Section 2.2.9 of the bcp47 spec | 792 * Check the structual Validity of the language tag per ECMA 402 6.2.2: |
| 786 * defines a valid tag. | 793 * - Well-formed per RFC 5646 2.1 |
| 794 * - There are no duplicate variant subtags | |
| 795 * - There are no duplicate singletion (extension) subtags | |
| 796 * | |
| 797 * One extra-check is done (from RFC 5646 2.2.9): the tag is compared | |
| 798 * against the list of grandfathered tags. However, subtags for | |
| 799 * primary/extended language, script, region, variant are not checked | |
| 800 * against the IANA language subtag registry. | |
| 787 * | 801 * |
| 788 * ICU is too permissible and lets invalid tags, like | 802 * ICU is too permissible and lets invalid tags, like |
| 789 * hant-cmn-cn, through. | 803 * hant-cmn-cn, through. |
| 790 * | 804 * |
| 791 * Returns false if the language tag is invalid. | 805 * Returns false if the language tag is invalid. |
| 792 */ | 806 */ |
| 793 function isValidLanguageTag(locale) { | 807 function isStructuallyValidLanguageTag(locale) { |
| 794 // Check if it's well-formed, including grandfadered tags. | 808 // Check if it's well-formed, including grandfadered tags. |
| 795 if (IS_NULL(InternalRegExpMatch(GetLanguageTagRE(), locale))) { | 809 if (IS_NULL(InternalRegExpMatch(GetLanguageTagRE(), locale))) { |
| 796 return false; | 810 return false; |
| 797 } | 811 } |
| 798 | 812 |
| 799 // Just return if it's a x- form. It's all private. | 813 // Just return if it's a x- form. It's all private. |
| 800 if (%_Call(StringIndexOf, locale, 'x-') === 0) { | 814 if (%_Call(StringIndexOf, locale, 'x-') === 0) { |
| 801 return true; | 815 return true; |
| 802 } | 816 } |
| 803 | 817 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 878 var resolvedAccessor = { | 892 var resolvedAccessor = { |
| 879 get() { | 893 get() { |
| 880 %IncrementUseCounter(kIntlResolved); | 894 %IncrementUseCounter(kIntlResolved); |
| 881 return this[resolvedSymbol]; | 895 return this[resolvedSymbol]; |
| 882 }, | 896 }, |
| 883 set(value) { | 897 set(value) { |
| 884 this[resolvedSymbol] = value; | 898 this[resolvedSymbol] = value; |
| 885 } | 899 } |
| 886 }; | 900 }; |
| 887 | 901 |
| 902 // ECMA 402 section 8.2.1 | |
| 903 InstallFunction(Intl, 'getCanonicalLocales', function(locales) { | |
| 904 if (!IS_UNDEFINED(new.target)) { | |
| 905 throw %make_type_error(kOrdinaryFunctionCalledAsConstructor); | |
| 906 } | |
| 907 | |
| 908 return initializeLocaleList(locales); | |
| 909 } | |
| 910 ); | |
| 911 | |
| 888 /** | 912 /** |
| 889 * Initializes the given object so it's a valid Collator instance. | 913 * Initializes the given object so it's a valid Collator instance. |
| 890 * Useful for subclassing. | 914 * Useful for subclassing. |
| 891 */ | 915 */ |
| 892 function initializeCollator(collator, locales, options) { | 916 function initializeCollator(collator, locales, options) { |
| 893 if (%IsInitializedIntlObject(collator)) { | 917 if (%IsInitializedIntlObject(collator)) { |
| 894 throw %make_type_error(kReinitializeIntl, "Collator"); | 918 throw %make_type_error(kReinitializeIntl, "Collator"); |
| 895 } | 919 } |
| 896 | 920 |
| 897 if (IS_UNDEFINED(options)) { | 921 if (IS_UNDEFINED(options)) { |
| (...skipping 1348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2246 } | 2270 } |
| 2247 ); | 2271 ); |
| 2248 | 2272 |
| 2249 utils.Export(function(to) { | 2273 utils.Export(function(to) { |
| 2250 to.AddBoundMethod = AddBoundMethod; | 2274 to.AddBoundMethod = AddBoundMethod; |
| 2251 to.IntlParseDate = IntlParseDate; | 2275 to.IntlParseDate = IntlParseDate; |
| 2252 to.IntlParseNumber = IntlParseNumber; | 2276 to.IntlParseNumber = IntlParseNumber; |
| 2253 }); | 2277 }); |
| 2254 | 2278 |
| 2255 }) | 2279 }) |
| OLD | NEW |