// Copyright 2019 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.

#include 'src/builtins/builtins-typed-array-gen.h'

namespace typed_array {
const kBuiltinNameReduceRight: constexpr string =
    '%TypedArray%.prototype.reduceRight';

// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.reduceright
transitioning macro ReduceRightAllElements(implicit context: Context)(
    array: typed_array::AttachedJSTypedArray, callbackfn: Callable,
    initialValue: JSAny|TheHole): JSAny {
  let witness = typed_array::NewAttachedJSTypedArrayWitness(array);
  const length: uintptr = witness.Get().length;
  let accumulator = initialValue;
  for (let k: uintptr = length; k-- > 0;) {
    let value: JSAny;
    try {
      witness.Recheck()
          otherwise goto IsDetached;
      value = witness.Load(k);
    } label IsDetached deferred {
      value = Undefined;
    }
    typeswitch (accumulator) {
      case (TheHole): {
        accumulator = value;
      }
      case (accumulatorNotHole: JSAny): {
        // TODO(v8:4153): Consider versioning this loop for Smi and non-Smi
        // indices to optimize Convert<Number>(k) for the most common case.
        accumulator = Call(
            context, callbackfn, Undefined, accumulatorNotHole, value,
            Convert<Number>(k), witness.GetStable());
      }
    }
  }
  typeswitch (accumulator) {
    case (TheHole): {
      ThrowTypeError(
          MessageTemplate::kReduceNoInitial, kBuiltinNameReduceRight);
    }
    case (accumulator: JSAny): {
      return accumulator;
    }
  }
}

// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.reduceright
transitioning javascript builtin
TypedArrayPrototypeReduceRight(
    js-implicit context: NativeContext, receiver: JSAny)(...arguments): JSAny {
  // arguments[0] = callback
  // arguments[1] = initialValue.
  try {
    const array: JSTypedArray = Cast<JSTypedArray>(receiver)
        otherwise NotTypedArray;
    const uarray = typed_array::EnsureAttached(array) otherwise IsDetached;

    const callbackfn = Cast<Callable>(arguments[0]) otherwise NotCallable;
    const initialValue = arguments.length >= 2 ? arguments[1] : TheHole;

    return ReduceRightAllElements(uarray, callbackfn, initialValue);
  } label NotCallable deferred {
    ThrowTypeError(MessageTemplate::kCalledNonCallable, arguments[0]);
  } label NotTypedArray deferred {
    ThrowTypeError(MessageTemplate::kNotTypedArray, kBuiltinNameReduceRight);
  } label IsDetached deferred {
    ThrowTypeError(
        MessageTemplate::kDetachedOperation, kBuiltinNameReduceRight);
  }
}
}