V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
builtins-intl-gen.cc
1 // Copyright 2017 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_INTL_SUPPORT
6 #error Internationalization is expected to be enabled.
7 #endif // V8_INTL_SUPPORT
8 
9 #include "src/builtins/builtins-iterator-gen.h"
10 #include "src/builtins/builtins-utils-gen.h"
11 #include "src/code-stub-assembler.h"
12 #include "src/objects-inl.h"
13 #include "src/objects.h"
14 #include "src/objects/js-list-format-inl.h"
15 #include "src/objects/js-list-format.h"
16 
17 namespace v8 {
18 namespace internal {
19 
20 template <class T>
21 using TNode = compiler::TNode<T>;
22 
24  public:
26  : CodeStubAssembler(state) {}
27 
28  void ListFormatCommon(TNode<Context> context, TNode<Int32T> argc,
29  Runtime::FunctionId format_func_id,
30  const char* method_name);
31 
32  TNode<JSArray> AllocateEmptyJSArray(TNode<Context> context);
33 };
34 
35 TF_BUILTIN(StringToLowerCaseIntl, IntlBuiltinsAssembler) {
36  Node* const string = Parameter(Descriptor::kString);
37  Node* const context = Parameter(Descriptor::kContext);
38 
39  CSA_ASSERT(this, IsString(string));
40 
41  Label call_c(this), return_string(this), runtime(this, Label::kDeferred);
42 
43  // Early exit on empty strings.
44  TNode<Uint32T> const length = LoadStringLengthAsWord32(string);
45  GotoIf(Word32Equal(length, Uint32Constant(0)), &return_string);
46 
47  // Unpack strings if possible, and bail to runtime unless we get a one-byte
48  // flat string.
49  ToDirectStringAssembler to_direct(
50  state(), string, ToDirectStringAssembler::kDontUnpackSlicedStrings);
51  to_direct.TryToDirect(&runtime);
52 
53  Node* const instance_type = to_direct.instance_type();
54  CSA_ASSERT(this,
55  Word32BinaryNot(IsIndirectStringInstanceType(instance_type)));
56  GotoIfNot(IsOneByteStringInstanceType(instance_type), &runtime);
57 
58  // For short strings, do the conversion in CSA through the lookup table.
59 
60  Node* const dst = AllocateSeqOneByteString(context, length);
61 
62  const int kMaxShortStringLength = 24; // Determined empirically.
63  GotoIf(Uint32GreaterThan(length, Uint32Constant(kMaxShortStringLength)),
64  &call_c);
65 
66  {
67  Node* const dst_ptr = PointerToSeqStringData(dst);
68  VARIABLE(var_cursor, MachineType::PointerRepresentation(),
69  IntPtrConstant(0));
70 
71  Node* const start_address = to_direct.PointerToData(&call_c);
72  TNode<IntPtrT> const end_address =
73  Signed(IntPtrAdd(start_address, ChangeUint32ToWord(length)));
74 
75  Node* const to_lower_table_addr =
76  ExternalConstant(ExternalReference::intl_to_latin1_lower_table());
77 
78  VARIABLE(var_did_change, MachineRepresentation::kWord32, Int32Constant(0));
79 
80  VariableList push_vars({&var_cursor, &var_did_change}, zone());
81  BuildFastLoop(push_vars, start_address, end_address,
82  [=, &var_cursor, &var_did_change](Node* current) {
83  Node* c = Load(MachineType::Uint8(), current);
84  Node* lower =
85  Load(MachineType::Uint8(), to_lower_table_addr,
86  ChangeInt32ToIntPtr(c));
87  StoreNoWriteBarrier(MachineRepresentation::kWord8, dst_ptr,
88  var_cursor.value(), lower);
89 
90  var_did_change.Bind(Word32Or(Word32NotEqual(c, lower),
91  var_did_change.value()));
92 
93  Increment(&var_cursor);
94  },
95  kCharSize, INTPTR_PARAMETERS, IndexAdvanceMode::kPost);
96 
97  // Return the original string if it remained unchanged in order to preserve
98  // e.g. internalization and private symbols (such as the preserved object
99  // hash) on the source string.
100  GotoIfNot(var_did_change.value(), &return_string);
101 
102  Return(dst);
103  }
104 
105  // Call into C for case conversion. The signature is:
106  // String ConvertOneByteToLower(String src, String dst);
107  BIND(&call_c);
108  {
109  Node* const src = to_direct.string();
110 
111  Node* const function_addr =
112  ExternalConstant(ExternalReference::intl_convert_one_byte_to_lower());
113 
114  MachineType type_tagged = MachineType::AnyTagged();
115 
116  Node* const result = CallCFunction2(type_tagged, type_tagged, type_tagged,
117  function_addr, src, dst);
118 
119  Return(result);
120  }
121 
122  BIND(&return_string);
123  Return(string);
124 
125  BIND(&runtime);
126  {
127  Node* const result = CallRuntime(Runtime::kStringToLowerCaseIntl,
128  NoContextConstant(), string);
129  Return(result);
130  }
131 }
132 
133 TF_BUILTIN(StringPrototypeToLowerCaseIntl, IntlBuiltinsAssembler) {
134  Node* const maybe_string = Parameter(Descriptor::kReceiver);
135  Node* const context = Parameter(Descriptor::kContext);
136 
137  Node* const string =
138  ToThisString(context, maybe_string, "String.prototype.toLowerCase");
139 
140  Return(CallBuiltin(Builtins::kStringToLowerCaseIntl, context, string));
141 }
142 
143 void IntlBuiltinsAssembler::ListFormatCommon(TNode<Context> context,
144  TNode<Int32T> argc,
145  Runtime::FunctionId format_func_id,
146  const char* method_name) {
147  CodeStubArguments args(this, ChangeInt32ToIntPtr(argc));
148 
149  // Label has_list(this);
150  // 1. Let lf be this value.
151  // 2. If Type(lf) is not Object, throw a TypeError exception.
152  TNode<Object> receiver = args.GetReceiver();
153 
154  // 3. If lf does not have an [[InitializedListFormat]] internal slot, throw a
155  // TypeError exception.
156  ThrowIfNotInstanceType(context, receiver, JS_INTL_LIST_FORMAT_TYPE,
157  method_name);
158  TNode<JSListFormat> list_format = CAST(receiver);
159 
160  // 4. If list is not provided or is undefined, then
161  TNode<Object> list = args.GetOptionalArgumentValue(0);
162  Label has_list(this);
163  {
164  GotoIfNot(IsUndefined(list), &has_list);
165  if (format_func_id == Runtime::kFormatList) {
166  // a. Return an empty String.
167  args.PopAndReturn(EmptyStringConstant());
168  } else {
169  DCHECK_EQ(format_func_id, Runtime::kFormatListToParts);
170  // a. Return an empty Array.
171  args.PopAndReturn(AllocateEmptyJSArray(context));
172  }
173  }
174  BIND(&has_list);
175  {
176  // 5. Let x be ? IterableToList(list).
177  TNode<Object> x =
178  CallBuiltin(Builtins::kIterableToListWithSymbolLookup, context, list);
179 
180  // 6. Return ? FormatList(lf, x).
181  args.PopAndReturn(CallRuntime(format_func_id, context, list_format, x));
182  }
183 }
184 
185 TNode<JSArray> IntlBuiltinsAssembler::AllocateEmptyJSArray(
186  TNode<Context> context) {
187  return CodeStubAssembler::AllocateJSArray(
188  PACKED_ELEMENTS,
189  LoadJSArrayElementsMap(PACKED_ELEMENTS, LoadNativeContext(context)),
190  SmiConstant(0), SmiConstant(0));
191 }
192 
193 TF_BUILTIN(ListFormatPrototypeFormat, IntlBuiltinsAssembler) {
194  ListFormatCommon(
195  CAST(Parameter(Descriptor::kContext)),
196  UncheckedCast<Int32T>(Parameter(Descriptor::kJSActualArgumentsCount)),
197  Runtime::kFormatList, "Intl.ListFormat.prototype.format");
198 }
199 
200 TF_BUILTIN(ListFormatPrototypeFormatToParts, IntlBuiltinsAssembler) {
201  ListFormatCommon(
202  CAST(Parameter(Descriptor::kContext)),
203  UncheckedCast<Int32T>(Parameter(Descriptor::kJSActualArgumentsCount)),
204  Runtime::kFormatListToParts, "Intl.ListFormat.prototype.formatToParts");
205 }
206 
207 } // namespace internal
208 } // namespace v8
Definition: libplatform.h:13