• 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.indexof
10transitioning javascript builtin
11StringPrototypeIndexOf(
12    js-implicit context: NativeContext, receiver: JSAny)(...arguments): Smi {
13  const methodName: constexpr string = 'String.prototype.indexOf';
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 searchStr be ? ToString(searchString).
22  const searchStr = ToString_Inline(searchString);
23
24  // 4. Let pos be ? ToIntegerOrInfinity(position).
25  // 5. Assert: If position is undefined, then pos is 0.
26  let start: Smi = 0;
27  if (position != Undefined) {
28    // 6. Let len be the length of S.
29    const len = s.length_uintptr;
30
31    // 7. Let start be the result of clamping pos between 0 and len.
32    StaticAssertStringLengthFitsSmi();
33    start = Convert<Smi>(Signed(ClampToIndexRange(position, len)));
34  }
35
36  // 8. Let index be ! StringIndexOf(S, searchStr, start).
37  return StringIndexOf(s, searchStr, start);
38}
39}
40