• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2019 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-typed-array-gen.h'
6
7namespace typed_array {
8const kBuiltinNameOf: constexpr string = '%TypedArray%.of';
9
10// %TypedArray%.of ( ...items )
11// https://tc39.github.io/ecma262/#sec-%typedarray%.of
12transitioning javascript builtin
13TypedArrayOf(js-implicit context: NativeContext, receiver: JSAny)(...arguments):
14    JSTypedArray {
15  try {
16    // 1. Let len be the actual number of arguments passed to this function.
17    const len: uintptr = Unsigned(arguments.length);
18
19    // 2. Let items be the List of arguments passed to this function.
20
21    // 3. Let C be the this value.
22    // 4. If IsConstructor(C) is false, throw a TypeError exception.
23    const constructor = Cast<Constructor>(receiver) otherwise NotConstructor;
24
25    // 5. Let newObj be ? TypedArrayCreate(C, len).
26    const newObj = TypedArrayCreateByLength(
27        constructor, Convert<Number>(len), kBuiltinNameOf);
28
29    const accessor: TypedArrayAccessor =
30        GetTypedArrayAccessor(newObj.elements_kind);
31
32    // 6. Let k be 0.
33    // 7. Repeat, while k < len
34    for (let k: uintptr = 0; k < len; k++) {
35      // 7a. Let kValue be items[k].
36      const kValue: JSAny = arguments[Signed(k)];
37
38      // 7b. Let Pk be ! ToString(k).
39      // 7c. Perform ? Set(newObj, Pk, kValue, true).
40      // Buffer may be detached during executing ToNumber/ToBigInt.
41      accessor.StoreJSAny(context, newObj, k, kValue) otherwise IfDetached;
42
43      // 7d. Increase k by 1. (done by the loop).
44    }
45
46    // 8. Return newObj.
47    return newObj;
48  } label NotConstructor deferred {
49    ThrowTypeError(MessageTemplate::kNotConstructor, receiver);
50  } label IfDetached deferred {
51    ThrowTypeError(MessageTemplate::kDetachedOperation, kBuiltinNameOf);
52  }
53}
54}
55