|
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 static void ExtractBooleanOption(const v8::Local<v8::Object>& options, | |
Nebojša Ćirić
2011/04/11 23:10:33
Comment on static method?
| |
70 const char* option, | |
71 bool default_value, | |
72 bool *result) { | |
73 v8::Local<v8::Value> value = options->Get(v8::String::New(option)); | |
74 *result = default_value; | |
75 if (!value->IsUndefined() && !value->IsNull()) { | |
76 if (value->IsBoolean()) | |
77 *result = value->BooleanValue(); | |
78 } | |
79 } | |
80 | |
81 static v8::Handle<v8::Value> ThrowExceptionForICUError(const char* message, | |
82 icu::Collator* collator) { | |
83 delete collator; | |
84 return v8::ThrowException(v8::Exception::Error(v8::String::New(message))); | |
85 } | |
86 | |
87 v8::Handle<v8::Value> Collator::CollatorCompare(const v8::Arguments& args) { | |
88 if (args.Length() != 2 || !args[0]->IsString() || !args[1]->IsString()) { | |
89 return v8::ThrowException(v8::Exception::SyntaxError( | |
90 v8::String::New("Two string arguments are required."))); | |
91 } | |
92 | |
93 icu::Collator* collator = UnpackCollator(args.Holder()); | |
94 if (!collator) { | |
95 return ThrowUnexpectedObjectError(); | |
96 } | |
97 | |
98 v8::String::Value string_value1(args[0]); | |
99 v8::String::Value string_value2(args[1]); | |
100 const UChar* string1 = reinterpret_cast<const UChar*>(*string_value1); | |
101 const UChar* string2 = reinterpret_cast<const UChar*>(*string_value2); | |
102 UErrorCode status = U_ZERO_ERROR; | |
103 UCollationResult result = collator->compare( | |
104 string1, string_value1.length(), string2, string_value2.length(), status); | |
105 | |
106 if (U_FAILURE(status)) { | |
107 return v8::ThrowException(v8::Exception::Error( | |
108 v8::String::New("Unexpected failure in Collator.compare."))); | |
109 } | |
110 | |
111 return v8::Int32::New(result); | |
112 } | |
113 | |
114 v8::Handle<v8::Value> Collator::JSCollator(const v8::Arguments& args) { | |
115 v8::HandleScope handle_scope; | |
116 | |
117 if (args.Length() != 2 || !args[0]->IsString() || !args[1]->IsObject()) { | |
118 return v8::ThrowException(v8::Exception::SyntaxError( | |
119 v8::String::New("Locale and collation option are required."))); | |
120 } | |
121 | |
122 v8::String::AsciiValue locale(args[0]); | |
123 icu::Locale icu_locale(*locale); | |
124 | |
125 icu::Collator* collator = NULL; | |
126 UErrorCode status = U_ZERO_ERROR; | |
127 collator = icu::Collator::createInstance(icu_locale, status); | |
128 | |
129 if (U_FAILURE(status)) | |
130 return ThrowExceptionForICUError("Failed to create collator.", collator); | |
131 | |
132 v8::Local<v8::Object> options(args[1]->ToObject()); | |
133 | |
134 bool ignore_case, ignore_accents, numeric; | |
Nebojša Ćirić
2011/04/11 23:10:33
Is this ok wrt. style?
| |
135 ExtractBooleanOption(options, "ignoreCase", true, &ignore_case); | |
136 ExtractBooleanOption(options, "ignoreAccents", true, &ignore_accents); | |
137 ExtractBooleanOption(options, "numeric", false, &numeric); | |
138 | |
139 collator->setStrength(icu::Collator::PRIMARY); | |
Nebojša Ćirić
2011/04/11 23:10:33
Maybe put a comment that this is a default strengt
| |
140 if (!ignore_accents) | |
141 collator->setStrength(icu::Collator::SECONDARY); | |
142 if (!ignore_case) { | |
143 collator->setAttribute(UCOL_CASE_LEVEL, UCOL_ON, status); | |
144 if (U_FAILURE(status)) | |
145 return ThrowExceptionForICUError("Failed to set ignoreCase to false.", | |
146 collator); | |
147 } | |
148 if (numeric) { | |
149 collator->setAttribute(UCOL_NUMERIC_COLLATION, UCOL_ON, status); | |
150 if (U_FAILURE(status)) | |
151 return ThrowExceptionForICUError("Failed to turn on numeric sort.", | |
152 collator); | |
153 } | |
154 | |
Nebojša Ćirić
2011/04/11 23:10:33
Extra new line here.
| |
155 | |
156 if (collator_template_.IsEmpty()) { | |
157 v8::Local<v8::FunctionTemplate> raw_template(v8::FunctionTemplate::New()); | |
158 | |
159 raw_template->SetClassName(v8::String::New("v8Locale.Collator")); | |
160 | |
161 // Define internal field count on instance template. | |
162 v8::Local<v8::ObjectTemplate> object_template = | |
163 raw_template->InstanceTemplate(); | |
164 | |
165 // Set aside internal fields for icu collator. | |
166 object_template->SetInternalFieldCount(1); | |
167 | |
168 // Define all of the prototype methods on prototype template. | |
169 v8::Local<v8::ObjectTemplate> proto = raw_template->PrototypeTemplate(); | |
170 proto->Set(v8::String::New("compare"), | |
171 v8::FunctionTemplate::New(CollatorCompare)); | |
172 | |
173 collator_template_ = | |
174 v8::Persistent<v8::FunctionTemplate>::New(raw_template); | |
175 } | |
176 | |
177 // Create an empty object wrapper. | |
178 v8::Local<v8::Object> local_object = | |
179 collator_template_->GetFunction()->NewInstance(); | |
180 v8::Persistent<v8::Object> wrapper = | |
181 v8::Persistent<v8::Object>::New(local_object); | |
182 | |
183 // Set collator as internal field of the resulting JS object. | |
184 wrapper->SetPointerInInternalField(0, collator); | |
185 | |
186 // Make object handle weak so we can delete iterator once GC kicks in. | |
187 wrapper.MakeWeak(NULL, DeleteCollator); | |
188 | |
189 return wrapper; | |
190 } | |
191 | |
192 } } // namespace v8::internal | |
193 | |
OLD | NEW |