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 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: | |
Dan Ehrenberg
2016/08/26 19:07:10
Could you add a comment explaining why this is UNR
| |
442 UNREACHABLE(); | |
443 } | |
444 } | |
445 | |
446 bool AddElement(Handle<JSArray> array, int index, int32_t field_id, | |
447 const icu::UnicodeString& formatted, int32_t begin, int32_t end, | |
448 Isolate* isolate) { | |
449 HandleScope scope(isolate); | |
450 Factory* factory = isolate->factory(); | |
451 Handle<JSObject> element = factory->NewJSObject(isolate->object_function()); | |
452 Handle<String> value = IcuDateFieldIdToDateType(field_id, isolate); | |
453 JSObject::AddProperty(element, factory->type_string(), value, NONE); | |
454 | |
455 icu::UnicodeString field(formatted.tempSubStringBetween(begin, end)); | |
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, factory->value_string(), 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 fp_iter; | |
488 icu::FieldPosition fp; | |
489 UErrorCode status = U_ZERO_ERROR; | |
490 date_format->format(value->Number(), formatted, &fp_iter, status); | |
491 if (U_FAILURE(status)) return isolate->heap()->undefined_value(); | |
492 | |
493 Handle<JSArray> result = factory->NewJSArray(0); | |
494 int index = 0; | |
495 int32_t previous_end_pos = 0; | |
496 while (fp_iter.next(fp)) { | |
497 int32_t begin_pos = fp.getBeginIndex(); | |
498 int32_t end_pos = fp.getEndIndex(); | |
499 | |
500 if (previous_end_pos < begin_pos) { | |
501 if (!AddElement(result, index, -1, formatted, previous_end_pos, begin_pos, | |
502 isolate)) { | |
503 return isolate->heap()->undefined_value(); | |
504 } | |
505 ++index; | |
506 } | |
507 if (!AddElement(result, index, fp.getField(), formatted, begin_pos, end_pos, | |
508 isolate)) { | |
509 return isolate->heap()->undefined_value(); | |
510 } | |
511 previous_end_pos = end_pos; | |
512 ++index; | |
513 } | |
514 int32_t length = formatted.length(); | |
515 if (previous_end_pos < length) { | |
516 if (!AddElement(result, index, -1, formatted, previous_end_pos, length, | |
517 isolate)) { | |
518 return isolate->heap()->undefined_value(); | |
519 } | |
520 } | |
521 JSObject::ValidateElements(result); | |
522 return *result; | |
523 } | |
396 | 524 |
397 RUNTIME_FUNCTION(Runtime_InternalDateParse) { | 525 RUNTIME_FUNCTION(Runtime_InternalDateParse) { |
398 HandleScope scope(isolate); | 526 HandleScope scope(isolate); |
399 | 527 |
400 DCHECK(args.length() == 2); | 528 DCHECK(args.length() == 2); |
401 | 529 |
402 CONVERT_ARG_HANDLE_CHECKED(JSObject, date_format_holder, 0); | 530 CONVERT_ARG_HANDLE_CHECKED(JSObject, date_format_holder, 0); |
403 CONVERT_ARG_HANDLE_CHECKED(String, date_string, 1); | 531 CONVERT_ARG_HANDLE_CHECKED(String, date_string, 1); |
404 | 532 |
405 v8::String::Utf8Value utf8_date(v8::Utils::ToLocal(date_string)); | 533 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 = | 1284 Handle<FixedArray> date_cache_version = |
1157 Handle<FixedArray>::cast(isolate->eternal_handles()->GetSingleton( | 1285 Handle<FixedArray>::cast(isolate->eternal_handles()->GetSingleton( |
1158 EternalHandles::DATE_CACHE_VERSION)); | 1286 EternalHandles::DATE_CACHE_VERSION)); |
1159 return date_cache_version->get(0); | 1287 return date_cache_version->get(0); |
1160 } | 1288 } |
1161 | 1289 |
1162 } // namespace internal | 1290 } // namespace internal |
1163 } // namespace v8 | 1291 } // namespace v8 |
1164 | 1292 |
1165 #endif // V8_I18N_SUPPORT | 1293 #endif // V8_I18N_SUPPORT |
OLD | NEW |