mirror of https://github.com/lukechilds/node.git
131 changed files with 5439 additions and 3411 deletions
@ -0,0 +1,361 @@ |
|||
// 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.
|
|||
|
|||
#ifndef V8_EFFECTS_H_ |
|||
#define V8_EFFECTS_H_ |
|||
|
|||
#include "v8.h" |
|||
|
|||
#include "types.h" |
|||
|
|||
namespace v8 { |
|||
namespace internal { |
|||
|
|||
|
|||
// A simple struct to represent (write) effects. A write is represented as a
|
|||
// modification of type bounds (e.g. of a variable).
|
|||
//
|
|||
// An effect can either be definite, if the write is known to have taken place,
|
|||
// or 'possible', if it was optional. The difference is relevant when composing
|
|||
// effects.
|
|||
//
|
|||
// There are two ways to compose effects: sequentially (they happen one after
|
|||
// the other) or alternatively (either one or the other happens). A definite
|
|||
// effect cancels out any previous effect upon sequencing. A possible effect
|
|||
// merges into a previous effect, i.e., type bounds are merged. Alternative
|
|||
// composition always merges bounds. It yields a possible effect if at least
|
|||
// one was only possible.
|
|||
struct Effect { |
|||
enum Modality { POSSIBLE, DEFINITE }; |
|||
|
|||
Modality modality; |
|||
Bounds bounds; |
|||
|
|||
Effect() {} |
|||
Effect(Bounds b, Modality m = DEFINITE) : modality(m), bounds(b) {} |
|||
|
|||
// The unknown effect.
|
|||
static Effect Unknown(Isolate* isolate) { |
|||
return Effect(Bounds::Unbounded(isolate), POSSIBLE); |
|||
} |
|||
|
|||
static Effect Forget(Isolate* isolate) { |
|||
return Effect(Bounds::Unbounded(isolate), DEFINITE); |
|||
} |
|||
|
|||
// Sequential composition, as in 'e1; e2'.
|
|||
static Effect Seq(Effect e1, Effect e2, Isolate* isolate) { |
|||
if (e2.modality == DEFINITE) return e2; |
|||
return Effect(Bounds::Either(e1.bounds, e2.bounds, isolate), e1.modality); |
|||
} |
|||
|
|||
// Alternative composition, as in 'cond ? e1 : e2'.
|
|||
static Effect Alt(Effect e1, Effect e2, Isolate* isolate) { |
|||
return Effect( |
|||
Bounds::Either(e1.bounds, e2.bounds, isolate), |
|||
e1.modality == POSSIBLE ? POSSIBLE : e2.modality); |
|||
} |
|||
}; |
|||
|
|||
|
|||
// Classes encapsulating sets of effects on variables.
|
|||
//
|
|||
// Effects maps variables to effects and supports sequential and alternative
|
|||
// composition.
|
|||
//
|
|||
// NestedEffects is an incremental representation that supports persistence
|
|||
// through functional extension. It represents the map as an adjoin of a list
|
|||
// of maps, whose tail can be shared.
|
|||
//
|
|||
// Both classes provide similar interfaces, implemented in parts through the
|
|||
// EffectsMixin below (using sandwich style, to work around the style guide's
|
|||
// MI restriction).
|
|||
//
|
|||
// We also (ab)use Effects/NestedEffects as a representation for abstract
|
|||
// store typings. In that case, only definite effects are of interest.
|
|||
|
|||
template<class Var, class Base, class Effects> |
|||
class EffectsMixin: public Base { |
|||
public: |
|||
explicit EffectsMixin(Zone* zone) : Base(zone) {} |
|||
|
|||
Effect Lookup(Var var) { |
|||
Locator locator; |
|||
return this->Find(var, &locator) |
|||
? locator.value() : Effect::Unknown(Base::isolate()); |
|||
} |
|||
|
|||
Bounds LookupBounds(Var var) { |
|||
Effect effect = Lookup(var); |
|||
return effect.modality == Effect::DEFINITE |
|||
? effect.bounds : Bounds::Unbounded(Base::isolate()); |
|||
} |
|||
|
|||
// Sequential composition.
|
|||
void Seq(Var var, Effect effect) { |
|||
Locator locator; |
|||
if (!this->Insert(var, &locator)) { |
|||
effect = Effect::Seq(locator.value(), effect, Base::isolate()); |
|||
} |
|||
locator.set_value(effect); |
|||
} |
|||
|
|||
void Seq(Effects that) { |
|||
SeqMerger<EffectsMixin> merge = { *this }; |
|||
that.ForEach(&merge); |
|||
} |
|||
|
|||
// Alternative composition.
|
|||
void Alt(Var var, Effect effect) { |
|||
Locator locator; |
|||
if (!this->Insert(var, &locator)) { |
|||
effect = Effect::Alt(locator.value(), effect, Base::isolate()); |
|||
} |
|||
locator.set_value(effect); |
|||
} |
|||
|
|||
void Alt(Effects that) { |
|||
AltWeakener<EffectsMixin> weaken = { *this, that }; |
|||
this->ForEach(&weaken); |
|||
AltMerger<EffectsMixin> merge = { *this }; |
|||
that.ForEach(&merge); |
|||
} |
|||
|
|||
// Invalidation.
|
|||
void Forget() { |
|||
Overrider override = { |
|||
Effect::Forget(Base::isolate()), Effects(Base::zone()) }; |
|||
this->ForEach(&override); |
|||
Seq(override.effects); |
|||
} |
|||
|
|||
protected: |
|||
typedef typename Base::Locator Locator; |
|||
|
|||
template<class Self> |
|||
struct SeqMerger { |
|||
void Call(Var var, Effect effect) { self.Seq(var, effect); } |
|||
Self self; |
|||
}; |
|||
|
|||
template<class Self> |
|||
struct AltMerger { |
|||
void Call(Var var, Effect effect) { self.Alt(var, effect); } |
|||
Self self; |
|||
}; |
|||
|
|||
template<class Self> |
|||
struct AltWeakener { |
|||
void Call(Var var, Effect effect) { |
|||
if (effect.modality == Effect::DEFINITE && !other.Contains(var)) { |
|||
effect.modality = Effect::POSSIBLE; |
|||
Locator locator; |
|||
self.Insert(var, &locator); |
|||
locator.set_value(effect); |
|||
} |
|||
} |
|||
Self self; |
|||
Effects other; |
|||
}; |
|||
|
|||
struct Overrider { |
|||
void Call(Var var, Effect effect) { effects.Seq(var, new_effect); } |
|||
Effect new_effect; |
|||
Effects effects; |
|||
}; |
|||
}; |
|||
|
|||
|
|||
template<class Var, Var kNoVar> class Effects; |
|||
template<class Var, Var kNoVar> class NestedEffectsBase; |
|||
|
|||
template<class Var, Var kNoVar> |
|||
class EffectsBase { |
|||
public: |
|||
explicit EffectsBase(Zone* zone) : map_(new(zone) Mapping(zone)) {} |
|||
|
|||
bool IsEmpty() { return map_->is_empty(); } |
|||
|
|||
protected: |
|||
friend class NestedEffectsBase<Var, kNoVar>; |
|||
friend class |
|||
EffectsMixin<Var, NestedEffectsBase<Var, kNoVar>, Effects<Var, kNoVar> >; |
|||
|
|||
Zone* zone() { return map_->allocator().zone(); } |
|||
Isolate* isolate() { return zone()->isolate(); } |
|||
|
|||
struct SplayTreeConfig { |
|||
typedef Var Key; |
|||
typedef Effect Value; |
|||
static const Var kNoKey = kNoVar; |
|||
static Effect NoValue() { return Effect(); } |
|||
static int Compare(int x, int y) { return y - x; } |
|||
}; |
|||
typedef ZoneSplayTree<SplayTreeConfig> Mapping; |
|||
typedef typename Mapping::Locator Locator; |
|||
|
|||
bool Contains(Var var) { |
|||
ASSERT(var != kNoVar); |
|||
return map_->Contains(var); |
|||
} |
|||
bool Find(Var var, Locator* locator) { |
|||
ASSERT(var != kNoVar); |
|||
return map_->Find(var, locator); |
|||
} |
|||
bool Insert(Var var, Locator* locator) { |
|||
ASSERT(var != kNoVar); |
|||
return map_->Insert(var, locator); |
|||
} |
|||
|
|||
template<class Callback> |
|||
void ForEach(Callback* callback) { |
|||
return map_->ForEach(callback); |
|||
} |
|||
|
|||
private: |
|||
Mapping* map_; |
|||
}; |
|||
|
|||
template<class Var, Var kNoVar> |
|||
const Var EffectsBase<Var, kNoVar>::SplayTreeConfig::kNoKey; |
|||
|
|||
template<class Var, Var kNoVar> |
|||
class Effects: public |
|||
EffectsMixin<Var, EffectsBase<Var, kNoVar>, Effects<Var, kNoVar> > { |
|||
public: |
|||
explicit Effects(Zone* zone) |
|||
: EffectsMixin<Var, EffectsBase<Var, kNoVar>, Effects<Var, kNoVar> >(zone) |
|||
{} |
|||
}; |
|||
|
|||
|
|||
template<class Var, Var kNoVar> |
|||
class NestedEffectsBase { |
|||
public: |
|||
explicit NestedEffectsBase(Zone* zone) : node_(new(zone) Node(zone)) {} |
|||
|
|||
template<class Callback> |
|||
void ForEach(Callback* callback) { |
|||
if (node_->previous) NestedEffectsBase(node_->previous).ForEach(callback); |
|||
node_->effects.ForEach(callback); |
|||
} |
|||
|
|||
Effects<Var, kNoVar> Top() { return node_->effects; } |
|||
|
|||
bool IsEmpty() { |
|||
for (Node* node = node_; node != NULL; node = node->previous) { |
|||
if (!node->effects.IsEmpty()) return false; |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
protected: |
|||
typedef typename EffectsBase<Var, kNoVar>::Locator Locator; |
|||
|
|||
Zone* zone() { return node_->zone; } |
|||
Isolate* isolate() { return zone()->isolate(); } |
|||
|
|||
void push() { node_ = new(node_->zone) Node(node_->zone, node_); } |
|||
void pop() { node_ = node_->previous; } |
|||
bool is_empty() { return node_ == NULL; } |
|||
|
|||
bool Contains(Var var) { |
|||
ASSERT(var != kNoVar); |
|||
for (Node* node = node_; node != NULL; node = node->previous) { |
|||
if (node->effects.Contains(var)) return true; |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
bool Find(Var var, Locator* locator) { |
|||
ASSERT(var != kNoVar); |
|||
for (Node* node = node_; node != NULL; node = node->previous) { |
|||
if (node->effects.Find(var, locator)) return true; |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
bool Insert(Var var, Locator* locator); |
|||
|
|||
private: |
|||
struct Node: ZoneObject { |
|||
Zone* zone; |
|||
Effects<Var, kNoVar> effects; |
|||
Node* previous; |
|||
explicit Node(Zone* zone, Node* previous = NULL) |
|||
: zone(zone), effects(zone), previous(previous) {} |
|||
}; |
|||
|
|||
explicit NestedEffectsBase(Node* node) : node_(node) {} |
|||
|
|||
Node* node_; |
|||
}; |
|||
|
|||
|
|||
template<class Var, Var kNoVar> |
|||
bool NestedEffectsBase<Var, kNoVar>::Insert(Var var, Locator* locator) { |
|||
ASSERT(var != kNoVar); |
|||
if (!node_->effects.Insert(var, locator)) return false; |
|||
Locator shadowed; |
|||
for (Node* node = node_->previous; node != NULL; node = node->previous) { |
|||
if (node->effects.Find(var, &shadowed)) { |
|||
// Initialize with shadowed entry.
|
|||
locator->set_value(shadowed.value()); |
|||
return false; |
|||
} |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
|
|||
template<class Var, Var kNoVar> |
|||
class NestedEffects: public |
|||
EffectsMixin<Var, NestedEffectsBase<Var, kNoVar>, Effects<Var, kNoVar> > { |
|||
public: |
|||
explicit NestedEffects(Zone* zone) : |
|||
EffectsMixin<Var, NestedEffectsBase<Var, kNoVar>, Effects<Var, kNoVar> >( |
|||
zone) {} |
|||
|
|||
// Create an extension of the current effect set. The current set should not
|
|||
// be modified while the extension is in use.
|
|||
NestedEffects Push() { |
|||
NestedEffects result = *this; |
|||
result.push(); |
|||
return result; |
|||
} |
|||
|
|||
NestedEffects Pop() { |
|||
NestedEffects result = *this; |
|||
result.pop(); |
|||
ASSERT(!this->is_empty()); |
|||
return result; |
|||
} |
|||
}; |
|||
|
|||
} } // namespace v8::internal
|
|||
|
|||
#endif // V8_EFFECTS_H_
|
@ -1,366 +0,0 @@ |
|||
// 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.
|
|||
|
|||
#include "collator.h" |
|||
|
|||
#include "i18n-utils.h" |
|||
#include "unicode/coll.h" |
|||
#include "unicode/locid.h" |
|||
#include "unicode/ucol.h" |
|||
|
|||
namespace v8_i18n { |
|||
|
|||
static icu::Collator* InitializeCollator( |
|||
v8::Handle<v8::String>, v8::Handle<v8::Object>, v8::Handle<v8::Object>); |
|||
|
|||
static icu::Collator* CreateICUCollator( |
|||
const icu::Locale&, v8::Handle<v8::Object>); |
|||
|
|||
static bool SetBooleanAttribute( |
|||
UColAttribute, const char*, v8::Handle<v8::Object>, icu::Collator*); |
|||
|
|||
static void SetResolvedSettings( |
|||
const icu::Locale&, icu::Collator*, v8::Handle<v8::Object>); |
|||
|
|||
static void SetBooleanSetting( |
|||
UColAttribute, icu::Collator*, const char*, v8::Handle<v8::Object>); |
|||
|
|||
icu::Collator* Collator::UnpackCollator(v8::Handle<v8::Object> obj) { |
|||
v8::HandleScope handle_scope; |
|||
|
|||
if (obj->HasOwnProperty(v8::String::New("collator"))) { |
|||
return static_cast<icu::Collator*>( |
|||
obj->GetAlignedPointerFromInternalField(0)); |
|||
} |
|||
|
|||
return NULL; |
|||
} |
|||
|
|||
void Collator::DeleteCollator(v8::Isolate* isolate, |
|||
v8::Persistent<v8::Object>* object, |
|||
void* param) { |
|||
// 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 collator.
|
|||
v8::HandleScope handle_scope(isolate); |
|||
v8::Local<v8::Object> handle = v8::Local<v8::Object>::New(isolate, *object); |
|||
delete UnpackCollator(handle); |
|||
|
|||
// Then dispose of the persistent handle to JS object.
|
|||
object->Dispose(isolate); |
|||
} |
|||
|
|||
|
|||
// Throws a JavaScript exception.
|
|||
static v8::Handle<v8::Value> ThrowUnexpectedObjectError() { |
|||
// 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."))); |
|||
} |
|||
|
|||
|
|||
// When there's an ICU error, throw a JavaScript error with |message|.
|
|||
static v8::Handle<v8::Value> ThrowExceptionForICUError(const char* message) { |
|||
return v8::ThrowException(v8::Exception::Error(v8::String::New(message))); |
|||
} |
|||
|
|||
|
|||
// static
|
|||
void Collator::JSInternalCompare( |
|||
const v8::FunctionCallbackInfo<v8::Value>& args) { |
|||
if (args.Length() != 3 || !args[0]->IsObject() || |
|||
!args[1]->IsString() || !args[2]->IsString()) { |
|||
v8::ThrowException(v8::Exception::SyntaxError( |
|||
v8::String::New("Collator and two string arguments are required."))); |
|||
return; |
|||
} |
|||
|
|||
icu::Collator* collator = UnpackCollator(args[0]->ToObject()); |
|||
if (!collator) { |
|||
ThrowUnexpectedObjectError(); |
|||
return; |
|||
} |
|||
|
|||
v8::String::Value string_value1(args[1]); |
|||
v8::String::Value string_value2(args[2]); |
|||
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)) { |
|||
ThrowExceptionForICUError( |
|||
"Internal error. Unexpected failure in Collator.compare."); |
|||
return; |
|||
} |
|||
|
|||
args.GetReturnValue().Set(result); |
|||
} |
|||
|
|||
void Collator::JSCreateCollator( |
|||
const v8::FunctionCallbackInfo<v8::Value>& args) { |
|||
if (args.Length() != 3 || !args[0]->IsString() || !args[1]->IsObject() || |
|||
!args[2]->IsObject()) { |
|||
v8::ThrowException(v8::Exception::SyntaxError( |
|||
v8::String::New("Internal error, wrong parameters."))); |
|||
return; |
|||
} |
|||
|
|||
v8::Isolate* isolate = args.GetIsolate(); |
|||
v8::Local<v8::ObjectTemplate> intl_collator_template = |
|||
Utils::GetTemplate(isolate); |
|||
|
|||
// Create an empty object wrapper.
|
|||
v8::Local<v8::Object> local_object = intl_collator_template->NewInstance(); |
|||
// But the handle shouldn't be empty.
|
|||
// That can happen if there was a stack overflow when creating the object.
|
|||
if (local_object.IsEmpty()) { |
|||
args.GetReturnValue().Set(local_object); |
|||
return; |
|||
} |
|||
|
|||
// Set collator as internal field of the resulting JS object.
|
|||
icu::Collator* collator = InitializeCollator( |
|||
args[0]->ToString(), args[1]->ToObject(), args[2]->ToObject()); |
|||
|
|||
if (!collator) { |
|||
v8::ThrowException(v8::Exception::Error(v8::String::New( |
|||
"Internal error. Couldn't create ICU collator."))); |
|||
return; |
|||
} else { |
|||
local_object->SetAlignedPointerInInternalField(0, collator); |
|||
|
|||
// Make it safer to unpack later on.
|
|||
v8::TryCatch try_catch; |
|||
local_object->Set(v8::String::New("collator"), v8::String::New("valid")); |
|||
if (try_catch.HasCaught()) { |
|||
v8::ThrowException(v8::Exception::Error( |
|||
v8::String::New("Internal error, couldn't set property."))); |
|||
return; |
|||
} |
|||
} |
|||
|
|||
v8::Persistent<v8::Object> wrapper(isolate, local_object); |
|||
// Make object handle weak so we can delete iterator once GC kicks in.
|
|||
wrapper.MakeWeak<void>(NULL, &DeleteCollator); |
|||
args.GetReturnValue().Set(wrapper); |
|||
wrapper.ClearAndLeak(); |
|||
} |
|||
|
|||
static icu::Collator* InitializeCollator(v8::Handle<v8::String> locale, |
|||
v8::Handle<v8::Object> options, |
|||
v8::Handle<v8::Object> resolved) { |
|||
// Convert BCP47 into ICU locale format.
|
|||
UErrorCode status = U_ZERO_ERROR; |
|||
icu::Locale icu_locale; |
|||
char icu_result[ULOC_FULLNAME_CAPACITY]; |
|||
int icu_length = 0; |
|||
v8::String::AsciiValue bcp47_locale(locale); |
|||
if (bcp47_locale.length() != 0) { |
|||
uloc_forLanguageTag(*bcp47_locale, icu_result, ULOC_FULLNAME_CAPACITY, |
|||
&icu_length, &status); |
|||
if (U_FAILURE(status) || icu_length == 0) { |
|||
return NULL; |
|||
} |
|||
icu_locale = icu::Locale(icu_result); |
|||
} |
|||
|
|||
icu::Collator* collator = CreateICUCollator(icu_locale, options); |
|||
if (!collator) { |
|||
// Remove extensions and try again.
|
|||
icu::Locale no_extension_locale(icu_locale.getBaseName()); |
|||
collator = CreateICUCollator(no_extension_locale, options); |
|||
|
|||
// Set resolved settings (pattern, numbering system).
|
|||
SetResolvedSettings(no_extension_locale, collator, resolved); |
|||
} else { |
|||
SetResolvedSettings(icu_locale, collator, resolved); |
|||
} |
|||
|
|||
return collator; |
|||
} |
|||
|
|||
static icu::Collator* CreateICUCollator( |
|||
const icu::Locale& icu_locale, v8::Handle<v8::Object> options) { |
|||
// Make collator from options.
|
|||
icu::Collator* collator = NULL; |
|||
UErrorCode status = U_ZERO_ERROR; |
|||
collator = icu::Collator::createInstance(icu_locale, status); |
|||
|
|||
if (U_FAILURE(status)) { |
|||
delete collator; |
|||
return NULL; |
|||
} |
|||
|
|||
// Set flags first, and then override them with sensitivity if necessary.
|
|||
SetBooleanAttribute(UCOL_NUMERIC_COLLATION, "numeric", options, collator); |
|||
|
|||
// Normalization is always on, by the spec. We are free to optimize
|
|||
// if the strings are already normalized (but we don't have a way to tell
|
|||
// that right now).
|
|||
collator->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, status); |
|||
|
|||
icu::UnicodeString case_first; |
|||
if (Utils::ExtractStringSetting(options, "caseFirst", &case_first)) { |
|||
if (case_first == UNICODE_STRING_SIMPLE("upper")) { |
|||
collator->setAttribute(UCOL_CASE_FIRST, UCOL_UPPER_FIRST, status); |
|||
} else if (case_first == UNICODE_STRING_SIMPLE("lower")) { |
|||
collator->setAttribute(UCOL_CASE_FIRST, UCOL_LOWER_FIRST, status); |
|||
} else { |
|||
// Default (false/off).
|
|||
collator->setAttribute(UCOL_CASE_FIRST, UCOL_OFF, status); |
|||
} |
|||
} |
|||
|
|||
icu::UnicodeString sensitivity; |
|||
if (Utils::ExtractStringSetting(options, "sensitivity", &sensitivity)) { |
|||
if (sensitivity == UNICODE_STRING_SIMPLE("base")) { |
|||
collator->setStrength(icu::Collator::PRIMARY); |
|||
} else if (sensitivity == UNICODE_STRING_SIMPLE("accent")) { |
|||
collator->setStrength(icu::Collator::SECONDARY); |
|||
} else if (sensitivity == UNICODE_STRING_SIMPLE("case")) { |
|||
collator->setStrength(icu::Collator::PRIMARY); |
|||
collator->setAttribute(UCOL_CASE_LEVEL, UCOL_ON, status); |
|||
} else { |
|||
// variant (default)
|
|||
collator->setStrength(icu::Collator::TERTIARY); |
|||
} |
|||
} |
|||
|
|||
bool ignore; |
|||
if (Utils::ExtractBooleanSetting(options, "ignorePunctuation", &ignore)) { |
|||
if (ignore) { |
|||
collator->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, status); |
|||
} |
|||
} |
|||
|
|||
return collator; |
|||
} |
|||
|
|||
static bool SetBooleanAttribute(UColAttribute attribute, |
|||
const char* name, |
|||
v8::Handle<v8::Object> options, |
|||
icu::Collator* collator) { |
|||
UErrorCode status = U_ZERO_ERROR; |
|||
bool result; |
|||
if (Utils::ExtractBooleanSetting(options, name, &result)) { |
|||
collator->setAttribute(attribute, result ? UCOL_ON : UCOL_OFF, status); |
|||
if (U_FAILURE(status)) { |
|||
return false; |
|||
} |
|||
} |
|||
|
|||
return true; |
|||
} |
|||
|
|||
static void SetResolvedSettings(const icu::Locale& icu_locale, |
|||
icu::Collator* collator, |
|||
v8::Handle<v8::Object> resolved) { |
|||
SetBooleanSetting(UCOL_NUMERIC_COLLATION, collator, "numeric", resolved); |
|||
|
|||
UErrorCode status = U_ZERO_ERROR; |
|||
|
|||
switch (collator->getAttribute(UCOL_CASE_FIRST, status)) { |
|||
case UCOL_LOWER_FIRST: |
|||
resolved->Set(v8::String::New("caseFirst"), v8::String::New("lower")); |
|||
break; |
|||
case UCOL_UPPER_FIRST: |
|||
resolved->Set(v8::String::New("caseFirst"), v8::String::New("upper")); |
|||
break; |
|||
default: |
|||
resolved->Set(v8::String::New("caseFirst"), v8::String::New("false")); |
|||
} |
|||
|
|||
switch (collator->getAttribute(UCOL_STRENGTH, status)) { |
|||
case UCOL_PRIMARY: { |
|||
resolved->Set(v8::String::New("strength"), v8::String::New("primary")); |
|||
|
|||
// case level: true + s1 -> case, s1 -> base.
|
|||
if (UCOL_ON == collator->getAttribute(UCOL_CASE_LEVEL, status)) { |
|||
resolved->Set(v8::String::New("sensitivity"), v8::String::New("case")); |
|||
} else { |
|||
resolved->Set(v8::String::New("sensitivity"), v8::String::New("base")); |
|||
} |
|||
break; |
|||
} |
|||
case UCOL_SECONDARY: |
|||
resolved->Set(v8::String::New("strength"), v8::String::New("secondary")); |
|||
resolved->Set(v8::String::New("sensitivity"), v8::String::New("accent")); |
|||
break; |
|||
case UCOL_TERTIARY: |
|||
resolved->Set(v8::String::New("strength"), v8::String::New("tertiary")); |
|||
resolved->Set(v8::String::New("sensitivity"), v8::String::New("variant")); |
|||
break; |
|||
case UCOL_QUATERNARY: |
|||
// We shouldn't get quaternary and identical from ICU, but if we do
|
|||
// put them into variant.
|
|||
resolved->Set(v8::String::New("strength"), v8::String::New("quaternary")); |
|||
resolved->Set(v8::String::New("sensitivity"), v8::String::New("variant")); |
|||
break; |
|||
default: |
|||
resolved->Set(v8::String::New("strength"), v8::String::New("identical")); |
|||
resolved->Set(v8::String::New("sensitivity"), v8::String::New("variant")); |
|||
} |
|||
|
|||
if (UCOL_SHIFTED == collator->getAttribute(UCOL_ALTERNATE_HANDLING, status)) { |
|||
resolved->Set(v8::String::New("ignorePunctuation"), |
|||
v8::Boolean::New(true)); |
|||
} else { |
|||
resolved->Set(v8::String::New("ignorePunctuation"), |
|||
v8::Boolean::New(false)); |
|||
} |
|||
|
|||
// Set the locale
|
|||
char result[ULOC_FULLNAME_CAPACITY]; |
|||
status = U_ZERO_ERROR; |
|||
uloc_toLanguageTag( |
|||
icu_locale.getName(), result, ULOC_FULLNAME_CAPACITY, FALSE, &status); |
|||
if (U_SUCCESS(status)) { |
|||
resolved->Set(v8::String::New("locale"), v8::String::New(result)); |
|||
} else { |
|||
// This would never happen, since we got the locale from ICU.
|
|||
resolved->Set(v8::String::New("locale"), v8::String::New("und")); |
|||
} |
|||
} |
|||
|
|||
static void SetBooleanSetting(UColAttribute attribute, |
|||
icu::Collator* collator, |
|||
const char* property, |
|||
v8::Handle<v8::Object> resolved) { |
|||
UErrorCode status = U_ZERO_ERROR; |
|||
if (UCOL_ON == collator->getAttribute(attribute, status)) { |
|||
resolved->Set(v8::String::New(property), v8::Boolean::New(true)); |
|||
} else { |
|||
resolved->Set(v8::String::New(property), v8::Boolean::New(false)); |
|||
} |
|||
} |
|||
|
|||
} // namespace v8_i18n
|
@ -1,418 +0,0 @@ |
|||
// 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.
|
|||
|
|||
#include "number-format.h" |
|||
|
|||
#include <string.h> |
|||
|
|||
#include "i18n-utils.h" |
|||
#include "unicode/curramt.h" |
|||
#include "unicode/dcfmtsym.h" |
|||
#include "unicode/decimfmt.h" |
|||
#include "unicode/locid.h" |
|||
#include "unicode/numfmt.h" |
|||
#include "unicode/numsys.h" |
|||
#include "unicode/uchar.h" |
|||
#include "unicode/ucurr.h" |
|||
#include "unicode/unum.h" |
|||
#include "unicode/uversion.h" |
|||
|
|||
namespace v8_i18n { |
|||
|
|||
static icu::DecimalFormat* InitializeNumberFormat(v8::Handle<v8::String>, |
|||
v8::Handle<v8::Object>, |
|||
v8::Handle<v8::Object>); |
|||
static icu::DecimalFormat* CreateICUNumberFormat(const icu::Locale&, |
|||
v8::Handle<v8::Object>); |
|||
static void SetResolvedSettings(const icu::Locale&, |
|||
icu::DecimalFormat*, |
|||
v8::Handle<v8::Object>); |
|||
|
|||
icu::DecimalFormat* NumberFormat::UnpackNumberFormat( |
|||
v8::Handle<v8::Object> obj) { |
|||
v8::HandleScope handle_scope; |
|||
|
|||
// v8::ObjectTemplate doesn't have HasInstance method so we can't check
|
|||
// if obj is an instance of NumberFormat class. We'll check for a property
|
|||
// that has to be in the object. The same applies to other services, like
|
|||
// Collator and DateTimeFormat.
|
|||
if (obj->HasOwnProperty(v8::String::New("numberFormat"))) { |
|||
return static_cast<icu::DecimalFormat*>( |
|||
obj->GetAlignedPointerFromInternalField(0)); |
|||
} |
|||
|
|||
return NULL; |
|||
} |
|||
|
|||
void NumberFormat::DeleteNumberFormat(v8::Isolate* isolate, |
|||
v8::Persistent<v8::Object>* object, |
|||
void* param) { |
|||
// 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 date time formatter.
|
|||
v8::HandleScope handle_scope(isolate); |
|||
v8::Local<v8::Object> handle = v8::Local<v8::Object>::New(isolate, *object); |
|||
delete UnpackNumberFormat(handle); |
|||
|
|||
// Then dispose of the persistent handle to JS object.
|
|||
object->Dispose(isolate); |
|||
} |
|||
|
|||
void NumberFormat::JSInternalFormat( |
|||
const v8::FunctionCallbackInfo<v8::Value>& args) { |
|||
if (args.Length() != 2 || !args[0]->IsObject() || !args[1]->IsNumber()) { |
|||
v8::ThrowException(v8::Exception::Error( |
|||
v8::String::New("Formatter and numeric value have to be specified."))); |
|||
return; |
|||
} |
|||
|
|||
icu::DecimalFormat* number_format = UnpackNumberFormat(args[0]->ToObject()); |
|||
if (!number_format) { |
|||
v8::ThrowException(v8::Exception::Error( |
|||
v8::String::New("NumberFormat method called on an object " |
|||
"that is not a NumberFormat."))); |
|||
return; |
|||
} |
|||
|
|||
// ICU will handle actual NaN value properly and return NaN string.
|
|||
icu::UnicodeString result; |
|||
number_format->format(args[1]->NumberValue(), result); |
|||
|
|||
args.GetReturnValue().Set(v8::String::New( |
|||
reinterpret_cast<const uint16_t*>(result.getBuffer()), result.length())); |
|||
} |
|||
|
|||
void NumberFormat::JSInternalParse( |
|||
const v8::FunctionCallbackInfo<v8::Value>& args) { |
|||
if (args.Length() != 2 || !args[0]->IsObject() || !args[1]->IsString()) { |
|||
v8::ThrowException(v8::Exception::Error( |
|||
v8::String::New("Formatter and string have to be specified."))); |
|||
return; |
|||
} |
|||
|
|||
icu::DecimalFormat* number_format = UnpackNumberFormat(args[0]->ToObject()); |
|||
if (!number_format) { |
|||
v8::ThrowException(v8::Exception::Error( |
|||
v8::String::New("NumberFormat method called on an object " |
|||
"that is not a NumberFormat."))); |
|||
return; |
|||
} |
|||
|
|||
// ICU will handle actual NaN value properly and return NaN string.
|
|||
icu::UnicodeString string_number; |
|||
if (!Utils::V8StringToUnicodeString(args[1]->ToString(), &string_number)) { |
|||
string_number = ""; |
|||
} |
|||
|
|||
UErrorCode status = U_ZERO_ERROR; |
|||
icu::Formattable result; |
|||
// ICU 4.6 doesn't support parseCurrency call. We need to wait for ICU49
|
|||
// to be part of Chrome.
|
|||
// TODO(cira): Include currency parsing code using parseCurrency call.
|
|||
// We need to check if the formatter parses all currencies or only the
|
|||
// one it was constructed with (it will impact the API - how to return ISO
|
|||
// code and the value).
|
|||
number_format->parse(string_number, result, status); |
|||
if (U_FAILURE(status)) { |
|||
return; |
|||
} |
|||
|
|||
switch (result.getType()) { |
|||
case icu::Formattable::kDouble: |
|||
args.GetReturnValue().Set(result.getDouble()); |
|||
return; |
|||
case icu::Formattable::kLong: |
|||
args.GetReturnValue().Set(result.getLong()); |
|||
return; |
|||
case icu::Formattable::kInt64: |
|||
args.GetReturnValue().Set(static_cast<double>(result.getInt64())); |
|||
return; |
|||
default: |
|||
return; |
|||
} |
|||
} |
|||
|
|||
void NumberFormat::JSCreateNumberFormat( |
|||
const v8::FunctionCallbackInfo<v8::Value>& args) { |
|||
if (args.Length() != 3 || |
|||
!args[0]->IsString() || |
|||
!args[1]->IsObject() || |
|||
!args[2]->IsObject()) { |
|||
v8::ThrowException(v8::Exception::Error( |
|||
v8::String::New("Internal error, wrong parameters."))); |
|||
return; |
|||
} |
|||
|
|||
v8::Isolate* isolate = args.GetIsolate(); |
|||
v8::Local<v8::ObjectTemplate> number_format_template = |
|||
Utils::GetTemplate(isolate); |
|||
|
|||
// Create an empty object wrapper.
|
|||
v8::Local<v8::Object> local_object = number_format_template->NewInstance(); |
|||
// But the handle shouldn't be empty.
|
|||
// That can happen if there was a stack overflow when creating the object.
|
|||
if (local_object.IsEmpty()) { |
|||
args.GetReturnValue().Set(local_object); |
|||
return; |
|||
} |
|||
|
|||
// Set number formatter as internal field of the resulting JS object.
|
|||
icu::DecimalFormat* number_format = InitializeNumberFormat( |
|||
args[0]->ToString(), args[1]->ToObject(), args[2]->ToObject()); |
|||
|
|||
if (!number_format) { |
|||
v8::ThrowException(v8::Exception::Error(v8::String::New( |
|||
"Internal error. Couldn't create ICU number formatter."))); |
|||
return; |
|||
} else { |
|||
local_object->SetAlignedPointerInInternalField(0, number_format); |
|||
|
|||
v8::TryCatch try_catch; |
|||
local_object->Set(v8::String::New("numberFormat"), |
|||
v8::String::New("valid")); |
|||
if (try_catch.HasCaught()) { |
|||
v8::ThrowException(v8::Exception::Error( |
|||
v8::String::New("Internal error, couldn't set property."))); |
|||
return; |
|||
} |
|||
} |
|||
|
|||
v8::Persistent<v8::Object> wrapper(isolate, local_object); |
|||
// Make object handle weak so we can delete iterator once GC kicks in.
|
|||
wrapper.MakeWeak<void>(NULL, &DeleteNumberFormat); |
|||
args.GetReturnValue().Set(wrapper); |
|||
wrapper.ClearAndLeak(); |
|||
} |
|||
|
|||
static icu::DecimalFormat* InitializeNumberFormat( |
|||
v8::Handle<v8::String> locale, |
|||
v8::Handle<v8::Object> options, |
|||
v8::Handle<v8::Object> resolved) { |
|||
// Convert BCP47 into ICU locale format.
|
|||
UErrorCode status = U_ZERO_ERROR; |
|||
icu::Locale icu_locale; |
|||
char icu_result[ULOC_FULLNAME_CAPACITY]; |
|||
int icu_length = 0; |
|||
v8::String::AsciiValue bcp47_locale(locale); |
|||
if (bcp47_locale.length() != 0) { |
|||
uloc_forLanguageTag(*bcp47_locale, icu_result, ULOC_FULLNAME_CAPACITY, |
|||
&icu_length, &status); |
|||
if (U_FAILURE(status) || icu_length == 0) { |
|||
return NULL; |
|||
} |
|||
icu_locale = icu::Locale(icu_result); |
|||
} |
|||
|
|||
icu::DecimalFormat* number_format = |
|||
CreateICUNumberFormat(icu_locale, options); |
|||
if (!number_format) { |
|||
// Remove extensions and try again.
|
|||
icu::Locale no_extension_locale(icu_locale.getBaseName()); |
|||
number_format = CreateICUNumberFormat(no_extension_locale, options); |
|||
|
|||
// Set resolved settings (pattern, numbering system).
|
|||
SetResolvedSettings(no_extension_locale, number_format, resolved); |
|||
} else { |
|||
SetResolvedSettings(icu_locale, number_format, resolved); |
|||
} |
|||
|
|||
return number_format; |
|||
} |
|||
|
|||
static icu::DecimalFormat* CreateICUNumberFormat( |
|||
const icu::Locale& icu_locale, v8::Handle<v8::Object> options) { |
|||
// Make formatter from options. Numbering system is added
|
|||
// to the locale as Unicode extension (if it was specified at all).
|
|||
UErrorCode status = U_ZERO_ERROR; |
|||
icu::DecimalFormat* number_format = NULL; |
|||
icu::UnicodeString style; |
|||
icu::UnicodeString currency; |
|||
if (Utils::ExtractStringSetting(options, "style", &style)) { |
|||
if (style == UNICODE_STRING_SIMPLE("currency")) { |
|||
Utils::ExtractStringSetting(options, "currency", ¤cy); |
|||
|
|||
icu::UnicodeString display; |
|||
Utils::ExtractStringSetting(options, "currencyDisplay", &display); |
|||
#if (U_ICU_VERSION_MAJOR_NUM == 4) && (U_ICU_VERSION_MINOR_NUM <= 6) |
|||
icu::NumberFormat::EStyles style; |
|||
if (display == UNICODE_STRING_SIMPLE("code")) { |
|||
style = icu::NumberFormat::kIsoCurrencyStyle; |
|||
} else if (display == UNICODE_STRING_SIMPLE("name")) { |
|||
style = icu::NumberFormat::kPluralCurrencyStyle; |
|||
} else { |
|||
style = icu::NumberFormat::kCurrencyStyle; |
|||
} |
|||
#else // ICU version is 4.8 or above (we ignore versions below 4.0).
|
|||
UNumberFormatStyle style; |
|||
if (display == UNICODE_STRING_SIMPLE("code")) { |
|||
style = UNUM_CURRENCY_ISO; |
|||
} else if (display == UNICODE_STRING_SIMPLE("name")) { |
|||
style = UNUM_CURRENCY_PLURAL; |
|||
} else { |
|||
style = UNUM_CURRENCY; |
|||
} |
|||
#endif |
|||
|
|||
number_format = static_cast<icu::DecimalFormat*>( |
|||
icu::NumberFormat::createInstance(icu_locale, style, status)); |
|||
} else if (style == UNICODE_STRING_SIMPLE("percent")) { |
|||
number_format = static_cast<icu::DecimalFormat*>( |
|||
icu::NumberFormat::createPercentInstance(icu_locale, status)); |
|||
if (U_FAILURE(status)) { |
|||
delete number_format; |
|||
return NULL; |
|||
} |
|||
// Make sure 1.1% doesn't go into 2%.
|
|||
number_format->setMinimumFractionDigits(1); |
|||
} else { |
|||
// Make a decimal instance by default.
|
|||
number_format = static_cast<icu::DecimalFormat*>( |
|||
icu::NumberFormat::createInstance(icu_locale, status)); |
|||
} |
|||
} |
|||
|
|||
if (U_FAILURE(status)) { |
|||
delete number_format; |
|||
return NULL; |
|||
} |
|||
|
|||
// Set all options.
|
|||
if (!currency.isEmpty()) { |
|||
number_format->setCurrency(currency.getBuffer(), status); |
|||
} |
|||
|
|||
int32_t digits; |
|||
if (Utils::ExtractIntegerSetting( |
|||
options, "minimumIntegerDigits", &digits)) { |
|||
number_format->setMinimumIntegerDigits(digits); |
|||
} |
|||
|
|||
if (Utils::ExtractIntegerSetting( |
|||
options, "minimumFractionDigits", &digits)) { |
|||
number_format->setMinimumFractionDigits(digits); |
|||
} |
|||
|
|||
if (Utils::ExtractIntegerSetting( |
|||
options, "maximumFractionDigits", &digits)) { |
|||
number_format->setMaximumFractionDigits(digits); |
|||
} |
|||
|
|||
bool significant_digits_used = false; |
|||
if (Utils::ExtractIntegerSetting( |
|||
options, "minimumSignificantDigits", &digits)) { |
|||
number_format->setMinimumSignificantDigits(digits); |
|||
significant_digits_used = true; |
|||
} |
|||
|
|||
if (Utils::ExtractIntegerSetting( |
|||
options, "maximumSignificantDigits", &digits)) { |
|||
number_format->setMaximumSignificantDigits(digits); |
|||
significant_digits_used = true; |
|||
} |
|||
|
|||
number_format->setSignificantDigitsUsed(significant_digits_used); |
|||
|
|||
bool grouping; |
|||
if (Utils::ExtractBooleanSetting(options, "useGrouping", &grouping)) { |
|||
number_format->setGroupingUsed(grouping); |
|||
} |
|||
|
|||
// Set rounding mode.
|
|||
number_format->setRoundingMode(icu::DecimalFormat::kRoundHalfUp); |
|||
|
|||
return number_format; |
|||
} |
|||
|
|||
static void SetResolvedSettings(const icu::Locale& icu_locale, |
|||
icu::DecimalFormat* number_format, |
|||
v8::Handle<v8::Object> resolved) { |
|||
icu::UnicodeString pattern; |
|||
number_format->toPattern(pattern); |
|||
resolved->Set(v8::String::New("pattern"), |
|||
v8::String::New(reinterpret_cast<const uint16_t*>( |
|||
pattern.getBuffer()), pattern.length())); |
|||
|
|||
// Set resolved currency code in options.currency if not empty.
|
|||
icu::UnicodeString currency(number_format->getCurrency()); |
|||
if (!currency.isEmpty()) { |
|||
resolved->Set(v8::String::New("currency"), |
|||
v8::String::New(reinterpret_cast<const uint16_t*>( |
|||
currency.getBuffer()), currency.length())); |
|||
} |
|||
|
|||
// Ugly hack. ICU doesn't expose numbering system in any way, so we have
|
|||
// to assume that for given locale NumberingSystem constructor produces the
|
|||
// same digits as NumberFormat would.
|
|||
UErrorCode status = U_ZERO_ERROR; |
|||
icu::NumberingSystem* numbering_system = |
|||
icu::NumberingSystem::createInstance(icu_locale, status); |
|||
if (U_SUCCESS(status)) { |
|||
const char* ns = numbering_system->getName(); |
|||
resolved->Set(v8::String::New("numberingSystem"), v8::String::New(ns)); |
|||
} else { |
|||
resolved->Set(v8::String::New("numberingSystem"), v8::Undefined()); |
|||
} |
|||
delete numbering_system; |
|||
|
|||
resolved->Set(v8::String::New("useGrouping"), |
|||
v8::Boolean::New(number_format->isGroupingUsed())); |
|||
|
|||
resolved->Set(v8::String::New("minimumIntegerDigits"), |
|||
v8::Integer::New(number_format->getMinimumIntegerDigits())); |
|||
|
|||
resolved->Set(v8::String::New("minimumFractionDigits"), |
|||
v8::Integer::New(number_format->getMinimumFractionDigits())); |
|||
|
|||
resolved->Set(v8::String::New("maximumFractionDigits"), |
|||
v8::Integer::New(number_format->getMaximumFractionDigits())); |
|||
|
|||
if (resolved->HasOwnProperty(v8::String::New("minimumSignificantDigits"))) { |
|||
resolved->Set(v8::String::New("minimumSignificantDigits"), v8::Integer::New( |
|||
number_format->getMinimumSignificantDigits())); |
|||
} |
|||
|
|||
if (resolved->HasOwnProperty(v8::String::New("maximumSignificantDigits"))) { |
|||
resolved->Set(v8::String::New("maximumSignificantDigits"), v8::Integer::New( |
|||
number_format->getMaximumSignificantDigits())); |
|||
} |
|||
|
|||
// Set the locale
|
|||
char result[ULOC_FULLNAME_CAPACITY]; |
|||
status = U_ZERO_ERROR; |
|||
uloc_toLanguageTag( |
|||
icu_locale.getName(), result, ULOC_FULLNAME_CAPACITY, FALSE, &status); |
|||
if (U_SUCCESS(status)) { |
|||
resolved->Set(v8::String::New("locale"), v8::String::New(result)); |
|||
} else { |
|||
// This would never happen, since we got the locale from ICU.
|
|||
resolved->Set(v8::String::New("locale"), v8::String::New("und")); |
|||
} |
|||
} |
|||
|
|||
} // namespace v8_i18n
|
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue