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