Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 | |
| 401 // https://tc39.github.io/ecma402/#table-datetimeformat-components | |
| 402 // TODO(jshin): Map UDAT.*FIELD to DateTimeFormatPartType enums | |
| 403 // for each of which return NewStringFromStaticChars or 'AtomicString' | |
| 404 // if available. | |
| 405 const char* kIcuDateFieldIdToDateType[] = { | |
| 406 "era", // UDAT_ERA_FIELD = 0, | |
| 407 "year", // UDAT_YEAR_FIELD = 1, | |
| 408 "month", // UDAT_MONTH_FIELD = 2, | |
| 409 "day", // UDAT_DATE_FIELD = 3, | |
| 410 "hour", // UDAT_HOUR_OF_DAY1_FIELD = 4, | |
| 411 "hour", // UDAT_HOUR_OF_DAY0_FIELD = 5, | |
| 412 "minute", // UDAT_MINUTE_FIELD = 6, | |
| 413 "second", // UDAT_SECOND_FIELD = 7, | |
| 414 nullptr, // UDAT_FRACTIONAL_SECOND_FIELD = 8, | |
| 415 "weekday", // UDAT_DAY_OF_WEEK_FIELD = 9, | |
| 416 nullptr, // UDAT_DAY_OF_YEAR_FIELD = 10, | |
| 417 nullptr, // UDAT_DAY_OF_WEEK_IN_MONTH_FIELD = 11, | |
| 418 nullptr, // UDAT_WEEK_OF_YEAR_FIELD = 12, | |
| 419 nullptr, // UDAT_WEEK_OF_MONTH_FIELD = 13, | |
| 420 "dayperiod", // UDAT_AM_PM_FIELD = 14, | |
| 421 "hour", // UDAT_HOUR1_FIELD = 15, | |
| 422 "hour", // UDAT_HOUR0_FIELD = 16, | |
| 423 "timeZoneName", // UDAT_TIMEZONE_FIELD = 17, | |
| 424 nullptr, // UDAT_YEAR_WOY_FIELD = 18, | |
| 425 "weekday", // UDAT_DOW_LOCAL_FIELD = 19, | |
| 426 "year", // UDAT_EXTENDED_YEAR_FIELD = 20, | |
| 427 "day", // UDAT_JULIAN_DAY_FIELD = 21, | |
| 428 nullptr, // UDAT_MILLISECONDS_IN_DAY_FIELD = 22, | |
| 429 "timeZoneName", // UDAT_TIMEZONE_RFC_FIELD = 23, | |
| 430 "timeZoneName", // UDAT_TIMEZONE_GENERIC_FIELD = 24, | |
| 431 "weekday", // UDAT_STANDALONE_DAY_FIELD = 25, | |
| 432 "month", // UDAT_STANDALONE_MONTH_FIELD = 26, | |
| 433 nullptr, // UDAT_QUARTER_FIELD = 27, | |
| 434 nullptr, // UDAT_STANDALONE_QUARTER_FIELD = 28, | |
| 435 "timeZoneName", // UDAT_TIMEZONE_SPECIAL_FIELD = 29, | |
| 436 "year", // UDAT_YEAR_NAME_FIELD = 30, | |
| 437 "timeZoneName", // UDAT_TIMEZONE_LOCALIZED_GMT_OFFSET_FIELD = 31, | |
| 438 "timeZoneName", // UDAT_TIMEZONE_ISO_FIELD = 32, | |
| 439 "timeZoneName", // UDAT_TIMEZONE_ISO_LOCAL_FIELD = 33, | |
| 440 nullptr, // UDAT_RELATED_YEAR_FIELD = 34, | |
| 441 nullptr, // UDAT_TIME_SEPARATOR_FIELD = 35, | |
| 442 nullptr, // UDAT_FIELD_COUNT = 36 | |
| 443 }; | |
| 444 | |
| 445 bool AddElement(Handle<JSArray> array, int index, const char* type, | |
| 446 const icu::UnicodeString& formatted, int32_t begin, int32_t end, | |
| 447 Handle<Name> type_literal, Handle<Name> value_literal, | |
| 448 Isolate* isolate) { | |
|
jungshik at Google
2016/08/24 23:03:35
I guess it can be converted to lambda function if
Dan Ehrenberg
2016/08/25 16:54:45
What do you mean?
| |
| 449 Factory* factory = isolate->factory(); | |
| 450 Handle<JSObject> element = factory->NewJSObject(isolate->object_function()); | |
| 451 Handle<String> value = factory->NewStringFromAsciiChecked(type); | |
| 452 JSObject::AddProperty(element, type_literal, value, NONE); | |
| 453 | |
| 454 icu::UnicodeString field; | |
| 455 formatted.extractBetween(begin, end, field); | |
| 456 ASSIGN_RETURN_ON_EXCEPTION_VALUE( | |
| 457 isolate, value, factory->NewStringFromTwoByte(Vector<const uint16_t>( | |
| 458 reinterpret_cast<const uint16_t*>(field.getBuffer()), | |
| 459 field.length())), | |
| 460 false); | |
| 461 | |
| 462 JSObject::AddProperty(element, value_literal, value, NONE); | |
| 463 RETURN_ON_EXCEPTION_VALUE( | |
| 464 isolate, JSObject::AddDataElement(array, index, element, NONE), false); | |
| 465 return true; | |
| 466 } | |
| 467 | |
| 468 } // namespace | |
| 469 | |
| 470 RUNTIME_FUNCTION(Runtime_InternalDateFormatToParts) { | |
| 471 HandleScope scope(isolate); | |
| 472 Factory* factory = isolate->factory(); | |
| 473 | |
| 474 DCHECK(args.length() == 2); | |
| 475 | |
| 476 CONVERT_ARG_HANDLE_CHECKED(JSObject, date_format_holder, 0); | |
| 477 CONVERT_ARG_HANDLE_CHECKED(JSDate, date, 1); | |
| 478 | |
| 479 Handle<Object> value; | |
| 480 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, value, Object::ToNumber(date)); | |
| 481 | |
| 482 icu::SimpleDateFormat* date_format = | |
| 483 DateFormat::UnpackDateFormat(isolate, date_format_holder); | |
| 484 if (!date_format) return isolate->ThrowIllegalOperation(); | |
| 485 | |
| 486 icu::UnicodeString formatted; | |
| 487 icu::FieldPositionIterator fpIter; | |
| 488 icu::FieldPosition fp; | |
| 489 UErrorCode status = U_ZERO_ERROR; | |
| 490 date_format->format(value->Number(), formatted, &fpIter, status); | |
| 491 if (U_FAILURE(status)) return isolate->heap()->undefined_value(); | |
| 492 | |
| 493 Handle<JSArray> result = factory->NewJSArray(0); | |
| 494 Handle<Name> type_literal = factory->NewStringFromStaticChars("type"); | |
| 495 Handle<Name> value_literal = factory->NewStringFromStaticChars("value"); | |
|
jungshik at Google
2016/08/24 23:03:35
Is there a better way to represent the above two c
Dan Ehrenberg
2016/08/25 16:54:45
Yes, you can add them to the INTERNALIZED_STRING_L
| |
| 496 | |
| 497 int index = 0; | |
| 498 int32_t previous_end_pos = 0; | |
| 499 while (fpIter.next(fp)) { | |
| 500 int32_t fieldId = fp.getField(); | |
| 501 const char* type = kIcuDateFieldIdToDateType[fieldId]; | |
| 502 int32_t begin_pos = fp.getBeginIndex(); | |
| 503 int32_t end_pos = fp.getEndIndex(); | |
| 504 if (type == nullptr) { | |
| 505 previous_end_pos = end_pos; | |
| 506 continue; | |
| 507 } | |
| 508 if (previous_end_pos < begin_pos) { | |
| 509 if (!AddElement(result, index, "literal", formatted, previous_end_pos, | |
| 510 begin_pos, type_literal, value_literal, isolate)) { | |
| 511 return isolate->heap()->undefined_value(); | |
| 512 } | |
| 513 ++index; | |
| 514 } | |
| 515 if (!AddElement(result, index, type, formatted, begin_pos, end_pos, | |
| 516 type_literal, value_literal, isolate)) { | |
| 517 return isolate->heap()->undefined_value(); | |
| 518 } | |
| 519 previous_end_pos = end_pos; | |
| 520 ++index; | |
| 521 } | |
| 522 int32_t length = formatted.length(); | |
| 523 if (previous_end_pos < length) { | |
| 524 if (!AddElement(result, index, "literal", formatted, previous_end_pos, | |
| 525 length, type_literal, value_literal, isolate)) { | |
| 526 return isolate->heap()->undefined_value(); | |
| 527 } | |
| 528 } | |
| 529 JSObject::ValidateElements(result); | |
| 530 return *result; | |
| 531 } | |
| 396 | 532 |
| 397 RUNTIME_FUNCTION(Runtime_InternalDateParse) { | 533 RUNTIME_FUNCTION(Runtime_InternalDateParse) { |
| 398 HandleScope scope(isolate); | 534 HandleScope scope(isolate); |
| 399 | 535 |
| 400 DCHECK(args.length() == 2); | 536 DCHECK(args.length() == 2); |
| 401 | 537 |
| 402 CONVERT_ARG_HANDLE_CHECKED(JSObject, date_format_holder, 0); | 538 CONVERT_ARG_HANDLE_CHECKED(JSObject, date_format_holder, 0); |
| 403 CONVERT_ARG_HANDLE_CHECKED(String, date_string, 1); | 539 CONVERT_ARG_HANDLE_CHECKED(String, date_string, 1); |
| 404 | 540 |
| 405 v8::String::Utf8Value utf8_date(v8::Utils::ToLocal(date_string)); | 541 v8::String::Utf8Value utf8_date(v8::Utils::ToLocal(date_string)); |
| (...skipping 750 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1156 Handle<FixedArray> date_cache_version = | 1292 Handle<FixedArray> date_cache_version = |
| 1157 Handle<FixedArray>::cast(isolate->eternal_handles()->GetSingleton( | 1293 Handle<FixedArray>::cast(isolate->eternal_handles()->GetSingleton( |
| 1158 EternalHandles::DATE_CACHE_VERSION)); | 1294 EternalHandles::DATE_CACHE_VERSION)); |
| 1159 return date_cache_version->get(0); | 1295 return date_cache_version->get(0); |
| 1160 } | 1296 } |
| 1161 | 1297 |
| 1162 } // namespace internal | 1298 } // namespace internal |
| 1163 } // namespace v8 | 1299 } // namespace v8 |
| 1164 | 1300 |
| 1165 #endif // V8_I18N_SUPPORT | 1301 #endif // V8_I18N_SUPPORT |
| OLD | NEW |