63#if USE(ICU_UNICODE) && !UCONFIG_NO_FORMATTING && defined(ICU_VERSION_AT_LEAST) && ICU_VERSION_AT_LEAST(4, 4, 0)
64
65static UNumberFormat* cachedNumberFormatter = 0;
66static Mutex& cachedNumberFormatterMutex()
67{
68 AtomicallyInitializedStatic(Mutex&, mutex = *new Mutex);
69 return mutex;
70}
71
72static JSCell* formatLocaleNumber(ExecState* exec, JSValue v)
73{
74 ASSERT(v.isNumber());
75
76 Locker<Mutex> lock(cachedNumberFormatterMutex());
77 UErrorCode status = U_ZERO_ERROR;
78 UNumberFormat* nf = 0;
79 if (cachedNumberFormatter) {
80 const char* cachedNumberFormatterLocale = unum_getLocaleByType(cachedNumberFormatter, ULOC_REQUESTED_LOCALE, &status);
81 ASSERT(U_SUCCESS(status));
82 ASSERT(cachedNumberFormatterLocale);
83
84 if (!strcmp(cachedNumberFormatterLocale, currentNumericLocaleID()))
85 nf = cachedNumberFormatter;
86 else {
87 // locale changed... we'll need a new formatter
88 unum_close(cachedNumberFormatter);
89 cachedNumberFormatter = 0;
90 }
91 }
92
93 if (!nf) {
94 nf = unum_open(UNUM_DEFAULT, 0, 0, currentNumericLocaleID(), 0, &status);
95 if (!nf || U_FAILURE(status))
96 return jsEmptyString(exec);
97 cachedNumberFormatter = nf;
98 }
99
100 unum_setAttribute(nf, UNUM_MAX_SIGNIFICANT_DIGITS, WTF::NumberToStringBufferLength);
101 unum_setAttribute(nf, UNUM_SIGNIFICANT_DIGITS_USED, 1);
102
103 NumberToUStringBuffer buffer;
104 int32_t length;
105
106 if (v.isInt32()) {
107 int32_t num = v.asInt32();
108 length = unum_format(nf, num, buffer, WTF::NumberToStringBufferLength, 0, &status);
109 } else {
110 double num;
111 if (v.isDouble())
112 num = v.asDouble();
113 else
114 num = v.toNumber(exec);
115
116 DecimalNumber d(num);
117 char numberString[WTF::NumberToStringBufferLength];
118 length = d.toStringDecimal(numberString, WTF::NumberToStringBufferLength);
119 length = unum_formatDecimal(nf, numberString, length, buffer, WTF::NumberToStringBufferLength, 0, &status);
120 }
121
122 if (U_FAILURE(status))
123 return jsEmptyString(exec);
124
125 return jsNontrivialString(exec, UString(buffer, length));
126}
127
128#else
129
130static inline JSCell* formatLocaleNumber(ExecState* exec, JSValue v)
131{
132 // FIXME: Not implemented yet.
133
134 return v.toString(exec);
135}
136
137#endif
138