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

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

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

Powered by Google App Engine
This is Rietveld 408576698