Skip to content

Commit

Permalink
Add support for DateTimeFormat.formatToParts
Browse files Browse the repository at this point in the history
Spec discussion:  tc39/ecma402#30

It's in stage 4 and Firefox has already implemented it.

For now, it's added to HARMONY_IN_PROGRESS bucket behind
'--datetime-format-to-parts' flag.

BUG=v8:5244
TEST=intl/date-format/date-format-to-parts.js
TEST=test262/intl402/DateTimeFormat/prototype/formatToParts/*

Review-Url: https://codereview.chromium.org/2273953003
Cr-Commit-Position: refs/heads/master@{#39225}
  • Loading branch information
jungshik authored and Commit bot committed Sep 6, 2016
1 parent 01cc19f commit a3db819
Show file tree
Hide file tree
Showing 13 changed files with 256 additions and 13 deletions.
1 change: 1 addition & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ action("js2c_experimental") {

if (v8_enable_i18n_support) {
sources += [
"src/js/datetime-format-to-parts.js",
"src/js/icu-case-mapping.js",
"src/js/intl-extra.js",
]
Expand Down
15 changes: 13 additions & 2 deletions src/bootstrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ class Genesis BASE_EMBEDDED {
HARMONY_INPROGRESS(DECLARE_FEATURE_INITIALIZATION)
HARMONY_STAGED(DECLARE_FEATURE_INITIALIZATION)
HARMONY_SHIPPING(DECLARE_FEATURE_INITIALIZATION)
#ifdef V8_I18N_SUPPORT
DECLARE_FEATURE_INITIALIZATION(intl_extra, "")
#endif
#undef DECLARE_FEATURE_INITIALIZATION

Handle<JSFunction> InstallArrayBuffer(Handle<JSObject> target,
Expand Down Expand Up @@ -2209,7 +2211,9 @@ void Genesis::InitializeExperimentalGlobal() {
HARMONY_INPROGRESS(FEATURE_INITIALIZE_GLOBAL)
HARMONY_STAGED(FEATURE_INITIALIZE_GLOBAL)
HARMONY_SHIPPING(FEATURE_INITIALIZE_GLOBAL)
#ifdef V8_I18N_SUPPORT
FEATURE_INITIALIZE_GLOBAL(intl_extra, "")
#endif
#undef FEATURE_INITIALIZE_GLOBAL
}

Expand Down Expand Up @@ -2771,6 +2775,7 @@ void Bootstrapper::ExportExperimentalFromRuntime(Isolate* isolate,
Handle<JSObject> container) {
HandleScope scope(isolate);

#ifdef V8_I18N_SUPPORT
#define INITIALIZE_FLAG(FLAG) \
{ \
Handle<String> name = \
Expand All @@ -2782,6 +2787,7 @@ void Bootstrapper::ExportExperimentalFromRuntime(Isolate* isolate,
INITIALIZE_FLAG(FLAG_intl_extra)

#undef INITIALIZE_FLAG
#endif
}


Expand All @@ -2794,13 +2800,14 @@ EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_regexp_lookbehind)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_regexp_named_captures)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_regexp_property)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_function_sent)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(intl_extra)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_explicit_tailcalls)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_tailcalls)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_restrictive_declarations)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_string_padding)
#ifdef V8_I18N_SUPPORT
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(datetime_format_to_parts)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(icu_case_mapping)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(intl_extra)
#endif
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_async_await)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_restrictive_generators)
Expand Down Expand Up @@ -3381,7 +3388,6 @@ bool Genesis::InstallExperimentalNatives() {
static const char* harmony_regexp_named_captures_natives[] = {nullptr};
static const char* harmony_regexp_property_natives[] = {nullptr};
static const char* harmony_function_sent_natives[] = {nullptr};
static const char* intl_extra_natives[] = {"native intl-extra.js", nullptr};
static const char* harmony_object_values_entries_natives[] = {nullptr};
static const char* harmony_object_own_property_descriptors_natives[] = {
nullptr};
Expand All @@ -3391,6 +3397,9 @@ bool Genesis::InstallExperimentalNatives() {
#ifdef V8_I18N_SUPPORT
static const char* icu_case_mapping_natives[] = {"native icu-case-mapping.js",
nullptr};
static const char* intl_extra_natives[] = {"native intl-extra.js", nullptr};
static const char* datetime_format_to_parts_natives[] = {
"native datetime-format-to-parts.js", nullptr};
#endif
static const char* harmony_async_await_natives[] = {
"native harmony-async-await.js", nullptr};
Expand All @@ -3414,7 +3423,9 @@ bool Genesis::InstallExperimentalNatives() {
HARMONY_INPROGRESS(INSTALL_EXPERIMENTAL_NATIVES);
HARMONY_STAGED(INSTALL_EXPERIMENTAL_NATIVES);
HARMONY_SHIPPING(INSTALL_EXPERIMENTAL_NATIVES);
#ifdef V8_I18N_SUPPORT
INSTALL_EXPERIMENTAL_NATIVES(intl_extra, "");
#endif
#undef INSTALL_EXPERIMENTAL_NATIVES
}

Expand Down
12 changes: 11 additions & 1 deletion src/flag-definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,18 @@ DEFINE_BOOL(harmony, false, "enable all completed harmony features")
DEFINE_BOOL(harmony_shipping, true, "enable all shipped harmony features")
DEFINE_IMPLICATION(es_staging, harmony)

#ifdef V8_I18N_SUPPORT
DEFINE_BOOL(intl_extra, false, "additional V8 Intl functions")
// Removing extra Intl functions is shipped
DEFINE_NEG_VALUE_IMPLICATION(harmony_shipping, intl_extra, true)
#endif

// Activate on ClusterFuzz.
DEFINE_IMPLICATION(es_staging, harmony_regexp_lookbehind)
DEFINE_IMPLICATION(es_staging, move_object_start)

// Features that are still work in progress (behind individual flags).
#define HARMONY_INPROGRESS(V) \
#define HARMONY_INPROGRESS_BASE(V) \
V(harmony_array_prototype_values, "harmony Array.prototype.values") \
V(harmony_function_sent, "harmony function.sent") \
V(harmony_sharedarraybuffer, "harmony sharedarraybuffer") \
Expand All @@ -204,6 +206,14 @@ DEFINE_IMPLICATION(es_staging, move_object_start)
V(harmony_trailing_commas, \
"harmony trailing commas in function parameter lists")

#ifdef V8_I18N_SUPPORT
#define HARMONY_INPROGRESS(V) \
HARMONY_INPROGRESS_BASE(V) \
V(datetime_format_to_parts, "Intl.DateTimeFormat.formatToParts")
#else
#define HARMONY_INPROGRESS(V) HARMONY_INPROGRESS_BASE(V)
#endif

// Features that are complete (but still behind --harmony/es-staging flag).
#define HARMONY_STAGED_BASE(V) \
V(harmony_regexp_lookbehind, "harmony regexp lookbehind") \
Expand Down
20 changes: 17 additions & 3 deletions src/heap-symbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
#define INTERNALIZED_STRING_LIST(V) \
V(anonymous_string, "anonymous") \
V(apply_string, "apply") \
V(assign_string, "assign") \
V(arguments_string, "arguments") \
V(Arguments_string, "Arguments") \
V(Array_string, "Array") \
V(arguments_to_string, "[object Arguments]") \
V(Array_string, "Array") \
V(assign_string, "assign") \
V(array_to_string, "[object Array]") \
V(boolean_to_string, "[object Boolean]") \
V(date_to_string, "[object Date]") \
Expand Down Expand Up @@ -48,6 +48,8 @@
V(construct_string, "construct") \
V(create_string, "create") \
V(Date_string, "Date") \
V(dayperiod_string, "dayperiod") \
V(day_string, "day") \
V(default_string, "default") \
V(defineProperty_string, "defineProperty") \
V(deleteProperty_string, "deleteProperty") \
Expand All @@ -57,6 +59,7 @@
V(dot_string, ".") \
V(entries_string, "entries") \
V(enumerable_string, "enumerable") \
V(era_string, "era") \
V(Error_string, "Error") \
V(eval_string, "eval") \
V(EvalError_string, "EvalError") \
Expand All @@ -74,6 +77,8 @@
V(get_string, "get") \
V(global_string, "global") \
V(has_string, "has") \
V(hour_string, "hour") \
V(ignoreCase_string, "ignoreCase") \
V(illegal_access_string, "illegal access") \
V(illegal_argument_string, "illegal argument") \
V(index_string, "index") \
Expand All @@ -92,10 +97,14 @@
V(last_index_string, "lastIndex") \
V(length_string, "length") \
V(line_string, "line") \
V(literal_string, "literal") \
V(Map_string, "Map") \
V(message_string, "message") \
V(minus_infinity_string, "-Infinity") \
V(minus_zero_string, "-0") \
V(minute_string, "minute") \
V(month_string, "month") \
V(multiline_string, "multiline") \
V(name_string, "name") \
V(nan_string, "NaN") \
V(next_string, "next") \
Expand All @@ -120,6 +129,7 @@
V(ReferenceError_string, "ReferenceError") \
V(RegExp_string, "RegExp") \
V(script_string, "script") \
V(second_string, "second") \
V(setPrototypeOf_string, "setPrototypeOf") \
V(set_string, "set") \
V(Set_string, "Set") \
Expand All @@ -138,10 +148,12 @@
V(this_string, "this") \
V(throw_string, "throw") \
V(timed_out, "timed-out") \
V(timeZoneName_string, "timeZoneName") \
V(toJSON_string, "toJSON") \
V(toString_string, "toString") \
V(true_string, "true") \
V(TypeError_string, "TypeError") \
V(type_string, "type") \
V(uint16x8_string, "uint16x8") \
V(Uint16x8_string, "Uint16x8") \
V(uint32x4_string, "uint32x4") \
Expand All @@ -156,7 +168,9 @@
V(value_string, "value") \
V(WeakMap_string, "WeakMap") \
V(WeakSet_string, "WeakSet") \
V(writable_string, "writable")
V(weekday_string, "weekday") \
V(writable_string, "writable") \
V(year_string, "year")

#define PRIVATE_SYMBOL_LIST(V) \
V(array_iteration_kind_symbol) \
Expand Down
12 changes: 6 additions & 6 deletions src/i18n.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "src/i18n.h"

#include <memory>

#include "src/api.h"
#include "src/factory.h"
#include "src/isolate.h"
Expand Down Expand Up @@ -115,13 +117,11 @@ icu::SimpleDateFormat* CreateICUDateFormat(
icu::SimpleDateFormat* date_format = NULL;
icu::UnicodeString skeleton;
if (ExtractStringSetting(isolate, options, "skeleton", &skeleton)) {
icu::DateTimePatternGenerator* generator =
icu::DateTimePatternGenerator::createInstance(icu_locale, status);
std::unique_ptr<icu::DateTimePatternGenerator> generator(
icu::DateTimePatternGenerator::createInstance(icu_locale, status));
icu::UnicodeString pattern;
if (U_SUCCESS(status)) {
if (U_SUCCESS(status))
pattern = generator->getBestPattern(skeleton, status);
delete generator;
}

date_format = new icu::SimpleDateFormat(pattern, icu_locale, status);
if (U_SUCCESS(status)) {
Expand All @@ -132,7 +132,7 @@ icu::SimpleDateFormat* CreateICUDateFormat(
if (U_FAILURE(status)) {
delete calendar;
delete date_format;
date_format = NULL;
date_format = nullptr;
}

return date_format;
Expand Down
16 changes: 16 additions & 0 deletions src/js/datetime-format-to-parts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

(function(global, utils) {
"use strict";

%CheckIsBootstrapping();

var GlobalIntl = global.Intl;
var FormatDateToParts = utils.ImportNow("FormatDateToParts");

utils.InstallFunctions(GlobalIntl.DateTimeFormat.prototype, DONT_ENUM, [
'formatToParts', FormatDateToParts
]);
})
26 changes: 26 additions & 0 deletions src/js/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -1797,6 +1797,29 @@ function formatDate(formatter, dateValue) {
new GlobalDate(dateMs));
}

function FormatDateToParts(dateValue) {
if (!IS_UNDEFINED(new.target)) {
throw %make_type_error(kOrdinaryFunctionCalledAsConstructor);
}
CHECK_OBJECT_COERCIBLE(this, "Intl.DateTimeFormat.prototype.formatToParts");
if (!IS_OBJECT(this)) {
throw %make_type_error(kCalledOnNonObject, this);
}
var dateMs;
if (IS_UNDEFINED(dateValue)) {
dateMs = %DateCurrentTime();
} else {
dateMs = TO_NUMBER(dateValue);
}

if (!NUMBER_IS_FINITE(dateMs)) throw %make_range_error(kDateRange);

return %InternalDateFormatToParts(
%GetImplFromInitializedIntlObject(this), new GlobalDate(dateMs));
}

%FunctionSetLength(FormatDateToParts, 0);


/**
* Returns a Date object representing the result of calling ToString(value)
Expand Down Expand Up @@ -2291,8 +2314,11 @@ OverrideFunction(GlobalDate.prototype, 'toLocaleTimeString', function() {
}
);

%FunctionRemovePrototype(FormatDateToParts);

utils.Export(function(to) {
to.AddBoundMethod = AddBoundMethod;
to.FormatDateToParts = FormatDateToParts;
to.IntlParseDate = IntlParseDate;
to.IntlParseNumber = IntlParseNumber;
});
Expand Down
1 change: 1 addition & 0 deletions src/js/prologue.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ function PostNatives(utils) {
"ArrayToString",
"AsyncFunctionNext",
"AsyncFunctionThrow",
"FormatDateToParts",
"GetIterator",
"GetMethod",
"GlobalPromise",
Expand Down
Loading

0 comments on commit a3db819

Please sign in to comment.