Commit ebadc421 authored by jochen@chromium.org's avatar jochen@chromium.org

Move i18n collator code to runtime.

BUG=v8:2745
R=mstarzinger@chromium.org

Review URL: https://codereview.chromium.org/22671002

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@16126 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 899e8013
This diff is collapsed.
// Copyright 2013 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.
// limitations under the License.
#ifndef V8_EXTENSIONS_I18N_COLLATOR_H_
#define V8_EXTENSIONS_I18N_COLLATOR_H_
#include "unicode/uversion.h"
#include "v8.h"
namespace U_ICU_NAMESPACE {
class Collator;
class UnicodeString;
}
namespace v8_i18n {
class Collator {
public:
static void JSCreateCollator(const v8::FunctionCallbackInfo<v8::Value>& args);
// Helper methods for various bindings.
// Unpacks collator object from corresponding JavaScript object.
static icu::Collator* UnpackCollator(v8::Handle<v8::Object> obj);
// Release memory we allocated for the Collator once the JS object that
// holds the pointer gets garbage collected.
static void DeleteCollator(v8::Isolate* isolate,
v8::Persistent<v8::Object>* object,
void* param);
// Compare two strings and returns -1, 0 and 1 depending on
// whether string1 is smaller than, equal to or larger than string2.
static void JSInternalCompare(
const v8::FunctionCallbackInfo<v8::Value>& args);
private:
Collator() {}
};
} // namespace v8_i18n
#endif // V8_EXTENSIONS_I18N_COLLATOR_H_
......@@ -35,8 +35,6 @@
* Useful for subclassing.
*/
function initializeCollator(collator, locales, options) {
native function NativeJSCreateCollator();
if (collator.hasOwnProperty('__initializedIntlObject')) {
throw new TypeError('Trying to re-initialize Collator object.');
}
......@@ -103,9 +101,9 @@ function initializeCollator(collator, locales, options) {
usage: {value: internalOptions.usage, writable: true}
});
var internalCollator = NativeJSCreateCollator(requestedLocale,
internalOptions,
resolved);
var internalCollator = %CreateCollator(requestedLocale,
internalOptions,
resolved);
// Writable, configurable and enumerable are set to false by default.
Object.defineProperty(collator, 'collator', {value: internalCollator});
......@@ -204,8 +202,7 @@ function initializeCollator(collator, locales, options) {
* the sort order, or x comes after y in the sort order, respectively.
*/
function compare(collator, x, y) {
native function NativeJSInternalCompare();
return NativeJSInternalCompare(collator.collator, String(x), String(y));
return %InternalCompare(collator.collator, String(x), String(y));
};
......
......@@ -29,7 +29,6 @@
#include "i18n-extension.h"
#include "break-iterator.h"
#include "collator.h"
#include "natives.h"
using v8::internal::I18NNatives;
......@@ -46,13 +45,6 @@ Extension::Extension()
v8::Handle<v8::FunctionTemplate> Extension::GetNativeFunction(
v8::Handle<v8::String> name) {
// Collator.
if (name->Equals(v8::String::New("NativeJSCreateCollator"))) {
return v8::FunctionTemplate::New(Collator::JSCreateCollator);
} else if (name->Equals(v8::String::New("NativeJSInternalCompare"))) {
return v8::FunctionTemplate::New(Collator::JSInternalCompare);
}
// Break iterator.
if (name->Equals(v8::String::New("NativeJSCreateBreakIterator"))) {
return v8::FunctionTemplate::New(BreakIterator::JSCreateBreakIterator);
......
This diff is collapsed.
......@@ -33,6 +33,7 @@
#include "v8.h"
namespace U_ICU_NAMESPACE {
class Collator;
class DecimalFormat;
class SimpleDateFormat;
}
......@@ -100,6 +101,29 @@ class NumberFormat {
NumberFormat();
};
class Collator {
public:
// Create a collator for the specificied locale and options. Returns the
// resolved settings for the locale / options.
static icu::Collator* InitializeCollator(
Isolate* isolate,
Handle<String> locale,
Handle<JSObject> options,
Handle<JSObject> resolved);
// Unpacks collator object from corresponding JavaScript object.
static icu::Collator* UnpackCollator(Isolate* isolate, Handle<JSObject> obj);
// Release memory we allocated for the Collator once the JS object that holds
// the pointer gets garbage collected.
static void DeleteCollator(v8::Isolate* isolate,
Persistent<v8::Object>* object,
void* param);
private:
Collator();
};
} } // namespace v8::internal
#endif // V8_I18N_H_
......@@ -83,6 +83,7 @@
#include "unicode/smpdtfmt.h"
#include "unicode/timezone.h"
#include "unicode/uchar.h"
#include "unicode/ucol.h"
#include "unicode/ucurr.h"
#include "unicode/uloc.h"
#include "unicode/unum.h"
......@@ -13795,6 +13796,79 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_InternalNumberParse) {
return isolate->heap()->undefined_value();
}
}
RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateCollator) {
HandleScope scope(isolate);
ASSERT(args.length() == 3);
CONVERT_ARG_HANDLE_CHECKED(String, locale, 0);
CONVERT_ARG_HANDLE_CHECKED(JSObject, options, 1);
CONVERT_ARG_HANDLE_CHECKED(JSObject, resolved, 2);
Handle<ObjectTemplateInfo> collator_template = I18N::GetTemplate(isolate);
// Create an empty object wrapper.
bool has_pending_exception = false;
Handle<JSObject> local_object = Execution::InstantiateObject(
collator_template, &has_pending_exception);
if (has_pending_exception) {
ASSERT(isolate->has_pending_exception());
return Failure::Exception();
}
// Set collator as internal field of the resulting JS object.
icu::Collator* collator = Collator::InitializeCollator(
isolate, locale, options, resolved);
if (!collator) return isolate->ThrowIllegalOperation();
local_object->SetInternalField(0, reinterpret_cast<Smi*>(collator));
RETURN_IF_EMPTY_HANDLE(isolate,
JSObject::SetLocalPropertyIgnoreAttributes(
local_object,
isolate->factory()->NewStringFromAscii(CStrVector("collator")),
isolate->factory()->NewStringFromAscii(CStrVector("valid")),
NONE));
Persistent<v8::Object> wrapper(reinterpret_cast<v8::Isolate*>(isolate),
v8::Utils::ToLocal(local_object));
// Make object handle weak so we can delete the collator once GC kicks in.
wrapper.MakeWeak<void>(NULL, &Collator::DeleteCollator);
Handle<Object> result = Utils::OpenPersistent(wrapper);
wrapper.ClearAndLeak();
return *result;
}
RUNTIME_FUNCTION(MaybeObject*, Runtime_InternalCompare) {
HandleScope scope(isolate);
ASSERT(args.length() == 3);
CONVERT_ARG_HANDLE_CHECKED(JSObject, collator_holder, 0);
CONVERT_ARG_HANDLE_CHECKED(String, string1, 1);
CONVERT_ARG_HANDLE_CHECKED(String, string2, 2);
icu::Collator* collator = Collator::UnpackCollator(isolate, collator_holder);
if (!collator) return isolate->ThrowIllegalOperation();
v8::String::Value string_value1(v8::Utils::ToLocal(string1));
v8::String::Value string_value2(v8::Utils::ToLocal(string2));
const UChar* u_string1 = reinterpret_cast<const UChar*>(*string_value1);
const UChar* u_string2 = reinterpret_cast<const UChar*>(*string_value2);
UErrorCode status = U_ZERO_ERROR;
UCollationResult result = collator->compare(u_string1,
string_value1.length(),
u_string2,
string_value2.length(),
status);
if (U_FAILURE(status)) return isolate->ThrowIllegalOperation();
return *isolate->factory()->NewNumberFromInt(result);
}
#endif // V8_I18N_SUPPORT
......
......@@ -553,6 +553,10 @@ namespace internal {
F(CreateNumberFormat, 3, 1) \
F(InternalNumberFormat, 2, 1) \
F(InternalNumberParse, 2, 1) \
\
/* Collator. */ \
F(CreateCollator, 3, 1) \
F(InternalCompare, 3, 1) \
#else
#define RUNTIME_FUNCTION_LIST_I18N_SUPPORT(F)
......
......@@ -827,8 +827,6 @@
'../../src/i18n.h',
'../../src/extensions/i18n/break-iterator.cc',
'../../src/extensions/i18n/break-iterator.h',
'../../src/extensions/i18n/collator.cc',
'../../src/extensions/i18n/collator.h',
'../../src/extensions/i18n/i18n-extension.cc',
'../../src/extensions/i18n/i18n-extension.h',
'../../src/extensions/i18n/i18n-utils.cc',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment