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

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

Issue 2006673002: Reduce boilerplace for common pattern to return MaybeHandle. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: rebase and fix Created 4 years, 7 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
« no previous file with comments | « src/runtime/runtime-function.cc ('k') | src/runtime/runtime-internal.cc » ('j') | 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 "src/api.h" 9 #include "src/api.h"
10 #include "src/api-natives.h" 10 #include "src/api-natives.h"
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 Handle<Object> value; 375 Handle<Object> value;
376 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, value, Object::ToNumber(date)); 376 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, value, Object::ToNumber(date));
377 377
378 icu::SimpleDateFormat* date_format = 378 icu::SimpleDateFormat* date_format =
379 DateFormat::UnpackDateFormat(isolate, date_format_holder); 379 DateFormat::UnpackDateFormat(isolate, date_format_holder);
380 if (!date_format) return isolate->ThrowIllegalOperation(); 380 if (!date_format) return isolate->ThrowIllegalOperation();
381 381
382 icu::UnicodeString result; 382 icu::UnicodeString result;
383 date_format->format(value->Number(), result); 383 date_format->format(value->Number(), result);
384 384
385 Handle<String> result_str; 385 RETURN_RESULT_OR_FAILURE(
386 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( 386 isolate, isolate->factory()->NewStringFromTwoByte(Vector<const uint16_t>(
387 isolate, result_str, 387 reinterpret_cast<const uint16_t*>(result.getBuffer()),
388 isolate->factory()->NewStringFromTwoByte(Vector<const uint16_t>( 388 result.length())));
389 reinterpret_cast<const uint16_t*>(result.getBuffer()),
390 result.length())));
391 return *result_str;
392 } 389 }
393 390
394 391
395 RUNTIME_FUNCTION(Runtime_InternalDateParse) { 392 RUNTIME_FUNCTION(Runtime_InternalDateParse) {
396 HandleScope scope(isolate); 393 HandleScope scope(isolate);
397 394
398 DCHECK(args.length() == 2); 395 DCHECK(args.length() == 2);
399 396
400 CONVERT_ARG_HANDLE_CHECKED(JSObject, date_format_holder, 0); 397 CONVERT_ARG_HANDLE_CHECKED(JSObject, date_format_holder, 0);
401 CONVERT_ARG_HANDLE_CHECKED(String, date_string, 1); 398 CONVERT_ARG_HANDLE_CHECKED(String, date_string, 1);
402 399
403 v8::String::Utf8Value utf8_date(v8::Utils::ToLocal(date_string)); 400 v8::String::Utf8Value utf8_date(v8::Utils::ToLocal(date_string));
404 icu::UnicodeString u_date(icu::UnicodeString::fromUTF8(*utf8_date)); 401 icu::UnicodeString u_date(icu::UnicodeString::fromUTF8(*utf8_date));
405 icu::SimpleDateFormat* date_format = 402 icu::SimpleDateFormat* date_format =
406 DateFormat::UnpackDateFormat(isolate, date_format_holder); 403 DateFormat::UnpackDateFormat(isolate, date_format_holder);
407 if (!date_format) return isolate->ThrowIllegalOperation(); 404 if (!date_format) return isolate->ThrowIllegalOperation();
408 405
409 UErrorCode status = U_ZERO_ERROR; 406 UErrorCode status = U_ZERO_ERROR;
410 UDate date = date_format->parse(u_date, status); 407 UDate date = date_format->parse(u_date, status);
411 if (U_FAILURE(status)) return isolate->heap()->undefined_value(); 408 if (U_FAILURE(status)) return isolate->heap()->undefined_value();
412 409
413 Handle<JSDate> result; 410 RETURN_RESULT_OR_FAILURE(
414 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( 411 isolate, JSDate::New(isolate->date_function(), isolate->date_function(),
415 isolate, result, 412 static_cast<double>(date)));
416 JSDate::New(isolate->date_function(), isolate->date_function(),
417 static_cast<double>(date)));
418 return *result;
419 } 413 }
420 414
421 415
422 RUNTIME_FUNCTION(Runtime_CreateNumberFormat) { 416 RUNTIME_FUNCTION(Runtime_CreateNumberFormat) {
423 HandleScope scope(isolate); 417 HandleScope scope(isolate);
424 418
425 DCHECK(args.length() == 3); 419 DCHECK(args.length() == 3);
426 420
427 CONVERT_ARG_HANDLE_CHECKED(String, locale, 0); 421 CONVERT_ARG_HANDLE_CHECKED(String, locale, 0);
428 CONVERT_ARG_HANDLE_CHECKED(JSObject, options, 1); 422 CONVERT_ARG_HANDLE_CHECKED(JSObject, options, 1);
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
469 Handle<Object> value; 463 Handle<Object> value;
470 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, value, Object::ToNumber(number)); 464 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, value, Object::ToNumber(number));
471 465
472 icu::DecimalFormat* number_format = 466 icu::DecimalFormat* number_format =
473 NumberFormat::UnpackNumberFormat(isolate, number_format_holder); 467 NumberFormat::UnpackNumberFormat(isolate, number_format_holder);
474 if (!number_format) return isolate->ThrowIllegalOperation(); 468 if (!number_format) return isolate->ThrowIllegalOperation();
475 469
476 icu::UnicodeString result; 470 icu::UnicodeString result;
477 number_format->format(value->Number(), result); 471 number_format->format(value->Number(), result);
478 472
479 Handle<String> result_str; 473 RETURN_RESULT_OR_FAILURE(
480 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( 474 isolate, isolate->factory()->NewStringFromTwoByte(Vector<const uint16_t>(
481 isolate, result_str, 475 reinterpret_cast<const uint16_t*>(result.getBuffer()),
482 isolate->factory()->NewStringFromTwoByte(Vector<const uint16_t>( 476 result.length())));
483 reinterpret_cast<const uint16_t*>(result.getBuffer()),
484 result.length())));
485 return *result_str;
486 } 477 }
487 478
488 479
489 RUNTIME_FUNCTION(Runtime_InternalNumberParse) { 480 RUNTIME_FUNCTION(Runtime_InternalNumberParse) {
490 HandleScope scope(isolate); 481 HandleScope scope(isolate);
491 482
492 DCHECK(args.length() == 2); 483 DCHECK(args.length() == 2);
493 484
494 CONVERT_ARG_HANDLE_CHECKED(JSObject, number_format_holder, 0); 485 CONVERT_ARG_HANDLE_CHECKED(JSObject, number_format_holder, 0);
495 CONVERT_ARG_HANDLE_CHECKED(String, number_string, 1); 486 CONVERT_ARG_HANDLE_CHECKED(String, number_string, 1);
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
640 // Read-only alias of the normalized prefix. 631 // Read-only alias of the normalized prefix.
641 result.setTo(false, input.getBuffer(), normalized_prefix_length); 632 result.setTo(false, input.getBuffer(), normalized_prefix_length);
642 // copy-on-write; normalize the suffix and append to |result|. 633 // copy-on-write; normalize the suffix and append to |result|.
643 normalizer->normalizeSecondAndAppend(result, unnormalized, status); 634 normalizer->normalizeSecondAndAppend(result, unnormalized, status);
644 } 635 }
645 636
646 if (U_FAILURE(status)) { 637 if (U_FAILURE(status)) {
647 return isolate->heap()->undefined_value(); 638 return isolate->heap()->undefined_value();
648 } 639 }
649 640
650 Handle<String> result_str; 641 RETURN_RESULT_OR_FAILURE(
651 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( 642 isolate, isolate->factory()->NewStringFromTwoByte(Vector<const uint16_t>(
652 isolate, result_str, 643 reinterpret_cast<const uint16_t*>(result.getBuffer()),
653 isolate->factory()->NewStringFromTwoByte(Vector<const uint16_t>( 644 result.length())));
654 reinterpret_cast<const uint16_t*>(result.getBuffer()),
655 result.length())));
656 return *result_str;
657 } 645 }
658 646
659 647
660 RUNTIME_FUNCTION(Runtime_CreateBreakIterator) { 648 RUNTIME_FUNCTION(Runtime_CreateBreakIterator) {
661 HandleScope scope(isolate); 649 HandleScope scope(isolate);
662 650
663 DCHECK(args.length() == 3); 651 DCHECK(args.length() == 3);
664 652
665 CONVERT_ARG_HANDLE_CHECKED(String, locale, 0); 653 CONVERT_ARG_HANDLE_CHECKED(String, locale, 0);
666 CONVERT_ARG_HANDLE_CHECKED(JSObject, options, 1); 654 CONVERT_ARG_HANDLE_CHECKED(JSObject, options, 1);
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
841 // semantics) and will be modified to contain the converted result. 829 // semantics) and will be modified to contain the converted result.
842 // Using read-only alias at first saves one copy operation if 830 // Using read-only alias at first saves one copy operation if
843 // transliteration does not change the input, which is rather rare. 831 // transliteration does not change the input, which is rather rare.
844 // Moreover, transliteration takes rather long so that saving one copy 832 // Moreover, transliteration takes rather long so that saving one copy
845 // helps only a little bit. 833 // helps only a little bit.
846 converted.setTo(false, src, src_length); 834 converted.setTo(false, src, src_length);
847 ConvertCaseWithTransliterator(&converted, "el-Upper"); 835 ConvertCaseWithTransliterator(&converted, "el-Upper");
848 // If no change is made, just return |s|. 836 // If no change is made, just return |s|.
849 if (converted.getBuffer() == src) return *s; 837 if (converted.getBuffer() == src) return *s;
850 } 838 }
851 Handle<String> result; 839 RETURN_RESULT_OR_FAILURE(
852 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( 840 isolate,
853 isolate, result,
854 isolate->factory()->NewStringFromTwoByte(Vector<const uint16_t>( 841 isolate->factory()->NewStringFromTwoByte(Vector<const uint16_t>(
855 reinterpret_cast<const uint16_t*>(converted.getBuffer()), 842 reinterpret_cast<const uint16_t*>(converted.getBuffer()),
856 converted.length()))); 843 converted.length())));
857 return *result;
858 } 844 }
859 845
860 auto case_converter = is_to_upper ? u_strToUpper : u_strToLower; 846 auto case_converter = is_to_upper ? u_strToUpper : u_strToLower;
861 847
862 int32_t dest_length = src_length; 848 int32_t dest_length = src_length;
863 UErrorCode status; 849 UErrorCode status;
864 Handle<SeqTwoByteString> result; 850 Handle<SeqTwoByteString> result;
865 base::SmartArrayPointer<uc16> sap; 851 base::SmartArrayPointer<uc16> sap;
866 852
867 // This is not a real loop. It'll be executed only once (no overflow) or 853 // This is not a real loop. It'll be executed only once (no overflow) or
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after
1159 Handle<FixedArray> date_cache_version = 1145 Handle<FixedArray> date_cache_version =
1160 Handle<FixedArray>::cast(isolate->eternal_handles()->GetSingleton( 1146 Handle<FixedArray>::cast(isolate->eternal_handles()->GetSingleton(
1161 EternalHandles::DATE_CACHE_VERSION)); 1147 EternalHandles::DATE_CACHE_VERSION));
1162 return date_cache_version->get(0); 1148 return date_cache_version->get(0);
1163 } 1149 }
1164 1150
1165 } // namespace internal 1151 } // namespace internal
1166 } // namespace v8 1152 } // namespace v8
1167 1153
1168 #endif // V8_I18N_SUPPORT 1154 #endif // V8_I18N_SUPPORT
OLDNEW
« no previous file with comments | « src/runtime/runtime-function.cc ('k') | src/runtime/runtime-internal.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698