OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 10 matching lines...) Expand all Loading... |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 // limitations under the License. | 27 // limitations under the License. |
28 | 28 |
29 #include "i18n.h" | 29 #include "i18n.h" |
30 | 30 |
| 31 #include "unicode/brkiter.h" |
31 #include "unicode/calendar.h" | 32 #include "unicode/calendar.h" |
32 #include "unicode/coll.h" | 33 #include "unicode/coll.h" |
33 #include "unicode/curramt.h" | 34 #include "unicode/curramt.h" |
34 #include "unicode/dcfmtsym.h" | 35 #include "unicode/dcfmtsym.h" |
35 #include "unicode/decimfmt.h" | 36 #include "unicode/decimfmt.h" |
36 #include "unicode/dtfmtsym.h" | 37 #include "unicode/dtfmtsym.h" |
37 #include "unicode/dtptngen.h" | 38 #include "unicode/dtptngen.h" |
38 #include "unicode/locid.h" | 39 #include "unicode/locid.h" |
39 #include "unicode/numfmt.h" | 40 #include "unicode/numfmt.h" |
40 #include "unicode/numsys.h" | 41 #include "unicode/numsys.h" |
| 42 #include "unicode/rbbi.h" |
41 #include "unicode/smpdtfmt.h" | 43 #include "unicode/smpdtfmt.h" |
42 #include "unicode/timezone.h" | 44 #include "unicode/timezone.h" |
43 #include "unicode/uchar.h" | 45 #include "unicode/uchar.h" |
44 #include "unicode/ucol.h" | 46 #include "unicode/ucol.h" |
45 #include "unicode/ucurr.h" | 47 #include "unicode/ucurr.h" |
46 #include "unicode/unum.h" | 48 #include "unicode/unum.h" |
47 #include "unicode/uversion.h" | 49 #include "unicode/uversion.h" |
48 | 50 |
49 namespace v8 { | 51 namespace v8 { |
50 namespace internal { | 52 namespace internal { |
(...skipping 671 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
722 // This would never happen, since we got the locale from ICU. | 724 // This would never happen, since we got the locale from ICU. |
723 JSObject::SetProperty( | 725 JSObject::SetProperty( |
724 resolved, | 726 resolved, |
725 isolate->factory()->NewStringFromAscii(CStrVector("locale")), | 727 isolate->factory()->NewStringFromAscii(CStrVector("locale")), |
726 isolate->factory()->NewStringFromAscii(CStrVector("und")), | 728 isolate->factory()->NewStringFromAscii(CStrVector("und")), |
727 NONE, | 729 NONE, |
728 kNonStrictMode); | 730 kNonStrictMode); |
729 } | 731 } |
730 } | 732 } |
731 | 733 |
| 734 |
| 735 icu::BreakIterator* CreateICUBreakIterator( |
| 736 Isolate* isolate, |
| 737 const icu::Locale& icu_locale, |
| 738 Handle<JSObject> options) { |
| 739 UErrorCode status = U_ZERO_ERROR; |
| 740 icu::BreakIterator* break_iterator = NULL; |
| 741 icu::UnicodeString type; |
| 742 if (!ExtractStringSetting(isolate, options, "type", &type)) return NULL; |
| 743 |
| 744 if (type == UNICODE_STRING_SIMPLE("character")) { |
| 745 break_iterator = |
| 746 icu::BreakIterator::createCharacterInstance(icu_locale, status); |
| 747 } else if (type == UNICODE_STRING_SIMPLE("sentence")) { |
| 748 break_iterator = |
| 749 icu::BreakIterator::createSentenceInstance(icu_locale, status); |
| 750 } else if (type == UNICODE_STRING_SIMPLE("line")) { |
| 751 break_iterator = |
| 752 icu::BreakIterator::createLineInstance(icu_locale, status); |
| 753 } else { |
| 754 // Defualt is word iterator. |
| 755 break_iterator = |
| 756 icu::BreakIterator::createWordInstance(icu_locale, status); |
| 757 } |
| 758 |
| 759 if (U_FAILURE(status)) { |
| 760 delete break_iterator; |
| 761 return NULL; |
| 762 } |
| 763 |
| 764 return break_iterator; |
| 765 } |
| 766 |
| 767 |
| 768 void SetResolvedBreakIteratorSettings(Isolate* isolate, |
| 769 const icu::Locale& icu_locale, |
| 770 icu::BreakIterator* break_iterator, |
| 771 Handle<JSObject> resolved) { |
| 772 UErrorCode status = U_ZERO_ERROR; |
| 773 |
| 774 // Set the locale |
| 775 char result[ULOC_FULLNAME_CAPACITY]; |
| 776 status = U_ZERO_ERROR; |
| 777 uloc_toLanguageTag( |
| 778 icu_locale.getName(), result, ULOC_FULLNAME_CAPACITY, FALSE, &status); |
| 779 if (U_SUCCESS(status)) { |
| 780 JSObject::SetProperty( |
| 781 resolved, |
| 782 isolate->factory()->NewStringFromAscii(CStrVector("locale")), |
| 783 isolate->factory()->NewStringFromAscii(CStrVector(result)), |
| 784 NONE, |
| 785 kNonStrictMode); |
| 786 } else { |
| 787 // This would never happen, since we got the locale from ICU. |
| 788 JSObject::SetProperty( |
| 789 resolved, |
| 790 isolate->factory()->NewStringFromAscii(CStrVector("locale")), |
| 791 isolate->factory()->NewStringFromAscii(CStrVector("und")), |
| 792 NONE, |
| 793 kNonStrictMode); |
| 794 } |
| 795 } |
| 796 |
732 } // namespace | 797 } // namespace |
733 | 798 |
734 | 799 |
735 // static | 800 // static |
736 Handle<ObjectTemplateInfo> I18N::GetTemplate(Isolate* isolate) { | 801 Handle<ObjectTemplateInfo> I18N::GetTemplate(Isolate* isolate) { |
737 return GetEternal<1, i::EternalHandles::I18N_TEMPLATE_ONE>(isolate); | 802 return GetEternal<1, i::EternalHandles::I18N_TEMPLATE_ONE>(isolate); |
738 } | 803 } |
739 | 804 |
740 | 805 |
741 // static | 806 // static |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
923 Persistent<v8::Object>* object, | 988 Persistent<v8::Object>* object, |
924 void* param) { | 989 void* param) { |
925 // First delete the hidden C++ object. | 990 // First delete the hidden C++ object. |
926 delete reinterpret_cast<icu::Collator*>(Handle<JSObject>::cast( | 991 delete reinterpret_cast<icu::Collator*>(Handle<JSObject>::cast( |
927 v8::Utils::OpenPersistent(object))->GetInternalField(0)); | 992 v8::Utils::OpenPersistent(object))->GetInternalField(0)); |
928 | 993 |
929 // Then dispose of the persistent handle to JS object. | 994 // Then dispose of the persistent handle to JS object. |
930 object->Dispose(isolate); | 995 object->Dispose(isolate); |
931 } | 996 } |
932 | 997 |
| 998 |
| 999 icu::BreakIterator* BreakIterator::InitializeBreakIterator( |
| 1000 Isolate* isolate, |
| 1001 Handle<String> locale, |
| 1002 Handle<JSObject> options, |
| 1003 Handle<JSObject> resolved) { |
| 1004 // Convert BCP47 into ICU locale format. |
| 1005 UErrorCode status = U_ZERO_ERROR; |
| 1006 icu::Locale icu_locale; |
| 1007 char icu_result[ULOC_FULLNAME_CAPACITY]; |
| 1008 int icu_length = 0; |
| 1009 v8::String::Utf8Value bcp47_locale(v8::Utils::ToLocal(locale)); |
| 1010 if (bcp47_locale.length() != 0) { |
| 1011 uloc_forLanguageTag(*bcp47_locale, icu_result, ULOC_FULLNAME_CAPACITY, |
| 1012 &icu_length, &status); |
| 1013 if (U_FAILURE(status) || icu_length == 0) { |
| 1014 return NULL; |
| 1015 } |
| 1016 icu_locale = icu::Locale(icu_result); |
| 1017 } |
| 1018 |
| 1019 icu::BreakIterator* break_iterator = CreateICUBreakIterator( |
| 1020 isolate, icu_locale, options); |
| 1021 if (!break_iterator) { |
| 1022 // Remove extensions and try again. |
| 1023 icu::Locale no_extension_locale(icu_locale.getBaseName()); |
| 1024 break_iterator = CreateICUBreakIterator( |
| 1025 isolate, no_extension_locale, options); |
| 1026 |
| 1027 // Set resolved settings (locale). |
| 1028 SetResolvedBreakIteratorSettings( |
| 1029 isolate, no_extension_locale, break_iterator, resolved); |
| 1030 } else { |
| 1031 SetResolvedBreakIteratorSettings( |
| 1032 isolate, icu_locale, break_iterator, resolved); |
| 1033 } |
| 1034 |
| 1035 return break_iterator; |
| 1036 } |
| 1037 |
| 1038 |
| 1039 icu::BreakIterator* BreakIterator::UnpackBreakIterator(Isolate* isolate, |
| 1040 Handle<JSObject> obj) { |
| 1041 Handle<String> key = |
| 1042 isolate->factory()->NewStringFromAscii(CStrVector("breakIterator")); |
| 1043 if (obj->HasLocalProperty(*key)) { |
| 1044 return reinterpret_cast<icu::BreakIterator*>(obj->GetInternalField(0)); |
| 1045 } |
| 1046 |
| 1047 return NULL; |
| 1048 } |
| 1049 |
| 1050 |
| 1051 void BreakIterator::DeleteBreakIterator(v8::Isolate* isolate, |
| 1052 Persistent<v8::Value>* object, |
| 1053 void* param) { |
| 1054 // First delete the hidden C++ object. |
| 1055 delete reinterpret_cast<icu::BreakIterator*>(Handle<JSObject>::cast( |
| 1056 v8::Utils::OpenPersistent(object))->GetInternalField(0)); |
| 1057 |
| 1058 delete reinterpret_cast<icu::UnicodeString*>(Handle<JSObject>::cast( |
| 1059 v8::Utils::OpenPersistent(object))->GetInternalField(1)); |
| 1060 |
| 1061 // Then dispose of the persistent handle to JS object. |
| 1062 object->Dispose(isolate); |
| 1063 } |
| 1064 |
933 } } // namespace v8::internal | 1065 } } // namespace v8::internal |
OLD | NEW |