OLD | NEW |
| (Empty) |
1 // Copyright 2013 the V8 project authors. All rights reserved. | |
2 // Redistribution and use in source and binary forms, with or without | |
3 // modification, are permitted provided that the following conditions are | |
4 // met: | |
5 // | |
6 // * Redistributions of source code must retain the above copyright | |
7 // notice, this list of conditions and the following disclaimer. | |
8 // * Redistributions in binary form must reproduce the above | |
9 // copyright notice, this list of conditions and the following | |
10 // disclaimer in the documentation and/or other materials provided | |
11 // with the distribution. | |
12 // * Neither the name of Google Inc. nor the names of its | |
13 // contributors may be used to endorse or promote products derived | |
14 // from this software without specific prior written permission. | |
15 // | |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
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. | |
27 // limitations under the License. | |
28 | |
29 #include "collator.h" | |
30 | |
31 #include "i18n-utils.h" | |
32 #include "unicode/coll.h" | |
33 #include "unicode/locid.h" | |
34 #include "unicode/ucol.h" | |
35 | |
36 namespace v8_i18n { | |
37 | |
38 static icu::Collator* InitializeCollator( | |
39 v8::Handle<v8::String>, v8::Handle<v8::Object>, v8::Handle<v8::Object>); | |
40 | |
41 static icu::Collator* CreateICUCollator( | |
42 const icu::Locale&, v8::Handle<v8::Object>); | |
43 | |
44 static bool SetBooleanAttribute( | |
45 UColAttribute, const char*, v8::Handle<v8::Object>, icu::Collator*); | |
46 | |
47 static void SetResolvedSettings( | |
48 const icu::Locale&, icu::Collator*, v8::Handle<v8::Object>); | |
49 | |
50 static void SetBooleanSetting( | |
51 UColAttribute, icu::Collator*, const char*, v8::Handle<v8::Object>); | |
52 | |
53 icu::Collator* Collator::UnpackCollator(v8::Handle<v8::Object> obj) { | |
54 v8::HandleScope handle_scope; | |
55 | |
56 if (obj->HasOwnProperty(v8::String::New("collator"))) { | |
57 return static_cast<icu::Collator*>( | |
58 obj->GetAlignedPointerFromInternalField(0)); | |
59 } | |
60 | |
61 return NULL; | |
62 } | |
63 | |
64 void Collator::DeleteCollator(v8::Isolate* isolate, | |
65 v8::Persistent<v8::Object>* object, | |
66 void* param) { | |
67 // First delete the hidden C++ object. | |
68 // Unpacking should never return NULL here. That would only happen if | |
69 // this method is used as the weak callback for persistent handles not | |
70 // pointing to a collator. | |
71 v8::HandleScope handle_scope(isolate); | |
72 v8::Local<v8::Object> handle = v8::Local<v8::Object>::New(isolate, *object); | |
73 delete UnpackCollator(handle); | |
74 | |
75 // Then dispose of the persistent handle to JS object. | |
76 object->Dispose(isolate); | |
77 } | |
78 | |
79 | |
80 // Throws a JavaScript exception. | |
81 static v8::Handle<v8::Value> ThrowUnexpectedObjectError() { | |
82 // Returns undefined, and schedules an exception to be thrown. | |
83 return v8::ThrowException(v8::Exception::Error( | |
84 v8::String::New("Collator method called on an object " | |
85 "that is not a Collator."))); | |
86 } | |
87 | |
88 | |
89 // When there's an ICU error, throw a JavaScript error with |message|. | |
90 static v8::Handle<v8::Value> ThrowExceptionForICUError(const char* message) { | |
91 return v8::ThrowException(v8::Exception::Error(v8::String::New(message))); | |
92 } | |
93 | |
94 | |
95 // static | |
96 void Collator::JSInternalCompare( | |
97 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
98 if (args.Length() != 3 || !args[0]->IsObject() || | |
99 !args[1]->IsString() || !args[2]->IsString()) { | |
100 v8::ThrowException(v8::Exception::SyntaxError( | |
101 v8::String::New("Collator and two string arguments are required."))); | |
102 return; | |
103 } | |
104 | |
105 icu::Collator* collator = UnpackCollator(args[0]->ToObject()); | |
106 if (!collator) { | |
107 ThrowUnexpectedObjectError(); | |
108 return; | |
109 } | |
110 | |
111 v8::String::Value string_value1(args[1]); | |
112 v8::String::Value string_value2(args[2]); | |
113 const UChar* string1 = reinterpret_cast<const UChar*>(*string_value1); | |
114 const UChar* string2 = reinterpret_cast<const UChar*>(*string_value2); | |
115 UErrorCode status = U_ZERO_ERROR; | |
116 UCollationResult result = collator->compare( | |
117 string1, string_value1.length(), string2, string_value2.length(), status); | |
118 | |
119 if (U_FAILURE(status)) { | |
120 ThrowExceptionForICUError( | |
121 "Internal error. Unexpected failure in Collator.compare."); | |
122 return; | |
123 } | |
124 | |
125 args.GetReturnValue().Set(result); | |
126 } | |
127 | |
128 void Collator::JSCreateCollator( | |
129 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
130 if (args.Length() != 3 || !args[0]->IsString() || !args[1]->IsObject() || | |
131 !args[2]->IsObject()) { | |
132 v8::ThrowException(v8::Exception::SyntaxError( | |
133 v8::String::New("Internal error, wrong parameters."))); | |
134 return; | |
135 } | |
136 | |
137 v8::Isolate* isolate = args.GetIsolate(); | |
138 v8::Local<v8::ObjectTemplate> intl_collator_template = | |
139 Utils::GetTemplate(isolate); | |
140 | |
141 // Create an empty object wrapper. | |
142 v8::Local<v8::Object> local_object = intl_collator_template->NewInstance(); | |
143 // But the handle shouldn't be empty. | |
144 // That can happen if there was a stack overflow when creating the object. | |
145 if (local_object.IsEmpty()) { | |
146 args.GetReturnValue().Set(local_object); | |
147 return; | |
148 } | |
149 | |
150 // Set collator as internal field of the resulting JS object. | |
151 icu::Collator* collator = InitializeCollator( | |
152 args[0]->ToString(), args[1]->ToObject(), args[2]->ToObject()); | |
153 | |
154 if (!collator) { | |
155 v8::ThrowException(v8::Exception::Error(v8::String::New( | |
156 "Internal error. Couldn't create ICU collator."))); | |
157 return; | |
158 } else { | |
159 local_object->SetAlignedPointerInInternalField(0, collator); | |
160 | |
161 // Make it safer to unpack later on. | |
162 v8::TryCatch try_catch; | |
163 local_object->Set(v8::String::New("collator"), v8::String::New("valid")); | |
164 if (try_catch.HasCaught()) { | |
165 v8::ThrowException(v8::Exception::Error( | |
166 v8::String::New("Internal error, couldn't set property."))); | |
167 return; | |
168 } | |
169 } | |
170 | |
171 v8::Persistent<v8::Object> wrapper(isolate, local_object); | |
172 // Make object handle weak so we can delete iterator once GC kicks in. | |
173 wrapper.MakeWeak<void>(NULL, &DeleteCollator); | |
174 args.GetReturnValue().Set(wrapper); | |
175 wrapper.ClearAndLeak(); | |
176 } | |
177 | |
178 static icu::Collator* InitializeCollator(v8::Handle<v8::String> locale, | |
179 v8::Handle<v8::Object> options, | |
180 v8::Handle<v8::Object> resolved) { | |
181 // Convert BCP47 into ICU locale format. | |
182 UErrorCode status = U_ZERO_ERROR; | |
183 icu::Locale icu_locale; | |
184 char icu_result[ULOC_FULLNAME_CAPACITY]; | |
185 int icu_length = 0; | |
186 v8::String::AsciiValue bcp47_locale(locale); | |
187 if (bcp47_locale.length() != 0) { | |
188 uloc_forLanguageTag(*bcp47_locale, icu_result, ULOC_FULLNAME_CAPACITY, | |
189 &icu_length, &status); | |
190 if (U_FAILURE(status) || icu_length == 0) { | |
191 return NULL; | |
192 } | |
193 icu_locale = icu::Locale(icu_result); | |
194 } | |
195 | |
196 icu::Collator* collator = CreateICUCollator(icu_locale, options); | |
197 if (!collator) { | |
198 // Remove extensions and try again. | |
199 icu::Locale no_extension_locale(icu_locale.getBaseName()); | |
200 collator = CreateICUCollator(no_extension_locale, options); | |
201 | |
202 // Set resolved settings (pattern, numbering system). | |
203 SetResolvedSettings(no_extension_locale, collator, resolved); | |
204 } else { | |
205 SetResolvedSettings(icu_locale, collator, resolved); | |
206 } | |
207 | |
208 return collator; | |
209 } | |
210 | |
211 static icu::Collator* CreateICUCollator( | |
212 const icu::Locale& icu_locale, v8::Handle<v8::Object> options) { | |
213 // Make collator from options. | |
214 icu::Collator* collator = NULL; | |
215 UErrorCode status = U_ZERO_ERROR; | |
216 collator = icu::Collator::createInstance(icu_locale, status); | |
217 | |
218 if (U_FAILURE(status)) { | |
219 delete collator; | |
220 return NULL; | |
221 } | |
222 | |
223 // Set flags first, and then override them with sensitivity if necessary. | |
224 SetBooleanAttribute(UCOL_NUMERIC_COLLATION, "numeric", options, collator); | |
225 | |
226 // Normalization is always on, by the spec. We are free to optimize | |
227 // if the strings are already normalized (but we don't have a way to tell | |
228 // that right now). | |
229 collator->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, status); | |
230 | |
231 icu::UnicodeString case_first; | |
232 if (Utils::ExtractStringSetting(options, "caseFirst", &case_first)) { | |
233 if (case_first == UNICODE_STRING_SIMPLE("upper")) { | |
234 collator->setAttribute(UCOL_CASE_FIRST, UCOL_UPPER_FIRST, status); | |
235 } else if (case_first == UNICODE_STRING_SIMPLE("lower")) { | |
236 collator->setAttribute(UCOL_CASE_FIRST, UCOL_LOWER_FIRST, status); | |
237 } else { | |
238 // Default (false/off). | |
239 collator->setAttribute(UCOL_CASE_FIRST, UCOL_OFF, status); | |
240 } | |
241 } | |
242 | |
243 icu::UnicodeString sensitivity; | |
244 if (Utils::ExtractStringSetting(options, "sensitivity", &sensitivity)) { | |
245 if (sensitivity == UNICODE_STRING_SIMPLE("base")) { | |
246 collator->setStrength(icu::Collator::PRIMARY); | |
247 } else if (sensitivity == UNICODE_STRING_SIMPLE("accent")) { | |
248 collator->setStrength(icu::Collator::SECONDARY); | |
249 } else if (sensitivity == UNICODE_STRING_SIMPLE("case")) { | |
250 collator->setStrength(icu::Collator::PRIMARY); | |
251 collator->setAttribute(UCOL_CASE_LEVEL, UCOL_ON, status); | |
252 } else { | |
253 // variant (default) | |
254 collator->setStrength(icu::Collator::TERTIARY); | |
255 } | |
256 } | |
257 | |
258 bool ignore; | |
259 if (Utils::ExtractBooleanSetting(options, "ignorePunctuation", &ignore)) { | |
260 if (ignore) { | |
261 collator->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, status); | |
262 } | |
263 } | |
264 | |
265 return collator; | |
266 } | |
267 | |
268 static bool SetBooleanAttribute(UColAttribute attribute, | |
269 const char* name, | |
270 v8::Handle<v8::Object> options, | |
271 icu::Collator* collator) { | |
272 UErrorCode status = U_ZERO_ERROR; | |
273 bool result; | |
274 if (Utils::ExtractBooleanSetting(options, name, &result)) { | |
275 collator->setAttribute(attribute, result ? UCOL_ON : UCOL_OFF, status); | |
276 if (U_FAILURE(status)) { | |
277 return false; | |
278 } | |
279 } | |
280 | |
281 return true; | |
282 } | |
283 | |
284 static void SetResolvedSettings(const icu::Locale& icu_locale, | |
285 icu::Collator* collator, | |
286 v8::Handle<v8::Object> resolved) { | |
287 SetBooleanSetting(UCOL_NUMERIC_COLLATION, collator, "numeric", resolved); | |
288 | |
289 UErrorCode status = U_ZERO_ERROR; | |
290 | |
291 switch (collator->getAttribute(UCOL_CASE_FIRST, status)) { | |
292 case UCOL_LOWER_FIRST: | |
293 resolved->Set(v8::String::New("caseFirst"), v8::String::New("lower")); | |
294 break; | |
295 case UCOL_UPPER_FIRST: | |
296 resolved->Set(v8::String::New("caseFirst"), v8::String::New("upper")); | |
297 break; | |
298 default: | |
299 resolved->Set(v8::String::New("caseFirst"), v8::String::New("false")); | |
300 } | |
301 | |
302 switch (collator->getAttribute(UCOL_STRENGTH, status)) { | |
303 case UCOL_PRIMARY: { | |
304 resolved->Set(v8::String::New("strength"), v8::String::New("primary")); | |
305 | |
306 // case level: true + s1 -> case, s1 -> base. | |
307 if (UCOL_ON == collator->getAttribute(UCOL_CASE_LEVEL, status)) { | |
308 resolved->Set(v8::String::New("sensitivity"), v8::String::New("case")); | |
309 } else { | |
310 resolved->Set(v8::String::New("sensitivity"), v8::String::New("base")); | |
311 } | |
312 break; | |
313 } | |
314 case UCOL_SECONDARY: | |
315 resolved->Set(v8::String::New("strength"), v8::String::New("secondary")); | |
316 resolved->Set(v8::String::New("sensitivity"), v8::String::New("accent")); | |
317 break; | |
318 case UCOL_TERTIARY: | |
319 resolved->Set(v8::String::New("strength"), v8::String::New("tertiary")); | |
320 resolved->Set(v8::String::New("sensitivity"), v8::String::New("variant")); | |
321 break; | |
322 case UCOL_QUATERNARY: | |
323 // We shouldn't get quaternary and identical from ICU, but if we do | |
324 // put them into variant. | |
325 resolved->Set(v8::String::New("strength"), v8::String::New("quaternary")); | |
326 resolved->Set(v8::String::New("sensitivity"), v8::String::New("variant")); | |
327 break; | |
328 default: | |
329 resolved->Set(v8::String::New("strength"), v8::String::New("identical")); | |
330 resolved->Set(v8::String::New("sensitivity"), v8::String::New("variant")); | |
331 } | |
332 | |
333 if (UCOL_SHIFTED == collator->getAttribute(UCOL_ALTERNATE_HANDLING, status)) { | |
334 resolved->Set(v8::String::New("ignorePunctuation"), | |
335 v8::Boolean::New(true)); | |
336 } else { | |
337 resolved->Set(v8::String::New("ignorePunctuation"), | |
338 v8::Boolean::New(false)); | |
339 } | |
340 | |
341 // Set the locale | |
342 char result[ULOC_FULLNAME_CAPACITY]; | |
343 status = U_ZERO_ERROR; | |
344 uloc_toLanguageTag( | |
345 icu_locale.getName(), result, ULOC_FULLNAME_CAPACITY, FALSE, &status); | |
346 if (U_SUCCESS(status)) { | |
347 resolved->Set(v8::String::New("locale"), v8::String::New(result)); | |
348 } else { | |
349 // This would never happen, since we got the locale from ICU. | |
350 resolved->Set(v8::String::New("locale"), v8::String::New("und")); | |
351 } | |
352 } | |
353 | |
354 static void SetBooleanSetting(UColAttribute attribute, | |
355 icu::Collator* collator, | |
356 const char* property, | |
357 v8::Handle<v8::Object> resolved) { | |
358 UErrorCode status = U_ZERO_ERROR; | |
359 if (UCOL_ON == collator->getAttribute(attribute, status)) { | |
360 resolved->Set(v8::String::New(property), v8::Boolean::New(true)); | |
361 } else { | |
362 resolved->Set(v8::String::New(property), v8::Boolean::New(false)); | |
363 } | |
364 } | |
365 | |
366 } // namespace v8_i18n | |
OLD | NEW |