| 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. | |
| 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 const UChar* text = | |
| 86 reinterpret_cast<const UChar*>(*v8::String::Value(text_value)); | |
| 87 UnicodeString text(text, text_value->Length()); | |
| 88 | |
| 89 break_iterator->setText(text); | |
| 90 | |
| 91 return v8::Undefined(); | |
| 92 } | |
| 93 | |
| 94 v8::Handle<v8::Value> BreakIterator::BreakIteratorFirst( | |
| 95 const v8::Arguments& args) { | |
| 96 icu::BreakIterator* break_iterator = UnpackBreakIterator(args.Holder()); | |
| 97 if (!break_iterator) { | |
| 98 return ThrowUnexpectedObjectError(); | |
| 99 } | |
| 100 | |
| 101 return v8::Int32::New(break_iterator->first()); | |
| 102 } | |
| 103 | |
| 104 v8::Handle<v8::Value> BreakIterator::BreakIteratorNext( | |
| 105 const v8::Arguments& args) { | |
| 106 icu::BreakIterator* break_iterator = UnpackBreakIterator(args.Holder()); | |
| 107 if (!break_iterator) { | |
| 108 return ThrowUnexpectedObjectError(); | |
| 109 } | |
| 110 | |
| 111 return v8::Int32::New(break_iterator->next()); | |
| 112 } | |
| 113 | |
| 114 v8::Handle<v8::Value> BreakIterator::BreakIteratorCurrent( | |
| 115 const v8::Arguments& args) { | |
| 116 icu::BreakIterator* break_iterator = UnpackBreakIterator(args.Holder()); | |
| 117 if (!break_iterator) { | |
| 118 return ThrowUnexpectedObjectError(); | |
| 119 } | |
| 120 | |
| 121 return v8::Int32::New(break_iterator->current()); | |
| 122 } | |
| 123 | |
| 124 v8::Handle<v8::Value> BreakIterator::BreakIteratorBreakType( | |
| 125 const v8::Arguments& args) { | |
| 126 icu::BreakIterator* break_iterator = UnpackBreakIterator(args.Holder()); | |
| 127 if (!break_iterator) { | |
| 128 return ThrowUnexpectedObjectError(); | |
| 129 } | |
| 130 | |
| 131 // TODO(cira): Remove cast once ICU fixes base BreakIterator class. | |
| 132 int32_t status = | |
| 133 static_cast<RuleBasedBreakIterator*>(break_iterator)->getRuleStatus(); | |
| 134 // Keep return values in sync with JavaScript BreakType enum. | |
| 135 if (status >= UBRK_WORD_NONE && status < UBRK_WORD_NONE_LIMIT) { | |
| 136 return v8::Int32::New(UBRK_WORD_NONE); | |
| 137 } else if (status >= UBRK_WORD_NUMBER && status < UBRK_WORD_NUMBER_LIMIT) { | |
| 138 return v8::Int32::New(UBRK_WORD_NUMBER); | |
| 139 } else if (status >= UBRK_WORD_LETTER && status < UBRK_WORD_LETTER_LIMIT) { | |
| 140 return v8::Int32::New(UBRK_WORD_LETTER); | |
| 141 } else if (status >= UBRK_WORD_KANA && status < UBRK_WORD_KANA_LIMIT) { | |
| 142 return v8::Int32::New(UBRK_WORD_KANA); | |
| 143 } else if (status >= UBRK_WORD_IDEO && status < UBRK_WORD_IDEO_LIMIT) { | |
| 144 return v8::Int32::New(UBRK_WORD_IDEO); | |
| 145 } else { | |
| 146 return v8::Int32::New(-1); | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 v8::Handle<v8::Value> BreakIterator::JSBreakIterator( | |
| 151 const v8::Arguments& args) { | |
| 152 v8::HandleScope handle_scope; | |
| 153 | |
| 154 if (args.Length() != 2 || !args[0]->IsString() || !args[1]->IsString()) { | |
| 155 return v8::ThrowException(v8::Exception::SyntaxError( | |
| 156 v8::String::New("Locale and iterator type are required."))); | |
| 157 } | |
| 158 | |
| 159 const char* locale = *v8::String::Utf8Value(args[0]->ToString()); | |
| 160 icu::Locale icu_locale(locale); | |
| 161 | |
| 162 UErrorCode status = U_ZERO_ERROR; | |
| 163 icu::BreakIterator* break_iterator = NULL; | |
| 164 const char* type = *v8::String::Utf8Value(args[1]->ToString()); | |
| 165 if (!strcmp(type, "character")) { | |
| 166 break_iterator = | |
| 167 icu::BreakIterator::createCharacterInstance(icu_locale, status); | |
| 168 } else if (!strcmp(type, "word")) { | |
| 169 break_iterator = | |
| 170 icu::BreakIterator::createWordInstance(icu_locale, status); | |
| 171 } else if (!strcmp(type, "sentence")) { | |
| 172 break_iterator = | |
| 173 icu::BreakIterator::createSentenceInstance(icu_locale, status); | |
| 174 } else if (!strcmp(type, "line")) { | |
| 175 break_iterator = | |
| 176 icu::BreakIterator::createLineInstance(icu_locale, status); | |
| 177 } else { | |
| 178 return v8::ThrowException(v8::Exception::SyntaxError( | |
| 179 v8::String::New("Invalid iterator type."))); | |
| 180 } | |
| 181 | |
| 182 if (U_FAILURE(status)) { | |
| 183 delete break_iterator; | |
| 184 return v8::ThrowException(v8::Exception::Error( | |
| 185 v8::String::New("Failed to create break iterator."))); | |
| 186 } | |
| 187 | |
| 188 if (break_iterator_template_.IsEmpty()) { | |
| 189 v8::Local<v8::FunctionTemplate> raw_template(v8::FunctionTemplate::New()); | |
| 190 | |
| 191 raw_template->SetClassName(v8::String::New("v8Locale.v8BreakIterator")); | |
| 192 | |
| 193 // Define internal field count on instance template. | |
| 194 v8::Local<v8::ObjectTemplate> object_template = | |
| 195 raw_template->InstanceTemplate(); | |
| 196 object_template->SetInternalFieldCount(1); | |
| 197 | |
| 198 // Define all of the prototype methods on prototype template. | |
| 199 v8::Local<v8::ObjectTemplate> proto = raw_template->PrototypeTemplate(); | |
| 200 proto->Set(v8::String::New("adoptText"), | |
| 201 v8::FunctionTemplate::New(BreakIteratorAdoptText)); | |
| 202 proto->Set(v8::String::New("first"), | |
| 203 v8::FunctionTemplate::New(BreakIteratorFirst)); | |
| 204 proto->Set(v8::String::New("next"), | |
| 205 v8::FunctionTemplate::New(BreakIteratorNext)); | |
| 206 proto->Set(v8::String::New("current"), | |
| 207 v8::FunctionTemplate::New(BreakIteratorCurrent)); | |
| 208 proto->Set(v8::String::New("breakType"), | |
| 209 v8::FunctionTemplate::New(BreakIteratorBreakType)); | |
| 210 | |
| 211 break_iterator_template_ = | |
| 212 v8::Persistent<v8::FunctionTemplate>::New(raw_template); | |
| 213 } | |
| 214 | |
| 215 // Create an empty object wrapper. | |
| 216 v8::Local<v8::Object> local_object = | |
| 217 break_iterator_template_->GetFunction()->NewInstance(); | |
| 218 v8::Persistent<v8::Object> wrapper = | |
| 219 v8::Persistent<v8::Object>::New(local_object); | |
| 220 | |
| 221 // Set break iterator as internal field of the resulting JS object. | |
| 222 wrapper->SetPointerInInternalField(0, break_iterator); | |
| 223 | |
| 224 // Make object handle weak so we can delete iterator once GC kicks in. | |
| 225 wrapper.MakeWeak(NULL, DeleteBreakIterator); | |
| 226 | |
| 227 return wrapper; | |
| 228 } | |
| 229 | |
| 230 } } // namespace v8::internal | |
| OLD | NEW |