Skip to content

Commit

Permalink
deps: V8: backport 3f8dc4b2e5ba
Browse files Browse the repository at this point in the history
Original commit message:

    [intl] Remove soon-to-be removed getAllFieldPositions

    Needed to land ICU67.1 soon.

    Bug: v8:10393
    Change-Id: I3c7737ca600d6ccfdc46ffaddfb318ce60bc7618
    Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2136489
    Reviewed-by: Jakob Kummerow <jkummerow@chromium.org>
    Commit-Queue: Frank Tang <ftang@chromium.org>
    Cr-Commit-Position: refs/heads/master@{#67027}

Refs: v8/v8@3f8dc4b

PR-URL: #32993
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Matheus Marchini <mat@mmarchini.me>
Reviewed-By: Steven R Loomis <srloomis@us.ibm.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
  • Loading branch information
ryzokuken authored and BridgeAR committed Apr 28, 2020
1 parent 9bcfc8e commit 3956ab5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 36 deletions.
2 changes: 1 addition & 1 deletion common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

# Reset this number to 0 on major V8 upgrades.
# Increment by one for each non-official patch applied to deps/v8.
'v8_embedder_string': '-node.31',
'v8_embedder_string': '-node.32',

##### V8 defaults for Node.js #####

Expand Down
72 changes: 37 additions & 35 deletions deps/v8/src/objects/js-number-format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1187,42 +1187,31 @@ MaybeHandle<JSNumberFormat> JSNumberFormat::New(Isolate* isolate,
}

namespace {
Maybe<icu::UnicodeString> IcuFormatNumber(
Maybe<bool> IcuFormatNumber(
Isolate* isolate,
const icu::number::LocalizedNumberFormatter& number_format,
Handle<Object> numeric_obj, icu::FieldPositionIterator* fp_iter) {
Handle<Object> numeric_obj, icu::number::FormattedNumber* formatted) {
// If it is BigInt, handle it differently.
UErrorCode status = U_ZERO_ERROR;
icu::number::FormattedNumber formatted;
if (numeric_obj->IsBigInt()) {
Handle<BigInt> big_int = Handle<BigInt>::cast(numeric_obj);
Handle<String> big_int_string;
ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, big_int_string,
BigInt::ToString(isolate, big_int),
Nothing<icu::UnicodeString>());
formatted = number_format.formatDecimal(
Nothing<bool>());
*formatted = number_format.formatDecimal(
{big_int_string->ToCString().get(), big_int_string->length()}, status);
} else {
double number = numeric_obj->Number();
formatted = number_format.formatDouble(number, status);
*formatted = number_format.formatDouble(number, status);
}
if (U_FAILURE(status)) {
// This happen because of icu data trimming trim out "unit".
// See https://bugs.chromium.org/p/v8/issues/detail?id=8641
THROW_NEW_ERROR_RETURN_VALUE(isolate,
NewTypeError(MessageTemplate::kIcuError),
Nothing<icu::UnicodeString>());
}
if (fp_iter) {
formatted.getAllFieldPositions(*fp_iter, status);
THROW_NEW_ERROR_RETURN_VALUE(
isolate, NewTypeError(MessageTemplate::kIcuError), Nothing<bool>());
}
icu::UnicodeString result = formatted.toString(status);
if (U_FAILURE(status)) {
THROW_NEW_ERROR_RETURN_VALUE(isolate,
NewTypeError(MessageTemplate::kIcuError),
Nothing<icu::UnicodeString>());
}
return Just(result);
return Just(true);
}

} // namespace
Expand All @@ -1233,10 +1222,16 @@ MaybeHandle<String> JSNumberFormat::FormatNumeric(
Handle<Object> numeric_obj) {
DCHECK(numeric_obj->IsNumeric());

Maybe<icu::UnicodeString> maybe_format =
IcuFormatNumber(isolate, number_format, numeric_obj, nullptr);
icu::number::FormattedNumber formatted;
Maybe<bool> maybe_format =
IcuFormatNumber(isolate, number_format, numeric_obj, &formatted);
MAYBE_RETURN(maybe_format, Handle<String>());
return Intl::ToString(isolate, maybe_format.FromJust());
UErrorCode status = U_ZERO_ERROR;
icu::UnicodeString result = formatted.toString(status);
if (U_FAILURE(status)) {
THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kIcuError), String);
}
return Intl::ToString(isolate, result);
}

