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 #include "unicode/uloc.h" |
| 34 |
| 35 namespace v8 { |
| 36 namespace internal { |
| 37 |
| 38 v8::Persistent<v8::FunctionTemplate> BreakIterator::break_iterator_template_; |
| 39 |
| 40 icu::BreakIterator* BreakIterator::UnpackBreakIterator( |
| 41 v8::Handle<v8::Object> obj) { |
| 42 if (break_iterator_template_->HasInstance(obj)) { |
| 43 return static_cast<icu::BreakIterator*>( |
| 44 obj->GetPointerFromInternalField(0)); |
| 45 } |
| 46 |
| 47 return NULL; |
| 48 } |
| 49 |
| 50 void BreakIterator::DeleteBreakIterator(v8::Persistent<v8::Value> object, |
| 51 void* param) { |
| 52 v8::Persistent<v8::Object> persistent_object = |
| 53 v8::Persistent<v8::Object>::Cast(object); |
| 54 |
| 55 // First delete the hidden C++ object. Deleting NULL is ok. |
| 56 delete UnpackBreakIterator(persistent_object); |
| 57 |
| 58 // Then dispose of the persistent handle to JS object. |
| 59 persistent_object.Dispose(); |
| 60 } |
| 61 |
| 62 // Returns a ThrowException value in case we encounter invalid |
| 63 // object in UnpackBreakIterator method. |
| 64 static v8::Handle<v8::Value> ThrowUnexpectedObjectError() { |
| 65 return v8::ThrowException(v8::Exception::Error( |
| 66 v8::String::New("Internal - Unexpected object type."))); |
| 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 // No need to check args here, we do it in JavaScript. |
| 149 v8::HandleScope handle_scope; |
| 150 |
| 151 const char* locale = *v8::String::Utf8Value(args[0]->ToString()); |
| 152 icu::Locale icu_locale(locale); |
| 153 |
| 154 UErrorCode status = U_ZERO_ERROR; |
| 155 icu::BreakIterator* break_iterator = NULL; |
| 156 const char* type = *v8::String::Utf8Value(args[1]->ToString()); |
| 157 if (!strcmp(type, "character")) { |
| 158 break_iterator = |
| 159 icu::BreakIterator::createCharacterInstance(icu_locale, status); |
| 160 } else if (!strcmp(type, "word")) { |
| 161 break_iterator = |
| 162 icu::BreakIterator::createWordInstance(icu_locale, status); |
| 163 } else if (!strcmp(type, "sentence")) { |
| 164 break_iterator = |
| 165 icu::BreakIterator::createSentenceInstance(icu_locale, status); |
| 166 } else if (!strcmp(type, "line")) { |
| 167 break_iterator = |
| 168 icu::BreakIterator::createLineInstance(icu_locale, status); |
| 169 } else { |
| 170 return v8::ThrowException(v8::Exception::SyntaxError( |
| 171 v8::String::New("Invalid iterator type."))); |
| 172 } |
| 173 |
| 174 if (U_FAILURE(status)) { |
| 175 delete break_iterator; |
| 176 return v8::ThrowException(v8::Exception::Error( |
| 177 v8::String::New("Failed to create break iterator."))); |
| 178 } |
| 179 |
| 180 if (break_iterator_template_.IsEmpty()) { |
| 181 v8::Local<v8::FunctionTemplate> raw_template(v8::FunctionTemplate::New()); |
| 182 |
| 183 raw_template->SetClassName(v8::String::New("v8Locale.v8BreakIterator")); |
| 184 |
| 185 // Define internal field count on instance template. |
| 186 v8::Local<v8::ObjectTemplate> object_template = |
| 187 raw_template->InstanceTemplate(); |
| 188 object_template->SetInternalFieldCount(1); |
| 189 |
| 190 // Define all of the prototype methods on prototype template. |
| 191 v8::Local<v8::ObjectTemplate> proto = raw_template->PrototypeTemplate(); |
| 192 proto->Set(v8::String::New("adoptText"), |
| 193 v8::FunctionTemplate::New(BreakIteratorAdoptText)); |
| 194 proto->Set(v8::String::New("first"), |
| 195 v8::FunctionTemplate::New(BreakIteratorFirst)); |
| 196 proto->Set(v8::String::New("next"), |
| 197 v8::FunctionTemplate::New(BreakIteratorNext)); |
| 198 proto->Set(v8::String::New("current"), |
| 199 v8::FunctionTemplate::New(BreakIteratorCurrent)); |
| 200 proto->Set(v8::String::New("breakType"), |
| 201 v8::FunctionTemplate::New(BreakIteratorBreakType)); |
| 202 |
| 203 break_iterator_template_ = |
| 204 v8::Persistent<v8::FunctionTemplate>::New(raw_template); |
| 205 } |
| 206 |
| 207 // Create an empty object wrapper. |
| 208 v8::Local<v8::Object> local_object = |
| 209 break_iterator_template_->GetFunction()->NewInstance(); |
| 210 v8::Persistent<v8::Object> wrapper = |
| 211 v8::Persistent<v8::Object>::New(local_object); |
| 212 |
| 213 // Set break iterator as internal field of the resulting JS object. |
| 214 wrapper->SetPointerInInternalField(0, break_iterator); |
| 215 |
| 216 // Make object handle weak so we can delete iterator once GC kicks in. |
| 217 wrapper.MakeWeak(NULL, DeleteBreakIterator); |
| 218 |
| 219 return wrapper; |
| 220 } |
| 221 |
| 222 } } // namespace v8::internal |
OLD | NEW |