|
OLD | NEW |
---|---|
(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. Deleting NULL is ok. | |
Mads Ager (chromium)
2011/03/01 18:58:32
Unpacking should never give NULL here. That will o
Nebojša Ćirić
2011/03/01 19:09:12
Done.
| |
55 delete UnpackBreakIterator(persistent_object); | |
56 | |
57 // Then dispose of the persistent handle to JS object. | |
58 persistent_object.Dispose(); | |
59 } | |
60 | |
61 // Returns a ThrowException value in case we encounter invalid | |
Mads Ager (chromium)
2011/03/01 18:58:32
ThrowException returns undefined and schedules an
Nebojša Ćirić
2011/03/01 19:09:12
Done.
| |
62 // object in UnpackBreakIterator method. | |
63 static v8::Handle<v8::Value> ThrowUnexpectedObjectError() { | |
64 return v8::ThrowException(v8::Exception::Error( | |
65 v8::String::New("BreakIterator method called on an object " | |
66 "that is not a BreakIterator."))); | |
67 } | |
68 | |
69 v8::Handle<v8::Value> BreakIterator::BreakIteratorAdoptText( | |
70 const v8::Arguments& args) { | |
71 if (args.Length() != 1 || !args[0]->IsString()) { | |
72 return v8::ThrowException(v8::Exception::SyntaxError( | |
73 v8::String::New("Text input is required."))); | |
74 } | |
75 | |
76 icu::BreakIterator* break_iterator = UnpackBreakIterator(args.Holder()); | |
77 if (!break_iterator) { | |
78 return ThrowUnexpectedObjectError(); | |
79 } | |
80 | |
81 v8::Local<v8::String> text_value = args[0]->ToString(); | |
82 UnicodeString text(*v8::String::Value(text_value), | |
83 text_value->Length()); | |
84 | |
85 break_iterator->setText(text); | |
86 | |
87 return v8::Undefined(); | |
88 } | |
89 | |
90 v8::Handle<v8::Value> BreakIterator::BreakIteratorFirst( | |
91 const v8::Arguments& args) { | |
92 icu::BreakIterator* break_iterator = UnpackBreakIterator(args.Holder()); | |
93 if (!break_iterator) { | |
94 return ThrowUnexpectedObjectError(); | |
95 } | |
96 | |
97 return v8::Int32::New(break_iterator->first()); | |
98 } | |
99 | |
100 v8::Handle<v8::Value> BreakIterator::BreakIteratorNext( | |
101 const v8::Arguments& args) { | |
102 icu::BreakIterator* break_iterator = UnpackBreakIterator(args.Holder()); | |
103 if (!break_iterator) { | |
104 return ThrowUnexpectedObjectError(); | |
105 } | |
106 | |
107 return v8::Int32::New(break_iterator->next()); | |
108 } | |
109 | |
110 v8::Handle<v8::Value> BreakIterator::BreakIteratorCurrent( | |
111 const v8::Arguments& args) { | |
112 icu::BreakIterator* break_iterator = UnpackBreakIterator(args.Holder()); | |
113 if (!break_iterator) { | |
114 return ThrowUnexpectedObjectError(); | |
115 } | |
116 | |
117 return v8::Int32::New(break_iterator->current()); | |
118 } | |
119 | |
120 v8::Handle<v8::Value> BreakIterator::BreakIteratorBreakType( | |
121 const v8::Arguments& args) { | |
122 icu::BreakIterator* break_iterator = UnpackBreakIterator(args.Holder()); | |
123 if (!break_iterator) { | |
124 return ThrowUnexpectedObjectError(); | |
125 } | |
126 | |
127 // TODO(cira): Remove cast once ICU fixes base BreakIterator class. | |
128 int32_t status = | |
129 static_cast<RuleBasedBreakIterator*>(break_iterator)->getRuleStatus(); | |
130 // Keep return values in sync with JavaScript BreakType enum. | |
131 if (status >= UBRK_WORD_NONE && status < UBRK_WORD_NONE_LIMIT) { | |
132 return v8::Int32::New(UBRK_WORD_NONE); | |
133 } else if (status >= UBRK_WORD_NUMBER && status < UBRK_WORD_NUMBER_LIMIT) { | |
134 return v8::Int32::New(UBRK_WORD_NUMBER); | |
135 } else if (status >= UBRK_WORD_LETTER && status < UBRK_WORD_LETTER_LIMIT) { | |
136 return v8::Int32::New(UBRK_WORD_LETTER); | |
137 } else if (status >= UBRK_WORD_KANA && status < UBRK_WORD_KANA_LIMIT) { | |
138 return v8::Int32::New(UBRK_WORD_KANA); | |
139 } else if (status >= UBRK_WORD_IDEO && status < UBRK_WORD_IDEO_LIMIT) { | |
140 return v8::Int32::New(UBRK_WORD_IDEO); | |
141 } else { | |
142 return v8::Int32::New(-1); | |
143 } | |
144 } | |
145 | |
146 v8::Handle<v8::Value> BreakIterator::JSBreakIterator( | |
147 const v8::Arguments& args) { | |
148 v8::HandleScope handle_scope; | |
149 | |
150 if (args.Length() != 2 || !args[0]->IsString() || !args[1]->IsString()) { | |
151 return v8::ThrowException(v8::Exception::SyntaxError( | |
152 v8::String::New("Locale and iterator type are required."))); | |
153 } | |
154 | |
155 const char* locale = *v8::String::Utf8Value(args[0]->ToString()); | |
156 icu::Locale icu_locale(locale); | |
157 | |
158 UErrorCode status = U_ZERO_ERROR; | |
159 icu::BreakIterator* break_iterator = NULL; | |
160 const char* type = *v8::String::Utf8Value(args[1]->ToString()); | |
161 if (!strcmp(type, "character")) { | |
162 break_iterator = | |
163 icu::BreakIterator::createCharacterInstance(icu_locale, status); | |
164 } else if (!strcmp(type, "word")) { | |
165 break_iterator = | |
166 icu::BreakIterator::createWordInstance(icu_locale, status); | |
167 } else if (!strcmp(type, "sentence")) { | |
168 break_iterator = | |
169 icu::BreakIterator::createSentenceInstance(icu_locale, status); | |
170 } else if (!strcmp(type, "line")) { | |
171 break_iterator = | |
172 icu::BreakIterator::createLineInstance(icu_locale, status); | |
173 } else { | |
174 return v8::ThrowException(v8::Exception::SyntaxError( | |
175 v8::String::New("Invalid iterator type."))); | |
176 } | |
177 | |
178 if (U_FAILURE(status)) { | |
179 delete break_iterator; | |
180 return v8::ThrowException(v8::Exception::Error( | |
181 v8::String::New("Failed to create break iterator."))); | |
182 } | |
183 | |
184 if (break_iterator_template_.IsEmpty()) { | |
185 v8::Local<v8::FunctionTemplate> raw_template(v8::FunctionTemplate::New()); | |
186 | |
187 raw_template->SetClassName(v8::String::New("v8Locale.v8BreakIterator")); | |
188 | |
189 // Define internal field count on instance template. | |
190 v8::Local<v8::ObjectTemplate> object_template = | |
191 raw_template->InstanceTemplate(); | |
192 object_template->SetInternalFieldCount(1); | |
193 | |
194 // Define all of the prototype methods on prototype template. | |
195 v8::Local<v8::ObjectTemplate> proto = raw_template->PrototypeTemplate(); | |
196 proto->Set(v8::String::New("adoptText"), | |
197 v8::FunctionTemplate::New(BreakIteratorAdoptText)); | |
198 proto->Set(v8::String::New("first"), | |
199 v8::FunctionTemplate::New(BreakIteratorFirst)); | |
200 proto->Set(v8::String::New("next"), | |
201 v8::FunctionTemplate::New(BreakIteratorNext)); | |
202 proto->Set(v8::String::New("current"), | |
203 v8::FunctionTemplate::New(BreakIteratorCurrent)); | |
204 proto->Set(v8::String::New("breakType"), | |
205 v8::FunctionTemplate::New(BreakIteratorBreakType)); | |
206 | |
207 break_iterator_template_ = | |
208 v8::Persistent<v8::FunctionTemplate>::New(raw_template); | |
209 } | |
210 | |
211 // Create an empty object wrapper. | |
212 v8::Local<v8::Object> local_object = | |
213 break_iterator_template_->GetFunction()->NewInstance(); | |
214 v8::Persistent<v8::Object> wrapper = | |
215 v8::Persistent<v8::Object>::New(local_object); | |
216 | |
217 // Set break iterator as internal field of the resulting JS object. | |
218 wrapper->SetPointerInInternalField(0, break_iterator); | |
219 | |
220 // Make object handle weak so we can delete iterator once GC kicks in. | |
221 wrapper.MakeWeak(NULL, DeleteBreakIterator); | |
222 | |
223 return wrapper; | |
224 } | |
225 | |
226 } } // namespace v8::internal | |
OLD | NEW |