 Chromium Code Reviews
 Chromium Code Reviews| 
 | 
| 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 "collator.h" | |
| 29 | |
| 30 #include "unicode/coll.h" | |
| 31 #include "unicode/locid.h" | |
| 32 #include "unicode/ucol.h" | |
| 33 | |
| 34 namespace v8 { | |
| 35 namespace internal { | |
| 36 | |
| 37 v8::Persistent<v8::FunctionTemplate> Collator::collator_template_; | |
| 38 | |
| 39 icu::Collator* Collator::UnpackCollator(v8::Handle<v8::Object> obj) { | |
| 40 if (collator_template_->HasInstance(obj)) { | |
| 41 return static_cast<icu::Collator*>(obj->GetPointerFromInternalField(0)); | |
| 42 } | |
| 43 | |
| 44 return NULL; | |
| 45 } | |
| 46 | |
| 47 void Collator::DeleteCollator(v8::Persistent<v8::Value> object, void* param) { | |
| 48 v8::Persistent<v8::Object> persistent_object = | |
| 49 v8::Persistent<v8::Object>::Cast(object); | |
| 50 | |
| 51 // First delete the hidden C++ object. | |
| 52 // Unpacking should never return NULL here. That would only happen if | |
| 53 // this method is used as the weak callback for persistent handles not | |
| 54 // pointing to a break iterator. | |
| 55 delete UnpackCollator(persistent_object); | |
| 56 | |
| 57 // Then dispose of the persistent handle to JS object. | |
| 58 persistent_object.Dispose(); | |
| 59 } | |
| 60 | |
| 61 // Throws a JavaScript exception. | |
| 62 static v8::Handle<v8::Value> ThrowUnexpectedObjectError() { | |
| 63 // Returns undefined, and schedules an exception to be thrown. | |
| 64 return v8::ThrowException(v8::Exception::Error( | |
| 65 v8::String::New("Collator method called on an object " | |
| 66 "that is not a Collator."))); | |
| 67 } | |
| 68 | |
| 69 // Extract a boolean option named in |option| and set it to |result|. | |
| 70 // Return true if it's specified. Otherwise, return false. | |
| 71 static bool ExtractBooleanOption(const v8::Local<v8::Object>& options, | |
| 72 const char* option, | |
| 73 bool *result) { | |
| 
Mads Ager (google)
2011/04/13 13:43:24
bool * -> bool*
 
jungshik at Google
2011/04/13 21:36:38
Done.
 | |
| 74 v8::Local<v8::Value> value = options->Get(v8::String::New(option)); | |
| 
Mads Ager (google)
2011/04/13 13:43:24
I would guess that this can throw an exception? Ca
 | |
| 75 if (!value->IsUndefined() && !value->IsNull()) { | |
| 76 if (value->IsBoolean()) { | |
| 77 *result = value->BooleanValue(); | |
| 78 return true; | |
| 79 } | |
| 80 } | |
| 81 return false; | |
| 82 } | |
| 83 | |
| 84 // When there's an ICU error, delete |collator| and throw a JavaScript error | |
| 
Mads Ager (google)
2011/04/13 13:43:24
Please add to this comment that no JavaScript wrap
 
jungshik at Google
2011/04/13 21:36:38
Ok. I changed this function to just throw an error
 | |
| 85 // with |message|. | |
| 86 static v8::Handle<v8::Value> ThrowExceptionForICUError(const char* message, | |
| 87 icu::Collator* collator) { | |
| 88 delete collator; | |
| 89 return v8::ThrowException(v8::Exception::Error(v8::String::New(message))); | |
| 90 } | |
| 91 | |
| 92 v8::Handle<v8::Value> Collator::CollatorCompare(const v8::Arguments& args) { | |
| 93 if (args.Length() != 2 || !args[0]->IsString() || !args[1]->IsString()) { | |
| 94 return v8::ThrowException(v8::Exception::SyntaxError( | |
| 95 v8::String::New("Two string arguments are required."))); | |
| 96 } | |
| 97 | |
| 98 icu::Collator* collator = UnpackCollator(args.Holder()); | |
| 99 if (!collator) { | |
| 100 return ThrowUnexpectedObjectError(); | |
| 101 } | |
| 102 | |
| 103 v8::String::Value string_value1(args[0]); | |
| 104 v8::String::Value string_value2(args[1]); | |
| 105 const UChar* string1 = reinterpret_cast<const UChar*>(*string_value1); | |
| 106 const UChar* string2 = reinterpret_cast<const UChar*>(*string_value2); | |
| 107 UErrorCode status = U_ZERO_ERROR; | |
| 108 UCollationResult result = collator->compare( | |
| 109 string1, string_value1.length(), string2, string_value2.length(), status); | |
| 110 | |
| 111 if (U_FAILURE(status)) { | |
| 112 return v8::ThrowException(v8::Exception::Error( | |
| 113 v8::String::New("Unexpected failure in Collator.compare."))); | |
| 114 } | |
| 115 | |
| 116 return v8::Int32::New(result); | |
| 117 } | |
| 118 | |
| 119 v8::Handle<v8::Value> Collator::JSCollator(const v8::Arguments& args) { | |
| 120 v8::HandleScope handle_scope; | |
| 121 | |
| 122 if (args.Length() != 2 || !args[0]->IsString() || !args[1]->IsObject()) { | |
| 123 return v8::ThrowException(v8::Exception::SyntaxError( | |
| 124 v8::String::New("Locale and collation options are required."))); | |
| 
Mads Ager (google)
2011/04/13 13:43:24
Four-space indent.
 
jungshik at Google
2011/04/13 21:36:38
Done.
 | |
| 125 } | |
| 126 | |
| 127 v8::String::AsciiValue locale(args[0]); | |
| 128 icu::Locale icu_locale(*locale); | |
| 129 | |
| 130 icu::Collator* collator = NULL; | |
| 131 UErrorCode status = U_ZERO_ERROR; | |
| 132 collator = icu::Collator::createInstance(icu_locale, status); | |
| 133 | |
| 134 if (U_FAILURE(status)) | |
| 
Mads Ager (google)
2011/04/13 13:43:24
Please use braces for multi-line if.
 
jungshik at Google
2011/04/13 21:36:38
Done.
 | |
| 135 return ThrowExceptionForICUError("Failed to create collator.", collator); | |
| 136 | |
| 137 v8::Local<v8::Object> options(args[1]->ToObject()); | |
| 138 | |
| 139 // Below, we change collation options that are explicitly specified | |
| 140 // by a caller in JavaScript. Otherwise, we don't touch because | |
| 141 // we don't want to change the locale-dependent default value. | |
| 142 // The three options below are very likely to have the same default | |
| 143 // across locales, but I haven't checked them all. Others we may add | |
| 144 // in the future have certainly locale-dependent default (e.g. | |
| 145 // caseFirst is upperFirst for Danish while is off for most other locales). | |
| 146 | |
| 147 bool ignore_case, ignore_accents, numeric; | |
| 148 | |
| 149 if (ExtractBooleanOption(options, "ignoreCase", &ignore_case)) { | |
| 150 collator->setAttribute(UCOL_CASE_LEVEL, ignore_case ? UCOL_OFF : UCOL_ON, | |
| 151 status); | |
| 152 if (U_FAILURE(status)) | |
| 
Mads Ager (google)
2011/04/13 13:43:24
Braces.
 
jungshik at Google
2011/04/13 21:36:38
Done.
 | |
| 153 return ThrowExceptionForICUError("Failed to set ignoreCase.", | |
| 154 collator); | |
| 155 } | |
| 156 | |
| 157 if (ExtractBooleanOption(options, "ignoreAccents", &ignore_accents)) { | |
| 158 if (!ignore_accents) | |
| 
Mads Ager (google)
2011/04/13 13:43:24
Braces for these.
 
jungshik at Google
2011/04/13 21:36:38
Done.
 | |
| 159 collator->setStrength(icu::Collator::SECONDARY); | |
| 160 else | |
| 161 collator->setStrength(icu::Collator::PRIMARY); | |
| 162 } | |
| 163 | |
| 164 if (ExtractBooleanOption(options, "numeric", &numeric)) { | |
| 165 collator->setAttribute(UCOL_NUMERIC_COLLATION, | |
| 166 numeric ? UCOL_ON : UCOL_OFF, status); | |
| 167 if (U_FAILURE(status)) | |
| 
Mads Ager (google)
2011/04/13 13:43:24
Braces.
 
jungshik at Google
2011/04/13 21:36:38
Done.
 | |
| 168 return ThrowExceptionForICUError("Failed to set numeric sort option.", | |
| 169 collator); | |
| 170 } | |
| 171 | |
| 172 if (collator_template_.IsEmpty()) { | |
| 173 v8::Local<v8::FunctionTemplate> raw_template(v8::FunctionTemplate::New()); | |
| 174 raw_template->SetClassName(v8::String::New("v8Locale.Collator")); | |
| 175 | |
| 176 // Define internal field count on instance template. | |
| 177 v8::Local<v8::ObjectTemplate> object_template = | |
| 178 raw_template->InstanceTemplate(); | |
| 179 | |
| 180 // Set aside internal fields for icu collator. | |
| 181 object_template->SetInternalFieldCount(1); | |
| 182 | |
| 183 // Define all of the prototype methods on prototype template. | |
| 184 v8::Local<v8::ObjectTemplate> proto = raw_template->PrototypeTemplate(); | |
| 185 proto->Set(v8::String::New("compare"), | |
| 186 v8::FunctionTemplate::New(CollatorCompare)); | |
| 187 | |
| 188 collator_template_ = | |
| 189 v8::Persistent<v8::FunctionTemplate>::New(raw_template); | |
| 190 } | |
| 191 | |
| 192 // Create an empty object wrapper. | |
| 193 v8::Local<v8::Object> local_object = | |
| 194 collator_template_->GetFunction()->NewInstance(); | |
| 195 v8::Persistent<v8::Object> wrapper = | |
| 196 v8::Persistent<v8::Object>::New(local_object); | |
| 197 | |
| 198 // Set collator as internal field of the resulting JS object. | |
| 199 wrapper->SetPointerInInternalField(0, collator); | |
| 200 | |
| 201 // Make object handle weak so we can delete iterator once GC kicks in. | |
| 202 wrapper.MakeWeak(NULL, DeleteCollator); | |
| 203 | |
| 204 return wrapper; | |
| 205 } | |
| 206 | |
| 207 } } // namespace v8::internal | |
| 208 | |
| OLD | NEW |