|
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) { | |
Nebojša Ćirić
2011/03/11 21:15:20
I see delete and unpack methods repeated in the fu
| |
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() { | |
Nebojša Ćirić
2011/03/11 21:15:20
This could also be refactored into separate method
| |
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 v8::Handle<v8::Value> Collator::CollatorCompare(const v8::Arguments& args) { | |
70 if (args.Length() != 2 || !args[0]->IsString() || !args[1]->IsString()) { | |
71 return v8::ThrowException(v8::Exception::SyntaxError( | |
72 v8::String::New("Two string arguments are required."))); | |
73 } | |
74 | |
75 icu::Collator* collator = UnpackCollator(args.Holder()); | |
76 if (!collator) { | |
77 return ThrowUnexpectedObjectError(); | |
78 } | |
79 | |
80 v8::String::Value string_value1(args[0]); | |
81 v8::String::Value string_value2(args[1]); | |
82 const UChar* string1 = reinterpret_cast<const UChar*>(*string_value1); | |
83 const UChar* string2 = reinterpret_cast<const UChar*>(*string_value2); | |
84 UErrorCode status = U_ZERO_ERROR; | |
85 UCollationResult result = collator->compare( | |
86 string1, string_value1.length(), string2, string_value2.length(), status); | |
87 | |
88 if (U_FAILURE(status)) { | |
89 return v8::ThrowException(v8::Exception::Error( | |
90 v8::String::New("Unexpected failure in Collator.compare."))); | |
91 } | |
92 | |
93 return v8::Int32::New(result); | |
94 } | |
95 | |
96 v8::Handle<v8::Value> Collator::JSCollator(const v8::Arguments& args) { | |
97 v8::HandleScope handle_scope; | |
98 | |
99 if (args.Length() != 2 || !args[0]->IsString() || !args[1]->IsObject()) { | |
100 return v8::ThrowException(v8::Exception::SyntaxError( | |
101 v8::String::New("Locale and collation option are required."))); | |
Nebojša Ćirić
2011/03/11 21:15:20
Is it option or options?
| |
102 } | |
103 | |
104 v8::String::AsciiValue locale(args[0]); | |
105 icu::Locale icu_locale(*locale); | |
106 | |
107 icu::Collator* collator = NULL; | |
108 UErrorCode status = U_ZERO_ERROR; | |
109 collator = icu::Collator::createInstance(icu_locale, status); | |
110 | |
111 if (U_FAILURE(status)) { | |
112 delete collator; | |
113 return v8::ThrowException(v8::Exception::Error( | |
114 v8::String::New("Failed to create collator."))); | |
115 } | |
116 | |
117 v8::Local<v8::Object> options(args[1]->ToObject()); | |
118 //v8::Local<v8::Object> options = v8::Local<Object>::Cast(options(args[1])); | |
Nebojša Ćirić
2011/03/11 21:15:20
Remove commented out code?
| |
119 v8::Local<v8::Value> strength_value = | |
120 options->Get(v8::String::New("v8Strength")); | |
Nebojša Ćirić
2011/03/11 21:15:20
Do we need v8Strength? I know we didn't settle yet
| |
121 if (!strength_value->IsUndefined() && !strength_value->IsNull()) { | |
122 v8::String::AsciiValue strength(strength_value); | |
123 if (!*strength) { | |
124 return v8::ThrowException(v8::Exception::Error( | |
125 v8::String::New("Invalid collation strength."))); | |
126 } | |
127 | |
128 if (!strcmp(*strength, "primary")) { | |
129 collator->setStrength(icu::Collator::PRIMARY); | |
130 } else if (!strcmp(*strength, "secondary")) { | |
131 collator->setStrength(icu::Collator::SECONDARY); | |
132 } else if (!strcmp(*strength, "tertiary")) { | |
133 collator->setStrength(icu::Collator::TERTIARY); | |
134 } else if (!strcmp(*strength, "quaternary")) { | |
135 collator->setStrength(icu::Collator::QUATERNARY); | |
136 } else { | |
137 return v8::ThrowException(v8::Exception::SyntaxError( | |
138 v8::String::New("v8Strength should be primary, secondary, tertiary," | |
139 "or quaternary."))); | |
140 } | |
141 } | |
142 | |
143 v8::Local<v8::Value> case_level = | |
144 options->Get(v8::String::New("v8CaseLevel")); | |
145 if (!case_level->IsUndefined() && !case_level->IsNull()) { | |
146 if (case_level->BooleanValue()) { | |
147 collator->setAttribute(UCOL_CASE_LEVEL, UCOL_ON, status); | |
148 } | |
149 } | |
150 | |
151 if (collator_template_.IsEmpty()) { | |
152 v8::Local<v8::FunctionTemplate> raw_template(v8::FunctionTemplate::New()); | |
153 | |
154 raw_template->SetClassName(v8::String::New("v8Locale.Collator")); | |
155 | |
156 // Define internal field count on instance template. | |
157 v8::Local<v8::ObjectTemplate> object_template = | |
158 raw_template->InstanceTemplate(); | |
159 | |
160 // Set aside internal fields for icu collator. | |
161 object_template->SetInternalFieldCount(1); | |
162 | |
163 // Define all of the prototype methods on prototype template. | |
164 v8::Local<v8::ObjectTemplate> proto = raw_template->PrototypeTemplate(); | |
165 proto->Set(v8::String::New("compare"), | |
166 v8::FunctionTemplate::New(CollatorCompare)); | |
167 | |
168 collator_template_ = | |
169 v8::Persistent<v8::FunctionTemplate>::New(raw_template); | |
170 } | |
171 | |
172 // Create an empty object wrapper. | |
173 v8::Local<v8::Object> local_object = | |
174 collator_template_->GetFunction()->NewInstance(); | |
175 v8::Persistent<v8::Object> wrapper = | |
176 v8::Persistent<v8::Object>::New(local_object); | |
177 | |
178 // Set collator as internal field of the resulting JS object. | |
179 wrapper->SetPointerInInternalField(0, collator); | |
180 | |
181 // Make object handle weak so we can delete iterator once GC kicks in. | |
182 wrapper.MakeWeak(NULL, DeleteCollator); | |
183 | |
184 return wrapper; | |
185 } | |
186 | |
187 } } // namespace v8::internal | |
OLD | NEW |