Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(683)

Side by Side Diff: src/extensions/i18n/break-iterator.cc

Issue 18487004: Import the v8-i18n extension into v8 (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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
28 #include "break-iterator.h"
29
30 #include <string.h>
31
32 #include "i18n-utils.h"
33 #include "unicode/brkiter.h"
34 #include "unicode/locid.h"
35 #include "unicode/rbbi.h"
36
37 namespace v8_i18n {
38
39 static v8::Handle<v8::Value> ThrowUnexpectedObjectError();
40 static icu::UnicodeString* ResetAdoptedText(v8::Handle<v8::Object>,
41 v8::Handle<v8::Value>);
42 static icu::BreakIterator* InitializeBreakIterator(v8::Handle<v8::String>,
43 v8::Handle<v8::Object>,
44 v8::Handle<v8::Object>);
45 static icu::BreakIterator* CreateICUBreakIterator(const icu::Locale&,
46 v8::Handle<v8::Object>);
47 static void SetResolvedSettings(const icu::Locale&,
48 icu::BreakIterator*,
49 v8::Handle<v8::Object>);
50
51 icu::BreakIterator* BreakIterator::UnpackBreakIterator(
52 v8::Handle<v8::Object> obj) {
53 v8::HandleScope handle_scope;
54
55 // v8::ObjectTemplate doesn't have HasInstance method so we can't check
56 // if obj is an instance of BreakIterator class. We'll check for a property
57 // that has to be in the object. The same applies to other services, like
58 // Collator and DateTimeFormat.
59 if (obj->HasOwnProperty(v8::String::New("breakIterator"))) {
60 return static_cast<icu::BreakIterator*>(
61 obj->GetAlignedPointerFromInternalField(0));
62 }
63
64 return NULL;
65 }
66
67 void BreakIterator::DeleteBreakIterator(v8::Isolate* isolate,
68 v8::Persistent<v8::Object>* object,
69 void* param) {
70 // First delete the hidden C++ object.
71 // Unpacking should never return NULL here. That would only happen if
72 // this method is used as the weak callback for persistent handles not
73 // pointing to a break iterator.
74 v8::HandleScope handle_scope(isolate);
75 v8::Local<v8::Object> handle = v8::Local<v8::Object>::New(isolate, *object);
76 delete UnpackBreakIterator(handle);
77
78 delete static_cast<icu::UnicodeString*>(
79 handle->GetAlignedPointerFromInternalField(1));
80
81 // Then dispose of the persistent handle to JS object.
82 object->Dispose(isolate);
83 }
84
85 // Throws a JavaScript exception.
86 static v8::Handle<v8::Value> ThrowUnexpectedObjectError() {
87 // Returns undefined, and schedules an exception to be thrown.
88 return v8::ThrowException(v8::Exception::Error(
89 v8::String::New("BreakIterator method called on an object "
90 "that is not a BreakIterator.")));
91 }
92
93 // Deletes the old value and sets the adopted text in corresponding
94 // JavaScript object.
95 icu::UnicodeString* ResetAdoptedText(
96 v8::Handle<v8::Object> obj, v8::Handle<v8::Value> value) {
97 // Get the previous value from the internal field.
98 icu::UnicodeString* text = static_cast<icu::UnicodeString*>(
99 obj->GetAlignedPointerFromInternalField(1));
100 delete text;
101
102 // Assign new value to the internal pointer.
103 v8::String::Value text_value(value);
104 text = new icu::UnicodeString(
105 reinterpret_cast<const UChar*>(*text_value), text_value.length());
106 obj->SetAlignedPointerInInternalField(1, text);
107
108 // Return new unicode string pointer.
109 return text;
110 }
111
112 void BreakIterator::JSInternalBreakIteratorAdoptText(
113 const v8::FunctionCallbackInfo<v8::Value>& args) {
114 if (args.Length() != 2 || !args[0]->IsObject() || !args[1]->IsString()) {
115 v8::ThrowException(v8::Exception::Error(
116 v8::String::New(
117 "Internal error. Iterator and text have to be specified.")));
118 return;
119 }
120
121 icu::BreakIterator* break_iterator = UnpackBreakIterator(args[0]->ToObject());
122 if (!break_iterator) {
123 ThrowUnexpectedObjectError();
124 return;
125 }
126
127 break_iterator->setText(*ResetAdoptedText(args[0]->ToObject(), args[1]));
128 }
129
130 void BreakIterator::JSInternalBreakIteratorFirst(
131 const v8::FunctionCallbackInfo<v8::Value>& args) {
132 icu::BreakIterator* break_iterator = UnpackBreakIterator(args[0]->ToObject());
133 if (!break_iterator) {
134 ThrowUnexpectedObjectError();
135 return;
136 }
137
138 args.GetReturnValue().Set(static_cast<int32_t>(break_iterator->first()));
139 }
140
141 void BreakIterator::JSInternalBreakIteratorNext(
142 const v8::FunctionCallbackInfo<v8::Value>& args) {
143 icu::BreakIterator* break_iterator = UnpackBreakIterator(args[0]->ToObject());
144 if (!break_iterator) {
145 ThrowUnexpectedObjectError();
146 return;
147 }
148
149 args.GetReturnValue().Set(static_cast<int32_t>(break_iterator->next()));
150 }
151
152 void BreakIterator::JSInternalBreakIteratorCurrent(
153 const v8::FunctionCallbackInfo<v8::Value>& args) {
154 icu::BreakIterator* break_iterator = UnpackBreakIterator(args[0]->ToObject());
155 if (!break_iterator) {
156 ThrowUnexpectedObjectError();
157 return;
158 }
159
160 args.GetReturnValue().Set(static_cast<int32_t>(break_iterator->current()));
161 }
162
163 void BreakIterator::JSInternalBreakIteratorBreakType(
164 const v8::FunctionCallbackInfo<v8::Value>& args) {
165 icu::BreakIterator* break_iterator = UnpackBreakIterator(args[0]->ToObject());
166 if (!break_iterator) {
167 ThrowUnexpectedObjectError();
168 return;
169 }
170
171 // TODO(cira): Remove cast once ICU fixes base BreakIterator class.
172 icu::RuleBasedBreakIterator* rule_based_iterator =
173 static_cast<icu::RuleBasedBreakIterator*>(break_iterator);
174 int32_t status = rule_based_iterator->getRuleStatus();
175 // Keep return values in sync with JavaScript BreakType enum.
176 v8::Handle<v8::String> result;
177 if (status >= UBRK_WORD_NONE && status < UBRK_WORD_NONE_LIMIT) {
178 result = v8::String::New("none");
179 } else if (status >= UBRK_WORD_NUMBER && status < UBRK_WORD_NUMBER_LIMIT) {
180 result = v8::String::New("number");
181 } else if (status >= UBRK_WORD_LETTER && status < UBRK_WORD_LETTER_LIMIT) {
182 result = v8::String::New("letter");
183 } else if (status >= UBRK_WORD_KANA && status < UBRK_WORD_KANA_LIMIT) {
184 result = v8::String::New("kana");
185 } else if (status >= UBRK_WORD_IDEO && status < UBRK_WORD_IDEO_LIMIT) {
186 result = v8::String::New("ideo");
187 } else {
188 result = v8::String::New("unknown");
189 }
190 args.GetReturnValue().Set(result);
191 }
192
193 void BreakIterator::JSCreateBreakIterator(
194 const v8::FunctionCallbackInfo<v8::Value>& args) {
195 if (args.Length() != 3 || !args[0]->IsString() || !args[1]->IsObject() ||
196 !args[2]->IsObject()) {
197 v8::ThrowException(v8::Exception::Error(
198 v8::String::New("Internal error, wrong parameters.")));
199 return;
200 }
201
202 v8::Isolate* isolate = args.GetIsolate();
203 v8::Local<v8::ObjectTemplate> break_iterator_template =
204 Utils::GetTemplate2(isolate);
205
206 // Create an empty object wrapper.
207 v8::Local<v8::Object> local_object = break_iterator_template->NewInstance();
208 // But the handle shouldn't be empty.
209 // That can happen if there was a stack overflow when creating the object.
210 if (local_object.IsEmpty()) {
211 args.GetReturnValue().Set(local_object);
212 return;
213 }
214
215 // Set break iterator as internal field of the resulting JS object.
216 icu::BreakIterator* break_iterator = InitializeBreakIterator(
217 args[0]->ToString(), args[1]->ToObject(), args[2]->ToObject());
218
219 if (!break_iterator) {
220 v8::ThrowException(v8::Exception::Error(v8::String::New(
221 "Internal error. Couldn't create ICU break iterator.")));
222 return;
223 } else {
224 local_object->SetAlignedPointerInInternalField(0, break_iterator);
225 // Make sure that the pointer to adopted text is NULL.
226 local_object->SetAlignedPointerInInternalField(1, NULL);
227
228 v8::TryCatch try_catch;
229 local_object->Set(v8::String::New("breakIterator"),
230 v8::String::New("valid"));
231 if (try_catch.HasCaught()) {
232 v8::ThrowException(v8::Exception::Error(
233 v8::String::New("Internal error, couldn't set property.")));
234 return;
235 }
236 }
237
238 v8::Persistent<v8::Object> wrapper(isolate, local_object);
239 // Make object handle weak so we can delete iterator once GC kicks in.
240 wrapper.MakeWeak<void>(NULL, &DeleteBreakIterator);
241 args.GetReturnValue().Set(wrapper);
242 wrapper.ClearAndLeak();
243 }
244
245 static icu::BreakIterator* InitializeBreakIterator(
246 v8::Handle<v8::String> locale,
247 v8::Handle<v8::Object> options,
248 v8::Handle<v8::Object> resolved) {
249 // Convert BCP47 into ICU locale format.
250 UErrorCode status = U_ZERO_ERROR;
251 icu::Locale icu_locale;
252 char icu_result[ULOC_FULLNAME_CAPACITY];
253 int icu_length = 0;
254 v8::String::AsciiValue bcp47_locale(locale);
255 if (bcp47_locale.length() != 0) {
256 uloc_forLanguageTag(*bcp47_locale, icu_result, ULOC_FULLNAME_CAPACITY,
257 &icu_length, &status);
258 if (U_FAILURE(status) || icu_length == 0) {
259 return NULL;
260 }
261 icu_locale = icu::Locale(icu_result);
262 }
263
264 icu::BreakIterator* break_iterator =
265 CreateICUBreakIterator(icu_locale, options);
266 if (!break_iterator) {
267 // Remove extensions and try again.
268 icu::Locale no_extension_locale(icu_locale.getBaseName());
269 break_iterator = CreateICUBreakIterator(no_extension_locale, options);
270
271 // Set resolved settings (locale).
272 SetResolvedSettings(no_extension_locale, break_iterator, resolved);
273 } else {
274 SetResolvedSettings(icu_locale, break_iterator, resolved);
275 }
276
277 return break_iterator;
278 }
279
280 static icu::BreakIterator* CreateICUBreakIterator(
281 const icu::Locale& icu_locale, v8::Handle<v8::Object> options) {
282 UErrorCode status = U_ZERO_ERROR;
283 icu::BreakIterator* break_iterator = NULL;
284 icu::UnicodeString type;
285 if (!Utils::ExtractStringSetting(options, "type", &type)) {
286 // Type had to be in the options. This would be an internal error.
287 return NULL;
288 }
289
290 if (type == UNICODE_STRING_SIMPLE("character")) {
291 break_iterator =
292 icu::BreakIterator::createCharacterInstance(icu_locale, status);
293 } else if (type == UNICODE_STRING_SIMPLE("sentence")) {
294 break_iterator =
295 icu::BreakIterator::createSentenceInstance(icu_locale, status);
296 } else if (type == UNICODE_STRING_SIMPLE("line")) {
297 break_iterator =
298 icu::BreakIterator::createLineInstance(icu_locale, status);
299 } else {
300 // Defualt is word iterator.
301 break_iterator =
302 icu::BreakIterator::createWordInstance(icu_locale, status);
303 }
304
305 if (U_FAILURE(status)) {
306 delete break_iterator;
307 return NULL;
308 }
309
310 return break_iterator;
311 }
312
313 static void SetResolvedSettings(const icu::Locale& icu_locale,
314 icu::BreakIterator* date_format,
315 v8::Handle<v8::Object> resolved) {
316 UErrorCode status = U_ZERO_ERROR;
317
318 // Set the locale
319 char result[ULOC_FULLNAME_CAPACITY];
320 status = U_ZERO_ERROR;
321 uloc_toLanguageTag(
322 icu_locale.getName(), result, ULOC_FULLNAME_CAPACITY, FALSE, &status);
323 if (U_SUCCESS(status)) {
324 resolved->Set(v8::String::New("locale"), v8::String::New(result));
325 } else {
326 // This would never happen, since we got the locale from ICU.
327 resolved->Set(v8::String::New("locale"), v8::String::New("und"));
328 }
329 }
330
331 } // namespace v8_i18n
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698