array-at.tq 1.34 KB
Newer Older
1 2 3 4 5
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

namespace array {
6 7 8 9 10 11 12 13
macro ConvertRelativeIndex(index: Number, length: Number):
    Number labels OutOfBoundsLow, OutOfBoundsHigh {
  const relativeIndex = index >= 0 ? index : length + index;
  if (relativeIndex < 0) goto OutOfBoundsLow;
  if (relativeIndex >= length) goto OutOfBoundsHigh;
  return relativeIndex;
}

14 15 16 17 18
// https://tc39.es/proposal-item-method/#sec-array.prototype.at
transitioning javascript builtin ArrayPrototypeAt(
    js-implicit context: NativeContext, receiver: JSAny)(index: JSAny): JSAny {
  // 1. Let O be ? ToObject(this value).
  const o = ToObject_Inline(context, receiver);
19

20 21
  // 2. Let len be ? LengthOfArrayLike(O).
  const len = GetLengthProperty(o);
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

  try {
    // 3. Let relativeIndex be ? ToInteger(index).
    const relativeIndex = ToInteger_Inline(index);

    // 4. If relativeIndex ≥ 0, then
    //   a. Let k be relativeIndex.
    // 5. Else,
    //   a. Let k be len + relativeIndex.
    const k = ConvertRelativeIndex(relativeIndex, len) otherwise OutOfBounds,
          OutOfBounds;

    // 7. Return ? Get(O, ! ToString(k)).
    return GetProperty(o, k);
  } label OutOfBounds {
    // 6. If k < 0 or k ≥ len, then return undefined.
38 39 40 41
    return Undefined;
  }
}
}