• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2021 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#include 'src/builtins/builtins-string-gen.h'
6
7namespace string {
8
9// https://tc39.es/ecma262/#sec-string.prototype.includes
10transitioning javascript builtin
11StringPrototypeIncludes(js-implicit context: NativeContext, receiver: JSAny)(
12    ...arguments): Boolean {
13  const methodName: constexpr string = 'String.prototype.includes';
14  const searchString: JSAny = arguments[0];
15  const position: JSAny = arguments[1];
16
17  // 1. Let O be ? RequireObjectCoercible(this value).
18  // 2. Let S be ? ToString(O).
19  const s = ToThisString(receiver, methodName);
20
21  // 3. Let isRegExp be ? IsRegExp(searchString).
22  // 4. If isRegExp is true, throw a TypeError exception.
23  if (regexp::IsRegExp(searchString)) {
24    ThrowTypeError(MessageTemplate::kFirstArgumentNotRegExp, methodName);
25  }
26
27  // 5. Let searchStr be ? ToString(searchString).
28  const searchStr = ToString_Inline(searchString);
29
30  // 6. Let pos be ? ToIntegerOrInfinity(position).
31  // 7. Assert: If position is undefined, then pos is 0.
32  let start: Smi = 0;
33  if (position != Undefined) {
34    // 8. Let len be the length of S.
35    const len = s.length_uintptr;
36
37    // 9. Let start be the result of clamping pos between 0 and len.
38    StaticAssertStringLengthFitsSmi();
39    start = Convert<Smi>(Signed(ClampToIndexRange(position, len)));
40  }
41
42  // 10. Let index be ! StringIndexOf(S, searchStr, start).
43  const index = StringIndexOf(s, searchStr, start);
44
45  // 11. If index is not -1, return true.
46  // 12. Return false.
47  return index != -1 ? True : False;
48}
49}
50