Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: src/extensions/experimental/collator.cc

Issue 6673011: Add v8Locale.Collator (Closed) Base URL: http://v8.googlecode.com/svn/trunk/
Patch Set: updated to the trunk Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/extensions/experimental/collator.h ('k') | src/extensions/experimental/experimental.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(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.
Mads Ager (chromium) 2011/04/14 08:57:52 break iterator -> 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 collator->setAttribute(UCOL_CASE_LEVEL, ignore_case ? UCOL_OFF : UCOL_ON,
157 status);
158 if (U_FAILURE(status)) {
159 delete collator;
160 return ThrowExceptionForICUError("Failed to set ignoreCase.");
161 }
162 }
163
164 // Accents are taken into account with strength secondary or higher.
165 if (ExtractBooleanOption(options, "ignoreAccents", &ignore_accents)) {
166 if (!ignore_accents) {
167 collator->setStrength(icu::Collator::SECONDARY);
168 } else {
169 collator->setStrength(icu::Collator::PRIMARY);
170 }
171 }
172
173 if (ExtractBooleanOption(options, "numeric", &numeric)) {
174 collator->setAttribute(UCOL_NUMERIC_COLLATION,
175 numeric ? UCOL_ON : UCOL_OFF, status);
176 if (U_FAILURE(status)) {
177 delete collator;
178 return ThrowExceptionForICUError("Failed to set numeric sort option.");
179 }
180 }
181
182 if (collator_template_.IsEmpty()) {
183 v8::Local<v8::FunctionTemplate> raw_template(v8::FunctionTemplate::New());
184 raw_template->SetClassName(v8::String::New("v8Locale.Collator"));
185
186 // Define internal field count on instance template.
187 v8::Local<v8::ObjectTemplate> object_template =
188 raw_template->InstanceTemplate();
189
190 // Set aside internal fields for icu collator.
191 object_template->SetInternalFieldCount(1);
192
193 // Define all of the prototype methods on prototype template.
194 v8::Local<v8::ObjectTemplate> proto = raw_template->PrototypeTemplate();
195 proto->Set(v8::String::New("compare"),
196 v8::FunctionTemplate::New(CollatorCompare));
197
198 collator_template_ =
199 v8::Persistent<v8::FunctionTemplate>::New(raw_template);
200 }
201
202 // Create an empty object wrapper.
203 v8::Local<v8::Object> local_object =
204 collator_template_->GetFunction()->NewInstance();
205 v8::Persistent<v8::Object> wrapper =
206 v8::Persistent<v8::Object>::New(local_object);
207
208 // Set collator as internal field of the resulting JS object.
209 wrapper->SetPointerInInternalField(0, collator);
210
211 // Make object handle weak so we can delete iterator once GC kicks in.
212 wrapper.MakeWeak(NULL, DeleteCollator);
213
214 return wrapper;
215 }
216
217 } } // namespace v8::internal
218
OLDNEW
« no previous file with comments | « src/extensions/experimental/collator.h ('k') | src/extensions/experimental/experimental.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698