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

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: exception check wip 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.
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::Local<v8::TryCatch> try_catch;
75 v8::Local<v8::Value> value = options->Get(v8::String::New(option));
76 if (!try_catch.IsEmpty() && try_catch->HasCaught())
77 return false;
jungshik at Google 2011/04/13 21:56:32 I put up a test page at http://www.i18nl10n.com/ch
78 if (!value.IsEmpty() && !value->IsUndefined() && !value->IsNull()) {
79 if (value->IsBoolean()) {
80 *result = value->BooleanValue();
81 return true;
82 }
83 }
84 return false;
85 }
86
87 // When there's an ICU error, throw a JavaScript error with |message|.
88 static v8::Handle<v8::Value> ThrowExceptionForICUError(const char* message) {
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 ThrowExceptionForICUError(
113 "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.")));
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)) {
135 delete collator;
136 return ThrowExceptionForICUError("Failed to create collator.");
137 }
138
139 v8::Local<v8::Object> options(args[1]->ToObject());
140
141 // Below, we change collation options that are explicitly specified
142 // by a caller in JavaScript. Otherwise, we don't touch because
143 // we don't want to change the locale-dependent default value.
144 // The three options below are very likely to have the same default
145 // across locales, but I haven't checked them all. Others we may add
146 // in the future have certainly locale-dependent default (e.g.
147 // caseFirst is upperFirst for Danish while is off for most other locales).
148
149 bool ignore_case, ignore_accents, numeric;
150
151 if (ExtractBooleanOption(options, "ignoreCase", &ignore_case)) {
152 collator->setAttribute(UCOL_CASE_LEVEL, ignore_case ? UCOL_OFF : UCOL_ON,
153 status);
154 if (U_FAILURE(status)) {
155 delete collator;
156 return ThrowExceptionForICUError("Failed to set ignoreCase.");
157 }
158 }
159
160 // Accents are taken into account with strength secondary or higher.
161 if (ExtractBooleanOption(options, "ignoreAccents", &ignore_accents)) {
162 if (!ignore_accents) {
163 collator->setStrength(icu::Collator::SECONDARY);
164 } else {
165 collator->setStrength(icu::Collator::PRIMARY);
166 }
167 }
168
169 if (ExtractBooleanOption(options, "numeric", &numeric)) {
170 collator->setAttribute(UCOL_NUMERIC_COLLATION,
171 numeric ? UCOL_ON : UCOL_OFF, status);
172 if (U_FAILURE(status)) {
173 delete collator;
174 return ThrowExceptionForICUError("Failed to set numeric sort option.");
175 }
176 }
177
178 if (collator_template_.IsEmpty()) {
179 v8::Local<v8::FunctionTemplate> raw_template(v8::FunctionTemplate::New());
jungshik at Google 2011/04/13 21:56:32 Here I got an console message about an uncaught ex
180 raw_template->SetClassName(v8::String::New("v8Locale.Collator"));
181
182 // Define internal field count on instance template.
183 v8::Local<v8::ObjectTemplate> object_template =
184 raw_template->InstanceTemplate();
185
186 // Set aside internal fields for icu collator.
187 object_template->SetInternalFieldCount(1);
188
189 // Define all of the prototype methods on prototype template.
190 v8::Local<v8::ObjectTemplate> proto = raw_template->PrototypeTemplate();
191 proto->Set(v8::String::New("compare"),
192 v8::FunctionTemplate::New(CollatorCompare));
193
194 collator_template_ =
195 v8::Persistent<v8::FunctionTemplate>::New(raw_template);
196 }
197
198 // Create an empty object wrapper.
199 v8::Local<v8::Object> local_object =
200 collator_template_->GetFunction()->NewInstance();
201 v8::Persistent<v8::Object> wrapper =
202 v8::Persistent<v8::Object>::New(local_object);
203
204 // Set collator as internal field of the resulting JS object.
205 wrapper->SetPointerInInternalField(0, collator);
206
207 // Make object handle weak so we can delete iterator once GC kicks in.
208 wrapper.MakeWeak(NULL, DeleteCollator);
209
210 return wrapper;
211 }
212
213 } } // namespace v8::internal
214
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