typed-array-of.tq 1.86 KB
Newer Older
1 2 3 4 5 6 7
// 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 {
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
const kBuiltinNameOf: constexpr string = '%TypedArray%.of';

// %TypedArray%.of ( ...items )
// https://tc39.github.io/ecma262/#sec-%typedarray%.of
transitioning javascript builtin
TypedArrayOf(js-implicit context: NativeContext, receiver: JSAny)(...arguments):
    JSTypedArray {
  try {
    // 1. Let len be the actual number of arguments passed to this function.
    const len: uintptr = Unsigned(arguments.length);

    // 2. Let items be the List of arguments passed to this function.

    // 3. Let C be the this value.
    // 4. If IsConstructor(C) is false, throw a TypeError exception.
    const constructor = Cast<Constructor>(receiver) otherwise NotConstructor;

    // 5. Let newObj be ? TypedArrayCreate(C, len).
    const newObj = TypedArrayCreateByLength(
        constructor, Convert<Number>(len), kBuiltinNameOf);

    const accessor: TypedArrayAccessor =
        GetTypedArrayAccessor(newObj.elements_kind);

    // 6. Let k be 0.
    // 7. Repeat, while k < len
    for (let k: uintptr = 0; k < len; k++) {
      // 7a. Let kValue be items[k].
      const kValue: JSAny = arguments[Signed(k)];

      // 7b. Let Pk be ! ToString(k).
      // 7c. Perform ? Set(newObj, Pk, kValue, true).
      // Buffer may be detached during executing ToNumber/ToBigInt.
      accessor.StoreJSAny(context, newObj, k, kValue) otherwise IfDetached;

      // 7d. Increase k by 1. (done by the loop).
44
    }
45 46 47 48 49 50 51

    // 8. Return newObj.
    return newObj;
  } label NotConstructor deferred {
    ThrowTypeError(MessageTemplate::kNotConstructor, receiver);
  } label IfDetached deferred {
    ThrowTypeError(MessageTemplate::kDetachedOperation, kBuiltinNameOf);
52 53
  }
}
54
}