[Intl] Clean up error handling

- Return empty handles instead of exceptions.
- Handlify functions instead of returning raw pointers.

Bug: v8:5751
Cq-Include-Trybots: luci.v8.try:v8_linux_noi18n_rel_ng
Change-Id: I28b018e5bdcaa375748eb04e9a6fed8cf64a5b05
Reviewed-on: https://chromium-review.googlesource.com/1148301
Commit-Queue: Sathya Gunasekaran <gsathya@chromium.org>
Reviewed-by: Jakob Gruber <jgruber@chromium.org>
Cr-Commit-Position: refs/heads/master@{#54675}
This commit is contained in:
Sathya Gunasekaran 2018-07-25 12:32:22 +05:30 committed by Commit Bot
parent 021d814021
commit 0af7b0d6ea
8 changed files with 75 additions and 65 deletions

View File

@ -111,7 +111,8 @@ BUILTIN(StringPrototypeNormalizeIntl) {
}
if (U_FAILURE(status)) {
return ReadOnlyRoots(isolate).undefined_value();
THROW_NEW_ERROR_RETURN_FAILURE(isolate,
NewTypeError(MessageTemplate::kIcuError));
}
RETURN_RESULT_OR_FAILURE(
@ -282,19 +283,21 @@ bool cmp_NumberFormatSpan(const NumberFormatSpan& a,
return a.field_id < b.field_id;
}
Object* FormatNumberToParts(Isolate* isolate, icu::NumberFormat* fmt,
double number) {
MaybeHandle<Object> FormatNumberToParts(Isolate* isolate,
icu::NumberFormat* fmt, double number) {
Factory* factory = isolate->factory();
icu::UnicodeString formatted;
icu::FieldPositionIterator fp_iter;
UErrorCode status = U_ZERO_ERROR;
fmt->format(number, formatted, &fp_iter, status);
if (U_FAILURE(status)) return ReadOnlyRoots(isolate).undefined_value();
if (U_FAILURE(status)) {
THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kIcuError), Object);
}
Handle<JSArray> result = factory->NewJSArray(0);
int32_t length = formatted.length();
if (length == 0) return *result;
if (length == 0) return result;
std::vector<NumberFormatSpan> regions;
// Add a "literal" backdrop for the entire string. This will be used if no
@ -323,16 +326,16 @@ Object* FormatNumberToParts(Isolate* isolate, icu::NumberFormat* fmt,
Maybe<bool> maybe_added_element =
AddElement(isolate, result, index, field_type_string, formatted,
part.begin_pos, part.end_pos);
MAYBE_RETURN(maybe_added_element, ReadOnlyRoots(isolate).undefined_value());
MAYBE_RETURN(maybe_added_element, MaybeHandle<Object>());
++index;
}
JSObject::ValidateElements(*result);
return *result;
return result;
}
Object* FormatDateToParts(Isolate* isolate, icu::DateFormat* format,
double date_value) {
MaybeHandle<Object> FormatDateToParts(Isolate* isolate, icu::DateFormat* format,
double date_value) {
Factory* factory = isolate->factory();
icu::UnicodeString formatted;
@ -340,11 +343,13 @@ Object* FormatDateToParts(Isolate* isolate, icu::DateFormat* format,
icu::FieldPosition fp;
UErrorCode status = U_ZERO_ERROR;
format->format(date_value, formatted, &fp_iter, status);
if (U_FAILURE(status)) return ReadOnlyRoots(isolate).undefined_value();
if (U_FAILURE(status)) {
THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kIcuError), Object);
}
Handle<JSArray> result = factory->NewJSArray(0);
int32_t length = formatted.length();
if (length == 0) return *result;
if (length == 0) return result;
int index = 0;
int32_t previous_end_pos = 0;
@ -356,15 +361,14 @@ Object* FormatDateToParts(Isolate* isolate, icu::DateFormat* format,
Maybe<bool> maybe_added_element = AddElement(
isolate, result, index, IcuDateFieldIdToDateType(-1, isolate),
formatted, previous_end_pos, begin_pos);
MAYBE_RETURN(maybe_added_element,
ReadOnlyRoots(isolate).undefined_value());
MAYBE_RETURN(maybe_added_element, MaybeHandle<Object>());
++index;
}
Maybe<bool> maybe_added_element =
AddElement(isolate, result, index,
IcuDateFieldIdToDateType(fp.getField(), isolate), formatted,
begin_pos, end_pos);
MAYBE_RETURN(maybe_added_element, ReadOnlyRoots(isolate).undefined_value());
MAYBE_RETURN(maybe_added_element, MaybeHandle<Object>());
previous_end_pos = end_pos;
++index;
}
@ -372,10 +376,10 @@ Object* FormatDateToParts(Isolate* isolate, icu::DateFormat* format,
Maybe<bool> maybe_added_element = AddElement(
isolate, result, index, IcuDateFieldIdToDateType(-1, isolate),
formatted, previous_end_pos, length);
MAYBE_RETURN(maybe_added_element, ReadOnlyRoots(isolate).undefined_value());
MAYBE_RETURN(maybe_added_element, MaybeHandle<Object>());
}
JSObject::ValidateElements(*result);
return *result;
return result;
}
} // namespace
@ -496,8 +500,8 @@ BUILTIN(NumberFormatPrototypeFormatToParts) {
NumberFormat::UnpackNumberFormat(number_format_holder);
CHECK_NOT_NULL(number_format);
Object* result = FormatNumberToParts(isolate, number_format, x->Number());
return result;
RETURN_RESULT_OR_FAILURE(
isolate, FormatNumberToParts(isolate, number_format, x->Number()));
}
BUILTIN(DateTimeFormatPrototypeFormatToParts) {
@ -532,7 +536,8 @@ BUILTIN(DateTimeFormatPrototypeFormatToParts) {
DateFormat::UnpackDateFormat(date_format_holder);
CHECK_NOT_NULL(date_format);
return FormatDateToParts(isolate, date_format, date_value);
RETURN_RESULT_OR_FAILURE(isolate,
FormatDateToParts(isolate, date_format, date_value));
}
BUILTIN(NumberFormatPrototypeFormatNumber) {
@ -846,7 +851,7 @@ bool GetURelativeDateTimeUnit(Handle<String> unit,
return true;
}
Object* RelativeTimeFormatPrototypeFormatCommon(
MaybeHandle<Object> RelativeTimeFormatPrototypeFormatCommon(
BuiltinArguments args, Isolate* isolate,
Handle<JSRelativeTimeFormat> format_holder, const char* func_name,
bool to_parts) {
@ -856,20 +861,21 @@ Object* RelativeTimeFormatPrototypeFormatCommon(
// 3. Let value be ? ToNumber(value).
Handle<Object> value;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, value,
Object::ToNumber(isolate, value_obj));
ASSIGN_RETURN_ON_EXCEPTION(isolate, value,
Object::ToNumber(isolate, value_obj), Object);
double number = value->Number();
// 4. Let unit be ? ToString(unit).
Handle<String> unit;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, unit,
Object::ToString(isolate, unit_obj));
ASSIGN_RETURN_ON_EXCEPTION(isolate, unit, Object::ToString(isolate, unit_obj),
Object);
// 4. If isFinite(value) is false, then throw a RangeError exception.
if (!std::isfinite(number)) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewRangeError(
MessageTemplate::kNotFiniteNumber,
isolate->factory()->NewStringFromAsciiChecked(func_name)));
THROW_NEW_ERROR(
isolate,
NewRangeError(MessageTemplate::kNotFiniteNumber,
isolate->factory()->NewStringFromAsciiChecked(func_name)),
Object);
}
icu::RelativeDateTimeFormatter* formatter =
@ -878,11 +884,12 @@ Object* RelativeTimeFormatPrototypeFormatCommon(
URelativeDateTimeUnit unit_enum;
if (!GetURelativeDateTimeUnit(unit, &unit_enum)) {
THROW_NEW_ERROR_RETURN_FAILURE(
THROW_NEW_ERROR(
isolate,
NewRangeError(MessageTemplate::kInvalidUnit,
isolate->factory()->NewStringFromAsciiChecked(func_name),
unit));
unit),
Object);
}
UErrorCode status = U_ZERO_ERROR;
@ -899,8 +906,7 @@ Object* RelativeTimeFormatPrototypeFormatCommon(
}
if (U_FAILURE(status)) {
// Internal ICU error.
return ReadOnlyRoots(isolate).undefined_value();
THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kIcuError), Object);
}
if (to_parts) {
@ -908,20 +914,21 @@ Object* RelativeTimeFormatPrototypeFormatCommon(
icu::FieldPosition pos;
formatter->getNumberFormat().format(std::abs(number), integer, pos, status);
if (U_FAILURE(status)) {
// Internal ICU error.
return ReadOnlyRoots(isolate).undefined_value();
THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kIcuError),
Object);
}
Handle<JSArray> elements;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
ASSIGN_RETURN_ON_EXCEPTION(
isolate, elements,
GenerateRelativeTimeFormatParts(isolate, formatted, integer, unit));
return *elements;
GenerateRelativeTimeFormatParts(isolate, formatted, integer, unit),
Object);
return elements;
}
RETURN_RESULT_OR_FAILURE(
isolate, factory->NewStringFromTwoByte(Vector<const uint16_t>(
reinterpret_cast<const uint16_t*>(formatted.getBuffer()),
formatted.length())));
return factory->NewStringFromTwoByte(Vector<const uint16_t>(
reinterpret_cast<const uint16_t*>(formatted.getBuffer()),
formatted.length()));
}
} // namespace
@ -934,8 +941,9 @@ BUILTIN(RelativeTimeFormatPrototypeFormat) {
// true, throw a TypeError exception.
CHECK_RECEIVER(JSRelativeTimeFormat, format_holder,
"Intl.RelativeTimeFormat.prototype.format");
return RelativeTimeFormatPrototypeFormatCommon(args, isolate, format_holder,
"format", false);
RETURN_RESULT_OR_FAILURE(isolate,
RelativeTimeFormatPrototypeFormatCommon(
args, isolate, format_holder, "format", false));
}
BUILTIN(RelativeTimeFormatPrototypeFormatToParts) {
@ -946,8 +954,9 @@ BUILTIN(RelativeTimeFormatPrototypeFormatToParts) {
// true, throw a TypeError exception.
CHECK_RECEIVER(JSRelativeTimeFormat, format_holder,
"Intl.RelativeTimeFormat.prototype.formatToParts");
return RelativeTimeFormatPrototypeFormatCommon(args, isolate, format_holder,
"formatToParts", true);
RETURN_RESULT_OR_FAILURE(
isolate, RelativeTimeFormatPrototypeFormatCommon(
args, isolate, format_holder, "formatToParts", true));
}
// Locale getters.

View File

@ -263,6 +263,7 @@ class ErrorUtils : public AllStatic {
T(Unsupported, "Not supported") \
T(WrongServiceType, "Internal error, wrong service type: %") \
T(WrongValueType, "Internal error. Wrong value type.") \
T(IcuError, "Internal error. Icu error.") \
/* TypeError */ \
T(ApplyNonFunction, \
"Function.prototype.apply was called on %, which is a % and not a " \

View File

@ -362,7 +362,7 @@ bytecodes: [
B(TestTypeOf), U8(6),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(150),
B(Wide), B(LdaSmi), I16(151),
B(Star), R(18),
B(LdaConstant), U8(14),
B(Star), R(19),

View File

@ -123,7 +123,7 @@ bytecodes: [
B(TestTypeOf), U8(6),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(150),
B(Wide), B(LdaSmi), I16(151),
B(Star), R(19),
B(LdaConstant), U8(11),
B(Star), R(20),
@ -377,7 +377,7 @@ bytecodes: [
B(TestTypeOf), U8(6),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(150),
B(Wide), B(LdaSmi), I16(151),
B(Star), R(19),
B(LdaConstant), U8(11),
B(Star), R(20),
@ -653,7 +653,7 @@ bytecodes: [
B(TestTypeOf), U8(6),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(150),
B(Wide), B(LdaSmi), I16(151),
B(Star), R(19),
B(LdaConstant), U8(11),
B(Star), R(20),
@ -885,7 +885,7 @@ bytecodes: [
B(TestTypeOf), U8(6),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(150),
B(Wide), B(LdaSmi), I16(151),
B(Star), R(17),
B(LdaConstant), U8(9),
B(Star), R(18),

View File

@ -85,7 +85,7 @@ bytecodes: [
B(TestTypeOf), U8(6),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(150),
B(Wide), B(LdaSmi), I16(151),
B(Star), R(12),
B(LdaConstant), U8(7),
B(Star), R(13),
@ -217,7 +217,7 @@ bytecodes: [
B(TestTypeOf), U8(6),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(150),
B(Wide), B(LdaSmi), I16(151),
B(Star), R(13),
B(LdaConstant), U8(7),
B(Star), R(14),
@ -361,7 +361,7 @@ bytecodes: [
B(TestTypeOf), U8(6),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(150),
B(Wide), B(LdaSmi), I16(151),
B(Star), R(12),
B(LdaConstant), U8(7),
B(Star), R(13),
@ -495,7 +495,7 @@ bytecodes: [
B(TestTypeOf), U8(6),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(150),
B(Wide), B(LdaSmi), I16(151),
B(Star), R(11),
B(LdaConstant), U8(9),
B(Star), R(12),

View File

@ -89,7 +89,7 @@ bytecodes: [
B(TestTypeOf), U8(6),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(150),
B(Wide), B(LdaSmi), I16(151),
B(Star), R(14),
B(LdaConstant), U8(6),
B(Star), R(15),
@ -256,7 +256,7 @@ bytecodes: [
B(TestTypeOf), U8(6),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(150),
B(Wide), B(LdaSmi), I16(151),
B(Star), R(14),
B(LdaConstant), U8(11),
B(Star), R(15),
@ -401,7 +401,7 @@ bytecodes: [
B(TestTypeOf), U8(6),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(150),
B(Wide), B(LdaSmi), I16(151),
B(Star), R(12),
B(LdaConstant), U8(8),
B(Star), R(13),
@ -495,7 +495,7 @@ bytecodes: [
B(JumpIfUndefined), U8(6),
B(Ldar), R(6),
B(JumpIfNotNull), U8(16),
B(LdaSmi), I8(78),
B(LdaSmi), I8(79),
B(Star), R(18),
B(LdaConstant), U8(4),
B(Star), R(19),
@ -550,7 +550,7 @@ bytecodes: [
B(TestTypeOf), U8(6),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(150),
B(Wide), B(LdaSmi), I16(151),
B(Star), R(17),
B(LdaConstant), U8(8),
B(Star), R(18),
@ -697,7 +697,7 @@ bytecodes: [
B(TestTypeOf), U8(6),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(150),
B(Wide), B(LdaSmi), I16(151),
B(Star), R(15),
B(LdaConstant), U8(9),
B(Star), R(16),
@ -859,7 +859,7 @@ bytecodes: [
B(TestTypeOf), U8(6),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(150),
B(Wide), B(LdaSmi), I16(151),
B(Star), R(14),
B(LdaConstant), U8(12),
B(Star), R(15),
@ -1007,7 +1007,7 @@ bytecodes: [
B(TestTypeOf), U8(6),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(150),
B(Wide), B(LdaSmi), I16(151),
B(Star), R(20),
B(LdaConstant), U8(6),
B(Star), R(21),
@ -1218,7 +1218,7 @@ bytecodes: [
B(TestTypeOf), U8(6),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(150),
B(Wide), B(LdaSmi), I16(151),
B(Star), R(19),
B(LdaConstant), U8(7),
B(Star), R(20),

View File

@ -203,7 +203,7 @@ bytecodes: [
B(TestTypeOf), U8(6),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(150),
B(Wide), B(LdaSmi), I16(151),
B(Star), R(14),
B(LdaConstant), U8(13),
B(Star), R(15),

View File

@ -229,7 +229,7 @@ bytecodes: [
B(JumpIfUndefined), U8(6),
B(Ldar), R(3),
B(JumpIfNotNull), U8(16),
B(LdaSmi), I8(78),
B(LdaSmi), I8(79),
B(Star), R(4),
B(LdaConstant), U8(1),
B(Star), R(5),