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

Side by Side Diff: src/runtime.cc

Issue 21499003: Move helper methods from i18n extension into runtime. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 7 years, 4 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 | Annotate | Revision Log
« no previous file with comments | « src/runtime.h ('k') | tools/gyp/v8.gyp » ('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 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 #include "runtime.h" 59 #include "runtime.h"
60 #include "scopeinfo.h" 60 #include "scopeinfo.h"
61 #include "smart-pointers.h" 61 #include "smart-pointers.h"
62 #include "string-search.h" 62 #include "string-search.h"
63 #include "stub-cache.h" 63 #include "stub-cache.h"
64 #include "uri.h" 64 #include "uri.h"
65 #include "v8conversions.h" 65 #include "v8conversions.h"
66 #include "v8threads.h" 66 #include "v8threads.h"
67 #include "vm-state-inl.h" 67 #include "vm-state-inl.h"
68 68
69 #ifdef V8_I18N_SUPPORT
70 #include "unicode/brkiter.h"
71 #include "unicode/coll.h"
72 #include "unicode/datefmt.h"
73 #include "unicode/numfmt.h"
74 #include "unicode/uloc.h"
75 #include "unicode/uversion.h"
76 #endif
77
69 #ifndef _STLP_VENDOR_CSTD 78 #ifndef _STLP_VENDOR_CSTD
70 // STLPort doesn't import fpclassify and isless into the std namespace. 79 // STLPort doesn't import fpclassify and isless into the std namespace.
71 using std::fpclassify; 80 using std::fpclassify;
72 using std::isless; 81 using std::isless;
73 #endif 82 #endif
74 83
75 namespace v8 { 84 namespace v8 {
76 namespace internal { 85 namespace internal {
77 86
78 87
(...skipping 13276 matching lines...) Expand 10 before | Expand all | Expand 10 after
13355 int usage = static_cast<int>(isolate->heap()->SizeOfObjects()); 13364 int usage = static_cast<int>(isolate->heap()->SizeOfObjects());
13356 if (!Smi::IsValid(usage)) { 13365 if (!Smi::IsValid(usage)) {
13357 return *isolate->factory()->NewNumberFromInt(usage); 13366 return *isolate->factory()->NewNumberFromInt(usage);
13358 } 13367 }
13359 return Smi::FromInt(usage); 13368 return Smi::FromInt(usage);
13360 } 13369 }
13361 13370
13362 #endif // ENABLE_DEBUGGER_SUPPORT 13371 #endif // ENABLE_DEBUGGER_SUPPORT
13363 13372
13364 13373
13374 #ifdef V8_I18N_SUPPORT
13375 RUNTIME_FUNCTION(MaybeObject*, Runtime_CanonicalizeLanguageTag) {
13376 HandleScope scope(isolate);
13377
Michael Starzinger 2013/08/01 09:58:20 nit: Can we add asserts that check the number of a
13378 CONVERT_ARG_HANDLE_CHECKED(String, locale_id_str, 0);
13379
13380 v8::String::Utf8Value locale_id(v8::Utils::ToLocal(locale_id_str));
13381
13382 // Return value which denotes invalid language tag.
13383 const char* const kInvalidTag = "invalid-tag";
13384
13385 UErrorCode error = U_ZERO_ERROR;
13386 char icu_result[ULOC_FULLNAME_CAPACITY];
13387 int icu_length = 0;
13388
13389 uloc_forLanguageTag(*locale_id, icu_result, ULOC_FULLNAME_CAPACITY,
13390 &icu_length, &error);
13391 if (U_FAILURE(error) || icu_length == 0) {
13392 return isolate->heap()->AllocateStringFromOneByte(CStrVector(kInvalidTag));
13393 }
13394
13395 char result[ULOC_FULLNAME_CAPACITY];
13396
13397 // Force strict BCP47 rules.
13398 uloc_toLanguageTag(icu_result, result, ULOC_FULLNAME_CAPACITY, TRUE, &error);
13399
13400 if (U_FAILURE(error)) {
13401 return isolate->heap()->AllocateStringFromOneByte(CStrVector(kInvalidTag));
13402 }
13403
13404 return isolate->heap()->AllocateStringFromOneByte(CStrVector(result));
13405 }
13406
13407
13408 RUNTIME_FUNCTION(MaybeObject*, Runtime_AvailableLocalesOf) {
13409 HandleScope scope(isolate);
13410
Michael Starzinger 2013/08/01 09:58:20 Likewise.
13411 CONVERT_ARG_HANDLE_CHECKED(String, service, 0);
13412
13413 const icu::Locale* available_locales = NULL;
13414 int32_t count = 0;
13415
13416 if (service->IsUtf8EqualTo(CStrVector("collator"))) {
13417 available_locales = icu::Collator::getAvailableLocales(count);
13418 } else if (service->IsUtf8EqualTo(CStrVector("numberformat"))) {
13419 available_locales = icu::NumberFormat::getAvailableLocales(count);
13420 } else if (service->IsUtf8EqualTo(CStrVector("dateformat"))) {
13421 available_locales = icu::DateFormat::getAvailableLocales(count);
13422 } else if (service->IsUtf8EqualTo(CStrVector("breakiterator"))) {
13423 available_locales = icu::BreakIterator::getAvailableLocales(count);
13424 }
13425
13426 UErrorCode error = U_ZERO_ERROR;
13427 char result[ULOC_FULLNAME_CAPACITY];
13428 Handle<JSObject> locales =
13429 isolate->factory()->NewJSObject(isolate->object_function());
13430
13431 for (int32_t i = 0; i < count; ++i) {
13432 const char* icu_name = available_locales[i].getName();
13433
13434 error = U_ZERO_ERROR;
13435 // No need to force strict BCP47 rules.
13436 uloc_toLanguageTag(icu_name, result, ULOC_FULLNAME_CAPACITY, FALSE, &error);
13437 if (U_FAILURE(error)) {
13438 // This shouldn't happen, but lets not break the user.
13439 continue;
13440 }
13441
13442 RETURN_IF_EMPTY_HANDLE(isolate,
13443 JSObject::SetLocalPropertyIgnoreAttributes(
13444 locales,
13445 isolate->factory()->NewStringFromAscii(CStrVector(result)),
13446 isolate->factory()->NewNumber(i),
13447 NONE));
13448 }
13449
13450 return *locales;
13451 }
13452
13453
13454 RUNTIME_FUNCTION(MaybeObject*, Runtime_GetDefaultICULocale) {
13455 SealHandleScope shs(isolate);
13456
Michael Starzinger 2013/08/01 09:58:20 Likewise.
13457 icu::Locale default_locale;
13458
13459 // Set the locale
13460 char result[ULOC_FULLNAME_CAPACITY];
13461 UErrorCode status = U_ZERO_ERROR;
13462 uloc_toLanguageTag(
13463 default_locale.getName(), result, ULOC_FULLNAME_CAPACITY, FALSE, &status);
13464 if (U_SUCCESS(status)) {
13465 return isolate->heap()->AllocateStringFromOneByte(CStrVector(result));
13466 }
13467
13468 return isolate->heap()->AllocateStringFromOneByte(CStrVector("und"));
13469 }
13470
13471
13472 RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLanguageTagVariants) {
13473 HandleScope scope(isolate);
13474
Michael Starzinger 2013/08/01 09:58:20 Likewise.
13475 CONVERT_ARG_HANDLE_CHECKED(JSArray, input, 0);
13476
13477 uint32_t length = static_cast<uint32_t>(input->length()->Number());
13478 Handle<FixedArray> output = isolate->factory()->NewFixedArray(length);
13479 Handle<Name> maximized =
13480 isolate->factory()->NewStringFromAscii(CStrVector("maximized"));
13481 Handle<Name> base =
13482 isolate->factory()->NewStringFromAscii(CStrVector("base"));
13483 for (unsigned int i = 0; i < length; ++i) {
13484 MaybeObject* maybe_string = input->GetElement(i);
13485 Object* locale_id;
13486 if (!maybe_string->ToObject(&locale_id) || !locale_id->IsString()) {
13487 return isolate->Throw(isolate->heap()->illegal_argument_string());
13488 }
13489
13490 v8::String::Utf8Value utf8_locale_id(
13491 v8::Utils::ToLocal(Handle<String>(String::cast(locale_id))));
13492
13493 UErrorCode error = U_ZERO_ERROR;
13494
13495 // Convert from BCP47 to ICU format.
13496 // de-DE-u-co-phonebk -> de_DE@collation=phonebook
13497 char icu_locale[ULOC_FULLNAME_CAPACITY];
13498 int icu_locale_length = 0;
13499 uloc_forLanguageTag(*utf8_locale_id, icu_locale, ULOC_FULLNAME_CAPACITY,
13500 &icu_locale_length, &error);
13501 if (U_FAILURE(error) || icu_locale_length == 0) {
13502 return isolate->Throw(isolate->heap()->illegal_argument_string());
13503 }
13504
13505 // Maximize the locale.
13506 // de_DE@collation=phonebook -> de_Latn_DE@collation=phonebook
13507 char icu_max_locale[ULOC_FULLNAME_CAPACITY];
13508 uloc_addLikelySubtags(
13509 icu_locale, icu_max_locale, ULOC_FULLNAME_CAPACITY, &error);
13510
13511 // Remove extensions from maximized locale.
13512 // de_Latn_DE@collation=phonebook -> de_Latn_DE
13513 char icu_base_max_locale[ULOC_FULLNAME_CAPACITY];
13514 uloc_getBaseName(
13515 icu_max_locale, icu_base_max_locale, ULOC_FULLNAME_CAPACITY, &error);
13516
13517 // Get original name without extensions.
13518 // de_DE@collation=phonebook -> de_DE
13519 char icu_base_locale[ULOC_FULLNAME_CAPACITY];
13520 uloc_getBaseName(
13521 icu_locale, icu_base_locale, ULOC_FULLNAME_CAPACITY, &error);
13522
13523 // Convert from ICU locale format to BCP47 format.
13524 // de_Latn_DE -> de-Latn-DE
13525 char base_max_locale[ULOC_FULLNAME_CAPACITY];
13526 uloc_toLanguageTag(icu_base_max_locale, base_max_locale,
13527 ULOC_FULLNAME_CAPACITY, FALSE, &error);
13528
13529 // de_DE -> de-DE
13530 char base_locale[ULOC_FULLNAME_CAPACITY];
13531 uloc_toLanguageTag(
13532 icu_base_locale, base_locale, ULOC_FULLNAME_CAPACITY, FALSE, &error);
13533
13534 if (U_FAILURE(error)) {
13535 return isolate->Throw(isolate->heap()->illegal_argument_string());
13536 }
13537
13538 Handle<JSObject> result =
13539 isolate->factory()->NewJSObject(isolate->object_function());
13540 RETURN_IF_EMPTY_HANDLE(isolate,
13541 JSObject::SetLocalPropertyIgnoreAttributes(
13542 result,
13543 maximized,
13544 isolate->factory()->NewStringFromAscii(CStrVector(base_max_locale)),
13545 NONE));
13546 RETURN_IF_EMPTY_HANDLE(isolate,
13547 JSObject::SetLocalPropertyIgnoreAttributes(
13548 result,
13549 base,
13550 isolate->factory()->NewStringFromAscii(CStrVector(base_locale)),
13551 NONE));
13552 output->set(i, *result);
13553 }
13554
13555 Handle<JSArray> result = isolate->factory()->NewJSArrayWithElements(output);
13556 result->set_length(Smi::FromInt(length));
13557 return *result;
13558 }
13559 #endif // V8_I18N_SUPPORT
13560
13561
13365 // Finds the script object from the script data. NOTE: This operation uses 13562 // Finds the script object from the script data. NOTE: This operation uses
13366 // heap traversal to find the function generated for the source position 13563 // heap traversal to find the function generated for the source position
13367 // for the requested break point. For lazily compiled functions several heap 13564 // for the requested break point. For lazily compiled functions several heap
13368 // traversals might be required rendering this operation as a rather slow 13565 // traversals might be required rendering this operation as a rather slow
13369 // operation. However for setting break points which is normally done through 13566 // operation. However for setting break points which is normally done through
13370 // some kind of user interaction the performance is not crucial. 13567 // some kind of user interaction the performance is not crucial.
13371 static Handle<Object> Runtime_GetScriptFromScriptName( 13568 static Handle<Object> Runtime_GetScriptFromScriptName(
13372 Handle<String> script_name) { 13569 Handle<String> script_name) {
13373 // Scan the heap for Script objects to find the script with the requested 13570 // Scan the heap for Script objects to find the script with the requested
13374 // script data. 13571 // script data.
(...skipping 578 matching lines...) Expand 10 before | Expand all | Expand 10 after
13953 // Handle last resort GC and make sure to allow future allocations 14150 // Handle last resort GC and make sure to allow future allocations
13954 // to grow the heap without causing GCs (if possible). 14151 // to grow the heap without causing GCs (if possible).
13955 isolate->counters()->gc_last_resort_from_js()->Increment(); 14152 isolate->counters()->gc_last_resort_from_js()->Increment();
13956 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, 14153 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags,
13957 "Runtime::PerformGC"); 14154 "Runtime::PerformGC");
13958 } 14155 }
13959 } 14156 }
13960 14157
13961 14158
13962 } } // namespace v8::internal 14159 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/runtime.h ('k') | tools/gyp/v8.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698