array-of.tq 1.44 KB
Newer Older
1 2 3 4
// Copyright 2018 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.

5
namespace array {
6 7 8 9 10 11 12 13 14 15 16
// https://tc39.github.io/ecma262/#sec-array.of
transitioning javascript builtin
ArrayOf(
    js-implicit context: NativeContext, receiver: JSAny)(...arguments): JSAny {
  // 1. Let len be the actual number of arguments passed to this function.
  const len: Smi = Convert<Smi>(arguments.length);

  // 2. Let items be the List of arguments passed to this function.
  const items: Arguments = arguments;

  // 3. Let C be the this value.
17
  const c: JSAny = HasBuiltinSubclassingFlag() ? receiver : GetArrayFunction();
18 19 20 21 22 23 24 25

  let a: JSReceiver;

  // 4. If IsConstructor(C) is true, then
  typeswitch (c) {
    case (c: Constructor): {
      // a. Let A be ? Construct(C, « len »).
      a = Construct(c, len);
26
    }
27 28 29 30 31
    case (JSAny): {
      // a. Let A be ? ArrayCreate(len).
      a = ArrayCreate(len);
    }
  }
32

33 34
  // 6. Let k be 0.
  let k: Smi = 0;
35

36 37 38 39
  // 7. Repeat, while k < len
  while (k < len) {
    // a. Let kValue be items[k].
    const kValue: JSAny = items[Convert<intptr>(k)];
40

41 42 43
    // b. Let Pk be ! ToString(k).
    // c. Perform ? CreateDataPropertyOrThrow(A, Pk, kValue).
    FastCreateDataProperty(a, k, kValue);
44

45 46 47
    // d. Increase k by 1.
    k++;
  }
48

49 50
  // 8. Perform ? Set(A, "length", len, true).
  array::SetPropertyLength(a, len);
51

52 53 54
  // 9. Return A.
  return a;
}
55
}