runtime-intl.cc 2.84 KB
Newer Older
1 2 3 4
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7
#ifndef V8_INTL_SUPPORT
#error Internationalization is expected to be enabled.
#endif  // V8_INTL_SUPPORT
8

9
#include <cmath>
10 11
#include <memory>

12
#include "src/api-inl.h"
13
#include "src/api-natives.h"
14
#include "src/arguments-inl.h"
15
#include "src/counters.h"
16
#include "src/date.h"
17
#include "src/global-handles.h"
18
#include "src/heap/factory.h"
19
#include "src/isolate-inl.h"
20
#include "src/objects/intl-objects.h"
21
#include "src/objects/js-array-inl.h"
22
#include "src/objects/js-collator-inl.h"
23
#include "src/objects/js-date-time-format-inl.h"
24 25
#include "src/objects/js-list-format-inl.h"
#include "src/objects/js-list-format.h"
26
#include "src/objects/js-number-format-inl.h"
27
#include "src/objects/js-plural-rules-inl.h"
28
#include "src/objects/managed.h"
29
#include "src/runtime/runtime-utils.h"
30
#include "src/utils.h"
31 32 33 34

namespace v8 {
namespace internal {

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
// ecma402 #sec-formatlist
RUNTIME_FUNCTION(Runtime_FormatList) {
  HandleScope scope(isolate);
  DCHECK_EQ(2, args.length());
  CONVERT_ARG_HANDLE_CHECKED(JSListFormat, list_format, 0);
  CONVERT_ARG_HANDLE_CHECKED(JSArray, list, 1);
  RETURN_RESULT_OR_FAILURE(
      isolate, JSListFormat::FormatList(isolate, list_format, list));
}

// ecma402 #sec-formatlisttoparts
RUNTIME_FUNCTION(Runtime_FormatListToParts) {
  HandleScope scope(isolate);
  DCHECK_EQ(2, args.length());
  CONVERT_ARG_HANDLE_CHECKED(JSListFormat, list_format, 0);
  CONVERT_ARG_HANDLE_CHECKED(JSArray, list, 1);
  RETURN_RESULT_OR_FAILURE(
      isolate, JSListFormat::FormatListToParts(isolate, list_format, list));
}

55
// ECMA 402 6.2.3
56 57
RUNTIME_FUNCTION(Runtime_CanonicalizeLanguageTag) {
  HandleScope scope(isolate);
58

59
  DCHECK_EQ(1, args.length());
60
  CONVERT_ARG_HANDLE_CHECKED(Object, locale, 0);
61

62 63 64 65 66
  std::string canonicalized;
  if (!Intl::CanonicalizeLanguageTag(isolate, locale).To(&canonicalized)) {
    return ReadOnlyRoots(isolate).exception();
  }
  return *isolate->factory()->NewStringFromAsciiChecked(canonicalized.c_str());
67 68 69 70 71
}

RUNTIME_FUNCTION(Runtime_GetDefaultICULocale) {
  HandleScope scope(isolate);

72
  DCHECK_EQ(0, args.length());
73 74
  return *isolate->factory()->NewStringFromAsciiChecked(
      Intl::DefaultLocale(isolate).c_str());
75 76
}

77
RUNTIME_FUNCTION(Runtime_StringToLowerCaseIntl) {
78 79 80
  HandleScope scope(isolate);
  DCHECK_EQ(args.length(), 1);
  CONVERT_ARG_HANDLE_CHECKED(String, s, 0);
81
  s = String::Flatten(isolate, s);
Frank Tang's avatar
Frank Tang committed
82
  RETURN_RESULT_OR_FAILURE(isolate, Intl::ConvertToLower(isolate, s));
83 84
}

85
RUNTIME_FUNCTION(Runtime_StringToUpperCaseIntl) {
86 87 88
  HandleScope scope(isolate);
  DCHECK_EQ(args.length(), 1);
  CONVERT_ARG_HANDLE_CHECKED(String, s, 0);
89
  s = String::Flatten(isolate, s);
Frank Tang's avatar
Frank Tang committed
90
  RETURN_RESULT_OR_FAILURE(isolate, Intl::ConvertToUpper(isolate, s));
91 92
}

93 94
}  // namespace internal
}  // namespace v8