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 "src/extensions/experimental/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 collator. | |
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) { | |
74 v8::HandleScope handle_scope; | |
75 v8::TryCatch try_catch; | |
76 v8::Handle<v8::Value> value = options->Get(v8::String::New(option)); | |
77 if (try_catch.HasCaught()) { | |
78 return false; | |
79 } | |
80 // No need to check if |value| is empty because it's taken care of | |
81 // by TryCatch above. | |
82 if (!value->IsUndefined() && !value->IsNull()) { | |
83 if (value->IsBoolean()) { | |
84 *result = value->BooleanValue(); | |
85 return true; | |
86 } | |
87 } | |
88 return false; | |
89 } | |
90 | |
91 // When there's an ICU error, throw a JavaScript error with |message|. | |
92 static v8::Handle<v8::Value> ThrowExceptionForICUError(const char* message) { | |
93 return v8::ThrowException(v8::Exception::Error(v8::String::New(message))); | |
94 } | |
95 | |
96 v8::Handle<v8::Value> Collator::CollatorCompare(const v8::Arguments& args) { | |
97 if (args.Length() != 2 || !args[0]->IsString() || !args[1]->IsString()) { | |
98 return v8::ThrowException(v8::Exception::SyntaxError( | |
99 v8::String::New("Two string arguments are required."))); | |
100 } | |
101 | |
102 icu::Collator* collator = UnpackCollator(args.Holder()); | |
103 if (!collator) { | |
104 return ThrowUnexpectedObjectError(); | |
105 } | |
106 | |
107 v8::String::Value string_value1(args[0]); | |
108 v8::String::Value string_value2(args[1]); | |
109 const UChar* string1 = reinterpret_cast<const UChar*>(*string_value1); | |
110 const UChar* string2 = reinterpret_cast<const UChar*>(*string_value2); | |
111 UErrorCode status = U_ZERO_ERROR; | |
112 UCollationResult result = collator->compare( | |
113 string1, string_value1.length(), string2, string_value2.length(), status); | |
114 | |
115 if (U_FAILURE(status)) { | |
116 return ThrowExceptionForICUError( | |
117 "Unexpected failure in Collator.compare."); | |
118 } | |
119 | |
120 return v8::Int32::New(result); | |
121 } | |
122 | |
123 v8::Handle<v8::Value> Collator::JSCollator(const v8::Arguments& args) { | |
124 v8::HandleScope handle_scope; | |
125 | |
126 if (args.Length() != 2 || !args[0]->IsString() || !args[1]->IsObject()) { | |
127 return v8::ThrowException(v8::Exception::SyntaxError( | |
128 v8::String::New("Locale and collation options are required."))); | |
129 } | |
130 | |
131 v8::String::AsciiValue locale(args[0]); | |
132 icu::Locale icu_locale(*locale); | |
133 | |
134 icu::Collator* collator = NULL; | |
135 UErrorCode status = U_ZERO_ERROR; | |
136 collator = icu::Collator::createInstance(icu_locale, status); | |
137 | |
138 if (U_FAILURE(status)) { | |
139 delete collator; | |
140 return ThrowExceptionForICUError("Failed to create collator."); | |
141 } | |
142 | |
143 v8::Local<v8::Object> options(args[1]->ToObject()); | |
144 | |
145 // Below, we change collation options that are explicitly specified | |
146 // by a caller in JavaScript. Otherwise, we don't touch because | |
147 // we don't want to change the locale-dependent default value. | |
148 // The three options below are very likely to have the same default | |
149 // across locales, but I haven't checked them all. Others we may add | |
150 // in the future have certainly locale-dependent default (e.g. | |
151 // caseFirst is upperFirst for Danish while is off for most other locales). | |
152 | |
153 bool ignore_case, ignore_accents, numeric; | |
154 | |
155 if (ExtractBooleanOption(options, "ignoreCase", &ignore_case)) { | |
156 // We need to explicitly set the level to secondary to get case ignored. | |
157 // The default L3 ignores UCOL_CASE_LEVEL == UCOL_OFF ! | |
158 if (ignore_case) { | |
159 collator->setStrength(icu::Collator::SECONDARY); | |
160 } | |
161 collator->setAttribute(UCOL_CASE_LEVEL, ignore_case ? UCOL_OFF : UCOL_ON, | |
162 status); | |
163 if (U_FAILURE(status)) { | |
164 delete collator; | |
165 return ThrowExceptionForICUError("Failed to set ignoreCase."); | |
166 } | |
167 } | |
168 | |
169 // Accents are taken into account with strength secondary or higher. | |
170 if (ExtractBooleanOption(options, "ignoreAccents", &ignore_accents)) { | |
171 if (!ignore_accents) { | |
172 collator->setStrength(icu::Collator::SECONDARY); | |
173 } else { | |
174 collator->setStrength(icu::Collator::PRIMARY); | |
175 } | |
176 } | |
177 | |
178 if (ExtractBooleanOption(options, "numeric", &numeric)) { | |
179 collator->setAttribute(UCOL_NUMERIC_COLLATION, | |
180 numeric ? UCOL_ON : UCOL_OFF, status); | |
181 if (U_FAILURE(status)) { | |
182 delete collator; | |
183 return ThrowExceptionForICUError("Failed to set numeric sort option."); | |
184 } | |
185 } | |
186 | |
187 if (collator_template_.IsEmpty()) { | |
188 v8::Local<v8::FunctionTemplate> raw_template(v8::FunctionTemplate::New()); | |
189 raw_template->SetClassName(v8::String::New("v8Locale.Collator")); | |
190 | |
191 // Define internal field count on instance template. | |
192 v8::Local<v8::ObjectTemplate> object_template = | |
193 raw_template->InstanceTemplate(); | |
194 | |
195 // Set aside internal fields for icu collator. | |
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("compare"), | |
201 v8::FunctionTemplate::New(CollatorCompare)); | |
202 | |
203 collator_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 collator_template_->GetFunction()->NewInstance(); | |
210 v8::Persistent<v8::Object> wrapper = | |
211 v8::Persistent<v8::Object>::New(local_object); | |
212 | |
213 // Set collator as internal field of the resulting JS object. | |
214 wrapper->SetPointerInInternalField(0, collator); | |
215 | |
216 // Make object handle weak so we can delete iterator once GC kicks in. | |
217 wrapper.MakeWeak(NULL, DeleteCollator); | |
218 | |
219 return wrapper; | |
220 } | |
221 | |
222 } } // namespace v8::internal | |
OLD | NEW |