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

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

Issue 6598014: Adding break iterator support to the i18n api extension.... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 9 years, 9 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
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright 2011 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 "unicode/brkiter.h"
31 #include "unicode/locid.h"
32 #include "unicode/rbbi.h"
33
34 namespace v8 {
35 namespace internal {
36
37 v8::Persistent<v8::FunctionTemplate> BreakIterator::break_iterator_template_;
38
39 icu::BreakIterator* BreakIterator::UnpackBreakIterator(
40 v8::Handle<v8::Object> obj) {
41 if (break_iterator_template_->HasInstance(obj)) {
42 return static_cast<icu::BreakIterator*>(
43 obj->GetPointerFromInternalField(0));
44 }
45
46 return NULL;
47 }
48
49 void BreakIterator::DeleteBreakIterator(v8::Persistent<v8::Value> object,
50 void* param) {
51 v8::Persistent<v8::Object> persistent_object =
52 v8::Persistent<v8::Object>::Cast(object);
53
54 // First delete the hidden C++ object.
55 // Unpacking should never return NULL here. That would only happen if
56 // this method is used as the weak callback for persistent handles not
57 // pointing to a break iterator.
58 delete UnpackBreakIterator(persistent_object);
59
60 // Then dispose of the persistent handle to JS object.
61 persistent_object.Dispose();
62 }
63
64 // Throws a JavaScript exception.
65 static v8::Handle<v8::Value> ThrowUnexpectedObjectError() {
66 // Returns undefined, and schedules an exception to be thrown.
67 return v8::ThrowException(v8::Exception::Error(
68 v8::String::New("BreakIterator method called on an object "
69 "that is not a BreakIterator.")));
70 }
71
72 v8::Handle<v8::Value> BreakIterator::BreakIteratorAdoptText(
73 const v8::Arguments& args) {
74 if (args.Length() != 1 || !args[0]->IsString()) {
75 return v8::ThrowException(v8::Exception::SyntaxError(
76 v8::String::New("Text input is required.")));
77 }
78
79 icu::BreakIterator* break_iterator = UnpackBreakIterator(args.Holder());
80 if (!break_iterator) {
81 return ThrowUnexpectedObjectError();
82 }
83
84 v8::Local<v8::String> text_value = args[0]->ToString();
85 UnicodeString text(*v8::String::Value(text_value),
86 text_value->Length());
87
88 break_iterator->setText(text);
89
90 return v8::Undefined();
91 }
92
93 v8::Handle<v8::Value> BreakIterator::BreakIteratorFirst(
94 const v8::Arguments& args) {
95 icu::BreakIterator* break_iterator = UnpackBreakIterator(args.Holder());
96 if (!break_iterator) {
97 return ThrowUnexpectedObjectError();
98 }
99
100 return v8::Int32::New(break_iterator->first());
101 }
102
103 v8::Handle<v8::Value> BreakIterator::BreakIteratorNext(
104 const v8::Arguments& args) {
105 icu::BreakIterator* break_iterator = UnpackBreakIterator(args.Holder());
106 if (!break_iterator) {
107 return ThrowUnexpectedObjectError();
108 }
109
110 return v8::Int32::New(break_iterator->next());
111 }
112
113 v8::Handle<v8::Value> BreakIterator::BreakIteratorCurrent(
114 const v8::Arguments& args) {
115 icu::BreakIterator* break_iterator = UnpackBreakIterator(args.Holder());
116 if (!break_iterator) {
117 return ThrowUnexpectedObjectError();
118 }
119
120 return v8::Int32::New(break_iterator->current());
121 }
122
123 v8::Handle<v8::Value> BreakIterator::BreakIteratorBreakType(
124 const v8::Arguments& args) {
125 icu::BreakIterator* break_iterator = UnpackBreakIterator(args.Holder());
126 if (!break_iterator) {
127 return ThrowUnexpectedObjectError();
128 }
129
130 // TODO(cira): Remove cast once ICU fixes base BreakIterator class.
131 int32_t status =
132 static_cast<RuleBasedBreakIterator*>(break_iterator)->getRuleStatus();
133 // Keep return values in sync with JavaScript BreakType enum.
134 if (status >= UBRK_WORD_NONE && status < UBRK_WORD_NONE_LIMIT) {
135 return v8::Int32::New(UBRK_WORD_NONE);
136 } else if (status >= UBRK_WORD_NUMBER && status < UBRK_WORD_NUMBER_LIMIT) {
137 return v8::Int32::New(UBRK_WORD_NUMBER);
138 } else if (status >= UBRK_WORD_LETTER && status < UBRK_WORD_LETTER_LIMIT) {
139 return v8::Int32::New(UBRK_WORD_LETTER);
140 } else if (status >= UBRK_WORD_KANA && status < UBRK_WORD_KANA_LIMIT) {
141 return v8::Int32::New(UBRK_WORD_KANA);
142 } else if (status >= UBRK_WORD_IDEO && status < UBRK_WORD_IDEO_LIMIT) {
143 return v8::Int32::New(UBRK_WORD_IDEO);
144 } else {
145 return v8::Int32::New(-1);
146 }
147 }
148
149 v8::Handle<v8::Value> BreakIterator::JSBreakIterator(
150 const v8::Arguments& args) {
151 v8::HandleScope handle_scope;
152
153 if (args.Length() != 2 || !args[0]->IsString() || !args[1]->IsString()) {
154 return v8::ThrowException(v8::Exception::SyntaxError(
155 v8::String::New("Locale and iterator type are required.")));
156 }
157
158 const char* locale = *v8::String::Utf8Value(args[0]->ToString());
159 icu::Locale icu_locale(locale);
160
161 UErrorCode status = U_ZERO_ERROR;
162 icu::BreakIterator* break_iterator = NULL;
163 const char* type = *v8::String::Utf8Value(args[1]->ToString());
164 if (!strcmp(type, "character")) {
165 break_iterator =
166 icu::BreakIterator::createCharacterInstance(icu_locale, status);
167 } else if (!strcmp(type, "word")) {
168 break_iterator =
169 icu::BreakIterator::createWordInstance(icu_locale, status);
170 } else if (!strcmp(type, "sentence")) {
171 break_iterator =
172 icu::BreakIterator::createSentenceInstance(icu_locale, status);
173 } else if (!strcmp(type, "line")) {
174 break_iterator =
175 icu::BreakIterator::createLineInstance(icu_locale, status);
176 } else {
177 return v8::ThrowException(v8::Exception::SyntaxError(
178 v8::String::New("Invalid iterator type.")));
179 }
180
181 if (U_FAILURE(status)) {
182 delete break_iterator;
183 return v8::ThrowException(v8::Exception::Error(
184 v8::String::New("Failed to create break iterator.")));
185 }
186
187 if (break_iterator_template_.IsEmpty()) {
188 v8::Local<v8::FunctionTemplate> raw_template(v8::FunctionTemplate::New());
189
190 raw_template->SetClassName(v8::String::New("v8Locale.v8BreakIterator"));
191
192 // Define internal field count on instance template.
193 v8::Local<v8::ObjectTemplate> object_template =
194 raw_template->InstanceTemplate();
195 object_template->SetInternalFieldCount(1);
196
197 // Define all of the prototype methods on prototype template.
198 v8::Local<v8::ObjectTemplate> proto = raw_template->PrototypeTemplate();
199 proto->Set(v8::String::New("adoptText"),
200 v8::FunctionTemplate::New(BreakIteratorAdoptText));
201 proto->Set(v8::String::New("first"),
202 v8::FunctionTemplate::New(BreakIteratorFirst));
203 proto->Set(v8::String::New("next"),
204 v8::FunctionTemplate::New(BreakIteratorNext));
205 proto->Set(v8::String::New("current"),
206 v8::FunctionTemplate::New(BreakIteratorCurrent));
207 proto->Set(v8::String::New("breakType"),
208 v8::FunctionTemplate::New(BreakIteratorBreakType));
209
210 break_iterator_template_ =
211 v8::Persistent<v8::FunctionTemplate>::New(raw_template);
212 }
213
214 // Create an empty object wrapper.
215 v8::Local<v8::Object> local_object =
216 break_iterator_template_->GetFunction()->NewInstance();
217 v8::Persistent<v8::Object> wrapper =
218 v8::Persistent<v8::Object>::New(local_object);
219
220 // Set break iterator as internal field of the resulting JS object.
221 wrapper->SetPointerInInternalField(0, break_iterator);
222
223 // Make object handle weak so we can delete iterator once GC kicks in.
224 wrapper.MakeWeak(NULL, DeleteBreakIterator);
225
226 return wrapper;
227 }
228
229 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/extensions/experimental/break-iterator.h ('k') | src/extensions/experimental/experimental.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698