• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2020 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
5namespace string {
6// https://tc39.es/proposal-item-method/#sec-string.prototype.at
7transitioning javascript builtin StringPrototypeAt(
8    js-implicit context: NativeContext, receiver: JSAny)(index: JSAny): JSAny {
9  // 1. Let O be ? RequireObjectCoercible(this value).
10  // 2. Let S be ? ToString(O).
11  const s = ToThisString(receiver, 'String.prototype.at');
12  // 3. Let len be the length of S.
13  const len = s.length_smi;
14  // 4. Let relativeIndex be ? ToInteger(index).
15  const relativeIndex = ToInteger_Inline(index);
16  // 5. If relativeIndex ≥ 0, then
17  //   a. Let k be relativeIndex.
18  // 6. Else,
19  //   a. Let k be len + relativeIndex.
20  const k = relativeIndex >= 0 ? relativeIndex : len + relativeIndex;
21  // 7. If k < 0 or k ≥ len, then return undefined.
22  if (k < 0 || k >= len) {
23    return Undefined;
24  }
25  // 8. Return the String value consisting of only the code unit at position k
26  // in S.
27  return StringFromSingleCharCode(StringCharCodeAt(s, Convert<uintptr>(k)));
28}
29}
30