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 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 if (!input->IsJSObject()) { | 317 if (!input->IsJSObject()) { |
316 THROW_NEW_ERROR_RETURN_FAILURE( | 318 THROW_NEW_ERROR_RETURN_FAILURE( |
317 isolate, NewTypeError(MessageTemplate::kNotIntlObject, input)); | 319 isolate, NewTypeError(MessageTemplate::kNotIntlObject, input)); |
318 } | 320 } |
319 | 321 |
320 Handle<JSObject> obj = Handle<JSObject>::cast(input); | 322 Handle<JSObject> obj = Handle<JSObject>::cast(input); |
321 | 323 |
322 Handle<Symbol> marker = isolate->factory()->intl_impl_object_symbol(); | 324 Handle<Symbol> marker = isolate->factory()->intl_impl_object_symbol(); |
323 | 325 |
324 Handle<Object> impl = JSReceiver::GetDataProperty(obj, marker); | 326 Handle<Object> impl = JSReceiver::GetDataProperty(obj, marker); |
325 if (impl->IsTheHole(isolate)) { | 327 if (!impl->IsJSObject()) { |
326 THROW_NEW_ERROR_RETURN_FAILURE( | 328 THROW_NEW_ERROR_RETURN_FAILURE( |
327 isolate, NewTypeError(MessageTemplate::kNotIntlObject, obj)); | 329 isolate, NewTypeError(MessageTemplate::kNotIntlObject, obj)); |
328 } | 330 } |
329 return *impl; | 331 return *impl; |
330 } | 332 } |
331 | 333 |
332 | 334 |
333 RUNTIME_FUNCTION(Runtime_CreateDateTimeFormat) { | 335 RUNTIME_FUNCTION(Runtime_CreateDateTimeFormat) { |
334 HandleScope scope(isolate); | 336 HandleScope scope(isolate); |
335 | 337 |
(...skipping 50 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 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 // To prevent MSVC from issuing C4715 warning. |
| 446 return Handle<String>(); |
| 447 } |
| 448 } |
| 449 |
| 450 bool AddElement(Handle<JSArray> array, int index, int32_t field_id, |
| 451 const icu::UnicodeString& formatted, int32_t begin, int32_t end, |
| 452 Isolate* isolate) { |
| 453 HandleScope scope(isolate); |
| 454 Factory* factory = isolate->factory(); |
| 455 Handle<JSObject> element = factory->NewJSObject(isolate->object_function()); |
| 456 Handle<String> value = IcuDateFieldIdToDateType(field_id, isolate); |
| 457 JSObject::AddProperty(element, factory->type_string(), value, NONE); |
| 458 |
| 459 icu::UnicodeString field(formatted.tempSubStringBetween(begin, end)); |
| 460 ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 461 isolate, value, factory->NewStringFromTwoByte(Vector<const uint16_t>( |
| 462 reinterpret_cast<const uint16_t*>(field.getBuffer()), |
| 463 field.length())), |
| 464 false); |
| 465 |
| 466 JSObject::AddProperty(element, factory->value_string(), value, NONE); |
| 467 RETURN_ON_EXCEPTION_VALUE( |
| 468 isolate, JSObject::AddDataElement(array, index, element, NONE), false); |
| 469 return true; |
| 470 } |
| 471 |
| 472 } // namespace |
| 473 |
| 474 RUNTIME_FUNCTION(Runtime_InternalDateFormatToParts) { |
| 475 HandleScope scope(isolate); |
| 476 Factory* factory = isolate->factory(); |
| 477 |
| 478 DCHECK(args.length() == 2); |
| 479 |
| 480 CONVERT_ARG_HANDLE_CHECKED(JSObject, date_format_holder, 0); |
| 481 CONVERT_ARG_HANDLE_CHECKED(JSDate, date, 1); |
| 482 |
| 483 Handle<Object> value; |
| 484 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, value, Object::ToNumber(date)); |
| 485 |
| 486 icu::SimpleDateFormat* date_format = |
| 487 DateFormat::UnpackDateFormat(isolate, date_format_holder); |
| 488 if (!date_format) return isolate->ThrowIllegalOperation(); |
| 489 |
| 490 icu::UnicodeString formatted; |
| 491 icu::FieldPositionIterator fp_iter; |
| 492 icu::FieldPosition fp; |
| 493 UErrorCode status = U_ZERO_ERROR; |
| 494 date_format->format(value->Number(), formatted, &fp_iter, status); |
| 495 if (U_FAILURE(status)) return isolate->heap()->undefined_value(); |
| 496 |
| 497 Handle<JSArray> result = factory->NewJSArray(0); |
| 498 int32_t length = formatted.length(); |
| 499 if (length == 0) return *result; |
| 500 |
| 501 int index = 0; |
| 502 int32_t previous_end_pos = 0; |
| 503 while (fp_iter.next(fp)) { |
| 504 int32_t begin_pos = fp.getBeginIndex(); |
| 505 int32_t end_pos = fp.getEndIndex(); |
| 506 |
| 507 if (previous_end_pos < begin_pos) { |
| 508 if (!AddElement(result, index, -1, formatted, previous_end_pos, begin_pos, |
| 509 isolate)) { |
| 510 return isolate->heap()->undefined_value(); |
| 511 } |
| 512 ++index; |
| 513 } |
| 514 if (!AddElement(result, index, fp.getField(), formatted, begin_pos, end_pos, |
| 515 isolate)) { |
| 516 return isolate->heap()->undefined_value(); |
| 517 } |
| 518 previous_end_pos = end_pos; |
| 519 ++index; |
| 520 } |
| 521 if (previous_end_pos < length) { |
| 522 if (!AddElement(result, index, -1, formatted, previous_end_pos, length, |
| 523 isolate)) { |
| 524 return isolate->heap()->undefined_value(); |
| 525 } |
| 526 } |
| 527 JSObject::ValidateElements(result); |
| 528 return *result; |
| 529 } |
396 | 530 |
397 RUNTIME_FUNCTION(Runtime_InternalDateParse) { | 531 RUNTIME_FUNCTION(Runtime_InternalDateParse) { |
398 HandleScope scope(isolate); | 532 HandleScope scope(isolate); |
399 | 533 |
400 DCHECK(args.length() == 2); | 534 DCHECK(args.length() == 2); |
401 | 535 |
402 CONVERT_ARG_HANDLE_CHECKED(JSObject, date_format_holder, 0); | 536 CONVERT_ARG_HANDLE_CHECKED(JSObject, date_format_holder, 0); |
403 CONVERT_ARG_HANDLE_CHECKED(String, date_string, 1); | 537 CONVERT_ARG_HANDLE_CHECKED(String, date_string, 1); |
404 | 538 |
405 v8::String::Utf8Value utf8_date(v8::Utils::ToLocal(date_string)); | 539 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 = | 1290 Handle<FixedArray> date_cache_version = |
1157 Handle<FixedArray>::cast(isolate->eternal_handles()->GetSingleton( | 1291 Handle<FixedArray>::cast(isolate->eternal_handles()->GetSingleton( |
1158 EternalHandles::DATE_CACHE_VERSION)); | 1292 EternalHandles::DATE_CACHE_VERSION)); |
1159 return date_cache_version->get(0); | 1293 return date_cache_version->get(0); |
1160 } | 1294 } |
1161 | 1295 |
1162 } // namespace internal | 1296 } // namespace internal |
1163 } // namespace v8 | 1297 } // namespace v8 |
1164 | 1298 |
1165 #endif // V8_I18N_SUPPORT | 1299 #endif // V8_I18N_SUPPORT |
OLD | NEW |