[Intl] move the logic of formatDate into C++

Spin off cl from +/1155271

Bug: v8:7961
Cq-Include-Trybots: luci.v8.try:v8_linux_noi18n_rel_ng
Change-Id: Ibf4bfdcea5ba391281a7d57ffa23d6a96c6ce6a1
Reviewed-on: https://chromium-review.googlesource.com/1164528
Reviewed-by: Sathya Gunasekaran <gsathya@chromium.org>
Commit-Queue: Frank Tang <ftang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55144}
This commit is contained in:
Frank Tang 2018-08-15 10:25:02 -07:00 committed by Commit Bot
parent 9040405eb9
commit 6950ba614d
5 changed files with 48 additions and 29 deletions

View File

@ -1345,14 +1345,7 @@ DEFINE_METHOD(
* DateTimeFormat.
*/
function formatDate(formatter, dateValue) {
var dateMs;
if (IS_UNDEFINED(dateValue)) {
dateMs = %DateCurrentTime();
} else {
dateMs = TO_NUMBER(dateValue);
}
return %InternalDateFormat(formatter, dateMs);
return %FormatDate(formatter, dateValue);
}
// Length is 1 as specified in ECMA 402 v2+
@ -1630,7 +1623,7 @@ function toLocaleDateTime(date, locales, options, required, defaults, service) {
var dateFormat =
cachedOrNewService(service, locales, options, internalOptions);
return formatDate(dateFormat, date);
return %FormatDate(dateFormat, date);
}

View File

@ -620,6 +620,42 @@ void DateFormat::DeleteDateFormat(const v8::WeakCallbackInfo<void>& data) {
GlobalHandles::Destroy(reinterpret_cast<Object**>(data.GetParameter()));
}
MaybeHandle<String> DateFormat::FormatDate(Isolate* isolate,
Handle<JSObject> date_format_holder,
Handle<Object> date) {
// 3. If date is not provided or is undefined, then
double x;
if (date->IsUndefined()) {
// 3.a Let x be Call(%Date_now%, undefined).
x = JSDate::CurrentTimeValue(isolate);
} else {
// 4. Else,
// a. Let x be ? ToNumber(date).
ASSIGN_RETURN_ON_EXCEPTION(isolate, date, Object::ToNumber(isolate, date),
String);
CHECK(date->IsNumber());
x = date->Number();
}
double date_value = DateCache::TimeClip(x);
if (std::isnan(date_value)) {
THROW_NEW_ERROR(isolate, NewRangeError(MessageTemplate::kInvalidTimeValue),
String);
}
CHECK(Intl::IsObjectOfType(isolate, date_format_holder,
Intl::Type::kDateTimeFormat));
icu::SimpleDateFormat* date_format =
DateFormat::UnpackDateFormat(date_format_holder);
CHECK_NOT_NULL(date_format);
icu::UnicodeString result;
date_format->format(date_value, result);
return isolate->factory()->NewStringFromTwoByte(Vector<const uint16_t>(
reinterpret_cast<const uint16_t*>(result.getBuffer()), result.length()));
}
icu::DecimalFormat* NumberFormat::InitializeNumberFormat(
Isolate* isolate, Handle<String> locale, Handle<JSObject> options,
Handle<JSObject> resolved) {

View File

@ -49,6 +49,11 @@ class DateFormat {
// holds the pointer gets garbage collected.
static void DeleteDateFormat(const v8::WeakCallbackInfo<void>& data);
// ecma402/#sec-datetime-format-functions
V8_WARN_UNUSED_RESULT static MaybeHandle<String> FormatDate(
Isolate* isolate, Handle<JSObject> date_format_holder,
Handle<Object> date);
// Layout description.
static const int kSimpleDateFormat = JSObject::kHeaderSize;
static const int kSize = kSimpleDateFormat + kPointerSize;

View File

@ -228,31 +228,16 @@ RUNTIME_FUNCTION(Runtime_CreateDateTimeFormat) {
return *local_object;
}
RUNTIME_FUNCTION(Runtime_InternalDateFormat) {
RUNTIME_FUNCTION(Runtime_FormatDate) {
HandleScope scope(isolate);
DCHECK_EQ(2, args.length());
CONVERT_ARG_HANDLE_CHECKED(JSObject, date_format_holder, 0);
CONVERT_NUMBER_ARG_HANDLE_CHECKED(date, 1);
double date_value = DateCache::TimeClip(date->Number());
if (std::isnan(date_value)) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewRangeError(MessageTemplate::kInvalidTimeValue));
}
icu::SimpleDateFormat* date_format =
DateFormat::UnpackDateFormat(date_format_holder);
CHECK_NOT_NULL(date_format);
icu::UnicodeString result;
date_format->format(date_value, result);
CONVERT_ARG_HANDLE_CHECKED(Object, date, 1);
RETURN_RESULT_OR_FAILURE(
isolate, isolate->factory()->NewStringFromTwoByte(Vector<const uint16_t>(
reinterpret_cast<const uint16_t*>(result.getBuffer()),
result.length())));
isolate, DateFormat::FormatDate(isolate, date_format_holder, date));
}
RUNTIME_FUNCTION(Runtime_CreateNumberFormat) {

View File

@ -213,15 +213,15 @@ namespace internal {
F(CreateBreakIterator, 3, 1) \
F(CreateDateTimeFormat, 3, 1) \
F(CreateNumberFormat, 3, 1) \
F(DefineWEProperty, 3, 1) \
F(CurrencyDigits, 1, 1) \
F(DateCacheVersion, 0, 1) \
F(DefaultNumberOption, 5, 1) \
F(DefineWEProperty, 3, 1) \
F(FormatList, 2, 1) \
F(FormatListToParts, 2, 1) \
F(FormatDate, 2, 1) \
F(GetDefaultICULocale, 0, 1) \
F(GetNumberOption, 5, 1) \
F(InternalDateFormat, 2, 1) \
F(IntlUnwrapReceiver, 5, 1) \
F(IsInitializedIntlObjectOfType, 2, 1) \
F(IsWellFormedCurrencyCode, 1, 1) \