namespace {
Expand Down Expand Up @@ -1349,12 +1344,18 @@ std::vector<NumberFormatSpan> FlattenRegionsToParts(
}

namespace {
Maybe<int> ConstructParts(Isolate* isolate, const icu::UnicodeString& formatted,
icu::FieldPositionIterator* fp_iter,
Maybe<int> ConstructParts(Isolate* isolate,
icu::number::FormattedNumber* formatted,
Handle<JSArray> result, int start_index,
Handle<Object> numeric_obj, bool style_is_unit) {
UErrorCode status = U_ZERO_ERROR;
icu::UnicodeString formatted_text = formatted->toString(status);
if (U_FAILURE(status)) {
THROW_NEW_ERROR_RETURN_VALUE(
isolate, NewTypeError(MessageTemplate::kIcuError), Nothing<int>());
}
DCHECK(numeric_obj->IsNumeric());
int32_t length = formatted.length();
int32_t length = formatted_text.length();
int index = start_index;
if (length == 0) return Just(index);

Expand All @@ -1363,13 +1364,14 @@ Maybe<int> ConstructParts(Isolate* isolate, const icu::UnicodeString& formatted,
// other region covers some part of the formatted string. It's possible
// there's another field with exactly the same begin and end as this backdrop,
// in which case the backdrop's field_id of -1 will give it lower priority.
regions.push_back(NumberFormatSpan(-1, 0, formatted.length()));
regions.push_back(NumberFormatSpan(-1, 0, formatted_text.length()));

{
icu::FieldPosition fp;
while (fp_iter->next(fp)) {
regions.push_back(NumberFormatSpan(fp.getField(), fp.getBeginIndex(),
fp.getEndIndex()));
icu::ConstrainedFieldPosition cfp;
cfp.constrainCategory(UFIELD_CATEGORY_NUMBER);
while (formatted->nextPosition(cfp, status)) {
regions.push_back(
NumberFormatSpan(cfp.getField(), cfp.getStart(), cfp.getLimit()));
}
}

Expand All @@ -1391,7 +1393,7 @@ Maybe<int> ConstructParts(Isolate* isolate, const icu::UnicodeString& formatted,
Handle<String> substring;
ASSIGN_RETURN_ON_EXCEPTION_VALUE(
isolate, substring,
Intl::ToString(isolate, formatted, part.begin_pos, part.end_pos),
Intl::ToString(isolate, formatted_text, part.begin_pos, part.end_pos),
Nothing<int>());
Intl::AddElement(isolate, result, index, field_type_string, substring);
++index;
Expand All @@ -1411,14 +1413,14 @@ MaybeHandle<JSArray> JSNumberFormat::FormatToParts(
number_format->icu_number_formatter().raw();
CHECK_NOT_NULL(fmt);

icu::FieldPositionIterator fp_iter;
Maybe<icu::UnicodeString> maybe_format =
IcuFormatNumber(isolate, *fmt, numeric_obj, &fp_iter);
icu::number::FormattedNumber formatted;
Maybe<bool> maybe_format =
IcuFormatNumber(isolate, *fmt, numeric_obj, &formatted);
MAYBE_RETURN(maybe_format, Handle<JSArray>());

Handle<JSArray> result = factory->NewJSArray(0);
Maybe<int> maybe_format_to_parts = ConstructParts(
isolate, maybe_format.FromJust(), &fp_iter, result, 0, numeric_obj,
isolate, &formatted, result, 0, numeric_obj,
number_format->style() == JSNumberFormat::Style::UNIT);
MAYBE_RETURN(maybe_format_to_parts, Handle<JSArray>());

Expand Down

0 comments on commit 3956ab5

Please sign in to comment.