Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(187)

Side by Side Diff: src/runtime/runtime-i18n.cc

Issue 2273953003: Add support for DateTimeFormat.formatToParts (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: explain why UDAT_*FILEDS cannot show up Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« src/js/i18n.js ('K') | « src/runtime/runtime.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 the V8 project authors. All rights reserved. 1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 5
6 #ifdef V8_I18N_SUPPORT 6 #ifdef V8_I18N_SUPPORT
7 #include "src/runtime/runtime-utils.h" 7 #include "src/runtime/runtime-utils.h"
8 8
9 #include <memory> 9 #include <memory>
10 10
11 #include "src/api.h" 11 #include "src/api.h"
12 #include "src/api-natives.h" 12 #include "src/api-natives.h"
13 #include "src/arguments.h" 13 #include "src/arguments.h"
14 #include "src/factory.h" 14 #include "src/factory.h"
15 #include "src/i18n.h" 15 #include "src/i18n.h"
16 #include "src/isolate-inl.h" 16 #include "src/isolate-inl.h"
17 #include "src/messages.h" 17 #include "src/messages.h"
18 18
19 #include "unicode/brkiter.h" 19 #include "unicode/brkiter.h"
20 #include "unicode/calendar.h" 20 #include "unicode/calendar.h"
21 #include "unicode/coll.h" 21 #include "unicode/coll.h"
22 #include "unicode/curramt.h" 22 #include "unicode/curramt.h"
23 #include "unicode/datefmt.h" 23 #include "unicode/datefmt.h"
24 #include "unicode/dcfmtsym.h" 24 #include "unicode/dcfmtsym.h"
25 #include "unicode/decimfmt.h" 25 #include "unicode/decimfmt.h"
26 #include "unicode/dtfmtsym.h" 26 #include "unicode/dtfmtsym.h"
27 #include "unicode/dtptngen.h" 27 #include "unicode/dtptngen.h"
28 #include "unicode/fieldpos.h"
29 #include "unicode/fpositer.h"
28 #include "unicode/locid.h" 30 #include "unicode/locid.h"
29 #include "unicode/normalizer2.h" 31 #include "unicode/normalizer2.h"
30 #include "unicode/numfmt.h" 32 #include "unicode/numfmt.h"
31 #include "unicode/numsys.h" 33 #include "unicode/numsys.h"
32 #include "unicode/rbbi.h" 34 #include "unicode/rbbi.h"
33 #include "unicode/smpdtfmt.h" 35 #include "unicode/smpdtfmt.h"
34 #include "unicode/timezone.h" 36 #include "unicode/timezone.h"
35 #include "unicode/translit.h" 37 #include "unicode/translit.h"
36 #include "unicode/uchar.h" 38 #include "unicode/uchar.h"
37 #include "unicode/ucol.h" 39 #include "unicode/ucol.h"
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 388
387 icu::UnicodeString result; 389 icu::UnicodeString result;
388 date_format->format(value->Number(), result); 390 date_format->format(value->Number(), result);
389 391
390 RETURN_RESULT_OR_FAILURE( 392 RETURN_RESULT_OR_FAILURE(
391 isolate, isolate->factory()->NewStringFromTwoByte(Vector<const uint16_t>( 393 isolate, isolate->factory()->NewStringFromTwoByte(Vector<const uint16_t>(
392 reinterpret_cast<const uint16_t*>(result.getBuffer()), 394 reinterpret_cast<const uint16_t*>(result.getBuffer()),
393 result.length()))); 395 result.length())));
394 } 396 }
395 397
398 namespace {
399 // The list comes from third_party/icu/source/i18n/unicode/udat.h.
400 // They're mapped to DateTimeFormat components listed at
401 // https://tc39.github.io/ecma402/#sec-datetimeformat-abstracts .
402
403 Handle<String> IcuDateFieldIdToDateType(int32_t field_id, Isolate* isolate) {
404 switch (field_id) {
405 case -1:
406 return isolate->factory()->literal_string();
407 case UDAT_YEAR_FIELD:
408 case UDAT_EXTENDED_YEAR_FIELD:
409 case UDAT_YEAR_NAME_FIELD:
410 return isolate->factory()->year_string();
411 case UDAT_MONTH_FIELD:
412 case UDAT_STANDALONE_MONTH_FIELD:
413 return isolate->factory()->month_string();
414 case UDAT_DATE_FIELD:
415 return isolate->factory()->day_string();
416 case UDAT_HOUR_OF_DAY1_FIELD:
417 case UDAT_HOUR_OF_DAY0_FIELD:
418 case UDAT_HOUR1_FIELD:
419 case UDAT_HOUR0_FIELD:
420 return isolate->factory()->hour_string();
421 case UDAT_MINUTE_FIELD:
422 return isolate->factory()->minute_string();
423 case UDAT_SECOND_FIELD:
424 return isolate->factory()->second_string();
425 case UDAT_DAY_OF_WEEK_FIELD:
426 case UDAT_DOW_LOCAL_FIELD:
427 case UDAT_STANDALONE_DAY_FIELD:
428 return isolate->factory()->weekday_string();
429 case UDAT_AM_PM_FIELD:
430 return isolate->factory()->dayperiod_string();
431 case UDAT_TIMEZONE_FIELD:
432 case UDAT_TIMEZONE_RFC_FIELD:
433 case UDAT_TIMEZONE_GENERIC_FIELD:
434 case UDAT_TIMEZONE_SPECIAL_FIELD:
435 case UDAT_TIMEZONE_LOCALIZED_GMT_OFFSET_FIELD:
436 case UDAT_TIMEZONE_ISO_FIELD:
437 case UDAT_TIMEZONE_ISO_LOCAL_FIELD:
438 return isolate->factory()->timeZoneName_string();
439 case UDAT_ERA_FIELD:
440 return isolate->factory()->era_string();
441 default:
442 // Other UDAT_*_FIELD's cannot show up because there is no way to specify
443 // them via options of Intl.DateTimeFormat.
444 UNREACHABLE();
445 }
446 }
447
448 bool AddElement(Handle<JSArray> array, int index, int32_t field_id,
449 const icu::UnicodeString& formatted, int32_t begin, int32_t end,
450 Isolate* isolate) {
451 HandleScope scope(isolate);
452 Factory* factory = isolate->factory();
453 Handle<JSObject> element = factory->NewJSObject(isolate->object_function());
454 Handle<String> value = IcuDateFieldIdToDateType(field_id, isolate);
455 JSObject::AddProperty(element, factory->type_string(), value, NONE);
456
457 icu::UnicodeString field(formatted.tempSubStringBetween(begin, end));
458 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
459 isolate, value, factory->NewStringFromTwoByte(Vector<const uint16_t>(
460 reinterpret_cast<const uint16_t*>(field.getBuffer()),
461 field.length())),
462 false);
463
464 JSObject::AddProperty(element, factory->value_string(), value, NONE);
465 RETURN_ON_EXCEPTION_VALUE(
466 isolate, JSObject::AddDataElement(array, index, element, NONE), false);
467 return true;
468 }
469
470 } // namespace
471
472 RUNTIME_FUNCTION(Runtime_InternalDateFormatToParts) {
473 HandleScope scope(isolate);
474 Factory* factory = isolate->factory();
475
476 DCHECK(args.length() == 2);
477
478 CONVERT_ARG_HANDLE_CHECKED(JSObject, date_format_holder, 0);
479 CONVERT_ARG_HANDLE_CHECKED(JSDate, date, 1);
480
481 Handle<Object> value;
482 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, value, Object::ToNumber(date));
483
484 icu::SimpleDateFormat* date_format =
485 DateFormat::UnpackDateFormat(isolate, date_format_holder);
486 if (!date_format) return isolate->ThrowIllegalOperation();
487
488 icu::UnicodeString formatted;
489 icu::FieldPositionIterator fp_iter;
490 icu::FieldPosition fp;
491 UErrorCode status = U_ZERO_ERROR;
492 date_format->format(value->Number(), formatted, &fp_iter, status);
493 if (U_FAILURE(status)) return isolate->heap()->undefined_value();
494
495 Handle<JSArray> result = factory->NewJSArray(0);
496 int index = 0;
497 int32_t previous_end_pos = 0;
498 while (fp_iter.next(fp)) {
499 int32_t begin_pos = fp.getBeginIndex();
500 int32_t end_pos = fp.getEndIndex();
501
502 if (previous_end_pos < begin_pos) {
503 if (!AddElement(result, index, -1, formatted, previous_end_pos, begin_pos,
504 isolate)) {
505 return isolate->heap()->undefined_value();
506 }
507 ++index;
508 }
509 if (!AddElement(result, index, fp.getField(), formatted, begin_pos, end_pos,
510 isolate)) {
511 return isolate->heap()->undefined_value();
512 }
513 previous_end_pos = end_pos;
514 ++index;
515 }
516 int32_t length = formatted.length();
517 if (previous_end_pos < length) {
518 if (!AddElement(result, index, -1, formatted, previous_end_pos, length,
519 isolate)) {
520 return isolate->heap()->undefined_value();
521 }
522 }
523 JSObject::ValidateElements(result);
524 return *result;
525 }
396 526
397 RUNTIME_FUNCTION(Runtime_InternalDateParse) { 527 RUNTIME_FUNCTION(Runtime_InternalDateParse) {
398 HandleScope scope(isolate); 528 HandleScope scope(isolate);
399 529
400 DCHECK(args.length() == 2); 530 DCHECK(args.length() == 2);
401 531
402 CONVERT_ARG_HANDLE_CHECKED(JSObject, date_format_holder, 0); 532 CONVERT_ARG_HANDLE_CHECKED(JSObject, date_format_holder, 0);
403 CONVERT_ARG_HANDLE_CHECKED(String, date_string, 1); 533 CONVERT_ARG_HANDLE_CHECKED(String, date_string, 1);
404 534
405 v8::String::Utf8Value utf8_date(v8::Utils::ToLocal(date_string)); 535 v8::String::Utf8Value utf8_date(v8::Utils::ToLocal(date_string));
(...skipping 750 matching lines...) Expand 10 before | Expand all | Expand 10 after
1156 Handle<FixedArray> date_cache_version = 1286 Handle<FixedArray> date_cache_version =
1157 Handle<FixedArray>::cast(isolate->eternal_handles()->GetSingleton( 1287 Handle<FixedArray>::cast(isolate->eternal_handles()->GetSingleton(
1158 EternalHandles::DATE_CACHE_VERSION)); 1288 EternalHandles::DATE_CACHE_VERSION));
1159 return date_cache_version->get(0); 1289 return date_cache_version->get(0);
1160 } 1290 }
1161 1291
1162 } // namespace internal 1292 } // namespace internal
1163 } // namespace v8 1293 } // namespace v8
1164 1294
1165 #endif // V8_I18N_SUPPORT 1295 #endif // V8_I18N_SUPPORT
OLDNEW
« src/js/i18n.js ('K') | « src/runtime/runtime.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698