Index: src/extensions/experimental/collator.cc |
=================================================================== |
--- src/extensions/experimental/collator.cc (revision 0) |
+++ src/extensions/experimental/collator.cc (revision 0) |
@@ -0,0 +1,187 @@ |
+// Copyright 2011 the V8 project authors. All rights reserved. |
+// Redistribution and use in source and binary forms, with or without |
+// modification, are permitted provided that the following conditions are |
+// met: |
+// |
+// * Redistributions of source code must retain the above copyright |
+// notice, this list of conditions and the following disclaimer. |
+// * Redistributions in binary form must reproduce the above |
+// copyright notice, this list of conditions and the following |
+// disclaimer in the documentation and/or other materials provided |
+// with the distribution. |
+// * Neither the name of Google Inc. nor the names of its |
+// contributors may be used to endorse or promote products derived |
+// from this software without specific prior written permission. |
+// |
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+ |
+#include "collator.h" |
+ |
+#include "unicode/coll.h" |
+#include "unicode/locid.h" |
+#include "unicode/ucol.h" |
+ |
+namespace v8 { |
+namespace internal { |
+ |
+v8::Persistent<v8::FunctionTemplate> Collator::collator_template_; |
+ |
+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
|
+ if (collator_template_->HasInstance(obj)) { |
+ return static_cast<icu::Collator*>(obj->GetPointerFromInternalField(0)); |
+ } |
+ |
+ return NULL; |
+} |
+ |
+void Collator::DeleteCollator(v8::Persistent<v8::Value> object, void* param) { |
+ v8::Persistent<v8::Object> persistent_object = |
+ v8::Persistent<v8::Object>::Cast(object); |
+ |
+ // First delete the hidden C++ object. |
+ // Unpacking should never return NULL here. That would only happen if |
+ // this method is used as the weak callback for persistent handles not |
+ // pointing to a break iterator. |
+ delete UnpackCollator(persistent_object); |
+ |
+ // Then dispose of the persistent handle to JS object. |
+ persistent_object.Dispose(); |
+} |
+ |
+// Throws a JavaScript exception. |
+static v8::Handle<v8::Value> ThrowUnexpectedObjectError() { |
Nebojša Ćirić
2011/03/11 21:15:20
This could also be refactored into separate method
|
+ // Returns undefined, and schedules an exception to be thrown. |
+ return v8::ThrowException(v8::Exception::Error( |
+ v8::String::New("Collator method called on an object " |
+ "that is not a Collator."))); |
+} |
+ |
+v8::Handle<v8::Value> Collator::CollatorCompare(const v8::Arguments& args) { |
+ if (args.Length() != 2 || !args[0]->IsString() || !args[1]->IsString()) { |
+ return v8::ThrowException(v8::Exception::SyntaxError( |
+ v8::String::New("Two string arguments are required."))); |
+ } |
+ |
+ icu::Collator* collator = UnpackCollator(args.Holder()); |
+ if (!collator) { |
+ return ThrowUnexpectedObjectError(); |
+ } |
+ |
+ v8::String::Value string_value1(args[0]); |
+ v8::String::Value string_value2(args[1]); |
+ const UChar* string1 = reinterpret_cast<const UChar*>(*string_value1); |
+ const UChar* string2 = reinterpret_cast<const UChar*>(*string_value2); |
+ UErrorCode status = U_ZERO_ERROR; |
+ UCollationResult result = collator->compare( |
+ string1, string_value1.length(), string2, string_value2.length(), status); |
+ |
+ if (U_FAILURE(status)) { |
+ return v8::ThrowException(v8::Exception::Error( |
+ v8::String::New("Unexpected failure in Collator.compare."))); |
+ } |
+ |
+ return v8::Int32::New(result); |
+} |
+ |
+v8::Handle<v8::Value> Collator::JSCollator(const v8::Arguments& args) { |
+ v8::HandleScope handle_scope; |
+ |
+ if (args.Length() != 2 || !args[0]->IsString() || !args[1]->IsObject()) { |
+ return v8::ThrowException(v8::Exception::SyntaxError( |
+ v8::String::New("Locale and collation option are required."))); |
Nebojša Ćirić
2011/03/11 21:15:20
Is it option or options?
|
+ } |
+ |
+ v8::String::AsciiValue locale(args[0]); |
+ icu::Locale icu_locale(*locale); |
+ |
+ icu::Collator* collator = NULL; |
+ UErrorCode status = U_ZERO_ERROR; |
+ collator = icu::Collator::createInstance(icu_locale, status); |
+ |
+ if (U_FAILURE(status)) { |
+ delete collator; |
+ return v8::ThrowException(v8::Exception::Error( |
+ v8::String::New("Failed to create collator."))); |
+ } |
+ |
+ v8::Local<v8::Object> options(args[1]->ToObject()); |
+ //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?
|
+ v8::Local<v8::Value> strength_value = |
+ 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
|
+ if (!strength_value->IsUndefined() && !strength_value->IsNull()) { |
+ v8::String::AsciiValue strength(strength_value); |
+ if (!*strength) { |
+ return v8::ThrowException(v8::Exception::Error( |
+ v8::String::New("Invalid collation strength."))); |
+ } |
+ |
+ if (!strcmp(*strength, "primary")) { |
+ collator->setStrength(icu::Collator::PRIMARY); |
+ } else if (!strcmp(*strength, "secondary")) { |
+ collator->setStrength(icu::Collator::SECONDARY); |
+ } else if (!strcmp(*strength, "tertiary")) { |
+ collator->setStrength(icu::Collator::TERTIARY); |
+ } else if (!strcmp(*strength, "quaternary")) { |
+ collator->setStrength(icu::Collator::QUATERNARY); |
+ } else { |
+ return v8::ThrowException(v8::Exception::SyntaxError( |
+ v8::String::New("v8Strength should be primary, secondary, tertiary," |
+ "or quaternary."))); |
+ } |
+ } |
+ |
+ v8::Local<v8::Value> case_level = |
+ options->Get(v8::String::New("v8CaseLevel")); |
+ if (!case_level->IsUndefined() && !case_level->IsNull()) { |
+ if (case_level->BooleanValue()) { |
+ collator->setAttribute(UCOL_CASE_LEVEL, UCOL_ON, status); |
+ } |
+ } |
+ |
+ if (collator_template_.IsEmpty()) { |
+ v8::Local<v8::FunctionTemplate> raw_template(v8::FunctionTemplate::New()); |
+ |
+ raw_template->SetClassName(v8::String::New("v8Locale.Collator")); |
+ |
+ // Define internal field count on instance template. |
+ v8::Local<v8::ObjectTemplate> object_template = |
+ raw_template->InstanceTemplate(); |
+ |
+ // Set aside internal fields for icu collator. |
+ object_template->SetInternalFieldCount(1); |
+ |
+ // Define all of the prototype methods on prototype template. |
+ v8::Local<v8::ObjectTemplate> proto = raw_template->PrototypeTemplate(); |
+ proto->Set(v8::String::New("compare"), |
+ v8::FunctionTemplate::New(CollatorCompare)); |
+ |
+ collator_template_ = |
+ v8::Persistent<v8::FunctionTemplate>::New(raw_template); |
+ } |
+ |
+ // Create an empty object wrapper. |
+ v8::Local<v8::Object> local_object = |
+ collator_template_->GetFunction()->NewInstance(); |
+ v8::Persistent<v8::Object> wrapper = |
+ v8::Persistent<v8::Object>::New(local_object); |
+ |
+ // Set collator as internal field of the resulting JS object. |
+ wrapper->SetPointerInInternalField(0, collator); |
+ |
+ // Make object handle weak so we can delete iterator once GC kicks in. |
+ wrapper.MakeWeak(NULL, DeleteCollator); |
+ |
+ return wrapper; |
+} |
+ |
+} } // namespace v8::internal |
Property changes on: src/extensions/experimental/collator.cc |
___________________________________________________________________ |
Added: svn:eol-style |
+ LF |