LCOV - code coverage report
Current view: top level - src/builtins - builtins-intl-gen.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 68 68 100.0 %
Date: 2019-01-20 Functions: 13 13 100.0 %

          Line data    Source code
       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             : 
      23             : class IntlBuiltinsAssembler : public CodeStubAssembler {
      24             :  public:
      25             :   explicit IntlBuiltinsAssembler(compiler::CodeAssemblerState* state)
      26         224 :       : 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         224 : 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          56 :   Label call_c(this), return_string(this), runtime(this, Label::kDeferred);
      42             : 
      43             :   // Early exit on empty strings.
      44          56 :   TNode<Uint32T> const length = LoadStringLengthAsWord32(string);
      45         112 :   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         112 :       state(), string, ToDirectStringAssembler::kDontUnpackSlicedStrings);
      51          56 :   to_direct.TryToDirect(&runtime);
      52             : 
      53             :   Node* const instance_type = to_direct.instance_type();
      54             :   CSA_ASSERT(this,
      55             :              Word32BinaryNot(IsIndirectStringInstanceType(instance_type)));
      56         112 :   GotoIfNot(IsOneByteStringInstanceType(instance_type), &runtime);
      57             : 
      58             :   // For short strings, do the conversion in CSA through the lookup table.
      59             : 
      60         112 :   Node* const dst = AllocateSeqOneByteString(context, length);
      61             : 
      62             :   const int kMaxShortStringLength = 24;  // Determined empirically.
      63          56 :   GotoIf(Uint32GreaterThan(length, Uint32Constant(kMaxShortStringLength)),
      64         112 :          &call_c);
      65             : 
      66             :   {
      67          56 :     Node* const dst_ptr = PointerToSeqStringData(dst);
      68         112 :     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         112 :         Signed(IntPtrAdd(start_address, ChangeUint32ToWord(length)));
      74             : 
      75             :     Node* const to_lower_table_addr =
      76         112 :         ExternalConstant(ExternalReference::intl_to_latin1_lower_table());
      77             : 
      78         168 :     VARIABLE(var_did_change, MachineRepresentation::kWord32, Int32Constant(0));
      79             : 
      80         112 :     VariableList push_vars({&var_cursor, &var_did_change}, zone());
      81             :     BuildFastLoop(push_vars, start_address, end_address,
      82          56 :                   [=, &var_cursor, &var_did_change](Node* current) {
      83          56 :                     Node* c = Load(MachineType::Uint8(), current);
      84             :                     Node* lower =
      85             :                         Load(MachineType::Uint8(), to_lower_table_addr,
      86         112 :                              ChangeInt32ToIntPtr(c));
      87             :                     StoreNoWriteBarrier(MachineRepresentation::kWord8, dst_ptr,
      88          56 :                                         var_cursor.value(), lower);
      89             : 
      90         112 :                     var_did_change.Bind(Word32Or(Word32NotEqual(c, lower),
      91         280 :                                                  var_did_change.value()));
      92             : 
      93          56 :                     Increment(&var_cursor);
      94          56 :                   },
      95         112 :                   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         112 :     GotoIfNot(var_did_change.value(), &return_string);
     101             : 
     102         112 :     Return(dst);
     103             :   }
     104             : 
     105             :   // Call into C for case conversion. The signature is:
     106             :   // String ConvertOneByteToLower(String src, String dst);
     107          56 :   BIND(&call_c);
     108             :   {
     109             :     Node* const src = to_direct.string();
     110             : 
     111             :     Node* const function_addr =
     112         112 :         ExternalConstant(ExternalReference::intl_convert_one_byte_to_lower());
     113             : 
     114          56 :     MachineType type_tagged = MachineType::AnyTagged();
     115             : 
     116             :     Node* const result = CallCFunction2(type_tagged, type_tagged, type_tagged,
     117          56 :                                         function_addr, src, dst);
     118             : 
     119          56 :     Return(result);
     120             :   }
     121             : 
     122          56 :   BIND(&return_string);
     123          56 :   Return(string);
     124             : 
     125          56 :   BIND(&runtime);
     126             :   {
     127             :     Node* const result = CallRuntime(Runtime::kStringToLowerCaseIntl,
     128         112 :                                      NoContextConstant(), string);
     129          56 :     Return(result);
     130          56 :   }
     131          56 : }
     132             : 
     133         224 : 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         112 :       ToThisString(context, maybe_string, "String.prototype.toLowerCase");
     139             : 
     140         112 :   Return(CallBuiltin(Builtins::kStringToLowerCaseIntl, context, string));
     141          56 : }
     142             : 
     143         112 : void IntlBuiltinsAssembler::ListFormatCommon(TNode<Context> context,
     144             :                                              TNode<Int32T> argc,
     145             :                                              Runtime::FunctionId format_func_id,
     146             :                                              const char* method_name) {
     147         336 :   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         112 :   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         112 :                          method_name);
     158             :   TNode<JSListFormat> list_format = CAST(receiver);
     159             : 
     160             :   // 4. If list is not provided or is undefined, then
     161         112 :   TNode<Object> list = args.GetOptionalArgumentValue(0);
     162             :   Label has_list(this);
     163             :   {
     164         224 :     GotoIfNot(IsUndefined(list), &has_list);
     165         112 :     if (format_func_id == Runtime::kFormatList) {
     166             :       // a. Return an empty String.
     167         112 :       args.PopAndReturn(EmptyStringConstant());
     168             :     } else {
     169             :       DCHECK_EQ(format_func_id, Runtime::kFormatListToParts);
     170             :       // a. Return an empty Array.
     171         112 :       args.PopAndReturn(AllocateEmptyJSArray(context));
     172             :     }
     173             :   }
     174         112 :   BIND(&has_list);
     175             :   {
     176             :     // 5. Let x be ? IterableToList(list).
     177             :     TNode<Object> x =
     178         112 :         CallBuiltin(Builtins::kIterableToListWithSymbolLookup, context, list);
     179             : 
     180             :     // 6. Return ? FormatList(lf, x).
     181         112 :     args.PopAndReturn(CallRuntime(format_func_id, context, list_format, x));
     182         112 :   }
     183         112 : }
     184             : 
     185          56 : TNode<JSArray> IntlBuiltinsAssembler::AllocateEmptyJSArray(
     186             :     TNode<Context> context) {
     187             :   return CodeStubAssembler::AllocateJSArray(
     188             :       PACKED_ELEMENTS,
     189         112 :       LoadJSArrayElementsMap(PACKED_ELEMENTS, LoadNativeContext(context)),
     190         280 :       SmiConstant(0), SmiConstant(0));
     191             : }
     192             : 
     193         224 : TF_BUILTIN(ListFormatPrototypeFormat, IntlBuiltinsAssembler) {
     194             :   ListFormatCommon(
     195             :       CAST(Parameter(Descriptor::kContext)),
     196             :       UncheckedCast<Int32T>(Parameter(Descriptor::kJSActualArgumentsCount)),
     197          56 :       Runtime::kFormatList, "Intl.ListFormat.prototype.format");
     198          56 : }
     199             : 
     200         224 : TF_BUILTIN(ListFormatPrototypeFormatToParts, IntlBuiltinsAssembler) {
     201             :   ListFormatCommon(
     202             :       CAST(Parameter(Descriptor::kContext)),
     203             :       UncheckedCast<Int32T>(Parameter(Descriptor::kJSActualArgumentsCount)),
     204          56 :       Runtime::kFormatListToParts, "Intl.ListFormat.prototype.formatToParts");
     205          56 : }
     206             : 
     207             : }  // namespace internal
     208       94089 : }  // namespace v8

Generated by: LCOV version 1.10